Skip to content

More tests on graphical comparisons #47

@jcrivenaes

Description

@jcrivenaes

The previous issue #46 demonstrates that unit tests that compare graphical output should be addressed.

One taks is to make a separate comparison function, used by tests, that can compare the generated plots with base line plots. This has been tried earlier but the tests need some kind of tolerance due to subtle library changes (and possibly differences between os)

E.g. (via Copilot):

# conftest.py
import os
from PIL import Image
from skimage.metrics import structural_similarity as compare_ssim
import numpy as np

def compare_plots(generated_plot_path, reference_plot_path, threshold=0.95):
    """
    Compare two plots using Structural Similarity Index (SSIM).

    :param generated_plot_path: Path to the generated plot.
    :param reference_plot_path: Path to the reference plot.
    :param threshold: SSIM threshold to consider images approximately equal.
    :return: None. Raises AssertionError if plots are not approximately equal.
    """
    # Load the images
    generated_image = Image.open(generated_plot_path).convert('L')
    reference_image = Image.open(reference_plot_path).convert('L')

    # Convert images to numpy arrays
    generated_image_np = np.array(generated_image)
    reference_image_np = np.array(reference_image)

    # Compute the Structural Similarity Index (SSIM) between the images
    ssim_index, _ = compare_ssim(generated_image_np, reference_image_np, full=True)

    print("SSIM index:", ssim_index)

    # Assert that the SSIM index is above the threshold
    assert ssim_index > threshold, "The plot has changed significantly!"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions