From 2021f28eff355d5fc0c01ad2e66c8baeea80adb0 Mon Sep 17 00:00:00 2001 From: Nathan Keim Date: Wed, 22 Nov 2023 16:37:54 -0500 Subject: [PATCH] MNT: Work around new stats.mode behavior for scipy >= 1.11 --- trackpy/refine/brightfield_ring.py | 7 ++++--- trackpy/utils.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/trackpy/refine/brightfield_ring.py b/trackpy/refine/brightfield_ring.py index 9161e5d9..50d5f027 100644 --- a/trackpy/refine/brightfield_ring.py +++ b/trackpy/refine/brightfield_ring.py @@ -9,7 +9,8 @@ from scipy.ndimage import map_coordinates from scipy import stats -from ..utils import (validate_tuple, guess_pos_columns, default_pos_columns) +from ..utils import (validate_tuple, guess_pos_columns, default_pos_columns, + stats_mode_scalar) def refine_brightfield_ring(image, radius, coords_df, pos_columns=None, @@ -197,9 +198,9 @@ def _min_edge(arr, threshold=0.45, max_dev=1, axis=1, bright_left=True, if np.sum(mask) == 0: return r_dev - # filter by deviations from most occuring value + # filter by deviations from most occurring value max_dev = np.round(max_dev) - most_likely = stats.mode(r_dev[mask])[0][0] + most_likely = stats_mode_scalar(r_dev[mask]) mask[mask] &= np.abs(r_dev[mask]-most_likely) > max_dev r_dev[mask] = np.nan diff --git a/trackpy/utils.py b/trackpy/utils.py index f2be2161..c9415bd7 100644 --- a/trackpy/utils.py +++ b/trackpy/utils.py @@ -416,3 +416,18 @@ def get_pool(processes): map_func = map return pool, map_func + + +def stats_mode_scalar(*args, **kwargs): + """Returns a scalar from scipy.stats.mode(). + + Works around new default in scipy 1.11 to eliminate extra axes + that were previously removed by trackpy. + + See https://docs.scipy.org/doc/scipy-1.9.3/reference/generated/scipy.stats.mode.html + """ + result = stats.mode(*args, **kwargs)[0] + try: + return result[0] # Old behavior + except IndexError: + return result # scipy >= 1.11 \ No newline at end of file