Skip to content

scrublet crashes on dense input #1645

Closed

Description

@fbnrst
  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of scanpy.
  • (optional) I have confirmed this bug exists on the master branch of scanpy.

Minimal code sample (that we can copy&paste without having any data)

import scanpy as sc
adata = sc.datasets.paul15()
sc.external.pp.scrublet(adata, threshold=0.1)
WARNING: In Scanpy 0.*, this returned logarithmized data. Now it returns non-logarithmized data.
... storing 'paul15_clusters' as categorical
/opt/conda/lib/python3.7/site-packages/pandas/core/arrays/categorical.py:2487: FutureWarning: The `inplace` parameter in pandas.Categorical.remove_unused_categories is deprecated and will be removed in a future version.
  res = method(*args, **kwargs)
Trying to set attribute `.uns` of view, copying.
/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_normalization.py:138: UserWarning: Revieved a view of an AnnData. Making a copy.
  view_to_actual(adata)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-3dabe52b6132> in <module>
      1 import scanpy as sc
      2 adata = sc.datasets.paul15()
----> 3 sc.external.pp.scrublet(adata, threshold=0.1)

/opt/conda/lib/python3.7/site-packages/scanpy/external/pp/_scrublet.py in scrublet(adata, adata_sim, sim_doublet_ratio, expected_doublet_rate, stdev_doublet_rate, synthetic_doublet_umi_subsampling, knn_dist_metric, normalize_variance, log_transform, mean_center, n_prin_comps, use_approx_neighbors, get_doublet_neighbor_parents, n_neighbors, threshold, verbose, copy, random_state)
    208         expected_doublet_rate=expected_doublet_rate,
    209         stdev_doublet_rate=stdev_doublet_rate,
--> 210         random_state=random_state,
    211     )
    212 

/opt/conda/lib/python3.7/site-packages/scanpy/external/pp/_scrublet.py in _scrublet_call_doublets(adata_obs, adata_sim, n_neighbors, expected_doublet_rate, stdev_doublet_rate, mean_center, normalize_variance, n_prin_comps, use_approx_neighbors, knn_dist_metric, get_doublet_neighbor_parents, random_state, verbose)
    349 
    350     if mean_center and normalize_variance:
--> 351         sl.pipeline_zscore(scrub)
    352     elif mean_center:
    353         sl.pipeline_mean_center(scrub)

/opt/conda/lib/python3.7/site-packages/scrublet/helper_functions.py in pipeline_zscore(self)
     62 def pipeline_zscore(self):
     63     gene_means = self._E_obs_norm.mean(0)
---> 64     gene_stdevs = np.sqrt(sparse_var(self._E_obs_norm))
     65     self._E_obs_norm = np.array(sparse_zscore(self._E_obs_norm, gene_means, gene_stdevs))
     66     if self._E_sim_norm is not None:

/opt/conda/lib/python3.7/site-packages/scrublet/helper_functions.py in sparse_var(E, axis)
    153     ''' variance across the specified axis '''
    154 
--> 155     mean_gene = E.mean(axis=axis).A.squeeze()
    156     tmp = E.copy()
    157     tmp.data **= 2

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

Versions

Details

scanpy==1.7.0 anndata==0.7.5 umap==0.5.1 numpy==1.20.0 scipy==1.6.0 pandas==1.2.1 scikit-learn==0.24.1 statsmodels==0.12.2 python-igraph==0.8.3 louvain==0.7.0 leidenalg==0.8.3

Activity

  1. ivirshup commented on Feb 12, 2021

    @ivirshup
    Member

    I think this is upstream in scrublet, and I'm not sure it's appropriate to just convert whatever array is passed to a sparse array.

  2. fidelram commented on Feb 12, 2021

    @fidelram
    Collaborator

    From the error message, you may want to try to convert the dense matrix to sparse matrix format as follows:

    from scipy.sparse impor csr_matrix
    adata.X = csr_matrix(adata.X)
  3. fbnrst commented on Feb 12, 2021

    @fbnrst
    ContributorAuthor

    Sure, this works as a workaround. Thing is, if I call doublets like described in the scrublet README, I do not get an error:
    This works:

    import scrublet as scr
    import scanpy as sc
    adata = sc.datasets.paul15()
    
    scrub = scr.Scrublet(X)
    doublet_scores, predicted_doublets = scrub.scrub_doublets()

    So is this upstream?

  4. ivirshup commented on Mar 2, 2021

    @ivirshup
    Member

    @pinin4fjords, any thoughts on this?

  5. pinin4fjords commented on Mar 2, 2021

    @pinin4fjords
    Contributor

    Ahh, I think this is just because of the way I've tried to translate into to the Scanpy workflow. There's a sparsing step at the start of the basic Scrublet workflow, but I'm injecting the normalised matrix and effectively skipping that step.

    I'll PR a sparsing check and conversion (and yes @ivirshup , I'll add a test :-) ), but the workaround is perfectly valid for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions