Gamma Index Analysis

A collection of functions for the Gamma Index (GI) analysis, implemented in the fredtools.GammaIndex subpackage.

Note

The GI calculation is performed by libFredGI, an external C++ library (version 4.6), which is distributed with FREDtools in compiled form, in fredtools/GammaIndex:

  • libFredGI.so: Linux on x86_64, with glibc 2.28 or newer (e.g. RHEL/Rocky 8, Ubuntu 20.04, Debian 10 and newer),

  • libFredGI.dll: Windows 10 or newer on x86_64,

  • libFredGI.dylib: macOS 11 or newer, on Apple Silicon and Intel (a universal library).

A 64-bit Python is needed, and the library of the running system is chosen automatically. The results are the same on all three systems (bit-identical in the tests of the library). The library is not covered by the GPL licence of FREDtools; see its notice LICENSE-libFredGI.md.

On macOS, a library downloaded with a web browser (e.g. FREDtools downloaded as a ZIP archive) can be blocked by Gatekeeper. The quarantine attribute is then removed with xattr -d com.apple.quarantine fredtools/GammaIndex/libFredGI.dylib; a clone made with git is not affected.

The gamma index engine has been validated against the independent implementations in PyMedPhys and plastimatch. The comparison, together with a description of the algorithms and the reasons why the results of the tools are never exactly the same, is presented in the Gamma Index Validation Report.

fredtools.calcGammaIndex(imgRef: Image, imgEval: Image, DD: Annotated[int | float | number, FieldInfo(annotation=NoneType, required=True, metadata=[Strict(strict=True), Ge(ge=0), Le(le=100)])], DTA: int | float | number, DCO: Annotated[int | float | number, FieldInfo(annotation=NoneType, required=True, metadata=[Strict(strict=True), Ge(ge=0), Le(le=1)])], DDType: Literal['local', 'global'] = 'local', globalNorm: int | float | number | None = None, stepSize: int | float | number = 10, fractionalStepSize: bool = True, mode: Literal['gamma', 'pass-rate'] = 'gamma', displayInfo: bool = False) → Image

Calculate gamma index map.

The function calculates the gamma index map using the imgRef and imgEval, defined as SimpleITK 2D or 3D image objects of the same dimension, as the reference and evaluation images, respectively. The gamma index test is performed with a defined dose difference (DD) given in [%], distance to agreement (DTA) given in the same length unit as the reference image (in [mm] by default) and is calculated for the reference voxels with a dose strictly greater than the dose cut-off, which is a fraction of the global normalisation dose (the maximum dose in the reference image by default) given by the DCO parameter. The voxels with a dose equal to the cut-off are not analysed (-1). The gamma index can be calculated for local or global dose difference in two modes:

  • gamma: each voxel represents the gamma index value and the voxels excluded from the GI analysis have values -1.

  • pass-rate: each voxel represents passing (1) or failing (0) of the gamma index test and the voxels excluded from the GI analysis have values -1.

The gamma index calculation is performed by an external C++ library (libFredGI), compiled for Linux (x86_64), Windows (x86_64) and macOS (Apple Silicon and Intel), and shipped with FREDtools. The gamma index engine was developed by Angelo Schiavi and validated against the PyMedPhys [PyMedPhys] python library and the plastimatch [Plastimatch] gamma index tool (see the Gamma Index Validation Report).

Parameters:
  • imgRef (SimpleITK Image) – An object of a SimpleITK 2D or 3D image describing the reference. The image must have a scalar pixel type. An axis of size 1 is a single slice whose dose is constant across its voxel thickness. A 1D profile is given as a 2D or 3D image with all axes but one of size 1 (see Notes).

  • imgEval (SimpleITK Image) – An object of a SimpleITK 2D or 3D image describing the evaluation, of the same dimension and with the same requirements as for the imgRef. It can have a different size, spacing and origin than the imgRef but must have the same direction matrix.

  • DD (float) – Dose difference in [%].

  • DTA (float) – Distance-to-agreement, usually in [mm].

  • DCO (float) – Dose cut-off given as the fraction of the global normalisation dose (the maximum reference value by default, see globalNorm). The gamma index is calculated only for the reference voxels with a dose strictly greater than the cut-off. The voxels with a dose equal to or lower than the cut-off are not analysed (-1).

  • DDType ({'local', 'global'}, optional) –

    Method of calculating the absolute dose difference criterion. (def. ‘local’):

    • ’local’ : the absolute dose difference calculated as DD percent of the local reference value.

    • ’global’ : the absolute dose difference calculated as DD percent of the maximum reference value.

  • globalNorm (float, optional) – Global normalisation dose in the units of the input images, used to calculate the dose cutoff (DCO) and the ‘global’ dose difference criterion (DD). If not given or None then the maximum value of the reference image is used. (def. None)

  • stepSize (float, optional) – Step size to search for the minimum gamma index value. It can be given as an absolute step in the reference length unit (for instance in [mm]) if fractionalStepSize is False, or as the number of subdivisions of the distance-to-agreement if fractionalStepSize is True, so that the search step is calculated as DTA/stepSize. (def. 10)

  • fractionalStepSize (bool, optional) – Determine if the stepSize should be treated as the absolute step value (False) or as the number of subdivisions of the distance-to-agreement (True), meaning that the search step is DTA/stepSize. (def. True)

  • mode ({'gamma', 'pass-rate'}, optional) –

    Mode of calculation. (def. ‘gamma’):

    • ’gamma’: each voxel represents the gamma index value and the voxels excluded from the GI analysis have values -1.

    • ’pass-rate’: each voxel represents passing (1) or failing (0) of the gamma index test and the voxels excluded from the GI analysis have values -1.

  • displayInfo (bool, optional) – Displays a summary of the function results. (def. False)

Returns:

An object of a SimpleITK image describing the gamma index. It is of float or integer type when the calculation mode is ‘gamma’ or ‘pass-rate’, respectively.

Return type:

SimpleITK Image

Raises:
  • TypeError – If imgRef or imgEval is not an instance of a SimpleITK image object describing a scalar 2D or 3D image (e.g. a vector or a 4D image).

  • ValueError – If imgRef and imgEval have different dimensions, the value of DD, DTA, DCO, globalNorm or stepSize is out of range, DDType or mode cannot be recognised, the direction matrices of imgRef and imgEval do not match, the images do not overlap (detached frames of reference, see fredtools.compareImgFoROverlap()), any of the images contains NaN or Inf values, the maximum of the reference image is not positive, or no voxel of the reference image is above the dose cutoff.

  • OSError – If the gamma index library is not available for the platform (see above) or cannot be loaded.

  • RuntimeError –

    Run time error is raised when the execution of the gamma calculation failed, did not finish, or the library returned an invalid result (no voxel analysed). The following error codes can be raised:

    • -1 : generic error

    • -20 : value range error

    • -21 : value is nan error

    • -22 : value is inf error

    • -29 : null pointer error

    • -50 : ill-defined vector

    • -51 : ill-defined dimensions

    • -52 : ill-defined spacing

    • -53 : ill-defined offset

    • -100 : computation ongoing

    • -101 : setup not complete

    • -102 : criteria not defined

    • -103 : ref map not defined

    • -104 : eval map not defined

    • -105 : computation not done

See also

getGIstat

calculate the gamma index statistics including the gamma index pass rate.

Notes

The gamma index pass rates calculated with this function agree within 1 percentage point with PyMedPhys and plastimatch. Identical gamma index maps are not expected, because the tools discretise the search space and interpolate the evaluation dose differently. Refer to the Gamma Index Validation Report to read more about the validation results.

The images are required to be 2D or 3D, of the same dimension, and to have the same direction matrix. 2D images are calculated as 3D images with a single slice, so the gamma index search is in the image plane, and the gamma index map is returned as a 2D image. The calculation is performed in the common index frame of the images, which preserves the distances for any common direction matrix, and the resulting gamma index map is returned in the frame of reference of the imgRef. The dose cutoff in dose units is DCO times the global normalisation dose, computed by the gamma index library in single precision (float32), and the ‘global’ dose difference criterion is DD percent of the global normalisation dose, which is the maximum of the reference image unless globalNorm is given.

A 1D gamma index (of dose profiles) is calculated with images in which all axes but the one of the profile have size 1, because SimpleITK has no 1D images:

  • a 2D image with a single row or column, for instance of size [N, 1] for a profile along x,

  • a 3D image with two axes of size 1, for instance of size [N, 1, 1], [1, N, 1] or [1, 1, N].

The dose is constant across the voxel thickness of the axes of size 1, so the gamma index search is along the profile, and the result equals the gamma index of the central row of a 3D image constant along the other two axes. Both images must describe the profile along the same axis, and the gamma index map has the size of the reference image.

The calculation runs in a background thread of the gamma index library and can be interrupted with Ctrl+C (or by interrupting the Jupyter kernel), which stops the calculation and raises KeyboardInterrupt.

Examples

See example jupyter notebook at [GITutorial]

fredtools.getGIstat(imgGI: Image, displayInfo: bool = False) → DottedDict

Get statistics of Gamma Index.

The function calculates Gamma Index statistics from an image defined as a SimpleITK image object. Two modes of the gamma index calculation are recognised automatically based on the image type:

  • gamma (float): each voxel represents the gamma index value and the voxels excluded from the GI analysis have values -1 or numpy.nan.

  • pass-rate (integer): each voxel represents passing (1) or failing (0) of the gamma index test and the voxels excluded from the GI analysis have values -1.

Parameters:
  • imgGI (SimpleITK Image) – An object of a SimpleITK image.

  • displayInfo (bool, optional) – Displays a summary of the function results. (def. False)

Returns:

A dictionary-like object (DottedDict) with the gamma index statistics with the keys: ‘passRate’ (gamma index pass rate in [%]), ‘mean’, ‘std’, ‘min’ and ‘max’ of the gamma index values. In the ‘pass-rate’ mode the ‘mean’, ‘std’, ‘min’ and ‘max’ values are numpy.nan.

Return type:

DottedDict

Raises:
  • TypeError – If imgGI is not an instance of a SimpleITK image object, or the image is neither of integer nor float type.

  • ValueError – If the image is of integer type (‘pass-rate’ mode) but contains unique values other than -1, 0 and 1.

See also

calcGammaIndex

calculate the Gamma Index map for two images.

fredtools.getGIcmap(maxGI: int | float | number, N: Annotated[int, Ge(ge=0)] = 256) → LinearSegmentedColormap

Get colormap for Gamma Index images.

The function creates a colormap for Gamma Index (GI) images, that can be used by matplotlib.pyplot.imshow function for displaying 2D images. The colormap is created from 0 to the maxGI value, whereas from 0 to 1 (GI test passed) the color changes from dark blue to white, and from 1 to maxGI it changes from light red to red.

Parameters:
  • maxGI (scalar) – The maximum value of the colormap. Values less than 1 are clamped to 1 (with a warning).

  • N (int, optional) – Number of segments of the colormap, at least 2. (def. 256)

Returns:

An instance of matplotlib.colors.LinearSegmentedColormap object.

Return type:

colormap

Raises:

ValueError – If N is not an integer greater than 1.

See also

calcGammaIndex

calculate the Gamma Index for two images.

Examples

It is assumed that the img is an image describing a slice of Gamma Index (GI) values calculated up to maximum value 3. To plot the GI map with the GI colormap:

>>> plt.imshow(ft.arr(img), cmap=getGIcmap(maxGI=3))