Skip to content

Commit 8dcc65d

Browse files
committed
Comparison between pngs now uses pillow.
1 parent 29a08e4 commit 8dcc65d

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.9.5"
44
description = "Holography Antenna Commissioning Kit"
55
readme = "README.md"
66
requires-python = ">= 3.11, < 3.14"
7-
dependencies = [ "astropy", "dask", "shapely", "distributed", "dropbox", "toolviper", "ipywidgets", "matplotlib", "numba>=0.57.0", "numpy<=2.2", "prettytable", "pycryptodome", "pytest", "pytest-cov", "pytest-html", "scikit_image", "scikit-learn", "scipy", "rich", "xarray", "zarr<3.0.0", "bokeh", "jupyterlab", "python_casacore>=3.6.1; sys_platform != \"darwin\" ",]
7+
dependencies = [ "astropy", "dask", "shapely", "distributed", "dropbox", "toolviper", "ipywidgets", "matplotlib", "numba>=0.57.0", "numpy<=2.2", "prettytable", "pycryptodome", "pytest", "pytest-cov", "pytest-html", "scikit_image", "scikit-learn", "scipy", "rich", "xarray", "zarr<3.0.0", "bokeh", "pillow", "jupyterlab", "python_casacore>=3.6.1; sys_platform != \"darwin\" ",]
88
[[project.authors]]
99
name = "Joshua Hoskins"
1010
email = "jhoskins@nrao.edu"

tests/unit/test_beamcut_mds.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,48 @@
44
import io
55
import contextlib
66
import xarray
7-
import hashlib
87

8+
from PIL import Image, ImageChops
99
from collections.abc import KeysView
1010

1111
from toolviper.utils import data
1212

1313
from astrohack import open_beamcut, AstrohackBeamcutFile
1414

1515

16-
def are_binary_files_equal(file_a, file_b):
17-
with open(file_a, "rb") as bin_file_a:
18-
hash_a = hashlib.md5(bin_file_a.read()).hexdigest()
19-
with open(file_b, "rb") as bin_file_b:
20-
hash_b = hashlib.md5(bin_file_b.read()).hexdigest()
21-
return hash_a == hash_b
16+
def are_png_files_equal(img_path1, img_path2):
17+
# with open(file_a, "rb") as bin_file_a:
18+
# hash_a = hashlib.md5(bin_file_a.read()).hexdigest()
19+
# with open(file_b, "rb") as bin_file_b:
20+
# hash_b = hashlib.md5(bin_file_b.read()).hexdigest()
21+
# return hash_a == hash_b
22+
try:
23+
# Open images (Pillow handles various modes and removes metadata concerns for pixel data)
24+
with Image.open(img_path1) as img1, Image.open(img_path2) as img2:
25+
# Ensure both images are in the same mode for a reliable comparison (e.g., 'RGBA')
26+
img1 = img1.convert("RGBA")
27+
img2 = img2.convert("RGBA")
28+
29+
# Check if dimensions are the same
30+
if img1.size != img2.size:
31+
return False
32+
33+
# Calculate the difference between the images
34+
# This results in a new image where differing pixels are non-zero
35+
diff = ImageChops.difference(img1, img2)
36+
37+
# Split channels and check if the bounding box of non-zero pixels in any channel is None
38+
# If getbbox() returns None, the channel is all black (no differences)
39+
channels = diff.split()
40+
for channel in channels:
41+
if channel.getbbox() is not None:
42+
return False
43+
44+
return True
45+
46+
except IOError as e:
47+
print(f"Error opening images: {e}")
48+
return False
2249

2350

2451
class TestBeamcut:
@@ -151,21 +178,21 @@ def test_beamcut_plots(self):
151178
beamcut_mds = open_beamcut(self.remote_beamcut_name)
152179

153180
beamcut_mds.plot_beamcut_in_amplitude(self.destination_folder, ant=ant, ddi=ddi)
154-
assert are_binary_files_equal(
181+
assert are_png_files_equal(
155182
f"{self.destination_folder}/{amp_plot_name}",
156183
f"{self.ref_products_folder}/{amp_plot_name}",
157184
), "Amplitude plot hash is different from the expected hash"
158185

159186
beamcut_mds.plot_beamcut_in_attenuation(
160187
self.destination_folder, ant=ant, ddi=ddi
161188
)
162-
assert are_binary_files_equal(
189+
assert are_png_files_equal(
163190
f"{self.destination_folder}/{att_plot_name}",
164191
f"{self.ref_products_folder}/{att_plot_name}",
165192
), "Attenuation plot hash is different from the expected hash"
166193

167194
beamcut_mds.plot_beam_cuts_over_sky(self.destination_folder, ant=ant, ddi=ddi)
168-
assert are_binary_files_equal(
195+
assert are_png_files_equal(
169196
f"{self.destination_folder}/{lm_plot_name}",
170197
f"{self.ref_products_folder}/{lm_plot_name}",
171198
), "lm plot hash is different from the expected hash"

0 commit comments

Comments
 (0)