-
Notifications
You must be signed in to change notification settings - Fork 78
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
Basic support for HDF5 filters #350
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -189,12 +189,40 @@ def _transfer_attrs( | |
f"TypeError transferring attr, skipping:\n {n}@{h5obj.name} = {v} ({type(v)})" | ||
) | ||
|
||
def _decode_blosc(properties): # 32001 | ||
blosc_compressors = ('blosclz', 'lz4', 'lz4hc', 'snappy', 'zlib', 'zstd') | ||
_1, _2, bytes_per_num, total_bytes, clevel, shuffle, compressor = properties | ||
return dict (id="blosc", blocksize=total_bytes, clevel=clevel, shuffle=shuffle, cname=blosc_compressors[compressor],) | ||
|
||
def _decode_zstd(properties): #32015 | ||
return dict (id='zstd', level=properties[0],) | ||
|
||
|
||
decoders = { "32001" : _decode_blosc, | ||
"32015" : _decode_zstd, | ||
} | ||
|
||
def _decode_filters(self, h5obj: Union[h5py.Dataset, h5py.Group]): | ||
if len(h5obj._filters.keys()) > 1: | ||
raise RuntimeError( | ||
f"{h5obj.name} uses multiple filters {list (h5obj._filters.keys())}. This is not supported by kerchunk." | ||
) | ||
for filter_id, properties in h5obj._filters.items(): | ||
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. I am 95% sure that setting a list like
will work fine with compression=None (or we can make the last one the compression), if we include gzip/zlib in this function. Of course, would need to check the order assumed by numcodecs matches hdf. |
||
if not str(filter_id) in self.decoders.keys(): | ||
raise RuntimeError( | ||
f"{h5obj.name} uses filter id {filter_id} with properties {properties}, not supported by kerchunk., supported filters are {self.decoders.keys()}" | ||
) | ||
else: | ||
return numcodecs.get_codec(self.decoders[filter_id](properties)) | ||
|
||
|
||
def _translator(self, name: str, h5obj: Union[h5py.Dataset, h5py.Group]): | ||
"""Produce Zarr metadata for all groups and datasets in the HDF5 file.""" | ||
try: # method must not raise exception | ||
kwargs = {} | ||
if isinstance(h5obj, h5py.Dataset): | ||
lggr.debug(f"HDF5 dataset: {h5obj.name}") | ||
lggr.debug (f"HDF5 compression: {h5obj.compression}") | ||
if h5obj.id.get_create_plist().get_layout() == h5py.h5d.COMPACT: | ||
# Only do if h5obj.nbytes < self.inline?? | ||
kwargs["data"] = h5obj[:] | ||
|
@@ -214,7 +242,7 @@ def _translator(self, name: str, h5obj: Union[h5py.Dataset, h5py.Group]): | |
if h5obj.compression == "gzip": | ||
compression = numcodecs.Zlib(level=h5obj.compression_opts) | ||
else: | ||
compression = None | ||
compression = self._decode_filters(h5obj) | ||
filters = [] | ||
dt = None | ||
# Get storage info of this HDF5 dataset... | ||
|
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.
This came up in #342 - actually zarr does support multiple filters, except only the final one is called "compression"