Skip to content

Commit

Permalink
Add DParams dataclass && avoid rewriting the defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
martaiborra committed Sep 18, 2024
1 parent b4fab55 commit a08ac92
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
9 changes: 9 additions & 0 deletions doc/reference/storage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ CParams
:nosignatures:

CParams

DParams
-------

.. autosummary::
:toctree: autofiles/storage
:nosignatures:

DParams
2 changes: 1 addition & 1 deletion src/blosc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/blosc2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand All @@ -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)


Expand Down
68 changes: 31 additions & 37 deletions src/blosc2/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SChunk>` or :ref:`NDArray <NDArray>`.
"""


def default_nthreads():
return nthreads
Expand Down Expand Up @@ -123,4 +87,34 @@ class CParams:
tuner: blosc2.Tuner = blosc2.Tuner.STUNE

# def __post_init__(self):
# if len(self.filters) > 6:
# 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 <SChunk>` or :ref:`NDArray <NDArray>`.
"""
4 changes: 2 additions & 2 deletions tests/test_compress2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
filters_meta=[10, 0],
typesize=4
)},
{"nthreads": 4},
{'dparams': blosc2.DParams(nthreads=4)},
),
(
np.arange(10, dtype="float32"),
Expand All @@ -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),
Expand Down

0 comments on commit a08ac92

Please sign in to comment.