nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: SpikeInterface/spikeinterface
Pull request #258 (2021-09-10 12:02:56)

adding segment option for get templates
getting waveform template metrics for each segment of recording/sorting 


Patch:
diff --git a/spikeinterface/core/waveform_extractor.py b/spikeinterface/core/waveform_extractor.py
index 4b75a05f3..73dbd86af 100644
--- a/spikeinterface/core/waveform_extractor.py
+++ b/spikeinterface/core/waveform_extractor.py
@@ -223,6 +223,11 @@ class WaveformExtractor:
         else:
             return wfs
 
+    def get_waveforms_segment(self, segment_index, unit_id):
+        wfs, index_ar = self.get_waveforms(unit_id, with_index=True)
+        segment_index_ar = np.array([i[1] for i in index_ar])
+        return wfs[segment_index_ar == segment_index, :, :]
+
     def get_template(self, unit_id, mode='median', quantile_value=0.5):
         """
         Return template (average waveform)
@@ -307,6 +312,20 @@ class WaveformExtractor:
             templates[i, :, :] = self.get_template(unit_id, mode=mode, quantile_value=quantile_value)
         return templates
 
+    def get_template_segment(self, unit_id, segment_index, quantile_value=None, mode='median'):
+        assert mode in ('median', 'average', 'std', 'quantile')
+        assert unit_id in self.sorting.unit_ids
+        waveforms_segment = self.get_waveforms_segment(segment_index, unit_id)
+        if mode == 'median':
+            return np.median(waveforms_segment, axis=0)
+        elif mode == 'average':
+            return np.mean(waveforms_segment, axis=0)
+        elif mode == 'std':
+            return np.std(waveforms_segment, axis=0)
+        elif mode == 'quantile':
+            assert quantile_value is not None, 'enter quantile value'
+            return np.quantile(waveforms_segment, quantile_value, axis=0)
+
     def sample_spikes(self):
         p = self._params
         nbefore = self.nbefore


Test patch:
diff --git a/spikeinterface/core/tests/test_waveform_extractor.py b/spikeinterface/core/tests/test_waveform_extractor.py
index bd9b8b05f..104920d6b 100644
--- a/spikeinterface/core/tests/test_waveform_extractor.py
+++ b/spikeinterface/core/tests/test_waveform_extractor.py
@@ -69,11 +69,16 @@ def test_WaveformExtractor():
     wfs_std = we.get_all_templates(mode='std')
     assert wfs_std.shape == (5, 210, 2)
 
-    wf_qnt = we.get_template(0, mode='quantile', quantile_value = 0.9)
+    wf_qnt = we.get_template(0, mode='quantile', quantile_value=0.9)
     assert wf_qnt.shape == (210, 2)
     wf_qnt = we.get_all_templates(mode='quantile')
     assert wf_qnt.shape == (5, 210, 2)
 
+    wf_segment = we.get_template_segment(unit_id=0,segment_index=0)
+    assert wf_segment.shape == (210, 2)
+    wf_segment = we.get_template_segment(unit_id=0, segment_index=1, quantile_value=0.9, mode='quantile')
+    assert wf_segment.shape == (210, 2)
+
 def test_extract_waveforms():
     # 2 segments
Repository: SpikeInterface/spikeinterface
Pull request #1658 (2023-05-17 10:45:35)

Allow `new_unit_id` to be same id in `CurationSorting.merge`
Right now it's not possible to do `CurationSorting.merge([1, 2], new_unit_id=1)`, and it should be possible


Patch:
diff --git a/src/spikeinterface/curation/curationsorting.py b/src/spikeinterface/curation/curationsorting.py
index 37f27ac03..0c53e83b1 100644
--- a/src/spikeinterface/curation/curationsorting.py
+++ b/src/spikeinterface/curation/curationsorting.py
@@ -80,7 +80,7 @@ class CurationSorting:
         current_sorting = self._sorting_stages[self._sorting_stages_i]
         if new_unit_id is None:
             new_unit_id = self._get_unused_id()[0]
-        else:
+        elif new_unit_id not in units_to_merge:
             assert new_unit_id not in current_sorting.unit_ids, f"new_unit_id already exists!"
         new_sorting = MergeUnitsSorting(
             parent_sorting=current_sorting,


Test patch:
diff --git a/src/spikeinterface/curation/tests/test_curationsorting.py b/src/spikeinterface/curation/tests/test_curationsorting.py
index 6a1b76a0e..ae625d7e5 100644
--- a/src/spikeinterface/curation/tests/test_curationsorting.py
+++ b/src/spikeinterface/curation/tests/test_curationsorting.py
@@ -59,13 +59,16 @@ def test_curation():
     cs = CurationSorting(parent_sort, properties_policy="remove")
     # %%
     cs.merge(["a", "c"])
-    split_index = [v["b"] < 6 for v in spikestimes]  # spit class 4 in even and odds
+    assert cs.sorting.get_num_units() == len(spikestimes[0]) - 1
+    split_index = [v["b"] < 6 for v in spikestimes]  # split class 4 in even and odds
     cs.split("b", split_index)
     after_split = cs.sorting
+    assert cs.sorting.get_num_units() == len(spikestimes[0])
 
     all_units = cs.sorting.get_unit_ids()
-    cs.merge(all_units)
+    cs.merge(all_units, new_unit_id=all_units[0])
     assert len(cs.sorting.get_unit_ids()) == 1, "error merging units"
+    assert cs.sorting.unit_ids[0] == all_units[0]
     cs.undo()
 
     assert cs.sorting is after_split
Repository: SpikeInterface/spikeinterface
Pull request #1738 (2023-06-22 15:46:24)

Fix crash in `CurationSorting` with empty sorting


Patch:
diff --git a/src/spikeinterface/curation/curationsorting.py b/src/spikeinterface/curation/curationsorting.py
index 0c53e83b1..f2776bafe 100644
--- a/src/spikeinterface/curation/curationsorting.py
+++ b/src/spikeinterface/curation/curationsorting.py
@@ -44,7 +44,7 @@ class CurationSorting:
         if np.issubdtype(parent_units.dtype, np.character):
             self.max_used_id = max([-1] + [int(p) for p in parent_units if p.isdigit()])
         else:
-            self.max_used_id = max(parent_units)
+            self.max_used_id = max(parent_units) if len(parent_units) > 0 else 0
 
         self._kwargs = dict(parent_sorting=parent_sorting, make_graph=make_graph, properties_policy=properties_policy)
 


Test patch:
diff --git a/src/spikeinterface/curation/tests/test_curationsorting.py b/src/spikeinterface/curation/tests/test_curationsorting.py
index ae625d7e5..fd0b20662 100644
--- a/src/spikeinterface/curation/tests/test_curationsorting.py
+++ b/src/spikeinterface/curation/tests/test_curationsorting.py
@@ -80,6 +80,9 @@ def test_curation():
             == parent_sort.get_unit_spike_train("c", segment_index=i)
         )
 
+    # Test with empty sorting
+    empty_sorting = CurationSorting(NumpySorting.from_dict({}, parent_sort.sampling_frequency))
+
 
 if __name__ == "__main__":
     test_split_merge()