theislab / cellrank

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

`ctk.plot_projection` gives `AttributeError: can't set attribute` error #1018

Closed cartal closed 1 year ago

cartal commented 1 year ago

Thanks for all the work that has gone on cellrank, super cool stuff.

I am re-running some of my old analysis notebooks for a project and I discovered that the syntax has changed. I am trying to visualise the inferred trajectories using the following:

ctk.plot_projection(color = ['ct_pseudotime', 'leiden_states'], cmap = 'plasma', frameon = False)
AttributeError                            Traceback (most recent call last)
Cell In[33], line 1
----> 1 ctk.plot_projection(color = ['ct_pseudotime', 'leiden_states'], cmap = 'plasma', frameon = False)

File [~/mambaforge/envs/cellrank/lib/python3.9/site-packages/cellrank/kernels/_utils.py:292](https://file+.vscode-resource.vscode-cdn.net/Users/cartalop/github/COPD_influenza/9-pseudotime/~/mambaforge/envs/cellrank/lib/python3.9/site-packages/cellrank/kernels/_utils.py:292), in require_tmat(wrapped, instance, args, kwargs)
    288 if instance.transition_matrix is None:
    289     raise RuntimeError(
    290         "Compute transition matrix first as `.compute_transition_matrix()`."
    291     )
--> 292 return wrapped(*args, **kwargs)

File [~/mambaforge/envs/cellrank/lib/python3.9/site-packages/cellrank/kernels/_base_kernel.py:304](https://file+.vscode-resource.vscode-cdn.net/Users/cartalop/github/COPD_influenza/9-pseudotime/~/mambaforge/envs/cellrank/lib/python3.9/site-packages/cellrank/kernels/_base_kernel.py:304), in KernelExpression.plot_projection(self, basis, key_added, recompute, stream, connectivities, **kwargs)
    300 proj = TmatProjection(self, basis=basis)
    301 proj.project(
    302     key_added=key_added, recompute=recompute, connectivities=connectivities
    303 )
--> 304 proj.plot(stream=stream, **kwargs)

File [~/mambaforge/envs/cellrank/lib/python3.9/site-packages/cellrank/kernels/utils/_projection.py:150](https://file+.vscode-resource.vscode-cdn.net/Users/cartalop/github/COPD_influenza/9-pseudotime/~/mambaforge/envs/cellrank/lib/python3.9/site-packages/cellrank/kernels/utils/_projection.py:150), in TmatProjection.plot(self, stream, *args, **kwargs)
    132 """
    133 Plot projected transition matrix in a embedding.
    134 
   (...)
    147 %(just_plots)s
...
File [~/mambaforge/envs/cellrank/lib/python3.9/site-packages/pandas/core/arrays/categorical.py:2460](https://file+.vscode-resource.vscode-cdn.net/Users/cartalop/github/COPD_influenza/9-pseudotime/~/mambaforge/envs/cellrank/lib/python3.9/site-packages/pandas/core/arrays/categorical.py:2460), in CategoricalAccessor._delegate_property_set(self, name, new_values)
   2459 def _delegate_property_set(self, name, new_values):
-> 2460     return setattr(self._parent, name, new_values)

AttributeError: can't set attribute

It does plot the pseudotime but it fails when I try to visualise the cell states (or any other key in adata.obs). Funny enough, it also complains that the transition matrix has not been computed, but I did it before using this:

ctk.compute_transition_matrix(threshold_scheme = "soft", nu = 0.5)

Is there something I am doing wrong?

Also, do you have a timeline for when the tutorials will be updated?

Versions:

anndata 0.8.0 scanpy 1.9.3

PIL 9.4.0 appnope 0.1.3 asttokens NA backcall 0.2.0 beta_ufunc NA binom_ufunc NA cellrank 1.5.1 cffi 1.15.1 colorama 0.4.6 comm 0.1.3 cycler 0.10.0 cython_runtime NA dateutil 2.8.2 debugpy 1.6.6 decorator 5.1.1 defusedxml 0.7.1 docrep 0.3.2 executing 1.2.0 h5py 3.8.0 hypergeom_ufunc NA igraph 0.10.4

Python 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:42:20) [Clang 14.0.6 ] macOS-13.3-x86_64-i386-64bit Session information updated at 2023-04-04 16:51

Marius1311 commented 1 year ago

Hi @cartal, without going into any details here, the new syntax is outlined in this new tutorial: https://cellrank.readthedocs.io/en/latest/notebooks/tutorials/cellrank_meets_pseudotime.html

We're currently pushing the other remaining parts of the 2.0 documentation and hope to be done with this soon!

Marius1311 commented 1 year ago

@michalk8 might be able to give you more details on the actual error.

cartal commented 1 year ago

Thank you so much @Marius1311 and @michalk8!!

michalk8 commented 1 year ago

Thank you so much @Marius1311 and @michalk8!!

@cartal can you please post the full traceback? This error comes from pandas, will need to know where exactly we try to set some attribute.

michalk8 commented 1 year ago

@WeilerP seems like pandas==2.0.0 broke scvelo, specifically here: scvelo/plotting/utils.py:555:

2023-04-04T21:01:11.0919104Z cellrank/kernels/_utils.py:292: in require_tmat
2023-04-04T21:01:11.0919509Z     return wrapped(*args, **kwargs)
2023-04-04T21:01:11.0919938Z cellrank/kernels/_base_kernel.py:304: in plot_projection
2023-04-04T21:01:11.0920683Z     proj.plot(stream=stream, **kwargs)
2023-04-04T21:01:11.0921179Z cellrank/kernels/utils/_projection.py:150: in plot
2023-04-04T21:01:11.0921620Z     return scv.pl.velocity_embedding_stream(
2023-04-04T21:01:11.0922461Z .tox/py38-linux-slepc/lib/python3.8/site-packages/scvelo/plotting/velocity_embedding_stream.py:252: in velocity_embedding_stream
2023-04-04T21:01:11.0922980Z     ax = scatter(
2023-04-04T21:01:11.0923565Z .tox/py38-linux-slepc/lib/python3.8/site-packages/scvelo/plotting/scatter.py:668: in scatter
2023-04-04T21:01:11.0924065Z     set_legend(
2023-04-04T21:01:11.0924635Z .tox/py38-linux-slepc/lib/python3.8/site-packages/scvelo/plotting/utils.py:555: in set_legend
2023-04-04T21:01:11.0928919Z     obs_vals.cat.categories = obs_vals.cat.categories.astype(str)
2023-04-04T21:01:11.0929665Z .tox/py38-linux-slepc/lib/python3.8/site-packages/pandas/core/base.py:178: in __setattr__
2023-04-04T21:01:11.0930156Z     object.__setattr__(self, key, value)
2023-04-04T21:01:11.0930753Z .tox/py38-linux-slepc/lib/python3.8/site-packages/pandas/core/accessor.py:99: in _setter
2023-04-04T21:01:11.0931272Z     return self._delegate_property_set(name, new_values)
allepalma commented 1 year ago

Hi @michalk8, I get the same error as @cartal after reinstalling CellRank on another server. The same dataset few weeks ago went through smoothly. Package versions are as reported by @cartal.

When I run this: g.compute_macrostates(n_states=6, cluster_key="cluster")

with the cluster column in my adata.obs being converted to category I get the following traceback:

AttributeError                            Traceback (most recent call last)
Cell In[25], line 1
----> 1 g.compute_macrostates(n_states=6, cluster_key="cluster")

File ~/miniconda3/envs/cellrank/lib/python3.9/site-packages/cellrank/tl/estimators/terminal_states/_gpcca.py:200, in GPCCA.compute_macrostates(self, n_states, n_cells, cluster_key, **kwargs)
    194     logg.warning(
    195         f"Unable to compute macrostates with `n_states={n_states}` because it will "
    196         f"split complex conjugate eigenvalues. Using `n_states={n_states + 1}`"
    197     )
    198     self._gpcca = self._gpcca.optimize(m=n_states + 1)
--> 200 self._set_macrostates(
    201     memberships=self._gpcca.memberships,
    202     n_cells=n_cells,
    203     cluster_key=cluster_key,
    204     params=self._create_params(),
    205     time=start,
    206 )

File ~/miniconda3/envs/cellrank/lib/python3.9/site-packages/cellrank/tl/estimators/terminal_states/_gpcca.py:931, in GPCCA._set_macrostates(self, memberships, n_cells, cluster_key, check_row_sums, time, params)
    928 self._write_terminal_states(None, None, None, None, log=False)
    930 # fmt: off
--> 931 assignment, colors = self._set_categorical_labels(assignment, cluster_key=cluster_key)
    932 memberships = Lineage(memberships, names=list(assignment.cat.categories), colors=colors)
    933 # fmt: on

File ~/miniconda3/envs/cellrank/lib/python3.9/site-packages/cellrank/tl/estimators/terminal_states/_term_states_estimator.py:224, in TermStatesEstimator._set_categorical_labels(self, categories, cluster_key, existing)
    217         colors_reference = _create_categorical_colors(len(series_reference.cat.categories))
    219     names, colors = _map_names_and_colors(
    220         series_reference=series_reference,
    221         series_query=series_query,
    222         colors_reference=colors_reference,
    223     )
--> 224     categories.cat.categories = names
    225 else:
    226     colors = _create_categorical_colors(len(categories.cat.categories))

File ~/miniconda3/envs/cellrank/lib/python3.9/site-packages/pandas/core/base.py:178, in NoNewAttributesMixin.__setattr__(self, key, value)
    172 if getattr(self, "__frozen", False) and not (
    173     key == "_cache"
    174     or key in type(self).__dict__
    175     or getattr(self, key, None) is not None
    176 ):
    177     raise AttributeError(f"You cannot add any new attribute '{key}'")
--> 178 object.__setattr__(self, key, value)

File ~/miniconda3/envs/cellrank/lib/python3.9/site-packages/pandas/core/accessor.py:99, in PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._setter(self, new_values)
     98 def _setter(self, new_values):
---> 99     return self._delegate_property_set(name, new_values)

File ~/miniconda3/envs/cellrank/lib/python3.9/site-packages/pandas/core/arrays/categorical.py:2460, in CategoricalAccessor._delegate_property_set(self, name, new_values)
   2459 def _delegate_property_set(self, name, new_values):
-> 2460     return setattr(self._parent, name, new_values)

AttributeError: can't set attribute

Let me know if you need additional information. And, more importantly, thank you!