forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_constructor.py
More file actions
389 lines (358 loc) · 12.4 KB
/
Copy pathtest_constructor.py
File metadata and controls
389 lines (358 loc) · 12.4 KB
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
import pytest
import numpy as np
import iarray as ia
# linspace
@pytest.mark.parametrize(
"start, stop, shape, chunks, blocks, dtype, contiguous, urlpath",
[
pytest.param(
0,
10,
[100, 90, 50],
[33, 21, 34],
[12, 13, 7],
np.float64,
False,
"test_linspace_sparse.iarr",
marks=pytest.mark.heavy,
),
pytest.param(-0.1, -0.2, [40, 39, 52, 12], [12, 17, 6, 5], [5, 4, 6, 5], np.float32, True, None, marks=pytest.mark.heavy),
(
0,
10,
[55, 24, 31],
[55, 24, 15],
[55, 24, 5],
np.float64,
True,
"test_linspace_contiguous.iarr",
),
(-0.1, -0.2, [4, 3, 5, 2], [4, 3, 5, 2], [2, 3, 2, 2], np.float32, False, None),
],
)
def test_linspace(start, stop, shape, chunks, blocks, dtype, contiguous, urlpath):
size = int(np.prod(shape))
ia.remove_urlpath(urlpath)
a = ia.linspace(shape, start, stop, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=contiguous, urlpath=urlpath)
b = ia.iarray2numpy(a)
npdtype = np.float64 if dtype == np.float64 else np.float32
c = np.linspace(start, stop, size, dtype=npdtype).reshape(shape)
np.testing.assert_almost_equal(b, c)
ia.remove_urlpath(urlpath)
# arange
@pytest.mark.parametrize(
"start, stop, shape, chunks, blocks, dtype, contiguous, urlpath",
[
(
0,
10,
[22, 21, 51],
[12, 14, 22],
[5, 3, 6],
np.float64,
False,
"test_arange_sparse.iarr",
),
pytest.param(
0,
1,
[12, 12, 15, 13, 18, 19],
[6, 5, 4, 7, 7, 5],
[2, 2, 1, 2, 3, 3],
np.float32,
True,
None,
marks=pytest.mark.heavy
),
(
0,
10 * 12 * 5,
[10, 12, 5],
[5, 5, 5],
[2, 1, 2],
np.uint64,
True,
"test_arange_contiguous.iarr",
),
(-0.1, -0.2, [4, 3, 5, 2], [2, 2, 2, 2], [2, 2, 2, 2], np.int32, False, None),
],
)
def test_arange(start, stop, shape, chunks, blocks, dtype, contiguous, urlpath):
size = int(np.prod(shape))
step = (stop - start) / size
ia.remove_urlpath(urlpath)
a = ia.arange(shape, start, stop, step, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=contiguous, urlpath=urlpath)
b = ia.iarray2numpy(a)
npdtype = dtype
c = np.arange(start, stop, step, dtype=npdtype).reshape(shape)
np.testing.assert_almost_equal(b, c)
ia.remove_urlpath(urlpath)
# from_file
@pytest.mark.parametrize(
"start, stop, shape, chunks, blocks, dtype, contiguous, urlpath",
[
(0, 10, [1234], [123], [21], np.float64, False, "test.fromfile0.iarr"),
(
-0.1,
-0.10,
[10, 12, 21],
[4, 3, 5],
[2, 3, 2],
np.float32,
True,
"test.fromfile1.iarr",
),
pytest.param(
-0.1,
-0.10,
[10, 12, 21, 31, 11],
[4, 3, 5, 5, 2],
[2, 3, 2, 3, 2],
np.float32,
True,
"test.fromfile2.iarr",
marks=pytest.mark.heavy,
),
],
)
def test_from_file(start, stop, shape, chunks, blocks, dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
size = int(np.prod(shape))
npdtype = np.float64 if dtype == np.float64 else np.float32
a = np.linspace(start, stop, size, dtype=npdtype).reshape(shape)
ia.numpy2iarray(a, chunks=chunks, blocks=blocks, contiguous=contiguous, urlpath=urlpath)
c = ia.load(urlpath)
d = ia.iarray2numpy(c)
np.testing.assert_almost_equal(a, d)
ia.remove_urlpath(urlpath)
# get_slice
@pytest.mark.parametrize(
"start, stop, slice, shape, chunks, blocks, dtype, contiguous, urlpath",
[
(
0,
10,
(slice(2, 4), slice(5, 10), slice(1, 2)),
[21, 31, 21],
[10, 12, 5],
[3, 6, 2],
np.float64,
True,
None,
),
(
-0.1,
-0.2,
(slice(2, 4), slice(7, 12)),
[55, 123],
[12, 16],
[5, 7],
np.float32,
False,
"test_slice_sparse.iarr",
),
(
0,
10 * 12 * 5,
(slice(2, 4), slice(5, 10), slice(1, 2)),
[10, 12, 5],
[5, 6, 5],
[5, 6, 5],
np.uint32,
True,
"test_slice_contiguous.iarr",
),
(
-120 * 160,
0,
(slice(2, 4), slice(7, 12)),
[120, 160],
[120, 40],
[30, 20],
np.int16,
False,
None,
),
],
)
def test_slice(start, stop, slice, shape, chunks, blocks, dtype, contiguous, urlpath):
size = int(np.prod(shape))
step = (stop - start) / size
ia.remove_urlpath(urlpath)
a = ia.arange(shape, start, stop, step, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=contiguous,
urlpath=urlpath)
b = a[slice]
c = ia.iarray2numpy(b)
npdtype = dtype
d = np.arange(start, stop, step, dtype=npdtype).reshape(shape)[slice]
if dtype in [np.float64, np.float32]:
np.testing.assert_allclose(c, d)
else:
np.testing.assert_array_equal(c, d)
ia.remove_urlpath(urlpath)
# empty # TODO: make it work properly
@pytest.mark.parametrize(
"shape, chunks, blocks, dtype, contiguous, urlpath",
[
([55, 123, 72], [10, 12, 25], [2, 3, 7], np.float64, True, None),
([10, 12, 5], [10, 12, 1], [5, 12, 1], np.float32, False, "test_empty_sparse.iarr"),
([55, 123, 72], [10, 12, 25], [2, 3, 7], np.bool_, True, "test_empty_contiguous.iarr"),
([10, 12, 5], [5, 12, 5], [2, 12, 5], np.int64, False, None),
],
)
def test_empty(shape, chunks, blocks, dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
with ia.config(chunks=chunks, blocks=blocks, contiguous=contiguous, urlpath=urlpath):
a = ia.empty(shape, dtype=dtype)
b = ia.iarray2numpy(a)
npdtype = dtype
assert b.dtype == npdtype
assert b.shape == tuple(shape)
ia.remove_urlpath(urlpath)
# zeros
@pytest.mark.parametrize(
"shape, chunks, blocks, dtype, contiguous, urlpath",
[
pytest.param(
[134, 1234, 238],
[10, 25, 35],
[2, 7, 12],
np.float64,
True,
"test_zeros_contiguous.iarr",
marks=pytest.mark.heavy,
),
([456, 431], [102, 16], [12, 7], np.float32, False, "test_zeros_sparse.iarr"),
([10, 12, 5], [10, 1, 1], [10, 1, 1], np.int16, False, None),
([12, 16], [1, 16], [1, 16], np.uint8, True, None),
([12, 16], [1, 16], [1, 16], np.bool_, True, "test_zeros_contiguous.iarr"),
],
)
def test_zeros(shape, chunks, blocks, dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
a = ia.zeros(shape, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=contiguous, urlpath=urlpath)
b = ia.iarray2numpy(a)
npdtype = dtype
c = np.zeros(shape, dtype=npdtype)
if dtype in [np.float64, np.float32]:
np.testing.assert_almost_equal(b, c)
else:
np.testing.assert_array_equal(b, c)
ia.remove_urlpath(urlpath)
# ones
@pytest.mark.parametrize(
"shape, chunks, blocks, dtype, contiguous, urlpath",
[
([100, 12, 34], [55, 6, 21], [12, 3, 5], np.float64, False, None),
pytest.param([456, 12, 234], [55, 6, 21], [12, 3, 5], np.float64, False, None, marks=pytest.mark.heavy),
([100, 55], [66, 22], [12, 3], np.float32, True, "test_ones_contiguous.iarr"),
([10, 12, 5], [5, 6, 5], [5, 3, 5], np.int8, True, None),
([120, 130], [45, 64], [33, 12], np.uint16, False, "test_ones_sparse.iarr"),
([10, 12, 5], [5, 6, 5], [5, 3, 5], np.bool_, True, None),
],
)
def test_ones(shape, chunks, blocks, dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
a = ia.ones(shape, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=contiguous, urlpath=urlpath)
b = ia.iarray2numpy(a)
npdtype = dtype
c = np.ones(shape, dtype=npdtype)
if dtype in [np.float64, np.float32]:
np.testing.assert_almost_equal(b, c)
else:
np.testing.assert_array_equal(b, c)
ia.remove_urlpath(urlpath)
# full
@pytest.mark.parametrize(
"fill_value, shape, chunks, blocks, dtype, contiguous, urlpath",
[
pytest.param(8.34, [123, 432, 222], [24, 31, 15], [6, 6, 6], np.float64, True, None, marks=pytest.mark.heavy),
(2.00001, [56, 37], [20, 16], [9, 7], np.float32, False, "test_full_sparse.iarr"),
pytest.param(2.00001, [567, 375], [52, 16], [9, 7], np.float32, False, "test_full_sparse.iarr", marks=pytest.mark.heavy),
(8, [10, 12, 5], [5, 5, 5], [5, 5, 5], np.int32, True, "test_full_contiguous.iarr"),
(True, [12, 16], [12, 16], [10, 10], np.bool_, False, None),
],
)
def test_full(fill_value, shape, chunks, blocks, dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
a = ia.full(shape, fill_value, dtype=dtype, chunks=chunks, blocks=blocks, contiguous=contiguous, urlpath=urlpath)
b = ia.iarray2numpy(a)
npdtype = dtype
c = np.full(shape, fill_value, dtype=npdtype)
if dtype in [np.float64, np.float32]:
np.testing.assert_almost_equal(b, c)
else:
np.testing.assert_array_equal(b, c)
ia.remove_urlpath(urlpath)
# TODO: Update this when persistent sparse would be supported
@pytest.mark.parametrize(
"contiguous",
[
(True,),
(False,),
],
)
def test_overwrite(contiguous):
fname = "pepe.iarr"
ia.remove_urlpath(fname)
a = ia.arange([10, 20, 10, 14], contiguous=contiguous, urlpath=fname)
b = ia.arange([10, 20, 10, 14], contiguous=contiguous, urlpath=fname, mode="w")
with pytest.raises(IOError):
b = ia.arange([10, 20, 10, 14], contiguous=contiguous, urlpath=fname, mode="w-")
ia.remove_urlpath(fname)
# numpy views
@pytest.mark.parametrize(
"shape, starts, stops, dtype, contiguous, urlpath",
[
pytest.param([55, 123, 72], [10, 12, 25], [12, 14, 26], np.float64, False, "test_view_sparse.iarr", marks=pytest.mark.heavy),
([55, 123, 72], [10, 12, 25], [12, 14, 26], np.float64, True, "test_view_contiguous.iarr"),
([10, 12, 5], [3, 9, 1], [4, 12, 3], np.float32, True, None),
([10, 12, 5], [3, 9, 1], [4, 12, 3], np.float32, False, None),
],
)
def test_view(shape, starts, stops, dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
cfg = ia.set_config_defaults(contiguous=contiguous, urlpath=urlpath)
nelems = np.prod(shape)
a = np.linspace(0, 1, nelems, dtype=dtype).reshape(shape)
slice_ = tuple(slice(i, j) for i, j in zip(starts, stops))
a_view = a[slice_]
b = ia.numpy2iarray(a_view, cfg=cfg)
npdtype = np.float64 if dtype == np.float64 else np.float32
assert b.dtype == npdtype
assert b.shape == a_view.shape
np.testing.assert_almost_equal(a_view, b.data)
ia.remove_urlpath(urlpath)
# numpy arrays stored in Fortran ordering
@pytest.mark.parametrize(
"shape, dtype, contiguous, urlpath",
[
([55, 13, 2], np.float64, True, None),
([55, 13, 2], np.float64, False, None),
([10, 12], np.float32, False, "test_fortran_sparse.iarr"),
([10, 12], np.float32, True, "test_fortran_contiguous.iarr"),
],
)
def test_fortran(shape, dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
cfg = ia.set_config_defaults(contiguous=contiguous, urlpath=urlpath)
nelems = np.prod(shape)
a = np.linspace(0, 1, nelems, dtype=dtype).reshape(shape)
a_fortran = a.copy(order="F")
b = ia.numpy2iarray(a_fortran, cfg=cfg)
npdtype = dtype
assert b.dtype == npdtype
assert b.shape == a.shape
assert b.shape == a_fortran.shape
np.testing.assert_almost_equal(a_fortran, b.data)
ia.remove_urlpath(urlpath)
@pytest.mark.parametrize(
"clevel, codec, filters",
[
(1, ia.Codec.ZSTD, [ia.Filter.NOFILTER]),
],
)
def test_btune_copy(clevel, codec, filters):
a = ia.linspace([100], 0, 1)
a_copy = a.copy(clevel=clevel, codec=codec, filters=filters, btune=False)
np.testing.assert_almost_equal(a_copy.data, a.data)