Skip to content

Commit

Permalink
Fix two some small problems (#78)
Browse files Browse the repository at this point in the history
* Fix handling of sparse matrices

* Fix zgene bug
  • Loading branch information
nictru authored Sep 8, 2024
1 parent c54c399 commit 998f26b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ktplotspy/plot/plot_cpdb_chord.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def plot_cpdb_chord(
zgenes = list(set(decon_subset_expr.index))
for ct, adat in adata_list_alt.items():
for zg in zgenes:
cellfrac[ct][zg] = np.mean(adat[:, adata.var_names.isin(zg.split("_"))].X > 0)
zg_mask = adata.var_names.isin(zg.split("_"))
cellfrac[ct][zg] = np.mean(adat[:, zg_mask].X > 0) if np.sum(zg_mask) > 0 else 0
decon_subset_fraction = pd.DataFrame(cellfrac)
expr_df = pd.concat([adata_list2, decon_subset_expr])
fraction_df = pd.concat([adata_list3, decon_subset_fraction])
Expand Down
18 changes: 9 additions & 9 deletions ktplotspy/utils/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections import Counter
from itertools import count, tee
from matplotlib.colors import ListedColormap
from scipy.sparse import csr_matrix
from scipy.sparse import issparse
from typing import Dict, List, Optional


Expand Down Expand Up @@ -538,13 +538,13 @@ def celltype_means(adata: "AnnData", layer: Optional[str] = None) -> np.ndarray:
mean expression.
"""
if layer is None:
if isinstance(adata.X, csr_matrix):
return np.mean(adata.X.toarray(), axis=0)
if issparse(adata.X):
return adata.X.mean(axis=0).A1
else: # assume it's numpy array
return np.mean(adata.X, axis=0) # pragma: no cover
else:
if isinstance(adata.layers[layer], csr_matrix):
return np.mean(adata.layers[layer].toarray(), axis=0)
if issparse(adata.layers[layer]):
return adata.layers[layer].mean(axis=0).A1
else:
return np.mean(adata.layers[layer], axis=0)

Expand All @@ -565,13 +565,13 @@ def celltype_fraction(adata: "AnnData", layer: Optional[str] = None) -> np.ndarr
non-zero expression fraction
"""
if layer is None:
if isinstance(adata.X, csr_matrix):
return np.mean(adata.X.toarray() > 0, axis=0)
if issparse(adata.X):
return np.mean(adata.X > 0, axis=0).A1
else: # assume it's numpy array
return np.mean(adata.X > 0, axis=0) # pragma: no cover
else:
if isinstance(adata.layers[layer], csr_matrix):
return np.mean(adata.layers[layer].toarray() > 0, axis=0)
if issparse(adata.layers[layer]):
return np.mean(adata.layers[layer] > 0, axis=0).A1
else:
return np.mean(adata.layers[layer] > 0, axis=0)

Expand Down

0 comments on commit 998f26b

Please sign in to comment.