theislab / cellrank

CellRank: dynamics from multi-view single-cell data
https://cellrank.org
BSD 3-Clause "New" or "Revised" License
352 stars 46 forks source link

g.compute_macrostates() ValueError: Index of obs must match index of X #749

Closed zhenzuo2 closed 3 years ago

zhenzuo2 commented 3 years ago

g.compute_macrostates(n_states=7, cluster_key="seurat_clusters")

Computing 7 macrostates /storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/anndata/_core/anndata.py:120: ImplicitModificationWarning: Transforming to str index. warnings.warn("Transforming to str index.", ImplicitModificationWarning) /storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/anndata/_core/anndata.py:120: ImplicitModificationWarning: Transforming to str index. warnings.warn("Transforming to str index.", ImplicitModificationWarning)

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_8861/146493107.py in <module>
----> 1 g.compute_macrostates(n_states=7, cluster_key="seurat_clusters")
      2 g.compute_terminal_states()
      3 g.compute_absorption_probabilities()

/storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/cellrank/tl/estimators/terminal_states/_gpcca.py in compute_macrostates(self, n_states, n_cells, cluster_key, **kwargs)
    198             self._gpcca = self._gpcca.optimize(m=n_states + 1)
    199 
--> 200         self._set_macrostates(
    201             memberships=self._gpcca.memberships,
    202             n_cells=n_cells,

/storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/cellrank/tl/estimators/terminal_states/_gpcca.py in _set_macrostates(self, memberships, n_cells, cluster_key, check_row_sums, time, params)
    941             )
    942 
--> 943         self._write_macrostates(
    944             assignment, colors, memberships, time=time, params=params
    945         )

/storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/cellrank/tl/estimators/mixins/_utils.py in logger(wrapped, instance, args, kwargs)
     87     """Handle logging for :class:`anndata.AnnData` writing functions of :class:`cellrank.estimators.BaseEstimator`."""
     88     log, time = kwargs.pop("log", True), kwargs.pop("time", None)
---> 89     msg = wrapped(*args, **kwargs)
     90 
     91     if log:

/storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/cellrank/tl/estimators/mixins/_utils.py in shadow(wrapped, instance, args, kwargs)
    104     Used to easily create :class:`anndata.AnnData` serialization object in :class:`cellrank.estimators.BaseEstimator`.
    105     """
--> 106     res = wrapped(*args, **kwargs)
    107     with instance._shadow:
    108         try:

/storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/cellrank/tl/estimators/terminal_states/_gpcca.py in _write_macrostates(self, macrostates, colors, memberships, params)
    982             self._set("_coarse_init_dist", value=init_dist, shadow_only=True)
    983             self._set("_coarse_stat_dist", value=stat_dist, shadow_only=True)
--> 984             self._set(obj=self.adata.uns, key=Key.uns.coarse(self.backward), value=AnnData(tmat, obs=dists))
    985         else:
    986             for attr in ["_schur_vectors", "_schur_matrix", "_coarse_tmat", "_coarse_init_dist", "_coarse_stat_dist"]:

/storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/anndata/_core/anndata.py in __init__(self, X, obs, var, uns, obsm, varm, layers, raw, dtype, shape, filename, filemode, asview, obsp, varp, oidx, vidx)
    306             self._init_as_view(X, oidx, vidx)
    307         else:
--> 308             self._init_as_actual(
    309                 X=X,
    310                 obs=obs,

/storage/singlecell/zz4/miniconda3/envs/mypython/lib/python3.8/site-packages/anndata/_core/anndata.py in _init_as_actual(self, X, obs, var, uns, obsm, varm, varp, obsp, raw, layers, dtype, shape, filename, filemode)
    502                 attr.index = idx
    503             elif not idx.equals(attr.index):
--> 504                 raise ValueError(f"Index of {attr_name} must match {x_name} of X.")
    505 
    506         # unstructured annotations

ValueError: Index of obs must match index of X.
zhenzuo2 commented 3 years ago

Fixed. 'Clusters' must be a string categorical variable instead of numeric ones.

michalk8 commented 3 years ago

Thanks for spotting this, it occurs when we associate the names in adata.obs['seurat_clusters'] with macrostate names. Though I haven't been able to reproduce it with the code below, at least the final dtype should be all strings and be fixed in #750

import cellrank as cr
import numpy as np

adata = cr.datasets.pancreas_preprocessed()
k = cr.tl.transition_matrix(adata)

g = cr.tl.estimators.GPCCA(k)
g.compute_schur()
g.compute_macrostates()
adata.obs['cls_float'] = adata.obs['clusters'].cat.rename_categories(
    dict(zip(['Ngn3 low EP', 'Ngn3 high EP', 'Fev+', 'Beta', 'Alpha', 'Delta', 'Epsilon'],
    np.array(list(range(7)), dtype=float))))
g.compute_macrostates(n_states=7, cluster_key='cls_float')

More thorough look/refactoring will be done in #745

michalk8 commented 3 years ago

closed via #750