scverse / scanpy

Single-cell analysis in Python. Scales to >1M cells.
https://scanpy.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.92k stars 599 forks source link

KeyError: 'dpt_order_indices' missing if branching=0 #409

Open Xparx opened 5 years ago

Xparx commented 5 years ago

I don't think this is fixed in the previous issue https://github.com/theislab/scanpy/issues/129.

If you run sc.tl.dpt(adata) it creates the dpt_pseudotime property in obs as expected. However using dpt in plotting functions fails like sc.pl.dpt_timeseries(adata). This is due to the dpt_order_indices property not being created if branching=0.

falexwolf commented 5 years ago

Sorry about the slow response, I'll look into this early this week.

Xparx commented 5 years ago

No worries, It's a minor issue of convenience. If I interpreted the missing keys correctly it's not to difficult to add them manually. There are two keys missing. The one I mentioned and one related to the branching of the tree which I'm unsure how to interpret. I added the following two lines to my script.

adata.obs['dpt_order_indices'] = adata.obs['dpt_pseudotime'].argsort()
adata.uns['dpt_changepoints'] = np.ones(adata.obs['dpt_order_indices'].shape[0] - 1)
haskankaya commented 4 years ago

Hi there, I am having the same issue as above. I have tried the fix that @Xparx has provided but it yields more problems. See the below error which I am now receiving:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: float() argument must be a string or a number, not 'csc_matrix'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-48-abf5bf78cb77> in <module>
----> 1 sc.pl.dpt_timeseries(adata_HVG)

~/.conda/envs/python3/lib/python3.8/site-packages/scanpy/plotting/_tools/__init__.py in dpt_timeseries(adata, color_map, show, save, as_heatmap)
    159     if as_heatmap:
    160         # plot time series as heatmap, as in Haghverdi et al. (2016), Fig. 1d
--> 161         timeseries_as_heatmap(
    162             adata.X[adata.obs['dpt_order_indices'].values],
    163             var_names=adata.var_names,

~/.conda/envs/python3/lib/python3.8/site-packages/scanpy/plotting/_utils.py in timeseries_as_heatmap(X, var_names, highlights_x, color_map)
    197     _, ax = pl.subplots(figsize=(1.5 * 4, 2 * 4))
    198     ax.imshow(
--> 199         np.array(X, dtype=np.float_),
    200         aspect='auto',
    201         interpolation='nearest',

ValueError: setting an array element with a sequence.

I thought that this might be something to do with the fact that the np.ones object is a numpy array instead of a pandas series so I tried substituting this with the line adata.uns['dpt_changepoints'] = pd.Series(np.ones(adata.obs['dpt_order_indices'].shape[0] - 1)) instead, but this still yielded the same error.

Thanks in advance!

Update: I just tried to run this command having used `branching=1' in my analysis and not performing the above correction (even though I know it's inappropriate for my particular system, branching=0 is what I want to use) and it still yielded the same error. As such I think perhaps this could be something independent of the above issue.

Bhersbach commented 1 year ago

@falexwolf This issue still persists in version 1.9.1. and the work around suggested by @Xparx results in the same error that @haskankaya has reported. Any advice on how to get around this or a potential fix?