Skip to content

Commit b64669a

Browse files
committed
Retrieve correct peak slice for multiple retrievers
1 parent 6cc73f5 commit b64669a

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

src/spikeinterface/core/node_pipeline.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def __init__(
191191
self._dtype = spike_peak_dtype
192192

193193
self.include_spikes_in_margin = include_spikes_in_margin
194-
if include_spikes_in_margin is not None:
194+
if include_spikes_in_margin:
195195
self._dtype = spike_peak_dtype + [("in_margin", "bool")]
196196

197197
self.peaks = sorting_to_peaks(sorting, extremum_channel_inds, self._dtype)
@@ -228,12 +228,6 @@ def compute(self, traces, start_frame, end_frame, segment_index, max_margin, pea
228228
# get local peaks
229229
sl = self.segment_slices[segment_index]
230230
peaks_in_segment = self.peaks[sl]
231-
# if self.include_spikes_in_margin:
232-
# i0, i1 = np.searchsorted(
233-
# peaks_in_segment["sample_index"], [start_frame - max_margin, end_frame + max_margin]
234-
# )
235-
# else:
236-
# i0, i1 = np.searchsorted(peaks_in_segment["sample_index"], [start_frame, end_frame])
237231
i0, i1 = peak_slice
238232

239233
local_peaks = peaks_in_segment[i0:i1]
@@ -452,6 +446,21 @@ def find_parent_of_type(list_of_parents, parent_type, unique=True):
452446
return None
453447

454448

449+
def find_parents_of_type(list_of_parents, parent_type):
450+
if list_of_parents is None:
451+
return None
452+
453+
parents = []
454+
for parent in list_of_parents:
455+
if isinstance(parent, parent_type):
456+
parents.append(parent)
457+
458+
if len(parents) > 0:
459+
return parents
460+
else:
461+
return None
462+
463+
455464
def check_graph(nodes):
456465
"""
457466
Check that node list is orderd in a good (parents are before children)
@@ -471,7 +480,7 @@ def check_graph(nodes):
471480
assert parent in nodes, f"Node {node} has parent {parent} that was not passed in nodes"
472481
assert (
473482
nodes.index(parent) < i
474-
), f"Node are ordered incorrectly: {node} before {parent} in the pipeline definition."
483+
), f"Node are ordered incorrectly: {node} before {parent} in the pipeline definition."
475484

476485
return nodes
477486

@@ -607,12 +616,17 @@ def _compute_peak_pipeline_chunk(segment_index, start_frame, end_frame, worker_c
607616
skip_after_n_peaks_per_worker = worker_ctx["skip_after_n_peaks_per_worker"]
608617

609618
recording_segment = recording._recording_segments[segment_index]
610-
node0 = nodes[0]
611-
612-
if isinstance(node0, (SpikeRetriever, PeakRetriever)):
613-
# in this case PeakSource could have no peaks and so no need to load traces just skip
614-
peak_slice = i0, i1 = node0.get_peak_slice(segment_index, start_frame, end_frame, max_margin)
615-
load_trace_and_compute = i0 < i1
619+
retrievers = find_parents_of_type(nodes, (SpikeRetriever, PeakRetriever))
620+
# get peak slices once for all retrievers
621+
retriever_node = None
622+
peak_slice_by_retriever = {}
623+
for retriever in retrievers:
624+
peak_slice = i0, i1 = retriever_node.get_peak_slice(segment_index, start_frame, end_frame, max_margin)
625+
peak_slice_by_retriever[retriever] = peak_slice
626+
627+
if len(peak_slice_by_retriever) > 0:
628+
# in this case the retrievers could have no peaks, so we test if any spikes are in the chunk
629+
load_trace_and_compute = any(i0 < i1 for i0, i1 in peak_slice_by_retriever.values())
616630
else:
617631
# PeakDetector always need traces
618632
load_trace_and_compute = True
@@ -627,7 +641,7 @@ def _compute_peak_pipeline_chunk(segment_index, start_frame, end_frame, worker_c
627641
)
628642
# compute the graph
629643
pipeline_outputs = {}
630-
for node in nodes:
644+
for i, node in enumerate(nodes):
631645
node_parents = node.parents if node.parents else list()
632646
node_input_args = tuple()
633647
for parent in node_parents:
@@ -646,7 +660,8 @@ def _compute_peak_pipeline_chunk(segment_index, start_frame, end_frame, worker_c
646660
node_output = node.compute(trace_detection, start_frame, end_frame, segment_index, max_margin)
647661
# set sample index to local
648662
node_output[0]["sample_index"] += extra_margin
649-
elif isinstance(node, PeakSource):
663+
elif isinstance(node, (PeakRetriever, SpikeRetriever)):
664+
peak_slice = peak_slice_by_retriever[node]
650665
node_output = node.compute(traces_chunk, start_frame, end_frame, segment_index, max_margin, peak_slice)
651666
else:
652667
# TODO later when in master: change the signature of all nodes (or maybe not!)

0 commit comments

Comments
 (0)