aristoteleo / dynamo-release

Inclusive model of expression dynamics with conventional or metabolic labeling based scRNA-seq / multiomics, vector field reconstruction and differential geometry analyses
https://dynamo-release.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
419 stars 59 forks source link

dyn.tl.dynamics error: ... convert to numpy #575

Closed zelhar closed 12 months ago

zelhar commented 1 year ago

I tried to follow exactly the example from: https://dynamo-release.readthedocs.io/en/latest/notebooks/scEU_seq_organoid_analysis_kinetic.html

calling

dyn.tl.dynamics(adata, model='deterministic', tkey='time', assumption_mRNA='ss') results in the follwing error: Multi-dimensional indexing (e.g. obj[:, None]) is no longer supported. Convert to a numpy array before indexing instead.


If this is a question and not a bug report or enhancement request, please post to our Github Discussions channel at https://github.com/aristoteleo/dynamo-release/discussions

Describe the bug A clear and concise description of what the bug is.

To Reproduce The code that produced the bug:

Minimal data may be provided to reproduce the bug Traceback Post the error message from the Tracebook output:

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

session_info.show(): Install session-info via pip install session-info and post the output after running session_info.show()

import dynamo
import session_info
session_info.show()

Additional context Add any other context about the problem here.

Sichao25 commented 1 year ago

Hi, thanks for reporting the issue. Could you provide the complete traceback output to help us better identify the problem? Also, it will be helpful to provide your Dynamo version and Numpy version. I can't reproduce the error on my end so I suspect it's related to a specific version.

zelhar commented 1 year ago

Hi. So here are the details: python version: 3.11.4 numpy: 1.24.4 pandas:2.0.3 dynamo: 1.3.2

And this is the output I get from dyn.tl.dynamics(adata, model='deterministic', tkey='time', assumption_mRNA='ss'):

In [49]: dyn.tl.dynamics(adata, model='deterministic', tkey='time', assumption_mRNA='ss')
|-----> dynamics_del_2nd_moments_key is None. Using default value from DynamoAdataConfig: dynamics_del_2nd_moments_key=False
|-----? Looks like you are using minutes as the time unit. For the purpose of numeric stability, we recommend using hour as the time unit.
|-----------> removing existing M layers:[]...
|-----------> making adata smooth...
|-----> calculating first/second moments...
|-----> [moments calculation] completed [22.1245s]
|-----? Your adata only has labeling data, but `NTR_vel` is set to be `False`. Dynamo will reset it to `True` to enable this analysis.
|-----> experiment type: kin, method: direct, model: deterministic
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[49], line 1
----> 1 dyn.tl.dynamics(adata, model='deterministic', tkey='time', assumption_mRNA='ss')

File ~/micromamba/envs/mdc/lib/python3.11/site-packages/dynamo/tools/dynamics.py:725, in dynamics(adata, filter_gene_mode, use_smoothed, assumption_mRNA, assumption_protein, model, est_method, NTR_vel, group, protein_names, concat_data, log_unnormalized, one_shot_method, fraction_for_deg, re_smooth, sanity_check, del_2nd_moments, cores, tkey, **est_kwargs)
    722     est_method = "direct"
    723 data_type = "smoothed" if use_smoothed else "sfs"
--> 725 (params, half_life, cost, logLL, param_ranges, cur_X_data, cur_X_fit_data,) = kinetic_model(
    726     subset_adata,
    727     tkey,
    728     model,
    729     est_method,
    730     experiment_type,
    731     has_splicing,
    732     splicing_labeling,
    733     has_switch=True,
    734     param_rngs={},
    735     data_type=data_type,
    736     return_ntr=return_ntr,
    737     **est_kwargs,
    738 )
    740 if type(params) == dict:
    741     alpha = params.pop("alpha")

File ~/micromamba/envs/mdc/lib/python3.11/site-packages/dynamo/tools/dynamics.py:1323, in kinetic_model(subset_adata, tkey, model, est_method, experiment_type, has_splicing, splicing_labeling, has_switch, param_rngs, data_type, return_ntr, **est_kwargs)
   1321 if model.lower() in ["deterministic", "stochastic"]:
   1322     layer = "M_n" if ("M_n" in subset_adata.layers.keys() and data_type == "smoothed") else "X_new"
-> 1323     X, X_raw = prepare_data_no_splicing(
   1324         subset_adata,
   1325         subset_adata.var.index,
   1326         time,
   1327         layer=layer,
   1328         total_layer=total_layer,
   1329     )
   1330 elif model.lower().startswith("mixture"):
   1331     layers = (
   1332         ["M_n", "M_t"]
   1333         if ("M_n" in subset_adata.layers.keys() and data_type == "smoothed")
   1334         else ["X_new", "X_total"]
   1335     )

File ~/micromamba/envs/mdc/lib/python3.11/site-packages/dynamo/tools/moments.py:721, in prepare_data_no_splicing(adata, genes, time, layer, use_total_layers, total_layer, return_old, return_ntr)
    719     else:
    720         sfs = adata.obs.total_Size_Factor
--> 721     sfs, tot_sfs = sfs[:, None], sfs[:, None]
    722 else:
    723     sfs, _ = sz_util(
    724         adata,
    725         layer,
   (...)
    730         CM=layer_data,
    731     )

File ~/micromamba/envs/mdc/lib/python3.11/site-packages/pandas/core/series.py:1033, in Series.__getitem__(self, key)
   1030     key = np.asarray(key, dtype=bool)
   1031     return self._get_values(key)
-> 1033 return self._get_with(key)

File ~/micromamba/envs/mdc/lib/python3.11/site-packages/pandas/core/series.py:1048, in Series._get_with(self, key)
   1043     raise TypeError(
   1044         "Indexing a Series with DataFrame is not "
   1045         "supported, use the appropriate DataFrame column"
   1046     )
   1047 elif isinstance(key, tuple):
-> 1048     return self._get_values_tuple(key)
   1050 elif not is_list_like(key):
   1051     # e.g. scalars that aren't recognized by lib.is_scalar, GH#32684
   1052     return self.loc[key]

File ~/micromamba/envs/mdc/lib/python3.11/site-packages/pandas/core/series.py:1082, in Series._get_values_tuple(self, key)
   1077 if com.any_none(*key):
   1078     # mpl compat if we look up e.g. ser[:, np.newaxis];
   1079     #  see tests.series.timeseries.test_mpl_compat_hack
   1080     # the asarray is needed to avoid returning a 2D DatetimeArray
   1081     result = np.asarray(self._values[key])
-> 1082     disallow_ndim_indexing(result)
   1083     return result
   1085 if not isinstance(self.index, MultiIndex):

File ~/micromamba/envs/mdc/lib/python3.11/site-packages/pandas/core/indexers/utils.py:343, in disallow_ndim_indexing(result)
    335 """
    336 Helper function to disallow multi-dimensional indexing on 1D Series/Index.
    337
   (...)
    340 in GH#30588.
    341 """
    342 if np.ndim(result) > 1:
--> 343     raise ValueError(
    344         "Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer "
    345         "supported. Convert to a numpy array before indexing instead."
    346     )

ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead.
Sichao25 commented 1 year ago

Interesting. Could you take a look at adata.uns["pp"] after preprocessing? When I run the tutorial in Colab, the experiment type is one-shot because the "time" observation is all the same. Just from your output, your program is using kinetics experiments instead. It will also be helpful to output the np.unique(adata.obs.time) to help understand the reason.

The error itself seems to be caused by Pandas. Multi-dimensional indexing has been deprecated in the latest version. If you downgrade it to an earlier version, the error may be replaced by a warning.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 14 days