NiftiGifCreator#

class petpal.visualizations.image_visualization.NiftiGifCreator(path_to_image: str, view: str, output_directory: str, output_filename_prefix: str = '', fig_title: str = 'Patlak-$K_i$ Parametric Image', cbar_label: str = '$K_i$ (Infusion Rate)')#

This class is designed to create a GIF from a NIfTI image by iterating through its slices based on a specified view (‘coronal’, ‘sagittal’, or ‘axial’ (or ‘transverse’). The GIF is then written to the specified output directory with a filename prefix specified by the user.

Important

Makes a GIF of the image in the space defined by the nifty file.

Variables:
  • path_to_image (str) – Path to the NIfTI image file.

  • view (str) – Specifies on which plane the NIfTI image is sliced.

  • output_directory (str) – Directory where the GIF should be written.

  • prefix (str) – Filename prefix for the GIF.

  • fig (matplotlib.figure.Figure) – Matplotlib figure object for the GIF.

  • ax (matplotlib.axes.Axes) – Matplotlib axes object for the GIF.

  • im_kw (dict) – Dictionary of arguments passed to Axes.imshow for the GIF.

  • image (numpy.ndarray) – 3D numpy array representing the NIfTI image.

  • vmax (float) – Maximum value for the plot’s color limit.

  • ani_image (matplotlib.image.AxesImage) – An animated image rendered in the figure.

  • cbar (matplotlib.colorbar) – Colorbar associated with ani_image.

Example

An example of how to initialize and use the NiftiGifCreator class:

import petpal.image_visualization as pet_vis

gif_creator = pet_vis.NiftiGifCreator(path_to_image="./input.nii.gz",
                                      view="axial",
                                      output_directory="./output",
                                      output_filename_prefix="test")
gif_creator.make_gif()
gif_creator.write_gif()

Initialize the NiftiGifCreator. Assigns instance variables and sets up figure for animation.

Parameters:
  • path_to_image (str) – absolute path to the image file.

  • view (str) – desired view for the gif. Can be ‘coronal’, ‘sagittal’, ‘axial’.

  • output_directory (str) – absolute path to the directory where gif is to be stored.

  • output_filename_prefix (str, optional) – a string to be prepended to the filename of the gif.

  • fig_title (str, optional) – Title of the figure where the images will be plotted.

  • cbar_label (str, optional) – Label for the colorbar of the figure.

Raises:

ValueError – If view is not ‘coronal’, ‘sagittal’ or ‘axial’

_validate_view()#

Validate the value of view. It must be one of [‘coronal’, ‘sagittal’, ‘axial’, ‘x’, ‘y’, ‘z’].

Raises:

ValueError – If view is not ‘coronal’, ‘sagittal’, ‘axial’, ‘x’, ‘y’, ‘z’

make_first_frame()#

Makes the first frame of the animation by plotting an image on ax according to view.

Returns:

im (matplotlib.image.AxesImage) – Image plotted in the first frame.

Side Effects:

Modifies ani_image by creating the first frame of the GIF

set_figure_title_and_labels(title: str, cbar_label: str)#

Sets the title of the figure and labels of the color bar.

Parameters:
  • title (str) – Title to be set for the figure.

  • cbar_label (str) – Label to be set for the color bar.

Side Effects:
  • Modifies cbar with a new colorbar

  • Changes the title of fig

  • Modifies the x and y axes of ani_image

update_frame(i)#

Update function for matplotlib.animation.FuncAnimation. Updates the plotted image on the current axes.

Parameters:

i (int) – Index of the current frame

Returns:

Tuple containing im (matplotlib.image.AxesImage), which is updated to the i-th frame according to view.

Side Effects:

Modifies ani_image by updating its frame data

make_gif(frames: Iterable = None)#

Makes the GIF using FuncAnimation from matplotlib.animation module.

Parameters:

frames (Iterable, optional) – Iterable of frame indices to be included in the GIF. If not provided, all frames will be used.

Side Effects:

Modifies ani by creating a new matplotlib.animation.FuncAnimation object

write_gif(fps: int = 15)#

Writes the GIF to the output directory with filename {prefix}_view-{view}.gif.

Side Effects:
  • Creates a GIF file at the path specified by output_directory and prefix

  • Closes the fig matplotlib figure