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
199 lines (183 loc) · 6.46 KB
/
Copy pathtest_constructor.py
File metadata and controls
199 lines (183 loc) · 6.46 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
import os
import pytest
import numpy as np
import iarray as ia
# linspace
@pytest.mark.parametrize(
"start, stop, shape, chunks, blocks, dtype",
[
(0, 10, [100, 120, 50], [33, 21, 34], [12, 13, 7], np.float64),
(-0.1, -0.2, [40, 39, 52, 12], [12, 17, 6, 5], [5, 4, 6, 5], np.float32),
(0, 10, [55, 24, 31], None, None, np.float64),
(-0.1, -0.2, [4, 3, 5, 2], None, None, np.float32),
],
)
def test_linspace(start, stop, shape, chunks, blocks, dtype):
if blocks is None or chunks is None:
store = ia.Store(plainbuffer=True)
else:
store = ia.Store(chunks, blocks)
size = int(np.prod(shape))
a = ia.linspace(shape, start, stop, dtype=dtype, store=store)
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)
# arange
@pytest.mark.parametrize(
"start, stop, shape, chunks, blocks, dtype",
[
(0, 10, [22, 21, 51], [12, 14, 22], [5, 3, 6], np.float64),
(0, 1, [12, 12, 15, 13, 18, 19], [6, 5, 4, 7, 7, 5], [2, 2, 1, 2, 3, 3], np.float32),
(0, 10, [10, 12, 5], None, None, np.float64),
(-0.1, -0.2, [4, 3, 5, 2], None, None, np.float32),
],
)
def test_arange(start, stop, shape, chunks, blocks, dtype):
if blocks is None or chunks is None:
store = ia.Store(plainbuffer=True)
else:
store = ia.Store(chunks, blocks)
size = int(np.prod(shape))
step = (stop - start) / size
a = ia.arange(shape, start, stop, step, dtype=dtype, store=store)
b = ia.iarray2numpy(a)
npdtype = np.float64 if dtype == np.float64 else np.float32
c = np.arange(start, stop, step, dtype=npdtype).reshape(shape)
np.testing.assert_almost_equal(b, c)
# from_file
@pytest.mark.parametrize(
"start, stop, shape, chunks, blocks, dtype, urlpath",
[
(0, 10, [1234], [123], [21], np.float64, "test.fromfile0.iarray"),
(
-0.1,
-0.10,
[10, 12, 21, 31, 11],
[4, 3, 5, 5, 2],
[2, 3, 2, 3, 2],
np.float32,
"test.fromfile1.iarray",
),
],
)
def test_from_file(start, stop, shape, chunks, blocks, dtype, 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)
store = ia.Store(chunks, blocks, urlpath, enforce_frame=True)
ia.numpy2iarray(a, store=store)
c = ia.load(urlpath)
d = ia.iarray2numpy(c)
np.testing.assert_almost_equal(a, d)
os.remove(urlpath)
# get_slice
@pytest.mark.parametrize(
"start, stop, slice, shape, chunks, blocks, dtype",
[
(
0,
10,
(slice(2, 4), slice(5, 10), slice(1, 2)),
[21, 31, 21],
[10, 12, 5],
[3, 6, 2],
np.float64,
),
(-0.1, -0.2, (slice(2, 4), slice(7, 12)), [55, 123], [12, 16], [5, 7], np.float32),
(0, 10, (slice(2, 4), slice(5, 10), slice(1, 2)), [10, 12, 5], None, None, np.float64),
(-0.1, -0.2, (slice(2, 4), slice(7, 12)), [12, 16], None, None, np.float32),
],
)
def test_slice(start, stop, slice, shape, chunks, blocks, dtype):
if blocks is None or chunks is None:
store = ia.Store(plainbuffer=True)
else:
store = ia.Store(chunks, blocks)
size = int(np.prod(shape))
step = (stop - start) / size
a = ia.arange(shape, start, stop, step, dtype=dtype, store=store)
b = a[slice]
c = ia.iarray2numpy(b)
npdtype = np.float64 if dtype == np.float64 else np.float32
d = np.arange(start, stop, step, dtype=npdtype).reshape(shape)[slice]
np.testing.assert_almost_equal(c, d)
# empty # TODO: make it work properly
@pytest.mark.parametrize(
"shape, chunks, blocks, dtype",
[
([55, 123, 72], [10, 12, 25], [2, 3, 7], np.float64),
([10, 12, 5], None, None, np.float32),
],
)
def test_empty(shape, chunks, blocks, dtype):
if blocks is None or chunks is None:
store = ia.Store(plainbuffer=True)
else:
store = ia.Store(chunks, blocks)
with ia.config(store=store):
a = ia.empty(shape, dtype=dtype)
b = ia.iarray2numpy(a)
npdtype = np.float64 if dtype == np.float64 else np.float32
assert b.dtype == npdtype
assert b.shape == tuple(shape)
# zeros
@pytest.mark.parametrize(
"shape, chunks, blocks, dtype",
[
([134, 1234, 238], [10, 25, 35], [2, 7, 12], np.float64),
([456, 431], [102, 16], [12, 7], np.float32),
([10, 12, 5], None, None, np.float64),
([12, 16], None, None, np.float32),
],
)
def test_zeros(shape, chunks, blocks, dtype):
if blocks is None or chunks is None:
store = ia.Store(plainbuffer=True)
else:
store = ia.Store(chunks, blocks)
a = ia.zeros(shape, dtype=dtype, store=store)
b = ia.iarray2numpy(a)
npdtype = np.float64 if dtype == np.float64 else np.float32
c = np.zeros(shape, dtype=npdtype)
np.testing.assert_almost_equal(b, c)
# ones
@pytest.mark.parametrize(
"shape, chunks, blocks, dtype",
[
([456, 12, 234], [55, 6, 21], [12, 3, 5], np.float64),
([1024, 55], [66, 22], [12, 3], np.float32),
([10, 12, 5], None, None, np.float64),
([12, 16], None, None, np.float32),
],
)
def test_ones(shape, chunks, blocks, dtype):
if blocks is None:
store = ia.Store(plainbuffer=True)
else:
store = ia.Store(chunks, blocks, enforce_frame=True)
a = ia.ones(shape, dtype=dtype, store=store)
b = ia.iarray2numpy(a)
npdtype = np.float64 if dtype == np.float64 else np.float32
c = np.ones(shape, dtype=npdtype)
np.testing.assert_almost_equal(b, c)
# full
@pytest.mark.parametrize(
"fill_value, shape, chunks, blocks, dtype",
[
(8.34, [123, 432, 222], [24, 31, 15], [6, 6, 6], np.float64),
(2.00001, [567, 375], [52, 16], [9, 7], np.float32),
(8.34, [10, 12, 5], None, None, np.float64),
(2.00001, [12, 16], None, None, np.float32),
],
)
def test_full(fill_value, shape, chunks, blocks, dtype):
if blocks is None:
store = ia.Store(plainbuffer=True)
else:
store = ia.Store(chunks, blocks)
a = ia.full(shape, fill_value, dtype=dtype, store=store)
b = ia.iarray2numpy(a)
npdtype = np.float64 if dtype == np.float64 else np.float32
c = np.full(shape, fill_value, dtype=npdtype)
np.testing.assert_almost_equal(b, c)