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:

CompositeTransform

konfai.utils.ITK.flatten_transform(transform_files)[source]
Return type:

AffineTransform

konfai.utils.ITK.apply_to_image_rigid_transform(image, transform_files)[source]
Return type:

Image

konfai.utils.ITK.apply_to_data_transform(data, transform_files)[source]
Return type:

Image

konfai.utils.ITK.resample_itk(image_reference, image, transform_files, mask=False, default_pixel_value=None, torch_resample=False)[source]
Return type:

Image

konfai.utils.ITK.parametermap_to_transform(path_src)[source]
Return type:

Transform | list[Transform]

konfai.utils.ITK.resample_isotropic(image, spacing=None)[source]
Return type:

Image

konfai.utils.ITK.resample_resize(image, size=None)[source]
konfai.utils.ITK.box_with_mask(mask, label, dilatations)[source]
Return type:

ndarray

konfai.utils.ITK.crop_with_mask(image, box)[source]
Return type:

Image

konfai.utils.ITK.format_mask_label(mask, labels)[source]
Return type:

Image

konfai.utils.ITK.get_flat_label(mask, labels=None)[source]
Return type:

Image

konfai.utils.ITK.clip_and_cast(image, min_value, max_value, dtype)[source]
Return type:

Image

konfai.utils.config module

Configuration helpers that map YAML trees to KonfAI Python objects.

class konfai.utils.config.Config(key)[source]

Bases: object

Context 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.

create_dictionary(data, keys, i)[source]
Return type:

dict

merge(dict1, dict2)[source]
Return type:

dict

get_value(name, default)[source]
Return type:

object

konfai.utils.config.config(key=None)[source]

Attach a KonfAI configuration key to a class or callable.

Parameters:

key (str | None) – Configuration branch handled by the decorated object.

Returns:

Decorator storing the key on the decorated object.

Return type:

Callable

konfai.utils.config.apply_config(konfai_args=None)[source]

Recursively instantiate callables from the active KonfAI configuration.

Parameters:

konfai_args (str | None) – Root configuration path used to resolve nested constructor arguments.

Returns:

Decorator that injects configuration-backed arguments at call time.

Return type:

Callable

konfai.utils.dataset module

Dataset file abstractions and image conversion utilities for KonfAI.

class konfai.utils.dataset.Attribute(attributes=None)[source]

Bases: dict[str, Any]

Metadata container storing repeated values with a stack-like naming scheme.

pop(key, default=None)[source]

If the key is not found, return the default if given; otherwise, raise a KeyError.

Return type:

Any

get_np_array(key)[source]
Return type:

ndarray

get_tensor(key)[source]
Return type:

Tensor

pop_np_array(key)[source]
pop_tensor(key)[source]
Return type:

Tensor

is_info(key, value)[source]
Return type:

bool

konfai.utils.dataset.is_an_image(attributes)[source]

Return whether the given attribute set contains image geometry metadata.

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.

Return type:

tuple[ndarray, Attribute]

konfai.utils.dataset.get_infos(filename)[source]

Read shape and metadata from an image file without loading its full pixel data.

Return type:

tuple[list[int], Attribute]

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:

None

class konfai.utils.dataset.Dataset(filename, file_format)[source]

Bases: object

Filesystem or HDF5-backed dataset abstraction used across KonfAI.

class AbstractFile[source]

Bases: ABC

abstractmethod file_to_data(group, name)[source]
Return type:

tuple[ndarray, Attribute]

abstractmethod file_to_data_slice(group, name, slices)[source]
Return type:

tuple[ndarray, Attribute]

abstractmethod file_to_data_statistics(group, name, channels=None)[source]
Return type:

dict[str, float]

abstractmethod data_to_file(name, data, attributes=None)[source]
Return type:

None

abstractmethod get_names(group)[source]
Return type:

list[str]

abstractmethod get_group()[source]
Return type:

list[str]

abstractmethod is_exist(group, name=None)[source]
Return type:

bool

abstractmethod get_infos(group, name)[source]
Return type:

tuple[list[int], Attribute]

class H5File(filename, read)[source]

Bases: AbstractFile

file_to_data(groups, name)[source]
Return type:

tuple[ndarray, Attribute]

file_to_data_slice(groups, name, slices)[source]
Return type:

tuple[ndarray, Attribute]

file_to_data_statistics(groups, name, channels=None)[source]
Return type:

dict[str, float]

data_to_file(name, data, attributes=None)[source]
Return type:

None

is_exist(group, name=None)[source]
Return type:

bool

get_names(groups, h5_group=None)[source]
Return type:

list[str]

get_group()[source]
Return type:

list[str]

get_infos(groups, name)[source]
Return type:

tuple[list[int], Attribute]

class SitkFile(filename, read, file_format)[source]

Bases: AbstractFile

file_to_data(group, name)[source]
Return type:

tuple[ndarray, Attribute]

file_to_data_slice(group, name, slices)[source]
Return type:

tuple[ndarray, Attribute]

file_to_data_statistics(group, name, channels=None)[source]
Return type:

dict[str, float]

is_vtk_polydata(obj)[source]
data_to_file(name, data, attributes=None)[source]
Return type:

None

is_exist(group, name=None)[source]
Return type:

bool

get_names(group)[source]
Return type:

list[str]

get_group()[source]
get_infos(group, name)[source]
Return type:

tuple[list[int], Attribute]

class File(filename, read, file_format)[source]

Bases: object

write(group, name, data, attributes=None)[source]
read_data(groups, name)[source]
Return type:

tuple[ndarray, Attribute]

read_data_slice(groups, name, slices)[source]
Return type:

tuple[ndarray, Attribute]

read_data_statistics(groups, name, channels=None)[source]
Return type:

dict[str, float]

read_transform(group, name)[source]
Return type:

Transform

read_image(group, name)[source]
get_size(group)[source]
Return type:

int

is_group_exist(group)[source]
Return type:

bool

is_dataset_exist(group, name)[source]
Return type:

bool

get_names(groups, index=None)[source]
Return type:

list[str]

get_group()[source]
get_infos(groups, name)[source]
Return type:

tuple[list[int], Attribute]

get_statistics(groups)[source]
Return type:

dict[str, dict[str, dict[str, float | list[float]]]]

konfai.utils.utils module

Compatibility facade for KonfAI utility helpers and lightweight array utilities.

konfai.utils.utils.get_module(classpath, default_classpath)[source]
Return type:

tuple[ModuleType, str]

konfai.utils.utils.get_patch_slices_from_nb_patch_per_dim(patch_size_tmp, nb_patch_per_dim, overlap)[source]
Return type:

list[tuple[slice, ...]]

konfai.utils.utils.get_patch_slices_from_shape(patch_size, shape, overlap_tmp)[source]
Return type:

tuple[list[tuple[slice, ...]], list[tuple[int, bool]]]

konfai.utils.utils.is_windows_absolute_path(path)[source]

Return whether path looks like a Windows absolute path.

Return type:

bool

konfai.utils.utils.split_path_spec(value, *, default_format='mha', allowed_flags=None, supported_extensions=None)[source]

Split a KonfAI path[:flag]:format spec without breaking Windows paths.

KonfAI accepts dataset-like strings such as:

  • ./Dataset

  • ./Dataset:mha

  • ./Dataset:a:mha

  • C:\Data\Dataset:mha

  • C:\Data\Dataset:a:mha

Parsing is performed from the right so the drive separator in Windows paths is preserved.

Return type:

tuple[str, str | None, str]

Module contents

Utility modules supporting configuration, datasets, ITK, and runtime helpers.