diff --git a/doc/reference/storage.rst b/doc/reference/storage.rst index 92338884..2162191c 100644 --- a/doc/reference/storage.rst +++ b/doc/reference/storage.rst @@ -15,3 +15,12 @@ CParams :nosignatures: CParams + +DParams +------- + +.. autosummary:: + :toctree: autofiles/storage + :nosignatures: + + DParams diff --git a/src/blosc2/__init__.py b/src/blosc2/__init__.py index 0e170db0..9d4b161a 100644 --- a/src/blosc2/__init__.py +++ b/src/blosc2/__init__.py @@ -213,7 +213,7 @@ class Tuner(Enum): from .schunk import SChunk, open -from .storage import cpu_info, CParams, cparams_dflts, dparams_dflts, ncores, nthreads, storage_dflts +from .storage import cpu_info, CParams, cparams_dflts, DParams, dparams_dflts, ncores, nthreads, storage_dflts # Registry for postfilters diff --git a/src/blosc2/core.py b/src/blosc2/core.py index b0d680d2..96dcc3ae 100644 --- a/src/blosc2/core.py +++ b/src/blosc2/core.py @@ -1444,10 +1444,15 @@ def decompress2(src: object, dst: object | bytearray = None, **kwargs: dict) -> Other Parameters ---------------- kwargs: dict, optional + Decompression parameters. The default values are in :ref:`blosc2.DParams`. Keyword arguments supported: - nthreads: int - The number of threads to use internally (1 by default). + cparams: :class:`blosc2.DParams` + All the decompression parameters that you want to use as + a :class:`blosc2.DParams` instance. + others: Any + If `dparams` is not passed, all the parameters of a :class:`blosc2.DParams` + can be passed as keyword arguments. Returns ------- @@ -1469,6 +1474,12 @@ def decompress2(src: object, dst: object | bytearray = None, **kwargs: dict) -> If the length of :paramref:`src` is smaller than the minimum. If :paramref:`dst` is not None and its length is 0. """ + if kwargs is not None: + if 'dparams' in kwargs: + if len(kwargs) > 1: + raise AttributeError("Cannot pass both dparams and other kwargs already included in DParams") + kwargs = asdict(kwargs.get('dparams')) + return blosc2_ext.decompress2(src, dst, **kwargs) diff --git a/src/blosc2/storage.py b/src/blosc2/storage.py index 171257f3..23cabb51 100644 --- a/src/blosc2/storage.py +++ b/src/blosc2/storage.py @@ -22,42 +22,6 @@ # Experiments say that, when using a large number of threads, it is better to not use them all nthreads -= nthreads // 8 -# Defaults for compression params -cparams_dflts = { - "codec": blosc2.Codec.ZSTD, - "codec_meta": 0, - "clevel": 1, - "use_dict": False, - "typesize": 8, - "nthreads": nthreads, - "blocksize": 0, - "splitmode": blosc2.SplitMode.ALWAYS_SPLIT, - "filters": [ - blosc2.Filter.NOFILTER, - blosc2.Filter.NOFILTER, - blosc2.Filter.NOFILTER, - blosc2.Filter.NOFILTER, - blosc2.Filter.NOFILTER, - blosc2.Filter.SHUFFLE, - ], - "filters_meta": [0, 0, 0, 0, 0, 0], - "tuner": blosc2.Tuner.STUNE, -} -""" -Compression params defaults. -""" - -# Defaults for decompression params -dparams_dflts = {"nthreads": nthreads} -""" -Decompression params defaults. -""" -# Default for storage -storage_dflts = {"contiguous": False, "urlpath": None, "cparams": None, "dparams": None, "io": None} -""" -Storage params defaults. This is meant only for :ref:`SChunk ` or :ref:`NDArray `. -""" - def default_nthreads(): return nthreads @@ -123,4 +87,34 @@ class CParams: tuner: blosc2.Tuner = blosc2.Tuner.STUNE # def __post_init__(self): - # if len(self.filters) > 6: \ No newline at end of file + # if len(self.filters) > 6: + + +@dataclass +class DParams: + """Dataclass for hosting the different decompression parameters. + + Parameters + ---------- + nthreads: int + The number of threads to use internally. By default, blosc2 computes + a good guess. + """ + nthreads: int = field(default_factory=default_nthreads) + +# Defaults for compression params +cparams_dflts = asdict(CParams()) +""" +Compression params defaults. +""" + +# Defaults for decompression params +dparams_dflts = asdict(DParams()) +""" +Decompression params defaults. +""" +# Default for storage +storage_dflts = {"contiguous": False, "urlpath": None, "cparams": blosc2.CParams(), "dparams": blosc2.DParams} +""" +Storage params defaults. This is meant only for :ref:`SChunk ` or :ref:`NDArray `. +""" diff --git a/tests/test_compress2.py b/tests/test_compress2.py index 113169c4..257b8d5b 100644 --- a/tests/test_compress2.py +++ b/tests/test_compress2.py @@ -28,7 +28,7 @@ filters_meta=[10, 0], typesize=4 )}, - {"nthreads": 4}, + {'dparams': blosc2.DParams(nthreads=4)}, ), ( np.arange(10, dtype="float32"), @@ -43,7 +43,7 @@ ( random.integers(0, 1000, 1000, endpoint=True), {'cparams': blosc2.CParams(splitmode=blosc2.SplitMode.ALWAYS_SPLIT, nthreads=5, typesize=4)}, - {}, + {'dparams': blosc2.DParams()}, ), (np.arange(45, dtype=np.float64), {'cparams': blosc2.CParams(codec=blosc2.Codec.LZ4HC, typesize=4)}, {}), (np.arange(50, dtype=np.int64), {"typesize": 4}, blosc2.dparams_dflts),