Multivariate classification of multichannel long-term electrophysiology data identifies different sleep stages in fruit flies

Sleep is observed in most animals, which suggests it subserves a fundamental process associated with adaptive biological functions. However, the evidence to directly associate sleep with a specific function is lacking, in part because sleep is not a single process in many animals. In humans and other mammals, different sleep stages have traditionally been identified using electroencephalograms (EEGs), but such an approach is not feasible in different animals such as insects. Here, we perform long-term multichannel local field potential (LFP) recordings in the brains of behaving flies undergoing spontaneous sleep bouts. We developed protocols to allow for consistent spatial recordings of LFPs across multiple flies, allowing us to compare the LFP activity across awake and sleep periods and further compare the same to induced sleep. Using machine learning, we uncover the existence of distinct temporal stages of sleep and explore the associated spatial and spectral features across the fly brain. Further, we analyze the electrophysiological correlates of micro-behaviours associated with certain sleep stages. We confirm the existence of a distinct sleep stage associated with rhythmic proboscis extensions and show that spectral features of this sleep-related behavior differ significantly from those associated with the same behavior during wakefulness, indicating a dissociation between behavior and the brain states wherein these behaviors reside.

Humans spend a third of their life engaged in sleep, wherein they become less responsive to external 32 stimuli. Most animals studied so far, starting from the tiny fruit fly to the large sperm whale (Miller et  One of the primary challenges for understanding sleep architecture has been developing a capacity to 50 record and assess brain-wide patterns of electrical activity across long time periods that encompass 51 several sleep-wake transitions. In this context, small animals such as the fruitfly Drosophila 52 melanogaster present as extremely challenging subjects, even though they could potentially provide a 53 wealth of molecular genetic tools to help better understand sleep biology. Previous sleep studies in flies 54 have either recorded from just a single LFP channel during spontaneous sleep bouts (Yap et al. 2017; 55 ( Figure 1C, red trace). Since PEs were also often rhythmic during sleep, we characterized both micro-114 behaviors in the frequency domain ( Figure 1D,E, top) to determine if these were different between 115 sleep and wake. We found that a greater proportion of the sleeping states displayed both antennal 116 periodicity as well as PE periodicity, compared to the waking states ( Figure 1D,E, bottom), and that 117 antennal periodicity occurred at a small but significantly lower frequency during wake ( Figure S1G).  (for multiple comparisons) to identify differences between pairs that are significant. Thus, we found an 136 apparent increase in the likelihood of periodicity for both antennae and proboscis during the middle 137 segments of sleep bouts ( Figure 1F,G). This suggested physiological differences which might be detected 138 in the fly brain, so we then performed electrophysiological recordings in a similar context. 139 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint before end of sleep. The normalized count is significantly higher in the midsleep segments compared to other 152 segments. G) As with F, but for periodic extensions of the proboscis***p<0.001, ns indicates not significant.

154
Long-term multichannel recordings with spontaneous sleep bouts. 155 We recorded local field potentials (LFPs) across the fly brain using a linear 16-channel electrode 156 inserted into the left eye of flies in a similar context as above, walking (or resting) on an air-supported 157 ball (Figure 2A,B). The electrode insertion location was positioned to sample LFPs from the retina to recording channel locations in the brain for two sample flies. Using this method we identified three 168 broadly-defined brain recording regions to simplify our subsequent analyses ( Figure 2D): central 169 channels (1-5), middle channels (6-10) and peripheral channels (12-16); here assuming polarity reversal 170 in channel 11. Also for further analysis, as the polarity reversal channel is used for re-referencing, the 171 number of channels used in analysis becomes 15. CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made

193
The behavior of the flies was recorded under infrared lighting ( Figure 2F) and their movements were sleeping more at night on average. We found that flies were able to sleep in this preparation, and that 203 nighttime sleep bouts were indeed longer than daytime sleep bouts (median = 22.42 min vs 13.99 min, 204 respectively; t(13) = -2.32, p<0.05) ( Figure 2H). This confirms that similar to single channel LFP preparation, allowing us to assess changes in LFP activity across the fly brain during sleep and 207 wakefulness, and to relate these changes to sleep micro-behaviors. 208 209 LFP differences across the brain during spontaneous sleep and awake.

210
Next, we focused on the multichannel data to identify potential differences between sleep and wake 211 across the fly brain, separating our recordings into three broad regions: central, middle, and peripheral 212 ( Figure 3A). An example sleep bout and its corresponding spectrograms across the central, middle, and 213 peripheral channels reveals increased activity during sleep in the central brain compared to the 214 periphery ( Figure 3B). Additionally, we noted variegated effects in the lower frequencies (5-10 Hz) 215 within the sleep bout ( Figure 3B, arrowheads) as well as significant LFP activity (5-40 Hz) associated 216 with locomotion. 217 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made

220
-central) are illustrated on an outline of a standard drosophila brain. B) Spectrogram in different channels groups 221 across an example sleep bout shows variation (magenta arrowheads) in the lower frequency bands (5-10 Hz) 222 within the sleep bout, while activity across 5-40 Hz in the flanking awake period. C) Movement area (activity 223 pattern) plotted along with 'awake' and 'sleep' state labeling for this example sleep bout.

224
. CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint When we examined sample LFP data more closely across all channels (Figure 4), we observed higher 225 LPF amplitudes in the central and middle channels than in the peripheral channels, and more activity 226 during wake than during sleep ( Figure 4A,B).

228
Interestingly, the fly brain is not necessarily quiet during sleep, with some channels (e.g., channels 5-229 7) displaying increased activity compared to other channels. To substantiate our observations, we 230 performed spectral analysis on the data. For this purpose, we epoched the LFP data into 60 sec bins and 231 computed the power spectrum per epoch per channel (See Methods for LFP analysis -preprocessing, 232 power spectrum analysis). Since LFP data recorded from flies can be sensitive to physiological artifacts 233 such as heartbeat and body movements (Paulk et al. 2013), we employed a common referencing system 234 (based on a brain based signal) that allowed for removal of non-brain based physiological noise. Plotting 235 the power spectral density across the three different channel groupings for different frequency bands 236 during sleep compared to wake, suggesting a brain-wide effect. 241

242
We next examined more closely the relationship between individual channels and LFP spectral 243 frequency between sleep and wake states. We employed non-parametric resampling tools to identify 244 the precise patterns (frequency x channel pairs) differing across awake and sleep at the group level. For 245 this purpose, we first computed the difference in mean spectral data across awake and sleep for 246 individual flies. Then, we performed a cluster permutation test (flies x frequencies x channels) on the 247 difference between awake and sleep data ( Figure 4D   in Figure 4C that found a brain-wide decrease in power during sleep compared to wake. As we had a 251 single significant cluster (magenta box), we then sought to identify subclasses of frequencies and 252 channels within this cluster which might be more specifically associated with sleep. 253 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint

261
We computed the effect sizes for every channel x frequency combination ( Figure 4E -right panel). is however clear that LFP activity is mostly decreased during all of sleep compared to wake, even in 268 the 7-10 Hz range that has been associated with certain sleep stages ( Figure S5).  x frequencies x channels) on the difference between baseline wakefulness and induced sleep, to reveal 285 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint a significant cluster (frequency x channel pair). Thus, we uncovered a significant cluster ( Figure S6D) 286 in the central brain channels across all (5-40Hz) frequency bands, whereas the 104y-Gal4/+ control 287 flies did not reveal such a cluster ( Figure S6E,F). It is interesting to note that sleep induction using this 288 strain yielded an opposite effect to what we found during spontaneous sleep: LFP activity during 289 induced sleep is on average higher than during baseline wakefulness ( Figure S6D), while it was lower  (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made    Figure 5B. It 338 is interesting to note several points. First, the probability of awake data is ~0.7 and of midsleep is ~0.0 339 indicating that the classifier performs well on classes that it has already been trained on. Second, at the 340 epoch -2 to -1min, when the fly is still moving (yellow circles), LFP data indicates that it is closer to 341 resembling sleep (<0.5), before dropping fast to ~0.3 (turquoise circles) in the first two minutes of sleep. 342

343
The above analysis indicates that with this approach we could predict the probability a fly will fall 344 asleep 2 mins before the start of the immobility period. Interestingly, just 1 min before flies fall asleep 345 the LFP data indicates a brief moment more closely resembling wake (yellow circles), perhaps 346 associated with grooming periods (observed in honeybees for example (Eban-Rothschild and Bloch 347 2008)). Interestingly, in the first two minutes of sleep (turquoise circles) reveal a probability metric 348 halfway between midsleep and wake, suggesting either a gradual descent into deeper sleep or a distinct 349 sleep stage. Finally, at the epoch from x-2 to x-1 min before mobility resumes (brown circles), the 350 probability metric returns to a similar level as early sleep. Immediately after mobility resumes, the LFP 351 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint data is classified as no different than awake, i.e, there is no post-sleep ambiguity. It is important to note 352 that only the 'awake' and 'midsleep' data has been seen by the classifier, the rest of the data -4 to +2 353 min, x-2 to x+2 min has never been seen by the classifier. Additionally, midsleep collapses a wide range 354 of different sleep durations in different flies, so could still be averaging different sleep states within.  We then proceeded to examine more closely how differences in the sleep LFP might be segregated 376 across the fly brain ( Figure 5C) using post-hoc tests (using tukey adjustment for multiple comparisons) 377 from the epoch-channel model. In the central channels, the 'awake' data was significantly different 378 compared to all sleep categories, and critically was also different to the 'presleep' data. It is important 379 to note that behaviourally the fly is still considered awake in the 'presleep' period (i.e., it is still moving). 380 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint Thus, the ability to predict sleep at least 2 mins before the onset of immobility, which was revealed in 381 our SVM analysis ( Figure 5B), might be explained by these significant spectral differences only 382 observed in the central channels. In the middle channels, the 'awake' data was also significantly 383 different across all sleep categories, however was not different to the 'presleep' data. Further, the 384 'presleep' period was significantly different from 'earlysleep','midsleep','latesleep' periods. In the 385 peripheral channels, the 'awake' data was significantly different across all sleep categories, however 386 was again not different to the 'presleep' data. Taken together, mean power spectral data across different 387 channels was thus able to differentiate between 'awake', 'presleep', and different sleep epochs of sleep.  This could be illustrated by an example ( Figure 6A). In the first step subsets of training data (#1 to #n)

403
were created by making a random sample of size N with replacement. This allows for the ensemble of 404 decision trees (#1 to #n) to be decorrelated and the process of such random sampling is called bagging 405 (bootstrap aggregation). In the second step, each decision tree (#1 to #n) picks only a random subsample 406 of features (feature randomness) instead of all features (again allowing for the decision trees to be 407 decorrelated). 408 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint 409 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint  Figure 7D, we plot the mean proboscis to eye distance for all the flies 457 averaged across awake and sleep bouts. As described earlier for flies without implanted electrodes, PEs 458 executed during wake and sleep are behaviourally similar and hence would be difficult to distinguish 459 from each other using video alone. Similar to our behavioral dataset, PE events usually occur in 460 rhythmic bouts of more than one, rather than single events. In Figure 7E, we plot the inter-proboscis 461 interval period, which is the interval between consecutive PE events in a single proboscis bout. It can 462 be seen that most proboscis events occur within 1.8 sec (95 th percentile) of each other. As shown before 463 in our behavioral data without implanted electrodes, the inter-proboscis interval does not vary across 464 awake and sleep periods. Next in Figure 7F, we decided to probe the number of single (one PE event) 465 and multi (>1 PE event) across different flies. We found that occurrences of single PE events are 466 significantly lower than multi PE events using a pairwise t-test with t(13) = 3.72, p<0.01. 467 468 469 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made

487
To further illustrate this point in Figure 7G, we plotted the burst length of a PE event (number of 488 extension events within a PE bout) and found that only 33% of the events are single PE while the rest 489 are multiple PE events. Overall, our investigation of PEs in this multichannel recording dataset is in 490 concurrence with our first (electrode-free) dataset, suggesting that inserting probe into the fly brain 491 does not alter several measures associated with this micro-behavior. as suggested in our purely behavioral dataset ( Figure 1G). We found that more PE events occur after 5 496 min of a sleep bout, compared to those occurring before the 5th min of a sleep ( Figure 7H) (pairwise t-497 test, t(12) = -2.8, p<0.05), suggesting that PEs indeed predominate during deeper sleep. We also 498 compared PEs immediately after flies had awakened from sleep, which revealed no significant 499 difference ( Figure 7H) (pairwise t-test, t(13) = -1.92, p>0.05) between PE bouts occurring after the 5th 500 min of an awake bout compared to those occurring before the 5th min of an awake bout, confirming 501 that transitions into sleep (rather than transitions back to wake) were associated with increased PE 502 events.    We therefore focused on the multichannel data to identify any differences in the LFP activity associated 521 with PEs during wake and sleep epochs. We first identified the PE periods (Refer to Methods LFP 522 analysis -proboscis: Identification of proboscis periods) and extracted the LFP data and epoched them 523 into 1 sec bins. Second, we used spectral analysis to determine if epochs characterized by PEs differ in 524 frequencies across different channels, for wake compared to sleep. For this purpose, we computed the 525 spectral power for every 1 sec epoch per channel (See Methods for LFP analysis -proboscis: power 526 spectrum analysis), using as before a common reference system for re-referencing the LPF data. Third, 527 we employed non-parametric resampling tools to identify the precise patterns (frequency x channel 528 pairs) differing in proboscis periods within awake and sleep at the group level. For this purpose, we 529 first computed the difference in mean spectral data across non-proboscis periods (awake or sleep) and 530 proboscis periods (awake proboscis and sleep proboscis respectively) for individual flies. We then 531 performed a cluster permutation test (flies x frequencies x channels) on the difference data to reveal 532 significant clusters (frequency x channel pair).

542
'sleepprob' periods, while clustering analysis reveals a single significant cluster mostly across all channels in 543 higher frequencies (25 -40 Hz). Activity within the significant cluster indicates activity in the 'sleepprob' is 544 comparatively lower than 'awakeprob' periods, thereby elucidating a significant difference across proboscis 545 events occurring in sleep and awake periods (though phenotypically they look the same - Figure 7D).

547
. CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint

548
In Figure 8A, we show the difference data (awake proboscis -awake period) and clustering analysis, 549 which reveals a significant cluster in the middle channels (6-10) across all frequencies. Further, within 550 the significant cluster we also performed a post hoc analysis revealing that spectral activity within the 551 awake proboscis periods are lower than awake periods. In Figure 8B, we show the difference data (sleep 552 proboscis -sleep period) and clustering analysis reveals a significant cluster in the central channels (1-553 5) across higher frequencies (32-40 Hz). Further, within the significant cluster we also performed a 554 post hoc analysis revealing that spectral activity within the sleep proboscis periods are higher than 555 sleep periods (in contrast to the awake proboscis periods). In Figure 8C, we directly compared the 556 awake and sleep proboscis periods and showed the difference data (awake proboscis -sleep proboscis) 557 and clustering analysis, which reveals a significant cluster in the central, middle channels (1-9) across 558 higher frequencies (25-40 Hz). Further, within the significant cluster we also performed a post hoc 559 analysis revealing that spectral activity within the sleep proboscis periods are lower than awake 560 proboscis periods. This suggests that PEs occurring during sleep are qualitatively different from 561 identical PE events occurring during wake. This suggests that the brain activity state (e.g., quiet or deep Sleep is most likely a whole-brain phenomenon, meaning that its presumed varied functions 568 (Kirszenblat and van Swinderen 2015) are understood to be of benefit to the entire brain rather than 569 to only specific sub-circuits. There is good evidence for this in the Drosophila model, with synaptic activity across the fly brain, and not only in specific sub-circuits of interest. Unlike in larger animal 576 models such as mice, recording from multiple brain regions in behaving (and sleeping) flies has been 577 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint challenging, so there has been limited capacity to investigate dynamic brain processes during sleep in 578 this otherwise powerful model system. While genetically encoded reporters of neural activity (e.g., 579

GCaMPs) have been successfully used to describe spontaneous sleep in flies (Tainton-Heap et al. 2021; 580
Flores-Valle and Seelig 2022; Bushey, Tononi, and Cirelli 2015), these are typically still limited to a 581 narrow region of interest (e.g., the mushroom bodies, or the central complex), and imaging conditions 582 are rarely commensurate with the typical day-night cycles of normal sleep. In this study, we overcame 583 these drawbacks by recording electrical activity from 16 channels across the fly brain, in behaving flies 584 across long-lasting recordings that spanned a typical day and night. Our multichannel recording 585 preparation therefore approximates as closely as possible -in flies -a sleep EEG, which has been the 586 Rather than focus on specific frequency bands such as delta and theta, we conducted an agnostic 594 analysis of our multichannel LFP data using machine learning techniques. These unbiased classifiers 595 identified distinct stages of sleep, in flies that were otherwise entirely quiescent (apart from certain 596 micro-behaviors, which we discuss further below). These identified sleep stages align closely with 597 similar changes in brain activity dynamics observed in calcium imaging data in spontaneously sleeping 598 flies (Tainton-Heap et al. 2021). For example, in the calcium imaging data we showed that even before 599 sleep onset, the number of 'active' neurons is already different (lower) than wake; accordingly, in the 600 current electrophysiological data the classifiers predict sleep onset 2min before flies stop moving. This 601 also aligns with an older (single channel) electrophysiological sleep study in flies showing that brain 602 LFP activity becomes uncorrelated from behavior 5min before sleep onset (B. van Swinderen, Nitz, and 603 Greenspan 2004). Together, these findings make a compelling case for dissociative states in the fly 604 brain, which is consistent with the view that such states might also be changing within a sleep bout. 605 606 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint Our multichannel recordings also revealed that changes in sleep physiology are likely to encompass the 607 entire fly brain, from the optic lobes to the central complex. This is also consistent with other studies, 608 although this has not been previously demonstrated using a comprehensive multichannel approach. 609 An early study in honeybees showed that visually responsive neurons in the optic lobes become 610 unresponsive during sleep (Kaiser and Steiner-Kaiser 1983), and that these cells become rapidly 611 responsive again when bees are woken up with an air puff. Immunochemical studies investigating 612 synaptic proteins found that these were downregulated in the optic lobes during sleep (Donlea,613 Ramanan, and Shaw 2009), as well as in the whole brain (Gilestro, Tononi, and Cirelli 2009). It is 614 understood that the insect optic lobes receive significant feedback from the central brain, as well as 615 Bloch 2008) and antennal movements (Sauer et al. 2003). Interestingly, in our study we also found 636 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. have also shown that persistent depolarization of motor command activity of the Fdg (feeding) neurons 650 could also result in PEs. In this context, it is pertinent to note that LFP activity during PE events in the 651 awake periods are higher than those in the 'awake' periods without PE events, suggesting a distinct PE 652 signature. But this is not the case for the exact same behaviors during sleep. We found that LFP activity However, this would also be the case for windows in the brain created for calcium imaging (Tainton-663 Heap et al. 2021) (and in the latter scenario the proboscis is typically glued in place to prevent brain 664 motion artifacts), so no fly brain recording preparation (yet) can realistically sidestep these concerns. 665 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made Our study also paves the way for asking fundamental questions about fly sleep in the following fashion. 669 First, the LFP activity of mutant strains (with higher, or lower baseline sleep) could be recorded and 670 its differences across the wild type could be quantified. Second, for understanding and probing the 671 exact spatial patterns of specific sleep stages identified in this study with higher resolution, 2-photon 672 imaging at the whole brain level could be recorded for longer duration (controlled by closed loop  First, flies were anesthetized on a thermoelectric cooled-block maintained at a temperature of 1-2°C. 708 Second, the thorax, dorsal surface and wings of the fly were glued to a tungsten rod using dental cement 709 (Coltene Whaledent Synergy D6 Flow A3.5/ B3) and cured using high intensity blue light (Radii Plus, 710 Henry Scheinn Dental) for about 30-40 sec. Further, dental cement was also applied to the necks to 711 stabilize them and prevent lateral movement of the head during electrode insertion (next section). 712 Third, to prepare the fly for the multichannel overnight recording, we placed a sharpened fine wire 713 made of platinum into the thorax (0.25 mm; A-M systems). The platinum rod serves as a reference 714 electrode and helps filter the noise originating from non-brain sources. The insertion of a platinum 715 electrode (while providing minimal discomfort to movement of animal) was done using a custom 716 holder with a micro-manipulator to enable targeted depth of insertion. For flies in the behavioral 717 dataset, the procedure was the same, except that no reference wire was inserted.  First, the tethered fly from the previous step was placed on an air supported ball (polystyrene) that 721 served as a platform for walking/rest. Humidified air was delivered to the fly using a tube below the 722 ball (also from the side) to prevent desiccation. Second, to record from half of the regions in the fly 723 brain (half-brain probe) we used a 16-electrode linear silicon probe (model no. A1x16-3 mm50-177; 724 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint NeuroNexus Technologies). Third, the probe was inserted into the eye of the fly laterally using a micro-725 manipulator (Merzhauser, Wetzlar, Germany). The probe was inserted such that the electrode sites 726 faced the posterior side of the brain. The final electrode position (depth of insertion) was determined 727 using the polarity reversal procedure described below. For flies recorded in the behavioral dataset the 728 setup was similar, except that a custom chamber was lowered over the ball and fly to maintain a 729 humidified environment during recordings.  recording sites across flies, we designed a novel paradigm using visual evoked potentials ( Figure S2).

736
First, while the probe was being inserted from the periphery to the center of the brain, we used visual 737 stimuli (square wave of 3 sec in duration with 1Hz frequency) from a blue LED. When the visual stimuli 738 was displayed we simultaneously recorded the local field potentials from the 16 electrode sites. During 739 the initial stage of insertion, most of the electrodes are outside of the brain and only a few are inside 740 the eye, optic lobe. The recordings in the electrodes inside the eye, brain show a visual evoked potential 741 corresponding to the leading edge and the trailing edge of the square wave. Second, we move the probe 742 slowly towards the center of the brain so more of the electrode sites would now be inside the brain. 743 Third, we notice that some electrodes have a negative deflection and some have a positive deflection 744 with respect to the leading edge of the square wave. The electrodes in the eye, optic lobe regions display 745 a positive deflection and electrodes further to the central parts of the brain display a negative deflection. 746 However this polarity change usually happens in the electrodes that are coincident on the regions right 747 after the medulla. Fourth, for all flies we made sure that the polarity change coincided with the 748 electrodes 11-13 inorder to establish consistency in terms of the spatial locations. (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint Dye based localization.

755
Inorder to identify the possible locations in the brain targeted by the electrodes, we used a three step 756 procedure. In the first stage, we used immunohistochemistry to identify the locations of electrodes 757 using a fluorescent dye and neuropils using antibodies against nc82 (presynaptic marker bruchpilot) 758 respectively. In the second stage, we used a registration procedure to map the dye locations to an EM 759 dataset (using nc82 images). In the third stage, we used principal component analysis to identify the 760 precise neuropils targeted. 761 a) Immunohistochemistry. involved two steps: i) rigid affine registration that roughly aligned the source image to the template 777 space with 12 degrees of freedom (translation, rotation, scaling). ii) non-rigid registration that allowed 778 different brain regions to move independently with a smoothness penalty. The entire process was 779 carried out using the CMTK plugin (FiJi toolbox) as described here (Ostrovsky, Cachero, and Jefferis 780 2013). Second, we then used the JFRC2 (light-level) registration as bridging registration to FAFB14 781 (EM dataset) using the natverse toolbox (Bates et al. 2020) and mapped both the nc82 images and the 782 dye locations to the FAFB14 space. 783 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint c) Electrode localisation.

784
The electrode dye locations inside the brain are usually visible as fragments (points) instead of a single 785 continuous (line) segment, mainly because the insertion of the probe causes the smearing of the dye on 786 the neuropils in the brain. Inorder to identify the precise locations of the recording electrodes in the 787 brain, we first used the points and performed principal component analysis to find the eigenvector or 788 line (1st principal component) that would have minimize the distance between the different points to 789 the line itself. This line could be thought of as the main path of the probe as it entered into the brain.    The fly movement was quantified with the video files using Python (3.6.1), OpenCV (3.4.9) in the 826 following manner. First, every video file (1 per hour of recording) was read frame by frame. Second, 827 for each frame, we clipped the image such that the main focus was on the fly while ignoring items in 828 the background. Third, we converted the color space for each frame from BGR to grayscale. Fourth, 829 we computed the 'deltaframe' as the absolute difference of the current frame with the previous frame. 830 Fifth, we thresholded the deltaframe using a custom defined threshold per fly and converted them into 831 binary. Sixth, we dilated the thresholded image and identified contours in the dilated image and looped 832 over the different contours selecting those above a specific threshold (area). Finally, we drew rectangles 833 around the contours above the threshold on the original (color) image to manually verify the 834 movement location. Only those frames that had contours above threshold were regarded as 'moved' 835 frames, other frames would be classified as 'still'. Thus, each frame would be either 0 (still) or 1 (moved). 836 In the next stage, we used the frame by frame movement data to identify segments of LFP data as 'sleep' 837 or 'awake' etc in the following fashion. First, we synced the LFP data with the video data by using the 838 time stamps in both the LFP data and video metadata (csv files). Second, we clipped both the LFP and 839 video data to the first 8 hours of recording. Though 23 flies survived for more than 24 hours, we only 840 used the first 8 hours to ensure that the fly's health was completely optimal (considering the 841 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint circumstances) in both the behavior and brain recordings. Further only 16 flies were used for the 842 analysis, as 7 of them had issues with calibration (noisy or no calibration) or abnormal activity (either 843 no sleep trials or very active). Third, we pruned movement data to ensure brief noise in movements 844 are avoided. Fourth, we identified the segments of data wherein the fly was immobile for more than 5 845 mins as 'sleep' and the segment immediately preceding 2 mins before the sleep data as 'presleep' and 846 the rest of the data as 'awake'.  Second, the data were resampled to 250 Hz and bandpass filtered with zero phase shift between 0.5 and 855 40 Hz using hamming windowed-sinc FIR filter, further line noise at 50 Hz was removed using a notch 856 filter. Third, the hourly LFP data was saved to EEGLAB '.set' file format. Fourth, the hourly LFP data 857 were interpolated in a linear way to avoid any discontinuities between the hourly segments of data. 858 Fifth, the movement data (see Movement analysis) was added to the EEGLAB file along with the start 859 and end time based on video data. Sixth, the multi-hour LFP data (along with the movement data) is 860 collated for the first 8 hours of the recording. Seventh, we created separate epochs based on movement 861 data into 'sleep', 'presleep', 'awake' (where preceding 2 mins of immobility (-2 to 0 mins) is 'presleep' 862 and immobility is 'sleep' and the rest of the data is 'awake', here 0 mins is the start of the immobility). 863 Eighth, the epochs were now re-referenced based on the channel where the polarity reversal occurred. 864 For this we identified the channel wherein the polarity reversal occurred (see Polarity reversal section) 865 and subtracted all the channels from this channel, thus resulting in 15 channels after the re-referencing. 866 This brain based referencing technique (similar to the Cz based reference in human EEG recordings) 867 allows for filtering of non-brain based physiological noise (like heartbeat etc). Previous multichannel 868 recordings used only the thorax based referencing (followed by bipolar referencing) along with 869 Independent Component Analysis (ICA) to remove physiological noises. However, the identification 870 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint of noise components like heartbeat etc from ICA is subjective and further depends on the expertise of 871 the human curator. Our technique overcomes these issues while simultaneously providing a method to 872 remove physiological noises not originating from the brain. 873 b) Power spectrum analysis (sleep vs awake).

874
The power spectra of the LFP data was computed for each fly in the following fashion. First, each 875 condition ('awake', 'sleep' etc) of varying duration was re-epoched into trials of 60 sec duration. Second, 876 each trial was bandpass filtered with zero phase shift between 5 and 40 Hz using hamming windowed-877 sinc FIR filter. Third, for each trial, power spectra (in decibels) was computed using the 'spectopo'  The thermogenetic sleep induction data was collected using 104y-Gal4 lines as part of the study (Yap 890 et al. 2017). This multichannel recording consisted of a 16-electrode full-brain probe (model no. A1x16-891 3mm50-177; NeuroNexus Technologies) covering the whole of the brain ( Figure S6B) (in contrast to 892 the half-brain probe mentioned before) with interelectrode distance of 50 µm. The rest of the recording 893 parameters were the same as mentioned in the previous section. Sleep induction was achieved by 894 transient circuit activation of the sleep promoting circuit innervating the dorsal fan shaped body (dFB). 895 For example, this was done by using the 104y gal4 lines (offering cell type specificity in the dfB regions) 896 to control the expression of UAS driven TrpA1 (temperature sensitive cation channel), thereby 897 allowing for the activation of the specific neurons in dFB with temperature changes. As described in 898 (Yap et al. 2017), before the induction of sleep, the baseline activity was recorded in the 'baseline' 899 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint condition for 3 secs, followed by stimulation in the 'sleep induction' condition for 3 secs before 900 returning to recovery for 3 secs. 901 a) Preprocessing.

902
LFP data was analyzed with custom-made scripts in MATLAB (The MathWorks) using EEGLAB as 903 mentioned before. The preprocessing steps were as follows: First, the LFP data per condition ('baseline', 904 'sleep induction', 'recovery') was converted to EEGLAB '.set' file format with a sampling rate of 1 KHz. 905 Second, the LFP data was re-referenced using a differential approach, wherein nearby channels are 906 subtracted with each other resulting in 15 channels. 907 b) Power spectrum analysis (baseline vs sleep induction).

908
The power spectra of the LFP data was computed for each fly in the following fashion. First, each 909 condition ('baseline', 'sleep induction' etc) was reepoched into trials of 1 sec duration. Second, each 910 trial was bandpass filtered with zero phase shift between 5 and 40 Hz using hamming windowed-sinc 911 FIR filter. Third, for each trial, power spectra (in decibels) was computed using the 'spectopo' function 912 in the EEGLAB toolbox in MATLAB. Fourth, the mean power spectra for all the trials per condition 913 per fly was computed. The group level comparison was performed using cluster permutation test 914 methods (as described in previous sections) to identify differences in frequency x channels across 915 'preheat' and 'heaton' conditions.   Here, we relabelled the segments of data (already identified as 'sleep', 'awake' based on movement data) 924 in the following fashion. First, we labeled the segments of data in the first 2 mins (0 to 2 mins) after 925 the start of immobility as 'earlysleep' and the segments of the data in the preceding 2 mins (-2 to 0 926 mins) as 'presleep'. Second, we labeled the segments of data in the last 2 mins of sleep as 'latesleep' and 927 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint the segments of data in between the 'earlysleep' and 'latesleep' as 'midsleep'. The rest of the data is 928 considered as 'awake'. 929 b) Preprocessing & power spectrum computation.

930
The preprocessing steps were the same as mentioned in the previous section (LFP preprocessing). For 931 the computation of the power spectrum, we followed similar procedures as mentioned before, however 932 we saved the individual power spectrum per trial (channels x frequency) per fly in a csv file along with 933 the corresponding label of the sleep state. 934 c) Classifier probability analysis. 935 We implemented a support vector machine (svm) based classifier using scikit-learn (0.24.2) to classify 936 the LFP data using the following steps. First, we collated the features based on power spectrum 937 (channels x frequency) from all the flies across different sleep states. Second, we filtered the features to 938 only 'awake' (5106 epochs) and 'midsleep' (1165 epochs) states. Here, we also did not feed (for training) 939 the preceding 2 mins of 'presleep' and succeeding 2 mins of 'earlysleep' and the last 2 mins of sleep 940 'latesleep' into the classifier (we used those minutes for sanity check purposes -Refer to Figure 5A). 941 Third, we encoded the target labels ('awake', 'midsleep') into binary states using 'LabelEncoder' from 942 scikit-learn. Fourth, we balanced the composition of labels (or classes) to prevent bias due to unequal 943 distribution of classes in the training dataset. Fifth, we divided the dataset into train and test sets (80% 944 train, 20% test) using 'train_test_split' from scikit-learn in a stratified fashion. Sixth, we subjected both 945 the train and test data to a standard scaler using 'StandardScaler' from scikit-learn, which removes the 946 mean of the data and scales it by the variance. Seventh, we implemented a svm based classifier using a 947 'linear' kernel along with probability estimates per class and fit the classifier to the train dataset. Eighth, 948 we used the trained classifier on the test dataset and computed different metrics of classifier 949 performance like accuracy, roc_auc, recall, precision, f1-score etc using 'metrics' from scikit-learn 950 ( Figure S7B). Ninth, we used the trained classifier on all class labels ('awake', 'presleep', 'earlysleep',

951
'midsleep', 'latesleep', preceding 2 mins of 'presleep' and succeeding 2 mins of 'latesleep') from the 952 original dataset and computed the probability estimates per class. It is pertinent to note that none of 953 the 'presleep', 'earlysleep', 'latesleep', preceding 2 mins of 'presleep' and succeeding 2 mins of 'latesleep' 954 the data have not been seen by the classifier beforehand. The above process from Step 5 onwards is 955 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made  train and test sets (80% train, 20% test) using 'train_test_split' from scikit-learn in a stratified fashion. 967 Fourth, we subjected both the train and test data to a standard scaler using 'StandardScaler' from scikit-968 learn, as mentioned in the previous section. Fifth, we encoded the target labels into binary states using 969 'LabelBinarizer' from scikit-learn. Sixth, we implemented a random forest classifier for this multiclass 970 classification problem. As the random forest classifier has multiple hyperparameters that need to be 971 tuned, we first used a random grid (using 'RandomizedSearchCV' from scikit-learn) to search for the 972 hyperparameters and then further used these parameters in a grid search model (using 'GridSearchCV' 973 from scikit-learn) to identify the best hyperparameters. Seventh, we used the trained classifier on the 974 test dataset and computed different metrics of classifier performance like recall, precision, f1-score etc 975 using 'metrics' from scikit-learn separately for all the 5 classes. Furthermore, we also computed a 976 normalized confusion matrix using 'confusion_matrix' from scikit-learn. The above process from Step 977 5 onwards is repeated a further 4 times with different test, train splits to create five different iterations 978 of classifiers and performance metrics. Finally to identify and rank the importance of different features 979 we utilized the permutation importance metric (using 'permutation_importance' from scikit-learn). 980 The permutation feature importance works by randomly shuffling a single feature value and further 981 identifying the decrease in the model score (Breiman 2001). The process breaks the relationship 982 between the shuffled feature and the target, thus if the feature is very important, it would be indicated 983 by a high drop in model score, on the other hand if it is relatively unimportant, then the model score 984 would not be affected so much. We used the permutation importance with a repeat of 5, and for each 985 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint These models were fit using the 'lmer' function ('lmerTest' package) in R (Kuznetsova, Brockhoff, and 1103 Christensen 2017) and the winning model is identified as the one with the highest log-likelihood by 1104 comparing it with the null model, and performing a likelihood ratio chi-square test (χ2). Finally the 1105 winning model was analyzed using the 'anova' function (Supplementary Table 2    We defined 2 different multilevel models (Supplementary Table 9) to understand the modulation of PE 1126 event count by sleep epochs. In the null model, the PE event count depends only on the mean per fly 1127 (fixed effect) and the fly ID (random effect). In the second model (time_label model), the PE event 1128 count depends only on the specific temporal sleep stage (fixed effect) and the fly ID (random effect). 1129 These 2 models were fit using the 'lmer' function ('lmerTest' package) in R (Kuznetsova, Brockhoff, 1130 and Christensen 2017) and the winning model is identified as the one with the highest log-likelihood 1131 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made    1357 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made   (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint Suppl (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023.

1412
Bridging registration was used to register to FAFB space (F), the registration templates were applied on electrode 1413 dye locations (B) to produce co-localisation (H).

1414
. CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023.  (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint 1465 1466 . CC-BY 4.0 International license available under a (which was not certified by peer review) is the author/funder, who has granted bioRxiv a license to display the preprint in perpetuity. It is made The copyright holder for this preprint this version posted June 13, 2023. ; https://doi.org/10.1101/2023.06.12.544704 doi: bioRxiv preprint