theislab / anndata2ri

Convert between AnnData and SingleCellExperiment
https://icb-anndata2ri.readthedocs-hosted.com/
GNU General Public License v3.0
121 stars 16 forks source link

Unknown dtype dtype('int64') cannot be converted to ?gRMatrix. #122

Open lazappi opened 11 months ago

lazappi commented 11 months ago

I'm having issues converting scipy matrices (possibly other things as well but this is the reproducible example).

import anndata2ri
from rpy2.robjects import globalenv
from rpy2.robjects.conversion import localconverter
from numpy.random import poisson
from scipy.sparse import csr_matrix

mat = csr_matrix(poisson(1, size=(100, 2000)))

with localconverter(anndata2ri.converter):
    globalenv["mat"] = mat
Traceback (most recent call last):
  File "/Users/luke.zappia/Documents/Code/theislab/extended-single-cell-best-practices/rpy2-test.py", line 10, in <module>
    globalenv["mat"] = mat
  File "/Users/luke.zappia/miniconda3/envs/interoperability/lib/python3.9/site-packages/rpy2/robjects/environments.py", line 35, in __setitem__
    robj = conversion.get_conversion().py2rpy(value)
  File "/Users/luke.zappia/miniconda3/envs/interoperability/lib/python3.9/functools.py", line 888, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/Users/luke.zappia/miniconda3/envs/interoperability/lib/python3.9/site-packages/rpy2/robjects/conversion.py", line 240, in _py2rpy
    raise NotImplementedError(
NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'scipy.sparse._csr.csr_matrix'>'

Conda environment (I have also tried older scipy versions with the same result)

channels:
  - conda-forge
  - bioconda
  - defaults
dependencies:
  - conda-forge::python=3.9.16
  - conda-forge::jupyterlab=3.6.3
  - conda-forge::scanpy=1.9.3
  - anndata2ri=1.2
  - bioconductor-basilisk=1.9.12
  - bioconductor-scuttle=1.8.0
  - bioconductor-singlecellexperiment=1.20.0
  - bioconductor-zellkonverter=1.8.0
  - ipywidgets=8.1.0
  - r-base=4.2
  - r-hdf5r=1.3.8
  - r-remotes=2.4.2
  - r-reticulate=1.30
  - r-sessioninfo=1.2.2
  - r-seurat=4.3.0
  - r-seuratobject=4.1.3
  - rpy2=3.5.11
  - session-info=1.0.0
flying-sheep commented 11 months ago

Hm, seems like there’s an issue with the docs, sorry! What happens if you use

with localconverter(anndata2ri._conv.full_converter()):
    globalenv["mat"] = mat

or

adata = AnnData(mat)

with localconverter(anndata2ri._conv.full_converter()):
    globalenv["adata"] = adata
lazappi commented 11 months ago

Initially I got:

ValueError: Unknown dtype dtype('int64') cannot be converted to ?gRMatrix.

But if I change to:

mat = csr_matrix(poisson(1, size=(100, 2000)), dtype=numpy.float32)

Then both of these work. Thanks!

Should this same code be used within a notebook or is it different in that case?

flying-sheep commented 11 months ago

In notebooks, the best version is to still use activate, but I’ll get around to fix #109 eventually.

rpy2 is getting rid of activate, that’s why it has to change.

I’ll leave this open to