Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix counts provided for specimens without cells in the selected cell list #252

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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