theislab / scvelo

RNA Velocity generalized through dynamical modeling
https://scvelo.org
BSD 3-Clause "New" or "Revised" License
409 stars 102 forks source link

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. #1109

Closed GaF123 closed 1 year ago

GaF123 commented 1 year ago

Thank you so much for your great tool!

I use seuratdisk convert rds to h5ad, and use velocyto convert bam to loom. And I merged these 2 files. and when I got to the: scv.tl.velocity_graph(adata) It shows: 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.

Here is my all codes: import anndata import scvelo as scv import pandas as pd import numpy as np import scanpy as sc import matplotlib as plt file_path = '/Users/eaiscui/Desktop/temp/mouseforvelocyto_final.h5ad' adata=sc.read_h5ad('mouseforvelocyto_final.h5ad') adata.obs.seurat_clusters=adata.obs.seurat_clusters.astype('category') ldata1 = anndata.read_loom('/Users/eaiscui/Desktop/loomfiles/Dminus_H2B.loom') ldata2 = anndata.read_loom('/Users/eaiscui/Desktop/loomfiles/Dplus_H2B.loom')

rename barcodes in order to merge:

barcodes = [bc.split(':')[1] for bc in ldata1.obs.index.tolist()] barcodes = [bc[0:len(bc)-1] + '_10' for bc in barcodes] ldata1.obs.index = barcodes barcodes = [bc.split(':')[1] for bc in ldata2.obs.index.tolist()] barcodes = [bc[0:len(bc)-1] + '_11' for bc in barcodes] ldata2.obs.index = barcodes

make variable names unique

ldata1.var_names_make_unique() ldata2.var_names_make_unique()

concatenate the three loom

ldata = ldata1.concatenate([ldata2]) scv.utils.clean_obs_names(adata) scv.utils.clean_obs_names(ldata) adata = scv.utils.merge(adata, ldata) scv.settings.verbosity = 3 # show errors(0), warnings(1), info(2), hints(3) scv.settings.presenter_view = True # set max width size for presenter view scv.set_figure_params('scvelo') # for beautified visualization scv.pl.proportions(adata) scv.pp.filter_genes(adata, min_shared_counts=20) scv.pp.normalize_per_cell(adata) scv.pp.filter_genes_dispersion(adata, n_top_genes=2000) scv.pp.log1p(adata) scv.pp.filter_and_normalize(adata, min_shared_counts=20, n_top_genes=2000) scv.pp.moments(adata, n_pcs=30, n_neighbors=30) scv.tl.velocity(adata) scv.tl.velocity_graph(adata)

Then it shows: 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.

Please help! Thank you so much!

jeffhsu3 commented 1 year ago

Similar issue but with alevin-fry data, concatenated with scanpy.

adatas = []
for sample in samples:
    adata = pyroe.load_fry(
        f"alevin_fry/{sample}/", 
        output_format="snRNA")
    adata.obs['sample'] = sample
    adatas.append(adata)

adata = sc.concat(adatas, index_unique='_')
maartenciers commented 1 year ago

It has to do with the numpy version they used. It works when you lower numpy to 1.21.1 but installing this is not possible in more recent python versions, so I switched my conda env to python 3.8 and then I could downgrade. It would be nice if they fixed this issue for more recent python/numpy versions.

jeffhsu3 commented 1 year ago

Guessing this is the change in numpy: https://github.com/numpy/numpy/pull/14794 . Would a try except at the np.array(res) be an accepted way to deal with handling versions of numpy?

GaF123 commented 1 year ago

Thank you for your advice, it works for me!

WeilerP commented 1 year ago

The issue has been reported before and has been fixed in #1059.

yamihn commented 11 months ago

hi @WeilerP how did you solve the issue?

ramadatta commented 11 months ago

I used the following commands and created a new environment based on this https://github.com/theislab/scvelo/issues/1040#issuecomment-1556059697.

conda create -n scvelo_env python=3.9
conda activate scvelo_env
conda install numpy scipy cython numba matplotlib scikit-learn h5py click
pip install notebook
pip install scanpy
pip install leidenalg
pip install scvelo
pip install pandas==1.1.5 
pip install numpy==1.21.1

This error is resolved for me.

brendamelano commented 11 months ago

Hello, I have tried the tips above and in different threads including setting different environments with python=3.8 or 3.6. I have downgraded pandas and numpy with either numba compatability issues or kernels dying when generating umaps. Is there a way to fix this inhomogenous shape issue without downgrading libraries that the developers are working on?

hernandezvargash commented 10 months ago

Hi, same issue here. I tried the downgrading solution, as explained here. I'm also working on a fresh install of Ubuntu 22.04. Only difference is I'm using mamba instead of conda. Then I got a matplotlib error, so also downgraded to version 3.5.1 as suggested in this thread. This took me back to the original "inhomogeneous" error. So, I definitely agree with @brendamelano , we need a more stable solution with updated libraries. Any suggestions?