forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_type_view.py
More file actions
113 lines (99 loc) · 3.05 KB
/
Copy pathtest_type_view.py
File metadata and controls
113 lines (99 loc) · 3.05 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
import pytest
import iarray as ia
import numpy as np
@pytest.mark.parametrize(
"dtype, view_dtype",
[
(np.dtype(np.float32), np.dtype(np.int64)),
(np.dtype(np.uint64), np.float64),
(np.int64, np.dtype(np.float64)),
(np.int8, np.bool_),
(np.bool_, np.float32),
],
)
@pytest.mark.parametrize(
"shape, chunks, blocks, contiguous, urlpath",
[
pytest.param(
[123, 432, 222], [24, 31, 15], [24, 31, 15], True, None, marks=pytest.mark.heavy
),
([567, 375], [52, 16], [52, 16], False, "test_type_sparse.iarr"),
([10, 12, 5], [5, 5, 5], [5, 5, 5], True, "test_type_contiguous.iarr"),
([12, 16], [12, 16], [3, 5], False, None),
],
)
def test_type(shape, chunks, blocks, dtype, view_dtype, contiguous, urlpath):
ia.remove_urlpath(urlpath)
size = np.prod(shape)
if dtype == np.bool_:
a = ia.full(shape, True, dtype=dtype)
else:
a = ia.arange(size, shape=shape, dtype=dtype)
b = ia.iarray2numpy(a)
assert not a.is_view
c = ia.astype(a, view_dtype)
assert c.is_view
c = ia.iarray2numpy(c)
d = b.astype(view_dtype)
if view_dtype in [np.float64, np.float32]:
np.testing.assert_almost_equal(c, d)
else:
np.testing.assert_array_equal(c, d)
ia.remove_urlpath(urlpath)
# Slice + type views
slice_data = [
([30, 100], [20, 20], [10, 13], True, None),
([30, 130], [50, 50], [20, 25], False, None),
pytest.param(
[10, 78, 55, 21],
[3, 30, 30, 21],
[3, 12, 6, 21],
True,
"test_slice_acontiguous.iarr",
marks=pytest.mark.heavy,
),
([30, 100], [30, 44], [30, 2], False, "test_slice_asparse.iarr"),
]
@pytest.mark.parametrize(
"dtype, view_dtype",
[
(np.float32, np.uint64),
(np.uint64, np.float64),
(np.uint8, np.bool_),
(np.int16, np.uint32),
],
)
@pytest.mark.parametrize(
"shape, chunks, blocks, acontiguous, aurlpath",
slice_data,
)
def test_slice_type(shape, chunks, blocks, acontiguous, aurlpath, dtype, view_dtype):
ia.remove_urlpath(aurlpath)
cfg = ia.Config(
chunks=chunks, blocks=blocks, contiguous=acontiguous, urlpath=aurlpath, nthreads=1
)
max = 1
if dtype not in [np.float64, np.float32]:
for i in range(len(shape)):
max *= shape[i]
a = ia.linspace(0, max, int(np.prod(shape)), shape=shape, cfg=cfg, mode="w", dtype=dtype)
an = ia.iarray2numpy(a)
slices = tuple([slice(0, s - 1) for s in shape])
a[slices] = 0
an[slices] = 0
if dtype in [np.float32, np.float64]:
np.testing.assert_almost_equal(a.data, an)
else:
np.testing.assert_equal(a.data, an)
b_ = a[slices]
c = ia.astype(b_, view_dtype)
d_ = an[slices]
d = d_.astype(view_dtype)
bn = ia.iarray2numpy(c)
assert d.shape == bn.shape
assert d.ndim == bn.ndim
if view_dtype in [np.float32, np.float64]:
np.testing.assert_almost_equal(d, bn)
else:
np.testing.assert_equal(d, bn)
ia.remove_urlpath(aurlpath)