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

explicitly convert spmatrix #131

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions scprep/run/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .._lazyload import rpy2

import numpy as np
import scipy.sparse
import warnings


Expand Down Expand Up @@ -46,6 +47,18 @@ def _pysce2rpy(pyobject):
return pyobject


def _rpyspmatrix2py(robject):
if utils._try_import("anndata2ri"):
robject = anndata2ri.scipy2ri.rpy2py(robject)
return robject


def _pyspmatrix2rpy(pyobject):
if utils._try_import("anndata2ri") and isinstance(pyobject, scipy.sparse.spmatrix):
pyobject = anndata2ri.scipy2ri.py2rpy(pyobject)
return pyobject


def _is_r_object(obj):
return "rpy2.robjects" in str(type(obj)) or "rpy2.rinterface" in str(type(obj))

Expand Down Expand Up @@ -76,6 +89,7 @@ def rpy2py(robject):
for converter in [
_rpynull2py,
_rpysce2py,
_rpyspmatrix2py,
rpy2.robjects.pandas2ri.rpy2py,
_rpylist2py,
rpy2.robjects.numpy2ri.rpy2py,
Expand Down Expand Up @@ -119,6 +133,7 @@ def py2rpy(pyobject):
for converter in [
_pynull2rpy,
_pysce2rpy,
_pyspmatrix2rpy,
rpy2.robjects.pandas2ri.py2rpy,
rpy2.robjects.numpy2ri.py2rpy,
rpy2.robjects.conversion.py2rpy,
Expand Down
3 changes: 1 addition & 2 deletions scprep/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import numbers
import numpy as np
import pandas as pd
import scipy.sparse
import warnings

plt = matplotlib.pyplot
Expand Down Expand Up @@ -550,7 +549,7 @@ def differential_expression(
X, Y, measure="difference", direction="both", gene_names=None, n_jobs=-2
):
"""Calculate the most significant genes between two datasets.

If using ``measure="emd"``, the test statistic is multiplied by the sign of
the mean differencein order to allow for distinguishing between positive
and negative shifts. To ignore this, use ``direction="both"`` to sort by the
Expand Down
2 changes: 1 addition & 1 deletion scprep/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.2.0"
__version__ = "1.2.1"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"mock",
"h5py",
"matplotlib>=3.0",
"rpy2>=3.0",
"rpy2>=3.0,<3.4.3",
"black",
]

Expand Down
2 changes: 2 additions & 0 deletions test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ def test_conversion_spmatrix():
assert isinstance(x, scipy.sparse.csc_matrix)
assert x.shape == (2, 3)
assert np.all(x.toarray() == np.array([[1, 3, 5], [2, 4, 6]]))
y = scprep.run.conversion.py2rpy(x)
assert (scprep.run.conversion.rpy2py(y) != x).nnz == 0

def test_conversion_dataframe():
x = scprep.run.conversion.rpy2py(
Expand Down
1 change: 0 additions & 1 deletion test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import numpy as np
import os
import scipy.sparse
import scprep
import warnings

Expand Down