viqa.nr_metrics.cnr.CNR

class viqa.nr_metrics.cnr.CNR(data_range=255, normalize=False, **kwargs)[source]

Class to calculate the contrast-to-noise ratio (CNR) for an image.

score_val

CNR score value of the last calculation.

Type:

float

parameters

Dictionary containing the parameters for CNR calculation.

Type:

dict

Parameters:
  • data_range ({1, 255, 65535}, default=255) – Data range of the returned data in data loading. Is used for image loading when normalize is True. Passed to viqa.utils.load_data().

  • normalize (bool, default False) – If True, the input images are normalized to the data_range argument.

  • **kwargs (optional) – Additional parameters for data loading. The keyword arguments are passed to viqa.utils.load_data().

  • chromatic (bool, default False) –

    If True, the input images are expected to be RGB images.

    Note

    Currently not supported.

score(img, **kwargs)[source]

Calculate the contrast-to-noise ratio (CNR) for an image.

Parameters:
  • img (np.ndarray or Tensor or str or os.PathLike) – Image to calculate score of.

  • **kwargs (optional) – Additional parameters for CNR calculation. The keyword arguments are passed to viqa.nr_metrics.cnr.contrast_to_noise_ratio(). If signal_center, background_center and radius are not given the class attribute parameters is used.

Returns:

score_val – CNR score value.

Return type:

float

print_score(decimals=2)[source]

Print the CNR score value of the last calculation.

Parameters:

decimals (int, default=2) – Number of decimal places to print the score value.

Warns:

RuntimeWarning – If score_val is not available.

visualize_centers(img, signal_center=None, background_center=None, radius=None, export_path=None, **kwargs)[source]

Visualize the centers for CNR calculation.

The visualization shows the signal and background regions in a matplotlib plot. If export_path is provided, the plot is saved to the given path.

Parameters:
  • img (np.ndarray or Tensor or str or os.PathLike) – Image to visualize.

  • signal_center (Tuple(int), optional) – Center of the signal. Order is (x, y) for 2D images and (x, y, z) for 3D images. If not given the class attribute parameters is used.

  • background_center (Tuple(int), optional) – Center of the background. Order is (x, y) for 2D images and (x, y, z) for 3D images. If not given the class attribute parameters is used.

  • radius (int, optional) – Width of the regions. If not given the class attribute parameters is used.

  • export_path (str or os.PathLike, optional) – Path to export the visualization to.

  • **kwargs (optional) – Additional parameters for visualization. The keyword arguments are passed to matplotlib.pyplot.subplots().

Raises:
  • ImportError – If the visualization fails in a non-interactive environment.

  • ValueError – If the given centers are not in the same dimension as the image. If the center is too close to the border. If the image is not 2D or 3D.

  • TypeError – If the center is not a tuple of integers. If the radius is not a positive integer.

set_centers(img, **kwargs)[source]

Visualize and set the centers for CNR calculation interactively.

The visualization shows the signal and background regions in a matplotlib plot.

Parameters:
  • img (np.ndarray or Tensor or str or os.PathLike) – Image to visualize.

  • **kwargs (optional) –

    Additional parameters for visualization. The keyword arguments are passed to visualize_centers() and matplotlib.pyplot.subplots(). background_center, signal_center, and radius can be provided as starting points for the interactive center selection. If not provided, the center of the image is used as the starting point.

    Caution

    The class attribute parameters takes precedence over the given parameters.

Warning

This method is only available in an IPython environment. If not in an IPython environment, the method will try to visualize the centers in a non-interactive environment.

Raises:
  • ImportError – If the visualization fails in a non-interactive environment.

  • ValueError – If the given centers are not in the same dimension as the image. If the center is too close to the border. If the image is not 2D or 3D.

  • TypeError – If the center is not a tuple of integers. If the radius is not a positive integer.

export_results(path, filename)

Export the score to a csv file.

Parameters:
  • path (str) – The path where the csv file should be saved.

  • filename (str) – The name of the csv file.

Notes

The arguments get passed to viqa.utils.export_results().

load_images(img)

Load the image.

Uses the viqa.utils.load_data() function to load the image.

Parameters:

img (np.ndarray, viqa.ImageArray, torch.Tensor, str or os.PathLike) – The image to load.

Returns:

img – The loaded image as an viqa.utils.ImageArray.

Return type:

viqa.ImageArray