hdmf_zarr.utils module
Collection of utility I/O classes for the ZarrIO backend store.
- hdmf_zarr.utils.get_store_path(store)
Return the path identifying a Zarr store.
For a
LocalStorethis is the resolved absolute filesystem path. For every other store this isstr(store), whose form is defined by that store class:MemoryStoregives amemory://URL, while a store that defines no__str__, such asFsspecStore, falls back to itsrepr.
- class hdmf_zarr.utils.HDMFZarrArray(_async_array: AsyncArray[T_ArrayMetadata])
Bases:
ArrayA subclass of zarr.Array used by HDMF to provide compatibility with array-like interfaces expected by PyNWB and HDMF, including lazy decoding of variable-length strings, without monkey-patching the global zarr.Array class.
NOTE: Downstream codes should not rely on the use of HDMFZarrArray. This is an intermediate approach to enable compatibility with downstream libraries that depend on changes in the features of the Array class in Zarr V3, specificlally, removal of __len__ and __iter__, unwrapping of scalar arrays, and use of the new StringDType (kind “T”) instead of object (kind “O”) for representing strings. Use of HDMFZarrArray will be removed in a future release once HDMF/PyNWB have been updated to: 1) support np.dtypes.StringDType for strings. This can be removed on release of hdmf#1576 and hdmf#1578 2) not require __len__ and __iter__ on arrays. This can be removed on release of hdmf#1580 3) not require unwrapping of scalars. This can be removed on release of hdmf#1581, hdmf#1580, and NeurodataWithoutBorders/pynwb#2263
- property dtype
Return the dtype of the array.
For downstream compatibility, this function returns object dtype for arrays with StringDType.
- __getitem__(key)
Get an item from the array.
For downstream compatibility this functions: - Changes the dtype of np.dtypes.StringDType to object - Unwraps scalar arrays by returning result[()]
- class hdmf_zarr.utils.ZarrIODataChunkIteratorQueue(number_of_jobs: int = 1, max_threads_per_process: None | int = None, multiprocessing_context: None | Literal['fork', 'spawn'] = None)
Bases:
dequeHelper class used by ZarrIO to manage the write for DataChunkIterators Each queue element must be a tuple of two elements: 1) the dataset to write to and 2) the AbstractDataChunkIterator with the data :param number_of_jobs: The number of jobs used to write the datasets. The default is 1. :type number_of_jobs: integer :param max_threads_per_process: Limits the number of threads used by each process. The default is None (no limits). :type max_threads_per_process: integer or None :param multiprocessing_context: Context for multiprocessing. It can be None (default), “fork” or “spawn”. Note that “fork” is only available on UNIX systems (not Windows). :type multiprocessing_context: string or None
- exhaust_queue()
Read and write from any queued DataChunkIterators.
Operates in a round-robin fashion for a single job. Operates on a single dataset at a time with multiple jobs.
- append(dataset, data)
Append a value to the queue :param dataset: The dataset where the DataChunkIterator is written to :type dataset: Zarr array :param data: DataChunkIterator with the data to be written :type data: AbstractDataChunkIterator
- static initializer_wrapper(operation_to_run: callable, process_initialization: callable, initialization_arguments: Iterable, max_threads_per_process: int | None = None)
Needed as a part of a bug fix with cloud memory leaks discovered by SpikeInterface team.
Recommended fix is to have global wrappers for the working initializer that limits the threads used per process.
- class hdmf_zarr.utils.ZarrSpecWriter(group)
Bases:
SpecWriterClass used to write format specs to Zarr
- Parameters:
group (
Group) – the Zarr file to write specs to
- static stringify(spec)
Converts a spec into a JSON string to write to a dataset
- write_spec(spec, path)
Write a spec to the given path
- write_namespace(namespace, path)
Write a namespace to the given path
- class hdmf_zarr.utils.ZarrSpecReader(group)
Bases:
SpecReaderClass to read format specs from Zarr
- Parameters:
group (
Group) – the Zarr file to read specs from
- read_spec(spec_path)
Read a spec from the given path
- read_namespace(ns_path)
Read a namespace from the given path
- class hdmf_zarr.utils.ZarrDataIO(data, chunks=None, fillvalue=None, compressors=None, compressor=None, filters=None, serializer=None, shards=None, link_data=False)
Bases:
DataIOWrap data arrays for write via ZarrIO to customize I/O behavior, such as compression and chunking for data arrays.
- Parameters:
data (
ndarrayorlistortupleorArrayorIterable) – the data to be written. NOTE: If an zarr.Array is used, all other settings but link_data will be ignored as the dataset will either be linked to or copied as is in ZarrIO.chunks (
listortuple) – Chunk shape. Whenshardsis also specified,chunksdefines the inner chunk shape within each shard (i.e. the fine-grained I/O unit).fillvalue (None) – Value to be returned when reading uninitialized parts of the dataset
compressors (
BytesBytesCodecorlistortupleorbool) – Zarr compressor codecs (BytesBytesCodec) to be used. Can be a single codec or list of codecs. Set to True to use Zarr default. Set to False to disable compression. Use zarr.codecs (e.g., zarr.codecs.BloscCodec()) or zarr.codecs.numcodecs wrappers.compressor (None) – DEPRECATED. Zarr compressor filter to be used. Instead of using this argument, pass a zarr v3 BytesBytesCodec (e.g., zarr.codecs.BloscCodec()) to
compressors.filters (
listortuple) – One or more Zarr-supported codecs (ArrayArrayCodec) used to transform the array before it is serialized to bytes. Use zarr.codecs or zarr.codecs.numcodecs wrappers.serializer (
ArrayBytesCodec) – Zarr codec (ArrayBytesCodec) used to serialize the array to bytes, e.g., zarr.codecs.BytesCodec(). Zarr chooses a default for the dtype when this is not given.shards (
listortupleorstr) – Shard shape in array elements, or ‘auto’ to let Zarr choose the shape. Each shard is a single object in the store and contains multiple inner chunks defined bychunks. Sharding reduces the number of store objects and can improve performance for large arrays.chunksdefines the inner chunk shape. For efficient parallel iterator writes, prefer abuffer_shapeequal to the shard shape or an integer multiple along each axis, so each buffer contains complete shards. Buffers spanning a full array axis may include a partial edge shard. Aligned buffers are written as whole-buffer tasks. Other buffer shapes are supported safely by assigning each shard to one task and sequentially reading and writing the portions of iterator buffers inside it. Partial shard updates read the entire existing encoded shard and assemble its replacement. When shards span multiple buffers, this repeats for successive buffer pieces and can use substantially more memory than the source buffer.buffer_shapelimits source read sizes, not total write memory, including Zarr’s encoded shard buffers and codec workspace. With ‘auto’, scheduling uses the shard shape chosen by Zarr; buffer-shard alignment is not required.link_data (
bool) – If data is an zarr.Array should it be linked to or copied. NOTE: This parameter is only allowed if data is an zarr.Array
- property link_data: bool
Only applies to zarr.Array type data
- Type:
Bool indicating should it be linked to or copied. NOTE
- static from_h5py_dataset(h5dataset, **kwargs)
Factory method to create a ZarrDataIO instance from a h5py.Dataset. The ZarrDataIO object wraps the h5py.Dataset and the io filter settings are inferred from filters used in h5py such that the options in Zarr match (if possible) the options used in HDF5.
- Parameters:
dataset (h5py.Dataset) – h5py.Dataset object that should be wrapped
kwargs – Other keyword arguments to pass to ZarrDataIO.__init__
fillvalue,chunks,compressors, andfiltersoverride the corresponding values inferred fromh5dataset. When omitted, fill value and chunks are copied from the HDF5 dataset, while Zarr compressors and filters are inferred from its HDF5 filter pipeline.
- Returns:
ZarrDataIO object wrapping the dataset
- static hdf5_to_zarr_filters(h5dataset) list
From the given h5py.Dataset infer the corresponding zarr v3 codecs.
Returns a list of zarr v3 codec instances (BytesBytesCodec or ArrayArrayCodec). Uses zarr.codecs.numcodecs wrappers for codecs not natively available in zarr v3.
- static is_h5py_dataset(obj)
Check if the object is an instance of h5py.Dataset without requiring import of h5py