Skip to content

Commit b9cf1d8

Browse files
committed
Add split_by method to BaseSorting
1 parent 1eb05c7 commit b9cf1d8

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

src/spikeinterface/core/baserecordingsnippets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def split_by(self, property="group", outputs="dict"):
514514
recordings = {}
515515
for value in np.unique(values).tolist():
516516
(inds,) = np.nonzero(values == value)
517-
new_channel_ids = self.get_channel_ids()[inds]
517+
new_channel_ids = self.channel_ids[inds]
518518
subrec = self.select_channels(new_channel_ids)
519519
subrec.set_annotation("split_by_property", value=property)
520520
if outputs == "list":

src/spikeinterface/core/basesorting.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,47 @@ def time_slice(self, start_time: float | None, end_time: float | None) -> BaseSo
626626

627627
return self.frame_slice(start_frame=start_frame, end_frame=end_frame)
628628

629+
def split_by(self, property="group", outputs="dict"):
630+
"""
631+
Splits object based on a certain property (e.g. "group")
632+
633+
Parameters
634+
----------
635+
property : str, default: "group"
636+
The property to use to split the object, default: "group"
637+
outputs : "dict" | "list", default: "dict"
638+
Whether to return a dict or a list
639+
640+
Returns
641+
-------
642+
dict or list
643+
A dict or list with grouped objects based on property
644+
645+
Raises
646+
------
647+
ValueError
648+
Raised when property is not present
649+
"""
650+
assert outputs in ("list", "dict")
651+
values = self.get_property(property)
652+
if values is None:
653+
raise ValueError(f"property {property} is not set")
654+
655+
if outputs == "list":
656+
sortings = []
657+
elif outputs == "dict":
658+
sortings = {}
659+
for value in np.unique(values).tolist():
660+
(inds,) = np.nonzero(values == value)
661+
new_channel_ids = self.unit_ids[inds]
662+
subsort = self.select_units(new_channel_ids)
663+
subsort.set_annotation("split_by_property", value=property)
664+
if outputs == "list":
665+
sortings.append(subsort)
666+
elif outputs == "dict":
667+
sortings[value] = subsort
668+
return sortings
669+
629670
def time_to_sample_index(self, time, segment_index=0):
630671
"""
631672
Transform time in seconds into sample index

src/spikeinterface/core/tests/test_sortinganalyzer.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,9 @@ def test_create_by_dict():
215215
sort.set_property(key="group", values=[2, 2, 2, 1, 2, 2, 2, 1, 2, 1])
216216

217217
unit_ids = sort.unit_ids
218-
219-
split_sort = {key: sort.select_units(unit_ids=unit_ids[sort.get_property("group") == key]) for key in [1, 2]}
220-
221-
analyzer = create_sorting_analyzer(split_sort, rec.split_by("group"))
218+
split_sort = sort.split_by("group")
219+
split_rec = rec.split_by("group")
220+
analyzer = create_sorting_analyzer(split_sort, split_rec)
222221
analyzer_unit_ids = analyzer.unit_ids
223222

224223
assert set(analyzer.unit_ids) == set(sort.unit_ids)

0 commit comments

Comments
 (0)