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

Deprecate .A attributes of sparse matrix in Sijie july24 fix #691

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion dynamo/preprocessing/cell_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def group_corr(adata: anndata.AnnData, layer: Union[str, None], gene_list: list)
cor = (
einsum_correlation(
np.array(expression_matrix.toarray().T, dtype="float"),
np.array(avg_exp.toarray().ravel(), dtype="float"),
np.array(avg_exp.A1, dtype="float"),
)
if issparse(expression_matrix)
else einsum_correlation(
Expand Down
2 changes: 1 addition & 1 deletion dynamo/preprocessing/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _disp_calc_helper_NB_legacy(
nzGenes = (rounded > lowerDetectedLimit).sum(axis=0)
nzGenes = nzGenes > min_cells_detected

nzGenes = nzGenes.toarray().ravel() if issparse(rounded) else nzGenes
nzGenes = nzGenes.A1 if issparse(rounded) else nzGenes
if layer.startswith("X_"):
x = rounded[:, nzGenes]
else:
Expand Down
10 changes: 5 additions & 5 deletions dynamo/preprocessing/external/sctransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def gmean(
assert np.all(X.sum(0) > 0)
assert np.all(X.data > 0)
X.data[:] = np.log(X.data + eps)
res = np.exp(X.mean(axis).toarray().flatten()) - eps
res = np.exp(X.mean(axis).A.flatten()) - eps

assert np.all(res > 0)
return res
Expand Down Expand Up @@ -230,7 +230,7 @@ def sctransform_core(
X.eliminate_zeros()
gene_names = np.array(list(adata.var_names))
cell_names = np.array(list(adata.obs_names))
genes_cell_count = X.sum(0).toarray().flatten()
genes_cell_count = X.sum(0).A.flatten()
genes = np.where(genes_cell_count >= min_cells)[0]
genes_ix = genes.copy()

Expand All @@ -251,11 +251,11 @@ def sctransform_core(
genes_step1 = genes
genes_log_gmean_step1 = genes_log_gmean

umi = X.sum(1).toarray().flatten()
umi = X.sum(1).A.flatten()
log_umi = np.log10(umi)
X2 = X.copy()
X2.data[:] = 1
gene = X2.sum(1).toarray().flatten()
gene = X2.sum(1).A.flatten()
log_gene = np.log10(gene)
umi_per_gene = umi / gene
log_umi_per_gene = np.log10(umi_per_gene)
Expand Down Expand Up @@ -443,5 +443,5 @@ def sctransform(
X_squared = adata.X.copy()
X_squared.data **= 2
variance = X_squared.mean(0) - np.square(adata.X.mean(0))
adata.var["sct_score"] = variance.toarray().ravel() if sp.issparse(variance) else variance.ravel()
adata.var["sct_score"] = variance.A1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please double check if this line would work properly if variance is a sparse matrix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you've validated variance is not sparse, i've no additional comments

adata.var["use_for_pca"] = get_gene_selection_filter(adata.var["sct_score"], n_top_genes=n_top_genes)