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 two some small problems #78

Merged
merged 2 commits into from
Sep 8, 2024
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
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
Loading