Skip to content

Commit d9ad31f

Browse files
authored
Merge pull request #3488 from samuelgarcia/kilosort_matching_gpl
kilosort-matching in si
2 parents 12a1276 + 8e6efd7 commit d9ad31f

4 files changed

Lines changed: 44 additions & 5 deletions

File tree

src/spikeinterface/sortingcomponents/matching/method_list.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@
1212
"circus-omp-svd": CircusOMPSVDPeeler,
1313
"wobble": WobbleMatch,
1414
}
15+
16+
try:
17+
# Kilosort licence (GPL 3) is forcing us to make and use an external package
18+
from spikeinterface_kilosort_components import KiloSortMatching
19+
20+
matching_methods["kilosort-matching"] = KiloSortMatching
21+
except ImportError:
22+
pass

src/spikeinterface/sortingcomponents/motion/motion_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,5 @@ def ensure_time_bins(time_bin_centers_s=None, time_bin_edges_s=None):
632632
return time_bin_centers_s, time_bin_edges_s
633633

634634

635-
636635
def ensure_time_bin_edges(time_bin_centers_s=None, time_bin_edges_s=None):
637636
return ensure_time_bins(time_bin_centers_s, time_bin_edges_s)[1]

src/spikeinterface/sortingcomponents/motion/tests/test_motion_interpolation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from spikeinterface.sortingcomponents.tests.common import make_dataset
1313
from spikeinterface.core import generate_ground_truth_recording
1414

15+
1516
def make_fake_motion(rec):
1617
# make a fake motion object
1718

@@ -25,7 +26,7 @@ def make_fake_motion(rec):
2526
seg_time_bins = np.arange(0.5, duration - 0.49, 0.5)
2627
seg_disp = np.zeros((seg_time_bins.size, spatial_bins.size))
2728
seg_disp[:, :] = np.linspace(-30, 30, seg_time_bins.size)[:, None]
28-
29+
2930
temporal_bins.append(seg_time_bins)
3031
displacement.append(seg_disp)
3132

@@ -204,7 +205,6 @@ def test_InterpolateMotionRecording():
204205
seed=2205,
205206
)
206207

207-
208208
motion = make_fake_motion(rec)
209209

210210
rec2 = InterpolateMotionRecording(rec, motion, border_mode="force_extrapolate")

src/spikeinterface/sortingcomponents/tests/test_template_matching.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,39 @@ def test_find_spikes_from_templates(method, sorting_analyzer):
4545
"templates": templates,
4646
}
4747
method_kwargs = {}
48-
if method in ("naive", "tdc-peeler", "circus", "tdc-peeler2"):
48+
if method in (
49+
"naive",
50+
"tdc-peeler",
51+
"circus",
52+
):
4953
method_kwargs["noise_levels"] = noise_levels
5054

55+
if method == "kilosort-matching":
56+
from spikeinterface.sortingcomponents.peak_selection import select_peaks
57+
from spikeinterface.sortingcomponents.tools import extract_waveform_at_max_channel
58+
from spikeinterface.sortingcomponents.peak_detection import detect_peaks
59+
60+
peaks = detect_peaks(sorting_analyzer.recording, method="locally_exclusive", skip_after_n_peaks=5000)
61+
few_wfs = extract_waveform_at_max_channel(sorting_analyzer.recording, peaks, ms_before=1, ms_after=2)
62+
63+
wfs = few_wfs[:, :, 0]
64+
import numpy as np
65+
66+
n_components = 5
67+
from sklearn.cluster import KMeans
68+
69+
wfs /= np.linalg.norm(wfs, axis=1)[:, None]
70+
model = KMeans(n_clusters=n_components, n_init=10).fit(wfs)
71+
temporal_components = model.cluster_centers_
72+
temporal_components = temporal_components / np.linalg.norm(temporal_components[:, None])
73+
temporal_components = temporal_components.astype(np.float32)
74+
from sklearn.decomposition import TruncatedSVD
75+
76+
model = TruncatedSVD(n_components=n_components).fit(wfs)
77+
spatial_components = model.components_.astype(np.float32)
78+
method_kwargs["spatial_components"] = spatial_components
79+
method_kwargs["temporal_components"] = temporal_components
80+
5181
# method_kwargs["wobble"] = {
5282
# "templates": waveform_extractor.get_all_templates(),
5383
# "nbefore": waveform_extractor.nbefore,
@@ -91,5 +121,7 @@ def test_find_spikes_from_templates(method, sorting_analyzer):
91121
# method = "tdc-peeler"
92122
# method = "circus"
93123
# method = "circus-omp-svd"
94-
method = "wobble"
124+
# method = "wobble"
125+
method = "kilosort-matching"
126+
95127
test_find_spikes_from_templates(method, sorting_analyzer)

0 commit comments

Comments
 (0)