Skip to content
Merged
Show file tree
Hide file tree
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
51 changes: 38 additions & 13 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 @@ -1271,6 +1264,7 @@ def merge_units(
censor_ms: float | None = None,
merging_mode: str = "soft",
sparsity_overlap: float = 0.75,
raise_error_if_overlap_fails: bool = True,
new_id_strategy: str = "append",
return_new_unit_ids: bool = False,
format: str = "memory",
Expand Down Expand Up @@ -1300,7 +1294,10 @@ def merge_units(
reloading waveforms if needed
sparsity_overlap : float, default 0.75
The percentage of overlap that units should share in order to accept merges. If this criteria is not
achieved, soft merging will not be possible and an error will be raised
achieved for a pair of units, soft merging will not be applied to them.
raise_error_if_overlap_fails : bool, default: True
If True and `sparsity_overlap` fails for any unit merges, this will raise an error. If False, units which fail the
`sparsity_overlap` threshold will be skipped in the merge.
new_id_strategy : "append" | "take_first", default: "append"
The strategy that should be used, if `new_unit_ids` is None, to create new unit_ids.

Expand Down Expand Up @@ -1333,15 +1330,43 @@ 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:
if raise_error_if_overlap_fails:
error_message = 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}. Either lower your `sparsity_overlap` or use the flag `raise_error_if_overlap_fails = False` to skip these units in your merge."
raise Exception(error_message)
else:
warning_message = 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."
warnings.warn(warning_message)
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
5 changes: 5 additions & 0 deletions src/spikeinterface/curation/curation_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def apply_curation(
new_id_strategy: str = "append",
merging_mode: str = "soft",
sparsity_overlap: float = 0.75,
raise_error_if_overlap_fails: bool = True,
verbose: bool = False,
**job_kwargs,
):
Expand Down Expand Up @@ -181,6 +182,9 @@ def apply_curation(
sparsity_overlap : float, default 0.75
The percentage of overlap that units should share in order to accept merges. If this criteria is not
achieved, soft merging will not be possible and an error will be raised. This is for use with a SortingAnalyzer input.
raise_error_if_overlap_fails : bool, default: True
If True and `sparsity_overlap` fails for any unit merges, this will raise an error. If False, units which fail the
`sparsity_overlap` threshold will be skipped in the merge.
verbose : bool, default: False
If True, output is verbose
**job_kwargs : dict
Expand Down Expand Up @@ -234,6 +238,7 @@ def apply_curation(
censor_ms=censor_ms,
merging_mode=merging_mode,
sparsity_overlap=sparsity_overlap,
raise_error_if_overlap_fails=raise_error_if_overlap_fails,
new_id_strategy=new_id_strategy,
return_new_unit_ids=True,
format="memory",
Expand Down