konfai.utils package#
Submodules#
konfai.utils.ITK module#
SimpleITK-based helpers for geometric transforms, resampling, and masking.
- konfai.utils.ITK.compose_transform(transform_files, image=None)[source]#
- Return type:
sitk.CompositeTransform
- konfai.utils.ITK.apply_to_image_rigid_transform(image, transform_files)[source]#
- Return type:
sitk.Image
- konfai.utils.ITK.resample_itk(image_reference, image, transform_files, mask=False, default_pixel_value=None, torch_resample=False)[source]#
- Return type:
sitk.Image
konfai.utils.config module#
Configuration helpers that map YAML trees to KonfAI Python objects.
- class konfai.utils.config.Range(min, max)[source]#
Bases:
objectUI hint attached to a parameter’s type — its inclusive numeric bounds.
Use
Annotated[int, Range(0, 100)](orfloat) in a config-bound signature: the binder ignores the metadata and validates the base type, while a UI reads the bounds to size a spinbox. Introspection-only.
- class konfai.utils.config.Choices(values)[source]#
Bases:
objectUI hint attached to a parameter’s type — its allowed values.
Use
Annotated[str, Choices([...])]for a fixed list, orAnnotated[str, Choices(resolver)]whereresolveris a zero-arg callable the app owns (e.g. one that lists a model registry it already fetches).resolve()returns the list — a reader calls it lazily, so the app resolves its own values and no tool re-fetches. Introspection-only; the binder ignores it (a value outside the list is still accepted, e.g. a local path). For a small FIXED, binder-validated set, preferLiteral[...].
- class konfai.utils.config.Config(key)[source]#
Bases:
objectContext manager for reading and updating a subtree of the active YAML config.
- Parameters:
key (
str) – Dot-separated path pointing to the configuration subtree to inspect or materialize.
- konfai.utils.config.config(key=None)[source]#
Attach a KonfAI configuration key to a class or callable.
konfai.utils.dataset module#
Dataset file abstractions and image conversion utilities for KonfAI.
- class konfai.utils.dataset.Attribute(attributes=None)[source]#
-
Metadata container storing repeated values with a stack-like naming scheme.
- konfai.utils.dataset.is_an_image(attributes)[source]#
Return whether the given attribute set contains image geometry metadata.
- Return type:
- konfai.utils.dataset.data_to_image(data, attributes)[source]#
Convert a NumPy array and KonfAI attributes into a SimpleITK image.
- Return type:
Image
- konfai.utils.dataset.image_to_data(image)[source]#
Convert a SimpleITK image into a channel-first NumPy array and attributes.
- konfai.utils.dataset.get_infos(filename)[source]#
Read shape and metadata from an image file without loading its full pixel data.
- konfai.utils.dataset.read_landmarks(filename)[source]#
Read Slicer-style fiducial landmarks from disk.
- Return type:
ndarray|None
- konfai.utils.dataset.write_landmarks(data, filename)[source]#
Write landmarks to the Slicer Markups fiducial CSV-like format.
- Return type:
- class konfai.utils.dataset.DataStream[source]#
Bases:
ABCOne dataset entry written incrementally, region by region. Obtained from
Dataset.open_data_stream, which returnsNonewhen the write format cannot serve region writes (the caller then assembles the volume and usesDataset.write). Use as a context manager: a clean exit finalizes the entry, an exception removes the partial one so a reader never sees a half-written volume.The entry lives under a temporary name until the clean exit renames it into place: an existence probe (
is_dataset_exist) or a concurrent reader never sees the entry while it is being written, a replaced entry stays readable until its replacement is complete, and a hard-killed writer leaves only temporary debris, never a plausible-looking partial volume under the final name. The temporary name is unique per stream (PID + sequence): two writers of the same entry (a case landing on two workers) each own their temporary, and whichever finalizes last publishes — a complete entry either way, never an interleaving of the two.- static temporary_suffix()[source]#
The per-stream unique suffix a backend appends to its temporary name.
- Return type:
- class konfai.utils.dataset.Dataset(filename, file_format)[source]#
Bases:
objectFilesystem or HDF5-backed dataset abstraction used across KonfAI.
- class AbstractFile[source]#
Bases:
ABC
- class H5File(filename, read)[source]#
Bases:
AbstractFile
- class SitkFile(filename, read, file_format)[source]#
Bases:
AbstractFile
- class OmeZarrFile(filename, read, level=0)[source]#
Bases:
AbstractFileOME-NGFF backend using chunked Zarr reads for KonfAI patches.
levelselects the multiscale pyramid resolution to read (0 = full resolution, higher = coarser); it comes from theomezarr@<level>dataset-spec suffix.
- class DicomFile(filename, read)[source]#
Bases:
AbstractFileDICOM series backend with header-only metadata and slice-level reads.
- rebase(prefix)[source]#
Prepend
prefixto this dataset’s path, re-derivingis_directoryfrom the format.- Return type:
- concurrent_write_safe()[source]#
Whether writes to different entries land in disjoint files, so a background writer may flush one entry while another thread writes elsewhere in the dataset.
Mirrors the backend dispatch in
File.__enter__: everything that is not a single-store backend is aSitkFiledirectory, one image file per(group, name). A single store (one HDF5 file, one zarr hierarchy, a DICOM series) shares handles and metadata across entries and must stay serial.- Return type:
- can_stream_data(attributes)[source]#
Whether
open_data_streamcan serve this dataset’s write format.H5 and OME-Zarr always can; MetaImage
mhaneeds image geometry to write its header up front; every other format only writes whole volumes (usewrite).- Return type:
- open_data_stream(group, name, shape, dtype, attributes=None)[source]#
Open one entry for incremental region writes.
Returns
Nonewhen the write format cannot serve region writes; the caller then assembles the volume and useswrite. The returned stream is a context manager: a clean exit finalizes the entry, an exception removes the partial one.- Return type:
konfai.utils.utils module#
Compatibility facade for KonfAI utility helpers and lightweight array utilities.
- konfai.utils.utils.env_flag(name, default)[source]#
A
KONFAI_*boolean switch:0/falseis off, anything else set is on, unset isdefault— one parser, so a switch read in two places accepts the same spellings.- Return type:
- konfai.utils.utils.get_module(classpath, default_classpath)[source]#
Import the module a classpath names and return it with the name to take from it.
A
:separates the module from the name: everything before the last one is the module, sotorch:nn:L1Lossandtorch.nn:L1Lossname the same class. Without one, the name is taken from the kind’s own package, and the dots between them lead there:Diceis that package’s own,segmentation.UNet.UNetis under two of its subpackages.- Return type:
- konfai.utils.utils.get_patch_slices_from_nb_patch_per_dim(patch_size_tmp, nb_patch_per_dim, overlap)[source]#
- konfai.utils.utils.DEFAULT_OVERLAP_FRACTION = 0.2#
20 % of the patch.
- Type:
Default overlap on tiled axes when a free-axis patch does not say otherwise
- konfai.utils.utils.concretize_patch_size(patch_size, shape, multiple=None)[source]#
Resolve the per-axis patch convention onto a concrete shape:
0= free axis -> full extent.[0,0,0](orNone) is the whole volume;[1,0,0]is a full 2D slice; a positive entry is fixed by the user and passes through (clamped to the extent so a patch never exceeds the volume).multiple(the model’s per-axisdownsampling_factor) rounds a free axis UP to a valid input size for the network — 122 -> 128 for a factor 16 — so its encoder/decoder skips align. The rounded size may exceed the extent; the border padding (pad_to_patch) fills it and the accumulator crops it back, exactly as it does for any patch larger than its case. A factor shorter than the patch rank aligns to the TRAILING axes (a 2D model in a[1,0,0]slice regime constrains Y and X, not Z).
- konfai.utils.utils.free_axis_rounding(multiple, axis, rank)[source]#
The rounding factor a free axis at spatial index
axis(ofrankaxes) takes from the model’s per-axismultiple— trailing-aligned: a 2D model’s[fy, fx]in a 3-axis patch constrains Y and X and leaves Z at 1.- Return type:
- konfai.utils.utils.size_free_axes(template, worst, multiple)[source]#
The up-front concrete patch for a free (
0) axis config: the worst case with each free axis rounded up to the model’smultiple(itsdownsampling_factor), so the first attempt is a valid model input.Nonewhen there is nothing to size — no free axis, unknown worst case, or every free extent is already a valid multiple (the raw path already works). Shared by the prediction and training auto-patch loops so the rounding lives in one place.
- konfai.utils.utils.resolve_overlap(overlap, patch_size, shape)[source]#
Resolve an overlap spec into per-axis voxels for a CONCRETE
patch_size.Accepted forms:
None->DEFAULT_OVERLAP_FRACTIONof the patch per axis; anint-> absolute voxels on every axis; afloatin [0,1[ or a"20%"string -> that fraction of the patch per axis; a per-axis list mixing those forms. Whatever the spec, an axis that is not tiled (single patch spans the extent) resolves to 0 – overlap only exists between patches.
- konfai.utils.utils.PATCH_BUDGET_SAFETY_FRACTION = 0.8#
Fraction of the budget the sized patch may use; the rest absorbs intermediates the estimate misses.
- konfai.utils.utils.resolve_patch(patch_size, shape, channels, dtype_bytes, budget_bytes, resident_images=1, intermediate_factor=1.0, snap=None)[source]#
Size the free axes of
patch_sizeso ONE patch fitsbudget_bytes; fixed axes never move.0entries are free (their max = the volume’s extent); positive entries are pinned by the user. A patch’s footprint is(resident_images + intermediate_factor) * voxels * channels * dtype_bytes–resident_imagesis exact (counted from the config: output + targets + masks), theintermediate_factorcovers the op’s working copies. When everything fits, the free axes take their full extent (the whole volume when all axes are free). Otherwise the free axes shrink ISOTROPICALLY (the patch keeps the volume’s proportions), optionally snapped down tosnapmultiples (a model’s valid input sizes).budget_bytes=Nonedisables sizing (free = extent). Fixed axes alone exceeding the budget is an error: the user pinned more than the budget allows.
- konfai.utils.utils.get_patch_slices_from_shape(patch_size, shape, overlap_tmp, multiple=None, declared_free_axis=None)[source]#
- konfai.utils.utils.is_windows_absolute_path(path)[source]#
Return whether path looks like a Windows absolute path.
- Return type:
- konfai.utils.utils.split_format_level(file_format)[source]#
Split an optional pyramid-level suffix from a format token.
Used by the OME-Zarr backend to pick a multiscale resolution directly in the dataset spec, e.g.
omezarr@2selects pyramid level 2 (coarser), independently of any transform. Returns(base_format, level)and defaults to level 0 (full resolution) when no@<int>suffix is present.
- konfai.utils.utils.split_path_spec(value, *, default_format='mha', allowed_flags=None, supported_extensions=None)[source]#
Split a KonfAI
path[:flag]:formatspec without breaking Windows paths.KonfAI accepts dataset-like strings such as:
./Dataset./Dataset:mha./Dataset:a:mhaC:\Data\Dataset:mhaC:\Data\Dataset:a:mha
Parsing is performed from the right so the drive separator in Windows paths is preserved.
Module contents#
Utility modules supporting configuration, datasets, ITK, DICOM, OME-Zarr, and runtime helpers.