Inferer

Inferer

class brainles_aurora.inferer.inferer.AbstractInferer(config: BaseConfig)

Bases: ABC

Abstract base class for inference.

abstract infer()
class brainles_aurora.inferer.inferer.AuroraInferer(config: AuroraInfererConfig | None = None)

Bases: AbstractInferer

Inferer for the Aurora models.

infer(t1: str | Path | ndarray | None = None, t1c: str | Path | ndarray | None = None, t2: str | Path | ndarray | None = None, fla: str | Path | ndarray | None = None, segmentation_file: str | Path | None = None, whole_tumor_unbinarized_floats_file: str | Path | None = None, metastasis_unbinarized_floats_file: str | Path | None = None, log_file: str | Path | None = None) Dict[str, ndarray]

Perform inference on the provided images.

Parameters:
  • t1 (str | Path | np.ndarray | None, optional) – T1 modality. Defaults to None.

  • t1c (str | Path | np.ndarray | None, optional) – T1C modality. Defaults to None.

  • t2 (str | Path | np.ndarray | None, optional) – T2 modality. Defaults to None.

  • fla (str | Path | np.ndarray | None, optional) – FLAIR modality. Defaults to None.

  • segmentation_file (str | Path | None, optional) – Path where the segementation file should be stored. Defaults to None. Should be a nifti file. Defaults internally to a ‘./segmentation.nii.gz’.

  • whole_tumor_unbinarized_floats_file (str | Path | None, optional) – Output path. Defaults to None.

  • metastasis_unbinarized_floats_file (str | Path | None, optional) – Output path. Defaults to None.

  • log_file (str | Path | None, optional) – Logfile path. Defaults to o the same path as segmentation_file with the extension .log or to ./{self.__class__.__name__}.log if no segmentation_file is provided.

Returns:

Post-processed data.

Return type:

Dict[str, np.ndarray]

Constants

class brainles_aurora.inferer.constants.DataMode(value)

Bases: str, Enum

Enum representing different modes for handling input and output data.

NIFTI_FILE = 'NIFTI_FILEPATH'

Input data is provided as NIFTI file paths/ output is writte to NIFTI files.

NUMPY = 'NP_NDARRAY'

Input data is provided as NumPy arrays/ output is returned as NumPy arrays.

class brainles_aurora.inferer.constants.Device(value)

Bases: str, Enum

Enum representing device for model inference.

AUTO = 'auto'

Attempt to use GPU, fallback to CPU.

CPU = 'cpu'

Use CPU

GPU = 'cuda'

Use GPU (CUDA)

brainles_aurora.inferer.constants.IMGS_TO_MODE_DICT = {(False, False, False, True): InferenceMode.FLA_O, (False, True, False, False): InferenceMode.T1C_O, (False, True, False, True): InferenceMode.T1C_FLA, (True, False, False, False): InferenceMode.T1_O, (True, True, False, False): InferenceMode.T1_T1C, (True, True, False, True): InferenceMode.T1_T1C_FLA, (True, True, True, True): InferenceMode.T1_T1C_T2_FLA}

Dictionary mapping tuples of booleans representing presence of the modality in order [t1,t1c,t2,fla] to InferenceMode values.

class brainles_aurora.inferer.constants.InferenceMode(value)

Bases: str, Enum

Enum representing different modes of inference based on available image inputs.

In General You should aim to use as many modalities as possible to get the best results.

FLA_O = 'fla-o'

FLAIR is available.

T1C_FLA = 't1c-fla'

T1C and FLAIR are available.

T1C_O = 't1c-o'

T1C is available.

T1_O = 't1-o'

T1 is available.

T1_T1C = 't1-t1c'

T1 and T1C are available.

T1_T1C_FLA = 't1-t1c-fla'

T1, T1C, and FLAIR are available.

T1_T1C_T2_FLA = 't1-t1c-t2-fla'

All four modalities are available.

brainles_aurora.inferer.constants.MODALITIES = ['t1', 't1c', 't2', 'fla']

T1 T1C T2 FLAIR ([‘t1’, ‘t1c’, ‘t2’, ‘fla’])

Type:

List of modality names in standard order

class brainles_aurora.inferer.constants.ModelSelection(value)

Bases: str, Enum

Enum representing different strategies for model selection.

BEST = 'best'

Select the best performing model.

LAST = 'last'

Select the last model.

VANILLA = 'vanilla'

Select the vanilla model.

class brainles_aurora.inferer.constants.Output(value)

Bases: str, Enum

Enum representing different types of output.

METASTASIS_NETWORK = 'metastasis_network'

Metastasis network output.

SEGMENTATION = 'segmentation'

Segmentation mask

WHOLE_NETWORK = 'whole_network'

Whole network output.

brainles_aurora.inferer.constants.WEIGHTS_DIR_PATTERN = 'weights_v*.*.*'

Directory name pattern to store model weights. E.g. weights_v1.0.0

Data

class brainles_aurora.inferer.data.DataHandler(config: AuroraInfererConfig)

Bases: object

Class to perform data related tasks such as validation, loading, transformation, saving.

determine_inference_mode(images: List[ndarray | None] | List[Path | None]) InferenceMode

Determine the inference mode based on the provided images. :param images: List of validated images. :type images: List[np.ndarray | None] | List[Path | None]

Returns:

Inference mode based on the combination of input images.

Return type:

InferenceMode

Raises:
  • NotImplementedError – If no model is implemented for the combination of input images.

  • AssertionError – If the input mode is not set (i.e. input images were not validated)

get_data_loader(images: List[ndarray | None] | List[Path | None]) DataLoader

Get the data loader for inference.

Parameters:

images (List[np.ndarray | None] | List[Path | None]) – List of validated images.

Returns:

Data loader for inference.

Return type:

torch.utils.data.DataLoader

Raises:

AssertionError – If the input mode is not set (i.e. input images were not validated)

get_input_mode() DataMode

Get the input mode.

Returns:

Input mode.

Return type:

DataMode

Raises:

AssertionError – If the input mode is not set (i.e. input images were not validated)

get_num_input_modalities() int

Get the number of input modalities.

Returns:

Number of input modalities.

Return type:

int

Raises:

AssertionError – If the number of input modalities is not set (i.e. input images were not validated)

get_reference_nifti_file() Path | str

Get a reference NIfTI file from the input (first not None in order T1-T1C-T2-FLAIR) to match header and affine.

Returns:

Path to reference NIfTI file.

Return type:

Path

Raises:

AssertionError – If the reference NIfTI file is not set (i.e. input images were not validated)

save_as_nifti(postproc_data: Dict[str, ndarray], output_file_mapping: Dict[str, str]) None

Save post-processed data as NIFTI files.

Parameters:
  • postproc_data (Dict[str, np.ndarray]) – Post-processed data.

  • output_file_mapping (Dict[str,str]) – Mapping of output keys to output file paths.

validate_images(t1: str | Path | ndarray | None = None, t1c: str | Path | ndarray | None = None, t2: str | Path | ndarray | None = None, fla: str | Path | ndarray | None = None) List[ndarray | None] | List[Path | None]

Validate the input images.

Verify that the input images exist (for paths) and are all of the same type (NumPy or NIfTI). Sets internal variables input_mode, num_input_modalities and reference_nifti_file.

Parameters:
  • t1 (str | Path | np.ndarray | None, optional) – T1 modality. Defaults to None.

  • t1c (str | Path | np.ndarray | None, optional) – T1C modality. Defaults to None.

  • t2 (str | Path | np.ndarray | None, optional) – T2 modality. Defaults to None.

  • fla (str | Path | np.ndarray | None, optional) – FLAIR modality. Defaults to None.

Returns:

List of validated images.

Return type:

List[np.ndarray | None] | List[Path | None]

Raises:
  • FileNotFoundError – If a file is not found.

  • ValueError – If a file path is not a NIfTI file (.nii or .nii.gz).

Model

class brainles_aurora.inferer.model.ModelHandler(config: AuroraInfererConfig, device: device)

Bases: object

Class for model loading, inference and post processing

infer(data_loader: DataLoader) Dict[str, ndarray]

Perform aurora inference on the given data_loader.

Parameters:

data_loader (DataLoader) – data loader

Returns:

Post-processed data

Return type:

Dict[str, np.ndarray]

load_model(inference_mode: InferenceMode, num_input_modalities: int) None

Load the model based on the inference mode. Will reuse previously loaded model if inference mode is the same.

Parameters:
  • inference_mode (InferenceMode) – Inference mode

  • num_input_modalities (int) – Number of input modalities (range 1-4)

Configs

class brainles_aurora.inferer.config.AuroraInfererConfig(log_level: int = 20, device: Device = Device.AUTO, cuda_devices: str = '0', tta: bool = True, sliding_window_batch_size: int = 1, workers: int = 0, threshold: float = 0.5, sliding_window_overlap: float = 0.5, crop_size: Tuple[int, int, int] = (192, 192, 32), model_selection: ModelSelection = ModelSelection.BEST)

Bases: BaseConfig

Configuration for the Aurora model inferer.

crop_size: Tuple[int, int, int] = (192, 192, 32)

Crop size for sliding window inference. Defaults to (192, 192, 32).

model_selection: ModelSelection = 'best'

Model selection strategy. Defaults to ModelSelection.BEST.

sliding_window_batch_size: int = 1

Batch size for sliding window inference. Defaults to 1.

sliding_window_overlap: float = 0.5

Overlap ratio for sliding window inference. Defaults to 0.5.

threshold: float = 0.5

Threshold for binarizing the model outputs. Defaults to 0.5.

tta: bool = True

Whether to apply test-time augmentations. Defaults to True.

workers: int = 0

Number of workers for data loading. Defaults to 0.

class brainles_aurora.inferer.config.BaseConfig(log_level: int = 20, device: Device = Device.AUTO, cuda_devices: str = '0')

Bases: object

Base configuration for the Aurora model inferer.

cuda_devices: str = '0'

CUDA devices to use when using CUDA. Defaults to “0”.

device: Device = 'auto'

Device for model inference. Defaults to Device.AUTO.

log_level: int = 20

Logging level. Defaults to logging.INFO.