|
4 | 4 | import io |
5 | 5 | import contextlib |
6 | 6 | import xarray |
7 | | -import hashlib |
8 | 7 |
|
| 8 | +from PIL import Image, ImageChops |
9 | 9 | from collections.abc import KeysView |
10 | 10 |
|
11 | 11 | from toolviper.utils import data |
12 | 12 |
|
13 | 13 | from astrohack import open_beamcut, AstrohackBeamcutFile |
14 | 14 |
|
15 | 15 |
|
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 |
22 | 49 |
|
23 | 50 |
|
24 | 51 | class TestBeamcut: |
@@ -151,21 +178,21 @@ def test_beamcut_plots(self): |
151 | 178 | beamcut_mds = open_beamcut(self.remote_beamcut_name) |
152 | 179 |
|
153 | 180 | 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( |
155 | 182 | f"{self.destination_folder}/{amp_plot_name}", |
156 | 183 | f"{self.ref_products_folder}/{amp_plot_name}", |
157 | 184 | ), "Amplitude plot hash is different from the expected hash" |
158 | 185 |
|
159 | 186 | beamcut_mds.plot_beamcut_in_attenuation( |
160 | 187 | self.destination_folder, ant=ant, ddi=ddi |
161 | 188 | ) |
162 | | - assert are_binary_files_equal( |
| 189 | + assert are_png_files_equal( |
163 | 190 | f"{self.destination_folder}/{att_plot_name}", |
164 | 191 | f"{self.ref_products_folder}/{att_plot_name}", |
165 | 192 | ), "Attenuation plot hash is different from the expected hash" |
166 | 193 |
|
167 | 194 | 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( |
169 | 196 | f"{self.destination_folder}/{lm_plot_name}", |
170 | 197 | f"{self.ref_products_folder}/{lm_plot_name}", |
171 | 198 | ), "lm plot hash is different from the expected hash" |
|
0 commit comments