-
Notifications
You must be signed in to change notification settings - Fork 629
/
Copy pathtest_datasets.py
199 lines (153 loc) · 6.03 KB
/
test_datasets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""
Tests to make sure the example datasets load.
"""
from __future__ import annotations
import subprocess
import warnings
from collections import defaultdict
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING
import numpy as np
import pytest
from anndata.tests.helpers import assert_adata_equal
import scanpy as sc
from testing.scanpy._pytest.marks import needs
if TYPE_CHECKING:
from collections.abc import Callable
from anndata import AnnData
@pytest.fixture(autouse=True)
def _tmp_dataset_dir(tmp_path: Path) -> None:
"""Make sure that datasets are downloaded during the test run.
The default test environment stores them in a cached location.
"""
sc.settings.datasetdir = tmp_path / "scanpy_data"
@pytest.mark.internet
def test_burczynski06():
with pytest.warns(UserWarning, match=r"Variable names are not unique"):
adata = sc.datasets.burczynski06()
assert adata.shape == (127, 22283)
assert not (adata.X == 0).any()
@pytest.mark.internet
@needs.openpyxl
def test_moignard15():
with warnings.catch_warnings():
# https://foss.heptapod.net/openpyxl/openpyxl/-/issues/2051
warnings.filterwarnings(
"ignore",
r"datetime\.datetime\.utcnow\(\) is deprecated",
category=DeprecationWarning,
module="openpyxl",
)
adata = sc.datasets.moignard15()
assert adata.shape == (3934, 42)
@pytest.mark.internet
def test_paul15():
sc.datasets.paul15()
@pytest.mark.internet
def test_pbmc3k():
adata = sc.datasets.pbmc3k()
assert adata.shape == (2700, 32738)
assert "CD8A" in adata.var_names
@pytest.mark.internet
def test_pbmc3k_processed():
with warnings.catch_warnings(record=True) as records:
adata = sc.datasets.pbmc3k_processed()
assert adata.shape == (2638, 1838)
assert adata.raw.shape == (2638, 13714)
assert len(records) == 0
@pytest.mark.internet
def test_ebi_expression_atlas():
adata = sc.datasets.ebi_expression_atlas("E-MTAB-4888")
# The shape changes sometimes
assert 2261 <= adata.shape[0] <= 2315
assert 23899 <= adata.shape[1] <= 24051
def test_krumsiek11():
with pytest.warns(UserWarning, match=r"Observation names are not unique"):
adata = sc.datasets.krumsiek11()
assert adata.shape == (640, 11)
assert set(adata.obs["cell_type"]) == {"Ery", "Mk", "Mo", "Neu", "progenitor"}
def test_blobs():
n_obs = np.random.randint(15, 30)
n_var = np.random.randint(500, 600)
adata = sc.datasets.blobs(n_variables=n_var, n_observations=n_obs)
assert adata.shape == (n_obs, n_var)
def test_toggleswitch():
with pytest.warns(UserWarning, match=r"Observation names are not unique"):
sc.datasets.toggleswitch()
def test_pbmc68k_reduced():
with warnings.catch_warnings():
warnings.simplefilter("error")
sc.datasets.pbmc68k_reduced()
@pytest.mark.internet
def test_visium_datasets():
"""Tests that reading/ downloading works and is does not have global effects."""
with pytest.warns(UserWarning, match=r"Variable names are not unique"):
hheart = sc.datasets.visium_sge("V1_Human_Heart")
with pytest.warns(UserWarning, match=r"Variable names are not unique"):
hheart_again = sc.datasets.visium_sge("V1_Human_Heart")
assert_adata_equal(hheart, hheart_again)
@pytest.mark.internet
def test_visium_datasets_dir_change(tmp_path: Path):
"""Test that changing the dataset dir doesn't break reading."""
with pytest.warns(UserWarning, match=r"Variable names are not unique"):
mbrain = sc.datasets.visium_sge("V1_Adult_Mouse_Brain")
sc.settings.datasetdir = tmp_path
with pytest.warns(UserWarning, match=r"Variable names are not unique"):
mbrain_again = sc.datasets.visium_sge("V1_Adult_Mouse_Brain")
assert_adata_equal(mbrain, mbrain_again)
@pytest.mark.internet
def test_visium_datasets_images():
"""Test that image download works and is does not have global effects."""
# Test that downloading tissue image works
with pytest.warns(UserWarning, match=r"Variable names are not unique"):
mbrain = sc.datasets.visium_sge("V1_Adult_Mouse_Brain", include_hires_tiff=True)
expected_image_path = sc.settings.datasetdir / "V1_Adult_Mouse_Brain" / "image.tif"
image_path = Path(
mbrain.uns["spatial"]["V1_Adult_Mouse_Brain"]["metadata"]["source_image_path"]
)
assert image_path == expected_image_path
# Test that tissue image exists and is a valid image file
assert image_path.exists()
# Test that tissue image is a tif image file (using `file`)
process = subprocess.run(
["file", "--mime-type", image_path], stdout=subprocess.PIPE
)
output = process.stdout.strip().decode() # make process output string
assert output == str(image_path) + ": image/tiff"
def test_download_failure():
from urllib.error import HTTPError
with pytest.raises(HTTPError):
sc.datasets.ebi_expression_atlas("not_a_real_accession")
# These are tested via doctest
DS_INCLUDED = frozenset({"krumsiek11", "toggleswitch", "pbmc68k_reduced"})
# These have parameters that affect shape and so on
DS_DYNAMIC = frozenset({"ebi_expression_atlas"})
# Additional marks for datasets besides “internet”
DS_MARKS = defaultdict(list, moignard15=[needs.openpyxl])
@pytest.mark.parametrize(
"ds_name",
[
pytest.param(
ds,
id=ds,
marks=[
*(() if ds in DS_INCLUDED else [pytest.mark.internet]),
*DS_MARKS[ds],
],
)
for ds in sorted(set(sc.datasets.__all__) - DS_DYNAMIC)
],
)
def test_doc_shape(ds_name):
dataset_fn: Callable[[], AnnData] = getattr(sc.datasets, ds_name)
assert dataset_fn.__doc__, "No docstring"
docstring = dedent(dataset_fn.__doc__)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
r"(Observation|Variable) names are not unique",
category=UserWarning,
)
dataset = dataset_fn()
assert repr(dataset) in docstring