Skip to content

Commit e7e5824

Browse files
committed
python: Fixed comparing/updating charts with different sizes.
1 parent 68fef8f commit e7e5824

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

python/inet/test/chart.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ def sewar_rmse(a, b):
3737
b = b.astype(numpy.float64)
3838
return numpy.sqrt(numpy.mean((a.astype(numpy.float64)-b.astype(numpy.float64))**2))
3939

40+
def pad_to_size_centered(img, height, width):
41+
"""Pad image to (height, width) centered."""
42+
h, w = img.shape[:2]
43+
44+
dh = height - h
45+
dw = width - w
46+
if dh < 0 or dw < 0:
47+
raise ValueError("Target size must be >= image size")
48+
49+
top = dh // 2
50+
bottom = dh - top
51+
left = dw // 2
52+
right = dw - left
53+
54+
pad_width = (
55+
(top, bottom),
56+
(left, right),
57+
(0, 0),
58+
)
59+
60+
return np.pad(img, pad_width, mode="constant", constant_values=0)
61+
4062
class ChartTestTask(TestTask):
4163
def __init__(self, analysis_file_name, id, chart_name, simulation_project=None, name="chart test", **kwargs):
4264
super().__init__(name=name, **kwargs)
@@ -69,6 +91,10 @@ def run_protected(self, keep_charts=True, output_stream=sys.stdout, **kwargs):
6991
if os.path.exists(old_file_name):
7092
new_image = matplotlib.image.imread(new_file_name)
7193
old_image = matplotlib.image.imread(old_file_name)
94+
target_h = max(old_image.shape[0], new_image.shape[0])
95+
target_w = max(old_image.shape[1], new_image.shape[1])
96+
old_image = pad_to_size_centered(old_image, target_h, target_w)
97+
new_image = pad_to_size_centered(new_image, target_h, target_w)
7298
if old_image.shape != new_image.shape:
7399
return self.task_result_class(self, result="FAIL", reason="Supplied images have different sizes" + str(old_image.shape) + " and " + str(new_image.shape))
74100
metric = sewar_rmse(old_image, new_image)
@@ -178,6 +204,10 @@ def run_protected(self, keep_charts=True, **kwargs):
178204
if os.path.exists(old_file_name):
179205
new_image = matplotlib.image.imread(new_file_name)
180206
old_image = matplotlib.image.imread(old_file_name)
207+
target_h = max(old_image.shape[0], new_image.shape[0])
208+
target_w = max(old_image.shape[1], new_image.shape[1])
209+
old_image = pad_to_size_centered(old_image, target_h, target_w)
210+
new_image = pad_to_size_centered(new_image, target_h, target_w)
181211
if old_image.shape != new_image.shape:
182212
metric = 1
183213
else:

0 commit comments

Comments
 (0)