PrognosAIs.Preprocessing package

Submodules

PrognosAIs.Preprocessing.Preprocessors module

class PrognosAIs.Preprocessing.Preprocessors.BatchPreprocessor(samples_path: str, output_directory: str, config: dict)[source]

Bases: object

classmethod init_from_sys_args(args_in)[source]
split_into_subsets(samples: list, sample_labels: list) → dict[source]
start()[source]
class PrognosAIs.Preprocessing.Preprocessors.SingleSamplePreprocessor(sample: PrognosAIs.Preprocessing.Samples.ImageSample, config: dict, output_directory: str = None)[source]

Bases: object

static _get_all_images_from_sequence(image: SimpleITK.SimpleITK.Image, max_dims: int) → list[source]

Get all of the images from a sequence of images.

Parameters
  • image (sitk.Image) – Multi-dimensional image containg the sequence.

  • max_dims (int) – The number of dimension of each individual image. This should be equal to the dimensionality of the input image - 1. Otherwise, we do not know how to extract the appropiate images

Raises

ValueError – If the maximum number of dimensions does not fit with the sequences.

Returns

list – All images extracted from the sequence.

static _get_first_image_from_sequence(image: SimpleITK.SimpleITK.Image, max_dims: int) → SimpleITK.SimpleITK.Image[source]

Extract the first image from a sequence of images

Parameters
  • image (sitk.Image) – Multi-dimensional image containg the sequence.

  • max_dims (int) – The maximum number of dimension the output can be.

Returns

sitk.Image – The first image extracted from the sequence

apply_pipeline(pipeline=None)[source]
bias_field_correcting()[source]
build_pipeline() → list[source]
channel_imputation(sample_channels)[source]
channels_to_float16(sample_channels)[source]
crop_to_mask(ROI_mask: SimpleITK.SimpleITK.Image, process_masks: bool = True, apply_to_output: bool = False)[source]
mask_background(ROI_mask: SimpleITK.SimpleITK.Image, background_value: float = 0.0, process_masks: bool = True, apply_to_output: bool = False)[source]
static mask_background_to_min(image, mask)[source]
masking()[source]
multi_dimension_extracting()[source]

Extract invidiual images from a multi-dimensional sequence.

Raises

NotImplementedError – If an extraction type is requested that is not supported.

normalizing()[source]
patching() → None[source]
rejecting()[source]
resampling()[source]
saving()[source]

PrognosAIs.Preprocessing.Samples module

class PrognosAIs.Preprocessing.Samples.ImageSample(root_path: str, extension_keyword: str = None, mask_keyword: str = None, labels: dict = None, number_of_label_classes: dict = None, are_labels_one_hot: bool = False, output_channel_names: list = [], input_channel_names: list = [])[source]

Bases: abc.ABC

ImageSample base class

To be implemented by subclasses:

  • init_image_files: Contains logic for retrieval of channel filepaths

  • load_channels: Contains logic for loading of channels from filepaths

  • load_output_channels: Contains logic for loading of output channels from filepaths

  • load_masks: Contains logic of loading masks from filepaths

Parameters
  • root_path – Path of the sample. Should contain folders or directories of channels and masks

  • extension_keyword – Extension of the files to load

  • mask_keyword (optional) – Keyword to identify which filepaths are masks. Defaults to None.

_identify_channel_file(image_file: str) → bool[source]

Identify whether an image file should be included as channel

Parameters

image_file – Image file to check

Returns

bool – True if image_file is channel, False otherwise

_identify_mask_file(image_file: str) → bool[source]

Identify whether an image file is a mask based on the mask keyword

Parameters

image_file – Image file to check

Returns

bool – True if image_file is mask, False otherwise

_identify_output_channel_file(image_file: str) → bool[source]

Identify whether an image file is a output channel

Parameters

image_file – Image file to check

Returns

bool – True if image_file is output channel, False otherwise

_init_channel_files(image_files: list) → list[source]

Get only the channel files from the image files, filtering out masks.

Parameters

image_files (list) – Paths to the image files

Returns

list – The paths to the channel files

_init_mask_files(image_files: list) → list[source]

Get only the mask files from the image files, filtering out channels.

Parameters

image_files (list) – Paths to the image files

Returns

list – The paths to the mask files

_init_output_channel_files(image_files: list) → list[source]

Get the output channel files from the image files.

Parameters

image_files (list) – Paths to the image files

Returns

list – The paths to the output channel files

_parse_function_parameters(function_parameters)[source]

Parse the function parameters

Parameters

function_parameters (function or tuple) – Function and possible args and kw_args.

Returns

tuple – function, args, and kw_args

_perform_sanity_checks()[source]

Automatic sanity check to see if we can process the sample

Raises

NotImplementedError – If the configuration has not been implemented

add_to_labels(to_add_labels: List[dict], to_add_number_of_label_classes: dict) → None[source]
assert_all_channels_same_size()[source]

Check wheter all channels have the same size

Raises

ValueError – Raised when not all channels have same size

assert_all_masks_same_size()[source]

Check wheter all masks have the same size

Raises

ValueError – Raised when not all masks have same size

property channel_names

Names of the channels

Returns

list – Channel names

property channel_size

The image size of the channels

property channels

The channels present in the sample

Channels of a sample can be set by providing either a function,

or a tuple consisting of a function, possible function argument and function keyword arguments.

This function will then be applied to all channels in the sample. The function has to output either a SimpleITK Image or a list. In the last case it is assumed that these are patches and the class is updated accordingly

Returns

list – Channels present in the sample

copy()[source]

Returns a (deep) copy of the instance

Returns

ImageSample – Deep copy of the instance

static get_appropiate_dtype_from_image(image: SimpleITK.SimpleITK.Image) → int[source]

Find the minimum SimpleITK type need to represent the value

Parameters

value (float) – The value to check

Returns

int – The appropiate SimpleITK to which the value can be casted

static get_appropiate_dtype_from_scalar(value: Union[int, float], return_np_type: bool = False) → Union[int, numpy.dtype][source]

Find the minimum SimpleITK type need to represent the value

Parameters
  • value (float) – The value to check

  • return_np_type (bool) – If True returns the numpy type instead of the SimpleITK type. Defaults to False.

Returns

int – The appropiate SimpleITK to which the value can be casted

get_example_channel() → SimpleITK.SimpleITK.Image[source]

Provides an example channel of the samples

Returns

sitk.Image – Single channel of the sample

get_example_channel_patches() → list[source]

Provides an example of all patches of a channel, even if there is only one patch

Returns

list – Patch(es) of a single channel of the sample

get_example_mask() → SimpleITK.SimpleITK.Image[source]

Provides an example mask of the samples

Returns

sitk.Image – Single mask of the sample

get_example_mask_patches() → list[source]

Provides an example of all patches of a mask, even if there is only one patch

Returns

list – Patch(es) of a single mask of the sample

get_example_output_channel() → SimpleITK.SimpleITK.Image[source]

Provides an example output channel of the samples

Returns

sitk.Image – Single channel of the sample

get_example_output_channel_patches() → list[source]

Provides an example of all patches of a output channel, even if there is only one patch

Returns

list – Patch(es) of a single output channel of the sample

get_grouped_channels() → list[source]

Groups the channels on a per-patch basis instead of a per-channel basis

The channels property indexes first by channel and then by (possibly) patches. This function instead first indexes by patches (or the whole sample of no patches). This can be handy when all channels are needed at the same time

Returns

list – Grouped channels for each patch

get_grouped_masks() → list[source]

Groups the masks on a per-patch basis instead of a per-channel basis

The masks property indexes first by channel and then by (possibly) patches. This function instead first indexes by patches (or the whole sample of no patches). This can be handy when all masks are needed at the same time

Returns

list – Grouped channels for each patch. Empty lists if sample doesn’t have mask

get_grouped_output_channels() → list[source]

Groups the output channels on a per-patch basis instead of a per-channel basis

The channels property indexes first by channel and then by (possibly) patches. This function instead first indexes by patches (or the whole sample of no patches). This can be handy when all channels are needed at the same time

Returns

list – Grouped channels for each patch

static get_numpy_type_from_sitk_type(sitk_type: int) → numpy.dtype[source]
static get_sitk_type_from_numpy_type(numpy_type: numpy.dtype) → int[source]
abstract init_image_files() → list[source]

Get the filepaths (folders or files) of the channels for a single sample. To be implemented by the subclass

Returns

list – The filepaths of the channels

abstract load_channels(channel_files: list) → dict[source]

Load the channels from the channel files. To be implemented by the subclass

Example subclass implementation:

def load_channels(self, channel_files):
    channels = {}

    nifti_reader = sitk.ImageFileReader()
    nifti_reader.SetImageIO("NiftiImageIO")
    for i_channel_file in channel_files:
        nifti_reader.SetFileName(i_channel_file)
        i_channel = nifti_reader.Execute()
        i_channel_name = os.path.basename(i_channel_file)
        channels[i_channel_name] = i_channel
    return channels
Parameters

channel_files (list) – Paths to the channels to be loaded

Returns

dict – mapping the channel file to the loaded image

abstract load_masks(mask_files: list) → dict[source]

Load the masks from the mask files. To be implemented by the subclass

Example subclass implementation:

def load_masks(self, mask_files):
    masks = {}
    nifti_reader = sitk.ImageFileReader()
    nifti_reader.SetImageIO("NiftiImageIO")
    for i_mask_file in mask_files:
        nifti_reader.SetFileName(i_mask_file)
        i_mask = nifti_reader.Execute()
        i_mask = sitk.Cast(i_mask, sitk.sitkUInt8)
        i_mask_name = IO_utils.get_file_name(i_mask_file, self.image_extension)
        masks[i_mask_name] = i_mask
    return masks
Parameters

mask_files (list) – Paths to the masks to be loaded

Returns

dict – mapping the mask file to the loaded mask

property mask_names

Names of the masks

Returns

list – Mask names

property mask_size

The image size of the masks.

property masks

The masks present in the sample

Masks of a sample can be set by providing either a function,

or a tuple consisting of a function, possible function argument and function keyword arguments.

This function will then be applied to all masks in the sample. The function has to output either a SimpleITK Image or a list. In the last case it is assumed that these are patches and the class is updated accordingly

Returns

list – masks present in the sample

property output_channel_size

The image size of the channels

property output_channels

The output channels present in the sample

Output channels of a sample can be set by providing either a function,

or a tuple consisting of a function, possible function argument and function keyword arguments.

This function will then be applied to all output channels in the sample. The function has to output either a SimpleITK Image or a list. In the last case it is assumed that these are patches and the class is updated accordingly

Returns

list – Channels present in the sample

static promote_simpleitk_types(type_1: int, type_2: int) → int[source]

Get the datatype that can represent both datatypes

Parameters
  • type_1 (int) – SimpleITK datype of variable 1

  • type_2 (int) – SimpleITK datatype of variable 2

Returns

int – SimpleITK datatype that can represent both datatypes

update_channel_size()[source]

Update the channel size according to the current channels

update_labels() → None[source]
update_mask_size() → None[source]

Update the mask size according to the current masks

update_metadata() → None[source]
update_output_channel_size()[source]

Update the channel size according to the current channels

class PrognosAIs.Preprocessing.Samples.NIFTISample(**kwds)[source]

Bases: PrognosAIs.Preprocessing.Samples.ImageSample

init_image_files()[source]

Get the filepaths (folders or files) of the channels for a single sample. To be implemented by the subclass

Returns

list – The filepaths of the channels

load_channels(channel_files)[source]

Load the channels from the channel files. To be implemented by the subclass

Example subclass implementation:

def load_channels(self, channel_files):
    channels = {}

    nifti_reader = sitk.ImageFileReader()
    nifti_reader.SetImageIO("NiftiImageIO")
    for i_channel_file in channel_files:
        nifti_reader.SetFileName(i_channel_file)
        i_channel = nifti_reader.Execute()
        i_channel_name = os.path.basename(i_channel_file)
        channels[i_channel_name] = i_channel
    return channels
Parameters

channel_files (list) – Paths to the channels to be loaded

Returns

dict – mapping the channel file to the loaded image

load_masks(mask_files)[source]

Load the masks from the mask files. To be implemented by the subclass

Example subclass implementation:

def load_masks(self, mask_files):
    masks = {}
    nifti_reader = sitk.ImageFileReader()
    nifti_reader.SetImageIO("NiftiImageIO")
    for i_mask_file in mask_files:
        nifti_reader.SetFileName(i_mask_file)
        i_mask = nifti_reader.Execute()
        i_mask = sitk.Cast(i_mask, sitk.sitkUInt8)
        i_mask_name = IO_utils.get_file_name(i_mask_file, self.image_extension)
        masks[i_mask_name] = i_mask
    return masks
Parameters

mask_files (list) – Paths to the masks to be loaded

Returns

dict – mapping the mask file to the loaded mask

PrognosAIs.Preprocessing.Samples.get_sample_class(sample_type_name: str)[source]

Module contents