From fcc4f55c347b0dc23155ca14d6e88c5ef974ab31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Mon, 23 Nov 2026 12:32:17 +0100 Subject: [PATCH] fix: NaN brightness features for empty masks `compute_avg_sd_masked_uint8`, `compute_avg_sd_masked_int16`, and `compute_percentiles_10_90` failed with a `ZeroDivisionError` (or an `IndexError`) for an empty mask, so `brightness_features` could not be computed for the other masks either. Features that cannot be computed are NaN now, as for the other feature functions. Note that the pipeline never passes empty masks. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG | 2 ++ dcnum/feat/feat_brightness/bright_all.py | 15 ++++++++++++++- tests/test_feat_brightness.py | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 49a6908..88301c3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -59,6 +59,8 @@ - fix: running dcnum on its own output within the same second failed because of duplicate log names; a counter is appended to the names of the "dcnum-job" and "dcnum-log" logs if necessary + - fix: brightness features failed with a `ZeroDivisionError` for + empty masks; they are NaN now - docs: comment on intended behavior of `fill_holes` (mask post-processing), Haralick texture features, and the sparsemed background assignment after cleansing diff --git a/dcnum/feat/feat_brightness/bright_all.py b/dcnum/feat/feat_brightness/bright_all.py index 451b73f..88dfbcf 100644 --- a/dcnum/feat/feat_brightness/bright_all.py +++ b/dcnum/feat/feat_brightness/bright_all.py @@ -104,7 +104,7 @@ def compute_avg_sd_masked_uint8(image, mask): avg_sd: np.ndarray float array of shape (N, 2) with the mean (first column) and the standard deviation (second column) of the pixels - within each mask + within each mask (NaN for empty masks) """ size = mask.shape[0] avg_sd = np.zeros((size, 2), dtype=np.float64) @@ -114,6 +114,10 @@ def compute_avg_sd_masked_uint8(image, mask): image_idx = 0 else: image_idx = ii + if maski.size == 0: + # empty mask + avg_sd[ii, :] = np.nan + continue masked = image[image_idx].ravel()[maski] avg_sd[ii, 0] = np.mean(masked) avg_sd[ii, 1] = np.std(masked) @@ -135,6 +139,10 @@ def compute_avg_sd_masked_int16(image, mask): else: image_idx = ii maski = np.where(mask[ii].ravel())[0] + if maski.size == 0: + # empty mask + avg_sd[ii, :] = np.nan + continue masked = image[image_idx].ravel()[maski] avg_sd[ii, 0] = np.mean(masked) avg_sd[ii, 1] = np.std(masked) @@ -178,6 +186,7 @@ def compute_percentiles_10_90(image, mask): percentiles: np.ndarray float array of shape (N, 2) with the 10th (first column) and 90th (second column) percentile of the pixels within each mask + (NaN for empty masks) """ size = mask.shape[0] percentiles = np.zeros((size, 2), dtype=np.float64) @@ -187,6 +196,10 @@ def compute_percentiles_10_90(image, mask): image_idx = 0 else: image_idx = ii + if maski.size == 0: + # empty mask + percentiles[ii, :] = np.nan + continue masked = image[image_idx].ravel()[maski] peri = np.percentile(masked, q=(10, 90)) percentiles[ii, 0] = peri[0] diff --git a/tests/test_feat_brightness.py b/tests/test_feat_brightness.py index cb6fac1..a9024d0 100644 --- a/tests/test_feat_brightness.py +++ b/tests/test_feat_brightness.py @@ -48,3 +48,21 @@ def test_basic_brightness_single_image(): # control test assert not np.allclose(h5["events"]["bright_perc_10"][1], data["bright_perc_90"][0]) + + +def test_brightness_empty_mask(): + """Brightness features of an empty mask are NaN (dcnum 0.31.2 crashed)""" + image = np.full((1, 10, 10), 100, dtype=np.uint8) + image_bg = np.full((1, 10, 10), 110, dtype=np.uint8) + mask = np.zeros((2, 10, 10), dtype=bool) + mask[1, 2:6, 2:6] = True + data = feat_brightness.brightness_features(image=image, + mask=mask, + image_bg=image_bg) + for key in ["bright_avg", "bright_sd", "bright_bc_avg", "bright_bc_sd", + "bright_perc_10", "bright_perc_90"]: + assert np.isnan(data[key][0]), key + assert not np.isnan(data[key][1]), key + assert data["bright_avg"][1] == 100 + assert data["bright_bc_avg"][1] == -10 + assert np.all(data["bg_med"] == 110)