|
| 1 | +import numpy as np |
| 2 | +from spikeinterface.core import Templates |
| 3 | +from spikeinterface.core.node_pipeline import PeakDetector |
| 4 | + |
| 5 | +_base_matching_dtype = [ |
| 6 | + ("sample_index", "int64"), |
| 7 | + ("channel_index", "int64"), |
| 8 | + ("cluster_index", "int64"), |
| 9 | + ("amplitude", "float64"), |
| 10 | + ("segment_index", "int64"), |
| 11 | +] |
| 12 | + |
| 13 | + |
| 14 | +class BaseTemplateMatching(PeakDetector): |
| 15 | + def __init__(self, recording, templates, return_output=True, parents=None): |
| 16 | + # TODO make a sharedmem of template here |
| 17 | + # TODO maybe check that channel_id are the same with recording |
| 18 | + |
| 19 | + assert isinstance( |
| 20 | + templates, Templates |
| 21 | + ), f"The templates supplied is of type {type(templates)} and must be a Templates" |
| 22 | + self.templates = templates |
| 23 | + PeakDetector.__init__(self, recording, return_output=return_output, parents=parents) |
| 24 | + |
| 25 | + def get_dtype(self): |
| 26 | + return np.dtype(_base_matching_dtype) |
| 27 | + |
| 28 | + def get_trace_margin(self): |
| 29 | + raise NotImplementedError |
| 30 | + |
| 31 | + def compute(self, traces, start_frame, end_frame, segment_index, max_margin): |
| 32 | + spikes = self.compute_matching(traces, start_frame, end_frame, segment_index) |
| 33 | + spikes["segment_index"] = segment_index |
| 34 | + |
| 35 | + margin = self.get_trace_margin() |
| 36 | + if margin > 0 and spikes.size > 0: |
| 37 | + keep = (spikes["sample_index"] >= margin) & (spikes["sample_index"] < (traces.shape[0] - margin)) |
| 38 | + spikes = spikes[keep] |
| 39 | + |
| 40 | + # node pipeline need to return a tuple |
| 41 | + return (spikes,) |
| 42 | + |
| 43 | + def compute_matching(self, traces, start_frame, end_frame, segment_index): |
| 44 | + raise NotImplementedError |
| 45 | + |
| 46 | + def get_extra_outputs(self): |
| 47 | + # can be overwritten if need to ouput some variables with a dict |
| 48 | + return None |
0 commit comments