Functional clustering of dendritic activity during decision-making

The active properties of dendrites can support local nonlinear operations, but previous imaging and electrophysiological measurements have produced conflicting views regarding the prevalence and selectivity of local nonlinearities in vivo. We imaged calcium signals in pyramidal cell dendrites in the motor cortex of mice performing a tactile decision task. A custom microscope allowed us to image the soma and up to 300 μm of contiguous dendrite at 15 Hz, while resolving individual spines. New analysis methods were used to estimate the frequency and spatial scales of activity in dendritic branches and spines. The majority of dendritic calcium transients were coincident with global events. However, task-associated calcium signals in dendrites and spines were compartmentalized by dendritic branching and clustered within branches over approximately 10 μm. Diverse behavior-related signals were intermingled and distributed throughout the dendritic arbor, potentially supporting a large learning capacity in individual neurons.


Introduction
Neurons are bombarded by information from thousands of synaptic inputs, which are sculpted by the active properties of dendrites (Stuart and Spruston, 2015). The role of active dendrites in single-neuron computation remains unclear. Active membrane conductances may simply counteract location-dependent disparities and passive sublinearities across synapses, producing neurons that integrate input in a point-like, linear manner (Bernander et al., 1994;Cash and Yuste, 1999;Spencer and Kandel, 1961). Alternatively, active conductances may add nonlinear operations to the input-output transformation of neurons. This could involve a single nonlinear operation (Phillips et al., 2015;Takahashi et al., 2016;Ujfalussy et al., 2018) or many subunits performing independent nonlinear operations within dendritic branches (Poirazi et al., 2003;Polsky et al., 2004). These computations determine the capacity of individual neurons to store and process information (Poirazi and Mel, 2001). The relevant form of dendritic integration could be determined not only by dendritic structure and ion channel expression, but also by the spatiotemporal pattern of input to the dendrite under behaviorally relevant conditions Ujfalussy et al., 2018).
Active membrane conductances can support different types of regenerative events that impact dendritic integration of input. These event types vary in location of initiation, primary ionic conductance, dynamics, and extent of their spread within the dendrite (London and Häusser, 2005;Major et al., 2013;Spruston, 2008). All of these events are associated with calcium influx. Backpropagating action potentials (bAPs) can generate widespread calcium transients, dependent on firing patterns and synaptic input (Jaffe et al., 1992;Magee and Johnston, 1997;Spruston et al., 1995;Waters et al., 2003). Calcium spikes initiated in the apical tuft by the conjunction of bAPs and tuft depolarization (Kim and Connors, 1993;Larkum et al., 1999a) generate large and reliable multibranch calcium transients Xu et al., 2012). In contrast, local dendritic spikes can be initiated in individual thin branches and can be driven by electrogenesis produced by NMDA receptors (Branco and Häusser, 2011;Larkum et al., 2009;Major et al., 2008;Schiller et al., 2000;Wei et al., 2001) and other voltage-gated conductances (Ariav et al., 2003;Golding and Spruston, 1998;Losonczy and Magee, 2006;Milojkovic et al., 2005;Nevian et al., 2007). In isolation, a local dendritic spike generates calcium influx that is maximal near sites of coactive synaptic input within its dendrite of origin while calcium influx is minimal or absent at other dendrites (Major et al., 2008;Milojkovic et al., 2007;Schiller et al., 2000;Wei et al., 2001). Local dendritic spikes can be facilitated (Brandalise et al., 2016) or suppressed (Remy et al., 2009) by other regenerative events, potentially producing complex patterns of depolarization and calcium accumulation across the dendritic arbor.
The functional role of local dendritic spikes remains unclear. Some studies report a high prevalence of local dendritic spikes during sensory stimulation (Palmer et al., 2014;Smith et al., 2013), although the majority of local dendritic spikes were accompanied by somatic spikes (Palmer et al., 2014) and exhibited a sensory selectivity identical to the somatic spikes (Smith et al., 2013). Other studies failed to find local dendritic spikes during sensory stimulation Svoboda et al., 1999) or spontaneous activity (Hill et al., 2013).
Studies of local dendritic spikes during behavior have relied predominantly on calcium imaging. One study suggested that different behaviors trigger calcium transients in nonoverlapping tuft branches (Cichon and Gan, 2015), supporting independent local dendritic operations. However, another study found that calcium transients in the dendrites of CA1 neurons were almost always coincident with transients in the soma (Sheffield and Dombeck, 2015), implying that local dendritic spikes are rare or have a functional selectivity similar to the somatic output. Thus, the prevalence, independence, and selectivity of local dendritic activity during behavior remains uncertain.
Clustering of coactive inputs over small length scales could facilitate the generation of local dendritic spikes (Losonczy and Magee, 2006;Major et al., 2008;Polsky et al., 2009;Weber et al., 2016). Multiple in vivo studies have probed the selectivity of dendritic spine calcium signals in pyramidal neurons of primary sensory cortex; some of these studies support clustering of functionally similar inputs Wilson et al., 2016), while others do not (Chen et al., 2011;Jia et al., 2010;Varga et al., 2011). This discrepancy could reflect the details of the sensory features and species investigated; alternatively, differences in the methods used to disambiguate the contribution of bAPs, post-synaptic nonlinearities, and pre-synaptic input to spine calcium signals could complicate measurements of synaptic selectivity based on calcium imaging.
Here, we developed novel calcium imaging and analysis methods to estimate the spatial structure of activity in spines and dendrites while mice performed a decision-making task. Previous calcium imaging studies in dendrites during behavior have imaged short stretches of dendrite (Cichon and Gan, 2015;Sheffield and Dombeck, 2015), making it difficult to disambiguate global, branch-specific, or spine-specific activity. To address these issues, we constructed a custom random-access, high-resolution microscope that allowed us to simultaneously record calcium signals throughout a large part of the dendritic tree, while still resolving signals at the level of individual spines. We leverage these simultaneous recordings to correct for brain motion and estimate the spatial scales of dendritic activity, avoiding potential biases inherent in other measurements.
We imaged pyramidal neurons in the anterior lateral motor (ALM) cortex as mice performed a tactile decision-making task with three well-defined behavioral epochs: sensory sampling, planning (delay), and response (Guo et al., 2014a). Anterior lateral motor (ALM) cortex is critical for decision making and planned directional licking in rodents (Guo et al., 2014b;Li et al., 2016). Neurons within ALM (Chen et al., 2017;Guo et al., 2014b) and connected regions (Guo et al., 2017) exhibit diverse behavioral selectivity during a tactile decision task. We mapped the prevalence, selectivity, and organization of task-associated signals across the dendritic tree of individual neurons. We found that nearby spines and dendritic segments had similar behavioral selectivity, and that the branching structure of the dendritic tree compartmentalized task-associated calcium signals.

Results
High-resolution and large-scale dendritic calcium imaging during tactile decision-making We imaged calcium-dependent fluorescence changes ('calcium transients') in the dendrites of GCaMP6f-expressing neurons in the anterior lateral motor (ALM) cortex of mice performing a tactile delayed-response task (Guo et al., 2014b). To characterize the spatial organization of task-related signals within dendritic arbors, it was critical to image large parts of the dendrite with high spatial and temporal resolution. We therefore constructed a two-photon laser-scanning microscope ) that allows rapid (~15 Hz) imaging of the soma and contiguous dendrite in three dimensions, while resolving calcium transients in individual dendritic spines ( Figure 1A). Two mirror galvanometers and a remote focusing system (Botcherby et al., 2008) steer 16 kHz scan lines (24 mm long) arbitrarily in three dimensions. The use of a voice-coil to move the remote mirror is similar to a large field of view two-photon mesoscope system (Sofroniew et al., 2016; numerical aperture = 0.6) however, the system described here provides high numerical aperture (1.05), defocuses the beam after the scanning elements, and provides roughly twice the lateral and axial resolution. The system provided a 0.35 mm lateral and 1.9 mm axial resolution in the center and 0.56 mm lateral and 4.0 mm axial resolution at the edges of a 525 mm x 525 mm x 300 mm imaging volume.
Stable and sparse neuronal labeling is required for high signal-to-noise ratio and accurate reconstructions of dendritic morphology. We used Cre driver lines with sparse expression in L2/3 of ALM (Syt17_NO14-Cre) or L5 (Chrna2_OE25-Cre; Gerfen et al., 2013). Chrna2_OE25-Cre mice expressed in a subpopulation of pyramidal tract (PT) neurons and not intratelencephalic (IT) neurons (Figure 1figure supplement 1; Gerfen et al., 2013). These lines were crossed with a GCaMP6f reporter line (Ai93; Madisen et al., 2015) and tTa-expressing lines (see Materials and methods). Expression was sufficiently sparse and bright to allow reconstruction of dendritic arbors from two-photon anatomical stacks ( Figure 1B,F). We first traced the dendritic arbors of individual neurons ( Figure 1B,F). The morphological data were then imported by custom software for selecting dendritic branches for fast imaging. The software calculated imaging sequences that optimize the actuator trajectories to maximize speed and coverage ( Figure 1C,G). We used iterative, non-rigid registration to correct recordings for motion in three dimensions ( Figure 1D,E,H,I; Figure 1-figure supplement 2, Figure 1video 1, and Materials and methods). These methods allowed us to record calcium transients in the soma, dendrites (up to 300 mm total length), and up to 150 spines.
Mice performed a whisker-based object localization task (Guo et al., 2014b). A pole was presented at one of two locations (for 1.25 s) and withdrawn; after a delay epoch (2 s), mice licked either a right or left lickport based on the previous pole location ( Figure 2A). In L2/3 neurons, we imaged the soma or proximal apical dendrite as a reference for multi-branch ('global') events, likely mediated largely by bAPs ( Figure 2B, Figure 2-video 1). For each imaging session (maximum one session per day; median duration: 61 min) we targeted dendrites that were not previously imaged. We imaged a total of 3728 spines on 11.4 mm of dendrite from 14 L2/3 neurons (seven mice; 52 behavioral sessions; per-session medians: 241 trials, 74 spines, 221 mm of dendrite). In L5 neurons, we imaged the apical trunk as a reference for global events in the apical tuft ( Figure 2C, Figure 2video 2). We imaged a total of 655 spines on 3.9 mm of dendrite from five L5 neurons (four mice; 16 behavioral sessions; per-session medians: 276 trials, 39 spines, 259 mm of dendrite).

The majority of dendritic calcium transients are coincident with global events
Our imaging approach provided a map of calcium transients across the dendritic arbor during behavior, while simultaneously imaged the soma, where signals reflect action potentials, with negligible contributions from subthreshold calcium signals (Berger et al., 2007;Svoboda et al., 1997;Figure 2B). In some instances, we observed activity restricted to single spines ( Figure 2B, time-point i) as well as activity restricted to isolated dendritic branches, in the absence of detectable activity in the proximal apical dendrite of a L2/3 neuron (Figure 2-figure supplement 1), or the apical trunk of a L5 neuron ( Figure 2C, time-point ii). However, these isolated dendritic events were rare, as most events were 'global', in that calcium transients were detected simultaneously throughout the soma (or proximal dendrite) and all of the imaged parts of the dendritic arbor ( Figure 2B Optical layout for high-speed, high-resolution imaging in three dimensions. An x-axis mirror galvanometer, remote focusing arm, and prism-based GDD compensation unit were added to a high resolution (NA = 1.0) resonant two photon microscope. EOM, electro-optic modulator; GDD, group delay dispersion; Res., 8 kHz resonant scanner; PR, pupil relay; Gal., galvanometer; PBS, polarizing beam splitter; QWP, quarter wave plate; RFO, remote focusing objective; VC, voice coil; DM, dichroic mirror; IO, imaging objective; PMT, photomultiplier tube. (B) Maximum intensity projections (MIP) of anatomical stack collected from Syt17-Cre x Ai93 (pia to 306 um depth) mice. Traced dendrite (purple lines) and example targets (red lines) for an example imaging session. (C) Spatial and temporal distribution of the frames that compose the example functional imaging sequences in (    ii; Figure 2C, time-point i).
Detecting local dendritic events could be limited by the signal-to-noise ratio of our measurements. Although ex vivo (Golding et al., 2002) and in vivo ) experiments indicate that the calcium influx triggered by local regenerative dendritic events is often larger than the influx triggered by bAPs, we avoided assumptions about the magnitude or discrete nature of local events in dendrites during behavior. To estimate the prevalence of local events, we calculated a sample-by-sample probability that the global reference (soma or apical trunk) was below -and the dendrite above -a range of DF/F thresholds, while accounting for measurement noise (see Materials and methods, Figure 3A, Figure 3-figure supplement 1). We used these probabilities to estimate the proportion of activity that was independent of (i.e., not coincident with) the global reference. We also performed this analysis on spines. We limited our L2/3 data to sessions with simultaneous recording from the soma (234 dendritic segments, 30 mm long; 1625 spines). All of our L5 tuft recordings included simultaneous measurements from the apical trunk.
In L2/3 dendrites, independent dendritic activity was rare. For example, with the somatic threshold set to detecting 1-2 spikes (~15% DF/F; Chen et al., 2013) the probability of independent activity in L2/3 dendrites barely rose above the expected false-positive rate across a range of DF/F thresholds ( Figure 3C, mean difference at threshold of DF/F > 1: 0.037 ± 0.009). In contrast, the proportion of independent activity in spines was higher by one order of magnitude ( Figure 3B,C; see Figure 3-figure supplement 1 for complete co-active, independent, and false positive rate grids). The proportion of independent activity in spines was close to 30%, even at thresholds where the false positive rate approached zero (mean difference at threshold of DF/F > 1: 0.324 ± 0.006).
A higher proportion of independent dendrite activity was observed in L5 tufts, compared to L2/3 dendrites ( Figure 3C, mean difference at threshold of DF/F > 1: 0.15 ± 0.02, p<10 À12 Wilcoxon rank-sum test), although the majority of activity was still coincident with the global signal measured in the apical trunk. The distribution of independent activity across individual dendrite segments was skewed, especially in L2/3 ( Figure 3D), where the top 10% most independent segments accounted for 76% of all independent activity (versus 35% for L5 dendrite segments; p<0.001 K-S test on distributions). Independent activity in dendrites often corresponded to sustained elevation in fluorescence that began with a global event but outlasted the global event by 100 s of milliseconds or even several seconds (Figure 2-figure supplement 1; Figure 3-figure supplement 2). We note that these low rates of independent activity do not preclude local modulation of the amplitude of dendritic signals during global events, for example by local dendritic spiking or subthreshold NMDA receptor cooperativity that is coincident with global events.

Task-related calcium signals in the dendrite
Individual dendrites of sensory cortex neurons receive inputs with diverse feature selectivity Chen et al., 2011;Jia et al., 2010;Scholl et al., 2017;Varga et al., 2011;Wilson et al., 2016). Mapping the diversity of functional input to individual neurons in frontal cortex is critical to understand decision-making, motor planning, and short-term memory (Inagaki et al., 2019;Li et al., 2016). Synaptic input is reflected in local changes in dendritic and spine calcium. To characterize local components of dendritic activity we estimated and removed the bAP-related Video 1. Exploring the data online with SpineVis. In this screencast we show how to use the SpineVis website to look at the data in Figure 2B. On top center is the main viewing area where dragging will change the view in 3D. Clicking on a mask in that window will pull up the fluorescence trace for it in the lower window. The lower window has zoom and pan abilities that are linked to the upper window displaying the timepoint indicated by the black line in the center. To the left are display controls for changing lookup table values and opacity, followed by a timepoint selection window. To the right is the mask lookup window. Below the florescence trace are markers indicating behavioral events (e.g., blue triangle is a lick right event). , dendritic (magenta), and spine (green) calcium signals from a layer 2/3 example session (as shown Figure 1B-E). Bottom, maximum intensity projections (au) and branch insets at selected times (dashed vertical lines in upper traces). Note independent spine activity at time i. (C) Same as (B) but for the layer five example session (as in Figure 1F-I). The apical trunk (black) was targeted as a reference for global activity. Note branchspecific sustained activity at time ii. The online version of this article includes the following video and figure supplement(s) for figure 2:  the bAP component, trial-averaged spine activity was typically high during epochs when soma activity was also high ( Figure 4A,B, spine i; Figure 4E), but some spine responses were independent of soma activity ( Figure 4C,D; spine ii). Removal of the bAP-related component sharpened ( Figure 4A, B; spine ii; see Figure 4-figure supplement 2 for dendrite segment example), eliminated ( Figure 4A,B; spine i), or had no effect ( Figure 4C,D; spine ii) on the apparent selectivity of individual spines for specific trial epochs. After subtraction, the distribution of trial-averaged spine activity was less restricted to epochs with somatic activity ( Figure 4F), confirming the existence of spine activity independent of bAPs.
To obtain a one-dimensional measure of the selectivity of responses for specific behavioral epochs, we treated the mean responses during sample, delay and response epochs as the magnitudes of three vectors separated by 120˚in a polar coordinate system ( Figure 5). The angle of the vector average then determined the epoch selectivity of each dendritic segment or spine ( Figure 5B). To quantify and visualize selectivity for trial type, dendritic segments and spines were then given one of three colors depending on whether signals were selective for right (blue), left (red), or switched selectivity across epochs (purple; Figure 5J). In L2/3 dendrites (see Figure 5A-C for an example session), 63% of spines and 74% of short dendrite segments (~3 mm, see Materials and methods) exhibited significant (p<0.01, nonparametric ANOVA and standard error for epoch angle of <30 degrees) activity selective for specific trial epochs. 20% of spines and 30% of short dendrite segments exhibited significant (p<0.05, permutation test with Bonferroni correction) selectivity for trial-type (right vs. left) during at least one of the epochs. Similar selectivity was observed in L5 tuft dendrites (see Figure 5F-H for an example session; for all sessions: epoch selective: 46% of spines and 39% of dendrite segments, trial-type selective: 27% of spines and 38% of dendrite segments). Similar proportions of spines and dendritic segments with selectivity were observed after bAP subtraction (L2/3, Figure 5D,E, epoch selective: 53% of spines, 67% of dendrite segments, trial-type selective: 18% of spines and 27% of dendrite segments; L5, Figure 5I,J, epoch selective: 47% of spines, 54% of dendrite segments, trial-type selective: 28% of spines and 35% of dendrite segments). Thus, both spines and dendritic segments of neurons in ALM exhibit taskrelated selectivity distinct from the selectivity of global responses.

Limitations in unmixing local activity and back-propagating action potentials
Previous studies of the functional responses of dendritic spines estimated and removed the bAP component, using linear regression between signals in spines versus nearby dendrites Iacaruso et al., 2017;Wilson et al., 2016). Computer simulations show that this approach produces biased estimates of correlations between nearby spines (Figure 6-figure supplement 2). This is because the depolarization required for calcium influx in one compartment also acts on nearby compartments. In addition, the subtraction procedure does not account for differences in the decay times of bAP-generated transients in the soma compared to the dendrites. To overcome these issues, we deconvolved the reference signal (soma or apical trunk; Pnevmatikakis et al., 2016;Vogelstein et al., 2010), determined the amplitude and exponential decay that best fit each dendritic segment or spine signal (when convolved with the reference signal), and subtracted this fit (Figure 4-figure supplement 1).
Systematic errors in bAP-subtraction could affect the apparent organization of task-related calcium signals in the dendrite. Under-or over-subtraction sometimes produced inaccurate correlations between the bAP reference signal and spines as well as hypo-or hyper-diversity in the selectivity of spines ( Figure 6B). To analyze the accuracy of various measures of dendritic calcium signals with our subtraction approach, we performed computer simulations with different assumptions about the processes underlying the spike-to-fluorescence transformation ( Figure 6-figure supplement 1). We found that our subtraction procedure, as well as other linear subtraction methods we tested, failed to produce accurate estimates of correlations between spines and the global reference signal. Thus, we avoided a quantitative comparison of input (spine signals) and output (reference signals). The diversity of selectivity measured without subtraction ( Figure 5K,L) provides a lower bound on the diversity of task-related selectivity. Simulations also indicated that signals

Spatial clustering of task-related and trial-to-trial signals
Spatial clustering of coactive inputs could facilitate nonlinear computations within the dendrite (Losonczy and Magee, 2006;Major et al., 2008;Polsky et al., 2009;Weber et al., 2016). To quantify the similarity of selectivity as a function of distance along the dendrite, we calculated the correlation of average responses ('signal correlation') between pairs of spines and pairs of dendritic segments ( Figure 7A). We randomly selected non-overlapping sets of trials to calculate trial-average responses of each dendritic segment or spine in the pair, which prevented trial-to-trial correlations ('noise correlations') from contaminating our estimates of signal correlation (Cohen and Kohn, 2011). Our imaging method allowed us to measure pairwise correlation from simultaneous recordings at distances considerably longer than previous studies (Iacaruso et al., 2017;Wilson et al., 2016). Pairwise correlations were strongest for nearby dendritic segments and spines in both L2/3 . These signal correlations (especially in L2/3 dendrites) had a long linear decay in addition to a short exponential component (Iacaruso et al., 2017;Wilson et al., 2016). Fits to this exponentiallinear function (see Materials and methods), provided estimates of the length constant of the exponential component that ranged from 7 to 19 mm ( Figure 7C). Differences in exponential length constant between spine pairs, dendrite pairs, L2/3 neurons, and L5 neurons were not significant (p>0.05 for all comparisons, shuffles across sessions). Combining across cell types, we estimated a length constant of 8 ± 4 mm for dendrite segments and 13 ± 6 mm for spines. In addition, we measured noise correlation among spines and dendrite segments. These noise correlations may reflect variable sensation and behavior during task performance, common sources of input, or other processes. As with signal correlations, we observed a strong effect of distance on pairwise noise correlations for dendrite segment pairs and spine pairs in L2/3 dendrites and L5 tufts ( Figure 7E,F; p<0.001 for both, nonparametric ANOVA comparison to shuffle). Fits to the exponential-linear function provided estimates of the length constant of the exponential component that ranged from 9 to 18 mm ( Figure 7F). Differences in exponential length constant between spine pairs, dendrite pairs, L2/3 neurons, and L5 neurons were not significant (p>0.05 for all comparisons, shuffles across sessions). Combining across cell types, we estimated a length constant of 10 ± 3 mm for dendrite segments and 14 ± 3 mm for spines. Thus, the spatial profile of noise correlations within the dendrite was not significantly different from the profile for signal correlations.
To control for artefactual correlations due to residual image motion, we analyzed correlations in pairs with short Euclidean distance (<15 mm) but long distance along the dendrite (>30 mm). In dendrites of L2/3 neurons -where a sufficient number of pairs met this criteria -pairs with short Euclidean distance had significantly lower correlation than pairs with an equivalent distance along the dendrite ( Figure 7C,F, purple points), indicating that residual motion makes little contribution to the distance-dependent correlations.

Dendritic branching compartmentalizes task-related calcium signals
Impedance mismatch at branch points (Marlin and Carter, 2014;Müllner et al., 2015) and branchspecific regulation of excitability (Losonczy et al., 2008) may compartmentalize signals to dendritic branches. To test if this influences behavior-related calcium signals, we compared the similarity of selectivity within and across branches. The distribution of epoch selectivity was clearly different from branch to branch in some imaging sessions ( Figure 8A). We measured the mean signal correlation for spine and dendrite pairs within versus across branches. Branch location had a significant effect on spine and dendrite pairs from both L2/3 and L5 tuft ( Figure 8B). This could reflect an influence of branch structure, the distance-dependence of signal correlations, or both. To selectively test for an influence of branch points, we restricted the data to pairs less than 10 mm apart that were either within a branch or crossed a single branch point. Correlations were lower when a branch point was crossed ( Figure 8C; p<0.05 for all groups of pairs except L5 tuft spines).

Discussion
We recorded activity in the dendrites and spines of motor cortex pyramidal neurons as mice performed a tactile discrimination task. The majority of calcium transients in the dendrites were coincident with global events. Events that were not coincident with global events were rare and occurred with higher frequency in the dendritic tufts of L5 neurons than in the dendrites of L2/3 neurons. However, the amplitudes of local calcium signals were modulated by task-related variables and spatially clustered within individual dendritic branches. Our data suggest that sensorimotor signals are compartmentalized within the dendrites of neurons in motor cortex, consistent with models in which branch-specific information enhances the computational or learning capacity of neural circuits (Guerguiev et al., 2017;Poirazi and Mel, 2001;Urbanczik and Senn, 2014;Wu and Mel, 2009). Simultaneous imaging of soma, contiguous dendrites, and spines Our microscope and imaging strategy allowed us to obtain near-simultaneous, high-resolution 3D images of the soma and up to 300 mm of contiguous dendrite and spines at~15 Hz. This provided several advantages over functional imaging of dendrites that intersect one or two imaging planes (Cichon and Gan, 2015;Sheffield and Dombeck, 2015). First, we were able to sample dendrites more efficiently. Second, imaging long stretches of contiguous dendrite allowed us to localize activity to specific dendritic regions (Figure 2-figure supplement 1; Figures 7 and 8). Third, averaging fluorescence across extended (30 mm) dendritic segments improved the SNR for our branch measurements ( Figure 3) and reduced contamination of dendritic shaft signals by spine signals. Raw movies and processed traces for all figures can be browsed online ( Table 1; Video 1).
For rapid scanning in 3D we used a resonant mirror, mirror galvanometers, and remote focusing (Botcherby et al., 2008). Actuator control signals were optimized computationally (see Materials and methods). This approach offers some advantages over acousto-optical scanning methods, which can have higher rates of dendrite and spine imaging (Nadella et al., 2016;Szalay et al., 2016), but a smaller field-of-view (FOV) and 1.5-3 times lower resolution depending on location in the FOV. Dense and high-resolution images of the targeted dendrite and nearby space were critical for identifying and excluding signals from crossing axons and boutons that would otherwise appear as strongly independent dendrite or spine activity (see Materials and methods). In the future, pointscanning systems combining motionless and mechanical deflection may provide the optimal tradeoffs between speed, resolution, and FOV for dendrite and spine imaging (Heberle et al., 2016). Even higher imaging speeds can be obtained using alternatives to point-scanning, such as imaging with an elongated focus (Lu et al., 2017) or tomographic scans (Kazemipour et al., 2019), but at the expense of requiring higher average laser power and computational reconstruction of signal sources.
Identifying task-related activity in individual dendritic spines required recordings across 100 or more behavioral trials. Brain movement during task performance, especially during licking (Andermann, 2010; Komiyama et al., 2010), made stable spine recordings challenging. We addressed this issue by developing an iterative clustering and registration algorithm to obtain submicron, nonrigid registration (see

Analysis and interpretation of calcium signals in dendrites and spines
Understanding local dendritic operations will require precise measurements of synaptic activity and various post-synaptic processes. Calcium influx into the dendritic shaft and spines is produced by calcium-permeable receptors and voltage-gated calcium channels. Both types of conductances are modulated by synaptic currents and postsynaptic electrogenesis, complicating the interpretation of dendritic calcium signals. Synaptic signals can be isolated during dendritic spine imaging by abolishing bAPs, using invasive approaches that hyperpolarize (Jia et al., 2010;Levy et al., 2012) or depolarize (Mainen et al., 1999) the neuron. These manipulations, however, are difficult to apply in behaving animals, especially in chronic imaging preparations, and could trigger plasticity   , 1986). Several studies Iacaruso et al., 2017;Scholl et al., 2017;Wilson et al., 2016) have instead interpreted signals from nearby dendrites as a reference for computational removal of the bAP signal in spines. However, signals in the nearby dendrite are not necessarily exclusively or linearly related to bAPs: local synaptic input can generate coincident dendritic spikes (Golding and Spruston, 1998;Losonczy and Magee, 2006;Schiller et al., 2000), as well as amplify or suppress the amplitudes of bAP calcium transients in the dendrite (Magee and Johnston, 1997;Remy et al., 2009;Waters and Helmchen, 2004). To obtain a reference signal that more directly reflects action potentials, we used simultaneously recorded signals from the soma of L2/3 neurons, where subthreshold depolarization has a negligible impact on calcium signals (Berger et al., 2007;Svoboda et al., 1997). We developed a new bAP subtraction method that accounts for differences in calcium dynamics across compartments, especially the long decay dynamics in the soma compared to the dendrite. Computer simulations indicated that conclusions drawn from this approach are limited ( Figure 6figure supplement 1). Inferred correlations between input (spines) and output (soma) did not reliably predict true correlations, even in simple and linear models of the relationship between activity and fluorescence signals in dendritic spines. This was also true for all linear subtraction methods we tested. Correlations at timescales shorter than the decay of the GCaMP signals were most affected. This is also the timescale over which synaptic input would be expected to influence somatic spiking, and therefore neuronal output. We therefore avoided drawing conclusions about how synaptic input might drive output.
Other measures of the spatial structure of dendritic activity, however, were accurately estimated using our bAP subtraction approach (Figures 6 and 7 and Figure 6-figure supplement 2). We simulated pre-synaptic clustering or post-synaptic cooperativity with varying length scales, based on the real geometry and numbers of spines analyzed for L2/3 neurons (see Figures 1B-E, 2B, 3B, 4A, 5A and 8A for example sessions). Using our subtraction methods on simulated data, the presence or absence of structured correlations was accurately detected and the inferred length scale of pairwise correlations closely matched the simulated scale. These estimates were robust to potential differences in the rate of calcium extrusion across different dendritic compartments, which was simulated with a distance-dependent variation in decay dynamics (see Materials and methods). The length scales we estimate could reflect pre-and post-synaptic processes and should thus be regarded as a description of the local component of calcium signals in the dendrite.
Incorporation of assumptions about the temporal or functional structure of pre-and post-synaptic activity into bAP-removal methods may facilitate more accurate inference of input-output correlations. Studies of the functional properties of dendritic spines in visual cortex have excluded spines from analysis where removal of the bAP-component is suspect, using inclusion criteria based on the shape of receptive fields of synaptic inputs, or the magnitude of correlation with output (Iacaruso et al., 2017;Wilson et al., 2016). We did not apply any such constraints on the taskrelated responses of inputs to ALM neurons. However, future bAP subtraction methods could use other a priori information about temporal structure of input and output activity as constraints. These constrains could include the binary nature of spikes, the distribution of mean firing rates, and the relative refractory period. These priors could be used by nonlinear inference methods to more accurately disambiguate pre-and post-synaptic activity (J. Yan, A. Kerlin, L. Aitchison, K. Svoboda, S. Turaga, Cosyne Abstr. 2018).

Multi-branch events are coincident with the majority of calcium transients in dendritic branches
The independence of local dendritic spikes varies across models of dendritic computation. In some models, individual dendritic branches can behave as largely independent functional subunits capable of generating local dendritic spikes with a functional selectivity that is distinct from the somatic selectivity (Archie and Mel, 2000). Alternatively, facilitative interactions across branches (Major et al., 2013;Major et al., 2008) or network-level high input states  could favor the generation of local dendritic spikes that are synchronous across the dendritic tree and reliably accompanied by somatic spiking. The relative independence of local dendritic spikes will depend on many factors, including electrotonic distance between spike-generating sites and the precise spatiotemporal pattern of excitatory and inhibitory input (Archie and Mel, 2000;Behabadi and Mel, 2014;Gidon and Segev, 2012;Ujfalussy et al., 2018).
Our recordings from dendrites in motor cortex during a tactile decision-making task are not consistent with models that posit high rates of local dendritic spiking in the absence of global events. At reasonable thresholds for detecting calcium transients, we found almost no independent activity in L2/3 dendrites and a low rate of independent activity in the L5 tuft ( Figure 3C). It is possible that our estimate of independent activity in the L5 tuft is biased upward by low detectability of calcium transients produced by single sodium APs near the main apical bifurcation . However, much of the 'independent' activity in the L5 tuft took the form of branch-specific, sustained activity (up to seconds) following global events (Figure 2-figure supplement 1), similar in spatial spread and dynamics to post-plateau potentials that have been measured in ex vivo experiments (Antic et al., 2010;Milojkovic et al., 2007). In contrast, another study in motor cortex reported that at least 95% of calcium spikes in the L5 tuft were not shared across sibling branches during a forced-running paradigm (Cichon and Gan, 2015). This difference may reflect the fact that global events (bAP or tuft calcium spike) may not reliably invade all branches (Hill et al., 2013;Spruston et al., 1995) or technical differences in the recording and analysis of dendritic events. Alternatively, differences in the learning demands placed upon cortical circuits may affect the prevalence of independent local dendritic spikes. Our results, however, are similar to studies in sensory cortex (Smith et al., 2013) and hippocampus (Sheffield and Dombeck, 2015), in which the majority of dendritic spikes are coincident with the generation of somatic spikes.
Some branches in the apical tuft of layer five neurons maintained activity for 100's of milliseconds after a global event. What could be the computational roles of these long-lasting events? ALM is a key hub in a multi-regional neural circuit that sustains memory-related activity (Guo et al., 2014b). This sustained activity is internally maintained by attractor dynamics in recurrent connections (Inagaki et al., 2019), involving ALM, thalamus (Guo et al., 2017) and other brain regions (Gao et al., 2018). Biophysical models have shown that maintenance of memory-related activity requires time constants of excitation that outlast feedback inhibition (Wang, 1999). Long-lasting dendritic excitation could be part of the cellular mechanism of short-term memory.
It is possible that we underestimate the frequency of independent local dendritic spikes if the calcium transients they generate during behavior are well below our measurement noise. In ex vivo (Antic et al., 2010;Golding et al., 2002;Milojkovic et al., 2007;Nevian et al., 2007) and in vivo  experiments, however, the peak depolarization and calcium influx generated by local dendritic spikes are comparable or larger than the peaks generated by a bAP or calcium spike. Artificial generation of a local dendritic spike on a single branch does not reliably drive somatic action potentials or calcium spikes (Larkum et al., 2009;Losonczy and Magee, 2006;Palmer et al., 2014). Hence, our results suggest that during the tactile decision task, local dendritic spikes are either very rare or are almost always part of a synchronized, multi-branch depolarization of the dendrites that reliably drives somatic action potentials or calcium spikes.
The rarity of independent local dendritic spiking does not preclude functional compartmentalization within the dendrite. Local synaptic input may modulate the depolarization and calcium influx into the dendritic shaft produced by bAPs or calcium spikes (Larkum et al., 1999b;Stuart and Häusser, 2001;Waters et al., 2003), as well as the probability of coincident local dendritic spikes.

Organization of task-related signals in the dendritic tree
Task-related calcium signals were clustered within the dendritic tree of neurons in motor cortex (Figures 5 and 7). Functional similarity among spines and dendrite segments followed an exponential decay with a length constant of approximately 10 mm. Previous work in mouse visual cortex has also observed a distance dependence of retinotopic similarity among spines (Iacaruso et al., 2017). In ferret visual cortex, this distance-dependence exhibited an even smaller length constant (5 um; Scholl et al., 2017). The short exponential decay of distance-dependent correlations could reflect a clustering of pre-synaptic inputs with similar functional properties or input onto nearby spines from the same axon (Bloss et al., 2018;Kasthuri et al., 2015;Knott et al., 2006). The length constant (~10 mm) is similar to a number of spatially restricted plasticity mechanisms. When activated by stimulation of a single spine, small GTPases spread throughout~5-10 mm of the dendrite (Harvey et al., 2008;Murakoshi et al., 2011;Nishiyama and Yasuda, 2015) and influence plasticity at nearby spines (Harvey and Svoboda, 2007). During development, ryanodine-sensitive calcium release (Lee et al., 2016) and BDNF-mediated synaptic depression (Winnubst et al., 2015) can produce selective stabilization of inputs with similar spontaneous activity over distances of 5-10 mm. This length scale is also consistent with the high calcium zone produced by local dendritic spikes (Major et al., 2008) and subthreshold nonlinear NMDA receptor-mediated amplification of synaptic calcium signals by the activity of neighboring spines Weber et al., 2016).
Our analysis allowed us to measure pairwise correlations across branches and spanning large distances within the dendritic tree. We found that in addition to a short exponential decay, correlations exhibited a linear decay over longer distances (up to 180 mm). This was true of both signal and noise correlations. This decay may reflect a combination of multiple processes, including passive and active (via voltage-gated potassium channels) attenuation of both subthreshold potentials and locally initiated dendritic spikes (Gasparini et al., 2004;Harnett et al., 2013;Major et al., 2008;Milojkovic et al., 2007).
We also found that the branching structure of dendrites further compartmentalizes task-related signals within the dendrites. A number of mechanisms could confine excitability in branch-specific manner, including current sinks towards branch points (Branco et al., 2010;Marlin and Carter, 2014;Müllner et al., 2015), the sub-branch organization of inhibitory input (Bloss et al., 2016), and the distribution of Kv4.2 potassium channels (Losonczy et al., 2008).
Diverse behavior-related signals were distributed throughout the dendritic arbor and were compartmentalized by dendritic distance and branching. This compartmentalization may reflect local dendritic operations that expand the processing capacity of individual neurons (Archie and Mel, 2000;Poirazi and Mel, 2001). Understanding how these operations transform pre-synaptic information may be critical to interpreting the structure and function of cortical circuits. These local operations may also play a critical role in learning (Frick et al., 2004;Guerguiev et al., 2017;Losonczy et al., 2008;Urbanczik and Senn, 2014), and future work in the motor cortex could explore the relationship between compartmentalized anatomical changes in the dendrite (Chen et al., 2015;Fu et al., 2012;Yang et al., 2009) and clustered task-related activity during learning.

Animals
All procedures were in accordance with protocols approved by the Janelia Institutional Animal Care and Use Committee. Triple transgenic mice (both male and female) sparsely expressing GCaMP6f in a subset of layer 2/3 (Syt17 NO14 x CamK2a-tTA x Ai93; MGI:4940641 x JAX:007004 x JAX:024103) and layer 5 (Chrna2 OE25 x ZtTA x Ai93; MGI:5311721 x JAX:012266 x JAX:024103), were housed in a 12 hr:12 hr reverse light:dark cycle. We never observed seizures in these mice, as has been reported for Emx1-Cre x Camk2a-tTa x Ai93 crosses (Steinmetz et al., 2017). Surgical procedures were performed under isoflurane anesthesia (3% for induction, 1-1.5% during surgery). A circular (~2.5 mm diameter) craniotomy was made above left ALM (centered at 2.5 mm anterior and 1.5 mm lateral to bregma). A window (triple #1 coverglass 2.5/2.5/3.5 mm diameter; Potomac Photonics, Baltimore, MD) was fixed to the skull using dental adhesive (C and B Metabond; Parkell, Edgewood, NY). A metal bar for head fixation was implanted posterior to the window with a metal loop surrounding the window using dental acrylic. After the surgery, mice recovered for 3-7 days with free access to water. Then, mice were water restricted to 1 mL daily. Training started 3-5 days after the start of water restriction. On days of behavioral training, mice were tested in experimental sessions lasting 1 to 2 hr where they received all their water. Unless otherwise noted in the figure legend, N = 14 neurons, seven mice for figure panels regarding layer 2/3 neurons and N = 5 neurons, four mice for figure panels regarding layer five neurons.

Tactile decision task
Mice solved an object localization task with their whiskers (modified from Guo et al., 2014a;Guo et al., 2014b). The stimulus was a metal pin (0.9 mm in diameter) mounted on a galvo motor to reduce vibrations. The pole swung into one of two possible positions ( Figure 2A). The posterior pole position was approximately 5 mm from the center of the whisker pad. The anterior pole position was 4 mm anterior to the posterior position. A two-spout lickport (4.5 mm between spouts) delivered water reward and recorded the timing of licks.
The sample epoch is defined as the time between the pole movement onset to pole retraction onset (sample epoch, 1.2 s total). The delay epoch lasted for another 2 s after the beginning of pole retraction (delay epoch, 2 s total). An auditory 'response' cue indicated the end of the delay epoch (pure tone, 3.4 kHz, 0.1 s duration). Licking early during the delay period resets the delay-period timer (2 s). Licking the correct lickport after the auditory 'response' cue led to a small drop of water reward. Licking the incorrect lickport was not rewarded nor punished. Trials in which mice did not lick after the 'response' cue were rare and typically occurred only at the end of a session. Animals were trained daily until they reached~70% correct. Thereafter behavior was combined with imaging (typically 20-40 days after surgery).

Reference volume imaging
Before imaging during behavior, a reference volume for the field-of-view (FOV) was acquired. Dendrite tracing required a reference volume with high SNR and minimal brain motion artefact. To achieve this, we repeatedly imaged the FOV when the mouse was not behaving. We collected 100 image stacks (12 to 20 seconds / volume) at 1x zoom (frames: 525 mm x 525 mm, 1024 pixel x 1024 pixel) from the pia and down to the cell body of interest in 1.6 um steps (~320 um for Syt17 NO14 mice and up to 500 um for Chrna2 OE25 mice). Cross-correlation based registration of stacks to the mean of the most correlated stacks (iteratively: top 30%, then top 70%) removed motion artefacts. All stacks were then averaged to generate the final reference volume.

Cell selection
Soma locations within the reference volume were manually identified and those coordinates were provided to the imaging software. One to 31 somas or layer five apical trunks were imaged during task performance. A 40 mm x 20 mm imaging frame was centered on each soma or trunk. Registration was done by iterative cross-correlation. Trial-averaged fluorescence was computed for each soma. Cells were selected for functional imaging of the dendrite based on two qualitative criteria: modulation of the signal by the task and sufficient baseline fluorescence to trace dendrites.

Targeted imaging of dendrites
Tracing of dendrites was done using Neuromantic (Myatt et al., 2012) in Semi-Auto mode. Tracing data was loaded to a custom Matlab GUI that enabled selection of different combinations of dendrite branches for targeted imaging. All frames in an imaging sequence were 24 mm x 12 mm (72 pixels x 36 pixels) and had a duration of approximately 2 ms. Average power post-objective varied with imaging depth and ranged from 22 mW to 82 mW. The dendritic length imaged for each session was limited to maintain a sequence rate of approximately 14 Hz (see Figure 1B,C,D and Figure 1F, G,H for example sessions). Frame positions were calculated to completely cover the volume of the selected dendrite (treating each frame as a 24 mm x 12 mm x 3 mm volume) while minimizing both the total number of frames in the sequence and the predicted acceleration along the z-axis (our slowest axis). Additionally, before each imagining session a closed loop iterative optimization of the z actuator (voice coil) control signal was performed (similar to Botcherby et al., 2012). The z trajectory and field placements were then transferred to ScanImage using the MROI API.

Dendrite image registration and time course extraction
We developed a new process to obtain non-rigid registration of a sequence of small imaging frames irregularly distributed throughout space (see Boaz, 2019 for code and example session; copy archived at https://github.com/elifesciences-publications/SpineImagingALM). SNR and amount of dendrite in frames varied widely and, thus, independent frame-by-fame registration did not achieve good results. One registration target image per frame was also not suitable, as even small axial motion could be confused with a lateral translation of a thin 3D structures such as dendrites. To address these issues, we developed a multi-step registration and time course extraction procedure in python leveraging open source parallel computing tools (Thunder, RRID: SCR_016556; Apache Spark, RRID: SCR_016557).
Registration included the following steps: initial registration target selection, frame by frame clustering and registration (rigid lateral registration), re-clustering of this laterally aligned data and estimation axial positions to obtain multiple registration targets, and registration of frames to the appropriate axial target (see Figure 1-figure supplement 2 and Figure 1-video 1). The initial target for registration was selected by k-means clustering (30 clusters) on the first 40 PCA components of the complete imaging sequence. Averages of the four largest groups were visually inspected to select the registration target Figure 1-figure supplement 2C). K-means clustering was then used to independently group samples of each frame from the sequence (first 50 components, 200-800 clusters per frame; Figure 1-figure supplement 2D) to increase the SNR prior to registration. For each group of samples of each frame, the complete imaging sequence at those samples was averaged and registered to the initial target using cross-correlation (Figure 1-figure supplement 2E). Shifts calculated from this registration were then used to constrain (by minimizing brain velocity) the registration of each frame group average to the initial target. Hyper-parameters controlling the balance between cross-correlation peaks and brain velocity constraints in determining shift were optimized (via differential evolution) to maximize the sharpness of the average registered sequence. Lateral shifts calculated for each frame group average were then applied to all individual samples in the group. This laterally-registered data was again clustered (45-90 clusters based on 80 PCA components). These clusters reflected different axial positions as well as activity. A Traveling Salesman Problem solver (https://github.com/dmishin/tsp-solver) ordered these clusters, minimizing the total distance (dissimilarity) between adjacent groups. Adjacent groups were collapsed down to between 4 and 8 (median 5) final groups representing different axial positions (Figure 1-figure supplement  2F). The mean of each group served as the registration target for a final registration of all samples, frame-by-frame, belonging to the group. Registration was constrained again by brain velocity parameters, as described above. Thus, for every sample and every frame we determined x and y shifts for lateral motion and z group for axial motion (Figure 1-figure supplement 2G).
For high-resolution dendrite and dendritic spine tracing, all frames from a session were projected into 3D space and averaged, taking into account the estimated axial position of each sample and the PSF of the microscope. Dendrite centerline was traced using Neuromantic. This centerline was then dilated in 3D (2.5 mm in z; lateral dilation was based on the estimated radius from the tracing) and divided into 30 mm (Figures 2 and 3, Figure 2-figure supplement 1, Figure 4-figure supplement 1) or approximately 3 mm (Figures 4, 5, 7 and 8) segments (dendritic masks). Spines were segmented in 3D using a custom-built, semi-automated Matlab GUI (spine masks). The inverse transform of the frames to 3D space was used to extract the fluorescence (Df ) time course for each mask (dendritic segment or spine). Putative axonal boutons (identified by a 'bead on a string' appearance in the mean volume or during activity) adjacent to the dendrite were segmented but excluded from further analysis.
Baseline was estimated as the mode of a Gaussian kernel density estimator fit to the distribution of F values for a segment in a 2000 sample (~2 minute) sliding window. Estimated axial position of each sample was used to correct this baseline estimate for axial motion. Because time courses of individual pixels have Poisson statistics (photon shot noise is the main source of noise), the noise floor (expressed in Df =f 0 ) follows ffiffi ffi N p N where N is the number of photons collected in one sample. We calculated N as m Ã f , where f is the total fluorescence collected (arbitrary digital units) and m is the slope of a linear fit to the variance versus the mean of all pixels in the imaging sequence. This noise estimate was also adjusted sample-by-sample for the effects of changes in axial position.

Independent activity estimation
The signal used to estimate the independent activity was the Df =f 0 without bAP subtraction. For layer 2/3 sessions (N = 23) the reference signal was the soma and for layer 5 (N = 16) the apical trunk. Sessions in layer 2/3 where the soma was not imaged were excluded (N = 29). Given a threshold value for a signal we calculated the probability that the signal is above that threshold: where xðtÞ is the Df =f 0 at time t, u is the threshold, s is the shot-noise estimate.
A false positive probability (P FPR ) was computed using the same function but xðtÞ is fixed at the threshold and u ¼ 0. We defined the independent activity probability as: where P mask is the probability the spine or dendrite signal is above threshold and P ref is the probability the reference signal is above threshold. We defined the co-activity probability as: P coÀactive ðtÞ ¼ P mask ðtÞ Ã P ref ðtÞ: False positive rates for independent (P FPR;independent ðtÞ) and co-active (P FPR;coÀactive ðtÞ) probabilities were calculated in the same manner, but using the P FPR for mask and reference. Finally, we calculated the proportion independent from averages of probabilities across all timepoints in the recording session: Proportion independent ¼ P independent ðtÞ P independent ðtÞ þ P coÀactive ðtÞ To estimate the false positive (due to shot noise) proportion independent: Proportion independent FPR ¼ P FPR;independent ðtÞ À P FPR;coÀactive ðtÞ À Á P coÀactive ðtÞ þ P FPR;independent ðtÞ À Á To exclude independent activity preceding or following the reference (Figure 3-figure supplement 2), every value in P ref was replaced by the maximum value within a preceding ('after reference') or following ('before reference') time window ('exclusion window'). Proportion independent was then calculated as described above.

Back-propagating action potential subtraction
We used the soma (N = 23, L2/3 sessions), proximal dendrite (N = 29, L2/3 sessions, 15% most proximal of total dendrite), or apical trunk (N = 16, L5 tuft sessions) as a reference for global activity (putatively dominated by bAPs). The Df =f 0 of this reference was then processed with a constrained deconvolution spike inference algorithm (Pnevmatikakis et al., 2016;Vogelstein et al., 2010; https://github.com/epnev/constrained_foopsi_python) with autoregressive order of 1 and 'fudge factor' of 0.5 (Figure 4-figure supplement 1A). The time course for each spine and dendrite mask was fit (by differential evolution minimization of the L2-norm) to the model: Alternative bAP subtraction methods were evaluated in the simulations shown in Figure 6-figure supplements 1 and 2 (see below for simulation details). Rapid, negative fluorescence transients are inconsistent with the dynamics of GCaMP6, and thus likely represent bAP subtraction errors. 'Alternative 1: Non-negative fit' was the same as the method described above, but instead of minimizing the L2-norm we developed an objective function that penalized negative residuals beyond those expected from photon shot-noise: where r t ð Þ is the residual at time t of spine or dendrite signal mask t ð Þ, cdf neg is a cumulative distribution function estimated from the negative values of the Df =f 0 trace for each spine without subtraction and cdf pos is a cumulative distribution function estimated from the positive values of the Df =f 0 trace for each spine with subtraction computed by using the 'Alternative 2: Regression -Soma Referenced' subtraction method (see below). Although this approach generated bAP-subtracted traces with 'cleaner' appearance (less negative deflections; data not shown), it still produced inaccurate input-output correlations in our simulations ( Figure 6-figure supplement 1). For 'Alternative 2: Regression -Soma Referenced' we used the residuals of a robust regression of the spine Df =f 0 versus soma Df =f 0 . For 'Alternative 3: Regression -Dendrite Referenced' we used the residuals of a robust regression of the spine Df =f 0 versus the Df =f 0 of the closest 30 mm dendrite segment (Wilson et al., 2016;Chen et al., 2013;Scholl et al., 2017). For 'Alternative 4: Least-Squares Fit -Dendrite Referenced' we used the deconvolution subtraction method described above (also in Figure 4-figure supplement 1), but the Df =f 0 of the closest 30 mm dendrite segment was used as the reference signal instead of the soma.

Task-associated selectivity
Trials were divided into five epochs ( Figure 4): sample (pole within reach of whiskers, 1.25 s), early and late delay (1 s increments from end of sample to response cue), and early and late response (1 s increments from response cue). Only correct trials were included. Incorrect trials and trials during which the mouse licked before the go cue were excluded. To test for any task-associated selectivity, we averaged the signal (Df =f 0 during each epoch, separating correct left and correct right trials, and performed a nonparametric ANOVA (10 groups total: 5 epochs x two trial-types; p is the proportion of F-statistics from 1000 shuffles of epoch greater than the observed F-statistic). For all significance testing, the number of samples averaged from each epoch was equal (approximately 1 s worth of samples were randomly drawn from the sample epoch).
To visualize and quantify epoch selectivity, we averaged the mean responses during sample, delay (early and late) and response (early and late) epochs across both left and right trials. These mean responses were treated as the magnitudes of three vectors separated by 120˚in a polar coordinate system (see Figures 5B, 6B and 8A). The angle of the vector average provided a onedimensional representation of the epoch selectivity. Standard error in this angle for each segment was the circular standard deviation of 1000 bootstrap iterations (resampling trials). The significance of differences in epoch angle (D angle) between segments was determined by permutation test (1000 shuffles of trial identity, p is the proportion of D angles greater than the observed D angle). For the circular mean epoch angle across segments, the 95% confidence interval was calculated from 1000 bootstrap iterations (resampling segments).
To visualize and quantify trial-type selectivity, we tested each epoch for significant differences in response during left versus right trials by permutation test (p<0.05, with Bonferroni correction for five null hypotheses). We divided segments into three categories: significantly larger responses during right trials only, left trials only, or both left and right trials depending on epoch.

Spatial structure of pairwise correlations
Signal correlation between pairs of segments was the Pearson correlation between the mean responses to all 10 conditions (5 epochs x two trial-types) for each segment. To isolate signal correlations from noise correlations, we randomly selected and averaged a non-overlapping 50% of trials for each segment. This was repeated 100 times and the resulting correlation coefficients were averaged. Noise correlation between pairs of segments was the mean noise correlation (Pearson correlation between responses across trials within one condition) across all conditions.
For spines or dendrite segments that were connected by the dendrite imaged within a session, traversal distance was directly calculated from the high-resolution session-based reconstruction. However, for spines and dendrite that connected via dendrite that was not imaged as part of that session's imaging sequence, calculating traversal distance required precise alignment of the imaging session (with segmentation of dendrite and spines) back to the reference space (containing the complete reconstruction of the dendritic tree). A multi-resolution approach from SITK (Lowekamp et al., 2013;Yaniv et al., 2018) was used to fit a 3d affine transformation from the session space to the FOV space. Center points of spines and dendrite segments were transformed to the reference space and the closest point along the tracing of dendrite within reference space was determined. Traversal distance (distance of the shortest connecting path through the dendritic tree) could then be calculated from the reference reconstruction.
To quantify mean pairwise correlation as a function of traversal distance, pairs were divided into 11 exponentially spaced distance bins (edges: 0, 2.7, 4.5, 7.4, 12, 20, 33, 55, 90, 148, 245 mm). To calculate the standard error of the mean (SEM), we randomly drew pairs without replacement of the pair members, then the SEM was determined from the standard deviation of pair correlations and the number of pairs drawn for each bin. To average out variation across draws, the reported SEM is the mean of 100 repetitions of this process. Mean within-bin correlation was fit (by differential evolution minimization of the L2-norm) to a 4-parameter model: where x is the traversal distance between a pair, A is the amplitude of distance dependent correlation, l is the length constant of exponential decay, L is slope of the linear component, and B is the baseline correlation. Error in l was estimated as the standard deviation of 300 bootstrap iterations (resampling across sessions).

Simulations
Simulations were designed to analyze the robustness of various measures of dendritic calcium signals to subtraction approaches and variations in simple spike-to-calcium transformations. Simulations were not intended to be biophysically realistic. For each simulation, the geometry of spines and soma, sample rate, and total duration were derived from an actual imaging session. We ran three models of spike-to-calcium transformations with increasing complexity: 'Linear', 'Indicator Nonlinear', and Full Nonlinear. The following describes the Full Nonlinear model as the other models were simplifications thereof. We generated Poisson input (spine) and output (soma) spike trains with a wide distribution pairwise correlations (range of r:~0 -0.8, mean rate: 1.5 Hz) that were also traversal-distance dependent. This was accomplished by generating a random positive-semidefinite covariance matrix q, then adjusting it to be traversal-distance dependent according to: where a pre is the amplitude of traversal-distance dependent correlations and U is a weight matrix in which x i;j is the traversal distance between spines i and j, and l pre is the length constant of pre-synaptic correlations. We then calculated the positive-semidefinite matrix with unit diagonal that is closest to l (Higham, 2002) and used this covariance matrix to specify correlated Poisson spike trains (Macke et al., 2009). To implement input lags ( Figure 6-figure supplement 1), spine spike trains were temporally shifted with respect to the soma spike train. From these spike trains, we calculated a linear component of depolarization at spine i: where a s is the depolarization produced by one pre-synaptic spike, e s i t ð Þ are the events (spikes) at spine i, a c is the depolarization produced by the bAP, e c t ð Þ are the events (spikes) at the cell body (soma), a coop controls the overall cooperativity between spines, and w i;j is the distance-dependent cooperativity between spines i and j given in which x i;j is the traversal distance between spines i and j, and l coop is the length constant of cooperativity.
We then calculated a nonlinear component at spine i: where m i t ð Þ represents nonlinear voltage-dependent unblocking of channels given in which n nl is a cooperativity coefficient and K nl is the depolarization of 50% unblock. h t 0 À Á represents the dynamics of this component given in which t nl is the time constant of decay for the nonlinear component. Calcium in the spine was modeled according to: where r is the relative strength of the nonlinear component. Calcium was transformed to fluorescence, f , by the indictor by convolution linear exponential filter followed by a stationary nonlinearity: where n g is an indicator cooperativity coefficient, K g represents half-saturation of the indicator, k i t ð Þ is the convolution kernel in which t s i is the time constant of decay for spine i. To simulate the potential for localized differences in spine dynamics (for example, on thick versus thin branches) t s i was traversal distance dependent, drawn from a random multivariate normal distribution with covariance specified by exp Àx i;j l decay À Á . The soma time constant of decay was set by t c . f was then linearly scaled such that the 99.5 percentile of f (across all spines and samples) matched the 99.5 percentile of dendritic spine Df =f 0 typically observed in high SNR real data (f max ). Finally, Gaussian noise was added to spine (s s Þ) and soma (s c ) fluorescence.
For Figure 6-figure supplement 1, the following parameters were used for 'Full Nonlinear': a pre ¼ 0:5; l pre ¼ 10 m; a s ¼ 1; a c ¼ 3; a coop ¼ 0:5; l coop ¼ 10 m; n nl ¼ 2; K nl ¼ 5; t nl ¼ 100 ms; r ¼ 3; n g ¼ 2; K g ¼ 12; l decay ¼ 32 m; t s ¼ 0:24 sec mean across spines ð Þ; t c ¼ 0:4 sec; f max ¼ 16 Df =f 0 . Parameters for "Indicator Nonlinear" were the same as 'Full Nonlinear' except: a coop ¼ 0; r ¼ 0, which effectively removed cooperativity between spines and all nonlinearities except the indicator nonlinearity. Parameters for 'Linear' were the same as "Indicator Nonlinear", except for omission of the stationary nonlinearity step such that f i t ð Þ ¼ Ca ½ i t ð Þ Ã k i t ð Þ. One session (derived from one real L2/ 3 session with soma imaging) was simulated for each combination of transformation and lag in Figure 6-figure supplement 1.
For Figure 6-figure supplement 2, all parameters were the same as for simulations for Figure 6-figure supplement 1, with the following exceptions. For all 'Presynaptic Clustering' simulations a pre ¼ 0:5, a coop ¼ 0, and l pre was varied as indicated. For all 'Postsynaptic Cooperativity' simulations a coop ¼ 0:5, a pre ¼ 0, and l coop was varied as indicated. For all simulations in Figure 6figure supplement 2 where l= 0 is indicated, we set both a pre ¼ 0 and a coop ¼ 0: For each combination of distance-dependent process, l, and transformation we simulated 23 sessions (derived from the real L2/3 sessions with soma imaging).
The cartoon tuning curves, input-output correlations, and the pairwise correlations between spines in Figure 6 are shown for didactic purposes. They are simplifications of our conclusions from simulation results in Figure 6-figure supplements 1 and 2.