Skip to content

Commit 702692b

Browse files
adapted to anndata not exporting anndata.base anymore
Adapting according to discussion at scverse/anndata#174.
1 parent 73c8937 commit 702692b

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

diffxpy/testing/tests.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import scipy.sparse
99
import xarray as xr
1010

11+
try:
12+
from anndata.base import Raw
13+
except ImportError:
14+
from anndata import Raw
15+
1116
from batchglm import data as data_utils
1217
from batchglm.xarray_sparse import SparseXArrayDataSet
1318
from diffxpy import pkg_constants
@@ -197,7 +202,7 @@ def _fit(
197202

198203

199204
def lrt(
200-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
205+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
201206
full_formula_loc: str,
202207
reduced_formula_loc: str,
203208
full_formula_scale: str = "~1",
@@ -382,7 +387,7 @@ def lrt(
382387

383388

384389
def wald(
385-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
390+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
386391
factor_loc_totest: Union[str, List[str]] = None,
387392
coef_to_test: Union[str, List[str]] = None,
388393
formula_loc: Union[None, str] = None,
@@ -659,7 +664,7 @@ def wald(
659664

660665

661666
def t_test(
662-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
667+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
663668
grouping,
664669
gene_names: Union[np.ndarray, list] = None,
665670
sample_description: pd.DataFrame = None,
@@ -700,7 +705,7 @@ def t_test(
700705

701706

702707
def rank_test(
703-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
708+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
704709
grouping: Union[str, np.ndarray, list],
705710
gene_names: Union[np.ndarray, list] = None,
706711
sample_description: pd.DataFrame = None,
@@ -741,7 +746,7 @@ def rank_test(
741746

742747

743748
def two_sample(
744-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
749+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
745750
grouping: Union[str, np.ndarray, list],
746751
as_numeric: Union[List[str], Tuple[str], str] = (),
747752
test: str = "t-test",
@@ -909,7 +914,7 @@ def two_sample(
909914

910915

911916
def pairwise(
912-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
917+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
913918
grouping: Union[str, np.ndarray, list],
914919
as_numeric: Union[List[str], Tuple[str], str] = (),
915920
test: str = 'z-test',
@@ -1116,7 +1121,7 @@ def pairwise(
11161121

11171122

11181123
def versus_rest(
1119-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
1124+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
11201125
grouping: Union[str, np.ndarray, list],
11211126
as_numeric: Union[List[str], Tuple[str], str] = (),
11221127
test: str = 'wald',
@@ -1274,7 +1279,7 @@ def versus_rest(
12741279

12751280

12761281
def partition(
1277-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
1282+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
12781283
parts: Union[str, np.ndarray, list],
12791284
gene_names: Union[np.ndarray, list] = None,
12801285
sample_description: pd.DataFrame = None
@@ -1672,7 +1677,7 @@ def wald(
16721677

16731678

16741679
def continuous_1d(
1675-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
1680+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
16761681
continuous: str,
16771682
df: int = 5,
16781683
factor_loc_totest: Union[str, List[str]] = None,

diffxpy/testing/utils.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
from typing import List, Tuple, Union
77
import xarray as xr
88

9+
try:
10+
from anndata.base import Raw
11+
except ImportError:
12+
from anndata import Raw
13+
914
from batchglm import data as data_utils
1015
# Relay util functions for diffxpy api.
1116
# design_matrix, preview_coef_names and constraint_system_from_star are redefined here.
@@ -16,7 +21,7 @@
1621

1722
def parse_gene_names(data, gene_names):
1823
if gene_names is None:
19-
if anndata is not None and (isinstance(data, anndata.AnnData) or isinstance(data, anndata.base.Raw)):
24+
if anndata is not None and (isinstance(data, anndata.AnnData) or isinstance(data, Raw)):
2025
gene_names = data.var_names
2126
elif isinstance(data, xr.DataArray):
2227
gene_names = data["features"]
@@ -37,7 +42,7 @@ def parse_data(data, gene_names) -> xr.DataArray:
3742

3843

3944
def parse_sample_description(
40-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
45+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
4146
sample_description: Union[pd.DataFrame, None]
4247
) -> pd.DataFrame:
4348
"""
@@ -63,8 +68,8 @@ def parse_sample_description(
6368
"with corresponding sample annotations"
6469
)
6570

66-
if anndata is not None and isinstance(data, anndata.base.Raw):
67-
# anndata.base.Raw does not have attribute shape.
71+
if anndata is not None and isinstance(data, Raw):
72+
# Raw does not have attribute shape.
6873
assert data.X.shape[0] == sample_description.shape[0], \
6974
"data matrix and sample description must contain same number of cells"
7075
else:
@@ -75,7 +80,7 @@ def parse_sample_description(
7580

7681
def parse_size_factors(
7782
size_factors: Union[np.ndarray, pd.core.series.Series, np.ndarray],
78-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
83+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray, scipy.sparse.csr_matrix],
7984
sample_description: pd.DataFrame
8085
) -> Union[np.ndarray, None]:
8186
"""
@@ -121,7 +126,7 @@ def dmat_unique(dmat, sample_description):
121126

122127

123128
def design_matrix(
124-
data: Union[anndata.AnnData, anndata.base.Raw, xr.DataArray, xr.Dataset, np.ndarray,
129+
data: Union[anndata.AnnData, Raw, xr.DataArray, xr.Dataset, np.ndarray,
125130
scipy.sparse.csr_matrix] = None,
126131
sample_description: Union[None, pd.DataFrame] = None,
127132
formula: Union[None, str] = None,

0 commit comments

Comments
 (0)