Utils

This package provides utility functions as image operations, various decorators, path operations and time operations.

classtype_func(class_object)[source]

Decorator to Check whether the first argument of the decorated function is a subclass of a certain type

Parameters

class_object (Any) – type the first function argument should be subclassed from

Returns

Return type

Wrapped Function

Raises

AssertionError – First argument of decorated function is not a subclass of given type

dtype_func(class_object)[source]

Decorator to Check whether the first argument of the decorated function is of a certain type

Parameters

class_object (Any) – type the first function argument should have

Returns

Return type

Wrapped Function

Raises

AssertionError – First argument of decorated function is not of given type

make_deprecated(new_func)[source]

Decorator which raises a DeprecationWarning for the decorated object

Parameters

new_func (Any) – new function which should be used instead of the decorated one

Returns

Return type

Wrapped Function

Raises

Deprecation Warning

numpy_array_func(func)
torch_module_func(func)
torch_tensor_func(func)
bounding_box(mask, margin=None)[source]

Calculate bounding box coordinates of binary mask

Parameters
  • mask (SimpleITK.Image) – Binary mask

  • margin (int, default: None) – margin to be added to min/max on each dimension

Returns

bounding box coordinates of the form (xmin, xmax, ymin, ymax, zmin, zmax)

Return type

tuple

calculate_origin_offset(new_spacing, old_spacing)[source]

Calculates the origin offset of two spacings

Parameters
  • new_spacing (list or np.ndarray or tuple) – new spacing

  • old_spacing (list or np.ndarray or tuple) – old spacing

Returns

origin offset

Return type

np.ndarray

max_energy_slice(img)[source]

Determine the axial slice in which the image energy is max

Parameters

img (SimpleITK.Image) – given image

Returns

slice index

Return type

int

sitk_copy_metadata(img_source, img_target)[source]

Copy metadata (=DICOM Tags) from one image to another

Parameters
  • img_source (SimpleITK.Image) – Source image

  • img_target (SimpleITK.Image) – Source image

Returns

Target image with copied metadata

Return type

SimpleITK.Image

sitk_new_blank_image(size, spacing, direction, origin, default_value=0.0)[source]

Create a new blank image with given properties

Parameters
  • size (list or np.ndarray or tuple) – new image size

  • spacing (list or np.ndarray or tuple) – spacing of new image

  • direction – new image’s direction

  • origin – new image’s origin

  • default_value (float) – new image’s default value

Returns

Blank image with given properties

Return type

SimpleITK.Image

sitk_resample_to_image(image, reference_image, default_value=0.0, interpolator=SimpleITK.sitkLinear, transform=None, output_pixel_type=None)[source]

Resamples Image to reference image

Parameters
  • image (SimpleITK.Image) – the image which should be resampled

  • reference_image (SimpleITK.Image) – the resampling target

  • default_value (float) – default value

  • interpolator (Any) – implements the actual interpolation

  • transform (Any (default: None)) – transformation

  • output_pixel_type (Any (default:None)) – type of output pixels

Returns

resampled image

Return type

SimpleITK.Image

sitk_resample_to_shape(img, x, y, z, order=1)[source]

Resamples Image to given shape

Parameters
  • img (SimpleITK.Image) –

  • x (int) – shape in x-direction

  • y (int) – shape in y-direction

  • z (int) – shape in z-direction

  • order (int) – interpolation order

Returns

Resampled Image

Return type

SimpleITK.Image

sitk_resample_to_spacing(image, new_spacing=(1.0, 1.0, 1.0), interpolator=SimpleITK.sitkLinear, default_value=0.0)[source]

Resamples SITK Image to a given spacing

Parameters
  • image (SimpleITK.Image) – image which should be resampled

  • new_spacing (list or np.ndarray or tuple) – target spacing

  • interpolator (Any) – implements the actual interpolation

  • default_value (float) – default value

Returns

resampled Image with target spacing

Return type

SimpleITK.Image

subdirs(d)[source]

For a given directory, return a list of all subdirectories (full paths)

Parameters

d (string) – given root directory

Returns

list of strings of all subdirectories

Return type

list

now()[source]

Return current time as YYYY-MM-DD_HH-MM-SS

Returns

current time

Return type

string

class LookupConfig(*args, **kwargs)[source]

Bases: trixi.util.Config

Helper class to have nested lookups in all subdicts of Config

nested_get(key, *args, **kwargs)[source]

Returns all occurances of key in self and subdicts

Parameters
  • key (str) – the key to search for

  • *args – positional arguments to provide default value

  • **kwargs – keyword arguments to provide default value

Raises

KeyError – Multiple Values are found for key (unclear which value should be returned) OR No Value was found for key and no default value was given

Returns

value corresponding to key (or default if value was not found)

Return type

Any