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
350 lines (314 loc) · 10.5 KB
/
Copy pathtest_config_params.py
File metadata and controls
350 lines (314 loc) · 10.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
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
import pytest
import iarray as ia
@pytest.mark.parametrize(
"clevel, codec, filters, chunks, blocks, contiguous, urlpath",
[
(0, ia.Codec.ZSTD, [ia.Filter.SHUFFLE], None, None, False, None),
(1, ia.Codec.BLOSCLZ, [ia.Filter.SHUFFLE], [50, 50], [20, 20], True, None),
(
9,
ia.Codec.ZSTD,
[ia.Filter.SHUFFLE, ia.Filter.DELTA],
[50, 50],
[20, 20],
False,
b"test_config_params_sparse.iarr",
),
(
6,
ia.Codec.ZSTD,
[ia.Filter.SHUFFLE],
[100, 50],
[50, 20],
True,
b"test_config_params_contiguous.iarr",
),
(0, ia.Codec.ZSTD, [ia.Filter.SHUFFLE], None, None, False, None),
],
)
def test_global_config(clevel, codec, filters, chunks, blocks, contiguous, urlpath):
store = ia.Store(chunks, blocks, contiguous=contiguous, urlpath=urlpath)
ia.set_config_defaults(clevel=clevel, codec=codec, filters=filters, store=store, btune=False)
config = ia.get_config_defaults()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
store2 = config.store
assert store2.chunks == chunks
assert store2.blocks == blocks
assert store2.contiguous == contiguous
assert store2.urlpath == urlpath
# One can pass store parameters straight to config() dataclass too
ia.set_config_defaults(
clevel=clevel,
codec=codec,
filters=filters,
chunks=chunks,
blocks=blocks,
contiguous=False,
urlpath=None,
btune=False,
)
config = ia.get_config_defaults()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
store2 = ia.Store()
assert store2.chunks == chunks
assert store2.blocks == blocks
assert store2.contiguous == False
assert store2.urlpath == None
# Or, we can set defaults via Config (for better auto-completion)
cfg = ia.Config(
clevel=clevel,
codec=codec,
filters=filters,
chunks=chunks,
blocks=blocks,
contiguous=False,
urlpath=None,
btune=False,
)
ia.set_config_defaults(cfg)
config = ia.get_config_defaults()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
store2 = ia.Store()
assert store2.chunks == chunks
assert store2.blocks == blocks
assert store2.contiguous == False
assert store2.urlpath == None
# Or, we can use a mix of Config and keyword args
cfg = ia.Config(
clevel=clevel, codec=codec, blocks=blocks, contiguous=False, urlpath=urlpath, btune=False
)
ia.set_config_defaults(cfg, filters=filters, chunks=chunks)
config = ia.get_config_defaults()
assert config.clevel == clevel
assert config.codec == codec
assert config.filters == filters
store2 = ia.Store()
assert store2.chunks == chunks
assert store2.blocks == blocks
assert store2.contiguous == False
assert store2.urlpath == urlpath
@pytest.mark.parametrize(
"favor, chunks, blocks",
[
(ia.Favor.BALANCE, None, None),
(ia.Favor.SPEED, [50, 50], [20, 20]),
(ia.Favor.CRATIO, [50, 50], [20, 20]),
],
)
def test_global_favor(favor, chunks, blocks):
store = ia.Store(chunks, blocks)
ia.set_config_defaults(favor=favor, store=store)
config = ia.get_config_defaults()
assert config.favor == favor
assert config.btune == True
assert config.store.chunks == chunks
assert config.store.blocks == blocks
@pytest.mark.parametrize(
"favor",
[
ia.Favor.BALANCE,
ia.Favor.SPEED,
ia.Favor.CRATIO,
],
)
def test_favor_nobtune(favor):
with pytest.raises(ValueError):
ia.set_config_defaults(favor=favor, btune=False)
ia.set_config_defaults(btune=False)
with pytest.raises(ValueError):
ia.Config(favor=favor)
ia.Config(favor=favor, btune=True)
@pytest.mark.parametrize(
"clevel, codec, filters",
[
(1, None, None),
(None, ia.Codec.ZSTD, None),
(None, None, [ia.Filter.SHUFFLE]),
(1, ia.Codec.ZSTD, [ia.Filter.SHUFFLE]),
],
)
def test_btune_incompat(clevel, codec, filters):
with pytest.raises(ValueError):
ia.set_config_defaults(clevel=clevel, codec=codec, filters=filters, btune=True)
ia.set_config_defaults(btune=True)
with pytest.raises(ValueError):
ia.Config(clevel=clevel)
with pytest.raises(ValueError):
ia.Config(filters=filters)
with pytest.raises(ValueError):
ia.Config(codec=codec)
@pytest.mark.parametrize(
"chunks, blocks, 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(chunks, blocks, shape):
try:
store = ia.Store(chunks, blocks)
cfg = ia.set_config_defaults(shape=shape, store=store)
store2 = cfg.store
if chunks is not None:
assert store2.chunks == chunks
assert store2.blocks == blocks
else:
# automatic partitioning
assert store2.chunks <= shape
assert store2.blocks <= store2.chunks
except ValueError:
assert shape == ()
# One can pass store parameters straight to config() dataclass too
try:
cfg = ia.set_config_defaults(
shape=shape,
chunks=chunks,
blocks=blocks,
contiguous=True,
)
store2 = cfg.store
if chunks is not None:
assert store2.chunks == chunks
assert store2.blocks == blocks
else:
# automatic partitioning
assert store2.chunks <= shape
assert store2.blocks <= store2.chunks
assert store2.contiguous == True
except ValueError:
assert shape == ()
@pytest.mark.parametrize(
"clevel, codec, filters, chunks, blocks",
[
(0, ia.Codec.ZSTD, [ia.Filter.SHUFFLE], None, None),
(1, ia.Codec.BLOSCLZ, [ia.Filter.BITSHUFFLE], [50, 50], [20, 20]),
(9, ia.Codec.ZSTD, [ia.Filter.SHUFFLE, ia.Filter.DELTA], [50, 50], [20, 20]),
(6, ia.Codec.ZSTD, [ia.Filter.SHUFFLE], [100, 50], [50, 20]),
(0, ia.Codec.ZSTD, [ia.Filter.SHUFFLE], None, None),
],
)
def test_config_ctx(clevel, codec, filters, chunks, blocks):
try:
store = ia.Store(chunks, blocks)
with ia.config(
clevel=clevel, codec=codec, filters=filters, store=store, btune=False
) as cfg:
assert cfg.clevel == clevel
assert cfg.codec == codec
assert cfg.btune == False
assert cfg.filters == filters
assert cfg.store.chunks == chunks
assert cfg.store.blocks == blocks
except ValueError:
assert chunks is not None
# One can pass store parameters straight to config() dataclass too
try:
with ia.config(
clevel=clevel,
codec=codec,
filters=filters,
chunks=chunks,
blocks=blocks,
btune=False,
) as cfg:
assert cfg.clevel == clevel
assert cfg.codec == codec
assert cfg.btune == False
assert cfg.filters == filters
assert cfg.store.chunks == chunks
assert cfg.store.blocks == blocks
except ValueError:
assert chunks is not None
@pytest.mark.parametrize(
"chunks, blocks, 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(chunks, blocks, shape):
try:
store = ia.Store(chunks, blocks)
with ia.config(shape=shape, store=store) as cfg:
store2 = cfg.store
if chunks is not None:
assert store2.chunks == chunks
assert store2.blocks == blocks
else:
# automatic partitioning
assert store2.chunks <= shape
assert store2.blocks <= store2.chunks
except ValueError:
assert shape == ()
# One can pass store parameters straight to config() dataclass too
try:
with ia.config(
shape=shape,
chunks=chunks,
blocks=blocks,
contiguous=True,
) as cfg:
store2 = cfg.store
if chunks is not None:
assert store2.chunks == chunks
assert store2.blocks == blocks
else:
# automatic partitioning
assert store2.chunks <= shape
assert store2.blocks <= store2.chunks
assert store2.contiguous is True
except ValueError:
assert shape == ()
def test_nested_contexts():
# Set the default to enable compression
ia.set_config_defaults(clevel=5, btune=False)
a = ia.ones((100, 100))
b = a.data
assert a.cratio > 1
# Now play with contexts and params in calls
# Disable compression in contexts
with ia.config(clevel=0):
a = ia.numpy2iarray(b)
assert a.cratio < 1
# Enable compression in call
a = ia.numpy2iarray(b, clevel=1, split_mode=ia.SplitMode.NEVER_SPLIT)
assert a.cratio > 1
# Enable compression in nested context
with ia.config(clevel=1):
a = ia.numpy2iarray(b)
assert a.cratio > 1
# Disable compression in double nested context
with ia.config(clevel=0):
a = ia.numpy2iarray(b)
assert a.cratio < 1
# Finally, the default should be enabling compression again
a = ia.ones((100, 100))
assert a.cratio > 1
def test_default_params():
urlpath = "arr.iarr"
ia.remove_urlpath(urlpath)
cfg = ia.get_config_defaults()
ia.set_config_defaults(split_mode=ia.SplitMode.ALWAYS_SPLIT)
a = ia.linspace([10], start=0, stop=1, urlpath=urlpath, contiguous=False)
ia.set_config_defaults(split_mode=cfg.split_mode)
cfg2 = ia.Config()
assert cfg.split_mode == cfg2.split_mode
assert cfg.contiguous == cfg2.contiguous
assert cfg.urlpath == cfg2.urlpath
ia.remove_urlpath(urlpath)
@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)