Skip to content

Commit

Permalink
Merge pull request #743 from nkeim/fix-compat-nov23
Browse files Browse the repository at this point in the history
  • Loading branch information
nkeim authored Nov 26, 2023
2 parents 1a01aa3 + 0aad9ac commit d4ff59f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 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
6 changes: 3 additions & 3 deletions trackpy/tests/test_legacy_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,9 @@ def test_drop_link(self):
# The resulting subnet causes the trajectory to be broken
# when link_strategy is 'drop' and search_range is large enough.
N = 2
f_1particle = DataFrame({'x': np.arange(N), 'y': np.ones(N), 'frame': np.arange(N)})
f = f_1particle.append(DataFrame(
{'x': [3], 'y': [1], 'frame': [1]}), ignore_index=True)
f = DataFrame({'x': list(np.arange(N)) + [3],
'y': np.ones(N+1),
'frame': list(np.arange(N)) + [1]})
f_expected_without_subnet = f.copy()
f_expected_without_subnet['particle'] = [0, 0, 1]
# The linker assigns new particle IDs in arbitrary order. So
Expand Down
13 changes: 8 additions & 5 deletions trackpy/tests/test_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ def test_drop_link(self):
# The resulting subnet causes the trajectory to be broken
# when link_strategy is 'drop' and search_range is large enough.
N = 2
f_1particle = DataFrame({'x': np.arange(N), 'y': np.ones(N), 'frame': np.arange(N)})
f = f_1particle.append(DataFrame(
{'x': [3], 'y': [1], 'frame': [1]}), ignore_index=True)
f = DataFrame({'x': list(np.arange(N)) + [3],
'y': np.ones(N+1),
'frame': list(np.arange(N)) + [1]})
f_expected_without_subnet = f.copy()
f_expected_without_subnet['particle'] = [0, 0, 1]
# The linker assigns new particle IDs in arbitrary order. So
Expand Down Expand Up @@ -753,7 +753,10 @@ def dist_func(a, b):


def test_custom_dist_metric(self):
import sklearn.neighbors
try:
from sklearn.metrics import DistanceMetric
except ImportError:
from sklearn.neighbors import DistanceMetric

# Several 2D random walkers
N = 5
Expand All @@ -771,7 +774,7 @@ def test_custom_dist_metric(self):

# leave x, y for the comparison at the end

dist_func = sklearn.neighbors.DistanceMetric.get_metric("euclidean")
dist_func = DistanceMetric.get_metric("euclidean")

# link using a custom distance function
actual = self.link(f, search_range, dist_func=dist_func)
Expand Down
4 changes: 3 additions & 1 deletion trackpy/tests/test_static.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pandas

from trackpy.static import *
import pandas as pd
import numpy as np
Expand Down Expand Up @@ -175,7 +177,7 @@ def _rings_3d(self):
'z': np.concatenate(refz)})

# The last index is the center particle, which is used to calculate g_r
df = df.append(pd.DataFrame({'x': [0.], 'y': [0.], 'z':[0.]}))
df = pandas.concat([df, pd.DataFrame({'x': [0.], 'y': [0.], 'z':[0.]})])
return df


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 d4ff59f

Please sign in to comment.