Repository navigation
Fix run_for_all_spikes units and segments after the first - #4925
Conversation
run_for_all_spikes read traces without return_in_uV, so on a recording with a gain other than 1 it projected raw traces onto a PCA model fitted on waveforms in uV. It also only used the spikes of segment 0: chunks of later segments projected their traces at segment 0's spike times into segment 0's rows, and spikes of later segments were left at zero.
for more information, see https://pre-commit.ci
| worker_ctx["segment_slices"] = [ | ||
| np.searchsorted(spikes["segment_index"], [segment_index, segment_index + 1]) | ||
| for segment_index in range(sorting.get_num_segments()) | ||
| ] |
There was a problem hiding this comment.
this should use the cache:
| worker_ctx["segment_slices"] = [ | |
| np.searchsorted(spikes["segment_index"], [segment_index, segment_index + 1]) | |
| for segment_index in range(sorting.get_num_segments()) | |
| ] | |
| worker_ctx["segment_slices"] = sorting._cached_time_ordered["segment_slices"] |
Since ww called to_spike_vector, the cache is built!
alejoe91
left a comment
There was a problem hiding this comment.
Thanks for spotting this @grahamfindlay
Can you use the sorting cache for segment slices?
You're welcome! Done, but a little differently than suggested: I tried |
Context
I was investigating whether the L-ratio and isolation distance that SpikeInterface computes from its random-spike draw are good estimates of the textbook values (I think they are not, but that's a separate issue). In doing so, I was trying to keep the PCA fit out of the comparison, so I was fitting one PCA model and using
run_for_all_spikesto project a whole recording's spikes, and then randomly drawing subsets of those projections. That only works ifrun_for_all_spikesreproduces the analyzer's storedpca_projectionat the random spikes, so I checked and was surprised to find that it didn't. Further investigation revealed a pair of bugs...The bugs
ComputePrincipalComponents.run_for_all_spikes(used byexport_to_phyto writepc_features.npy) had two problems:Units. _all_pc_extractor_chunkcalledget_traces(...)withoutreturn_in_uV. The PCA model was fitted on waveforms extracted with the analyzer'sreturn_in_uV(True by default). If a recording's gain was not 1, it was projecting unscaled traces onto a model fitted in uV.Segments. _init_work_all_pc_extractorkeeps only segment 0's spikes, but the chunk executor runs over every segment. Chunks of later segments project their traces at segment 0's spike times and overwrite segment 0's rows, and spikes of later segments stay at zero.export_to_phyasserts one segment, so only direct calls are affected. Probably nobody ever hit this bug.Fixes
return_in_uVto the worker and toget_traces, so traces are in the units the PCA model was fitted on.test_compute_for_all_spikesrun on a copy of the 2-segment test recording with a gain and offset, and check that the all-spike projections at the random spikes equal the storedpca_projection.