konfai.data package

Submodules

konfai.data.augmentation module

Data augmentation primitives applied by KonfAI datasets.

class konfai.data.augmentation.Prob(prob=1.0)[source]

Bases: object

class konfai.data.augmentation.DataAugmentationsList(nb=10, data_augmentations={'default|Flip': <konfai.data.augmentation.Prob object>})[source]

Bases: object

prepare(key)[source]
Return type:

None

set_datasets(datasets)[source]
Return type:

None

class konfai.data.augmentation.DataAugmentation(groups=None)[source]

Bases: NeedDevice, ABC

load(prob)[source]
set_datasets(datasets)[source]
state_init(index, shapes, caches_attribute)[source]
Return type:

list[list[int]]

inverse(index, a, tensor)[source]
Return type:

Tensor

class konfai.data.augmentation.EulerTransform[source]

Bases: DataAugmentation

class konfai.data.augmentation.Translate(t_min=-10, t_max=10, is_int=False)[source]

Bases: EulerTransform

class konfai.data.augmentation.Rotate(a_min=0, a_max=360, is_quarter=False)[source]

Bases: EulerTransform

class konfai.data.augmentation.Scale(s_std=0.2)[source]

Bases: EulerTransform

class konfai.data.augmentation.Flip(f_prob=[0.33, 0.33, 0.33])[source]

Bases: DataAugmentation

class konfai.data.augmentation.ColorTransform(groups=None)[source]

Bases: DataAugmentation

class konfai.data.augmentation.Brightness(b_std, groups=None)[source]

Bases: ColorTransform

class konfai.data.augmentation.Contrast(c_std, groups=None)[source]

Bases: ColorTransform

class konfai.data.augmentation.LumaFlip(groups=None)[source]

Bases: ColorTransform

class konfai.data.augmentation.HUE(hue_max, groups=None)[source]

Bases: ColorTransform

class konfai.data.augmentation.Saturation(s_std, groups=None)[source]

Bases: ColorTransform

class konfai.data.augmentation.Noise(n_std, noise_step=1000, beta_start=0.0001, beta_end=0.02, groups=None)[source]

Bases: DataAugmentation

static enforce_zero_terminal_snr(betas)[source]
load(prob)[source]
class konfai.data.augmentation.CutOUT(c_prob, cutout_size, value, groups=None)[source]

Bases: DataAugmentation

class konfai.data.augmentation.Elastix(grid_spacing=16, max_displacement=16)[source]

Bases: DataAugmentation

class konfai.data.augmentation.Permute(prob_permute=[0.5, 0.5])[source]

Bases: DataAugmentation

class konfai.data.augmentation.Mask(mask, value, groups=None)[source]

Bases: DataAugmentation

konfai.data.data_manager module

Dataset assembly, subset selection, and dataloader orchestration for KonfAI.

class konfai.data.data_manager.GroupTransform(transforms={'default|Normalize|Standardize|Unsqueeze|TensorCast|ResampleIsotropic|ResampleResize': <konfai.data.transform.TransformLoader object>}, patch_transforms={'default|Normalize|Standardize|Unsqueeze|TensorCast|ResampleIsotropic|ResampleResize': <konfai.data.transform.TransformLoader object>}, is_input=True)[source]

Bases: object

Collection of transforms attached to one source-to-destination group path.

prepare(group_src, group_dest)[source]
Return type:

None

set_datasets(datasets)[source]
Return type:

None

to(device)[source]
class konfai.data.data_manager.GroupTransformMetric(transforms={'default|Normalize|Standardize|Unsqueeze|TensorCast|ResampleIsotropic|ResampleResize': <konfai.data.transform.TransformLoader object>})[source]

Bases: GroupTransform

Metric-specific group transform that omits patch-time transforms.

class konfai.data.data_manager.Group(groups_dest={'default|Labels': {'transforms': [], 'patch_transforms': []}})[source]

Bases: dict[str, GroupTransform]

Mapping of destination group names to transform pipelines.

class konfai.data.data_manager.GroupMetric(groups_dest={'default|group_dest': {'transforms': [], 'patch_transforms': []}})[source]

Bases: dict[str, GroupTransformMetric]

Metric-oriented variant of Group used during evaluation.

class konfai.data.data_manager.CustomSampler(size, shuffle=False)[source]

Bases: Sampler[int]

Simple sampler that optionally shuffles indices without distributed logic.

class konfai.data.data_manager.DataItem(name, tensor, attribute, x, a, p, is_input)[source]

Bases: object

Single tensor sample together with dataset metadata and patch indices.

name: str
tensor: Tensor
attribute: Attribute
x: int
a: int
p: int
is_input: bool
class konfai.data.data_manager.BatchDataItem(name, tensor, attribute, x, a, p, is_input)[source]

Bases: object

Batch-level representation of multiple DataItem objects.

name: list[str]
tensor: Tensor
attribute: list[Attribute]
x: list[int]
a: list[int]
p: list[int]
is_input: bool
konfai.data.data_manager.collate_konfai(batch)[source]

Collate KonfAI samples into the batch structure expected by the workflows.

Return type:

dict[str, BatchDataItem]

class konfai.data.data_manager.DatasetIter(rank, data, mapping, groups_src, inline_augmentations, data_augmentations_list, patch_size, overlap, buffer_size, use_cache=True)[source]

Bases: Dataset

Torch dataset view over KonfAI dataset managers and patch mappings.

get_patch_config()[source]
Return type:

tuple[list[int] | None, int | None]

to(device)[source]
get_dataset_from_index(group_dest, index)[source]
Return type:

DatasetManager

reset_augmentation(label)[source]
load(label)[source]
load_data(group_src, group_dest, index, augmentation_index=None)[source]
Return type:

bool

unload_data(group_dest, index)[source]
Return type:

None

class konfai.data.data_manager.Subset(subset=None, shuffle=True)[source]

Bases: object

requires_infos()[source]

Return whether this subset implementation needs per-sample metadata.

Return type:

bool

class konfai.data.data_manager.TrainSubset(subset=None, shuffle=True)[source]

Bases: Subset

class konfai.data.data_manager.PredictionSubset(subset=None)[source]

Bases: Subset

class konfai.data.data_manager.Data(dataset_filenames, groups_src, patch, use_cache, subset, batch_size, validation, inline_augmentations, data_augmentations_list, num_workers, pin_memory, prefetch_factor, persistent_workers)[source]

Bases: ABC

Abstract base class shared by training, prediction, and evaluation datasets.

data: list[list[dict[str, list[DatasetManager]]]]
mapping: list[list[list[tuple[int, int, int]]]]
datasets: dict[str, Dataset]
prepare()[source]

Instantiate config-driven transforms and augmentations before runtime.

Return type:

None

get_groups_dest()[source]
get_data(world_size)[source]
Return type:

tuple[list[list[DataLoader]], list[str], list[str]]

class konfai.data.data_manager.DataTrain(dataset_filenames=['default|./Dataset:mha'], groups_src={'default|Labels': {'default|Labels': {'transforms': [], 'patch_transforms': []}}}, augmentations={'DataAugmentation_0': <konfai.data.augmentation.DataAugmentationsList object>}, inline_augmentations=False, patch=<konfai.data.patching.DatasetPatch object>, use_cache=True, subset=<konfai.data.data_manager.TrainSubset object>, batch_size=1, validation=0.2, num_workers=None, pin_memory=False, prefetch_factor=None, persistent_workers=None)[source]

Bases: Data

Dataset configuration used by the training workflow.

class konfai.data.data_manager.DataPrediction(dataset_filenames=['default|./Dataset'], groups_src={'default': {'default|Labels': {'transforms': [], 'patch_transforms': []}}}, augmentations={'DataAugmentation_0': <konfai.data.augmentation.DataAugmentationsList object>}, patch=<konfai.data.patching.DatasetPatch object>, subset=<konfai.data.data_manager.PredictionSubset object>, batch_size=1, num_workers=None, pin_memory=False, prefetch_factor=None, persistent_workers=None)[source]

Bases: Data

Dataset configuration used by the prediction workflow.

class konfai.data.data_manager.DataMetric(dataset_filenames=['default|./Dataset:mha'], groups_src={'default': {'default|group_dest': {'transforms': [], 'patch_transforms': []}}}, subset=<konfai.data.data_manager.PredictionSubset object>, validation=None, num_workers=None, pin_memory=False, prefetch_factor=None, persistent_workers=None)[source]

Bases: Data

Dataset configuration used by the evaluation workflow.

konfai.data.patching module

Patch extraction, accumulation, and patch-combination helpers for KonfAI.

class konfai.data.patching.PatchReadPlan(data_slices, reflect_padding, constant_padding, concatenate_extend_slice)[source]

Bases: object

Precomputed slicing and padding instructions for one patch request.

data_slices: tuple[slice, ...]
reflect_padding: tuple[int, ...]
constant_padding: tuple[int, ...]
concatenate_extend_slice: bool
class konfai.data.patching.PathCombine[source]

Bases: ABC

Base class for overlap-aware weighting schemes applied during patch assembly.

data: Tensor
overlap: int
set_patch_config(patch_size, overlap)[source]
class konfai.data.patching.Mean[source]

Bases: PathCombine

Uniform patch-combination strategy for overlapping predictions.

class konfai.data.patching.Cosinus[source]

Bases: PathCombine

Cosine-based weighting strategy for smoother overlap blending.

class konfai.data.patching.Accumulator(patch_slices, patch_size, patch_combine=None, batch=True)[source]

Bases: object

Accumulate patch predictions and reassemble them into a full tensor.

patch_slices: list[tuple[slice, ...]]
add_layer(index, layer)[source]
Return type:

None

is_full()[source]
Return type:

bool

assemble()[source]
Return type:

Tensor

class konfai.data.patching.Patch(patch_size, overlap, pad_value=0, extend_slice=0)[source]

Bases: ABC

Abstract base class for dataset-level and model-level patch definitions.

load(shape, a=0)[source]
Return type:

None

abstractmethod init(key)[source]
get_patch_slices(a=0)[source]
get_read_plan(data_shape, index, a, is_input)[source]
Return type:

PatchReadPlan

apply_read_plan(data, plan)[source]
Return type:

Tensor

get_data(data, index, a, is_input)[source]
Return type:

list[Tensor]

get_size(a=0)[source]
Return type:

int

class konfai.data.patching.DatasetPatch(patch_size=[128, 128, 128], overlap=None, pad_value=None, extend_slice=0)[source]

Bases: Patch

Patch definition applied when sampling data from datasets.

init(key='')[source]
class konfai.data.patching.ModelPatch(patch_size=[128, 128, 128], overlap=None, patch_combine=None, pad_value=None, extend_slice=0)[source]

Bases: Patch

Patch definition applied inside model graphs during prediction or training.

patch_combine: PathCombine | None
init(key)[source]
disassemble(*data_list)[source]
Return type:

Iterator[list[Tensor]]

class konfai.data.patching.DatasetManager(index, group_src, group_dest, name, dataset, patch, transforms, data_augmentations_list)[source]

Bases: object

Cache-backed manager for one dataset case and one source/destination group.

cache_attributes: list[Attribute]
data: list[Tensor]
augmented_data: dict[int, Tensor]
reset_augmentation()[source]
load(pre_transform, data_augmentations_list, load_augmentations=True)[source]
Return type:

None

can_stream_patch(a)[source]
Return type:

bool

unload()[source]
Return type:

None

unload_augmentation()[source]
Return type:

None

get_data(index, a, patch_transforms, is_input)[source]
Return type:

Tensor

get_size(a)[source]
Return type:

int

konfai.data.transform module

Tensor and image transforms used in KonfAI preprocessing and postprocessing.

class konfai.data.transform.Transform[source]

Bases: NeedDevice, ABC

Base class for transforms operating on tensors and cached attributes.

supports_dataloader_workers = True
set_datasets(datasets)[source]
transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

class konfai.data.transform.TransformInverse(inverse)[source]

Bases: Transform, ABC

Base class for transforms that can also invert their effect.

abstractmethod inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.TransformLoader[source]

Bases: object

Resolve and instantiate transform classes from KonfAI configuration.

get_transform(classpath, konfai_args)[source]
Return type:

Transform

class konfai.data.transform.Clip(min_value=-1024, max_value=1024, save_clip_min=False, save_clip_max=False, mask=None)[source]

Bases: Transform

Clip tensor intensities to a fixed or data-dependent value range.

class konfai.data.transform.Normalize(lazy=False, channels=None, min_value=-1, max_value=1, inverse=True)[source]

Bases: TransformInverse

Map intensities to a target min/max interval and optionally invert it.

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.UnNormalize(min_value=-1024, max_value=3071)[source]

Bases: Transform

class konfai.data.transform.Standardize(lazy=False, mean=None, std=None, mask=None, inverse=True)[source]

Bases: TransformInverse

Standardize tensors using cached or computed mean and standard deviation.

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.TensorCast(dtype='float32', inverse=True)[source]

Bases: TransformInverse

dtype: dtype
static safe_dtype_cast(dtype_str)[source]
Return type:

dtype

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.Padding(padding=[0, 0, 0, 0, 0, 0], mode='constant', inverse=True)[source]

Bases: TransformInverse

transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.Squeeze(dim, inverse=True)[source]

Bases: TransformInverse

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.Resample(inverse)[source]

Bases: TransformInverse, ABC

abstractmethod transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.ResampleToResolution(spacing=[1.0, 1.0, 1.0], inverse=True)[source]

Bases: Resample

transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

class konfai.data.transform.ResampleToShape(shape=[100, 256, 256], inverse=True)[source]

Bases: Resample

transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

class konfai.data.transform.ResampleTransform(transforms, inverse=True)[source]

Bases: TransformInverse

transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.Mask(path='./default.mha', value_outside=0)[source]

Bases: Transform

class konfai.data.transform.Dilate(dilate=1)[source]

Bases: Transform

class konfai.data.transform.Sum(dim=0)[source]

Bases: Transform

class konfai.data.transform.Gradient(per_dim=False)[source]

Bases: Transform

class konfai.data.transform.Argmax(dim=0)[source]

Bases: Transform

class konfai.data.transform.Softmax(dim=0)[source]

Bases: Transform

class konfai.data.transform.FlatLabel(labels=None)[source]

Bases: Transform

class konfai.data.transform.Save(dataset, group=None)[source]

Bases: Transform

class konfai.data.transform.Flatten[source]

Bases: Transform

transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

class konfai.data.transform.Permute(dims='1|0|2', inverse=True)[source]

Bases: TransformInverse

transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.Flip(dims='1|0|2', inverse=True)[source]

Bases: TransformInverse

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.Canonical(inverse=True)[source]

Bases: TransformInverse

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.HistogramMatching(reference_group)[source]

Bases: Transform

class konfai.data.transform.SelectLabel(labels)[source]

Bases: Transform

class konfai.data.transform.OneHot(num_classes, inverse=True)[source]

Bases: TransformInverse

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

class konfai.data.transform.KonfAIInference(repo_id='VBoussot/MRSegmentator-KonfAI', model_name='MRSegmentator', checkpoints_name=['fold_0'], number_of_tta=0, number_of_mc=0, per_channel=False)[source]

Bases: Transform

supports_dataloader_workers = False
infer_entry(dataset_path, output_path, gpu)[source]
class konfai.data.transform.InferenceStack(dataset, name, mode='mean')[source]

Bases: Transform

class konfai.data.transform.Variance[source]

Bases: Transform

class konfai.data.transform.SegmentationDisagreement(ignore_background=False)[source]

Bases: Transform

class konfai.data.transform.Percentage(baseline)[source]

Bases: Transform

class konfai.data.transform.StandardDeviation[source]

Bases: Transform

class konfai.data.transform.Statistics[source]

Bases: Transform

class konfai.data.transform.Crop(inverse=True)[source]

Bases: TransformInverse

transform_shape(group_src, name, shape, cache_attribute)[source]
Return type:

list[int]

inverse(name, tensor, cache_attribute)[source]
Return type:

Tensor

Module contents

Data loading, patching, transform, and augmentation utilities for KonfAI.