PC metrics don't account for `random_spikes` subsampling

Aperta
#4,976 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
data

Direzione di ricerca

The issue does not name source files or tests. Start by locating the PC metric implementations and how they consume random_spikes; compare that path with run_for_all_spikes projections. Done means the isolation distance and L-ratio estimates account for subsampling and are validated against every-spike results; the nearest-neighbor metrics are described as a separate unresolved problem.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug metrics

I am computing several PC QMs on long recordings, and when I compare the values obtained using every spike (run_for_all_spikes) to values obtained using the defaults, they are far off. Surprisingly (at first, but now I understand why), raising the cap (e.g. from the default 500 to 20,000) doesn't fix it. An actual change to the QMs is needed, in order to be able to subsample spikes and still properly estimate the textbook values.

More details

The PC metrics compare a unit's spikes with those of its neighboring units (the units whose main channel is within the unit's sparsity, or all units if the analyzer is dense), in PC space. Both come only from the random_spikes draw, by default at most 500 spikes per unit.

For example, take a unit with 2,000 spikes next to a neighbor with 50,000. In the recording the neighbor has 25 spikes for each of the unit's. But after subsampling, both have 500. The consequence:

  • L-ratio sums over the neighbor's spikes and divides by the unit's count. After subsampling it comes out about 25 times too low (because the 25:1 ratio is now 1:1).
  • Isolation distance should be the D² of the 2,000th closest of the neighbor's 50,000 spikes. In real terms, the edge of the neighbor's closest 4%. After subsampling it is the 500th closest of the neighbor's 500 drawn spikes (i.e. the single farthest randomly selected spike), which is likely to be far away, and reads as (gives a false impression of) much better isolation.

Keep in mind that both isolation distance and L-ratio are originally defined on all of a unit's spikes and all of its neighbors' spikes, so our values are very much not comparable to the textbook values. FWIW, in bombcell they are computed that way, on every spike. [^1]

Median error across units, compared with using every spike, and the rank correlation of units with their every-spike values (lowest of 5 draws):

Recording Cap (spikes per unit) Isolation distance L-ratio Rank correlation (isolation distance / L-ratio)
Neuropixels, 6 h, 264 units (268 for L-ratio) (default 5 PCs, 100 µm) 500 (default) 22 % 77 % 0.73 / 0.75
same 20,000 8.8 % 47 % 0.89 / 0.91
Tetrodes, 48 h, 54 units (58 for L-ratio) 500 17.5 % 45.2 % 0.76 / 0.76
same 20,000 16.3 % 36.3 % 0.80 / 0.79

I was hoping that even if the values are way off the textbook ones, the rank order of units would stay the same, but it does not. The rank correlation is 0.76–0.80 on the tetrodes and 0.73–0.91 on Neuropixels.

The nearest-neighbor hit and miss rates have a related problem. They cull the randomly drawn spikes of the unit and its neighbors together (keeping every k-th spike to bring the total down to 10,000), so a unit with few spikes among neighbors with many winds up with only a handful at the end.

The d' is less affected.

Part of a possible solution: weighted draws of neighbors' spikes

I compared to the Allen Institute's ecephys_spike_sorting, which also caps a unit at 500 spikes but draws each neighbor in proportion to its spike count. This alleviates the issue somewhat.

In expectation, the same thing (as drawing in proportion to spike count) can be done with one shared draw by weighting: each drawn spike of a neighbor counts as (neighbor's total spikes) / (neighbor's drawn spikes), L-ratio is divided by the unit's total spike count, and isolation distance is taken where the cumulative weight of the neighbors' spikes, in order of distance, reaches the unit's total. With weighting alone [^2]:

Recording Cap (spikes per unit) Isolation distance L-ratio Rank correlation (isolation distance / L-ratio)
Neuropixels 500 (default) 10 % 42 % 0.99 / 0.99
same 20,000 0.1 % 0.5 % 1.00 / 1.00
Tetrodes 500 5.2 % 13.1 % 0.99 / 0.99
same 20,000 0.3 % 0.8 % 1.00 / 1.00

Much better! And now it converges as the cap grows. But at the default cap it is still off. Which brings us to...

The rest of a possible solution: scaling the distances

At 500 spikes the estimate is still off even with weighting, because the unit's own mean and covariance still come from its m drawn spikes.

D² measures distance in units of the unit's spread, by multiplying with the inverse of its covariance. A covariance estimated from m spikes is right on average, but its inverse is not, because inverting amplifies underestimates more than it shrinks overestimates: a variance estimated 20% too low gives an inverse 25% too high, while one estimated 20% too high gives an inverse only 17% too low. These errors don't cancel, and with p features there are p directions in which to make them. For Gaussian data this is a standard result: on average the inverse is too large by a factor of (m − 1)/(m − p − 2), so every D² is inflated by about that factor[^3]. Multiplying every D² by (m − p − 2)/(m − 1) undoes it.

The every-spike value is computed the same way, from the unit's N spikes, so it carries the same inflation with N in place of m (tiny for a unit with many spikes). To match the every-spike value itself, rather than the value for the true covariance, the full scaling is (m − p − 2)/(m − 1) × (N − 1)/(N − p − 2). When all of a unit's spikes are drawn (m = N), the two factors cancel and nothing changes.

With both weighting and scaling:

Recording Cap (spikes per unit) Isolation distance L-ratio
Neuropixels 500 (default) 1.7 % 8.2 %
same 20,000 0.1 % 0.4 %
Tetrodes 500 1.8 % 5.2 %
same 20,000 0.3 % 0.7 %

And of course this is just for a fixed number of features. The scaling becomes more important as the number of features grows.

Ways to fix
  1. The aforementioned weighting plus scaling.
  2. 1, plus also increase the default cap values from 500 to something that brings the median error down to an acceptable level. What is considered acceptable is TBD. Maybe a fixed percentage on some test or synthetic data. My preliminary results suggest that for a median isolation distance error of 1% at 45 features (SI default), ~1,100 spikes could suffice. But L-ratio will need more (on Neuropixels at 45 features, ~4,700 spikes for a median error of 2% and ~8,800 for 1%). Likely less than 20,000. The additional compute time required by drawing more spikes is easy to swallow. The additional memory footprint will hurt more. For ~60 tetrode units, the waveforms take 1.7 GB at 20,000 spikes vs 42 MB at 500, which is still within reach for an 8 GB machine and safe for a 16 GB machine. Neuropixels is heavier: our analyzer's waveforms take about 3.2 kB per spike (9 channels per unit), so 300 units at 20,000 spikes each would need roughly 20 GB, and about 5 GB at 5,000. Still, you can't put a price on correctness.
  3. Computing the metrics on run_for_all_spikes projections. This is what I'll be doing, even for my long recordings. On my 48 h tetrode recording (147 M spikes, 20 features), projecting every spike took 5.5 min with 48 jobs and 11.8 GB on disk.

The nearest-neighbor metrics would need their own fix. I haven't got to that yet.

My vote would be to implement option 2 but to make option 3 the default. It's the most correct, least surprising, most stable long-term, and usually tractable. Then, if a user finds that it is too slow and memory heavy for them, they can opt in to option 2.

@alejoe91 @samuelgarcia @chrishalcrow @JoeZiminski @h-mayorquin Thoughts?

[^1]: Another important difference: bombcell uses 3 Kilosort PCs on the unit's 4 nearest channels, whereas SpikeInterface's default features are different (45 features on Neuropixels data), which also changes both metrics. So the same threshold values (e.g. isolation distance > 20, L-ratio < 0.3, which SpikeInterface's bombcell_label_units applies by default) mean different things depending on who computed them.
[^2]: The Neuropixels numbers in this table and the next use each neighbor's own features. We currently fill zeros where a neighbor's sparsity lacks a channel, which is a separate problem.
[^3]: At m = 500 that factor is 1.04 for the tetrodes' 20 features and 1.10 for the 45 on Neuropixels.

Lingua principale
Python
Stelle
883
Fork
291
Merge medio
3g 17h
PR unite (30g)
40

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SpikeInterface/spikeinterface

Tutte le issue di SpikeInterface/spikeinterface

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.