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
168 lines (152 loc) · 7.5 KB
/
Copy pathtest_constructor.py
File metadata and controls
168 lines (152 loc) · 7.5 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
import os
import pytest
import iarray as ia
import numpy as np
# linspace
@pytest.mark.parametrize("start, stop, shape, chunkshape, blockshape, 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, chunkshape, blockshape, dtype):
if blockshape is None or chunkshape is None:
storage = ia.StorageProperties("plainbuffer")
else:
storage = ia.StorageProperties("blosc", chunkshape, blockshape, False)
size = int(np.prod(shape))
a = ia.linspace(ia.dtshape(shape, dtype), start, stop, storage=storage)
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, chunkshape, blockshape, 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, chunkshape, blockshape, dtype):
if blockshape is None or chunkshape is None:
storage = ia.StorageProperties("plainbuffer")
else:
storage = ia.StorageProperties("blosc", chunkshape, blockshape, False)
size = int(np.prod(shape))
step = (stop - start) / size
a = ia.arange(ia.dtshape(shape=shape, dtype=dtype), start, stop, step, storage=storage)
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, chunkshape, blockshape, dtype, filename",
[
(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, chunkshape, blockshape, dtype, filename):
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)
storage = ia.StorageProperties("blosc", chunkshape, blockshape, True, filename)
ia.numpy2iarray(a, storage=storage)
c = ia.load(filename)
d = ia.iarray2numpy(c)
np.testing.assert_almost_equal(a, d)
os.remove(filename)
# get_slice
@pytest.mark.parametrize(
"start, stop, slice, shape, chunkshape, blockshape, 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, chunkshape, blockshape, dtype):
if blockshape is None or chunkshape is None:
storage = ia.StorageProperties("plainbuffer")
else:
storage = ia.StorageProperties("blosc", chunkshape, blockshape, False)
size = int(np.prod(shape))
step = (stop - start) / size
a = ia.arange(ia.dtshape(shape=shape, dtype=dtype), start, stop, step, storage=storage)
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, chunkshape, blockshape, dtype",
[
([55, 123, 72], [10, 12, 25], [2, 3, 7], np.float64),
([10, 12, 5], None, None, np.float32),
])
def test_empty(shape, chunkshape, blockshape, dtype):
if blockshape is None or chunkshape is None:
storage = ia.StorageProperties("plainbuffer")
else:
storage = ia.StorageProperties("blosc", chunkshape, blockshape, False)
a = ia.empty(ia.dtshape(shape, dtype), storage=storage)
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, chunkshape, blockshape, 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, chunkshape, blockshape, dtype):
if blockshape is None or chunkshape is None:
storage = ia.StorageProperties("plainbuffer")
else:
storage = ia.StorageProperties("blosc", chunkshape, blockshape, False)
a = ia.zeros(ia.dtshape(shape, dtype), storage=storage)
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, chunkshape, blockshape, 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, chunkshape, blockshape, dtype):
if blockshape is None:
storage = ia.StorageProperties("plainbuffer")
else:
storage = ia.StorageProperties("blosc", chunkshape, blockshape, True)
a = ia.ones(ia.dtshape(shape, dtype), storage=storage)
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, chunkshape, blockshape, 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, chunkshape, blockshape, dtype):
if blockshape is None:
storage = ia.StorageProperties("plainbuffer")
else:
storage = ia.StorageProperties("blosc", chunkshape, blockshape, False)
a = ia.full(ia.dtshape(shape, dtype), fill_value, storage=storage)
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)