presence_ratio and firing_range are wrong when periods have gaps or do not start on the bin grid
Les mainteneurs répondent en général sous 2 jours
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 72/100
Piste de recherche
Start with compute_bin_edges_per_unit in src/spikeinterface/metrics/utils.py, then trace its callers in src/spikeinterface/metrics/quality/misc_metrics.py for presence ratio, firing range, and drift metrics. Add coverage for gapped periods and periods that start off the bin grid, checking that gaps do not affect the histogram and each period retains bins. Done means the reported presence ratio and firing range match the expected values in the reproduction.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
With periods, compute_bin_edges_per_unit gives wrong bins in two ways, so presence_ratio and firing_range are wrong without any warning. presence_ratio is a common curation filter, so this can discard good units.
Reproduction
main at 9ef464e66, Linux (WSL Ubuntu 24.04), Python 3.12.3, numpy 2.5.4:
import numpy as np
import spikeinterface.core as sc
from spikeinterface.core.base import unit_period_dtype
from spikeinterface.metrics.quality import compute_firing_ranges, compute_presence_ratios
fs = 1000.0
rec = sc.generate_recording(num_channels=2, sampling_frequency=fs, durations=[180.0], seed=0)
# One unit firing every 100 ms for the whole recording: present in every bin
sorting = sc.NumpySorting.from_unit_dict({0: np.arange(50, 180_000, 100)}, sampling_frequency=fs)
analyzer = sc.create_sorting_analyzer(sorting, rec, sparse=False)
def periods(*bounds_s):
return np.array([(0, int(a*fs), int(b*fs), 0) for a, b in bounds_s], dtype=unit_period_dtype)
for label, per in [
("no periods", None),
("[0, 60) + [120, 180)", periods((0, 60), (120, 180))),
("[0, 60) + [150, 180)", periods((0, 60), (150, 180))),
("[10, 70)", periods((10, 70))),
]:
print(f"{label:<22} presence_ratio = {compute_presence_ratios(analyzer, periods=per, bin_duration_s=60)[0]:.3f}")
# This unit has a firing range of 0 Hz whatever the periods
gapped = periods(*[(20*k, 20*k + 10) for k in range(6)])
print(f"firing_range, no periods: {compute_firing_ranges(analyzer, bin_size_s=5)[0]:.2f} Hz")
print(f"firing_range, 10 s on, 10 off: {compute_firing_ranges(analyzer, periods=gapped, bin_size_s=5)[0]:.2f} Hz")
no periods presence_ratio = 1.000
[0, 60) + [120, 180) presence_ratio = 0.667
[0, 60) + [150, 180) presence_ratio = 0.500
[10, 70) presence_ratio = 0.000
firing_range, no periods: 0.00 Hz
firing_range, 10 s on, 10 off: 10.00 Hz
The unit fires throughout, so I expect a presence ratio of 1 and a firing range of 0 Hz in every case.
Cause
Both defects are in the periods branch of compute_bin_edges_per_unit:
- The edges of all periods of a segment are merged into one array, which is then used as the bins of a histogram. The gap between two periods becomes a bin. It is always empty, because
select_periodshas removed its spikes, but it still counts in the denominator ofpresence_ratioand in the rate distribution offiring_range. - The end of a period is aligned on the absolute grid, starting at sample 0, not at the start of the period. A period that does not start on a multiple of the bin duration can lose its bins:
[10, 70)with bins of 60 s gives a single edge.
The same helper is used for presence_ratio, firing_range and the drift metrics. I did not run the drift metrics with gapped periods.
Scope
The figures above use periods= directly. compute_quality_metrics(..., use_valid_periods=True) passes the output of valid_unit_periods to the same code, so I expect valid periods with gaps to have the same problem. In the two synthetic cases that I tried, the extension returned the whole recording or no period, so I have not observed it through that route. #4590 asks what periods= may contain; these results hold for any non-contiguous periods.
Possible fix
Compute the histogram per period, with a grid anchored on the start of that period and a rule for a last partial bin, then concatenate the counts rather than the edges. The current tests use create_regular_periods, which is contiguous, so tests with gapped periods and periods off the grid would be needed. I have not written this change.
I found no existing report. The helper comes from #4302. I can work on a PR, if this approach suits you.
- Langage dominant
- Python
- Étoiles
- 883
- Forks
- 291
- Merge moyen
- 3 j 17 h
- PR mergées (30 j)
- 40
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de SpikeInterface/spikeinterface
-
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100
SpikeInterface/spikeinterface#5008 ·
Les mainteneurs répondent en général sous 2 jours
-
`plot_potential_merges` draws the correlograms of other units when a merge group is not sortedOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
SpikeInterface/spikeinterface#5007 ·
Les mainteneurs répondent en général sous 2 jours
-
`plot_unit_presence` shows bin indices on the `Time (s)` axis when `time_range` is not givenOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
SpikeInterface/spikeinterface#5006 ·
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100
SpikeInterface/spikeinterface#5005 ·
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
SpikeInterface/spikeinterface#5004 ·
Les mainteneurs répondent en général sous 2 jours
Toutes les issues de SpikeInterface/spikeinterface
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 69/100
daniel-ospina/tortoise#15308 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
Les mainteneurs répondent en général sous 1 jour
-
difficulty:easy prio:low type:bug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
NatronGitHub/Natron#2405 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
Jstar269/nakagawa-recomp#3746 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour