An error happend with “palantir.preprocess.log_transform ” function, and with error message "Adding a nonzero scalar to a sparse matrix is not supported
zyb1984 opened this issue · comments
I tried to follow the example of "Palantir analysis notebook", and an error message was reported with “palantir.preprocess.log_transform ” function, and an error message with "Adding a nonzero scalar to a sparse matrix is not supported".
The original code with
import palantir
import scanpy as sc
import numpy as np
import pandas as pd
import os
Plotting
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
Inline plotting
%matplotlib inline
sns.set_style('ticks')
matplotlib.rcParams['figure.figsize'] = [4, 4]
matplotlib.rcParams['figure.dpi'] = 100
matplotlib.rcParams['image.cmap'] = 'Spectral_r'
warnings.filterwarnings(action="ignore", module="matplotlib", message="findfont")
Reset random seed
np.random.seed(5)
Load sample data
palantir_dir = os.path.expanduser('data/')
ad = sc.read(palantir_dir + 'marrow_sample_scseq_counts.h5ad')
ad
sc.pp.normalize_per_cell(ad)
palantir.preprocess.log_transform(ad)
Error message
NotImplementedError Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 palantir.preprocess.log_transform(ad)
File C:\ProgramData\Anaconda3\envs\r-reticulate\lib\site-packages\palantir\preprocess.py:43, in log_transform(data, pseudo_count)
37 """Log transform the matrix
38
39 :param data: Counts matrix: Cells x Genes
40 :return: Log transformed matrix
41 """
42 if type(data) is sc.AnnData:
---> 43 data.X = np.log2(data.X + pseudo_count) - np.log2(pseudo_count)
44 else:
45 return np.log2(data + pseudo_count)
File C:\ProgramData\Anaconda3\envs\r-reticulate\lib\site-packages\scipy\sparse\base.py:410, in spmatrix.add(self, other)
408 return self.copy()
409 # Now we would add this scalar to every element.
--> 410 raise NotImplementedError('adding a nonzero scalar to a '
411 'sparse matrix is not supported')
412 elif isspmatrix(other):
413 if other.shape != self.shape:
NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported
palantir/SciPy/NumPy/Python version information
1.0.1 1.9.0 1.22.3 sys.version_info(major=3, minor=9, micro=12, releaselevel='final', serial=0)
Hello - very sorry for the delayed response.
You should be able to resolve this issues using
from spicy.sparse import csr_matrix
ad.X = csr_matrix(ad.X)
Hi @ManuSetty
I tried the same solution, but it does not seem to solve the issue described above. I took the liberty to inspect the log_transform
function in palantir. My suggestion is to replace if type(ad) is sc.Anndata
by if isinstance(ad, anndata.AnnData)
and import anndata
in your header.
I tested the adapted log_transform
function in Google Colabs and it worked fine.
Thank you!