forked from hyperspy/hyperspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lazy.py
391 lines (343 loc) · 13.5 KB
/
test_lazy.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# -*- coding: utf-8 -*-
# Copyright 2007-2020 The HyperSpy developers
#
# This file is part of HyperSpy.
#
# HyperSpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HyperSpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HyperSpy. If not, see <http://www.gnu.org/licenses/>.
import dask.array as da
import numpy as np
import pytest
from dask.threaded import get
import hyperspy.api as hs
from hyperspy import _lazy_signals
from hyperspy._signals.lazy import (
_reshuffle_mixed_blocks, to_array, get_navigation_dimension_host_chunk_slice)
from hyperspy.exceptions import VisibleDeprecationWarning
def _signal():
ar = da.from_array(np.arange(6. * 9 * 7 * 11).reshape((6, 9, 7, 11)),
chunks=((2, 1, 3), (4, 5), (7,), (11,))
)
return _lazy_signals.LazySignal2D(ar)
@pytest.fixture
def signal():
return _signal()
@pytest.mark.parametrize("sl", [(0, 0),
(slice(None,), 0),
(slice(None), slice(None))
]
)
def test_reshuffle(signal, sl):
sig = signal.isig[sl]
array = np.concatenate(
[a for a in sig._block_iterator(flat_signal=True,
navigation_mask=None,
signal_mask=None)],
axis=0
)
ndim = sig.axes_manager.navigation_dimension
ans = _reshuffle_mixed_blocks(array,
ndim,
sig.data.shape[ndim:],
sig.data.chunks[:ndim])
np.testing.assert_allclose(ans, sig.data.compute())
nav_mask = np.zeros((6, 9), dtype=bool)
nav_mask[0, 0] = True
nav_mask[1, 1] = True
sig_mask = np.zeros((7, 11), dtype=bool)
sig_mask[0, :] = True
@pytest.mark.parametrize('nm', [None, nav_mask])
@pytest.mark.parametrize('sm', [None, sig_mask])
@pytest.mark.parametrize('flat', [True, False])
@pytest.mark.parametrize('dtype', ['float', 'int'])
def test_blockiter_bothmasks(signal, flat, dtype, nm, sm):
real_first = get(signal.data.dask, (signal.data.name, 0, 0, 0, 0)).copy()
real_second = get(signal.data.dask, (signal.data.name, 0, 1, 0, 0)).copy()
# Don't want to rechunk, so change dtype manually
signal.data = signal.data.astype(dtype)
it = signal._block_iterator(flat_signal=flat,
navigation_mask=nm,
signal_mask=sm,
get=get)
first_block = next(it)
second_block = next(it)
if nm is not None:
nm = nm[:2, :4]
real_first = real_first.astype(dtype)
real_second = real_second.astype(dtype)
if flat:
if nm is not None:
nm = ~nm
navslice = np.where(nm.flat)[0]
else:
navslice = slice(None)
sigslice = slice(11, None) if sm is not None else slice(None)
slices1 = (navslice, sigslice)
real_first = real_first.reshape((2 * 4, -1))[slices1]
real_second = real_second.reshape((2 * 5, -1))[:, sigslice]
else:
value = np.nan if dtype == 'float' else 0
if nm is not None:
real_first[nm, ...] = value
if sm is not None:
real_first[..., sm] = value
real_second[..., sm] = value
np.testing.assert_allclose(first_block, real_first)
np.testing.assert_allclose(second_block, real_second)
@pytest.mark.parametrize('sig', [_signal(),
_signal().data,
_signal().data.compute()])
def test_as_array_numpy(sig):
thing = to_array(sig, chunks=None)
assert isinstance(thing, np.ndarray)
@pytest.mark.parametrize('sig', [_signal(),
_signal().data,
_signal().data.compute()])
def test_as_array_dask(sig):
chunks = ((6,), (9,), (7,), (11,))
thing = to_array(sig, chunks=chunks)
assert isinstance(thing, da.Array)
assert thing.chunks == chunks
def test_as_array_fail():
with pytest.raises(ValueError):
to_array('asd', chunks=None)
def test_ma_lazify():
s = hs.signals.BaseSignal(
np.ma.masked_array(
data=[
1, 2, 3], mask=[
0, 1, 0]))
l = s.as_lazy()
assert np.isnan(l.data[1].compute())
ss = hs.stack([s, s])
assert np.isnan(ss.data[:, 1]).all()
def test_warning():
sig = _signal()
with pytest.warns(VisibleDeprecationWarning, match="progressbar"):
sig.compute(progressbar=False)
assert sig._lazy == False
thing = to_array(sig, chunks=None)
assert isinstance(thing, np.ndarray)
class TestGetNavigationDimensionHostChunkSlice:
@pytest.mark.parametrize(
"position, chunk_slice",
[
((0, 0), np.s_[0:2, 0:2]),
((0, 19), np.s_[0:2, 18:20]),
((9, 9), np.s_[8:10, 8:10]),
((5, 14), np.s_[4:6, 14:16]),
],
)
def test_simple(self, position, chunk_slice):
dask_array = da.zeros((10, 20, 50, 50), chunks=(2, 2, 25, 25))
chunk_slice_output = get_navigation_dimension_host_chunk_slice(
position, dask_array.chunks
)
assert chunk_slice_output[:2] == chunk_slice
@pytest.mark.parametrize(
"position",
[(12, 0), (0, 25), (25, 32)],
)
def test_out_of_range(self, position):
dask_array = da.zeros((10, 20, 50, 50), chunks=(2, 2, 25, 25))
chunk_slice_output = get_navigation_dimension_host_chunk_slice(
position, dask_array.chunks
)
assert not chunk_slice_output
@pytest.mark.parametrize(
"shape",
[
(10, 20),
(6, 10, 20),
(4, 6, 10, 20),
(4, 4, 6, 10, 20),
(2, 4, 4, 6, 10, 20),
],
)
def test_dimensions(self, shape):
dask_array = da.zeros(shape, chunks=(2,) * len(shape))
position = (1,) * len(shape)
chunk_slice_output = get_navigation_dimension_host_chunk_slice(
position, dask_array.chunks
)
chunk_slice_compare = (slice(0, 2),) * len(shape)
assert chunk_slice_output == chunk_slice_compare
class TestGetTemporaryDaskChunk:
def test_correct_values(self):
chunk_slice_list = [
np.s_[0:5, 0:5],
np.s_[5:10, 0:5],
np.s_[0:5, 5:10],
np.s_[5:10, 5:10],
np.s_[0:5, 10:15],
np.s_[5:10, 10:15],
]
value_list = [1, 2, 3, 4, 5, 6]
data = np.zeros((10, 15, 50, 50))
for value, chunk_slice in zip(value_list, chunk_slice_list):
data[chunk_slice] = value
data = da.from_array(data, chunks=(5, 5, 25, 25))
s = _lazy_signals.LazySignal2D(data)
for value, chunk_slice in zip(value_list, chunk_slice_list):
value_output = s._get_temporary_dask_chunk(
(chunk_slice[0].start, chunk_slice[1].start, slice(None), slice(None))
)
assert s._temp_dask_chunk.shape == (5, 5, 50, 50)
assert np.all(s._temp_dask_chunk == value)
assert chunk_slice == s._temp_dask_chunk_slice
assert value == value_output.mean(dtype=np.uint16)
def test_change_position(self):
s = _lazy_signals.LazySignal2D(
da.zeros((10, 10, 20, 20), chunks=(5, 5, 10, 10))
)
s._get_temporary_dask_chunk((0, 0, slice(None), slice(None)))
chunk_slice0 = s._temp_dask_chunk_slice
s._temp_dask_chunk[:] = 2
s._get_temporary_dask_chunk((4, 4, slice(None), slice(None)))
chunk_slice1 = s._temp_dask_chunk_slice
assert chunk_slice0 == chunk_slice1
assert np.all(s._temp_dask_chunk == 2)
s._get_temporary_dask_chunk((6, 4, slice(None), slice(None)))
s._get_temporary_dask_chunk((0, 0, slice(None), slice(None)))
assert np.all(s._temp_dask_chunk == 0)
@pytest.mark.parametrize(
"shape",
[
(20, 30),
(10, 20, 30),
(6, 10, 20, 30),
(4, 6, 10, 20, 30),
(4, 4, 6, 10, 20, 30),
],
)
def test_dimensions(self, shape):
chunks = (2,) * len(shape)
s = _lazy_signals.LazySignal2D(da.zeros(shape), chunks=chunks)
position = s.axes_manager._getitem_tuple
s._get_temporary_dask_chunk(position)
assert len(position) == len(shape)
def test_correct_value_within_chunk(self):
data = np.zeros((10, 15, 50, 50))
data[0, 0] = 1
data[0, 1] = 2
data[1, 0] = 3
data[1, 1] = 4
data = da.from_array(data, chunks=(2, 2, 25, 25))
s = _lazy_signals.LazySignal2D(data)
value = s._get_temporary_dask_chunk(s.axes_manager._getitem_tuple)
assert np.all(value == 1)
s.axes_manager.indices = (1, 0)
value = s._get_temporary_dask_chunk(s.axes_manager._getitem_tuple)
assert np.all(value == 2)
s.axes_manager.indices = (0, 1)
value = s._get_temporary_dask_chunk(s.axes_manager._getitem_tuple)
assert np.all(value == 3)
s.axes_manager.indices = (1, 1)
value = s._get_temporary_dask_chunk(s.axes_manager._getitem_tuple)
assert np.all(value == 4)
def test_signal1d(self):
data = np.zeros((10, 10, 20))
data[5, 5] = 2
data = da.from_array(data, chunks=(2, 2, 10))
s = _lazy_signals.LazySignal1D(data)
value = s._get_temporary_dask_chunk(s.axes_manager._getitem_tuple)
assert len(s._temp_dask_chunk_slice) == 2
assert s._temp_dask_chunk.shape == (2, 2, 20)
assert s._temp_dask_chunk_slice == np.s_[0:2, 0:2]
assert len(value.shape) == 1
assert len(value) == 20
s.axes_manager.indices = (5, 5)
value = s._get_temporary_dask_chunk(s.axes_manager._getitem_tuple)
assert np.all(value == 2)
def test_changed_data(self):
s = _lazy_signals.LazySignal2D(da.zeros((6, 6, 8, 8), chunks=(2, 2, 4, 4)))
position = s.axes_manager._getitem_tuple
s._get_temporary_dask_chunk(position)
assert hasattr(s, "_temp_dask_chunk")
assert hasattr(s, "_temp_dask_chunk_slice")
s.events.data_changed.trigger(None)
assert not hasattr(s, "_temp_dask_chunk")
assert not hasattr(s, "_temp_dask_chunk_slice")
def test_map(self):
s = _lazy_signals.LazySignal2D(da.zeros((6, 6, 8, 8), chunks=(2, 2, 4, 4)))
s.__call__()
assert len(s._temp_dask_chunk.shape) == 4
s.map(np.sum, axis=1, ragged=False)
s.__call__()
assert len(s._temp_dask_chunk.shape) == 3
def test_clear_temp_dask_data(self):
s = _lazy_signals.LazySignal2D(da.zeros((6, 6, 8, 8), chunks=(2, 2, 4, 4)))
s.__call__()
s._clear_temp_dask_data(self)
assert not hasattr(s, "_temp_dask_data")
assert not hasattr(s, "_temp_dask_data_slice")
s._clear_temp_dask_data(self)
class TestLazyPlot:
def test_correct_value(self):
chunk_slice_list = [
np.s_[0:5, 0:5],
np.s_[5:10, 0:5],
np.s_[0:5, 5:10],
np.s_[5:10, 5:10],
np.s_[0:5, 10:15],
np.s_[5:10, 10:15],
]
value_list = [1, 2, 3, 4, 5, 6]
data = np.zeros((10, 15, 50, 50))
for value, chunk_slice in zip(value_list, chunk_slice_list):
data[chunk_slice] = value
data = da.from_array(data, chunks=(5, 5, 25, 25))
s = _lazy_signals.LazySignal2D(data)
for value, chunk_slice in zip(value_list, chunk_slice_list):
s.plot()
s.axes_manager.indices = (chunk_slice[1].start, chunk_slice[0].start)
assert s._temp_dask_chunk.shape == (5, 5, 50, 50)
assert np.all(s._temp_dask_chunk == value)
assert chunk_slice == s._temp_dask_chunk_slice
s._plot.close()
s._plot.close_navigator_plot()
@pytest.mark.parametrize(
"shape",
[
(20, 30),
(10, 20, 30),
(6, 10, 20, 30),
(4, 6, 10, 20, 30),
(4, 4, 6, 10, 20, 30),
],
)
def test_dimensions(self, shape):
chunks = (2,) * len(shape)
s = _lazy_signals.LazySignal2D(da.zeros(shape), chunks=chunks)
s.plot()
s._plot.close()
s._plot.close_navigator_plot()
def test_axes_manager(self):
data0 = np.zeros((30, 40, 50, 50), dtype=np.uint16)
data0[20, 20] = 100
data0 = da.from_array(data0, chunks=(10, 10, 25, 25))
s0 = _lazy_signals.LazySignal2D(data0)
data1 = da.zeros((30, 40, 50, 50), chunks=(10, 10, 25, 25))
s1 = _lazy_signals.LazySignal2D(data1)
s0.plot(axes_manager=s1.axes_manager)
s1.axes_manager.indices = (20, 20)
assert np.all(s0._temp_dask_chunk[0, 0] == 100)
s0._plot.close()
s0._plot.close_navigator_plot()
def test_signal1d(self):
s = _lazy_signals.LazySignal1D(da.zeros((10, 10, 20), chunks=(5, 5, 10)))
s.plot()
assert s._temp_dask_chunk.shape == (5, 5, 20)
assert s._temp_dask_chunk_slice == np.s_[0:5, 0:5]
s._plot.close()
s._plot.close_navigator_plot()