From 679b12958dcab408a097a42d172a0b3368105b31 Mon Sep 17 00:00:00 2001 From: JoepVanlier Date: Tue, 26 Sep 2023 17:35:51 +0200 Subject: [PATCH] diffusion: better error for "ols" of empty track --- changelog.md | 1 + lumicks/pylake/kymotracker/detail/msd_estimation.py | 6 ++++++ lumicks/pylake/kymotracker/tests/test_kymotrack.py | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/changelog.md b/changelog.md index a960146f1..1c2ee59fe 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,7 @@ #### Improvements * Kymographs that only consist of a single line no longer raise, but report a `line_time_seconds`. This also allows you to now call `Kymo.plot()` on these. +* Issue a more descriptive error when attempting to compute a diffusion constant of a track with no points. ## v1.2.1 | 2023-10-17 diff --git a/lumicks/pylake/kymotracker/detail/msd_estimation.py b/lumicks/pylake/kymotracker/detail/msd_estimation.py index cf7777ee4..370a63e8e 100644 --- a/lumicks/pylake/kymotracker/detail/msd_estimation.py +++ b/lumicks/pylake/kymotracker/detail/msd_estimation.py @@ -735,6 +735,12 @@ def determine_optimal_points(frame_idx, coordinate, max_iterations=100): num_slopes.add(num_slope) + if len(coordinate) <= 4: + raise RuntimeError( + "You need at least 5 time points to estimate the number of points to include in " + "the fit." + ) + # Determine the number of points to include in the next fit num_slope, num_intercept = optimal_points( calculate_localization_error(frame_lags[:num_slope], msd[:num_slope]), len(coordinate) diff --git a/lumicks/pylake/kymotracker/tests/test_kymotrack.py b/lumicks/pylake/kymotracker/tests/test_kymotrack.py index 94a0fad3a..e305f4ac4 100644 --- a/lumicks/pylake/kymotracker/tests/test_kymotrack.py +++ b/lumicks/pylake/kymotracker/tests/test_kymotrack.py @@ -1211,6 +1211,18 @@ def make_coordinates(length, divisor): d = tracks.estimate_diffusion("ols", min_length=2) +@pytest.mark.parametrize("data", [[], [1, 2, 3, 4]]) +def test_ols_empty_kymotrack(blank_kymo, data): + track = KymoTrack(data, data, blank_kymo, "red", 0) + + with pytest.raises( + RuntimeError, + match="You need at least 5 time points to estimate the number of points to include in the " + "fit.", + ): + track.estimate_diffusion("ols") + + @pytest.mark.parametrize("kbp_calibration, line_width", [(None, 7), (4, 7), (None, 8)]) def test_ensemble_msd_calibration_from_kymo(blank_kymo, kbp_calibration, line_width): """Checks whether all the properties are correctly forwarded from the Kymo"""