Skip to content

Commit

Permalink
Merge pull request #41 from LevyNat/all_zero_check
Browse files Browse the repository at this point in the history
All zero check
  • Loading branch information
deto authored Feb 24, 2025
2 parents 42ce35c + d8e3b82 commit 1055db5
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions hotspot/hotspot.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(
# because of transpose we check if its csr
if issparse(counts) and not isinstance(counts, csr_matrix):
warnings.warn(
"Hotspot will work faster when counts are a csc sparse matrix."
"Hotspot will work faster when counts are a csr sparse matrix."
)

if tree is not None:
Expand Down Expand Up @@ -129,11 +129,24 @@ def __init__(
if model not in valid_models:
raise ValueError("Input `model` should be one of {}".format(valid_models))

valid_genes = counts.sum(axis=1) > 0
if issparse(counts):
# For a sparse matrix, check if all values in each row are identical
# A row (gene) is considered valid if it has more than one unique value.
row_min = counts.min(axis=1).toarray().flatten()
row_max = counts.max(axis=1).toarray().flatten()
valid_genes = (
row_min != row_max
) # Valid if min and max are not equal, indicating variation
else:
# For a dense matrix, check if all values in each row are identical
valid_genes = ~(np.all(counts == counts[:, [0]], axis=1))

# valid_genes is now a boolean array indicating which rows (genes) have non-identical values.

n_invalid = counts.shape[0] - valid_genes.sum()
if n_invalid > 0:
raise ValueError(
"\nDetected all zero genes. Please filter adata and reinitialize."
"\nDetected genes with zero variance. Please filter adata and reinitialize."
)

self.adata = adata
Expand Down

0 comments on commit 1055db5

Please sign in to comment.