Skip to content

Commit

Permalink
add warning when no spots were detected
Browse files Browse the repository at this point in the history
  • Loading branch information
Henley13 committed Dec 8, 2020
1 parent b9d1afc commit 5ee4e7a
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions bigfish/detection/spot_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Functions to detect spots in 2-d and 3-d.
"""

import warnings
import scipy.ndimage as ndi
import numpy as np

Expand Down Expand Up @@ -232,7 +233,8 @@ def _detect_spots_from_images(images, threshold=None, remove_duplicate=True,
thresholds, count_spots = _get_spot_counts(thresholds, all_value_spots)

# select threshold where the kink of the distribution is located
threshold, _, _ = _get_breaking_point(thresholds, count_spots)
if count_spots.size > 0:
threshold, _, _ = _get_breaking_point(thresholds, count_spots)

# detect spots
all_spots = []
Expand Down Expand Up @@ -326,8 +328,9 @@ def spots_thresholding(image, mask_local_max, threshold,
Image with shape (z, y, x) or (y, x).
mask_local_max : np.ndarray, bool
Mask with shape (z, y, x) or (y, x) indicating the local peaks.
threshold : float or int
A threshold to discriminate relevant spots from noisy blobs.
threshold : float, int or None
A threshold to discriminate relevant spots from noisy blobs. If None,
detection is aborted with a warning.
remove_duplicate : bool
Remove potential duplicate coordinates for the same spots. Slow the
running.
Expand All @@ -348,9 +351,17 @@ def spots_thresholding(image, mask_local_max, threshold,
stack.check_array(mask_local_max,
ndim=[2, 3],
dtype=[bool])
stack.check_parameter(threshold=(float, int),
stack.check_parameter(threshold=(float, int, type(None)),
remove_duplicate=bool)

if threshold is None:
mask = np.zeros_like(image, dtype=bool)
spots = np.array([], dtype=np.int64).reshape((0, image.ndim))
warnings.warn("No spots were detected (threshold is {0})."
.format(threshold),
UserWarning)
return spots, mask

# remove peak with a low intensity
mask = (mask_local_max & (image > threshold))
if mask.sum() == 0:
Expand All @@ -377,6 +388,12 @@ def spots_thresholding(image, mask_local_max, threshold,
spots = np.nonzero(mask)
spots = np.column_stack(spots)

# case where no spots were detected
if spots.size == 0:
warnings.warn("No spots were detected (threshold is {0})."
.format(threshold),
UserWarning)

return spots, mask


Expand Down Expand Up @@ -423,7 +440,12 @@ def automated_threshold_setting(image, mask_local_max):
thresholds, count_spots = _get_spot_counts(thresholds, value_spots)

# select threshold where the kink of the distribution is located
optimal_threshold, _, _ = _get_breaking_point(thresholds, count_spots)
if count_spots.size > 0:
optimal_threshold, _, _ = _get_breaking_point(thresholds, count_spots)

# case where no spots were detected
else:
optimal_threshold = None

return optimal_threshold

Expand Down Expand Up @@ -466,6 +488,8 @@ def _get_spot_counts(thresholds, value_spots):
Returns
-------
thresholds : np.ndarray, np.float64
Candidate threshold values.
count_spots : np.ndarray, np.float64
Spots count function.
Expand Down

0 comments on commit 5ee4e7a

Please sign in to comment.