Skip to content

Commit

Permalink
fixed error caused by sparse matrix in computing variance explained
Browse files Browse the repository at this point in the history
  • Loading branch information
Helen Yihua Kang committed Jun 12, 2023
1 parent 1f1542e commit a577ee6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion workflow/scripts/variance_explained_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## compute variance explained for cNMF components

import numpy as np
import scipy
import pandas as pd
# import scipy.sparse as sp
import scanpy as sc
Expand Down Expand Up @@ -31,8 +32,15 @@
# parser.add_argument('--k', dest = 'k', type=int, help = 'number of components', default='35')
# parser.add_argument('--density_threshold', dest = 'density_threshold', type=float, help = 'component spectra clustering threshold, 2 for no filtering, recommend 0_2 (means 0.2)', default="0.2")


args = parser.parse_args()

# ## sdev debug for Disha's error
# args.X_normalized = "/oak/stanford/groups/engreitz/Users/kangh/scratch_space/230612_debug_cNMF_pipeline_variance_explained/Merge_Pauletal_subset_SMC.norm_counts.h5ad"
# args.outdir = "/oak/stanford/groups/engreitz/Users/kangh/scratch_space/230612_debug_cNMF_pipeline_variance_explained/"



sample = args.topic_sampleName
# output_sample = args.output_sampleName
# tpm_counts_path = args.tpm_counts_path
Expand All @@ -57,7 +65,10 @@

## functions
def compute_Var(X):
return np.sum(np.var(X, axis=0, ddof=1))
if scipy.sparse.issparse(X):
return np.sum(np.var(X.todense(), axis=0, ddof=1))
else:
return np.sum(np.var(X, axis=0, ddof=1))

# ## first turn X into TPM
# X_tpm_dense = X_original.X.todense()
Expand Down

0 comments on commit a577ee6

Please sign in to comment.