-
Notifications
You must be signed in to change notification settings - Fork 272
3. More on drift correction
Here we get into the details of drift: why it happens, what it looks like, how you can characterize it for your own recordings, and how you can tell if Kilosort2 is fixing it. Finally, we go over several examples of drift collected from acute Neuropixels 1.0 recordings in head-fixed mice.
Some amount of drift is unavoidable: the brain floats inside the skull, and moves when the animal moves on fast timescales, while on slow timescales physiological changes happen, some of which may be induced by the presence of the probe itself. Thus, there are two main types of drift, which we treat differently: slow (10s of minutes) and fast (10s of seconds). Slow drift is not a huge problem if the recording is short (<10 minutes). Fast drift is not a huge problem if the animal is not behaving. However, neither is true in a typical neuroscience experiment: the animal is performing a task which involves some motor actions, and it takes at least 30 minutes to characterize the neural activity. In a typical recording of 1-2 hours, the amount of slow and fast drift we'd expect is comparable, and can be anywhere from 5 to 20um and more if your preparation is unstable.
To recognize drift, look for changes in spike amplitude over time, or changes in feature amplitudes, where the feature can be the projection on the principal components of a channel. More dramatically, a cluster may simply "drop out" and its spikes lost when drift is too large. See the two examples below of slow and fast drift that Kilosort2 tracked, as well as a third cluster that was lost halfway through the recording.
Slow drift is generally easier to recognize and fix. A well-known version of slow drift happens after probe insertion in an acute experiment, which is why it is typical to wait 20-30 minutes for the tissue to relax before recording. However, even after that initial phase, there is a smaller amplitude, slow timescale drift that continues for at least a few hours, and has been reported to us even in chronic implants. Cumulative over time, slow drift can have a significant impact on spike sorting.
Fast drift is harder to diagnose but potentially more dangerous, because it may introduce behavior-dependent biases into the data. The bias may be produced if every time an animal performs a certain motor action, the probe moves a little, in which case the spikes from a small neuron may be completely lost. It will then appear as if this neuron was inhibited by movement. Conversely, a neuron may only come into the range of the electrodes during the movement, in which case it will appear as if that neuron is activated by movement. This behavior also makes fast drift harder to diagnose, because most neurons in the brain genuinely have movement-related spiking activity.
The main way to diagnose drift is, in fact, to run the first step of Kilosort2, which produces an interpretable picture of the spike waveform similarities between any two moments in the recording. To do this, Kilosort2 splits the data into batches of size ops.NT, and for each batch performs a full spike sorting routine. This summarizes the spikes in that batch with a small (Nchan/2) set of templates, which we then compare with the templates in every other batch. The final result of this procedure is a large Nbatches by Nbatches matrix of similarities between batches. If the similarity between batches is high, that indicates little movement. If the templates vary a lot over batches, and in a structured manner, that is likely to indicate drift.
The spike sorting routine for each batch is a very fast, lite clustering algorithm, where the spikes are first detected as threshold crossings in PCA space, their PCA features are then extracted and the spikes are clustered with scaled k-means. To get enough information from a single batch, there should be many spikes inside that time interval. The default time interval is 2s, which may be too short for <32 channels, in which case we recommend increasing the batch size.
Kilosort2 does not explicitly fix drift. However, it takes advantage of its knowledge of drift to perform better spike sorting. The simplest situation is where only slow drift happens in a recording. In this case, we could just initialize the templates at one end of the recording, and then sweep the recording in time, adjusting the templates to keep track of the slightly-changing waveforms of each neuron.
But what do we do if fast drift happens all of a sudden? We could reduce our timescale of tracking, but that strategy is brittle: a single mistake during a very fast drift and we risk switching tracks from one neuron to an adjacent one with similar waveform but offset in the direction of the drift. This problem is further compounded for low firing rate neurons, which may only fire a few spikes during an entire fast drift period.
The solution we found was to re-order the batches of data in time, so that consecutive batches represent similar absolute levels of drift. In other words, we wanted to order batches with similar drift positions close to each other, because that will ensure there is no single timepoint where a lot of drift happens very quickly. If the batch re-ordering is successful, we can then just go back to using our tracking strategy of slowly updating a cluster's template as we progress through the batches. Because the cluster changes waveform smoothly as a function drift position, tracking failures can be dramatically reduced this way.
To do this re-ordering, we needed to develop a travelling-salesman like algorithm....