Abstract
We report the existence of a working memory system in the nematode C. elegans that is employed for deferred action in a sensory-guided decision-making process. We find that the turn direction of discrete reorientations during navigation is under sensory-guided control and relies on a working memory that can persist over an intervening behavioral sequence. This memory system is implemented by the phasic interaction of two distributed oscillatory dynamical components. The interaction of oscillatory neural ensembles may be a conserved primitive of cognition across the animal kingdom.
Main
Distributed brain oscillations and their interactions have long been hypothesized to be a fundamental building block of neural computation and cognitive function in complex-brained animals (Buszaki, 2006). Considerable theoretical work has been devoted to understanding how these observed phenomena might implement particular cognitive functions (Engel et al., 2001). However, experimental establishment of causal roles and mechanistic understanding of proposed functional implementations have been elusive, due at least in part to the sheer complexity of the networks comprising these brains as well as the challenge of observing these networks at a high unit sampling density. By contrast, in simpler-brained animals with vastly lower neuron counts, oscillators serving to produce repetitive bodily movement, i.e. central pattern generators, have been closely studied and systematically dissected, often yielding deep mechanistic insight into the production of adaptive but robust rhythmic motor patterns (Harris-Warrick, 1992). But the general cognitive function of sensory-driven discrete decision-making has been rarely studied in simple animals (Briggman et al., 2005). Intuitively, even simple organisms stand to gain from the ability to execute a canonical decisional process: collecting sensory evidence, integrating that evidence with internal state, and executing a contingent action at a later time.
The 1 millimeter long 302-neuron nematode C. elegans crawls on its right or left side when on a flat surface. During foraging, the worm crawls in a straight or curved forward direction and punctuates bouts of forward crawling with discrete reversal-then-turn reorientation maneuvers, either in the dorsal or ventral direction (Fig. 1a). The choice of this direction has been traditionally modeled as an unregulated random process, akin to the randomizing tumbles of a bacterium performing biased random-walk chemotaxis (Pierce, 1999). In this study, we show that the dorsal-ventral turn decision, rather than being random, can be strongly biased by sensory stimulation.
To test whether the final turn direction was informed by sensory experience prior to the reversal, we used closed-loop optogenetic stimulation to simulate an asymmetric odor signal from the worm’s frame of reference. We repeatedly stimulated the AWA sensory neuron, known to mediate chemotaxis to certain attractants, such as diacetyl, via pulsatile activity responses (Itskovits et al., 2018), triggered on either ventral or dorsal head-swings during forward motion (Fig. 1a). Animals showed a strong preference for resolving the reversal with a turn in the favored direction as they resumed forward locomotion (Fig. 1b).
This observation suggests two key features of the sensory control of the behavior. First, to assign the stimulus to a particular direction, the animal needs to integrate sensation with proprioception or motor commands for head swings. Second, to act appropriately after the reversal, the animal needs some form of working memory (Fig. 1c).
To study the neural dynamics that coordinate this process, we recapitulated our headswing-timed closed-loop stimulation paradigm using volumetric confocal microscopy of an immobilized, paralyzed worm in a microfluidic chip. In this setup, we can continuously read out the whole brain activity of the animal at single-neuron resolution, extract detailed neuronal activity in real time using machine vision, and optogenetically stimulate selected neurons with respect to ongoing patterns of activity (Figs. 1d,e).
In our immobilized whole-brain imaging setup, in all trials, we observed cycles of the fictive command state sequence (forward-backward-turn) widely distributed across neurons. To distinguish between the observed behavior of the worm and the fictive command-state cycles which have been previously ascribed to neural dynamics in restrained worms (Kato et al., 2015, Fig. 1a), we abbreviate each state: forward (FWD), reversal (REV), and dorsal/ventral (DOR/VEN) turn (TURN). As reported previously, the first principal component of the whole brain recording reliably reveals this command-state-reflecting dynamical process, consisting of cycles of forward and reversal periods, which we term PC1 (Kato et al., 2015). During forward locomotion, head swings are especially pronounced. We therefore performed PCA on the residuals of PC1, restricting to periods for fictive forward locomotion only, which yielded a second strong stereotyped network oscillation (Fig. 1f), which we termed PC_DV. We found this faster oscillating component to be distributed across 6-10 neurons in the head of the animal that are variously implicated in motor control of the head muscles, sensorimotor integration, and proprioception (Gray et al., 2005; Yeon et al., 2018; Fig 1g).
The neuron class we observed with highest PC_DV loading was SMDV, and its contralateral neuron class SMDD has a large negative loading (Fig. 1g). Previous studies have shown SMDV reliably correlates with ventral head curvature during forward crawling and ventral post-reversal turns; SMDD correlates with dorsal turns (Hendricks et al., 2012; Kato et al., 2015). Here, their anti-phasic coupling during FWD suggests their activity corresponds to fictive head swings consistent with sinusoidal crawling during the forward command state.
During reverse locomotion, the head oscillations present during forward crawling are suppressed (Alkema et al., 2005). During REV epochs, the PC_DV component continues to display oscillatory behavior; however, the magnitude is typically attenuated and the regularity of the oscillation period and waveform is reduced (see example worm, Fig. 1g,h). Toward the end of a reversal epoch, the magnitude of the PC_DV oscillation typically increases over the course of 1-2 oscillations, exhibited in SMDD/V as well as other PC_DV neurons. As such, the magnitude of PC_DV stratifies neural trajectories of fictive post-reversal turns in the low-dimensional state space projection (see example worm, Fig. 1h), delineating the worm’s choice of fictive post-reversal turn direction. We surmise that the PC_DV oscillation decouples from motor output during reversals but does not disappear, and recouples leading up to a reversal termination in order to effect the post-reversal turn.
Distributed neural correlates of working memory
In order to test the hypothesis that sensory information that enters the system at a specific phase of the head swing during forward locomotion and is held in working memory to inform deferred behavioral choice, we combined whole-brain imaging with perturbative optogenetic stimulations during the FWD command state (Fig. 2a-c). Each 16-24 minute recording captured 10-30 forward-reversal-turn sequences. Stimulation of AWA shifted the distribution of times until the next REV (Fig. 2d) compared to a control distribution computed for spontaneous REV transitions. Using a trained decoding framework fit on individual stimulation trials (see Methods), we found that the dorsal/ventral identity of fictive post-reversal turns could be predicted by AWA optogenetic stimulation when combined with phase information of PC_DV at the time of stimulation (Fig. 2e,f). This confirms the existence of a functioning working memory in our immobilized setup, lasting up to tens of seconds during fictive reversal periods (Fig. 2g), as distributions of reversal times are typically longer in immobilization chips (Kato et al., 2015). The persistence of this working memory capability during immobilization and muscle paralysis demonstrates that the memory is carried by an internal state, rather than being embodied in body posture or muscle activation.
We hypothesized that the sensory memory is stored as a change in the levels of certain neurons, either absolute or relative to other neuron level changes. However, using our decoding framework, we were unable to find any neurons whose static or monotonic activity levels were predictive of turn identity. We then asked if the sensory memory is stored as a change in the amplitude of oscillating neurons. While we did observe shifting imbalances of oscillation amplitude in SMDV versus SMDD in some recordings, these imbalances were not predictive of the turn identity until roughly one oscillation just prior to the post-reversal turn (Fig. 3j), suggesting that the difference in oscillation amplitude of these neurons does not provide the working memory through the early period of reversal when PC_DV is attenuated. In any case, purely observational studies of neural activity do not discriminate causal activity from epiphenomenal “ride-along” activity.
Therefore, to better assess the causal role of particular neurons in holding a sensory memory or driving turn identity, we turned to selectively stimulating non-sensory neurons during whole-brain imaging, at particular times relative to the PC1 (command state) transitions as revealed by whole-brain activity. We started by stimulating SMDD/V neuron classes given that they signal turn choice. The opposing phase of the SMDD and SMDV class neurons and role naturally suggested a hypothesis that the selective activation of either opposing class leading up to reversal termination would influence the identity of the post-reversal turn. We used a digital micromirror device to spatially restrict our laser stimulation to selectively stimulate either neuron class, and we timed stimulations to occur during fictive reversals (Fig. 3a,b). Surprisingly, selective optogenetic stimulation of the ventral or dorsal class of neurons, e.g. SMD neurons, though demonstrably hyperpolarizing the neurons (Fig. 3c), did not affect the resulting turn identity (Fig. 3d; aggregate data of PC_DV neuron stimulation, see below). This suggests that the activity of SMD neurons, while clearly signaling the turn identity, are not the locus of the memory trace initially established upon sensory-proprioceptive integration. Interestingly, despite the inability to bias turn identity, stimulation of either SMD neuron class elicited immediate reversal terminations (Fig. 3e,f).
In search of other neurons that may encode future turn identity, we performed reversal-timed stimulations of other nonsensory head neurons RIA and RIV. Previous studies implicated RIA in sensory-proprioceptive integration; it exhibits compartmentalized calcium dynamics along the nrV and nrD regions of its nerve ring neurite, which tightly correlate with SMDV and SMDD activation during ventral and dorsal head bending, respectively (Hendricks et al. 2012). RIV has functional overlap with SMDV (Gray et al. 2005), however it is typically thought of as a motor neuron due to its synaptic connectivity (Witvliet et al. 2020). We found that spatially localized optogenetic stimulations of subcompartments of RIA, and as well as RIV, were capable of immediately terminating reversals upon stimulation (Fig. 3g-i). This substantiates the distributed nature of the PC_DV oscillation, not just in the display of shared activity but in influence on behavior.
Interestingly, the capability of RIA subcompartments to terminate fictive reversal was gated by the instantaneous activity of PC1 at the time of stimulation on a per-trial basis (Fig. 3k,l), suggesting that some time-varying process during reversal command state dictates REV termination by PC_DV neurons. Focusing next on stimulations that forced an output from the emerging D/V decision process by terminating reversal, we found that the activity level of PC_DV, or individual neurons with high PC_DV loadings (Fig. 1g), was predictive of the turn (Fig. 3j). We suspect the non-monotonic time-course of predictivity is a consequence of oscillatory dynamics in these neurons; PC_DV captures both DOR and VEN depolarizations, whereas individual PC_DV neurons only show turn-direction-dependent differential activity during half of the oscillation cycle, enabling better classification. The observation that both PC1 gating and D/V predictability diminish rapidly in models fit on timepoints prior to stimulation suggests that the timescale of PC1 gating and PC_DV recoupling may be coordinated.
Evidence for phase-based memory
To store a value reliably using the phase of an oscillator over several oscillation periods, we can envision two possible scenarios: (1) the oscillator must maintain a stable frequency and there must be another temporally stable process to function as a reference clock, such as a fixed-timescale ramp-to-threshold process or a slower oscillator with a period that is a near multiple of the first oscillator; (2) in the case of a variable oscillator, the oscillator must be coordinated dynamically with a process capable of maintaining a relative phase over oscillation cycles with jointly varying period. Given the aforementioned observed correlation of sensory stimulus phase and reversal onset phase, we surmised that the second temporal process, in either scenario, would reside in PC1 or some of its constituent neurons. Similar scenarios have been argued to be at play in mammalian working memory (Lisman et al., 2013).
Upon closer study of trials when we stimulated AWA during forward command states, we discovered a correlation between the phase of PC_DV oscillations at the time of the AWA stimulation pulse and the phase of PC_DV oscillations at the time of the subsequent PC1 reversal onset (Fig. 2h). Reversal command state transitions after AWA stimulation do not appear to be triggered immediately, in comparison to RIA/SMD/RIV evoked reversal terminations, which occur within seconds (Fig. 2d). Furthermore, the second peak in the distribution of time to initiation of REV (∼30s) for AWA stimulations roughly corresponds to one typical period of PC_DV oscillation (Fig. 1f, 2d). Therefore, our sensory stimulus triggers the onset of a fictive reversal not only in the near future, but also one oscillation cycle in the future, preserving the phase relationship between stimulation and internal representation of the proprioceptive state. Additionally, when paired with measurement of AWA activity at stimulation, the phase of PC_DV is predictive for turn direction on an individual trial basis (Fig. 2i), demonstrating that the PC_DV phase at the onset of fictive reversal is informative for prediction of turn choice but only following successful AWA stimulation. This suggests the possibility that the setting of the relative phase of PC_DV and PC1 may in fact be the mechanism for storing the memory of the head direction of a salient AWA stimulus.
To understand how the phase of PC_DV at (i) stimulation, (ii) REV onset, and (iii) during reversal interacts with PC1-mediated gating of reversal command state termination, we conducted additional analyses on the coordination between PC_DV phase and PC1 phase.
Integration of sensory-decision drive and proprioceptive drive for reorientations
One interpretation of the ability of proprioceptive neurons to immediately terminate fictive reversals is that they signal the successful execution of physical bending that produces reverse movement. In addition to their role in reorientations during foraging, reverse crawling is used by C. elegans to escape predatory fungal traps and noxious stimuli (Maguire et al., 2011; Kaplan and Horvitz, 1993). Escape reversal bouts can be longer and may partially explain the increase in long REV epochs observed in the chip. We wondered whether a proprioceptive reverse termination signal would interfere with the putative role of PC1 as a reliable clock supporting phase-based memory.
Returning to a decoding framework, we found that the magnitude of PC1, in the negative direction, was a predictor of successful reversal terminations initiated by RIA (nrV or nrD) stimulation (Fig. 3k). As PC1 decays, the system becomes permissive for proprioception-driven reversal termination. Additionally, individual neuron predictivity was positively correlated with PC1 loading (Fig. 3l). This suggests that the decay of the activity of PC1 neurons with positive PC1 loading such as OLQ, URX, or the ramping of PC1 neurons with negative loading such as RME, could potentially function as timers, possibly in conjunction with other sensory or motor roles previously implicated (Fig 4a) (Kaplan and Horvitz, 1993; Hart et al. 1995; Zimmer et al., 2009). Interestingly, the backward command neuron AVA, which has the largest PC1 loading of all neurons and is required for communication of the command state to the downstream body motor system that controls body posture (Kato et al., 2015; Pokala et al., 2014), was not a predictor of induced reversal terminations, suggesting it may not have a casual role in initiating reversal terminations. AVA’s relationship to PC1 dynamics as an output to downstream motor systems or muscles may be analogous to SMD’s relationship to PC_DV dynamics.
To test the hypothesis of dynamical coordination between PC1 and PC_DV, we first fit an exponential to PC1 during reversal command state (Fig. 4a) and found that the time constant of PC1 ramping is correlated with reversal duration (Fig. 4b), the timescale over which the memory must be maintained. We then examined whether the period of PC_DV oscillation is embedded within the longer period of PC1 oscillation (Fig. 4c). The distribution of the PC1 period appears to accumulate at integer multiples of the nested PC_DV phase, consistent with prior reports of the nesting of SMD oscillations and forward and backward command states (Kaplan et al. 2020). Combined with perturbative experiments (Fig. 3), we conclude that within a reversal epoch, the PC1-gated decoupling and recoupling of PC_DV from motor output during reversal are aligned to the phase of PC_DV.
Discussion
In summary, in our virtual-reality experimental paradigm (Fig. 1) we activate sensory neurons with pulses timed with respect to head bending or its neural correlate (Fig. 2), mimicking spatially asymmetric odor input with respect to the worm’s frame of reference. These stimuli trigger reversals aligned to the phase of proprioceptive dynamics (Fig. 3), which we speculate become uncoupled during reversal to allow for oscillatory dynamics to propagate sensory information forward in time and are then recoupled to behavioral output by coordination with a gating process present in PC1 dynamics (Fig. 3,4). Our experiments argue against the encoding of working memory in static neural activity levels for the sensory-guided decision-making capability we study here. They instead suggest that this working memory is implemented by the maintenance of the relative phase of two coordinated dynamical units distributed across many neurons.
The accuracy of the prediction response to a past stimulus (Fig. 2 e,i; Fig. 3 j,k) and the noise of the measurements of phase relationships and spread of state space trajectories imply the presence of uncontrolled variability. If neural responses were more reliable, perturbative experiments would be likely to be more effective and efficient in dissecting mechanisms. We might drive more reliable responses by exploring closed-loop stimulation parameters that generate more comprehensive or precise control of the sensorium as well as the organismal state of the animal. Previous work has found that downstream neural responses are more reliable when AWA optogenetic stimulation is paired with optogenetic stimulation of AWC, another sensory neuron that plays a key role in attractant chemotaxis (Dobosiewicz et al., 2019).
While no neurons in our recordings appeared to explicitly encode, across the intervening reversal period, future dorsal/ventral turns by way of modulating their stable activity level or in their amplitude of fluctuations, we cannot rule out the existence of neurons that possess sub-cellularly localized calcium fluctuations that do not reach the nuclear-localized calcium sensor, or that have an intracellular calcium concentration outside the dynamic range of our sensor, or that do not show activity via our pan-neuronal activity indicator, for technical reasons such as low genetic expression levels. We also cannot rule out the possibility that other non-neural activity internal signals, such as neuropeptide concentration or the facilitation of a particular set of synapses, carry the dorsal/ventral sensory memory. However, given the evidence of a causal role of relative phase in our perturbative studies, we suggest that phase encoding carries the early memory trace for this working memory system.
We surmise that the original function of the head neural oscillator was to produce physical movement, and the motor-decoupled mode of operation only arose later to support the function of working memory, endowing the animal the ability to perform deferred sensory-guided action selection. This evolutionary step, from embodied to internal neural oscillations, may represent the origin of a functional primitive of cognition subsequently recruited to do far more complex forms of thinking.
Methods
Strain list
Behavior imaging and quantification
Worms were prepared 24 hours in advance of imaging: L4 worms were picked onto fresh NGM agar plates with an OP50 lawn. Immediately before imaging, worms were picked onto a fresh NGM agar plate without food, into a drop of M9.
Single worms were aspirated out of that drop and washed 3x by aspirating into new drops of M9. Finally, the worm was aspirated onto an NGM agar plate without food, with care taken to re-aspirate excess M9. Worms were imaged while crawling on the surface of the agar without lid for 1-2 recordings of 12 minutes each. Data were acquired on a Leica M205 with TL5000 base. Images were captured on an ORCA-FLASH V3 set to 100 ms exposures. Image FOV was set to 4.78mm. Worms were automatically tracked over the course of an experiment with the microscope’s motorized stage centered on the worm’s centroid.
For experiments involving optogenetics, care was taken to reduce activation of the optogenetic channel from brightfield illumination light. About 24 hours prior to imaging, 80ul of 100uM all-trans retinal in M9 was added to the bacteria lawn of L4s. Worms were then placed in foil at 20 C until imaging. Microscope illumination was set to 2. During an imaging session, worms were partially covered if not being immediately prepared for an experiment. We used a Leica LED3 set to 100% for the optogenetic illumination source. We automatically triggered optogenetic stimulation to positive (clockwise) or negative (counterclockwise) head displacements using a threshold set by the operator during recording and switched between negative and positive midway through each imaging session. Real-time estimation of head displacement was computed using software incorporating worm posture extraction routines from Tierpsy Tracker (Javer et al., 2018).
We quantified the recording videos using Tierpsy Tracker to identify reversals, then for each reversal, we took the difference of the inbound angle of the preceding forward run and the outbound angle of the following forward run, as tracked by the automatic centroid tracker.
Whole-brain Ca2+ imaging of C. elegans
Two-layer PDMS microfluidic devices were manufactured as previously described (Zimmer et al., 2009, Cáceres et al., 2012). Worms were prepared 24 hours in advance of imaging: L4 worms were picked on to fresh NGM agar plates with an OP50 lawn. Immediately prior to imaging, worms were picked onto fresh NGM agar plates without food, and placed in a drop of M9 solution with 5mM tetramisole. Worms were left in tetramisole for 10 minutes prior to being aspirated into the microfluidic channel. Worms were imaged for 16-24 minutes. Data were acquired on an inverted spinning disk confocal microscope (Leica DMi8 with Yokogawa CSU-W1 SoRA spinning disk) equipped with a sCMOS camera (Telydyne Photometrics Kinnetix). About half of recordings were acquired at 2×2 camera binning. The microscope objective lens was 40x 1.25NA WI. Sample Volumetric scans were performed using a piezo stage with 10-12 Z-planes with z-spacing 2.5-3um. In select recordings, 4 Z-planes with z-spacing 3um were used to measure neurons in lateralized anterior, lateral, dorsal, and ventral gangla at higher temporal resolution under equivalent optical conditions. Prior to acquiring videos for calcium timeseries, a reference high-resolution structural image was acquired: 40×1um Z planes under 4 different optical conditions to measure NeuroPAL fluorescence (see Yemini et al. 2021). Exposures ranged from 30ms-80ms under 100-200uW illumination to minimize photobleaching.
For experiments involving optogenetics, care was taken to minimize activation of the optogenetic channel from the blue light illumination for measuring GCaMP fluorescence. About 24 hours prior to imaging, 80ul of 1uM ATR in M9 was added to the bacterial lawn of L4s. Worms were then placed in foil at 20 C until imaging. During an imaging session, worms were partially covered if not being immediately prepared for an experiment. Worms were imaged under 70-140uW of light at the sample, for 20-30 ms exposures within an 80 ms duty cycle - 20ms-30ms of light exposure, 50-60ms of blank time. The optogenetic illumination light source was an 89NorthLDI-7 laser diode at 640 nm, powered to 10-20% of maximum illumination intensity. This illumination was directed by a digital micromirror device (DMD, Mightex Polygon P1000). Illumination was brightfield for worms with optogenetic construct expressed under a single-neuron promoter, and localized to about 5×5um for localized illumination of optogenetic constructs driven by multi-neuronal promoters.
Closed-Loop Experimental Framework (CLEF)
Closed-loop acquisition of microscopy images and delivery of perturbative stimulation were achieved via Closed-Loop Experimental Framework (CLEF), a software system we developed to enable this study. Briefly, this system provides coordination between, and interfaces for, input (from microscopy devices), control (actuation/stimulation), storage (filesystem/network) and compute (computational models). CLEF interacts with microscope components via Pycro-Manager (Pinkard et al., 2021) and Micro-Manager (Edelstein et al., 2010).
Region of interest (ROI) detection in volumetric Ca2+ imaging data
ROI detection from neural timeseries videos was adapted from Kato et al. 2012, implemented by the Napari (Chiu and Clack, 2022b) eats-worm plugin. Briefly, interframe motion was first registered using manual tracking (Schindelin et al. 2012). A reference ROI movie was then generated composed of each image plane by averaging successive blocks of 20-200 movie frames to reduce noise. Each frame of the reference was adaptively thresholded based on median image brightness, median filtered, then convolved with a gaussian kernel. Local maxima were found and merged if peaks were adjacent within a greedy threshold. For each ROI center, a surrounding region with radius 5-7 was defined, with overlapping adjacent regions excluded via Voronoi tessellation with area shrinkage of 0.5 pixels. ROIs in adjacent timepoints were linked via local greedy matching. Cells below detection threshold were extrapolated based on the motion of neighboring ROIs. Finally, time-varying multi-plane ROIs were adjoined based on overlap. Each neuron was manually verified by R.L.D. For high temporal resolution half-brain imaging experiments, images were compressed along the Z axis via maximum intensity projection, becoming 3D ROIs. Neural time series extraction was adapted from Kato et al., 2015. Briefly, for each 4D ROI, a single-cell fluorescence intensity was computed taking the average of the brightest 30-60 voxels at every time point after subtracting z-plane-specific background intensity. Background values were computed by averaging pixels not belonging to any ROI within a radius of 21 pixels. DF/F0 was computed for each neuron, with F0 as mean background fluorescence.
Identification of neuron genetic identities
In each recording, we detected 45-140 neurons. Neurons were identified by assessing their anatomical position, relationship to surrounding neurons, and their established activity patterns. Furthermore, the strains used for experiments here express the NeuroPAL genetic cassette (Yemini et al., 2021), which uses a genetically defined combination of 4 fluorophores to discriminate neurons based on multi-color reporter expression. Neuron labels were assigned by hand, using NeuroPAL documentation for guidance. In many cases ambiguity still existed, so here we opted for a more conservative approach and chose not to ID neurons which could not be identified beyond a reasonable doubt, and in Fig. 1e ambiguous identities are denoted in parentheses.
Neural timeseries derivatives and embeddings
Derivatives and PCA on neural time series data was performed as previously described (Chartrand, 2011, Kato et al., 2015). Specifically, total-variation regularization was used to compute de-noised time derivatives while resolving the accuracy of command state transitions to single frames. Numerical differentiation helped to mitigate the effects of drift on the neural signal, improving subsequent analyses. This approach was also used for the calculation of PC_DV. In more detail, for calculating PC_DV, we first subtracted the sums of projections to the derivative timeseries onto temporal PC1. Next, we subsegmented forward command states and removed candidate forward states inconsistent with sustained forward run defined by a threshold (<4s). The resulting segments were concatenated for PCA (Jolliffe 2002) to define loadings. Full timeseries were then projected onto these loadings. Prior to PCA, timeseries were detrended and regularized. For comparison across animals, we applied PC matching by inverting projections such that genetically identified neuron SMDV would be negative. In some cases, such as a large number of neurons drifting out of the focal plane contributing high variance, this procedure was adjusted by first filtering neurons with thresholded (0.5) normalized covariance to SMDV or by further subsegmenting the beginning, or end, of the full timeseries, i.e. temporal cropping. Behavioral decoding of whole-brain recordings was performed as previously described (Kato et al. 2015).
Linear predictive modeling framework
For predictive modeling in Fig. 2-3, we fit linear models on single trials, predicting future command states, specifically TURN choice or REV termination. We used linear SVM classifiers implemented by scikit-learn (Pedregosa et al., 2011) with 5-fold cross validation to limit overfitting. To account for class balance, we report the balanced accuracy metric, which is equivalent to accuracy score with class-balanced sample weights. For timepoint predictive analysis (Fig. 3j,k), separate models were fit on neural activity (DFF and derivative as independent features) at single timepoints relative to stimulation.
Statistical tests
In Figs. 2d and 3e-i, the significance of the effect of neuron stimulation on time to reversal was assessed using a Kolmogorov-Smirnov test. The comparable control distribution must account for spontaneous command state transitions unaffected by stimulation. To calculate control null distribution, first we take unstimulated states from control worms and compute the marginal distribution of time until state transition. We then accumulate shifted state durations selected from our experimental group, based on time from stimulation, to generate the marginal distribution of time until state transition. The circular correlation coefficient PC_DV phase at stim onset and PC_DV phase at reversal onset (Fig. 2h) was calculated using astropy.stats.circcorrcoef (Price-Whelan et al., 2022). Significance was assessed using permutation testing. Non-significance of stimulation of dorsal and ventral turn neurons on turn bias in Fig. 3d was assessed using a chi-squared test.
PC1 ramping and phase analysis
The time constant of PC1 ramping was calculated by fitting a saturating exponential to PC1 traces scaled to each reversal. PC1 and PC_DV phase were calculated by smoothing both traces with a gaussian filter, identifying extrema with scipy.signal.find_peaks (Pauli, 2020), and linearly interpolating between peaks and valleys. The ratio of PC1 to PC_DV period was calculated using the inter-peak intervals of each signal for the corresponding cycles.
Author contributions
R.L.D. and S.K. designed and interpreted experiments. R.L.D., C.C., and S.K. developed analytical methods. J.M. generated transgenic strains. R.LD., G.C, and D.S. performed behavioral experiments. R.L.D. performed neural imaging experiments. R.L.D. and J.B. developed quantification methods. R.L.D. and S.K. wrote the manuscript.
Acknowledgements
We thank Rex Kerr for reading the manuscript. We thank the Innovation Core at the UCSF Weill Institute for Neurosciences and the Center for Advanced Light Microscopy at UCSF for microscopy resources and expertise. Some worm strains were provided by the CGC, which is funded by NIH Office of Research Infrastructure Programs (P40 OD010440). Research funding was provided by National Institutes of Health, specifically R35GM124735 (S.K), R01DC005991 (N.D.L), R01NS087544 (N.D.L.), and F31NS115572 R.L.D), the Weill Institute for Neurosciences, and the Weill Neurohub. The eats-worm software (J.B.) is supported by a napari Plugin Foundation grant from the Chan Zuckerberg Initiative.