Skip to content

Commit 2e56462

Browse files
authored
Add interpolation_kwargs to interpolate_templates (#4665)
1 parent 7889b52 commit 2e56462

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/spikeinterface/generation/drift_tools.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def interpolate_templates(
1313
source_locations: np.ndarray,
1414
dest_locations: np.ndarray,
1515
interpolation_method: Literal["cubic", "linear", "nearest", "thin_plate"] = "cubic",
16+
interpolation_kwargs: None | dict = None,
1617
):
1718
"""
1819
Interpolate templates_array to new positions.
@@ -33,6 +34,9 @@ def interpolate_templates(
3334
shape = (num_channels, 2) or (num_motions, num_channels, 2)
3435
interpolation_method : str, default "cubic"
3536
The interpolation method.
37+
interpolation_kwargs
38+
Kwargs that are passed to RBFInterpolator (if interpolation_method = "thin_plate") or to
39+
griddata (otherwise).
3640
3741
Returns
3842
-------
@@ -41,6 +45,9 @@ def interpolate_templates(
4145
"""
4246
from scipy.interpolate import griddata, RBFInterpolator
4347

48+
if interpolation_kwargs is None:
49+
interpolation_kwargs = dict()
50+
4451
source_locations = np.asarray(source_locations)
4552
dest_locations = np.asarray(dest_locations)
4653

@@ -65,7 +72,15 @@ def interpolate_templates(
6572

6673
if interpolation_method == "thin_plate":
6774

68-
tps_interpolator = RBFInterpolator(source_locations, template, kernel="thin_plate_spline", neighbors=12)
75+
if "neighbors" not in interpolation_kwargs:
76+
# If neighbors it not passed, `RBFInterpolator` uses all channels.
77+
# This works poorly for standard ephys template interpolation.
78+
# Depending on the probe, you might want to decrease this value.
79+
interpolation_kwargs["neighbors"] = 12
80+
81+
tps_interpolator = RBFInterpolator(
82+
source_locations, template, kernel="thin_plate_spline", **interpolation_kwargs
83+
)
6984
if dest_locations_dims == 2:
7085
interp_template = tps_interpolator(dest_locations)
7186
elif dest_locations_dims == 3:
@@ -75,7 +90,12 @@ def interpolate_templates(
7590

7691
else:
7792
interp_template = griddata(
78-
source_locations, template, dest_locations, method=interpolation_method, fill_value=0
93+
source_locations,
94+
template,
95+
dest_locations,
96+
method=interpolation_method,
97+
fill_value=0,
98+
**interpolation_kwargs,
7999
)
80100

81101
if dest_locations_dims == 2:

src/spikeinterface/generation/tests/test_drift_tools.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ def test_interpolate_templates():
8383
templates.templates_array, source_locations, dest_locations, interpolation_method=interpolation_method
8484
)
8585

86+
interpolate_templates(
87+
templates.templates_array,
88+
source_locations,
89+
dest_locations,
90+
interpolation_method="thin_plate",
91+
interpolation_kwargs={"neighbors": 12},
92+
)
93+
8694

8795
def test_move_dense_templates():
8896
templates = make_some_templates()

0 commit comments

Comments
 (0)