STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
193 stars 64 forks source link

'numpy.ndarray' object has no attribute 'toarray' running SingleR Annotation with cell labels #267

Closed maximelepetit closed 4 months ago

maximelepetit commented 5 months ago

Hi, i cannot run SingleR Annotation with cell labels.

I follow the tutorial :

ref_file = '/home/labex-cortex/Projects/SpatialTranscriptomics/stereoseq/data/l5_all.h5ad'
ref = st.io.read_h5ad(ref_file)
# preprocessing
ref.tl.log1p()
ref.tl.normalize_total()
data.tl.single_r(
    ref_exp_data=ref,
    ref_use_col='ClusterName',
    res_key='annotation',
    n_jobs=5,
    method='default'  #  Specifying the method as rapids means using gpu
)

The error :

INFO:numba.core.transforms:finding looplift candidates
[2024-04-24 16:19:42][Stereo][192975][MainThread][139761790124672][single_r][160][INFO]: start single-r with n_jobs=5 fine_tune_times=0
[2024-04-24 16:19:42][Stereo][192975][MainThread][139761790124672][single_r][162][INFO]: start training ref...
[2024-04-24 16:33:31][Stereo][192975][MainThread][139761790124672][single_r][165][INFO]: training ref finished, cost 828.4771797657013 seconds
[2024-04-24 16:33:31][Stereo][192975][MainThread][139761790124672][single_r][191][INFO]: start scoring test_data...

AttributeError                            Traceback (most recent call last)
Cell In[78], line 1
----> 1 data.tl.single_r(
      2     ref_exp_data=ref,
      3     ref_use_col='ClusterName',
      4     res_key='annotation',
      5     n_jobs=5,
      6     method='default'  #  Specifying the method as rapids means using gpu
      7 )

File ~/miniconda3/envs/st/lib/python3.8/site-packages/stereo/algorithm/single_r/single_r.py:193, in SingleR.main(self, ref_exp_data, ref_use_col, cluster_res_key, quantile, fine_tune_threshold, fine_tune_times, n_jobs, res_key, method, gpuid)
    191 logger.info('start scoring test_data...')
    192 start_time = time.time()
--> 193 output, labels_array = self._score_test_data(test_data, common_gene)
    194 logger.info(f'scoring test_data finished, cost {time.time() - start_time} seconds')
    196 logger.info('start fine-tuning...')

File ~/miniconda3/envs/st/lib/python3.8/site-packages/stereo/algorithm/single_r/single_r.py:280, in SingleR._score_test_data(self, test_data, common_gene)
    278     y_cell_bool_list = np.isin(self.ref_exp_data.cell_names, y['bins'].values)
    279     original_exp[x] = self.ref_exp_data.exp_matrix[y_cell_bool_list][:, common_gene_index].toarray()
--> 280 test_mat = test_data.exp_matrix[:, common_gene_index].toarray()
    281 ranked_mat_ref = apply_along_axis(test_mat.T)
    283 res_dict = dict(Parallel(n_jobs=self.n_jobs, backend="threading")(
    284     delayed(_spearman_parallel)(label, ranked_mat_ref, y.T, self.quantile)
    285     for label, y in tqdm(original_exp.items())
    286 ))

AttributeError: 'numpy.ndarray' object has no attribute 'toarray'

In contrast singler work good after clustering :


data.tl.single_r(
    ref_exp_data=ref, 
    ref_use_col='ClusterName', 
    cluster_res_key='leiden', 
    res_key='annotation'
)
[2024-04-23 15:13:28][Stereo][135079][MainThread][139942815433344][st_pipeline][77][INFO]: register algorithm single_r to <stereo.core.st_pipeline.StPipeline object at 0x7f46a0fc1b50>
INFO:numba.core.transforms:finding looplift candidates
[2024-04-23 15:14:01][Stereo][135079][MainThread][139942815433344][single_r][160][INFO]: start single-r with n_jobs=20 fine_tune_times=0
[2024-04-23 15:14:01][Stereo][135079][MainThread][139942815433344][single_r][162][INFO]: start training ref...
[2024-04-23 15:27:32][Stereo][135079][MainThread][139942815433344][single_r][165][INFO]: training ref finished, cost 811.0577094554901 seconds
[2024-04-23 15:27:46][Stereo][135079][MainThread][139942815433344][single_r][191][INFO]: start scoring test_data...
  0%|                                                   | 0/265 [00:00<?, ?it/s]INFO:numba.core.transforms:finding looplift candidates
100%|█████████████████████████████████████████| 265/265 [00:09<00:00, 26.70it/s]
[2024-04-23 15:28:06][Stereo][135079][MainThread][139942815433344][single_r][194][INFO]: scoring test_data finished, cost 19.836358070373535 seconds
[2024-04-23 15:28:06][Stereo][135079][MainThread][139942815433344][single_r][196][INFO]: start fine-tuning...
[2024-04-23 15:28:12][Stereo][135079][MainThread][139942815433344][single_r][302][INFO]: fine-tuning with test_data(shape=(6, 15490))
6it [00:00, 2562.71it/s]
INFO:numba.core.transforms:finding looplift candidates
INFO:numba.core.transforms:finding looplift candidates
[2024-04-23 15:28:29][Stereo][135079][MainThread][139942815433344][single_r][199][INFO]: fine-tuning finished, cost 23.345231771469116 seconds
[2024-04-23 15:28:29][Stereo][135079][MainThread][139942815433344][single_r][207][INFO]: single-r finished, cost 901.1310925483704 seconds

Can you help me ?

Bests.

Maxime

tanliwei-coder commented 5 months ago

SingleR only support sparse matrix currently, you can run data.array2sparse() to convert matrix from ndarray to sparse matrix.