| 1 |
import numpy |
| 2 |
import h5py |
| 3 |
import hdf5plugin |
| 4 |
import ujson |
| 5 |
import numcodecs |
| 6 |
import zarr |
| 7 |
from fsspec.implementations.reference import ReferenceFileSystem |
| 8 |
|
| 9 |
|
| 10 |
def _dict_add(a, b): |
| 11 |
c = a.copy() |
| 12 |
c.update(b) |
| 13 |
return c |
| 14 |
|
| 15 |
|
| 16 |
compressors = dict( |
| 17 |
zstd=hdf5plugin.Zstd(), |
| 18 |
shuffle_zstd=_dict_add( |
| 19 |
dict(shuffle=True), hdf5plugin.Zstd() |
| 20 |
), # Test for two filters |
| 21 |
blosc=hdf5plugin.Blosc(), |
| 22 |
shuffle_blosc=_dict_add(dict(shuffle=True), hdf5plugin.Blosc()), |
| 23 |
lz4=hdf5plugin.LZ4(), |
| 24 |
) |
| 25 |
|
| 26 |
for c in compressors: |
| 27 |
f = h5py.File(f"hdf5_mini_{c}.h5", "w") |
| 28 |
f.create_dataset("data", (3, 2), dtype=numpy.int32, **compressors[c]).write_direct( |
| 29 |
numpy.arange(6, dtype=numpy.int32).reshape((3, 2)) |
| 30 |
) |
| 31 |
f.close() |
| 32 |
|
| 33 |
f = h5py.File(f"hdf5_mali_chunk.h5", "w") |
| 34 |
f.create_dataset("data", (8,), dtype=numpy.int32, chunks=(7,), **hdf5plugin.Zstd()) |
| 35 |
f["data"][0:7] = numpy.arange(7, dtype=numpy.int32) |
| 36 |
f["data"].id.write_direct_chunk((7,), numpy.array([7] + [0] * 6, dtype=numpy.int32), 1) |
| 37 |
print(f["data"][:]) |
| 38 |
f.close() |
| 39 |
|
| 40 |
f = h5py.File(f"hdf5_mali_chunk2.h5", "w") |
| 41 |
f.create_dataset( |
| 42 |
"data", (8,), dtype=numpy.int32, chunks=(3,), shuffle=True, **hdf5plugin.Zstd() |
| 43 |
) |
| 44 |
f["data"].id.write_direct_chunk( |
| 45 |
(0,), numcodecs.Shuffle(4).encode(numpy.array([0, 1, 2], dtype=numpy.int32)), 2 |
| 46 |
) |
| 47 |
f["data"].id.write_direct_chunk((3,), numpy.array([3, 4, 5], dtype=numpy.int32), 3) |
| 48 |
f["data"][6:] = numpy.array( |
| 49 |
[ |
| 50 |
6, |
| 51 |
7, |
| 52 |
], |
| 53 |
dtype=numpy.int32, |
| 54 |
) |
| 55 |
print(f["data"][:]) |
| 56 |
f.close() |
| 57 |
|
| 58 |
# from kerchunk.hdf import SingleHdf5ToZarr |
| 59 |
# for c in compressors: |
| 60 |
# # if c=='lz4': |
| 61 |
# # continue |
| 62 |
# with open(f'hdf5_mini_{c}.h5', 'rb') as f: |
| 63 |
# print(ujson.dumps(SingleHdf5ToZarr(f, None, inline_threshold=0, unsupported_inline_threshold=1).translate(), indent=4)) |
| 64 |
# with open(f'hdf5_mali_chunk.h5', 'rb') as f: |
| 65 |
# ref = SingleHdf5ToZarr(f, None).translate() |
| 66 |
# print(ujson.dumps(ref, indent=4)) |
| 67 |
# print(zarr.group(ReferenceFileSystem(ref, target=f'hdf5_mali_chunk.h5').get_mapper())['data'][:]) |
| 68 |
# with open(f'hdf5_mali_chunk2.h5', 'rb') as f: |
| 69 |
# ref = SingleHdf5ToZarr(f, None).translate() |
| 70 |
# print(ujson.dumps(ref, indent=4)) |
| 71 |
# print(zarr.group(ReferenceFileSystem(ref, target=f'hdf5_mali_chunk2.h5').get_mapper())['data'][:]) |