theislab / cellrank

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

Error with computing transition matrix, NaNs #1003

Closed lindaboshans closed 1 year ago

lindaboshans commented 1 year ago

... Hello,

Thank you for a great program! Unfortunately I am running into an error that I get using both cell rank v 1.5.1 and the development version as well. I am using the dynamical scvelo velocity adata output. code:

from cellrank.tl.kernels import VelocityKernel

vk = cr.tl.kernels.VelocityKernel(adata, backward=False, vkey='velocity', xKey='Ms')
vk.compute_transition_matrix()

error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-deced64f7289> in <module>
----> 1 vk.compute_transition_matrix()

/hpc/packages/minerva-centos7/py_packages/3.7/lib/python3.7/site-packages/cellrank/tl/kernels/_velocity_kernel.py in compute_transition_matrix(self, mode, backward_mode, scheme, softmax_scale, n_samples, seed, check_irreducibility, **kwargs)
    276                 seed=seed,
    277                 backend=backend,
--> 278                 **kwargs,
    279             )
    280             softmax_scale = 1.0 / np.median(np.abs(cmat.data))

/hpc/packages/minerva-centos7/py_packages/3.7/lib/python3.7/site-packages/cellrank/ul/_utils.py in wrapper(*args, **kwargs)
    193     @wraps(func)
    194     def wrapper(*args, **kwargs):
--> 195         return dispatch(args[0])(*args[1:], **kwargs)
    196 
    197     wrapper.register = register

/hpc/packages/minerva-centos7/py_packages/3.7/lib/python3.7/site-packages/cellrank/tl/kernels/_velocity_kernel.py in <lambda>(**kwargs)
    560 )
    561 _dispatch_computation.register(VelocityMode.DETERMINISTIC)(
--> 562     lambda **kwargs: _run_in_parallel(_run_deterministic, **kwargs)
    563 )
    564 _dispatch_computation.register(VelocityMode.MONTE_CARLO)(

/hpc/packages/minerva-centos7/py_packages/3.7/lib/python3.7/site-packages/cellrank/tl/kernels/_velocity_kernel.py in _run_in_parallel(fn, conn, **kwargs)
    356         unit=unit,
    357         **_filter_kwargs(parallelize, **kwargs),
--> 358     )(**_filter_kwargs(fn, **kwargs))
    359 
    360 

/hpc/packages/minerva-centos7/py_packages/3.7/lib/python3.7/site-packages/cellrank/ul/_parallelize.py in wrapper(*args, **kwargs)
    119             thread.join()
    120 
--> 121         return res if extractor is None else extractor(res)
    122 
    123     col_len = collection.shape[0] if issparse(collection) else len(collection)

/hpc/packages/minerva-centos7/py_packages/3.7/lib/python3.7/site-packages/cellrank/tl/kernels/_velocity_kernel.py in <lambda>(res)
    353         ixs,
    354         as_array=False,
--> 355         extractor=lambda res: _reconstruct_one(np.concatenate(res, axis=-1), conn, ixs),
    356         unit=unit,
    357         **_filter_kwargs(parallelize, **kwargs),

/hpc/packages/minerva-centos7/py_packages/3.7/lib/python3.7/site-packages/cellrank/tl/kernels/_utils.py in _reconstruct_one(data, mat, ixs)
    213     if not np.all(close_to_1):
    214         raise ValueError(
--> 215             f"Matrix is not row-stochastic. The following rows don't sum to 1: `{row_sums[~close_to_1]}`."
    216         )
    217 

ValueError: Matrix is not row-stochastic. The following rows don't sum to 1: `[nan nan nan]`.
Marius1311 commented 1 year ago

Hi @lindaseong! It looks like you have 3 rows in your unnormalized transition matrix that are causing trouble during row-normalization. Please check your velocities and the imputed data in Ms (both in adata.layers) for Nans of Infs. Also, check whether any velocity is exactly zero. If yes for any of the above, remove the corresponding cell. This should remove exactly three cells. If none of this helps, also check the KNN graph (in adata.obsp), in particular check the connectivities for Infs and Nans, and remove the corresponding cells (i.e. rows)

lindaboshans commented 1 year ago

Hi @Marius1311

My Ms and velocities do have NaNs. How do I check if velocity is zero? Could you please advise on how to remove the rows with NaNs from adata?

Marius1311 commented 1 year ago

I think that is something you can probably ask chatGPT, that's standard scanpy/AnnData stuff.

Marius1311 commented 1 year ago

I'm closing this as I assume the issue has been solved.