-
Notifications
You must be signed in to change notification settings - Fork 14
/
loaders_test.py
219 lines (175 loc) · 7.27 KB
/
loaders_test.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Tests for loading files in loaders.
"""
import io
import zipfile
import numpy as np
import responses
from PIL import Image
from tifffile import TiffWriter
from deepcell_label.loaders import URLLoader
@responses.activate
def test_load_raw_npz():
"""Load NPZ file with just a raw array."""
expected = np.zeros((1, 1, 1, 1))
npz = io.BytesIO()
np.savez(npz, X=expected)
npz.seek(0)
url = 'http://example.com/mocked/raw.npz'
responses.add(responses.GET, url, body=io.BufferedReader(npz))
loader = URLLoader({'url': url})
np.testing.assert_array_equal(loader.raw_array, expected)
np.testing.assert_array_equal(loader.label_array, expected)
assert loader.cell_ids is not None
assert loader.cell_info is not None
@responses.activate
def test_load_raw_and_labeled_npz():
"""Loads NPZ file with both a raw and labeled array."""
expected_raw = np.zeros((1, 1, 1, 1))
expected_labeled = np.ones((1, 1, 1, 1))
npz = io.BytesIO()
np.savez(npz, X=expected_raw, y=expected_labeled)
npz.seek(0)
url = 'http://example.com/mocked/raw_and_labeled.npz'
responses.add(responses.GET, url, body=io.BufferedReader(npz))
loader = URLLoader({'url': url})
np.testing.assert_array_equal(loader.raw_array, expected_raw)
np.testing.assert_array_equal(loader.label_array, expected_labeled)
assert loader.cell_ids is not None
assert loader.cell_info is not None
@responses.activate
def test_load_separate_url_npz():
"""Loads raw and labeled arrays from separate responses."""
expected_raw = np.zeros((1, 1, 1, 1))
expected_labeled = np.ones((1, 1, 1, 1))
def create_npz():
npz = io.BytesIO()
np.savez(npz, X=expected_raw, y=expected_labeled)
npz.seek(0)
return io.BufferedReader(npz)
url = 'http://example.com/mocked/raw.npz'
labeled_url = 'http://example.com/mocked/labeled.npz'
responses.add(responses.GET, url, body=create_npz())
responses.add(responses.GET, labeled_url, body=create_npz())
loader = URLLoader({'url': url, 'labeled_url': labeled_url})
np.testing.assert_array_equal(loader.raw_array, expected_raw)
np.testing.assert_array_equal(loader.label_array, expected_labeled)
assert loader.cell_ids is not None
assert loader.cell_info is not None
@responses.activate
def test_load_raw_tiff():
"""Load raw array from tiff file."""
expected = np.zeros((1, 1, 1, 1))
tifffile = io.BytesIO()
with TiffWriter(tifffile) as writer:
writer.save(expected)
tifffile.seek(0)
url = 'http://example.com/mocked/raw.tiff'
responses.add(responses.GET, url, body=io.BufferedReader(tifffile))
loader = URLLoader({'url': url})
np.testing.assert_array_equal(loader.raw_array, expected)
np.testing.assert_array_equal(loader.label_array, expected)
assert loader.cell_ids is not None
assert loader.cell_info is not None
@responses.activate
def test_load_raw_and_labeled_tiff():
"""Load raw and labeled array from separate tiff files."""
expected_raw = np.zeros((1, 1, 1, 1))
expected_labeled = np.ones((1, 1, 1, 1))
raw = io.BytesIO()
labeled = io.BytesIO()
with TiffWriter(raw) as writer:
writer.save(expected_raw)
raw.seek(0)
with TiffWriter(labeled) as writer:
writer.save(expected_labeled)
labeled.seek(0)
url = 'http://example.com/mocked/raw.tiff'
labeled_url = 'http://example.com/mocked/labeled.tiff'
responses.add(responses.GET, url, body=io.BufferedReader(raw))
responses.add(responses.GET, labeled_url, body=io.BufferedReader(labeled))
loader = URLLoader({'url': url, 'labeled_url': labeled_url})
np.testing.assert_array_equal(loader.raw_array, expected_raw)
np.testing.assert_array_equal(loader.label_array, expected_labeled)
assert loader.cell_ids is not None
assert loader.cell_info is not None
@responses.activate
def test_load_raw_png():
"""Load raw array from png file."""
expected = np.zeros((1, 1, 1, 1))
raw = io.BytesIO()
img = Image.fromarray(np.zeros((1, 1)), mode='L')
img.save(raw, format="png")
raw.seek(0)
url = 'http://example.com/mocked/raw.png'
responses.add(responses.GET, url, body=io.BufferedReader(raw))
loader = URLLoader({'url': url})
np.testing.assert_array_equal(loader.raw_array, expected)
np.testing.assert_array_equal(loader.label_array, expected)
assert loader.cell_ids is not None
assert loader.cell_info is not None
@responses.activate
def test_load_raw_zip():
"""Load raw array from zip of tiff files."""
expected_raw = np.array([1, 2]).reshape((1, 1, 1, 2))
expected_labeled = np.array([0]).reshape((1, 1, 1, 1))
raw = io.BytesIO()
def make_tiff(array):
tiff = io.BytesIO()
with TiffWriter(tiff) as writer:
writer.save(array)
tiff.seek(0)
return tiff.read()
with zipfile.ZipFile(raw, mode='w') as zf:
with zf.open('channel0.tiff', 'w') as tiff:
channel_0 = make_tiff(np.array([1]).reshape((1, 1)))
tiff.write(channel_0)
with zf.open('channel1.tiff', 'w') as tiff:
channel_1 = make_tiff(np.array([2]).reshape((1, 1)))
tiff.write(channel_1)
raw.seek(0)
url = 'http://example.com/mocked/raw.zip'
responses.add(responses.GET, url, body=io.BufferedReader(raw))
loader = URLLoader({'url': url, 'axes': 'CYX'})
np.testing.assert_array_equal(loader.raw_array, expected_raw)
np.testing.assert_array_equal(loader.label_array, expected_labeled)
assert loader.cell_ids is not None
assert loader.cell_info is not None
@responses.activate
def test_load_labeled_zip():
"""Load labeled array from zip of tiff files."""
expected_raw = np.array([1, 2]).reshape((1, 1, 1, 2))
expected_labeled = np.array([3, 4]).reshape((1, 1, 1, 2))
raw = io.BytesIO()
labeled = io.BytesIO()
def make_tiff(array):
tiff = io.BytesIO()
with TiffWriter(tiff) as writer:
writer.save(array)
tiff.seek(0)
return tiff.read()
with zipfile.ZipFile(raw, mode='w') as zf:
with zf.open('channel0.tiff', 'w') as tiff:
channel_0 = make_tiff(np.array([1]).reshape((1, 1)))
tiff.write(channel_0)
with zf.open('channel1.tiff', 'w') as tiff:
channel_1 = make_tiff(np.array([2]).reshape((1, 1)))
tiff.write(channel_1)
raw.seek(0)
with zipfile.ZipFile(labeled, mode='w') as zf:
with zf.open('feature0.tiff', 'w') as tiff:
feature_0 = make_tiff(np.array([3]).reshape((1, 1)))
tiff.write(feature_0)
with zf.open('feature1.tiff', 'w') as tiff:
feature_1 = make_tiff(np.array([4]).reshape((1, 1)))
tiff.write(feature_1)
labeled.seek(0)
url = 'http://example.com/mocked/raw.zip'
labeled_url = 'http://example.com/mocked/labeled.zip'
responses.add(responses.GET, url, body=io.BufferedReader(raw))
responses.add(responses.GET, labeled_url, body=io.BufferedReader(labeled))
loader = URLLoader({'url': url, 'labeled_url': labeled_url, 'axes': 'CYX'})
np.testing.assert_array_equal(loader.raw_array, expected_raw)
np.testing.assert_array_equal(loader.label_array, expected_labeled)
assert loader.cell_ids is not None
assert loader.cell_info is not None