Skip to content

Conversation

@alejoe91
Copy link
Member

@alejoe91 alejoe91 commented Jan 7, 2026

Depends on #4316

Slices a sorting object based on an array ov valid periods. Periods are defined as a structured dtype as:

base_period_dtype = [
    ("start_sample_index", "int64"),
    ("end_sample_index", "int64"),
    ("segment_index", "int64"),
    ("unit_index", "int64"),
]

EDIT:

Refactored computation of spike train metrics, to make sure that periods are consistently taken into account. Added 2 utils functions to compute durations per unit and bin edges per unit, that optionally use the provided periods

@alejoe91 alejoe91 added the core Changes to core module label Jan 7, 2026
@alejoe91 alejoe91 mentioned this pull request Jan 7, 2026
1 task
@alejoe91 alejoe91 marked this pull request as ready for review January 8, 2026 07:36
@alejoe91 alejoe91 requested a review from chrishalcrow January 8, 2026 07:36
@samuelgarcia
Copy link
Member

This is OK for me.
Make a clear documentation somwhere woule help ?

@alejoe91
Copy link
Member Author

@chrishalcrow I refactored a few metrics to make sure durations, spike counts, and bins are properly accounted for when slicing with periods. Happy to discuss about this!

@alejoe91 alejoe91 changed the title Implement select_sorting_periods Implement select_sorting_periods in metrics Jan 13, 2026
@alejoe91 alejoe91 requested a review from yger January 15, 2026 15:13
Copy link
Collaborator

@yger yger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, I think this is a great extension to be able to compute metrics on sub_periods only. A couple of minor comments, and I think everything is not too slow, but good work!


# Check that all spikes in the sliced sorting are within the periods
for segment_index in range(sorting.get_num_segments()):
periods_in_segment = periods[periods["segment_index"] == segment_index]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we assume not too many periods, just to be sure that all these masks won't take too long?

for segment_index in range(sorting.get_num_segments()):
global_indices_segment = all_global_indices[segment_index]
# filter periods by segment
periods_in_segment = periods[periods["segment_index"] == segment_index]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming not too many periods, these masks would be fine. Otherwise, we'll need to optimize

@yger
Copy link
Collaborator

yger commented Jan 15, 2026

We don't want to extend these quality metrics to the extension() ? What if someone wants to only get the ISI, CCG or anything else only on the periods? Would it be easy to slice the sorting, and then compute only on the sub sorting? Are the extension robust w.r.t. periods?

@alejoe91
Copy link
Member Author

We don't want to extend these quality metrics to the extension() ? What if someone wants to only get the ISI, CCG or anything else only on the periods? Would it be easy to slice the sorting, and then compute only on the sub sorting? Are the extension robust w.r.t. periods?

I think that extension by extension we could use valid_unit_periods when computed :)

@alejoe91
Copy link
Member Author

@samuelgarcia @chrishalcrow tests added! all good now :)

num_segments = len(segment_samples)
bin_duration_samples = int(bin_duration_s * sorting.sampling_frequency)

if periods is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there are no good periods, then periods will be an empty array? Should we have a general strategy for when this is true - empty units often cause pain/nans...

"""

sorting = sorting_analyzer.sorting
sorting = sorting.select_periods(periods)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I choose periods so that only one unit is good, and get the num spikes, get

import spikeinterface.full as si
import numpy as np
from spikeinterface.core.base import unit_period_dtype

rec, sort = si.generate_ground_truth_recording(seed=1205)
sa = si.create_sorting_analyzer(sort, rec)
periods = np.array((0,1000,4000,0), dtype=unit_period_dtype)
quality_metrics = si.compute_quality_metrics(sa, periods=periods, metric_names = ['num_spikes'])

print(quality_metrics['num_spikes'].values)
>>> [162 173 135 154 158 144 151 141 153 162]

But this sorting only has spikes from unit index 0, right? Is count_num_spikes_per_unit below using an old cache or something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Changes to core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants