forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config_params.py
More file actions
276 lines (254 loc) · 9.79 KB
/
Copy pathtest_config_params.py
File metadata and controls
276 lines (254 loc) · 9.79 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
import pytest
import iarray as ia
@pytest.mark.parametrize(
"clevel, codec, filters, chunkshape, blockshape, enforce_frame",
[
(0, ia.Codecs.ZSTD,[ia.Filters.SHUFFLE], None, None, False),
(1, ia.Codecs.BLOSCLZ,[ia.Filters.SHUFFLE], [50, 50], [20, 20], True),
(9, ia.Codecs.ZSTD, [ia.Filters.SHUFFLE, ia.Filters.DELTA], [50, 50], [20, 20], False),
(6, ia.Codecs.ZSTD, [ia.Filters.SHUFFLE], [100, 50], [50, 20], False),
(0, ia.Codecs.ZSTD, [ia.Filters.SHUFFLE], None, None, False),
],
)
def test_global_config(clevel, codec, filters, chunkshape, blockshape, enforce_frame):
storage = ia.Storage(chunkshape, blockshape, enforce_frame=enforce_frame)
ia.set_config(clevel=clevel, codec=codec, filters=filters, storage=storage)
config = ia.get_config()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
storage2 = config.storage
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
assert storage2.enforce_frame == enforce_frame
# One can pass storage parameters straight to config() dataclass too
ia.set_config(
clevel=clevel,
codec=codec,
filters=filters,
chunkshape=chunkshape,
blockshape=blockshape,
enforce_frame=False,
)
config = ia.get_config()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
storage2 = ia.Storage()
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
assert storage2.enforce_frame == False
# Or, we can set defaults via Config (for better auto-completion)
cfg = ia.Config(
clevel=clevel,
codec=codec,
filters=filters,
chunkshape=chunkshape,
blockshape=blockshape,
enforce_frame=False,
)
ia.set_config(cfg)
config = ia.get_config()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
storage2 = ia.Storage()
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
assert storage2.enforce_frame == False
# Or, we can use a mix of Config and keyword args
cfg = ia.Config(
clevel=clevel,
codec=codec,
blockshape=blockshape,
enforce_frame=False,
)
ia.set_config(cfg, filters=filters, chunkshape=chunkshape)
config = ia.get_config()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
storage2 = ia.Storage()
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
assert storage2.enforce_frame == False
@pytest.mark.parametrize(
"favor, filters, chunkshape, blockshape",
[
(ia.Favors.BALANCE, [ia.Filters.BITSHUFFLE], None, None),
(ia.Favors.SPEED, [ia.Filters.SHUFFLE], [50, 50], [20, 20]),
(ia.Favors.CRATIO, [ia.Filters.BITSHUFFLE], [50, 50], [20, 20]),
],
)
def test_global_favor(favor, filters, chunkshape, blockshape):
storage = ia.Storage(chunkshape, blockshape)
ia.set_config(favor=favor, storage=storage)
config = ia.get_config()
assert config.favor == favor
assert config.filters == filters
assert config.storage.chunkshape == chunkshape
assert config.storage.blockshape == blockshape
@pytest.mark.parametrize(
"chunkshape, blockshape, shape",
[
(None, None, (100, 100)),
((50, 50), (20, 20), (100, 100)),
((50, 50), (20, 20), [10, 100]),
((100, 50), (50, 20), [100, 10]),
(None, None, ()),
],
)
def test_global_config_dtype(chunkshape, blockshape, shape):
try:
storage = ia.Storage(chunkshape, blockshape)
dtshape = ia.DTShape(shape)
cfg = ia.set_config(dtshape=dtshape, storage=storage)
storage2 = cfg.storage
if chunkshape is not None:
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
else:
# automatic partitioning
assert storage2.chunkshape <= shape
assert storage2.blockshape <= storage2.chunkshape
except ValueError:
# chunkshape cannot be set when a plainbuffer is used
assert shape == ()
# One can pass storage parameters straight to config() dataclass too
try:
dtshape = ia.DTShape(shape)
cfg = ia.set_config(
dtshape=dtshape,
chunkshape=chunkshape,
blockshape=blockshape,
enforce_frame=True,
)
storage2 = cfg.storage
if chunkshape is not None:
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
else:
# automatic partitioning
assert storage2.chunkshape <= shape
assert storage2.blockshape <= storage2.chunkshape
assert storage2.enforce_frame == True
except ValueError:
# chunkshape cannot be set when a plainbuffer is used
assert shape == ()
@pytest.mark.parametrize(
"clevel, codec, filters, chunkshape, blockshape, plainbuffer",
[
(0, ia.Codecs.ZSTD, [ia.Filters.SHUFFLE], None, None, False),
(1, ia.Codecs.BLOSCLZ, [ia.Filters.BITSHUFFLE], [50, 50], [20, 20], True),
(9, ia.Codecs.ZSTD, [ia.Filters.SHUFFLE, ia.Filters.DELTA], [50, 50], [20, 20], False),
(6, ia.Codecs.ZSTD, [ia.Filters.SHUFFLE], [100, 50], [50, 20], False),
(0, ia.Codecs.ZSTD, [ia.Filters.SHUFFLE], None, None, False),
],
)
def test_config_ctx(clevel, codec, filters, chunkshape, blockshape, plainbuffer):
try:
storage = ia.Storage(chunkshape, blockshape, plainbuffer=plainbuffer)
with ia.config(clevel=clevel, codec=codec, filters=filters, storage=storage) as cfg:
assert cfg.clevel == clevel
assert cfg.codec == codec
assert cfg.filters == filters
assert cfg.storage.chunkshape == chunkshape
assert cfg.storage.blockshape == blockshape
assert cfg.storage.plainbuffer == plainbuffer
except ValueError:
# chunkshape cannot be set when a plainbuffer is used
assert plainbuffer and chunkshape is not None
# One can pass storage parameters straight to config() dataclass too
try:
with ia.config(
clevel=clevel,
codec=codec,
filters=filters,
chunkshape=chunkshape,
blockshape=blockshape,
plainbuffer=False,
) as cfg:
assert cfg.clevel == clevel
assert cfg.codec == codec
assert cfg.filters == filters
assert cfg.storage.chunkshape == chunkshape
assert cfg.storage.blockshape == blockshape
assert cfg.storage.plainbuffer == False
except ValueError:
# chunkshape cannot be set when a plainbuffer is used
assert plainbuffer and chunkshape is not None
@pytest.mark.parametrize(
"chunkshape, blockshape, shape",
[
(None, None, (100, 100)),
((50, 50), (20, 20), (100, 100)),
((50, 50), (20, 20), [10, 100]),
((100, 50), (50, 20), [100, 10]),
(None, None, ()),
],
)
def test_config_ctx_dtype(chunkshape, blockshape, shape):
try:
storage = ia.Storage(chunkshape, blockshape)
dtshape = ia.DTShape(shape)
with ia.config(dtshape=dtshape, storage=storage) as cfg:
storage2 = cfg.storage
if chunkshape is not None:
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
else:
# automatic partitioning
assert storage2.chunkshape <= shape
assert storage2.blockshape <= storage2.chunkshape
except ValueError:
# chunkshape cannot be set when a plainbuffer is used
assert shape == ()
# One can pass storage parameters straight to config() dataclass too
try:
dtshape = ia.DTShape(shape)
with ia.config(
dtshape=dtshape,
chunkshape=chunkshape,
blockshape=blockshape,
enforce_frame=True,
) as cfg:
storage2 = cfg.storage
if chunkshape is not None:
assert storage2.chunkshape == chunkshape
assert storage2.blockshape == blockshape
else:
# automatic partitioning
assert storage2.chunkshape <= shape
assert storage2.blockshape <= storage2.chunkshape
assert storage2.enforce_frame == True
except ValueError:
# chunkshape cannot be set when a plainbuffer is used
assert shape == ()
def test_nested_contexts():
# Set the default to enable compression
ia.set_config(clevel=5)
a = ia.ones(ia.DTShape((100, 100)))
assert a.cratio > 1
# Now play with contexts and params in calls
# Disable compression in contexts
with ia.config(clevel=0) as cfg:
a = ia.ones(ia.DTShape((100, 100)), cfg, clevel=0)
assert a.cratio < 1
# Enable compression in call
a = ia.ones(ia.DTShape((100, 100)), cfg, clevel=1)
assert a.cratio > 1
# Enable compression in nested context
with ia.config(clevel=1) as cfg:
a = ia.ones(ia.DTShape((100, 100)), cfg)
assert a.cratio > 1
# Disable compression in double nested context
with ia.config(clevel=0) as cfg:
a = ia.ones(ia.DTShape((100, 100)), cfg)
assert a.cratio < 1
# Finally, the default should be enabling compression again
a = ia.ones(ia.DTShape((100, 100)))
assert a.cratio > 1
@pytest.fixture(scope="function", autouse=True)
def cleanup(request):
# Make the defaults sane for other tests to come
return request.addfinalizer(ia.reset_config_defaults)