From dc685bc693566d3e54145c2775d78130907b2f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Thu, 19 Nov 2026 10:19:33 +0100 Subject: [PATCH] fix: smooth contour spacing returned an unevaluated spacing after max_iter The spacing was halved at the end of every iteration, also after the last one. Now the spacing is halved at the beginning of the next iteration, so the returned spacing is always the last one evaluated. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 3 +++ dclab/kde/smooth_contour.py | 16 ++++++++++------ tests/test_kde_smooth_contour.py | 6 ++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 44c1d7d0..c3329e25 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -269,6 +269,9 @@ - fix: deprecated `export.hdf5_autocomplete_config` raised an AttributeError when given a path (`.close()` was called on the path instead of on the file opened by `RTDCWriter`) (#423) + - fix: `find_smooth_contour_spacing` returned half of the last + evaluated contour spacing when the maximum number of iterations + was reached (#424) 0.74.0 - fix: CLI commands compress, condense, join, and repack deleted the input file if it was also given as the output path diff --git a/dclab/kde/smooth_contour.py b/dclab/kde/smooth_contour.py index de797e56..60875895 100644 --- a/dclab/kde/smooth_contour.py +++ b/dclab/kde/smooth_contour.py @@ -100,13 +100,20 @@ def find_smooth_contour_spacing( } corners_found = False - for _ in range(max_iter): # hard-limit is 15 iterations - corners_found = False - + for ii in range(max_iter): # hard-limit is 15 iterations if abort_event is not None and abort_event.is_set(): results["reason"] = "abort event" break + if ii > 0: + # The contours computed with the previous spacing were not + # smooth enough. Halve the spacing here (and not at the end + # of the previous iteration), so that the spacing returned + # is always the last one that was evaluated. + xacc /= 2 + yacc /= 2 + + corners_found = False results["total iterations"] += 1 # maximum difference of opening angle from 180° [rad] @@ -185,9 +192,6 @@ def find_smooth_contour_spacing( results["reason"] = "maximum contour length reached" results["success"] = True break - else: - xacc /= 2 - yacc /= 2 results["corners found"] = corners_found results["spacing x"] = xacc diff --git a/tests/test_kde_smooth_contour.py b/tests/test_kde_smooth_contour.py index 95c80a36..2ad83ccc 100644 --- a/tests/test_kde_smooth_contour.py +++ b/tests/test_kde_smooth_contour.py @@ -222,9 +222,11 @@ def test_find_smooth_contour_spacing_max_iter(): assert not res["success"] assert not res["corners found"] assert res["reason"] == "maximum iterations reached" - assert np.allclose(res["spacing x"], 0.46201586550424045, + # The returned spacing is the one that was evaluated in the last + # iteration (here: the initial estimate, not half of it). + assert np.allclose(res["spacing x"], 2 * 0.46201586550424045, rtol=0, atol=1e-12) - assert np.allclose(res["spacing y"], 0.00024627719927894213, + assert np.allclose(res["spacing y"], 2 * 0.00024627719927894213, rtol=0, atol=1e-12)