cellrank icon indicating copy to clipboard operation
cellrank copied to clipboard

Incomprehensible error message when loading kernel from AnnData

Open Marius1311 opened this issue 2 years ago • 4 comments

I'm trying to load a VelocityKernel from an AnnData object that contains a precomputed transition matrix. However, it doesn't led me and gives an incomprehensible error message, which I think should be improved.

vk = cr.kernels.VelocityKernel.from_adata(adata, key='T_fwd')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/Projects/cellrank/cellrank/kernels/_base_kernel.py:554, in Kernel.from_adata(cls, adata, key, copy)
    553     params = adata.uns[f"{key}_params"]["params"].copy()
--> 554     init_params = adata.uns[f"{key}_params"]["init"].copy()
    555 except KeyError as e:

KeyError: 'init'

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

KeyError                                  Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 vk = cr.kernels.VelocityKernel.from_adata(adata, key='T_fwd')

File ~/Projects/cellrank/cellrank/kernels/_base_kernel.py:556, in Kernel.from_adata(cls, adata, key, copy)
    554     init_params = adata.uns[f"{key}_params"]["init"].copy()
    555 except KeyError as e:
--> 556     raise KeyError(f"Unable to kernel parameters, reason: `{e}`") from e
    558 if copy:
    559     transition_matrix = transition_matrix.copy()

KeyError: "Unable to kernel parameters, reason: `'init'`"

Versions:

cellrank==1.5.1+ge52b2ee9.d20220728 scanpy==1.9.1 anndata==0.8.0 numpy==1.22.4 numba==0.56.0 scipy==1.8.1 pandas==1.4.3 pygpcca==1.0.3 scikit-learn==1.1.1 statsmodels==0.13.2 scvelo==0.2.4 pygam==0.8.0 matplotlib==3.5.2 seaborn==0.11.2

...

Marius1311 avatar Jul 28 '22 08:07 Marius1311

Originally, the kernel had been written to AnnData using an older version of CellRank - I just updated to the current version on master and run this again. Now, I get:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 vk = cr.kernels.VelocityKernel.from_adata(adata, key='T_fwd')

File ~/Projects/cellrank/cellrank/kernels/_base_kernel.py:561, in Kernel.from_adata(cls, adata, key, copy)
    558 if copy:
    559     transition_matrix = transition_matrix.copy()
--> 561 kernel = cls(adata, **init_params)
    562 kernel.transition_matrix = transition_matrix
    563 kernel._params = params

File ~/Projects/cellrank/cellrank/kernels/_velocity_kernel.py:57, in VelocityKernel.__init__(self, adata, backward, xkey, vkey, **kwargs)
     49 def __init__(
     50     self,
     51     adata: AnnData,
   (...)
     55     **kwargs: Any,
     56 ):
---> 57     super().__init__(
     58         adata,
     59         backward=backward,
     60         xkey=xkey,
     61         vkey=vkey,
     62         **kwargs,
     63     )
     64     self._logits: Optional[np.ndarray] = None

File ~/Projects/cellrank/cellrank/kernels/mixins/_kernel.py:77, in BidirectionalMixin.__init__(self, backward, *args, **kwargs)
     75 super().__init__(*args, **kwargs)
     76 if not isinstance(backward, bool):
---> 77     raise TypeError(
     78         f"Expected `backward` to be `bool`, found `{type(backward).__name__}`."
     79     )
     80 self._backward = backward
     81 self._init_kwargs["backward"] = backward

TypeError: Expected `backward` to be `bool`, found `bool_`.

Marius1311 avatar Jul 28 '22 08:07 Marius1311

Same error when I call g.compute_states(). BTW, the naming is a bit unfortunate I think - you first have to call compute_macrostates, and upon that, compute_states. I think the second method needs a different name, maybe classify_macrostates. That would make more sense in my opinion.

Marius1311 avatar Jul 28 '22 09:07 Marius1311

Also, if you don't already know this method, it's going to be really hard to work with it, given that it exposes like none of its arguments:

Screenshot 2022-07-28 at 11 03 27

Marius1311 avatar Jul 28 '22 09:07 Marius1311

The loading should be fix in 5bb5ae474531855cf03b2d5becd36a029fbf0b8f

Also, if you don't already know this method, it's going to be really hard to work with it, given that it exposes like none of its arguments:

Can inject the docstring, would keep *args/**kwargs though.

I think the second method needs a different name, maybe classify_macrostates. That would make more sense in my opinion.

Will rename to classify_macrostates.

michalk8 avatar Jul 28 '22 20:07 michalk8

Will get back to this in the coming days and report back to you @michalk8

Marius1311 avatar Oct 10 '22 08:10 Marius1311

This seems to work now!

Marius1311 avatar Dec 18 '22 11:12 Marius1311