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

invalid class “dgRMatrix” object: slot j is not increasing inside a column #51

Closed
scottgigante opened this issue Aug 18, 2020 · 4 comments · Fixed by #52
Closed

Comments

@scottgigante
Copy link
Contributor

scottgigante commented Aug 18, 2020

I'm getting an error in scipy2ri.py2r on a CSR matrix.

scIB.preprocessing.normalize(adata)

----------------------
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/scIB/preprocessing.py", line 168, in normalize
    ro.globalenv['data_mat'] = adata.X.T
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/rpy2/robjects/environments.py", line 32, in __setitem__
    robj = conversion.converter.py2rpy(value)
  File "/opt/python/3.7-dev/lib/python3.7/functools.py", line 840, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/anndata2ri/scipy2ri/py2r.py", line 43, in wrapper
    return f(obj)
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/anndata2ri/scipy2ri/py2r.py", line 70, in csr_to_rmat
    Dim=as_integer(list(csr.shape)),
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/rpy2/robjects/functions.py", line 198, in __call__
    .__call__(*args, **kwargs))
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/rpy2/robjects/functions.py", line 125, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/rpy2/rinterface_lib/conversion.py", line 44, in _
    cdata = function(*args, **kwargs)
  File "/home/travis/virtualenv/python3.7-dev/lib/python3.7/site-packages/rpy2/rinterface.py", line 624, in __call__
    raise embedded.RRuntimeError(_rinterface._geterrmessage())
rpy2.rinterface_lib.embedded.RRuntimeError: Error in validObject(.Object) : 
  invalid class “dgRMatrix” object: slot j is not increasing inside a column
@flying-sheep
Copy link
Collaborator

flying-sheep commented Aug 18, 2020

Why is adata.X.T.indices not sorted? Shouldn’t that be guaranteed?

@scottgigante
Copy link
Contributor Author

indices is not, but indptr is.

import tempfile
import anndata
import os
import scprep
import numpy as np

URL = (
    "https://ndownloader.figshare.com/files/23992451?private_link=e3921450ec1bd0587870"
)
with tempfile.TemporaryDirectory() as tempdir:
    filepath = os.path.join(tempdir, "zebrafish.h5ad")
    scprep.io.download.download_url(URL, filepath)
    adata = anndata.read_h5ad(filepath)

np.all(adata.X.indices == np.sort(adata.X.indices))
# False
np.all(adata.X.indptr == np.sort(adata.X.indptr))
# True
np.all(adata.X.T.indices == np.sort(adata.X.T.indices))
# False
np.all(adata.X.T.indptr == np.sort(adata.X.T.indptr))
# True

@ivirshup
Copy link
Member

They're not always sorted, but there are a couple methods for checking and sorting these: csr_matrix.has_sorted_indices, csr_matrix.sort_indices, etc.

@ivirshup
Copy link
Member

Just a note for future reference, the results of those comparisons is pretty reasonable. The check for sorted-ness of indices should probably look more like:

all(np.array_equal(x, np.sort(x)) for x in np.split(s.indices, s.indptr[1:-1]))

And indptr is the only thing that has to be sorted.

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.

3 participants