neu_vol.backends.hdf5¶
HDF5-backed reader (h5py).
Reads one dataset of an HDF5 file as a volume, in canonical (z, y, x) /
(c, z, y, x) order. Reading only; HDF5Backend.write_region() says where HDF5
output is written instead. Each instance holds an open file, reopened from its spec on each
dask worker.
HDF5 is a container, so a path alone is ambiguous: sole_dataset() picks the dataset
when there is exactly one. The optional stored_offset / stored_voxel_size /
stored_axes / stored_units / stored_kind readers return the frame a writer
recorded beside the array, which source_metadata composes into the same metadata a
precomputed info yields. An axis order the file states but nobody can interpret is an
error, never a guess: reversed, the data lands mirrored through the z=x diagonal.
Attributes¶
Classes¶
View over a single dataset in an HDF5 file. |
Functions¶
|
An axis-order attribute as |
|
Every dataset in the file with at least |
|
Every volumetric dataset in the file, with its geometry and recorded frame. |
|
|
|
True if |
|
One dataset of an HDF5 file, ready to read regions from. |
|
|
|
The file's one volumetric dataset, for callers that did not name one. |
|
A local file to write in place of |
Module Contents¶
- class neu_vol.backends.hdf5.HDF5Backend(spec: Mapping[str, Any])¶
View over a single dataset in an HDF5 file.
- close() None¶
Release the open
h5py.File.Called by
release_backends(): HDF5 refuses to open a file for writing while any handle holds it read-only. Idempotent.
- classmethod open(spec: Mapping[str, Any]) HDF5Backend¶
- read_region(region: neu_vol.backends.base.Region) numpy.ndarray¶
- stored_axes(name: str = 'axes')¶
The axis order the writer recorded, as
(order, where), orNone.What makes
stored_offset()’s numbers unambiguous;neu-vol to-hdf5writes it. Dataset attributes first, then the root group’s.
- stored_kind(name: str = 'kind')¶
What the writer said the voxels MEAN, as
(kind, where), orNone.Attributes only, dataset before root. Written by
neu_vol.write_pieceandto-hdf5(ops/pack.KIND_ATTR). The caller trusts only values inneu_lib.KINDS, since another tool may use the word for something else.
- stored_offset(name: str = 'voxel_offset')¶
A voxel offset recorded in the file as
(value, where_it_was_found).Noneif the file records none. Searched in the dataset’s attributes, then the root group’s, then a top-level dataset of that name.The axis order is not knowable from here: the name is precomputed’s, where it means xyz. The numbers come back as stored and the caller decides, using
stored_axes()when the file has it.
- stored_units(name: str = 'units')¶
The physical unit the writer recorded, as
(unit, where), orNone.Attributes only, dataset before root.
neu-vol to-hdf5records it besidevoxel_size; the suite works in nm.
- stored_voxel_size(name: str = 'voxel_size')¶
A physical voxel size recorded in the file, as
(value, where), orNone.Searched as
stored_offset()is.nameis a parameter because another writer may call it something else.
- to_spec() dict[str, Any]¶
- abstractmethod write_region(region: neu_vol.backends.base.Region, data: numpy.ndarray) None¶
Never: HDF5 output is written whole, by a different path.
The block engine writes into a volume; one array in a container has no pyramid and no chunk objects, and HDF5 has no concurrent writers. A piece is written in one call by
neu_vol.write_piece()(an array in memory) orneu_vol.pack_hdf5()(neu-vol to-hdf5, from another source).
- property chunks: tuple[int, ...]¶
- property dtype: numpy.dtype¶
- property shape: tuple[int, ...]¶
- neu_vol.backends.hdf5.axes_string(value: Any) str¶
An axis-order attribute as
"zyx", however h5py handed it over."zyx",b"zyx",["z","y","x"]and[b"z",b"y",b"x"]are all accepted.
- neu_vol.backends.hdf5.datasets(path: str, *, min_ndim: int = 3) list[str]¶
Every dataset in the file with at least
min_ndimdimensions, by path.
- neu_vol.backends.hdf5.describe_datasets(path: str, *, min_ndim: int = 3) dict[str, dict]¶
Every volumetric dataset in the file, with its geometry and recorded frame.
{name: {"shape", "dtype", "chunks", "ndim", <frame attributes>}}, in file order, from one file open, attributes only. Whatneu-vol infolists for a container of several arrays, such as a file of ground-truth crops.Frame values come back as recorded, each with its own
axes, not reoriented to zyx (source_metadata._read_hdf5does that, strictly, for one array). Values fall back to the root group’s.
- neu_vol.backends.hdf5.local_copy(location: str) Iterator[str]¶
locationdownloaded into a temporary directory, removed when the block exits.h5py reads only filesystems (
neu_vol.transfer.download()says how the bytes move). Cached readers of the copy are closed before it is removed.
- neu_vol.backends.hdf5.names_remote_hdf5(src: Any, src_format: str | None = None) bool¶
True if
srcis an HDF5 file on an object store, bysrc_formator extension.
- neu_vol.backends.hdf5.open_hdf5(path: str, dataset: str | None = None) HDF5Backend¶
One dataset of an HDF5 file, ready to read regions from.
The front door for a file you are pointing at by hand:
be = open_hdf5("piece.h5") # one dataset: found be = open_hdf5("gt_v1_eval.h5", "/z07901") # a container: name it be.read_region((slice(0, 64), slice(0, 64), slice(0, 64)))
datasetis optional only when the file holds exactly one 3D+ array; with several it is required, and the error lists them (sole_dataset()).Not a general “open anything” helper: the format is in the name, so nothing is detected. To find out what something is, use
describe(). Goes throughopen_backend(), so the open file joins the per-process cache.
- neu_vol.backends.hdf5.require_local_path(path: str, what: str = 'an HDF5 file') str¶
pathif it is an ordinary filesystem path; a useful error if it is not.h5py has no object-store driver; without this check it tries to create a local file called
s3://bucket/piece.h5and reportserrno = 2.
- neu_vol.backends.hdf5.sole_dataset(path: str) str¶
The file’s one volumetric dataset, for callers that did not name one.
Answers only when there is exactly one 3D+ dataset; otherwise raises listing them and saying how to choose one.
- neu_vol.backends.hdf5.staged_output(location: str, *, upload: bool = True) Iterator[str]¶
A local file to write in place of
location, uploaded when the block exits cleanly.Starts as a copy of the object if there is one, so a file can be added to. Nothing is uploaded if the block raises, or with
upload=False(a dry run).
- neu_vol.backends.hdf5.DESCRIBED_ATTRIBUTES¶
- neu_vol.backends.hdf5.FRAME_ATTRIBUTES¶
- neu_vol.backends.hdf5.HDF5_EXTENSIONS = ('.h5', '.hdf5', '.hdf', '.he5')¶
- neu_vol.backends.hdf5.TAG = 'hdf5'¶