Drift correction for electrophysiology and two-photon calcium imaging

Vertical drift (Z-drift) is a major confound for neural recordings during behavior. The brain floats in liquid, and movements of tens of microns can easily occur, even in head-fixed animals. In the mouse, for instance, postural changes such as locomotion can cause vertical brain movements of up to 20 microns. This displacement creates an apparent change in the activity of neurons recorded with either electrode arrays or two-photon calcium imaging. Here, we present methods to estimate and correct the drift in both optical and electrical recordings. We demonstrate three methods to recover Z-drift in 2-photon calcium imaging. (1) Alignment to a densely-scanned reference volume (z-stack). (2) Estimation from a non-functional channel- such as tdTomato expressed in a neuronal subclass. (3) Estimation from changes in the shape of identified cells in functional recordings. We validate methods 2 and 3 by comparing to method 1, which provides ground truth. We then develop correction methods that remove the effects of Z-drift, and show that correlations of neuronal activity with running are significantly decreased. Finally, we develop a convenient online module for drift correction that eliminates Z-drift at sub-micron resolution. Z-drift also affects electrophysiological recordings. The amplitude and shape of extracellular action potentials changes when the electrode moves relative to the brain, and neurons may even disappear altogether from the set of recorded channels. Fortunately, new electrodes such as Neuropixels have dense arrays of channels, with inter-site spacings as low as 20um. We found that we could estimate the drift in extracellular recordings with linear electrodes by tracking neuronal waveform shifts, and corrected for it by spatially interpolating the raw data prior to spike sorting. In summary, the algorithms presented here provide effective methods to remove Z-drift, a major confound for neural recordings during behavioral experiments. We provide the code as part of the Suite2p and Kilosort pipelines.