Metrics

Metrics for comparing synthetic images with real microscope images.

This module provides three key metrics:

  • Color/Intensity Distribution (histogram comparison)

  • SSIM (Structural Similarity Index)

  • PSNR (Peak Signal-to-Noise Ratio)

These metrics are used to evaluate and optimize synthetic image generation to match real microscope images.

load_image(path: Path) ndarray

Load a TIFF image and convert to float [0,1].

Parameters:

path (Path) – Path to the image file.

Returns:

Image as float array with values in [0,1].

Return type:

np.ndarray

compute_color_distribution(image1: ndarray, image2: ndarray, bins: int = 256) Dict[str, ndarray]

Compute and compare color/intensity distributions of two images.

Parameters:
  • image1 (np.ndarray) – First image (e.g., original).

  • image2 (np.ndarray) – Second image (e.g., synthetic).

  • bins (int) – Number of histogram bins.

Returns:

Dictionary containing ‘hist1’, ‘hist2’, ‘bin_edges’,

’histogram_diff’, and ‘histogram_distance’ (L1 norm).

Return type:

dict

compute_ssim(image1: ndarray, image2: ndarray, data_range: float = 1.0) Dict[str, float]

Compute Structural Similarity Index (SSIM) between two images.

SSIM measures the structural similarity between images, considering luminance, contrast, and structure. Values range from -1 to 1, where 1 indicates perfect similarity.

Parameters:
  • image1 (np.ndarray) – First image (e.g., original).

  • image2 (np.ndarray) – Second image (e.g., synthetic).

  • data_range (float) – Data range of the images (1.0 for float images).

Returns:

Dictionary containing ‘ssim’ score (higher is better, max=1.0).

Return type:

dict

compute_psnr(image1: ndarray, image2: ndarray, data_range: float = 1.0) Dict[str, float]

Compute Peak Signal-to-Noise Ratio (PSNR) between two images.

PSNR measures the ratio between the maximum possible signal power and the power of corrupting noise. Higher values indicate better quality. Typical range: 20-50 dB (higher is better).

Parameters:
  • image1 (np.ndarray) – First image (e.g., original).

  • image2 (np.ndarray) – Second image (e.g., synthetic).

  • data_range (float) – Data range of the images (1.0 for float images).

Returns:

Dictionary containing ‘psnr’ value in dB (higher is better).

Return type:

dict

compute_all_metrics(original: ndarray, synthetic: ndarray, bins: int = 256) Dict[str, any]

Compute all metrics comparing original and synthetic images.

This is the main method that computes all three metrics in one call.

Parameters:
  • original (np.ndarray) – Original microscope image (float [0,1]).

  • synthetic (np.ndarray) – Synthetic image (float [0,1]).

  • bins (int) – Number of bins for histogram.

Returns:

Dictionary containing ‘color_distribution’, ‘ssim’, ‘psnr’,

and ‘summary’ statistics.

Return type:

dict

save_metrics_json(metrics: Dict, output_path: Path) None

Save metrics to a JSON file.

Parameters:
  • metrics (dict) – Metrics dictionary from compute_all_metrics().

  • output_path (Path) – Path where to save the JSON file.

plot_metrics(original: ndarray, synthetic: ndarray, metrics: Dict, output_path: Path | None = None, title: str = 'Image Comparison Metrics') None

Create a visualization of all metrics.

Parameters:
  • original (np.ndarray) – Original image.

  • synthetic (np.ndarray) – Synthetic image.

  • metrics (dict) – Metrics dictionary from compute_all_metrics().

  • output_path (Path) – If provided, save plot to this path. If None, plot is only displayed.

  • title (str) – Title for the plot.