welch-lab / MultiVelo

Multi-omic velocity inference
BSD 3-Clause "New" or "Revised" License
105 stars 12 forks source link

Error happened in computing velocity stream-- "ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (1, 4) + inhomogeneous part." #36

Closed wenhu0701 closed 9 months ago

wenhu0701 commented 10 months ago

Hi,

I used the example data to try reproducing the demo pipeline. With the adata_result generated, I used it for computing velocity stream and the error happened after running mv.velocity_graph(). Could you please help with figuring this out? Please find the details as followings. Thanks!

Best, Wen

adata_result ############################################################ AnnData object with n_obs × n_vars = 3365 × 865 obs: 'n_counts', 'celltype' var: 'Accession', 'Chromosome', 'End', 'Start', 'Strand', 'fit_alpha_c', 'fit_alpha', 'fit_beta', 'fit_gamma', 'fit_t_sw1', 'fit_t_sw2', 'fit_t_sw3', 'fit_scale_cc', 'fit_rescale_c', 'fit_rescale_u', 'fit_alignment_scaling', 'fit_model', 'fit_direction', 'fit_loss', 'fit_likelihood', 'fit_likelihood_c', 'fit_ssd_c', 'fit_var_c', 'fit_c0', 'fit_u0', 'fit_s0', 'fit_anchor_min_idx', 'fit_anchor_max_idx', 'fit_anchor_velo_min_idx', 'fit_anchor_velo_max_idx', 'velo_s_genes', 'velo_u_genes', 'velo_chrom_genes' uns: 'pca', 'neighbors', 'umap', 'celltype_colors', 'velo_s_params', 'velo_u_params', 'velo_chrom_params' obsm: 'X_pca', 'X_umap' varm: 'PCs', 'fit_anchor_c', 'fit_anchor_u', 'fit_anchor_s', 'fit_anchor_c_sw', 'fit_anchor_u_sw', 'fit_anchor_s_sw', 'fit_anchor_c_velo', 'fit_anchor_u_velo', 'fit_anchor_s_velo' layers: 'ambiguous', 'matrix', 'spliced', 'unspliced', 'Ms', 'Mu', 'ATAC', 'fit_t', 'fit_state', 'velo_s', 'velo_u', 'velo_chrom' obsp: 'distances', 'connectivities', '_RNA_conn', '_ATAC_conn'

Computing velocity stream and latent time

mv.velocity_graph(adata_result)

computing velocity graph (using 1/16 cores) /Users/apple/Wen/anaconda/anaconda3/envs/rstudio/lib/python3.8/site-packages/ipywidgets/widgets/widget.py:503: DeprecationWarning: The ipykernel.comm.Comm class has been deprecated. Please use the comm module instead.For creating comms, use the function from comm import create_comm. self.comm = Comm(**args) 100% 3365/3365 [00:05<00:00, 624.22cells/s]

ValueError Traceback (most recent call last) Cell In[211], line 2 1 #Computing velocity stream and latent time ----> 2 mv.velocity_graph(adata_result)

File ~/Wen/anaconda/anaconda3/envs/rstudio/lib/python3.8/site-packages/multivelo/dynamical_chrom_func.py:3504, in velocity_graph(adata, vkey, xkey, kwargs) 3502 if vkey+'_norm_genes' not in adata.var.columns: 3503 adata.var[vkey+'_norm_genes'] = adata.var[vkey+'_genes'] -> 3504 scv.tl.velocity_graph(adata, vkey=vkey+'_norm', xkey=xkey, kwargs)

File ~/Wen/anaconda/anaconda3/envs/rstudio/lib/python3.8/site-packages/scvelo/tools/velocity_graph.py:363, in velocity_graph(data, vkey, xkey, tkey, basis, n_neighbors, n_recurse_neighbors, random_neighbors_at_max, sqrt_transform, variance_stabilization, gene_subset, compute_uncertainties, approx, mode_neighbors, copy, n_jobs, backend) 359 n_jobs = get_n_jobs(n_jobs=n_jobs) 360 logg.info( 361 f"computing velocity graph (using {n_jobs}/{os.cpu_count()} cores)", r=True 362 ) --> 363 vgraph.compute_cosines(n_jobs=n_jobs, backend=backend) 365 adata.uns[f"{vkey}_graph"] = vgraph.graph 366 adata.uns[f"{vkey}_graph_neg"] = vgraph.graph_neg

File ~/Wen/anaconda/anaconda3/envs/rstudio/lib/python3.8/site-packages/scvelo/tools/velocity_graph.py:175, in VelocityGraph.compute_cosines(self, n_jobs, backend) 172 n_obs = self.X.shape[0] 174 # TODO: Use batches and vectorize calculation of dX in self._calculate_cosines --> 175 res = parallelize( 176 self._compute_cosines, 177 range(self.X.shape[0]), 178 n_jobs=n_jobs, 179 unit="cells", 180 backend=backend, 181 )() 182 uncertainties, vals, rows, cols = map(_flatten, zip(*res)) 184 vals = np.hstack(vals)

File ~/Wen/anaconda/anaconda3/envs/rstudio/lib/python3.8/site-packages/scvelo/core/_parallelize.py:138, in parallelize..wrapper(*args, *kwargs) 126 pbar, queue, thread = None, None, None 128 res = Parallel(n_jobs=n_jobs, backend=backend)( 129 delayed(callback)( 130 ((i, cs) if use_ixs else (cs,)), (...) 135 for i, cs in enumerate(collections) 136 ) --> 138 res = np.array(res) if as_array else res 139 if thread is not None: 140 thread.join()

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (1, 4) + inhomogeneous part.

jacobrepucci commented 10 months ago

Hey there,

So that I can best replicate the issue on my end, could I ask what versions of the required packages you're using?

wenhu0701 commented 10 months ago

Hi! Thanks for the reply, please find the packages as followings: python 3.8.2 multivelo = 0.1.3, pandas = 2.0.3, numpy = 1.24.4, scvelo =0.2.5, scanpy = 1.9.5, scipy = 1.10.1, matplotlib = 3.7.3

jacobrepucci commented 10 months ago

This is an issue originating with scvelo. However, as you can see in the links below, changing the python and package versions you use appears to fix the error!

https://github.com/theislab/scvelo/issues/1109

https://github.com/theislab/scvelo/issues/1121

Alternatively, you could edit the local scvelo code so that the line res = np.array(res) if as_array else res in scvelo/core/_parallelize.py is instead res = np.array(res, dtype=object) if as_array else res

jacobrepucci commented 9 months ago

Good news: this issue was fixed in the most recent release of scvelo!

I'm going to go ahead and close this issue.