State-aware detection of sensory stimuli in the cortex of the awake mouse

Cortical responses to sensory inputs vary across repeated presentations of identical stimuli, but how this trial-to-trial variability impacts detection of sensory inputs is not fully understood. Using multi-channel local field potential (LFP) recordings in primary somatosensory cortex (S1) of the awake mouse, we optimized a data-driven cortical state classifier to predict single-trial sensory-evoked responses, based on features of the spontaneous, ongoing LFP recorded across cortical layers. Our findings show that, by utilizing an ongoing prediction of the sensory response generated by this state classifier, an ideal observer improves overall detection accuracy and generates robust detection of sensory inputs across various states of ongoing cortical activity in the awake brain, which could have implications for variability in the performance of detection tasks across brain states.

cortical activity in the awake brain, which could have implications for variability in the 23 performance of detection tasks across brain states. 24 25 Author summary 26 Establishing the link between neural activity and behavior is a central goal of 27 neuroscience. One context in which to examine this link is in a sensory detection task, in which 28 an animal is trained to report the presence of a barely perceptible sensory stimulus. In such tasks, 29 both sensory responses in the brain and behavioral responses are highly variable. A simple 30 hypothesis, originating in signal detection theory, is that perceived inputs generate neural activity 31 that cross some threshold for detection. According to this hypothesis, sensory response 32 variability would predict behavioral variability, but previous studies have not born out this 33 prediction. Further complicating the picture, sensory response variability is partially dependent 34 on the ongoing state of cortical activity, and we wondered whether this could resolve the 35 mismatch between response variability and behavioral variability. Here, we use a computational 36 approach to study an adaptive observer that utilizes an ongoing prediction of sensory 37 responsiveness to detect sensory inputs. This observer has higher overall accuracy than the 38 standard ideal observer. Moreover, because of the adaptation, the observer breaks the direct 39 link between neural and behavioral variability, which could resolve discrepancies arising in past 40 studies. We suggest new experiments to test our theory. that sensory-evoked activity in primary cortical areas varies across repeated presentations of a 46 stimulus, particularly when the sensory stimulus is weak or near the threshold for sensory 47 perception (1-3), and have suggested that this is an equally important aspect of sensory coding 48 as the average response (4-6). Variability is thought to arise from a complex network-level 49 interaction between sensory-driven synaptic inputs and ongoing cortical activity, and single-trial 50 response variability is partially predictable from the ongoing activity at the time of stimulation. 51 A large body of work has focused on characterizing this relationship between notions of cortical 52 "state" and sensory-evoked responses (7-13), establishing some simple models of local cortical 53 dynamics (14). Less is known about the impact of this relationship for downstream circuits 54 (though see (15)). 55 56 As an example, consider the detection of a sensory stimulus, which has been foundational 57 in the human (16-21) and non-human primate psychophysical literature (22,23) and serves as 58 one of the most widely utilized behavioral paradigms in rodent literature (24)(25)(26). In an attempt 59 to link the underlying neural variability to behavior, the principal framework for describing 60 sensory perception of stimuli near the physical limits of detectability is signal detection theory 61 (27). A key prediction of signal detection theory is that, on single trials, detection of the stimulus 62 is determined by whether the neural response to the stimulus crosses a threshold. Particularly 63 large responses would be detected but smaller responses would not, so variability in neural 64 responses would lead to, and perhaps predict, variability in the behavioral response. From the 65 perspective of an ideal observer, if variability in the sensory-evoked response can be forecasted 66 using knowledge of cortical state, the observer could potentially make better inferences, but in 67 traditional (state-blind) observer analysis, the readout of the ideal observer is not tied to the 68 ongoing cortical state. 69 70 4 In this work, using network activity recordings from the whisker sensitive region of the 71 primary somatosensory cortex in the awake mouse, we develop a data-driven framework that 72 predicts the trial-by-trial variability in sensory-evoked responses in cortex by classifying ongoing 73 activity into discrete states that are associated with particular patterns of response. The classifier 74 takes as inputs features of network activity that are known to be predictive of single-trial 75 response from previous studies (9,14), as well as more complex spatial combinations of such 76 features across cortical layers, to generate ongoing discrete classifications of cortical state. We 77 optimize the performance of this state classifier by systematically varying the selection of 78 predictors. Finally, embedding this classification of state in a state-aware ideal observer analysis 79 of the detectability of the sensory-evoked responses, we analyze a downstream readout that 80 changes its detection criterion as a function of the current state. We find that state-aware 81 observers outperform state-blind observers and, further, that they equalize the detection 82 accuracy across states. Downstream networks in the brain could use such an adaptive strategy 83 to support robust sensory detection despite ongoing fluctuations in sensory responsiveness 84 during changes in brain state. 85 86

87
To directly assess the relationship between ongoing cortical activity and variability in the 88 sensory-evoked cortical response, we recorded extracellular activity across layers of cortex in the 89 awake head-fixed mouse. Specifically, spontaneous and sensory-evoked local field potentials 90 (LFPs) were recorded using a 32-channel laminar array targeted to the region of the primary 91 somatosensory cortex corresponding to facial vibrissae (S1 barrel cortex, Fig. 1A). Mice were 92 subjected to brief single-whisker deflections (11 recordings in 6 mice; average 438 (196 -616) 93 trials per recording). Intrinsic optical signal imaging was performed to locate the barrel column 94 corresponding to the stimulated whisker. The sensory stimulus (Fig. 1B, top) was a computer-95 controlled punctate deflection in the caudo-rostral plane (see Methods), designed to emulate 96 velocity transients observed during 'stick-slip' events in rodents whisking across surfaces (28-97 30). Cortical layers were assigned based on the trial-averaged spatial profile of the sensory-98 evoked LFP and current source density (CSD) responses, with layer 4 centered on the largest 99 6 F. Expanded view of neural activity in a 25-ms window (gray boxes in (E)). In these selected 127 examples, spontaneous fluctuations (W = -) are larger than the first sensory-evoked response (W 128 = +). 129 G. The state classifier (described in Fig. 2-4) estimated state ŝ for each trial based on ongoing/pre-130 stimulus activity. 131 H. Illustration of detection of input (W = + or W = -) based on state-blind threshold choice (black, 132 P(x|W)) and based on state-aware threshold choice (blue, P(x|W,ŝ)). In this example, the 133 threshold for state 2 was increased to reject the spontaneous activity (bottom row), while the 134 threshold for state 1 was decreased to accept the small sensory-evoked responses. 135 136 Sensory-evoked responses in layer 4 were variable: mean amplitude of the response in 137 layer 4 was a negative dip of 0.81 mV (+/-0.35 across recordings, N = 11), and the standard 138 deviation of evoked response size across trials was 0.45 mV (average SE across recordings, N = 139 11). We examined the impact of such variability on the detectability of sensory inputs in the 140 framework of ideal observer analysis, which is conceptually presented in Figure 1D. In this 141 scenario (Fig. 1D, top row), a sensory stimulus ) takes on one of two possible values: "+" in the 142 case that a sensory input was present and "−" in the absence of a sensory input. Neural activity 143 (!, see Methods), either spontaneous (W is "−") or generated by the stimulus (W is "+"), was 144 variable across trials and described by a conditional distribution ,(!|)). The task of a 145 downstream network, imagined here as an ideal observer, was to determine from the neural 146 activity whether or not there was a stimulus. In the classical signal detection framework, this was 147 envisioned as the observed activity arising from one of two distributions, ,(!|) = " − ") or 148 ,(!|) = " + "). The ideal observer ascribes activity above a chosen threshold (to the right of 149 dashed red line) as belonging to ,(!|) = " + "), and thus concludes that a stimulus was present, 150 and otherwise as belonging to ,(!|) = " − "), and thus the stimulus was deemed absent. In 151 this work, we considered an alternative perspective, which is that the ideal observer was "state-152 aware". That is, we considered the case in which the response distribution ,(!|), 3) depended 153 upon ongoing activity ("state," 3) as well as the stimulus (Fig. 1D, blue, bottom row). In this case, 154 the discrete state (3) is classified from the recorded, ongoing cortical activity, which is 155 7 subsequently used by the state-aware observer to set the detection threshold independently for 156 each state. 157

158
To illustrate how this framework operates, we show a set of example trials from a single 159 recording in Fig. 1E-H. Two of these examples of layer 4 LFP activity show responses to a whisker 160 input and one is a segment of spontaneous activity (Fig. 1E). Across the stimulus-evoked 161 responses, we observed significant variability in the overall size of sensory-evoked response (Fig.  162 1F). Moreover, one of the evoked responses (Fig. 1F, top row) is smaller than a spontaneously 163 occurring LFP event (Fig. 1F, bottom row). We also note that pre-stimulus LFP activity is quite 164 different across these recordings. The goal of the state classifier is to find consistent relationships 165 between features of the ongoing activity and the details of the single-trial sensory-evoked 166 response. Assuming for the moment that it is able to do so, the state classifier would classify the 167 pre-stimulus activity for these responses (Fig. 1E) into separate states (3 = 1 or 2, Fig. 1G). When 168 tasked with detecting or rejecting stimuli on the basis of the LFP response (Fig. 1H), an ideal 169 observer sets a single threshold for detection, which causes it to fail to detect a true sensory 170 response while generating a false alarm on the spontaneous fluctuation (Fig. 1H, black). In 171 contrast, the state-aware observer sets its detection criterion separately for each state. For this 172 example, the threshold may be lowered for state 1 and raised for state 2, thus the sensory-173 evoked responses would be detected but the spontaneous fluctuation would be rejected (Fig.  174 1H, blue). 175

176
The state-aware observer thus has two distinct stages: state classification and sensory 177 stimulus detection. The idea is that, by adapting its criterion for detection in accordance with the 178 expected response, the state-aware observer will more reliably detect sensory-evoked responses 179 and reject spontaneous fluctuations. Overall, the success of this strategy depends on a state 180 classifier that predicts variation in the future sensory-evoked response, so we first optimized 181 classification models with the goal of identifying the most relevant features of ongoing activity 182 for the prediction of the details of the sensory-evoked responses. We then use this framework 183 to classify ongoing activity into states and compare traditional (state-blind) and state-aware 184 8 observers to determine how using this prediction to adjust detection strategy impacts overall 185 detection performance. 186 187

State classification based on prediction of cortical responses 188
The foundation upon which the state-aware observer is constructed is a prediction of the 189 sensory-evoked cortical response. This prediction is based on classifying elements of the ongoing, 190 pre-stimulus activity into discrete "states," and the goal is to find the features of ongoing activity 191 and the classification rules that generate the best prediction of sensory-evoked responses. The 192 features of ongoing activity include the power spectrum of pre-stimulus LFP and the 193 instantaneous "LFP activation" (Fig. 2A). To describe sensory-evoked responses, we define a 194 parameterization of the LFP response using principal components analysis (Fig. 2B). The state 195 classifier is a function that takes as inputs features of pre-stimulus LFP and produces an estimate 196 of the principal component (PC) weights and thus of the single-trial evoked response (Fig. 2C). In 197 the following sections, we describe this process in detail. Five ranges are defined based on quintiles. Color indicates the PC1 ranges: largest responses, 207 orange; smallest responses, blue. Averages of responses falling into the 1st, 3rd, and 5th quintiles 208 of PC1 weights and PC2 weights are plotted, colored by the PC1 quintile (orange, green, blue) 209 with the overall average evoked response in gray. 210 C. Classifier boundaries in power ratio-activation space that predict the PC1 weights. Regions are 211 labeled from 1 ("small") to 5 ("large") and colored as in panel B. See Supporting Figures 1 and 2  212 for boundaries for other recordings. 213 9 D. Fraction of variance explained by the classifiers on trial-shuffled data and on the test set. 214 215 Initial selection of features of ongoing activity. The first step was to select features of ongoing 216 activity, which are the inputs to the state classifier. Based on previous work in somatosensory 217 cortex (8,9), we first identified two features that reflect the instantaneous phase and 218 synchronization of ongoing activity measured through LFP. To capture instantaneous phase, we 219 used pre-stimulus LFP activation, defined as the average LFP recorded over the 10 ms preceding 220 the stimulus, relative to the baseline activation level (LFP averaged from -1000 ms to -200 ms, 221 Fig. 2A, top). Synchronization is reflected by the low-frequency power in the LFP, quantified as 222 the ratio of power between 1 and 5 Hz (denoted here as "L") to the total power between 1 and 223 50 Hz (denoted here as "W", for wideband, Fig. 2A, bottom). Power ratio was computed using 224 the fast Fourier transform over the 2-second pre-stimulus period. 225

226
Parametrizing variability in evoked responses. The next step was to define a discrete 227 parameterization of the sensory-evoked LFP response, which will be the targeted output of the 228 state classifier. We focused on the single-trial sensory evoked LFP response in cortical layer 4 in 229 a 25-ms period following the delivery of the sensory stimulus. We parameterized the evoked 230 response using the principal components (PCs) of the evoked response across trials. On average, 231 the first two modes accounted for 89 % ± 2 % (N=11 recordings) of the cross-trial variance. Each 232 trial was therefore described as the average response, plus the weighted sum of the first two PCs 233 Prediction performance was quantified using the fraction of variance explained (fVE) over 259 the 25-ms evoked response window (Methods). We partitioned the data into three sets: one for 260 fitting classifiers ("training"), one for selecting the best pre-stimulus features ("cross-validation") 261 and one for quantifying fVE ("test"). This was important to avoid spuriously high performance of 262 optimized classifiers due to over-fitting (see Methods for details). Using activation and power 263 ratio (as defined in Fig. 2A) as pre-stimulus features, we found that the fVE was 0.18 ± 0.03, which 264 was significantly larger than the fVE when trials were shuffled within each recording (average fVE 265 for trial-shuffle: 0.001 ± 0.002, N = 11 recordings; Fig. 2D). Using for each recording the power ratio based on the optimized range of low-frequency 289 power (Fig. 3), we next determined where along the cortical depth the most predictive activity 290 was and whether taking spatial combinations of LFP activity could improve the prediction. Note 291 that in this analysis, the channel for the stimulus-evoked response was held fixed (L4) and thus 292 the parameterization of the evoked response using principal components did not change, but the 293 pre-stimulus channel was varied. For each recording, we thus built a series of classifiers, using 294 single-and multi-channel LFP activity from across the array ( the optimal channel only) and LFP activation from each of the two channels and compared the 326 fVE to that obtained using the optimal classifier channel only (Fig. 4D). We found no improvement 327 in the prediction using the pair combination compared to using the optimal channel alone ( Fig. 13 4D, mean fVE difference: 0.00 ± 0.01; 0/11 recordings with significant change, pair vs. single) or 329 using more complex combinations of channels (Supplemental Figure 3). 330

331
To summarize, we optimized classifiers based on pre-stimulus features to predict single-trial 332 sensory-evoked LFP responses in S1 cortex of awake mice. We found that the classifier 333 performance was improved by changing the definition of the power ratio (L/W) such that the 334 low-frequency range (L) extended from 1 Hz to 27 Hz, depending on the recording, which differed 335 from the range typically used from anesthetized recordings in S1 (1-5 Hz) (8,9). We also found 336 that the most predictive pre-stimulus LFP activation was near was near layer 4. 337 338

Ideal observer analysis of sensory-evoked responses. 339
After establishing a clear enhanced prediction of the single-trial stimulus-evoked 340 response within the LFP by considering the pre-stimulus activity, we investigated the impact of 341 this relationship on the detection of sensory stimuli from cortical LFP activity using a state-aware 342 ideal observer analysis. We first considered a simple matched-filter detection scheme (37) in 343 which the ideal observer operated by comparing single-trial evoked responses to the typical 344 shape of the sensory evoked response (Methods, Detection). The matched filter was defined by 345 the trial-average evoked LFP response, and this filtered the raw LFP ( Fig. 5A) to generate the LFP 346 score (Fig. 5B). For the state-blind observer, a detected event was defined as a peak in the LFP 347 score that exceeded a fixed threshold (Fig 5B, stars). The LFP score distributions from time 348 periods occurring during known stimulus-evoked responses and from the full spontaneous trace 349 were clearly distinct but overlapping ( 6A, inset). To compare between traditional (state-blind) and state-aware observers, we 380 compared hit rates at a single false alarm rate, determined for each recording as the false alarm 381 rate at which 80%-90% detection was achieved by a state-blind ideal observer. To select 382 thresholds for the state-aware observer, we systematically varied the thresholds in state 1 and 383 state 3, while adjusting the state-2 threshold such that average false alarm rate was held 384 constant. For each combination of thresholds, we computed the overall hit rate (Fig. 6C). For this 15 recording, the state-aware observer (hit rate: 96%) outperformed the traditional one (hit rate: 386 90%). This worked because the threshold in state 3 could be increased with very little decrease 387 in the hit rate (Fig. 6B), and this substantially decreased the false alarm rate in state 3 (Fig. 6A). 388 Because the overall false alarm rate is fixed, this meant more false alarms could be tolerated in 389 states 1 and 2. Consequently, thresholds in states 1 and 2 could be decreased, which increased 390 their hit rates. Across recordings, we found that the state-aware observer outperformed the 391 state-blind observer in 9 of 11 recordings ( Fig. 6D; Supplemental Figure 4-5). Hit rates slightly but 392 significantly increased from a baseline of 81% for the state-blind observer to 84% for state-aware 393 detection, or an average change of +3 percentage points (SE: 3%; signed-rank test, p < 0.01, N = 394 11). Although this is a modest change, overall hit rates were computed from the average of hit 395 rates in each state, weighted by the amount of time spent in each state. To separate these 396 factors, we analyzed the hit rate of the state-blind and state-aware observers by computing, for 397 each observer, the hit rate conditioned on each pre-stimulus state ( Fig 6E). For this recording, 398 the state-blind observer had very low hit rate in state 1 and high hit rates in states 2 and 3. In 399 comparison, hit rates were similar across the three state for the state-aware observer (Fig. 6D). 400 Thus, in state 1 (smallest responses, blue), we observed a large increase in the hit rate depending 401 on whether the observer used state-blind or state-aware thresholds. Averaged across all 402 recordings, the state-1 hit rates increased from 60% to 76%. This is a relative increase of 26% (SE 403 11%), which is substantial. Because this is weighted by the fraction of time spent in state 1, the 404 overall impact on the hit rate is smaller. Hit rates increased slightly on average in state 2 (+ 2%, 405 SE 4%) and decreased slightly in state 3 (-7%, SE 9%). The net impact of this is that across the 406 majority of recordings, the cross-state range of hit rates for the state-blind ideal observer was 407 much larger than that for the state-aware ideal observer (Fig. 6D, Fig. 6F

434
Due to the rapid development of tools that enable increasingly precise electrophysiology 435 in the awake animal, there is a growing appreciation that the "awake brain state" encompasses 436 a wide range of different states of ongoing cortical activity, and that this has a large potential 437 impact on sensory representations during behavior (38-43). Here, we constructed a framework 438 for the prediction of highly variable, single-trial sensory-evoked responses in the awake mouse 439 based on a data-driven classification of state from ongoing cortical activity. In related work, past 440 studies have used some combination of LFP/MUA features to successfully enhance the prediction 441 of future evoked MUA response (9,14). In this study, we extended these types of approaches for 17 state classification and response prediction to cortical recordings in the awake animal, opening 443 up the problem to allow complex combinations of ongoing activity in space as well as different 444 features of the power spectrum of the pre-stimulus activity. We found that simple features of 445 pre-stimulus activity sufficed to enable state classification that yielded single-trial prediction of 446 sensory evoked responses. These predictive features were analogous to the synchronization and 447 phase variables found in previous studies (8,9,14), though we found a revised definition of 448 synchronization was more predictive. In particular, we found that the very low-frequency band 449 of the LFP power spectrum (1-5 Hz) was less predictive of single-trial evoked responses in our 450 recordings than a wider band (e.g. 1 to 27 Hz). This is consistent with findings from a recent study 451 We found that the state-aware observer had higher accuracy than the traditional, state-492 blind observer, but the absolute gain in hit rate (at fixed false alarm rate) averaged across all 493 states was modest. When pre-stimulus states were analyzed separately, however, we found that 494 accuracy in the low-response state was substantially higher for the state-aware observer, where 495 there was a relative increase of 25% in the hit rate for this state. Because small sensory responses 496 are predictable from the ongoing activity, transiently lowering the threshold for detection 497 resulted in more "hits" in the low-response state, while false alarms in high-response states could 498 be avoided by raising the threshold when the state changed. However, the cortical activity was 499 classified to be in this particular state only approximately 20% of the time, and thus had a 500 19 relatively modest impact on the overall performance, averaged across all states. What is not 501 currently known is the overall statistics associated with the state transitions (i.e. distribution of 502 time spent in each state, rate of transitions, etc.) during engagement within perceptual tasks, but 503 in any case, what we observe here is a normalization of detectability across brain states. activity should be associated with higher or lower performance on a near-threshold detection 516 task, which has been observed in near-threshold detection studies in the rodent (25) and monkey 517 (23). It should be noted that there is controversy regarding the relevance of primary sensory 518 cortex in simple behavioral tasks (52,53), but this is likely related to the task difficulty (54), where 519 a large body literature has resolutely shown that processing in primary cortical areas is critical 520 for difficult tasks that increase cognitive load, and we suspect that near threshold stimuli such as 521 those shown here fall in that category. 522 523 Many studies have demonstrated a link between pre-stimulus cortical activity and 524 perceptual report on near-threshold detection tasks in humans (16,17,55-58). Currently, it is not 525 entirely clear how far the parallel in cortical dynamics between the mouse and human can be 526 taken. One challenge is that connecting invasive recordings in the mouse to non-invasive 527 recordings in human studies is non-trivial. Here, at the level of LFP, we observed similarities 528 between species in the interaction between ongoing and evoked activity: the largest evoked 20 responses tended to be preceded by positive deflection in the LFP, and the smallest evoked 530 responses were preceded by negative deflection in the LFP. This relationship, the negative 531 interaction phenomenon, points to a non-additive interaction between ongoing and evoked 532 activity and is also observed in both invasive and non-invasive recordings in humans 533 (32,55,59,60). Establishing parallels between cortical dynamics on a well-defined task, such as 534 sensory detection, between humans and animal models is an important direction for future 535 studies. 536

537
In summary, we have developed a framework for the prediction of highly variable, single-538 trial sensory-evoked responses and shown that this prediction based on cortical state 539 classification can be used to enhance the readout of sensory inputs. Utilizing state-dependent 540 decoders for brain-computer interfaces has been shown to greatly improve the readout of motor 541 commands from cortical activity (61,62), at the very end-stage of cortical processing. We suggest 542 this natural extension of signal detection theory shows how to solve a problem that the brain 543 faces at each stage of processing: how to adaptively read out a signal from a dynamical system 544 constantly generating its own internal activity. For analysis, the LFP was down-sampled to 2 kHz. The LFP signal entering the processing pipeline 584 is raw, with no filtering beyond the anti-aliasing filters used at acquisition, enabling future use of 585 these methods for real-time control. Prior to the analysis, signal quality on each channel was 586 verified. We analyzed the power spectrum of LFP recorded on each channel for line noise at 60 587 Hz. In some cases, line noise could be mitigated by fitting the phase and amplitude of a 60-Hz 588 sinusoid, as well as harmonics up to 300 Hz, over a 500-ms period in the pre-stimulus epoch, then 589 extrapolating the sinusoid over the stimulus window and subtracting. A small number of channels 590 displayed slow, irregular drift (2 or 3 of 32 channels) and these were discarded. All other channels 591 were used. 592

CSD calculation 594
Current source density (CSD) analysis was used for two different purposes: first, to functionally 595 determine layers based on the average stimulus-evoked response, and second, to analyze the 596 pre-stimulus activity (in single trials) to localize sinks and sources generating the predictive signal. 597 We describe the general method used here. Prior to computing the current source density (CSD), 598 each channel was scaled by its standard deviation to normalize impedance variation between 599 electrodes. We then implemented the kernel CSD method

Analysis framework 617
Defining features of ongoing LFP. The ongoing LFP activity was characterized over the 2-s pre-618 stimulus window using "activation" and "power ratio." LFP activation is defined here as the LFP 619 averaged over the 10 ms pre-stimulus relative to the baseline value of the LFP averaged over -620 1000 ms to -200 ms. We observed that the estimate of pre-stimulus phase was biased by filter 621 leakage from the evoked response (if the evoked response was not truncated) and biased by 622 truncation otherwise. This filter leakage causes a spurious increase in classifier performance, 623 because it reflected not only the evoked response but also the single-trial variability in the evoked 624 response, which was easily read out by the classifier. Therefore, we used the simpler measure of 625 LFP activation in place of pre-stimulus LFP phase. For the weighted average multi-channel LFP 626 activation (Fig. 4), we performed a spatial PCA during the spontaneous activity and retained up 627 to 3 spatial modes, or the number of modes that explained >95% of the variance, whichever was 628 fewer. We then projected the pre-stimulus multi-channel LFP into spatial PC space. The power 629 spectral density (PSD) was estimated over the 2-s pre-stimulus period on single trials using the 630 fast Fourier transform. The power ratio was computed from the PSD curve as the area over the 631 "L" range (1-5 Hz, or variable) normalized by the area over the "W" range (1-50 Hz). Analyses 24 were also carried out using a 2-s pre-stimulus window and multi-taper PSD estimate (with NW = 633 2, 3 tapers), and the results were qualitatively unchanged.  Note that the LFP at the time of stimulus delivery is subtracted in order to return the evoked 663 response (J & K ). The average evoked response over H Q trials is R S : 664 and the single-trial response is written as the sum of the mean response and a noise term T: 666 Here, we expand the evoked response at time G ' in terms of its first H U components across trials: 668 Fitting the state classifier. Here, we are motivated to find the pre-stimulus features that best 673 predict single-trial evoked responses, and to then define "cortical state" based on those 674 combinations of features that produce similar cortical responses. Therefore, we aim to find some 675 function of the pre-stimulus features that generates a prediction of the coefficients (V & K ,W ). The 676 predicted response for a stimulus at time G ' is J & K : 677 We considered the first two components, so H U = 2. Evoked response weights (V & K ,W ) were split 679 into equipartitioned groups based on quintiles of the weight distribution. Taking as predictors 680 (inputs to the classifier) the features of pre-stimulus activity described above, the classifier was 681 trained to predict to which group the evoked response belonged. Each evoked mode (_) is 682 independently predicted. 683 26 Each recording session was divided into a training/cross-validation set (70% of trials) and a test 685 set (30% of trials). Classifiers were fit on the training/CV set using five-fold cross-validation, and 686 predictions and accuracy were recorded for the cross-validation trials. Optimized parameters, 687 such as ranges for the L range of the power ratio, were selected on the basis of error in the cross-688 validation set. Reported classifier performance (fraction of variance explained, fVE, see below) 689 was then calculated on the reserved test set. For the Gaussian-kernel SVM, we used a medium-690 scale kernel (3; predictors range from 0 to 1 (power raito) and +/-1 (mV, activation)) and box 691 constraint of 1. Results were not sensitive to this choice. We also tested linear kernel SVMs as 692 well as more complicated decoders, such as k-nearest neighbor classifiers, but did not find any 693 increase in performance across recordings (not shown). All classifiers were fit using built-in 694 Matlab routines, fitcecoc, from the classification toolbox (MATLAB 2017a, Mathworks, Natick, 695 MA). 696 697

Classifier performance quantification (fVE). Fraction of variance explained (fVE) is defined as the 698
normalized difference between the total trial-by-trial variance (summed over time) and the trial-699 averaged squared error between the predicted response and the data (also summed over time): 700 Shuffled data fVE: trial labels (i.e. the quintile of the PC distribution to which a trial was assigned) 703 were shuffled. The classifier (fit on unshuffled data) was applied to the pre-stimulus activity, and 704 fVE was computed for the predicted classes supposing that the shuffled labels were accurate. 705 706 Uncertainty in fVE from finite quantity of data. Differences in fVE between classifiers arise from 707 differences in the accuracy of each classifier in predicting a subset of reserved data and therefore 708 partially reflect the true accuracy as well as random effects arising from the finite quantity of 709 data. We use a resampling approach to estimate the uncertainty in fVE. Because the test set is 710 stratified (i.e. it is balanced across labels (quintiles)), we use the leave-one-out jackknife: This is equivalent to computing the sum 721

722
In the standard encoding model, if T is zero-mean white noise, this gives a signal distribution 723 , 3~H( R l , m 8 ) 724 where m 8 = R l 8 m n 8 and a noise distribution with mean 0. In practice, we do not parameterize 725 the distribution, because T is not uncorrelated white noise, and work from the score distribution 726 directly. 727 For the state-aware decoder, we use the prediction V &,W of evoked responses 728 This changes the score to 730 Typically, one of the first two PCs (R 7 or R 8 ) has a very similar shape to R S , while the other one 732 has both positive and negative components (Fig. 2, Suppl. Fig. 1-2). For the state-aware threshold, 733 we use state predictions for the component that is more similar to R S , as indicated in 734 Supplemental Figure 1-2

. 735
An event is detected at time G for threshold p when s G > p is a local maximum that is separated 736 from the nearest peak by at least 15 ms and has a minimum prominence (i.e. drop in 3 before 737 encountering another peak that was higher than the original peak) of R S 8 /2.  A. Recordings of whisker-evoked neural activity in the principal barrel column were acquired using a laminar probe in awake mice. B. A single trial response to the punctate whisker stimulus (galvanometer trace, pink line at top), showing LFP activity across the 32 channels of the silicon probe (25 um spacing), with 1 s of pre-stimulus activity (x prestim , horizontal blue bar) and the sensory-evoked response starting at the time point indicated by the arrow (pink). C. Average spatial profile of evoked LFP at t = 10 ms post-stimulus (left). Right: average evoked CSD over the 25-ms post-stimulus window, with color indicating whether sink (red) or source (blue). Channels were assigned to layers (dashed horizontal lines separating sections labeled L2/3, L4 and L5) by centering the largest evoked response in layer 4. D. State-blind analysis (top row, black font): Neural responses encode sensory inputs, described by the encoding distribution P(x|W) of response (x) conditioned on stimulus identity (W, here the presence (+) or absence (-) of a sensory input). An ideal observer determines the stimulus from the response using P(W|x). State-aware analysis (bottom row, blue font): sensory responses also depend on cortical state (s). Here we estimate ŝ by computing a function of the pre-stimulus neural activity (x at time t' < t). An ideal observer sets a state-dependent threshold depending on the estimated state ŝ. Fig.  2-4: estimation of ŝ. Fig. 5-6: state-aware ideal observer analysis using P(W|x, ŝ). E-H. Illustration of the detection framework applied to an example drawn from recorded data. E. The recorded LFP in layer 4 is shown for three example trials, with W = + (top two) and W = -(bottom). F. Expanded view of neural activity in a 25-ms window (gray boxes in (E)). In these selected examples, spontaneous fluctuations (W = -) are larger than the first sensory-evoked response (W = +). G. The state classifier (described in Fig. 2-4) estimated state ŝ for each trial based on ongoing/pre-stimulus activity. H. Illustration of detection of input (W = + or W = -) based on state-blind threshold choice (black, P(x|W)) and based on state-aware threshold choice (blue, P(x|W,ŝ)). In this example, the threshold for state 2 was increased to reject the spontaneous activity (bottom row), while the threshold for state 1 was decreased to accept the small sensory-evoked responses. Features of the pre-stimulus activity used for the prediction. Top row: LFP activation is the LFP in the 10 ms preceding stimulus onset (pink arrow) minus the LFP averaged over the baseline window. Bottom row: power ratio is the ratio of LFP power in the 1-5 Hz range (L) to the power in the 1-50 Hz range (W).

B.
Top row: parameterization of the early (25-ms) sensory-evoked LFP response through principal components. Bottom row: Illustration of evoked responses represented in PC1-PC2 weight space. Five ranges are defined based on quintiles. Color indicates the PC1 ranges: largest responses, orange; smallest responses, blue. Averages of responses falling into the 1st, 3rd, and 5th quintiles of PC1 weights and PC2 weights are plotted, colored by the PC1 quintile (orange, green, blue) with the overall average evoked response in gray.

C.
Classifier boundaries in power ratio-activation space that predict the PC1 weights. Regions are labeled from 1 ("small") to 5 ("large") and colored as in panel B. See Supporting Figures 1  and 2 for boundaries for other recordings.

D.
Fraction of variance explained by the classifiers on trial-shuffled data and on the test set. B. LFP traces convolved with a filter matched to the average evoked response generate the LFP score. Peaks in the score that are above a threshold (gray) are detected as events (pink asterisks). C. Distribution of scores during spontaneous activity (dark gray) and of sensory-evoked responses (light gray). The two distributions are clearly distinct but with an overlapping area, leading to spontaneous events being detected as sensory-evoked responses (false alarms). D. Same example traces as in A, but with the "state" at each point in time indicated by the color overlay.
E. An example of how the detection threshold could be adjusted depending on the value of the ongoing state. F. Separating by ongoing state generates three sets of spontaneous/evoked score distributions (analogous to C).    A. False alarm (FA) rate vs. threshold for each pre-stimulus state (colors) as well as the full dataset (black dashed). Pie chart: fraction of time spent in each state. White fill circles: threshold for state-blind observer. Vermillion fill circle: threshold for state-aware observer (see C). Combined with the fraction of time spent in each state (pie, A), this sets the overall FA rate. B. Hit rate vs. threshold for each pre-stimulus state (colors) as well as the full dataset (black dashed). Combined with the fraction of time spent in each state (pie, A), this sets the overall hit rate. Circles marks same thresholds as in (A). C. Hit rate of state-aware observer for combinations of threshold that generate the same (1.6 Hz) FA rate is indicated by grayscale. Circles correspond to the combinations of thresholds shown in A and B. Dashed lines show region with no solution satisfying constraint on FA rate. D. Change in hit rate between state-blind and state-aware observers. Thresholds are chosen based on the optimization shown in (C), and hit rates are computed on a reserved set of trials and compared to the fixed-threshold hit rate at equivalent false alarm rate. E. For the example recording, we parse hit rate by pre-stimulus state for the state-blind and state-aware observers. The cross-state range of hit rates is defined as the range of hit rates observed across the three states. F. Cross-state range of hit rates for the state-aware observer versus cross-state range for the state-blind observer (see D). The cross-state range is smaller for the state-aware observer, meaning that hit rates are more consistent across pre-stimulus states for the optimal state-aware observer.  Rec. 4 Rec. 5 Rec. 1 Supplemental Figure 4 (associated with Figure 6): Optimized thresholds for the state-aware observer. Panels A-D of Figure 6 for recordings 1 through 5.    Figure 5 (associated with Figure 6): Panels A-D of Figure 6 for recordings 7 through 11. See SFig 4 for caption. Recording 6 is shown in Figure 6.