Skip to content

Commit 2093a17

Browse files
committed
Add requested changes
1 parent 7c627fd commit 2093a17

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

blosc2/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class Tuner(Enum):
7171
Available tuners.
7272
"""
7373

74+
#: A 'simple' tuner. This is the default in the Blosc2 library
75+
STUNE = 0
76+
#: A more sophisticated tuner that can select different codecs/filters for different chunks (more info `here <https://github.com/Blosc/blosc2_btune/>`_).
7477
BTUNE = 32
7578

7679

blosc2/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,12 +1230,12 @@ def compute_chunks_blocks(shape, chunks=None, blocks=None, dtype=np.uint8, **kwa
12301230
cparams2["filters"][i] = blosc2.Filter.NOFILTER
12311231
else:
12321232
cparams2 = cparams
1233-
# Do not apply specific tuner
1233+
# Force STUNE to get a hint on the blocksize
12341234
aux_tuner = cparams2["tuner"] if "tuner" in cparams2 else blosc2.Tuner.STUNE
12351235
cparams2["tuner"] = blosc2.Tuner.STUNE
12361236
src = blosc2.compress2(np.zeros(nitems, dtype=f"V{itemsize}"), **cparams2)
1237-
cparams2["tuner"] = aux_tuner
12381237
_, _, blocksize = blosc2.get_cbuffer_sizes(src)
1238+
cparams2["tuner"] = aux_tuner
12391239
# Starting point for the guess
12401240
if chunks is None:
12411241
blocks = [1 if shape[i] == 1 else 2 for i in range(len(shape))]

examples/btune.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#######################################################################
2+
# Copyright (c) 2019-present, Blosc Development Team <[email protected]>
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under a BSD-style license (found in the
6+
# LICENSE file in the root directory of this source tree)
7+
#
8+
# This example can only be run if blosc2-btune is installed. You can
9+
# get it from https://pypi.org/project/blosc2-btune/
10+
# For more info on this tuner plugin see
11+
# https://github.com/Blosc/blosc2_btune/blob/main/README.md
12+
#######################################################################
13+
14+
import numpy as np
15+
16+
import blosc2
17+
import blosc2_btune
18+
19+
nchunks = 10
20+
# Set the compression and decompression parameters, use BTUNE tuner
21+
cparams = {"codec": blosc2.Codec.LZ4HC, "typesize": 4, "tuner": blosc2.Tuner.BTUNE}
22+
dparams = {}
23+
contiguous = True
24+
urlpath = "filename"
25+
26+
storage = {"contiguous": contiguous, "urlpath": urlpath, "cparams": cparams, "dparams": dparams}
27+
blosc2.remove_urlpath(urlpath)
28+
29+
# Set the Btune configuration to use
30+
btune_conf = {"tradeoff": 0.3, "perf_mode": blosc2_btune.PerformanceMode.DECOMP}
31+
blosc2_btune.set_params_defaults(**kwargs)
32+
33+
# Create the SChunk
34+
data = np.arange(200 * 1000 * nchunks)
35+
schunk = blosc2.SChunk(chunksize=200 * 1000 * 4, data=data, **storage)
36+
37+
# Check data can be retrieved correctly
38+
data2 = np.empty(data.shape, dtype=data.dtype)
39+
schunk.get_slice(out=data2)
40+
assert np.array_equal(data, data2)
41+
42+
blosc2.remove_urlpath(urlpath)

0 commit comments

Comments
 (0)