Skip to content

Commit

Permalink
Read reference images externally
Browse files Browse the repository at this point in the history
Read reference images externally, like the input data.
  • Loading branch information
gerritholl committed Nov 22, 2024
1 parent 39ba8e4 commit 003c17c
Showing 1 changed file with 53 additions and 29 deletions.
82 changes: 53 additions & 29 deletions satpy/tests/behave/features/steps/image_comparison.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,74 +1,96 @@
# Copyright (c) 2024 Satpy developers
#
# This file is part of satpy.
#
# satpy is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# satpy is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.
"""Image comparison tests."""

import os
import warnings
from datetime import datetime
from glob import glob
from PIL import Image

import cv2
import dask
import numpy as np
from behave import given, when, then
from satpy import Scene
from datetime import datetime
import pytz
from behave import given, then, when

from satpy import Scene

ext_data_path = "/app/ext_data"
#ext_data_path = "/home/bildabgleich/pytroll-image-comparison-tests/data"
threshold = 2000

# Define a before_all hook to create the timestamp and test results directory
def before_all(context):
berlin_time = datetime.now(pytz.timezone('Europe/Berlin'))
"""Define a before_all hook to create the timestamp and test results directory."""
berlin_time = datetime.now(pytz.timezone("Europe/Berlin"))
context.timestamp = berlin_time.strftime("%Y-%m-%d-%H-%M-%S")
context.test_results_dir = f"{ext_data_path}/test_results/image_comparison/{context.timestamp}"
os.makedirs(os.path.join(context.test_results_dir, 'generated'), exist_ok=True)
os.makedirs(os.path.join(context.test_results_dir, 'difference'), exist_ok=True)
os.makedirs(os.path.join(context.test_results_dir, "generated"), exist_ok=True)
os.makedirs(os.path.join(context.test_results_dir, "difference"), exist_ok=True)

# Write the timestamp to test_results.txt
results_file = os.path.join(context.test_results_dir, 'test_results.txt')
with open(results_file, 'a') as f:
results_file = os.path.join(context.test_results_dir, "test_results.txt")
with open(results_file, "a") as f:
f.write(f"Test executed at {context.timestamp}.\n\n")

# Register the before_all hook
def setup_hooks():
"""Register the before_all hook."""
from behave import use_fixture
from behave.runner import Context

use_fixture(before_all, Context)

setup_hooks()
@given('I have a {composite} reference image file from {satellite}')
@given("I have a {composite} reference image file from {satellite}")
def step_given_reference_image(context, composite, satellite):
"""Prepare a reference image."""
reference_image = f"reference_image_{satellite}_{composite}.png"
context.reference_image = cv2.imread(f"./features/data/reference/{reference_image}")
#context.reference_image = cv2.imread(f"./features/data/reference/{reference_image}")
context.reference_image = cv2.imread(f"{ext_data_path}/reference_images/{reference_image}")
context.reference_different_image = cv2.imread(f"./features/data/reference_different/{reference_image}")
context.satellite = satellite
context.composite = composite


@when('I generate a new {composite} image file from {satellite}')
@when("I generate a new {composite} image file from {satellite}")
def step_when_generate_image(context, composite, satellite):
os.environ['OMP_NUM_THREADS'] = os.environ['MKL_NUM_THREADS'] = '2'
os.environ['PYTROLL_CHUNK_SIZE'] = '1024'
warnings.simplefilter('ignore')
dask.config.set(scheduler='threads', num_workers=4)
"""Generate test images."""
os.environ["OMP_NUM_THREADS"] = os.environ["MKL_NUM_THREADS"] = "2"
os.environ["PYTROLL_CHUNK_SIZE"] = "1024"
warnings.simplefilter("ignore")
dask.config.set(scheduler="threads", num_workers=4)

# Get the list of satellite files to open
filenames = glob(f'{ext_data_path}/satellite_data/{satellite}/*.nc')
filenames = glob(f"{ext_data_path}/satellite_data/{satellite}/*.nc")

scn = Scene(reader='abi_l1b', filenames=filenames)
scn = Scene(reader="abi_l1b", filenames=filenames)

scn.load([composite])

# Save the generated image in the generated folder
generated_image_path = os.path.join(context.test_results_dir, 'generated',
f'generated_{context.satellite}_{context.composite}.png')
scn.save_datasets(writer='simple_image', filename=generated_image_path)
generated_image_path = os.path.join(context.test_results_dir, "generated",
f"generated_{context.satellite}_{context.composite}.png")
scn.save_datasets(writer="simple_image", filename=generated_image_path)

# Save the generated image in the context
context.generated_image = cv2.imread(generated_image_path)


@then('the generated image should be the same as the reference image')
@then("the generated image should be the same as the reference image")
def step_then_compare_images(context):
"""Compare test image to reference image."""
# Load the images
imageA = cv2.cvtColor(context.reference_image, cv2.COLOR_BGR2GRAY) # reference_different_image for testing only
imageB = cv2.cvtColor(context.generated_image, cv2.COLOR_BGR2GRAY)
Expand All @@ -81,16 +103,16 @@ def step_then_compare_images(context):
result_matrix = (array1 != array2).astype(np.uint8) * 255

# Save the resulting numpy array as an image in the difference folder
diff_image_path = os.path.join(context.test_results_dir, 'difference',
f'diff_{context.satellite}_{context.composite}.png')
diff_image_path = os.path.join(context.test_results_dir, "difference",
f"diff_{context.satellite}_{context.composite}.png")
cv2.imwrite(diff_image_path, result_matrix)

# Count non-zero pixels in the result matrix
non_zero_count = np.count_nonzero(result_matrix)

# Write the results to a file in the test results directory
results_file = os.path.join(context.test_results_dir, 'test_results.txt')
with open(results_file, 'a') as f:
results_file = os.path.join(context.test_results_dir, "test_results.txt")
with open(results_file, "a") as f:
f.write(f"Test for {context.satellite} - {context.composite}\n")
f.write(f"Non-zero pixel differences: {non_zero_count}\n")
if non_zero_count < threshold:
Expand All @@ -99,4 +121,6 @@ def step_then_compare_images(context):
f.write(f"Result: Failed - {non_zero_count} pixel differences exceed the threshold of {threshold}.\n\n")

# Assert that the number of differences is below the threshold
assert non_zero_count < threshold, f"Images are not similar enough. {non_zero_count} pixel differences exceed the threshold of {threshold}."
assert non_zero_count < threshold, (f"Images are not similar enough. "
f"{non_zero_count} pixel differences exceed the threshold of "
f"{threshold}.")

0 comments on commit 003c17c

Please sign in to comment.