Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lazappi opened this issue Aug 4, 2023 · 3 comments · May be fixed by #126
Open

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

lazappi opened this issue Aug 4, 2023 · 3 comments · May be fixed by #126

Comments

@lazappi
Copy link
Member

lazappi commented Aug 4, 2023

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
Copy link
Collaborator

flying-sheep commented Aug 4, 2023

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
Copy link
Member Author

lazappi commented Aug 4, 2023

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
Copy link
Collaborator

flying-sheep commented Aug 4, 2023

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

  • fix the float64 use case.
  • make sure the valueerror contains info about the real issue (dtype)

@flying-sheep flying-sheep changed the title Conversion 'py2rpy' not defined for objects of type '<class 'scipy.sparse._csr.csr_matrix'>' Unknown dtype dtype('int64') cannot be converted to ?gRMatrix. Aug 21, 2023
@flying-sheep flying-sheep linked a pull request Aug 21, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants