diff --git a/ktplotspy/plot/plot_cpdb_chord.py b/ktplotspy/plot/plot_cpdb_chord.py index 3619db7..375fb21 100644 --- a/ktplotspy/plot/plot_cpdb_chord.py +++ b/ktplotspy/plot/plot_cpdb_chord.py @@ -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]) diff --git a/ktplotspy/utils/support.py b/ktplotspy/utils/support.py index 4eb7b13..a222cf5 100644 --- a/ktplotspy/utils/support.py +++ b/ktplotspy/utils/support.py @@ -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 @@ -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) @@ -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)