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

All zero check #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
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
Loading