-
Notifications
You must be signed in to change notification settings - Fork 97
Make all codecs pickleable #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TomNicholas
wants to merge
22
commits into
zarr-developers:main
Choose a base branch
from
TomNicholas:pickleable_all_codecs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−125
Open
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
80b3a42
make Zlib codec pickleable
TomNicholas 83826c0
add test
TomNicholas eba4a8f
show __init_subclass__ can work for Zlib
TomNicholas 775608c
refactor the BytesBytes Codecs to use __init_subclass__
TomNicholas 5304a56
remove snake_case function
TomNicholas 5ef6a47
Chuck's suggestions
TomNicholas 037cda4
redefine Bitround
TomNicholas 7e624f4
remove debugging prints from test
TomNicholas 6e33e10
redefine Shuffle
TomNicholas b1ede89
redefine Delta
TomNicholas 0a6c2c7
redefine FixedScaleOffset
TomNicholas 09997f5
Quantize
TomNicholas 6b57ca0
PackBits
TomNicholas 8907aa0
AsType
TomNicholas 51fbd8f
redefine checksum codecs
TomNicholas 97f0ac9
array to bytes codecs
TomNicholas fa2e448
remove todo
TomNicholas 78b82fc
remove dynamic constructors
TomNicholas 61ae42c
release note
TomNicholas 6d04f9b
remove unneeded imports
TomNicholas ea8c06a
style: pre-commit fixes
pre-commit-ci[bot] 4c229b7
remove unused type ignore
TomNicholas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
from functools import cached_property, partial | ||
from typing import Any, Self, TypeVar | ||
from warnings import warn | ||
import textwrap | ||
|
||
import numpy as np | ||
|
||
|
@@ -74,11 +75,26 @@ def _parse_codec_configuration(data: dict[str, JSON]) -> dict[str, JSON]: | |
return {"id": id, **parsed_configuration} | ||
|
||
|
||
def snake_case(codec_name: str) -> str: | ||
# TODO the Jenkins codec is a special case because it inserts an _ | ||
return codec_name.lower() | ||
|
||
|
||
@dataclass(frozen=True) | ||
class _NumcodecsCodec(Metadata): | ||
codec_name: str | ||
codec_config: dict[str, JSON] | ||
|
||
def __init_subclass__(cls, *, namespace: str | None = None, codec_name: str | None = None, **kwargs): | ||
"""To be used only when creating the actual public-facing codec class.""" | ||
super().__init_subclass__(**kwargs) | ||
if namespace is not None and codec_name is not None: | ||
cls_name = f"{CODEC_PREFIX}{namespace}.{codec_name}" | ||
cls.codec_name = f"{CODEC_PREFIX}{codec_name}" | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cls.__doc__ = f""" | ||
See :class:`{cls_name}` for more details and parameters. | ||
""" | ||
|
||
def __init__(self, **codec_config: JSON) -> None: | ||
if not self.codec_name: | ||
raise ValueError( | ||
|
@@ -184,30 +200,18 @@ async def _encode_single(self, chunk_ndbuffer: NDBuffer, chunk_spec: ArraySpec) | |
|
||
|
||
def _add_docstring(cls: type[T], ref_class_name: str) -> type[T]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All this can be deleted once the changes in this PR are applied to all the other codecs |
||
cls.__doc__ = f""" | ||
cls.__doc__ = textwrap.dedent( | ||
f""" | ||
See :class:`{ref_class_name}` for more details and parameters. | ||
""" | ||
) | ||
return cls | ||
|
||
|
||
def _add_docstring_wrapper(ref_class_name: str) -> partial: | ||
return partial(_add_docstring, ref_class_name=ref_class_name) | ||
|
||
|
||
def _make_bytes_bytes_codec(codec_name: str, cls_name: str) -> type[_NumcodecsBytesBytesCodec]: | ||
# rename for class scope | ||
_codec_name = CODEC_PREFIX + codec_name | ||
|
||
class _Codec(_NumcodecsBytesBytesCodec): | ||
codec_name = _codec_name | ||
|
||
def __init__(self, **codec_config: JSON) -> None: | ||
super().__init__(**codec_config) | ||
|
||
_Codec.__name__ = cls_name | ||
return _Codec | ||
|
||
|
||
def _make_array_array_codec(codec_name: str, cls_name: str) -> type[_NumcodecsArrayArrayCodec]: | ||
# rename for class scope | ||
_codec_name = CODEC_PREFIX + codec_name | ||
|
@@ -254,13 +258,32 @@ def compute_encoded_size(self, input_byte_length: int, chunk_spec: ArraySpec) -> | |
|
||
|
||
# bytes-to-bytes codecs | ||
Blosc = _add_docstring(_make_bytes_bytes_codec("blosc", "Blosc"), "numcodecs.blosc.Blosc") | ||
LZ4 = _add_docstring(_make_bytes_bytes_codec("lz4", "LZ4"), "numcodecs.lz4.LZ4") | ||
Zstd = _add_docstring(_make_bytes_bytes_codec("zstd", "Zstd"), "numcodecs.zstd.Zstd") | ||
Zlib = _add_docstring(_make_bytes_bytes_codec("zlib", "Zlib"), "numcodecs.zlib.Zlib") | ||
GZip = _add_docstring(_make_bytes_bytes_codec("gzip", "GZip"), "numcodecs.gzip.GZip") | ||
BZ2 = _add_docstring(_make_bytes_bytes_codec("bz2", "BZ2"), "numcodecs.bz2.BZ2") | ||
LZMA = _add_docstring(_make_bytes_bytes_codec("lzma", "LZMA"), "numcodecs.lzma.LZMA") | ||
class Blosc(_NumcodecsBytesBytesCodec, namespace="blosc", codec_name="Blosc"): | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
class LZ4(_NumcodecsBytesBytesCodec, namespace="lz4", codec_name="LZ4"): | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
class Zstd(_NumcodecsBytesBytesCodec, namespace="zstd", codec_name="Zstd"): | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
class Zlib(_NumcodecsBytesBytesCodec, namespace="zlib", codec_name="Zlib"): | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
class GZip(_NumcodecsBytesBytesCodec, namespace="gzip", codec_name="GZip"): | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
class BZ2(_NumcodecsBytesBytesCodec, namespace="bz2", codec_name="BZ2"): | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
class LZMA(_NumcodecsBytesBytesCodec, namespace="lzma",codec_name="LZMA"): | ||
TomNicholas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
@_add_docstring_wrapper("numcodecs.shuffle.Shuffle") | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for
numcodecs.zarr3.Blosc
this returnswhich all seems correct to me?