From 387eff51b5961874e99458735efd36e0f0dc2b84 Mon Sep 17 00:00:00 2001 From: Jeremy Muhlich Date: Thu, 6 Jul 2023 00:36:48 -0400 Subject: [PATCH] Fix errors when stitching synthetic data Input tiles created via whole-pixel crops of a larger image would generate negative error metric values due to numerical precision issues in utils.nccw, ultimately causing a networkx error when building the spanning tree. This change clamps small negative error metric values up to zero and raises an exception for large negative values. --- ashlar/utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ashlar/utils.py b/ashlar/utils.py index a8ede396..62244387 100644 --- a/ashlar/utils.py +++ b/ashlar/utils.py @@ -70,7 +70,18 @@ def nccw(img1, img2, sigma): correlation = np.abs(np.sum(img1w * img2w)) total_amplitude = np.linalg.norm(img1w) * np.linalg.norm(img2w) if correlation > 0 and total_amplitude > 0: - error = -np.log(correlation / total_amplitude) + diff = correlation - total_amplitude + if diff <= 0: + error = -np.log(correlation / total_amplitude) + elif diff < 1e-5: + # This situation can occur due to numerical precision issues when + # img1 and img2 are very nearly or exactly identical. If the + # difference is small enough, let it slide. + error = 0 + else: + raise RuntimeError( + f"correlation > total_amplitude (diff={diff})" + ) else: error = np.inf return error