Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion dcnum/feat/feat_brightness/bright_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_feat_brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading