## Population sleep and waking metrics

In order to determine whether a fly is in a sleeping or waking state, the movement state for each individual fly is calculated for each minute of the day starting at TDay0 (i.e., calculating eqn (5) with ΔT = 60 s). For each fly, all bouts of inactivity (i.e., contiguous time bins of inactivity) are determined. If an inactivity bout is 5 min or more in duration (or a “sleep bout”), then the time bins within the inactivity bouts are considered to be “sleep” minutes. All other time bins are therefore considered to be “waking” minutes. Given that the sleep/wake state of each fly is known for each minute in relation to the start of the day, beginning at TDay0 = 8 am, then it is possible to calculate the following population metrics for each hour in the day:

• Sleep bouts/hour – Average number of sleep bouts for each individual fly.

• Sleep minutes/hour – The average sleep duration for each individual fly.

• Sleep minutes/bout –Sleep minutes/hour divided by sleep bouts/hour.

• Hourly waking speed – Average of eqn (4) calculated for all waking minutes within the fly population.

To compare variations in the population activity for specific time periods during the day (i.e., day vs night), the aforementioned hourly metrics can be averaged over a duration of TGrpDay hours, where:

## Population movement

The population movement analysis is similar to the population waking metric analysis in that the function quantifies fly activity over an entire day. However, the population movement analysis differs in that A) it takes the activity of all flies into account (regardless of their state), and B) the time resolution is much finer.

To calculate this metric, the day is split up temporally into time bins of duration, TMove. For a given time bin, the average speed of an individual fly is calculated using eqn (4) (with ΔT = TMove), which in turn is averaged over all flies to obtain the population average. This can be extended to multi-day experiments to calculate daily average speed whereby the aforementioned time bins are temporally aligned to TDay0, and then averaged over all days.

## Fly location heatmap

The fly location heatmap is calculated by discretising the tube length spatially into NHM equal regions. From this, the spatial bin location of an individual fly, for any given movie frame, is calculated as follows:where floor(Y) is a mathematical operator which rounds Y down to the nearest integer, and Xmin/Xmax denotes the individual flies' minimum/maximum tube locations, respectively, over the entire experiment. The final population heatmap is calculated by splitting up the experiment temporally into time bins of duration THM. Each heatmap column therefore represents the spatial bin location histogram count divided by the total histogram count for a given time bin. From this, the mean fly population location, for a given time bin, is calculated by:where N(i) denotes histogram count for the ith tube spatial region.

## Stimuli Response Curve Fitting

The stimuli response curves are formed by first retrieving the fly location traces for the time points TBefore/TAfter minutes before/after each stimuli event, and interpolating the fly locations to the nearest second (see Fig. 3d). The stimuli response speed for each individual fly, at the jth signal time point, is estimated by eqn (4) from the preceding NAvg time points:with the population average stimuli response speed being the average over all flies. The relative stimuli response signal is calculated from the absolute stimuli response by subtracting the pre-stimuli speed, which is the average speed of the time points preceding the stimuli event (i.e., −TBefore < t < 0). To quantify the features of the stimuli response, the relative speed signals can be fit with either the single-inactivation exponential equation:or the double-inactivation exponential equation:where τ = t − δtH(t) is the heaviside step function (= 1 if t > 0, otherwise 0), A0–2 are scale factors τA is the activation time constant, and τA/τB are inactivation time constants. The exponential equation parameters (the scale factors, time constants and δt) for (13) and (14) are fitted using the Matlab optimization function, lsqcurvefit. For this study, all stimuli response signals were characterized using the single-inactivation exponential equation functional form.

Note that the stimuli response signals can also be grouped by time regions within the day (similar to the Sleep/Waking Metrics). It should be appreciated that if the number of samples in a given time group is too small (approx. < 50) then the exponential form of the response signal will divulge into a noisy signal

The pre/post-stimuli average speeds (VPre/VPost), for each fly, are calculated from eqn (4) for the time domains TPre/TPost minutes before/after each stimuli event. These values can also be grouped by time within the day in the same manner as Stimuli Response Curve Fitting (see above). The response gradient quantifies the effect the stimuli events have on fly speed, and is calculated as the ratio of the post-stimuli to pre-stimuli speeds. The overall response gradient, confidence intervals, and correlation values are all calculated by the Matlab curve fitting function, fit using the linear functional form, ‘y = mx’.

In addition to the response gradient, the pre/post-stimuli speed comparison can also be quantified in terms polar coordinates by the phase angle, ϕ and magnitude, R, which are calculated as follows:

## Pre- and post-stimuli speed comparison

The immobility time of an individual fly prior to a stimuli event at time, TS, can be calculated by eqns (5) & (6) using T0 = TS and reversing the direction of time (i.e. determining the last frame before T0 where eqn (4) is true). These immobility times are grouped into time bins of duration TImmob (see also Fig. 4a):For a given time group that has a total count of NTotal flies, given that NReact flies respond to the stimuli (calculated from eqn (6) using a post-stimuli event response duration of ΔT = TReact = 60 s) then the reaction proportion, PReact, is calculated as follows:To quantify effect of sleep intensity on arousal threshold, the stimuli response signals are be calculated and quantified for each of the immobility time bins using eqns (12)–(14). Further to this, all of the aforementioned sleep intensity metrics can be differentiated further by separating activity by day/night (where the day/night phases start at 8am/8pm respectively).