scverse / scanpy-tutorials

Scanpy Tutorials.
https://scanpy-tutorials.readthedocs.io/
189 stars 117 forks source link

Trajectory inference tutorial: broken category renaming code #135

Closed TranThiHang97 closed 2 months ago

TranThiHang97 commented 3 months ago

Please make sure these conditions are met

What happened?

I am learning the Trajectory inference tutorial. The code 1 from instruction gave me an error, then I fixed it. However, when I continued to the subsequent codes (code 2), I encountered an error that I do not know how to fix.

Minimal code sample

Code 1:

adata.obs["louvain_anno"].cat.categories = [
    *["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
    *["10/Ery", "11", "12", "13", "14", "15"],
    *["16/Stem", "17", "18"],
    *["19/Neu", "20/Mk", "21"],
    *["22/Baso", "23", "24/Mo"],
]

Fix for code 1:

adata.obs["louvain_anno"].cat.set_categories = [
    *["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
    *["10/Ery", "11", "12", "13", "14", "15"],
    *["16/Stem", "17", "18"],
    *["19/Neu", "20/Mk", "21"],
    *["22/Baso", "23", "24/Mo"],
]

Code 2: sc.tl.paga(adata, groups="louvain_anno")

Error output

Code 1

AttributeError                            Traceback (most recent call last)
Cell In[47], line 1
----> 1 adata.obs["louvain_anno"].cat.categories = [
      2     *["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      3     *["10/Ery", "11", "12", "13", "14", "15"],
      4     *["16/Stem", "17", "18"],
      5     *[
      6         "19/Neu",
      7         "20/Mk",
      8         "21",
      9     ],
     10     *["22/Baso", "23", "24/Mo"],
     11 ]

File ~/anaconda3/envs/py39/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 ~/anaconda3/envs/py39/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 ~/anaconda3/envs/py39/lib/python3.9/site-packages/pandas/core/arrays/categorical.py:2915, in CategoricalAccessor._delegate_property_set(self, name, new_values)
   2914 def _delegate_property_set(self, name: str, new_values):  # type: ignore[override]
-> 2915     return setattr(self._parent, name, new_values)

AttributeError: can't set attribute

Error of code 2:

running PAGA
---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
Cell In[43], line 1
----> 1 sc.tl.paga(adata, groups="louvain_anno")

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/legacy_api_wrap/__init__.py:80, in legacy_api.<locals>.wrapper.<locals>.fn_compatible(*args_all, **kw)
     77 @wraps(fn)
     78 def fn_compatible(*args_all: P.args, **kw: P.kwargs) -> R:
     79     if len(args_all) <= n_positional:
---> 80         return fn(*args_all, **kw)
     82     args_pos: P.args
     83     args_pos, args_rest = args_all[:n_positional], args_all[n_positional:]

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/scanpy/tools/_paga.py:134, in paga(adata, groups, use_rna_velocity, model, neighbors_key, copy)
    132     adata.uns["paga"] = {}
    133 if not use_rna_velocity:
--> 134     paga.compute_connectivities()
    135     adata.uns["paga"]["connectivities"] = paga.connectivities
    136     adata.uns["paga"]["connectivities_tree"] = paga.connectivities_tree

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/scanpy/tools/_paga.py:169, in PAGA.compute_connectivities(self)
    167 def compute_connectivities(self):
    168     if self._model == "v1.2":
--> 169         return self._compute_connectivities_v1_2()
    170     elif self._model == "v1.0":
    171         return self._compute_connectivities_v1_0()

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/scanpy/tools/_paga.py:190, in PAGA._compute_connectivities_v1_2(self)
    188 n = sum(ns)
    189 es_inner_cluster = [vc.subgraph(i).ecount() for i in range(len(ns))]
--> 190 cg = vc.cluster_graph(combine_edges="sum")
    191 inter_es = cg.get_adjacency_sparse(attribute="weight")
    192 es = np.array(es_inner_cluster) + inter_es.sum(axis=1).A1

File ~/anaconda3/envs/py39/lib/python3.9/site-packages/igraph/clustering.py:336, in VertexClustering.cluster_graph(self, combine_vertices, combine_edges)
    312 """Returns a graph where each cluster is contracted into a single
    313 vertex.
    314 
   (...)
    333 @return: the new graph.
    334 """
    335 result = self.graph.copy()
--> 336 result.contract_vertices(self.membership, combine_vertices)
    337 if combine_edges is not False:
    338     result.simplify(combine_edges=combine_edges)

InternalError: Error at src/constructors/basic_constructors.c:75: Invalid (negative or too large) vertex ID. -- Invalid vertex ID

Versions

``` ----- anndata 0.10.8 scanpy 1.10.2 ----- CoreFoundation NA Foundation NA PIL 10.4.0 PyObjCTools NA anyio NA appnope 0.1.4 arrow 1.3.0 asttokens NA attr 23.2.0 attrs 23.2.0 babel 2.14.0 backports NA brotli 1.1.0 certifi 2024.07.04 cffi 1.16.0 charset_normalizer 3.3.2 colorama 0.4.6 comm 0.2.2 cycler 0.12.1 cython_runtime NA dateutil 2.9.0 debugpy 1.8.2 decorator 5.1.1 defusedxml 0.7.1 exceptiongroup 1.2.2 executing 2.0.1 fastjsonschema NA fqdn NA h5py 3.11.0 idna 3.7 igraph 0.11.6 importlib_resources NA ipykernel 6.29.5 ipywidgets 8.1.3 isoduration NA jaraco NA jedi 0.19.1 jinja2 3.1.4 joblib 1.4.2 json5 0.9.25 jsonpointer 3.0.0 jsonschema 4.23.0 jsonschema_specifications NA jupyter_events 0.10.0 jupyter_server 2.14.2 jupyterlab_server 2.27.3 kiwisolver 1.4.5 legacy_api_wrap NA leidenalg 0.10.2 llvmlite 0.43.0 louvain 0.8.2 markupsafe 2.1.5 matplotlib 3.9.1 matplotlib_inline 0.1.7 more_itertools 10.3.0 mpl_toolkits NA natsort 8.4.0 nbformat 5.10.4 networkx 3.2.1 numba 0.60.0 numpy 1.26.4 objc 10.3.1 overrides NA packaging 24.1 pandas 2.2.2 parso 0.8.4 patsy 0.5.6 pexpect 4.9.0 pickleshare 0.7.5 pkg_resources NA platformdirs 4.2.2 prometheus_client NA prompt_toolkit 3.0.47 psutil 6.0.0 ptyprocess 0.7.0 pure_eval 0.2.3 pycparser 2.22 pydev_ipython NA pydevconsole NA pydevd 2.9.5 pydevd_file_utils NA pydevd_plugins NA pydevd_tracing NA pygments 2.18.0 pynndescent 0.5.13 pyparsing 3.1.2 pythonjsonlogger NA pytz 2024.1 referencing NA requests 2.32.3 rfc3339_validator 0.1.4 rfc3986_validator 0.1.1 rpds NA scipy 1.13.1 send2trash NA session_info 1.0.0 six 1.16.0 sklearn 1.5.1 sniffio 1.3.1 socks 1.7.1 stack_data 0.6.2 statsmodels 0.14.2 texttable 1.7.0 threadpoolctl 3.5.0 tornado 6.4.1 tqdm 4.66.4 traitlets 5.14.3 typing_extensions NA umap 0.5.5 uri_template NA urllib3 2.2.2 wcwidth 0.2.13 webcolors 24.6.0 websocket 1.8.0 yaml 6.0.1 zipp NA zmq 26.0.3 zoneinfo NA zstandard 0.22.0 ----- IPython 8.18.1 jupyter_client 8.6.2 jupyter_core 5.7.2 jupyterlab 4.2.4 notebook 7.2.1 ----- Python 3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:55:20) [Clang 16.0.6 ] macOS-14.6-arm64-arm-64bit ----- Session information updated at 2024-08-09 11:39 ```
tomyputw commented 2 months ago

I try and fix with pandas, as if the function has been covered.


adata.obs["louvain_anno"] = adata.obs["louvain_anno"].cat.rename_categories({
    '10':'10/Ery',
    '16':'16/Stem',
    '19':'19/Neu',
    '20':'20/Mk',
    '22':'22/Baso',
    '24':'24/Mo',
})

print(adata.obs.louvain_anno.cat.categories)
print(adata.obs.louvain_anno.head(40))
flying-sheep commented 2 months ago

Thanks, that’s a good fix!