From 9d1795606a0237ccc4b209a455bcd80dd6235605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Thu, 19 Nov 2026 10:00:56 +0100 Subject: [PATCH] fix: smooth contour spacing reported success when aborted mid-iteration - pass a flat list of quantiles to `get_contour_lines` (the nested list only worked by chance, the contour levels were nested as well) - check the abort event after iterating over the datasets Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 4 ++++ dclab/kde/smooth_contour.py | 21 ++++++++++++-------- tests/test_kde_smooth_contour.py | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ccb46c71..78dc34bc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -252,6 +252,10 @@ - fix: `Isoelastics.get` and `Isoelastics.get_with_rtdcbase` returned zero-valued emodulus for `viscosity=0` or `viscosity=False`; a ValueError is now raised for viscosities that are not positive (#418) + - fix: `find_smooth_contour_spacing` reported success ("target opening + angle reached") when the abort event was set while iterating over + the datasets; also pass a flat list of quantiles to + `get_contour_lines` (nested list only worked by chance) (#419) 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 db5fac28..de797e56 100644 --- a/dclab/kde/smooth_contour.py +++ b/dclab/kde/smooth_contour.py @@ -119,7 +119,7 @@ def find_smooth_contour_spacing( break # Compute the contour for the highest percentile of the plot. contours = kdi.get_contour_lines( - quantiles=[[np.max(quantiles)]], + quantiles=[np.max(quantiles)], xax=xax, yax=yax, xacc=xacc, @@ -162,13 +162,18 @@ def find_smooth_contour_spacing( max_dphi = max(max_dphi, dphi) max_length = min(max_length, len(cc)) - else: - results["reason"] = "maximum iterations reached" - results["success"] = False - if not num_contours: - # There is no contour we could smooth. - results["reason"] = "no contour found" - break + + if abort_event is not None and abort_event.is_set(): + # Aborted while iterating over the datasets; the contours + # of the remaining datasets were not checked. + results["reason"] = "abort event" + break + elif not num_contours: + # There is no contour we could smooth. + results["reason"] = "no contour found" + break + + results["reason"] = "maximum iterations reached" if max_dphi < np.deg2rad(23): # A contour is considered smooth when the maximum angle between diff --git a/tests/test_kde_smooth_contour.py b/tests/test_kde_smooth_contour.py index cb1008da..95c80a36 100644 --- a/tests/test_kde_smooth_contour.py +++ b/tests/test_kde_smooth_contour.py @@ -313,6 +313,40 @@ def test_find_smooth_contour_abort(): rtol=0, atol=1e-12) +def test_find_smooth_contour_abort_during_iteration(): + """Aborting while iterating over datasets must not report success""" + np.random.seed(47) + x0 = np.random.normal(loc=100, scale=10, size=100) + y0 = np.random.normal(loc=.1, scale=.01, size=100) + + ds = dclab.new_dataset({"area_um": x0, "deform": y0}) + + class LateEvent(threading.Event): + """Event that is set at the second call of `is_set`""" + def __init__(self): + super().__init__() + self.calls = 0 + + def is_set(self): + self.calls += 1 + return self.calls >= 2 + + res = smooth_contour.find_smooth_contour_spacing( + ds_list=[ds], + xax="area_um", + yax="deform", + xrange=(50, 150), + yrange=(0.09, 0.11), + quantiles=[0.90], + abort_event=LateEvent(), + ) + + # The abort event was set before the first contour was computed. + assert res["total iterations"] == 1 + assert not res["success"] + assert res["reason"] == "abort event" + + @pytest.mark.parametrize("angles", [ # NaN values and an opening angle of ~86 degrees [np.nan, np.nan, 1.5],