Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/spikeinterface/core/sortinganalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ def _save_or_select_or_merge_or_split(
# merge units
all_unit_ids = unit_ids
sparsity_mask = np.zeros((len(all_unit_ids), self.sparsity.mask.shape[1]), dtype=bool)
mergeable, masks = self.are_units_mergeable(
_, masks = self.are_units_mergeable(
merge_unit_groups,
sparsity_overlap=sparsity_overlap,
merging_mode=merging_mode,
Expand All @@ -1033,14 +1033,7 @@ def _save_or_select_or_merge_or_split(
for unit_index, unit_id in enumerate(all_unit_ids):
if unit_id in merge_new_unit_ids:
merge_unit_group = tuple(merge_unit_groups[merge_new_unit_ids.index(unit_id)])
if not mergeable[merge_unit_group]:
raise Exception(
f"The sparsity of {merge_unit_group} do not overlap enough for a soft merge using "
f"a sparsity threshold of {sparsity_overlap}. You can either lower the threshold or use "
"a hard merge."
)
else:
sparsity_mask[unit_index] = masks[merge_unit_group]
sparsity_mask[unit_index] = masks[merge_unit_group]
else:
# This means that the unit is already in the previous sorting
index = self.sorting.id_to_index(unit_id)
Expand Down Expand Up @@ -1333,15 +1326,40 @@ def merge_units(
if len(units) < 2:
raise ValueError("Merging requires at least two units to merge")

# remove units which do not pass the sparsity threshold
if self.sparsity is not None and merging_mode == "soft":

mergeable = self.are_units_mergeable(
merge_unit_groups,
sparsity_overlap=sparsity_overlap,
merging_mode=merging_mode,
return_masks=False,
)

mergeable_unit_groups = []
unmergeable_unit_groups = []
for merge_unit_group, mergeable in zip(merge_unit_groups, mergeable.values()):
if mergeable:
mergeable_unit_groups.append(merge_unit_group)
else:
unmergeable_unit_groups.append(merge_unit_group)

if len(unmergeable_unit_groups) > 0:
warnings.warn(
f"The sparsity of the units in the merge groups {unmergeable_unit_groups} do not overlap enough for a soft merge using a sparsity threshold of {sparsity_overlap}. They will not be merged."
)
else:
mergeable_unit_groups = merge_unit_groups

new_unit_ids = generate_unit_ids_for_merge_group(
self.unit_ids, merge_unit_groups, new_unit_ids, new_id_strategy
self.unit_ids, mergeable_unit_groups, new_unit_ids, new_id_strategy
)
all_unit_ids = _get_ids_after_merging(self.unit_ids, merge_unit_groups, new_unit_ids=new_unit_ids)
all_unit_ids = _get_ids_after_merging(self.unit_ids, mergeable_unit_groups, new_unit_ids=new_unit_ids)

new_analyzer = self._save_or_select_or_merge_or_split(
format=format,
folder=folder,
merge_unit_groups=merge_unit_groups,
merge_unit_groups=mergeable_unit_groups,
unit_ids=all_unit_ids,
censor_ms=censor_ms,
merging_mode=merging_mode,
Expand Down