Skip to content

Commit

Permalink
Merge pull request #168 from snilsn/bugfix_trackpy
Browse files Browse the repository at this point in the history
Resetting parameters and adding exceptions for adaptive search
  • Loading branch information
freemansw1 authored Sep 9, 2022
2 parents cd70bd1 + 20c4035 commit 955e799
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
37 changes: 37 additions & 0 deletions tobac/tests/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
from pandas.testing import assert_frame_equal
import numpy as np
import trackpy as tp


def test_linking_trackpy():
Expand Down Expand Up @@ -49,6 +50,42 @@ def test_linking_trackpy():
)


@pytest.mark.parametrize(
"max_trackpy, max_tobac, adaptive_step, adaptive_stop",
[(5, 10, None, None), (5, 10, 0.9, 0.1)],
)
def test_keep_trackpy_parameters(max_trackpy, max_tobac, adaptive_step, adaptive_stop):
"""
Tests that tobac does not change the parameters of trackpy
"""

tp.linking.Linker.MAX_SUB_NET_SIZE = max_trackpy
tp.linking.Linker.MAX_SUB_NET_SIZE_ADAPTIVE = max_trackpy

expected_value = tp.linking.Linker.MAX_SUB_NET_SIZE
expected_value_adaptive = tp.linking.Linker.MAX_SUB_NET_SIZE_ADAPTIVE

data = tobac.testing.make_simple_sample_data_2D()
dxy, dt = tobac.utils.get_spacings(data)
features = tobac.feature_detection.feature_detection_multithreshold(
data, dxy, threshold=0.1
)

track = tobac.linking_trackpy(
features,
data,
dt=dt,
dxy=dxy,
v_max=100,
adaptive_step=adaptive_step,
adaptive_stop=adaptive_stop,
subnetwork_size=max_tobac,
)

assert expected_value == tp.linking.Linker.MAX_SUB_NET_SIZE
assert expected_value_adaptive == tp.linking.Linker.MAX_SUB_NET_SIZE_ADAPTIVE


def test_trackpy_predict():
"""Function to test if linking_trackpy() with method='predict' correctly links two
features at constant speeds crossing each other.
Expand Down
36 changes: 30 additions & 6 deletions tobac/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ def linking_trackpy(
Default is 'random'.
adaptive_step : float, optional
Reduce search range by multiplying it by this factor.
Reduce search range by multiplying it by this factor. Needs to be
used in combination with adaptive_stop. Default is None.
adaptive_stop : float, optional
If not None, when encountering an oversize subnet, retry by progressively
reducing search_range until the subnet is solvable. If search_range
becomes <= adaptive_stop, give up and raise a SubnetOversizeException.
Default is None
reducing search_range by multiplying with adaptive_step until the subnet
is solvable. If search_range becomes <= adaptive_stop, give up and raise
a SubnetOversizeException. Needs to be used in combination with
adaptive_step. Default is None.
cell_number_start : int, optional
Cell number for first tracked cell.
Expand Down Expand Up @@ -195,6 +197,19 @@ def linking_trackpy(
FutureWarning,
)

# in case of adaptive search, check wether both parameters are specified
if adaptive_stop is not None:
if adaptive_step is None:
raise ValueError(
"Adaptive search requires values for adaptive_step and adaptive_stop. Please specify adaptive_step."
)

if adaptive_step is not None:
if adaptive_stop is None:
raise ValueError(
"Adaptive search requires values for adaptive_step and adaptive_stop. Please specify adaptive_stop."
)

if time_cell_min:
stubs = np.floor(time_cell_min / dt) + 1

Expand All @@ -204,10 +219,12 @@ def linking_trackpy(

# If subnetwork size given, set maximum subnet size
if subnetwork_size is not None:
# Choose the right parameter depending on the use of adaptive search
# Choose the right parameter depending on the use of adaptive search, save previously set values
if adaptive_step is None and adaptive_stop is None:
size_cache = tp.linking.Linker.MAX_SUB_NET_SIZE
tp.linking.Linker.MAX_SUB_NET_SIZE = subnetwork_size
else:
size_cache = tp.linking.Linker.MAX_SUB_NET_SIZE_ADAPTIVE
tp.linking.Linker.MAX_SUB_NET_SIZE_ADAPTIVE = subnetwork_size

# deep copy to preserve features field:
Expand Down Expand Up @@ -262,7 +279,14 @@ def linking_trackpy(
else:
raise ValueError("method_linking unknown")

# Filter trajectories to exclude short trajectories that are likely to be spurious
# Reset trackpy parameters to previously set values
if subnetwork_size is not None:
if adaptive_step is None and adaptive_stop is None:
tp.linking.Linker.MAX_SUB_NET_SIZE = size_cache
else:
tp.linking.Linker.MAX_SUB_NET_SIZE_ADAPTIVE = size_cache

# Filter trajectories to exclude short trajectories that are likely to be spurious
# trajectories_filtered = filter_stubs(trajectories_unfiltered,threshold=stubs)
# trajectories_filtered=trajectories_filtered.reset_index(drop=True)

Expand Down

0 comments on commit 955e799

Please sign in to comment.