Utils module

This modules contains helper functions for read and write I/O functions. Please use the functions to read/write images when using the packages in this repository to ensure consistency. For example, niibabel and simpleITK read in image differently, which can cause problems when using these together. The read/write functions in this module have all been adapated so that they are consistent with each other.

For reading .nii / .nii.gz / .mha use:
>>> image, spacing = read_image(vol_path)
For reading matlab image files use:
>>> image, spacing = read_matlab_image(vol_path)
For writing .nii / .nii.gz / .mha use:
>>> write_image(vol_path, image, spacing)
fetalbrain.utils.generate_nii_affine(spacing: tuple[float, float, float]) ndarray[source]

Generate the affine matrix from the spacing. The negative spacing for the first two dimensions is to ensure consistency with the mha format.

Parameters:

spacing – the spacing of the image

Returns:

affine – the affine matrix of size [4,4]

Example

>>> affine = generate_nii_affine(spacing=(0.6, 0.6, 0.6))
>>> assert affine.shape == (4,4)
fetalbrain.utils.plot_midplanes(image: ndarray, title: str) Figure[source]

Plot the midplanes of a 3D image

Parameters:
  • image – 3D array of image data

  • title – title of the plot

Returns:

fig – reference to the plot

Example

>>> test_image =  np.random.rand(160, 160, 160)
>>> fig = plot_midplanes(test_image, "Original")
fetalbrain.utils.plot_planes_segm(image: ndarray, segmentation: ndarray, title: str | None = None) Figure[source]

Plot planes of a 3D image and segmentation to demonstrate segmentation results.

TO-DO: Plot this properly with a colorbar and labels (taking a dictionary as input with class names)

Parameters:
  • image – 3D array of image data

  • segmentation – 3D array of segmentation data

  • title – title of the plot

Returns:

fig – reference to the plot

Example

>>> test_image =  np.random.rand(160, 160, 160)
>>> test_segmentation = np.random.randint(0, 10, size = (160,160,160))
>>> fig = plot_planes_segm(test_image, test_segmentation,  "segmentation")
fetalbrain.utils.read_image(vol_path: Path | str) tuple[ndarray, tuple[float, float, float]][source]

Reads in an image, and returns the numpy array and spacing

Parameters:

vol_path – path to the image

Raises:

ValueError – when the path has an unknown file extension

Returns:
  • nii_array – np.ndarray of the image

  • spacing – spacing of the image

Example

>>> example_scan, spacing = read_image(IMAGE_PATH)
fetalbrain.utils.read_matlab_image(vol_path: Path | str) tuple[ndarray, tuple[float, float, float]][source]

This function can be used to read the matlab image file from the longitudinal atlas. To use this function the package needs to be install with the [matlab] addition (or this can be manually installed with pip install scipy). As there is no spacing information in the matlab files, this is hardcode to (0.6, 0.6, 0.6).

Parameters:

vol_path – path to the matlab image

Returns:
  • image_data – np.ndarray of the image

  • spacing – spacing of the image (0.6, 0.6, 0.6)

fetalbrain.utils.write_image(vol_path: ~pathlib.Path | str, image_array: ~numpy.ndarray, spacing: tuple[float, float, float] = (0.6, 0.6, 0.6), dtype: ~numpy.dtype = <class 'numpy.uint8'>, segm: bool = False) None[source]

Saves a numpy array as an nifti or mha image, the type is determined by the file extension. The images are written with integer pixel values between 0 and 255

Parameters:
  • vol_path – path to save the images to

  • image_array – numpy array with the image data

  • spacing – spacing of the pixels, defaults to (0.6, 0.6, 0.6)

  • dtype – dtype of the saved image, defaults to np.uint8

Example

>>> test_image =  np.random.rand(160, 160, 160)
>>> savepath = 'test.nii.gz'
>>> write_image(savepath, test_image)