Hierarchical Compression Reveals Sub-Second to Day-Long Structure in Larval Zebrafish Behaviour

Animal behaviour is dynamic, evolving over multiple timescales from milliseconds to days and even across a lifetime. To understand the mechanisms governing these dynamics, it is necessary to capture multi-timescale structure from behavioural data. Here, we develop computational tools and study the behaviour of hundreds of larval zebrafish tracked continuously across multiple 24-hour day/night cycles. We extracted millions of movements and pauses, termed bouts, and used unsupervised learning to reduce each larva’s behaviour to an alternating sequence of active and inactive bout types, termed modules. Through hierarchical compression, we identified recurrent behavioural patterns, termed motifs. Module and motif usage varied across the day/night cycle, revealing structure at sub-second to day-long timescales. We further demonstrate that module and motif analysis can uncover novel pharmacological and genetic mutant phenotypes. Overall, our work reveals the organisation of larval zebrafish behaviour at multiple timescales and provides tools to identify structure from large-scale behavioural datasets.

To survive, animals must coordinate patterns of action and inaction in response to their environment. 2 These actions and inactions, which together we will define as behaviour, result from some function 3 incorporating internal (e.g. transcriptional, hormonal or neuronal activity) and external (e.g. time of 4 day or temperature) state. Thus, behavioural descriptions provide insight into the underlying 5 mechanisms that control behaviour and are a necessary step in understanding these systems 6 (Krakauer et al., 2017). 7 8 Animal behaviour, however, typically has many degrees of freedom and evolves over multiple 9 timescales from milliseconds (Wiltschko et al., 2015) to days (Proekt et al., 2012;Fulcher and Jones, 10 2017) and even across an animal's entire lifespan (Jordan et al., 2013;Stern et al., 2017). As such, 11 quantitatively describing behaviour remains both conceptually and technically challenging (Berman, 12 2018;Brown and de Bivort, 2018). Inspired by early ideas from ethology (Lashley, 1951;Tinbergen, 13 1963), one approach is to describe behaviour in terms of simple modules that are arranged into more 14 complex motifs. Behavioural modules are often defined from postural data as stereotyped 15 movements, such as walking in Drosophila (Berman et al., 2014;Vogelstein et al., 2014;Robie et al., 16 2017) and mice (Wiltschko et al., 2015), while behavioural motifs are defined as sequences of 17 modules, which capture the patterns inherent to animal behaviour, such as grooming in Drosophila 18 (Berman et al., 2014(Berman et al., , 2016. 19 20 Zebrafish larvae have emerged as a powerful model organism in neuroscience, owing to their genetic 21 tractability (Howe et al., 2013), translucency (Vanwalleghem et al., 2018) and amenability to 22 pharmacological screening (Rihel and Ghosh, 2015). In terms of behaviour larvae exhibit an alternating 23 sequence of movements and pauses, termed bouts. This structure is particularly suited to modular 24 description as individual bouts can be easily segmented and it is relatively easy to acquire many 25 examples from even a single animal due to the high frequency of their movement (Kim et al., 2017). 26 Leveraging these advantages, recent work used unsupervised learning to uncover a locomotor 27 repertoire of 13 swim types in larval zebrafish, including slow forward swims and faster escape swims 28 (Marques et al., 2018). However, the inactive periods between swim bouts, were not considered, 29 despite reflecting behavioural states such as passivity in the face of adversity (Mu et al., 2019) or even 30 sleep (Prober et al., 2006). 31

32
To explore an animal's full behavioural repertoire, from fast movements to sleep it is necessary to 3 Robie et al., 2017) to two hours (Marques et al., 2018) in length. Consequently, most identified 36 behavioural structure has been on the order of milliseconds and the existence of longer-timescale 37 structure, on the order of minutes to hours has remained largely unexplored. The development of 38 methods to extract multi-timescale structure from long-timescale recordings would open avenues to 39 explore questions including how behaviour varies across the day/night cycle and develops across an 40 animal's lifespan. Furthermore, as pharmacologically or genetically induced behavioural phenotypes 41 can differ at different times of the day/night cycle in zebrafish larvae (Rihel et al., 2010;Hoffman et 42 al., 2016), a long-timescale approach would provide valuable phenotyping information. 43 44 Currently, the limiting factor in scaling these methods is the volume of data, owing to the high-45 framerates and -dimensionality required to estimate animal posture. Here, we present an alternative 46 approach in which we trade dimensionality for scale, by building a module and motif description of 47 larval zebrafish behaviour from a one-dimensional behavioural parameter recorded over time. 48 Specifically, we used a high-throughput behavioural set-up (Rihel et al., 2010) to continuously monitor 49 the activity of hundreds of zebrafish larvae across multiple days and nights. To identify multi-timescale 50 behavioural structure, we developed a three-step computational approach. Firstly, we used 51 unsupervised learning to identify a set of 10 behavioural modules that describe both active and 52 inactive bout structure. Secondly, we applied a compression algorithm (Nevill-Manning and Witten, 53 2000) to our module data to compile a library of almost 50,000 motifs, revealing behavioural patterns 54 organised across sub-second to minute timescales. Finally, we used a supervised learning algorithm 55 (Peng et al., 2005) to identify motifs from the library, used at particular times of the day/night cycle. 56 To test the ability of our approach to detect biologically relevant phenotypes, we also studied the 57 behaviour of larvae exposed to the seizure-inducing drug, pentylenetetrazol (PTZ) (Baraban et al.,58 5 Collectively, these results quantitatively demonstrate the advantages of assessing Δ pixels data on a 106 frame by frame basis and provide insight into the behaviour of wild-type zebrafish larvae across the 107 day/night cycle as well as those subject to pharmacological or genetic manipulations. 108 109 110

111
Recent work has demonstrated that larval activity can be classified using unsupervised learning into 112 13 distinct bout types that represent different swimming movements (Marques et al., 2018). A full 113 description of larval behaviour, however, requires quantification of both the movements and pauses 114 that they execute. Thus, we sought to determine if distinct active or inactive bout types, which we 115 termed modules, were identifiable from our data, and if module usage depended upon behavioural 116 context. 117

118
To address these questions, we separately clustered the active and inactive bouts (combined across 119 experiments a total of 30,900,018 active and 30,900,418 inactive bouts) using an evidence 120 accumulation-based clustering algorithm (see Materials & Methods). In brief, 200 Gaussian Mixture 121 Models were built from each data set, then the results of these models were combined to generate 122 aggregate solutions. This clustering method identified 5 active and 5 inactive modules (Figure 2a mean length of 0.06s and ranged from a minimum of 0.04s (our sampling limit) to a maximum of 0.12s. 129 In contrast, the longest inactive module (module 5) had a mean length of 96s and covered a huge 130 range of values from a minimum of 20s to a maximum of 8.8hours. 131

132
To examine how module usage varied across time, we represented each larvae's behaviour as an 133 alternating sequence of active and inactive modules (Figure 2c, Supplementary video 2). In the wild-134 type data, module usage varied with both time of day and development (Figure 2d). For example, the 135 probability of observing inactive module 2, which consists of typical day pause lengths (0.16 -1.16s), 136 was on average 0.6 during the day and only 0.24 during the night, when inactive modules 1, 4 and 5 137 became more likely (Figure 2d). To reveal finer-grain temporal dynamics, we also examined each 138 module's mean frequency over time (Figure 2e). In general, both the active and the short inactive 6 responded to the sudden change in illumination. In contrast, the only module with a peak in frequency 141 at the dark-to-light transition was inactive module 4 (3.72 -20s), which also had an increased 142 frequency approaching the light-to-dark transition. Together these results reveal that zebrafish 143 employ different bout types in a time of day/night dependent manner. 144 145 Next, we examined the impact of pharmacological and genetic manipulations upon bout type usage. 146 Larvae dosed with melatonin showed a shift towards using shorter active modules and longer inactive 147 modules (Supplemental Figure 4b). In PTZ dosed larvae, there were also shifts in active module 148 probability. Particularly notable was the complete exclusion of active module 1 in 27 of the 28 (96.4%) 149 PTZ dosed larvae, while control larvae used this module with 0.12 probability during the day and 0.22 150 during the night (Supplementary Figure 4c). These shifts likely reflect the chaotic, seizure-like 151 swimming observed in PTZ-treated larvae (Baraban et al., 2005), although no single active module 152 clearly captured these behavioural seizures. PTZ also increased the probability of the shortest inactive 153 (module 1) as well as the two longest inactive modules (modules 4 and 5), the latter of which are likely 154 to correspond to the inter-ictal bouts of inactivity associated with seizures (Supplementary Figure 4c). 155 Conversely, hcrtr mutants exhibited no differences in either active or inactive module probabilities 156 compared to their wild-type siblings (Supplementary Figure 4d), demonstrating that bout type usage 157 is similar between these mutants and wild-type animals across the day/night cycle. 158 159 Collectively, these results reveal that zebrafish behaviour in this assay can be described by 5 types of 160 active and 5 types of inactive modules, the usage of which varies with behavioural context. 161 Interestingly, in many contexts, both active and inactive module probabilities were shifted, suggesting 162 that these module types may co-vary, perhaps by being arranged into recurrent sequences. 163 164 165

166
From a set of behavioural modules, an animal could structure their behaviour in a range of ways. At 167 one end of this spectrum, successive modules could be organised completely randomly, such that 168 prior modules exert no influence on future module selection. At the other end, module selection could 169 be fully deterministic with a particular module always following another. Rather than being fixed, 170 however, it is likely that animals adapt their behavioural structure in response to changing internal or 171 external states. We sought to map the structure of zebrafish behaviour in different contexts by 172 examining the presence and organisation of module sequences, which could provide insight into the 173 mechanisms governing behaviour. To do this, we used a compression algorithm (Nevill-Manning and 7 data. When applied to our dataset (Figure 3a), this algorithm iteratively identified motifs from each 176 larva's modular sequence and returned two outputs --compressibility, a measure of each larva's 177 behavioural structure, and a library of identified recurrent module sequences, termed motifs. 178

179
To quantify the structure of zebrafish behaviour, we first compressed every animal's full modular 180 sequence, which in wild type animals were on average 236,636 modules long across 70 hours. To 181 determine if the resultant compression values indicated more structure than would be expected based 182 on either the distribution or the transition structure of the active-to-inactive modules, we compared 183 each larva's compressibility to that of 10 sets of paired shuffled data. All wild-type larvae were more 184 compressive than their paired shuffled data, demonstrating that their behaviour is more structured 185 than expected from modular probabilities alone (Supplementary Figure 5a). Compressibility, however, 186 varies non-linearly with input sequence length, as longer sequences will be more likely to contain 187 motifs (Supplementary Figure 5b). Thus, to enable comparisons between samples with different 188 numbers of modules, we compressed non-overlapping 500 module blocks of sequence per larva. This 189 approach revealed that compressibility was higher during the day than the night ( Figure 3b) and 190 increased with developmental age. To determine if these differences were primarily due to the 191 presence of behavioural motifs or instead were a consequence of differences in module distribution, 192 we also compared the difference in compressibility (Δ compressibility) between each animal's real and 193 shuffled data. This approach revealed that the compressibility difference between the day and the 194 night is predominantly due to differences in module selection (Supplementary Figure 5d). To reveal 195 finer-grain temporal changes in compressibility, we plotted Δ compressibility across time 196 (Supplementary Figure 5e). This approach revealed peaks at the light-to-dark transitions in the 197 evenings, consistent with this stimulus eliciting stereotyped behavioural sequences (Burgess and 198 Granato, 2007;Emran et al., 2010). 199 200 Next, we used compressibility to assess how our pharmacological and genetic manipulations altered 201 the structure of larval behaviour. We found that melatonin decreased day compressibility to night-202 time levels (Figure 3b). In contrast, PTZ increased compressibility to a constant day/night value ( Figure  203 3b). PTZ, however, reduced Δ compressibility (Supplementary Figure 5d), indicating that changes in 204 module distribution, rather than motif usage, are the dominant driver of PTZ-induced behavioural 205 changes. Importantly, these drug-induced changes in compressibility do not simply reflect overall 206 activity levels. For example, PTZ exposed larvae are less active than controls during the day and more 207 active during the night (Supplementary Figure 1d) but have consistently higher compressibility ( Figure 8 3b). Finally, in hcrtr mutants we found no differences in either compressibility or Δ compressibility, 209 suggesting that hcrtr mutant behaviour is structured similarly to wild-type animals ( Figure 3b). 210 To gain insight into the behavioural sequence's larvae deploy, we then studied the motifs identified 211 by the compression algorithm. Compression of the real modular sequences identified a mean of 1901 212 motifs per animal (Supplementary Figure 5c). Interestingly, compression of the real data almost always 213 identified slightly fewer motifs than the shuffled data (Supplementary Figure 5c). This suggests that 214 the motifs identified from the real data were used more frequently than those in the shuffled data 215 and therefore likely reflect enriched behavioural sequences. Merging the motifs identified across all 216 animals generated a library of 46,554 unique behavioural motifs (Figure 3c). In terms of raw Δ pixels 217 data, each motif represented an approximately repeated pattern of movements and pauses of varying 218 length ( Figure 3d). Motifs in the library ranged from 2-20 modules long with a median length of 8 219 modules and spanned timescales from approximately 0.1s-11.3 minutes with a median length of 3.84s. 220 Motifs of different module lengths used distinct sub-sets of modules ( Figure 3c). For example, motifs 221 comprised of longer module sequences had a lower probability of using long inactive modules. 222 Together, these results reveal the varied timescales at which zebrafish larvae organise their behaviour 223 and suggest the presence of structure governing the arrangement of modules into motifs. 224

227
The large number of motifs in our library led us to hypothesise that each may be used in specific 228 behavioural contexts. To test this hypothesis, we counted the number of times each larva used each 229 motif within each time frame (e.g. day or night) and then normalised these counts by calculating 230 whether each motif was observed more or less frequently than in the paired shuffled data, a metric 231 we termed enrichment/constraint. Overall, we found that enrichment/constraint scores from our real 232 data were more prone to extreme positive (enriched) and negative (constrained) values than the 233 shuffled data (Figure 4a), suggesting that a minority of behavioural motifs were used more or less 234 frequently than would be expected by chance. 235

236
To test if these extremes occurred in particular contexts, we first compared motif usage between the 237 day and the night in wild-type larvae by generating a matrix of enrichment/constraint scores ( Figure  238 4b). To distil the most salient motifs from this and other contextual matrices, we used a three-step 239 approach. Firstly, we used the minimal-redundancy-maximal-relevance criterion (mRMR) algorithm 240 (Peng et al., 2005) to rank the motifs from most to least salient. Secondly, we trained linear 241 discriminant analysis classifiers using 10-fold cross validation as we iteratively increased the number 9 the subset of motifs which achieved the lowest classification error between groups in each context. 244 To determine how accurately these motif subsets could distinguish between behavioural contexts, we 245 compared each classifier's performance to that of a majority class classifier, which performed as well 246 as the ratio of samples between the two contexts. For example, in the day vs. night classification, a 247 majority class classifier would have an error rate of 50% (± standard error of proportion), as each larva 248 contributes an equal number of days and nights to the enrichment/constraint matrix. Additionally, to 249 demonstrate the salience of the motifs selected by the mRMR algorithm, we compared each classifiers 250 performance to a set of 10 classifiers built using the same number of motifs, though randomly 251 selected. For example, for a classifier which achieved its minimal classification error using 50 motifs, 252 we randomly selected 50 motifs from the library and built a classifier. For each comparison we 253 repeated this process 10 times. 254

255
Applying this algorithm to wild-type data revealed changes in motif usage across multiple timescales 256 (Supplementary Figure 6b). We found that only 15 motifs were required to classify day-and night-257 specific behaviour with only a 0.2% (±0.63% Std) classification error, compared to a majority class 258 classifier with 50% error and random 15-motif subsets with a mean error of 9.25% (Figure 4c, 259 Supplementary Table 1). The day enriched motifs consisted of high amplitude movements 260 interspersed with short pauses, while the night enriched motifs contained low amplitude movements 261 and long pauses ( Figure 4c). Next, we examined how motif usage changed over development by 262 comparing consecutive days and nights (5-6dpf). In both day 5 vs. day 6 and night 5 vs. night 6 263 comparisons, the classifiers achieved roughly 20% error using 93 and 85 motifs, respectively 264 (Supplementary Table 1). Thus, motif usage shifted over just 24 hours of development, though these 265 changes were far less prominent than those between the day and night. To study whether motif usage 266 varied at finer timescales, we first divided the day into morning/evening and the night into early/late 267 periods. In each case the mRMR algorithm performed better than the control classifiers 268 (morning/evening: 33%, early/late night: 36%) though the relatively high classification errors suggest 269 that motif selection did not vary strongly across each day or night (Supplementary Table 1

279
Finally, we hypothesised that behavioural motif usage would vary dose-dependently across 280 concentrations of melatonin and PTZ, providing insight into the mechanisms by which these 281 compounds exert their behavioural effects. Motif dose-dependency would suggest a continuously 282 modulated underlying process, which might arise if the fraction of bound receptors relates to neuronal 283 activity modulation. Alternatively, motifs enriched at only specific doses, would suggest discrete 284 effects upon neuronal circuitry, for example the binding of low affinity receptors. 285 286 Applying the mRMR algorithm to our pharmacological data revealed both dose-dependent and dose-287 specific modulation of motif usage. We found that each melatonin dose could be separated from the 288 others using 40 to 250 motifs with only 0-2.78% classification error (Figure 5a, Supplementary Table  289 2). Focussing on just the best motif for each comparison, we observed both dose-dependency as well 290 as dose-specificity. For example, comparing controls to all melatonin-dosed larvae identified a dose-291 dependent motif that consisted of large magnitude movements and short pauses, whose 292 enrichment/constraint score decreased with increasing melatonin concentration ( Figure 5a). 293 Conversely, the best 10µM motif, two long pauses broken by a small active bout sequence, showed 294 dose-specificity being enriched at only 3µM and 10µM doses ( Figure 5a). When applied to the PTZ 295 data, our approach performed even more accurately, achieving perfect classification (0% error) 296 between all conditions (Figure 5b and Appendix Table 2). Furthermore, in PTZ-dosed larvae we 297 observed enrichment for motifs highly constrained in wild-type larvae, highlighting the usage of motifs 298 beyond the normal wild-type repertoire, such as those corresponding to behavioural seizures ( Figure  299 5b). 300 301 Next, we tested whether our motif subset approach could detect hcrtr mutant phenotypes that were 302 not easily captured by other methods. For example, based upon human and rodent literature, where 303 loss of hypocretin is associated with narcolepsy (Lin et al., 1999) and prior zebrafish literature (Elbaz 304 et al., 2012), we expected abnormal transitions between active and inactive bouts. We found 305 reasonable performance when discriminating between hcrtr +/+ and hcrtr -/during both the day (16.7 ± 306 7.5% error with 195 motifs) and night (12.8 ± 9.6% error with 53 motifs) but weaker performance 307 when distinguishing between hcrtr +/+ and hcrtr -/+ , as expected for a haplosufficient gene 308 (Supplementary Figure 6c and Supplementary Table 2). Thus, homozygous loss of hcrtr impacts motif 309 usage enough to allow for successful classification of hcrtr -/mutants, though no single hcrtr -/motifs 310 with large differences in enrichment/constraint scores compared to wild type siblings were 311 particularly evident. 312 11 Collectively, these results demonstrate that behavioural motifs are used context dependently and 314 reveal how motif subsets can parse subtle differences in motif usage between behavioural contexts. 315 However, does motif analysis provide additional discriminatory power over module selection, which 316 also varies between behavioural contexts? To assess this, we compared the performance of each motif 317 classifier to paired module classifiers built from matrices of module probabilities. All of the motif 318 classifiers achieved better performance than their module pairs (Figure 5c), demonstrating both the 319 phenotyping value of the motifs and their importance in the structure of larval behaviour. 320 321 322

323
Here, we developed and applied computational tools to describe high-throughput, long-timescale 324 behavioural data in terms of behavioural units (modules), and sequences of modules (motifs) 325 organised across sub-second to day-long timescales. 326 327 328

329
Low dimensional representations of behaviour, such as the Δ pixels metric employed here, result in a 330 loss of information, for example direction of movement or posture. Such metrics do however facilitate 331 screening approaches and/or long-timescale tracking and in these contexts have provided biological 332 insight into the molecular targets of small molecules (Rihel et al., 2010) and genetics of ageing (Churgin 333 et al., 2017). Our work builds on previous long-timescale studies of behaviour by assessing sub-second 334 resolution Δ pixels data across multiple days and nights. This improved resolution enabled the 335 segmentation and parameterisation of individual active and inactive bouts from our data, revealing 336 how larvae adapt their behaviour across the day/night cycle and how behaviour is impacted by small 337 molecules. 338 339 Future work should aim to extend our assay by recording more detailed behavioural measures. 340 Indeed, a recent study using centroid tracking in 96 well plates revealed that larvae show a day/night 341 location preference within the well, and furthermore uncovered a mutant with a difference in this 342 metric (Thyme et al., 2019), demonstrating that even within the confined space of a 96-well plate, 343 location is an informative metric to record. It is likely that even more detailed behavioural measures, 344 like eye and tail angles, will yield additional insights, for example enabling the exploration of rapid-345 eye-movement sleep in zebrafish larvae (Shein-Idelson et al., 2016). Such metrics could be extracted 346 by skeletonization or even through the use of an autoencoder applied to the raw video frames from 12

349
A key idea in ethology is that behaviour consists of stereotyped modules arranged into motifs (Lashley, 350 1951;Tinbergen, 1963). While early studies described behaviour in this manner through manual 351 observations (Richard and Dawkins, 1976), recent advances in machine vision and learning have 352 automated these processes (Todd et al., 2017). For example, in zebrafish larvae, recent work used 353 unsupervised learning to uncover a locomotor repertoire of 13 swim types including slow forward 354 swims and faster escape swims (Marques et al., 2018), although inactive bouts were not considered. 355 From our dataset, we identified 5 active and 5 inactive modules, which respectively describe swim 356 these probabilities illustrates that module usage can be flexibly re-organised depending upon 361 behavioural context (Wiltschko et al., 2015). 362 363 To discretize our bouts into modules, we first extracted hand-engineered features from each bout 364 ( Figure 1a) and then applied an evidence accumulation based clustering algorithm (Fred andJain, 365 2002, 2005). While our results demonstrate the relevance and utility of these modules in describing 366 larval behaviour, it is possible that our approach missed rare bout types. Consequently, future work 367 should build upon our bout classification by exploring the benefits of including additional features, the 368 use of alternative clustering algorithms and our assumption of stereotypy, i.e. that all bouts can be fit 369 into a module (Berman, 2018). An alternative direction would be to produce a mapping between our 370 active modules and those identified from analysis of larval posture (Marques et al., 2018). Bridging 371 this gap could facilitate behavioural screening approaches, for example by using data from our set-up 372 to prioritise pharmacological compounds or mutants for postural analysis. 373 374 375

376
In some contexts, it is beneficial for animals to execute coordinated patterns of behaviour. For 377 example, to efficiently search an environment zebrafish larvae will execute organised sequences of 378 left and right turns (Dunn et al., 2016). In other contexts, more random behaviour will be 379 advantageous, such as when escaping from a predator (Maye et al., 2007). Quantifying structure in 380 behaviour thus provides insight into the overarching strategy being employed in particular contexts. 381 13 Association, 2013). Consequently, compression would be a relevant and likely informative metric to 384 record in animal models or even human cases for such conditions. 385 386 To quantify structure in larval zebrafish behaviour in different contexts, we inputted each larva's 387 modular sequence to a compression algorithm. We found that wild-type behaviour was more 388 compressive during the day than the night ( Figure 3b). This echoes recent work in Drosophila that 389 revealed higher temporal predictability during the day than the night as well as in females (Fulcher 390 and Jones, 2017). A likely explanation for these findings comes from work in C. elegans  Finally, by distilling salient subsets of motifs from our library, we demonstrated that motif usage was 419 context dependent and highlighted the discriminatory power of motif subsets, which were capable of 420 distinguishing between day/night behaviour and even between small changes in compound dose. 421 Comparing motif usage across the day/night cycle identified a set of highly night specific motifs ( Figure  422 4c), which may represent sleep behaviours. One way in which future studies could address this 423 possibility would be to deprive larvae of these motifs throughout the night, for example by using a 424 closed-loop paradigm (Geissmann et al., 2019), and observing the impact on larval behaviour the 425 following day. In relation to the PTZ data, comparing seizure motifs across epileptogenic compounds 426 and mutants with spontaneous seizures could suggest clues as to their underlying mechanism (Kokel 427 et al., 2010;Rihel et al., 2010). For example, seizures with similar motif usage patterns may originate 428 in the same brain area or impact awareness in the same manner. This hypothesis could be tested by 429 generating whole-brain activity maps (Randlett et al., 2015)

454
Adult zebrafish were reared by UCL Fish Facility on a 14hr/10hr light/dark cycle (lights on: 09:00 a.m. 455 to 23:00 p.m.). To obtain embryos, pairs of adult males and females were isolated overnight with a 456 divider that was removed at 09:00 a.m. the following morning. After a few hours, fertile embryos were 457 collected and sorted under a bright-field microscope into groups of 50 embryos per 10 cm petri dish 458 filled with fresh fish water (0.3g/L Instant Ocean). Plates were kept in an incubator at 28.5°C on a 459 14hr/10hr light/dark cycle. Using a Pasteur pipet under a bright-field microscope, debris was removed 460 from the plates and the fish water replaced each day. All work was in accordance with the UK Animal The term "wild-type" refers to the AB x TUP LF zebrafish strain. This line was used for the wild-type 489 experiments, as well as the melatonin and PTZ dose response curves. hcrtr (ZFIN ID: hu2098 490 (Yokogawa et al., 2007). Identified from an ethylnitrosourea-mutagenized screen. UCL Line 2114.) 491 experiments were carried out on embryos collected from heterozygous in-crosses, with larvae 492 Following each hcrtr experiment each larva was euthanised in its well (as above) and DNA was 498 extracted using HotSHOT DNA preparation (Truett et al., 2000). Larval samples were transferred to 499 the individual wells of a 96-well PCR plate. Excess liquid was pipetted from each well before applying 500 50µl of 1x base solution (1.25M KOH, 10mM EDTA in water). Plates were heat sealed and incubated 501 at 95°C for 30 minutes then cooled to room temperature before the addition of 50µl of 1x 502 neutralisation solution (2M Tris-HCL in water). 503 504 PCR 505 The following reaction mixture per sample was prepared on ice in a 96-well PCR plate: 18.3µl PCR mix 506 (2mM MgCl2, 14mM pH 8.4 Tris-HCl, 68mM KCl, 0.14% Gelatin in water, autoclaved for 20 minutes, 507 cooled to room temperature, chilled on ice, then we added: 1.8% 100mg/ml BSA and 0.14% 100mM 508 d [A, C, G, T ] TP), 0.5µl of forward and reverse primers (20 µM), 5.5µl water, 0.2µl of Taq polymerase 509 and 3.0µl of DNA. Next, each plate was heat sealed and placed into a thermocycler, set with the 510 following program: 95°C --5 minutes, 44 cycles: 95°C --30 seconds, 57°C --30 seconds and 72°C --45 511 seconds, then 72°C --10 minutes and 10°C until collection. Finally, samples were mixed with 6x loading 512 buffer (Colourless buffer: Ficoll-400 -12.5g, Tris-HCl (1M, pH 7.4) -5ml, EDTA (0.5M) -10mL, to 50ml 513 in pure water; heated to 65°C to dissolve, per 10ml of colourless buffer 25mg of both xylene cyanol 514 and orange G were added, then diluted to 6x) and run on agarose gels (1-2%) with 4% GelRed ( determining the number of pixels that changed intensity within each well between each pair of 578 frames, termed Δ pixels. To acquire behaviour data, each Zebrabox was setup using ViewPoint's 579 ZEBRALAB software (version 3.22), which outputs a .xls and a .raw file (ViewPoint specific format) per 580 experiment. Each behaviour .xls file was reorganised into a .txt file using the function 581 perl_batch_192.m (Jason Rihel). For each experiment a .txt metadata file assigning each animal to an 582 experimental group, for example genotype, was manually produced. To replicate the previous analysis 583 methodology, as in Supplemental Figure 1c, behaviour and metadata .txt files were input to the 584 function sleep_analysis2.m (Jason Rihel). 585

586
To assess data on a frame by frame basis, each experiment's .raw file which was output from 587 ViewPoint's Zebrabox, was exported within the ZEBRALAB software to thousands of .xls files. Each .xls 588 file contained 50,000 rows and 21 columns, with data from any given well listed approximately every 589 192 rows, as the setup always assumes recordings are from two 96-well plates. This formatting is, 590 however, only approximate as infrequently the well order is erroneously non-sequential; these rows 591 were termed ordering errors. Each .xls file is formatted with 21 columns, of which 3 contain useful 19 data: type -notes when ViewPoint defined data acquisition errors occurred; location --denotes which 593 well the data came from; and data1 -records the Δ pixel value from that well for that time point. 594 The function Vp_Extract.m was used to reformat the .xls files from each experiment to single frame 595 by fish matrices, from which each animal's behaviour was quantified. Vp_Extract.m requires three 596 inputs to be selected: a folder containing the .xls files; a .txt behaviour file output from 597 perl_batch_192.m; and a .txt metadata file. To ensure that each animal has the same number of 598 frames, frames with ViewPoint defined errors or ordering errors (which are automatically detected by 599 Vp_Extract.m) are discarded. A maximum Δ pixels value can be set and active bouts containing even a 600 single frame with a higher Δ pixels value than this are set to zero for the entire duration of the bout. 601 Here a maximum Δ pixels threshold of 200 was set. This value was determined from manual inspection 602 of the dataset as well as by comparisons of this data to data recorded from plates with no animals in. 603 Time periods during which water is being replenished are automatically detected and set to a Δ pixels The script Bout_Clustering.m was used to cluster all active and inactive bouts into behavioural 620 modules, as well as to compare the resultant modules. To cluster the data an evidence accumulation 621 approach is used (Fred andJain, 2002, 2005)  The function gmm_sample_ea.m clusters data using an evidence accumulation approach (Fred and627 Jain, 2002, 2005)  Computing Services, UCL) and processed in parallel with a worker for every fish. MATLAB code for 651 hierarchical compression is described in Gomez-Marin et al., (2016). MATLAB code for submitting 652 these jobs to Legion, analysing data and retrieving results is available at 653 https://github.com/ghoshm/Legion_Code. Ultimately, Bout_Transitions.m outputs a library of 654 behavioural motifs and motif related figures (e.g. Figure 3). At the acquisition stage, Δ pixels data was filtered by the software (ViewPoint) such that each frame 666 for a given well was scored as either zero or higher. In the absence of movement within a well, and 667 hence no pixels changing intensity, Δ pixels values of zero were recorded. These periods were termed 668 inactive bouts and were defined as any single or consecutive frames with Δ pixels values equal to zero. 669 The length of each inactive bout was used as a descriptive feature. When there was movement within 670 a well, Δ pixels values greater than zero were recorded. These periods were termed active bouts and 671 were defined as any single or consecutive frames with Δ pixels values greater than zero. Six features 672 were used to describe each active bout: length, mean, standard deviation, total, minimum and 673 maximum. These features, as well as the number of active bouts, percentage of time spent active and 674 total Δ pixels activity, were compared between conditions, e.g. day and night and dose of drug, in two 675 ways using the function Vp_Analyse.m. 676

677
To compare the distribution of values for each feature between conditions, a probability density 678 function (pdf) was fit to each animal's data and the mean shape of each condition's pdf was compared 679 using a Two-sample Kolmogorov-Smirnov test (e.g. Supplementary Figure 2a  Clustering reduced each animal's behaviour to a non-repetitive sequence of active and inactive bouts, 726 termed modules. On average this reduced each wild-type sequence length by 96%, from 6,308,514 727 frames to 236,636 modules, easing the computational demands of compressing these sequences.

23
To compress modular sequences, an offline compressive heuristic (Nevill-Manning and Witten, 2000) 730 was used (equation 2). At each iteration (i) of the algorithm, the most compressive motif was defined 731 as the motif which made the most savings, a balance between the length of the motif (W) and the 732 number of times it occurred in the sequence (N), which also considered the combined cost of adding 733 a new motif to the dictionary (W + 1) and of introducing a new symbol into the sequence (+N) at every 734 occurrence of this motif in the sequence. To identify both which and how many motifs were required to distinguish between behavioural 755 contexts (e.g. day and night), the following approach was executed by the function 756 Batch_Grammar_Freq.m. Firstly, the number of occurrences of every motif from the common motif 757 library was counted in every real and shuffled modular sequence. Next, to calculate 758 enrichment/constraint scores for every motif, the deviation of the real from shuffled counts, as well 759 as the deviation of each shuffle from the other shuffles, was calculated (equation 3). For a given animal formatted into a matrix of samples by motifs (e.g. Figure 4b). Scores for each motif (column) were 775 normalised by subtracting each column's mean score and dividing by each column's standard 776 deviation. A supervised feature selection algorithm (Peng et al., 2005) was applied to these matrices 777 to select the top 250 maximally relevant and minimally redundant (mRMR) motifs. To determine how 778 many of these motifs were necessary for accurate classification, linear discriminant analysis classifiers 779 were trained on this data using 10-fold cross validation as sequential mRMR motifs were added, and 780 classification error mean and standard deviation were calculated. The MATLAB function fitcdiscr 781 (Statistics and Machine Learning Toolbox) was used to implement these steps. Finally, to determine 782 how many motifs were necessary for a given comparison, classification error curves were smoothed 783 with a running average 3 motifs wide and the number of motifs at which the minimum classification 784 error occurred was identified (Supplementary Figure 6a). To evaluate classifier performance, the 785 results of each classifier were compared to a majority class classifier whose performance depended 786 upon the ratio of samples of each class. For example, in a dataset with two labels at a ratio of 0.1 : 0.9, 787 the majority class classifier would consistently assign the latter label and achieve a classification error 788 of 10% (± standard error of proportion). Additionally, we compared each classifiers performance to a 789 set of 10 classifiers built using the same number of motifs, though randomly selected. For example, 790 for a classifier which achieved its minimal classification error using 15 motifs, we randomly selected 791 15 motifs and trained classifiers as above. We repeated this process 10 times per classifier and report 792 the error and standard deviation across these 10 repeats. Processed behavioural data is available at: https://zenodo.org/record/3344770#.XYYwYyhKiUk. Raw 799 data is available upon request. 800

801
A video of 96, 6dpf zebrafish larvae swimming in our rig. The last 1 second of each larva's Δ pixels data 802 is plotted over each well. This video was filmed at 25Hz and is played back in real time. 803

804
A video of 96, 6dpf zebrafish larvae swimming in our rig. The last 1 second of each larva's Δ pixels data 805 is plotted over each well, with each active and inactive bout coloured according to its module 806 assignment. This video was filmed at 25Hz and is played back in real time. 807 808 Acknowledgements 809 We thank members of the UCL fish floor for insightful discussions, Ida Barlow and François Kroll for 810 comments on the manuscript, and the UCL Fish Facility and staff for support.