Skip to content

Commit

Permalink
Fix counts provided for specimens without cells in the selected cell …
Browse files Browse the repository at this point in the history
…list (#252)

* no cells selected in specimen return None

* adjust none condition

* forgot to add another None type to PhenotypeCount

* update a type hint
  • Loading branch information
CarlinLiao authored Nov 20, 2023
1 parent de1dc5c commit 01838ec
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions spatialprofilingtoolbox/db/exchange_data_formats/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class PhenotypeCount(BaseModel):
belonging to some specific class.
"""
specimen: str
count: int
percentage: float
count: int | None
percentage: float | None


class PhenotypeCounts(BaseModel):
Expand Down
10 changes: 5 additions & 5 deletions spatialprofilingtoolbox/ondemand/providers/counts_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def count_structures_of_partial_signed_signature(
negatives_signature: int,
measurement_study: str,
cells_selected: tuple[int, ...] | None = None,
) -> dict[str, list[int]]:
) -> dict[str, tuple[int, int] | tuple[None, None]]:
"""Count the number of structures per specimen that match this signature."""
counts: dict[str, list[int]] = {}
counts: dict[str, tuple[int, int] | tuple[None, None]] = {}
selection = Index(list(cells_selected)) if cells_selected else None
for specimen, data_array in self.data_arrays[measurement_study].items():
count = 0
Expand All @@ -48,10 +48,10 @@ def count_structures_of_partial_signed_signature(
data_array.loc[data_array.index.intersection(selection), 'integer'],
)
if len(integers) == 0:
message = 'Cell subselection %s resulted in no cells for specimen %s'
raise ValueError(message, cells_selected, specimen)
counts[specimen] = (None, None)
continue
count = self.get_count(integers, positives_signature, negatives_signature)
counts[specimen] = [count, len(integers)]
counts[specimen] = (count, len(integers))
return counts

def get_count(self, integers: list[int], positives_mask: int, negatives_mask: int) -> int:
Expand Down
2 changes: 1 addition & 1 deletion spatialprofilingtoolbox/ondemand/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _get_counts(
positives_signature: int,
negatives_signature: int,
cells_selected: set[int] | None = None,
) -> dict[str, list[int]]:
) -> dict[str, tuple[int, int] | tuple[None, None]]:
assert self.server.providers.counts is not None
measurement_study = self._get_measurement_study(study)
return self.server.providers.counts.count_structures_of_partial_signed_signature(
Expand Down
1 change: 1 addition & 0 deletions spatialprofilingtoolbox/ondemand/service_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def get_counts_by_specimen(
specimen=specimen,
count=count,
percentage=self._fancy_round(count / count_all_in_specimen)
if ((count is not None) and (count_all_in_specimen not in {0, None})) else None,
)
for specimen, (count, count_all_in_specimen) in response.items()
],
Expand Down

0 comments on commit 01838ec

Please sign in to comment.