Skip to content

Commit

Permalink
find_beads: handle failure to estimate background
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Dec 25, 2024
1 parent cd12f63 commit bc888aa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#### Bug fixes

* Ensure that operators such as (e.g. `+`, `-`, `/`) work on [`Slice`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.channel.Slice.html) with all types that are convertible to scalars. Previously these failed with zero dimensional numpy arrays and other convertible objects.
* Fixed a bug where bead edge determination could fail with an unhandled exception when determining the background failed rather than issued the expected `RuntimeError`.

## v1.5.3 | 2024-10-29

Expand Down
6 changes: 5 additions & 1 deletion lumicks/pylake/detail/bead_cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ def _guess_background(data):
"""Determine background level by determining most prominent mode"""
import scipy.stats

kde_estimate = scipy.stats.gaussian_kde(data, 0.02)
try:
kde_estimate = scipy.stats.gaussian_kde(data, 0.02)
except np.linalg.LinAlgError:
return np.median(data) # Use the median count as fallback

interpolated_kde = np.arange(min(data), np.max(data))
return interpolated_kde[np.argmax(kde_estimate.pdf(interpolated_kde))]

Expand Down
5 changes: 4 additions & 1 deletion lumicks/pylake/detail/tests/test_bead_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def test_bead_cropping_allow_negative_beads(filename, ref_edges, ref_edges_no_ne

def test_bead_cropping_failure():
mock = np.zeros((100, 1))
mock[50] = 1 # Standard deviation of the data should not be zero or the KDE estimate fails.
with pytest.raises(RuntimeError, match="Did not find two beads"):
find_beads_brightness(mock, bead_diameter_pixels=1, plot=True)

mock[50] = 1
with pytest.raises(RuntimeError, match="Did not find two beads"):
find_beads_brightness(mock, bead_diameter_pixels=1, plot=True)

Expand Down

0 comments on commit bc888aa

Please sign in to comment.