Skip to content

Commit

Permalink
MNT: Work around new stats.mode behavior for scipy >= 1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
nkeim committed Nov 22, 2023
1 parent 05a6439 commit 2021f28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions trackpy/refine/brightfield_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions trackpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2021f28

Please sign in to comment.