diff --git a/doc/api.rst b/doc/api.rst index 8eef7a80bc..806bd38d6b 100755 --- a/doc/api.rst +++ b/doc/api.rst @@ -404,9 +404,6 @@ Deprecated :noindex: .. autofunction:: apply_sortingview_curation - .. autoclass:: CurationSorting - .. autoclass:: MergeUnitsSorting - .. autoclass:: SplitUnitSorting spikeinterface.generation diff --git a/doc/modules/curation.rst b/doc/modules/curation.rst index 0ce4edcc94..17583d26a8 100644 --- a/doc/modules/curation.rst +++ b/doc/modules/curation.rst @@ -487,41 +487,3 @@ We have other tools for cleaning spike sorting outputs: * :py:func:`~spikeinterface.curation.find_duplicated_spikes` : find duplicated spikes in the spike trains * | :py:func:`~spikeinterface.curation.remove_excess_spikes` : remove spikes whose times are greater than the | recording's number of samples (by segment) - - -The `CurationSorting` class (deprecated) ----------------------------------------- - -SpikeInterface offers machinery to manually curate a sorting output and keep track of the curation history. -The curation has several "steps" that can be repeated and chained: - - * remove/select units - * split units - * merge units - -This functionality is done with :py:class:`~spikeinterface.curation.CurationSorting` class. -Internally, this class keeps the history of curation as a graph. -The merging and splitting operations are handled by the :py:class:`~spikeinterface.curation.MergeUnitsSorting` and -:py:class:`~spikeinterface.curation.SplitUnitSorting`. These two classes can also be used independently. - - -.. code-block:: python - - from spikeinterface.curation import CurationSorting - - sorting = run_sorter(sorter_name='kilosort2', recording=recording) - - cs = CurationSorting(parent_sorting=sorting) - - # make a first merge - cs.merge(units_to_merge=['#1', '#5', '#15']) - - # make a second merge - cs.merge(units_to_merge=['#11', '#21']) - - # make a split - split_index = ... # some criteria on spikes - cs.split(split_unit_id='#20', indices_list=split_index) - - # here is the final clean sorting - clean_sorting = cs.sorting diff --git a/src/spikeinterface/benchmark/benchmark_motion_interpolation.py b/src/spikeinterface/benchmark/benchmark_motion_interpolation.py index 3e96013e7b..32ba210c5c 100644 --- a/src/spikeinterface/benchmark/benchmark_motion_interpolation.py +++ b/src/spikeinterface/benchmark/benchmark_motion_interpolation.py @@ -1,13 +1,10 @@ import numpy as np +from spikeinterface.core.sorting_tools import apply_merges_to_sorting from spikeinterface.sorters import run_sorter - from spikeinterface.comparison import GroundTruthComparison from spikeinterface.sortingcomponents.motion.motion_interpolation import InterpolateMotionRecording -from spikeinterface.curation import MergeUnitsSorting - - from .benchmark_base import Benchmark, BenchmarkStudy @@ -77,7 +74,7 @@ def compute_result(self, exhaustive_gt=True, merging_score=0.2): if merge_ids.size > 1: to_merge.append(list(merge_ids)) - merged_sporting = MergeUnitsSorting(sorting, to_merge) + merged_sporting = apply_merges_to_sorting(sorting, to_merge) comparison_merged = GroundTruthComparison(self.gt_sorting, merged_sporting, exhaustive_gt=True) self.result["comparison_merged"] = comparison_merged diff --git a/src/spikeinterface/curation/__init__.py b/src/spikeinterface/curation/__init__.py index 9dff51dcac..e855720f18 100644 --- a/src/spikeinterface/curation/__init__.py +++ b/src/spikeinterface/curation/__init__.py @@ -8,11 +8,6 @@ auto_merge_units, ) -# manual sorting, -from .curationsorting import CurationSorting, curation_sorting -from .mergeunitssorting import MergeUnitsSorting, merge_units_sorting -from .splitunitsorting import SplitUnitSorting, split_unit_sorting - # curation format from .curation_format import validate_curation_dict, curation_label_to_dataframe, apply_curation, load_curation diff --git a/src/spikeinterface/curation/auto_merge.py b/src/spikeinterface/curation/auto_merge.py index 17a3eb28af..1a8174a88a 100644 --- a/src/spikeinterface/curation/auto_merge.py +++ b/src/spikeinterface/curation/auto_merge.py @@ -13,10 +13,10 @@ HAVE_NUMBA = False from spikeinterface.core import SortingAnalyzer +from spikeinterface.core.sorting_tools import apply_merges_to_sorting from spikeinterface.metrics.quality.misc_metrics import compute_refrac_period_violations from spikeinterface.metrics.spiketrain.metrics import compute_firing_rates -from .mergeunitssorting import MergeUnitsSorting from .curation_tools import resolve_merging_graph _compute_merge_presets = { @@ -956,8 +956,8 @@ def check_improve_contaminations_score( # make a merged sorting and tale one unit (unit_id1 is used) unit_id1, unit_id2 = sorting.unit_ids[ind1], sorting.unit_ids[ind2] - sorting_merged = MergeUnitsSorting( - sorting, [[unit_id1, unit_id2]], new_unit_ids=[unit_id1], delta_time_ms=censored_period_ms + sorting_merged = apply_merges_to_sorting( + sorting, [[unit_id1, unit_id2]], new_unit_ids=[unit_id1], censor_ms=censored_period_ms ).select_units([unit_id1]) # create recordingless analyzer diff --git a/src/spikeinterface/curation/curationsorting.py b/src/spikeinterface/curation/curationsorting.py deleted file mode 100644 index 52b879fb64..0000000000 --- a/src/spikeinterface/curation/curationsorting.py +++ /dev/null @@ -1,294 +0,0 @@ -from collections import namedtuple -from collections.abc import Iterable - -import numpy as np - -from .mergeunitssorting import MergeUnitsSorting -from .splitunitsorting import SplitUnitSorting -from spikeinterface.core.core_tools import define_function_from_class - -node_t = namedtuple("node_t", "unit_id stage_id") - - -class CurationSorting: - """ - Class that handles curation of a Sorting object. - - Parameters - ---------- - sorting : BaseSorting - The sorting object - properties_policy : "keep" | "remove", default: "keep" - Policy used to propagate properties after split and merge operation. If "keep" the properties will be - passed to the new units (if the original units have the same value). If "remove" the new units will have - an empty value for all the properties - make_graph : bool - True to keep a Networkx graph instance with the curation history - - Returns - ------- - sorting : Sorting - Sorting object with the selected units merged - """ - - def __init__(self, sorting, make_graph=False, properties_policy="keep"): - - # to allow undo and redo a list of sortingextractors is keep - self._sorting_stages = [sorting] - self._sorting_stages_i = 0 - self._properties_policy = properties_policy - parent_units = sorting.get_unit_ids() - self._make_graph = make_graph - if make_graph: - # to easily allow undo and redo a list of graphs with the history of the curation is keep - import networkx as nx - - self._nx = nx - self._graphs = [self._nx.DiGraph()] - self._graphs[0].add_nodes_from([node_t(u, 0) for u in parent_units]) - # check the maximum numeric id used, strings with digits will be cast to int to reduce confusion - 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) if len(parent_units) > 0 else 0 - - self._kwargs = dict(sorting=sorting, make_graph=make_graph, properties_policy=properties_policy) - - def _get_unused_id(self, n=1): - # check units in the graph to the next unused unit id - ids = [self.max_used_id + i for i in range(1, 1 + n)] - if np.issubdtype(self.sorting.get_unit_ids().dtype, np.character): - ids = [str(i) for i in ids] - return ids - - def split(self, split_unit_id, indices_list, new_unit_ids=None): - """ - Split a unit into multiple units. - - Parameters - ---------- - split_unit_id : int or str - The unit to split - indices_list : list or np.array - A list of index arrays selecting the spikes to split in each segment. - Each array can contain more than 2 indices (e.g. for splitting in 3 or more units) and it should - be the same length as the spike train (for each segment). - If the sorting has only one segment, indices_list can be a single array - new_unit_ids : list[str|int] or None - List of new unit ids. If None, a new unit id is automatically selected - """ - current_sorting = self._sorting_stages[self._sorting_stages_i] - if not isinstance(indices_list, list): - indices_list = [indices_list] - if not isinstance(indices_list[0], Iterable): - raise ValueError("indices_list must be a list of iterable arrays") - if new_unit_ids is None: - new_unit_ids = self._get_unused_id(np.max([len(np.unique(v)) for v in indices_list])) - new_sorting = SplitUnitSorting( - current_sorting, - split_unit_id=split_unit_id, - indices_list=indices_list, - new_unit_ids=new_unit_ids, - properties_policy=self._properties_policy, - ) - - if self._make_graph: - units = current_sorting.get_unit_ids() - i = self._sorting_stages_i - edges = [(node_t(u, i), node_t(u, i + 1)) for u in units if u != split_unit_id] - edges = edges + [(node_t(split_unit_id, i), node_t(u, i + 1)) for u in new_unit_ids] - else: - edges = None - self.max_used_id = self.max_used_id + 2 - self._add_new_stage(new_sorting, edges) - - def merge(self, units_to_merge, new_unit_id=None, delta_time_ms=0.4): - """ - Merge a list of units into a new unit. - - Parameters - ---------- - units_to_merge : list[str|int] - List of unit ids to merge - new_unit_id : int or str - The new unit id. If None, a new unit id is automatically selected - delta_time_ms : float - Number of ms to consider for duplicated spikes. None won't check for duplications - """ - current_sorting = self._sorting_stages[self._sorting_stages_i] - if new_unit_id is None: - new_unit_id = self._get_unused_id()[0] - elif new_unit_id not in units_to_merge: - assert new_unit_id not in current_sorting.unit_ids, "new_unit_id already exists!" - new_sorting = MergeUnitsSorting( - sorting=current_sorting, - units_to_merge=units_to_merge, - new_unit_ids=[new_unit_id], - delta_time_ms=delta_time_ms, - properties_policy=self._properties_policy, - ) - if self._make_graph: - units = current_sorting.get_unit_ids() - i = self._sorting_stages_i - edges = [(node_t(u, i), node_t(u, i + 1)) for u in units if u not in units_to_merge] - edges = edges + [(node_t(u, i), node_t(new_unit_id, i + 1)) for u in units_to_merge] - else: - edges = None - self.max_used_id = self.max_used_id + 1 - self._add_new_stage(new_sorting, edges) - - def remove_units(self, unit_ids): - """ - Remove a list of units. - - Parameters - ---------- - unit_ids : list[str|int] - List of unit ids to remove - """ - current_sorting = self._sorting_stages[self._sorting_stages_i] - unit2keep = [u for u in current_sorting.get_unit_ids() if u not in unit_ids] - if self._make_graph: - i = self._sorting_stages_i - edges = [(node_t(u, i), node_t(u, i + 1)) for u in unit2keep] - else: - edges = None - self._add_new_stage(current_sorting.select_units(unit2keep), edges) - - def remove_unit(self, unit_id): - """ - Remove a unit. - - Parameters - ---------- - unit_id : int or str - The unit id to remove - """ - self.remove_units([unit_id]) - - def select_units(self, unit_ids, renamed_unit_ids=None): - """ - Select a list of units. - - Parameters - ---------- - unit_ids : list[str|int] - List of unit ids to select - renamed_unit_ids : list or None, default: None - List of new unit ids to rename the selected units - """ - new_sorting = self._sorting_stages[self._sorting_stages_i].select_units(unit_ids, renamed_unit_ids) - if self._make_graph: - i = self._sorting_stages_i - if renamed_unit_ids is None: - edges = [(node_t(u, i), node_t(u, i + 1)) for u in unit_ids] - else: - edges = [(node_t(u, i), node_t(v, i + 1)) for u, v in zip(unit_ids, renamed_unit_ids)] - else: - edges = None - self._add_new_stage(new_sorting, edges) - - def rename(self, renamed_unit_ids): - """ - Rename a list of units. - - Parameters - ---------- - renamed_unit_ids : list[str|int] - List of unit ids to rename exisiting units - """ - self.select_units(self.current_sorting.unit_ids, renamed_unit_ids=renamed_unit_ids) - - def remove_empty_units(self): - """ - Remove empty units. - """ - i = self._sorting_stages_i - new_sorting = self._sorting_stages[i].remove_empty_units() - if self._make_graph: - curr_ids = self._sorting_stages[i].get_unit_ids() - edges = [(node_t(u, i), node_t(u, i + 1)) for u in curr_ids] - else: - edges = None - self._add_new_stage(new_sorting, edges) - - def redo_available(self): - """ - Check if redo is available. - - Returns - ------- - bool - True if redo is available - """ - # useful function for a gui - return self._sorting_stages_i < len(self._sorting_stages) - - def undo_available(self): - """ - Check if undo is available. - - Returns - ------- - bool - True if undo is available - """ - # useful function for a gui - return self._sorting_stages_i > 0 - - def undo(self): - """ - Undo the last operation. - """ - if self.undo_available(): - self._sorting_stages_i -= 1 - - def redo(self): - """ - Redo the last operation. - """ - if self.redo_available(): - self._sorting_stages_i += 1 - - def draw_graph(self, **kwargs): - """ - Draw the curation graph. - - Parameters - ---------- - **kwargs : dict - Keyword arguments for Networkx draw function - """ - assert self._make_graph, "to make a graph use make_graph=True" - graph = self.graph - ids = [c.unit_id for c in graph.nodes] - pos = {n: (n.stage_id, -ids.index(n.unit_id)) for n in graph.nodes} - labels = {n: str(n.unit_id) for n in graph.nodes} - self._nx.draw(graph, pos=pos, labels=labels, **kwargs) - - @property - def graph(self): - assert self._make_graph, "to have a graph use make_graph=True" - return self._graphs[self._sorting_stages_i] - - @property - def sorting(self): - return self.current_sorting - - @property - def current_sorting(self): - return self._sorting_stages[self._sorting_stages_i] - - def _add_new_stage(self, new_sorting, edges): - # adds the stage to the stage list and creates the associated new graph - self._sorting_stages = self._sorting_stages[0 : self._sorting_stages_i + 1] - self._sorting_stages.append(new_sorting) - if self._make_graph: - self._graphs = self._graphs[0 : self._sorting_stages_i + 1] - new_graph = self._graphs[self._sorting_stages_i].copy() - new_graph.add_edges_from(edges) - self._graphs.append(new_graph) - self._sorting_stages_i += 1 - - -curation_sorting = define_function_from_class(source_class=CurationSorting, name="curation_sorting") diff --git a/src/spikeinterface/curation/mergeunitssorting.py b/src/spikeinterface/curation/mergeunitssorting.py deleted file mode 100644 index 4d8fdddb82..0000000000 --- a/src/spikeinterface/curation/mergeunitssorting.py +++ /dev/null @@ -1,189 +0,0 @@ -from copy import deepcopy - -import numpy as np - -from spikeinterface.core.basesorting import BaseSorting, BaseSortingSegment -from spikeinterface.core.core_tools import define_function_from_class -from spikeinterface.core.sorting_tools import generate_unit_ids_for_merge_group - - -class MergeUnitsSorting(BaseSorting): - """ - Class that handles several merges of units from a Sorting object based on a list of lists of unit_ids. - - Parameters - ---------- - sorting : BaseSorting - The sorting object - units_to_merge : list/tuple of lists/tuples - A list of lists for every merge group. Each element needs to have at least two elements (two units to merge), - but it can also have more (merge multiple units at once). - new_unit_ids : None or list - A new unit_ids for merged units. If given, it needs to have the same length as `units_to_merge` - properties_policy : "keep" | "remove", default: "keep" - Policy used to propagate properties. If "keep" the properties will be passed to the new units - (if the units_to_merge have the same value). If "remove" the new units will have an empty - value for all the properties of the new unit. - delta_time_ms : float or None - Number of ms to consider for duplicated spikes. None won't check for duplications - - Returns - ------- - sorting : Sorting - Sorting object with the selected units merged - """ - - def __init__(self, sorting, units_to_merge, new_unit_ids=None, properties_policy="keep", delta_time_ms=0.4): - self._parent_sorting = sorting - - if not isinstance(units_to_merge[0], (list, tuple)): - # keep backward compatibility : the previous behavior was only one merge - units_to_merge = [units_to_merge] - - num_merge = len(units_to_merge) - - parents_unit_ids = sorting.unit_ids - sampling_frequency = sorting.get_sampling_frequency() - - new_unit_ids = generate_unit_ids_for_merge_group( - sorting.unit_ids, units_to_merge, new_unit_ids=new_unit_ids, new_id_strategy="append" - ) - - all_removed_ids = [] - for ids in units_to_merge: - all_removed_ids.extend(ids) - keep_unit_ids = [u for u in parents_unit_ids if u not in all_removed_ids] - - assert len(new_unit_ids) == num_merge, "new_unit_ids must have the same size as units_to_merge" - - # some checks - for ids in units_to_merge: - assert all(u in parents_unit_ids for u in ids), "units to merge are not all in parent" - assert properties_policy in ("keep", "remove"), "properties_policy must be keep or remove" - - # new units are put at the end - unit_ids = keep_unit_ids + list(new_unit_ids) - BaseSorting.__init__(self, sampling_frequency, unit_ids) - # assert all(np.isin(keep_unit_ids, self.unit_ids)), 'new_unit_id should have a compatible format with the parent ids' - - if delta_time_ms is None: - rm_dup_delta = None - else: - rm_dup_delta = int(delta_time_ms / 1000 * sampling_frequency) - for parent_segment in self._parent_sorting.segments: - sub_segment = MergeUnitsSortingSegment(parent_segment, units_to_merge, new_unit_ids, rm_dup_delta) - self.add_sorting_segment(sub_segment) - - ann_keys = sorting._annotations.keys() - self._annotations = deepcopy({k: sorting._annotations[k] for k in ann_keys}) - - # copy properties for unchanged units, and check if units propierties are the same - keep_parent_inds = sorting.ids_to_indices(keep_unit_ids) - # ~ all_removed_inds = parent_sorting.ids_to_indices(all_removed_ids) - keep_inds = self.ids_to_indices(keep_unit_ids) - # ~ merge_inds = self.ids_to_indices(new_unit_ids) - prop_keys = sorting.get_property_keys() - for key in prop_keys: - parent_values = sorting.get_property(key) - - if properties_policy == "keep": - # propagate keep values - shape = (len(unit_ids),) + parent_values.shape[1:] - new_values = np.empty(shape=shape, dtype=parent_values.dtype) - new_values[keep_inds] = parent_values[keep_parent_inds] - for new_id, ids in zip(new_unit_ids, units_to_merge): - removed_inds = sorting.ids_to_indices(ids) - merge_values = parent_values[removed_inds] - - same_property_values = np.all([np.array_equal(m, merge_values[0]) for m in merge_values[1:]]) - - ind = self.id_to_index(new_id) - if same_property_values: - # and new values only if they are all similar - new_values[ind] = merge_values[0] - else: - if parent_values.dtype.kind == "f": - new_values[ind] = np.nan - elif parent_values.dtype.kind in ("U", "S"): - new_values[ind] = "" - else: - new_values = new_values.astype(object) - new_values[ind] = None - self.set_property(key, new_values) - - elif properties_policy == "remove": - self.set_property(key, parent_values[keep_parent_inds], keep_unit_ids) - - if sorting.has_recording(): - self.register_recording(sorting._recording) - - # make it jsonable - units_to_merge = [list(e) for e in units_to_merge] - self._kwargs = dict( - sorting=sorting, - units_to_merge=units_to_merge, - new_unit_ids=new_unit_ids, - properties_policy=properties_policy, - delta_time_ms=delta_time_ms, - ) - - -merge_units_sorting = define_function_from_class(source_class=MergeUnitsSorting, name="merge_units_sorting") - - -class MergeUnitsSortingSegment(BaseSortingSegment): - def __init__(self, parent_segment, units_to_merge, new_unit_ids, rm_dup_delta): - BaseSortingSegment.__init__(self) - self._parent_segment = parent_segment - self._units_to_merge = units_to_merge - self.new_unit_ids = new_unit_ids - self._dup_delta = rm_dup_delta - # if cache compute - self._merged_spike_times = [] - for ids in units_to_merge: - spike_times = get_non_duplicated_events( - [self._parent_segment.get_unit_spike_train(u, None, None) for u in ids], rm_dup_delta - ) - self._merged_spike_times.append(spike_times) - - def get_unit_spike_train( - self, - unit_id, - start_frame: int | None = None, - end_frame: int | None = None, - ) -> np.ndarray: - if unit_id in self.new_unit_ids: - ind = self.new_unit_ids.index(unit_id) - spike_times = self._merged_spike_times[ind] - - if start_frame is not None: - start_i = np.searchsorted(spike_times, start_frame, side="left") - else: - start_i = 0 - if end_frame is not None: - end_i = np.searchsorted(spike_times, end_frame, side="right") - else: - end_i = len(spike_times) - return spike_times[start_i:end_i] - else: - spike_times = self._parent_segment.get_unit_spike_train(unit_id, start_frame, end_frame) - return spike_times - - -# TODO move this function to postprocessing or similar -def get_non_duplicated_events(times_list, delta): - times_concat = np.concatenate(times_list) - if len(times_concat) == 0: - return times_concat - indices = times_concat.argsort(kind="mergesort") - times_concat_sorted = times_concat[indices] - - if delta is None: - return times_concat_sorted - membership = np.concatenate([np.ones(t.shape) * i for i, t in enumerate(times_list)]) - membership_sorted = membership[indices] - - inds = np.nonzero((np.diff(times_concat_sorted) > delta) | (np.diff(membership_sorted) == 0))[0] - # always add the first one and realing counting - inds = np.concatenate([[0], inds + 1]) - return times_concat_sorted[inds] diff --git a/src/spikeinterface/curation/tests/test_curationsorting.py b/src/spikeinterface/curation/tests/test_curationsorting.py deleted file mode 100644 index b1a063eac5..0000000000 --- a/src/spikeinterface/curation/tests/test_curationsorting.py +++ /dev/null @@ -1,146 +0,0 @@ -import numpy as np - -from spikeinterface.core import NumpySorting -from spikeinterface.curation import CurationSorting, MergeUnitsSorting, SplitUnitSorting - - -def test_split_merge(): - spikestimes = [ - { - 0: np.arange(15), - 1: np.arange(17), - 2: np.arange(17) + 5, - 4: np.concatenate([np.arange(10), np.arange(20, 30)]), - 5: np.arange(9), - }, - {0: np.arange(15), 1: np.arange(17), 2: np.arange(40, 140), 4: np.arange(40, 140), 5: np.arange(40, 140)}, - ] - parent_sort = NumpySorting.from_unit_dict(spikestimes, sampling_frequency=1000) # to have 1 sample=1ms - parent_sort.set_property("someprop", [float(k) for k in spikestimes[0].keys()]) # float - - split_index = [v[4] % 2 for v in spikestimes] # spit class 4 in even and odds - splitted = SplitUnitSorting( - parent_sort, split_unit_id=4, indices_list=split_index, new_unit_ids=[8, 10], properties_policy="keep" - ) - - # Test 1D and multi-D properties of different types - # add 1D str property (with different values for units to be merged) -> "" - some_str_prop = ["merge"] * len(splitted.unit_ids) - some_str_prop[-1] = "different" - splitted.set_property("some_str_prop", some_str_prop) - - # add 1D float property (with same values for units to be merged) -> keep - some_prop_to_keep = np.ones(len(splitted.unit_ids)) - splitted.set_property("some_prop_to_keep", some_prop_to_keep) - # add 1D float property (with different values for units to be merged) -> nan - some_prop_to_remove = np.arange(len(splitted.unit_ids), dtype=float) - splitted.set_property("some_prop_to_remove", some_prop_to_remove) - # add 1D int property (with different values for units to be merged) -> None - some_prop_to_none = np.arange(len(splitted.unit_ids), dtype=int) - splitted.set_property("some_prop_to_none", some_prop_to_none) - - # add array property (with same values for units to be merged) -> keep - some_array_prop_same_values = np.ones((len(splitted.unit_ids), 2)) - splitted.set_property("some_array_prop_to_keep", some_array_prop_same_values) - # add float array property (with different values for units to be merged) -> nan - some_array_prop_to_remove = np.random.randn(len(splitted.unit_ids), 2) - splitted.set_property("some_array_prop_to_remove", some_array_prop_to_remove) - # add int array property (with different values for units to be merged) -> None - some_array_prop_to_none = np.ones((len(splitted.unit_ids), 2), dtype=int) - some_array_prop_to_none[-1] = [1, 2] - splitted.set_property("some_array_prop_to_none", some_array_prop_to_none) - - merged = MergeUnitsSorting(splitted, units_to_merge=[[8, 10]], new_unit_ids=[4], properties_policy="keep") - for i in range(len(spikestimes)): - assert all( - parent_sort.get_unit_spike_train(4, segment_index=i) == merged.get_unit_spike_train(4, segment_index=i) - ), "splir or merge error" - assert parent_sort.get_unit_property(4, "someprop") == merged.get_unit_property(4, "someprop"), ( - "property wasn't kept" - ) - # 1d - assert merged.get_unit_property(4, "some_str_prop") == "", "error with array property" - assert merged.get_unit_property(4, "some_prop_to_keep") == 1, "error with array property" - assert np.isnan(merged.get_unit_property(4, "some_prop_to_remove")), "error with array property" - assert merged.get_unit_property(4, "some_prop_to_none") is None, "error with array property" - # 2d - assert np.array_equal(merged.get_unit_property(4, "some_array_prop_to_keep"), [1, 1]), "error with array property" - assert np.all(np.isnan(merged.get_unit_property(4, "some_array_prop_to_remove"))), "error with array property" - assert np.array_equal(merged.get_unit_property(4, "some_array_prop_to_none"), [None, None]), ( - "error with array property" - ) - - merged_with_dups = MergeUnitsSorting( - parent_sort, new_unit_ids=[8], units_to_merge=[[0, 1]], properties_policy="remove", delta_time_ms=0.5 - ) - for i in range(len(spikestimes)): - assert all( - merged_with_dups.get_unit_spike_train(8, segment_index=i) - == parent_sort.get_unit_spike_train(1, segment_index=i) - ), "error removing duplications" - assert np.isnan(merged_with_dups.get_unit_property(8, "someprop")), "error creating empty property" - - -def test_curation(): - spikestimes = [ - { - "a": np.arange(15), - "b": np.arange(5, 10), - "c": np.arange(20), - }, - {"a": np.arange(12, 15), "b": np.arange(3, 17), "c": np.arange(50)}, - ] - parent_sort = NumpySorting.from_unit_dict(spikestimes, sampling_frequency=1000) # to have 1 sample=1ms - parent_sort.set_property("some_names", ["unit_{}".format(k) for k in spikestimes[0].keys()]) # float - cs = CurationSorting(parent_sort, properties_policy="remove") - - # merge a-c - cs.merge(["a", "c"], new_unit_id="a-c") - assert cs.sorting.get_num_units() == len(spikestimes[0]) - 1 - cs.undo() - - # split b in 2 - split_index = [v["b"] < 6 for v in spikestimes] # split class 4 in even and odds - cs.split("b", split_index, new_unit_ids=["b1", "b2"]) - after_split = cs.sorting - assert cs.sorting.get_num_units() == len(spikestimes[0]) + 1 - cs.undo() - - # split one unit in 3 - split_index3 = [v["b"] % 3 + 100 for v in spikestimes] # split class in 3 - cs.split("b", split_index3, new_unit_ids=["b1", "b2", "b3"]) - after_split = cs.sorting - for segment_index in range(len(spikestimes)): - _, split_counts = np.unique(split_index3[segment_index], return_counts=True) - for unit_id, count in zip(["b1", "b2", "b3"], split_counts): - assert len(after_split.get_unit_spike_train(unit_id, segment_index=segment_index)) == count - assert after_split.get_num_units() == len(spikestimes[0]) + 2 - cs.undo() - - # split with renaming - cs.split("b", split_index3) - after_split = cs.sorting - assert after_split.get_num_units() == len(spikestimes[0]) + 2 - - all_units = cs.sorting.get_unit_ids() - 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 - cs.redo() - unit = cs.sorting.get_unit_ids()[0] - for i in range(len(spikestimes)): - assert all( - cs.sorting.get_unit_spike_train(unit, segment_index=i) - == parent_sort.get_unit_spike_train("c", segment_index=i) - ) - - # Test with empty sorting - _ = CurationSorting(NumpySorting.from_unit_dict({}, parent_sort.sampling_frequency)) - - -if __name__ == "__main__": - test_split_merge() - test_curation()