theislab/cellrank

Problems with g.predict_terminal_states() and g.compute_fate_probabilities()

kathbosc opened this issue · 5 comments

...
Hi all, thanks for the great package!

Heads up: I'm new to cellrank and I am not very experienced in python either.

I followed these tutorials: CellRank Meets RNA Velocity and Computing Initial and Terminal States with my own data after running scvelo. (Preprocessing of the data was done in R using Seurat).

  1. Everything works fine until running g.predict_terminal_states(), it only works upon specifying method = "eigengap". If I use other methods I get the following error: "ValueError: No macrostates have been selected."
    What does that mean?

  2. I am unable to compute fate probabilities: When running g.compute_fate_probabilities() I get this error:
    AttributeError: 'petsc4py.PETSc.KSP' object has no attribute 'converged'. Did you mean: 'is_converged'?
    I couldn't find anything related to this error online.

Any advice would be greatly appreciated, many thanks in advance!

Hi @kathbosc, please provide us with a bit more context by posting the code you used to run cellrank. Thanks!

Thanks @Marius1311.
This is my current workflow:

import scanpy as sc
import anndata as ad
from scipy import io
from scipy.sparse import coo_matrix, csr_matrix
import numpy as np
import os
import pandas as pd
import matplotlib.pyplot as pl
import loompy

# import matplotlib
# 
# class mplDeprecation(UserWarning):
#     pass
# 
# matplotlib.cbook.mplDeprecation = mplDeprecation

import scvelo as scv
import cellrank as cr #to import cellrtank i needed to follow the instructions here: https://github.com/theislab/cellrank/issues/1183

import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)

from IPython.display import display

scv.settings.verbosity = 3
scv.settings.set_figure_params('scvelo', facecolor='white', dpi=100, frameon=False)
cr.settings.verbosity = 2

# loading adata after running scvelo
adata = sc.read_h5ad('2024-07-08_neus_adata_all_dyn.fin.h5ad')

# Cell rank
## Combine RNA velocity with expression similarity in high dimensions

vk = cr.kernels.VelocityKernel(adata)
vk.compute_transition_matrix()

ck = cr.kernels.ConnectivityKernel(adata)
ck.compute_transition_matrix()
combined_kernel = 0.8 * vk + 0.2 * ck

vk.plot_projection()

vk.plot_random_walks(start_ixs={"seurat_clusters": "7_3"}, max_iter=200, seed=0)

vk.write_to_adata()
adata.write(
    "neus_all_velocity_kernel.h5ad", compression="gzip"
)

## Computing Initial and Terminal States
import scipy.stats as st
vk = cr.kernels.VelocityKernel.from_adata(adata, key="T_fwd")
sc.pl.embedding(adata, basis="umap", color="seurat_clusters", legend_loc="left")
g = cr.estimators.GPCCA(vk)

g.fit(cluster_key="seurat_clusters")  #n_states=[6, 20]
g.fit(cluster_key="seurat_clusters", n_states=[4, 20])  #
g.fit(cluster_key="seurat_clusters", n_states=6)  #

g.plot_macrostates(which="all", discrete=True, legend_loc="best", s=100)

g.predict_terminal_states(method = "eigengap") # "eigengap" method is the only one that works
g.plot_macrostates(which="terminal", discrete=True, legend_loc="best", s=100)

g.predict_initial_states()
g.plot_macrostates(which="initial", legend_loc="best", s=100)

g.compute_fate_probabilities()
g.plot_fate_probabilities(same_plot=False)

I am unable to compute fate probabilities: When running g.compute_fate_probabilities() I get this error:
AttributeError: 'petsc4py.PETSc.KSP' object has no attribute 'converged'. Did you mean: 'is_converged'?
I couldn't find anything related to this error online.

Package version:
cellrank==2.0.0 scanpy==1.10.1 anndata==0.10.8 numpy==1.26.4 numba==0.59.1 scipy==1.13.1 pandas==2.2.2 pygpcca==1.0.4 scikit-learn==1.4.2 statsmodels==0.14.2 python-igraph==0.11.5 scvelo==0.3.2 pygam==0.9.1 matplotlib==3.8.4 seaborn==0.13.2

Am I missing something?
Thanks very much!

Maybe also worth mentioning, I am running this is RStudio using reticulate to access my conda python "scvelo/cellrank" environment.

Hi @kathbosc, please update to the most recent version of CellRank and check whether the error persists there. Also, you don't need to write and load your kernel if you're doing this analysis in the same notebook, you can just do

combined_kernel = 0.8 * vk + 0.2 * ck
g = cr.estimators.GPCCA(combined_kernel)

and continue from there.

Also, I would recommend to use the low-level estimator interface, as explained here: https://cellrank.readthedocs.io/en/latest/notebooks/tutorials/estimators/600_initial_terminal.html#advanced-usage

It will give you more control over your estimator object and will make it easier to diagnose any problems.

This was fixed in PR#1136, see #1134; updating cellrank to a newer version should work.