Skip to content

Commit

Permalink
Better error message when all components filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
johandahlberg committed Dec 10, 2024
1 parent 3845df2 commit 9899d56
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Clarify that `--min-size` and `--max-size` in the `annotate` stage should not be used at the same time as `--dynamic-filter`.
- Setting a lower threshold of 300 edges when `--dynamic-filter` is used in the `annotate` stage, components smaller than that will always
be filtered. Note that this can still be overridden by setting `--min-size` explicitly.
- Clarify error message when all components are filtered out in the `annotate` stage.

### Added

Expand Down
16 changes: 14 additions & 2 deletions src/pixelator/annotate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
MINIMUM_NBR_OF_CELLS_FOR_ANNOTATION,
)
from pixelator.config import AntibodyPanel
from pixelator.exceptions import PixelatorBaseException
from pixelator.graph.utils import components_metrics, edgelist_metrics
from pixelator.pixeldataset import SIZE_DEFINITION, PixelDataset
from pixelator.pixeldataset.utils import edgelist_to_anndata
Expand All @@ -37,6 +38,12 @@
logger = logging.getLogger(__name__)


class NoCellsFoundException(PixelatorBaseException):
"""Raised when no cells are found in the edge list."""

pass


def filter_components_sizes(
component_sizes: np.ndarray,
min_size: Optional[int],
Expand Down Expand Up @@ -81,7 +88,9 @@ def filter_components_sizes(
# check if none of the components pass the filters
n_components = filter_arr.sum()
if n_components == 0:
raise RuntimeError("None of the components pass the filters")
raise NoCellsFoundException(
"All cells were filtered by the size filters. Consider either setting different size filters or disabling them."
)

logger.debug(
"Filtering resulted in %i components that pass the filters",
Expand Down Expand Up @@ -199,8 +208,11 @@ def annotate_components(
].sum()

# save the components metrics (raw)
component_info_file_path = (
Path(output) / f"{output_prefix}.raw_components_metrics.csv.gz"
)
component_metrics.to_csv(
Path(output) / f"{output_prefix}.raw_components_metrics.csv.gz",
component_info_file_path,
header=True,
index=True,
sep=",",
Expand Down
31 changes: 30 additions & 1 deletion tests/annotate/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import pytest
from anndata import AnnData

from pixelator.annotate import cluster_components, filter_components_sizes
from pixelator.annotate import (
NoCellsFoundException,
cluster_components,
filter_components_sizes,
)
from pixelator.cli.annotate import annotate_components
from pixelator.config import AntibodyPanel
from pixelator.pixeldataset.utils import read_anndata
Expand Down Expand Up @@ -135,3 +139,28 @@ def test_annotate_adata(edgelist: pd.DataFrame, tmp_path: Path, panel: AntibodyP
assert (tmp_path / f"{output_prefix}.raw_components_metrics.csv.gz").is_file()
assert (tmp_path / f"{output_prefix}.annotate.dataset.pxl").is_file()
assert metrics_file.is_file()


@pytest.mark.integration_test
def test_annotate_adata_should_raise_no_cells_count_exception(
edgelist: pd.DataFrame, tmp_path: Path, panel: AntibodyPanel
):
with pytest.raises(NoCellsFoundException) as expected_exception:
output_prefix = "test_filtered"
metrics_file = tmp_path / "metrics.json"
assert not metrics_file.is_file()
tmp_edgelist_file = tmp_path / "tmp_edgelist.parquet"
edgelist.to_parquet(tmp_edgelist_file, index=False)

annotate_components(
input=str(tmp_edgelist_file),
panel=panel,
output=str(tmp_path),
output_prefix=output_prefix,
metrics_file=str(metrics_file),
min_size=100_000, # Nothing should pass this
max_size=None,
dynamic_filter=None,
verbose=True,
aggregate_calling=True,
)

0 comments on commit 9899d56

Please sign in to comment.