Scalable Apparatus to Measure Posture and Locomotion (SAMPL): a high-throughput solution to study unconstrained vertical behavior in small animals

Balance and movement are impaired in a wide variety of neurological disorders. Recent advances in behavioral monitoring provide unprecedented access to posture and locomotor kinematics, but without the throughput and scalability necessary to screen candidate genes / potential therapeutics. We present a powerful solution: a Scalable Apparatus to Measure Posture and Locomotion (SAMPL). SAMPL includes extensible imaging hardware and low-cost open-source acquisition software with real-time processing. We first demonstrate that SAMPL’s hardware and acquisition software can acquire data from D. melanogaster, C.elegans, and D. rerio as they move vertically. Next, we leverage SAMPL’s throughput to rapidly (two weeks) gather a new zebrafish dataset. We use SAMPL’s analysis and visualization tools to replicate and extend our current understanding of how zebrafish balance as they navigate through a vertical environment. Next, we discover (1) that key kinematic parameters vary systematically with genetic background, and (2) that such background variation is small relative to the changes that accompany early development. Finally, we simulate SAMPL’s ability to resolve differences in posture or vertical navigation as a function of effect size and data gathered – key data for screens. Taken together, our apparatus, data, and analysis provide a powerful solution for laboratories using small animals to investigate balance and locomotor disorders at scale. More broadly, SAMPL is both an adaptable resource for laboratories looking process videographic measures of behavior in real-time, and an exemplar of how to scale hardware to enable the throughput necessary for screening.


INTRODUCTION
Measuring posture and locomotion is key to understand nervous system function and evalu- 23 ate potential treatments for disease -particularly neurological disorders 1 . Behavioral screen- 24 ing is a fundamental part of both basic and translational approaches to disease 2,3 . For screens, 25 measuring behavior from large numbers of animals is necessary to differentiate individual vari- 26 ation 4 from changes seen in disease models and/or improvement following treatment 5,6 . The 27 demand for such high-throughput measurements comes at a cost: often, measurements that 28 require high resolution -such as posture -are limited. Modern machine learning algorithms 29 and inexpensive videographic / computing hardware have automated measurements of pos- 30 ture and kinematics [7][8][9] and illuminated our understanding of animal behavior [10][11][12] . We sought 31 to combine videographic analysis of posture and vertical locomotion with the scalability amenable 32 to screening. 33 Over the past decade, we have studied posture and locomotion using the larval zebrafish as a 34 model. Neural architecture is highly conserved across vertebrates, making larval zebrafish an 35 excellent model to understand the underpinnings of locomotion 13,14 and balance 15 . For our 36 studies, we developed a new apparatus/analysis pipeline to measure the statistics of posture 37 in the pitch (nose-up/nose-down) axis and locomotion as larvae swam freely in depth. We dis- 38 covered that larvae learn to time their movements to facilitate balance 16 , that larvae modulate 39 the kinematics of swimming to correct posture 17 , and that larvae engage their pectoral fins to 40 climb efficiently 18 , and implicated different neuronal circuits in each of these behaviors. While 41 informative, data collection was slow (months) on small numbers (<5) of apparatus. Increasing 42 throughput remains a challenge common to laboratories that develop new tools to measure 43 behavior. 44 To meet the needs of scalability, resolution, and extensibility we developed SAMPL: a low-cost, 45 open-source solution that measures posture and vertical locomotion in real-time in small an- 46 imals. Further, we provide a turn-key analysis pipeline to measure larval zebrafish balance be- 47 navigate (i.e. climb/dive) in the water column. Our new dataset represents two weeks worth 54 of data collection, and allowed us to detail variation in postural/locomotor behaviors. By mea- 55 suring behavior across different genetic backgrounds and development, we report two new 56 findings. First, variation in posture/locomotion is systematic across genotype and second, the 57 scale of variation in behavior across development is much larger than background genetic 58 variation. We use these new data to simulate the resolving power for each behavioral param- 59 eter as a function of data gathered -foundational information to rigorously assay the effects of 60 candidate genes or small molecules on posture or locomotion. SAMPL thus offers a straight- 61 forward way to gather data from small animals, and a turn-key solution to screen for balance 62 and vertical locomotion in larval zebrafish. More broadly, SAMPL offers a template for labora- 63 tories looking to scale their own behavioral apparatus to achieve the throughput necessary for 64 screens. SAMPL will thus facilitate reproducible studies of postural and locomotor behaviors in 65 both health and disease, addressing unmet needs in treating neurological disorders, particu- 66 larly with balance symptoms 19 . 67

68
SAMPL hardware & software overview 69 To overcome measure posture with the throughput necessary for genetic and drug screens, 70 we deployed SAMPL, a real-time videographic system ( Figure 1A) that records small animal 71 behavior in the vertical axis. Below we briefly describe the hardware and software that com- 72 prise SAMPL. SAMPL's hardware consists of three simple modules: an infrared (IR) illumination 73 module ( Figure 1B), a camera-lens module ( Figure 1C), and two clamps to hold fish chambers 74 ( Figure 1D). All three modules are mounted directly ( Figure 1A) onto an aluminum breadboard 75 ( Figure S1) and a light-tight enclosure covers the entire apparatus to permit individual control 76 of lighting ( Figures 1F and 1G). Details of hardware and software design can be found in Ap- 77 pendices 1&2. A complete parts list is in Table 1, hardware assembly instructions in Appendix The second module captures videographic data. It consists of a camera and lens optimized for 86 speed, resolution, compactness, and affordability. The camera hardware satisfies the follow- 87 ing demands: (1) large pixel size with low noise allowing for high dynamic range / signal-to- 88 noise ratio; (2) sufficient resolution to resolve subtle changes to animal posture; (3) an interface 89 with sufficient bandwidth for data transfer; (4) availability. The lens achieves (1) close focus; (2) 90 sufficient depth-of-field to cover the entire depth of the imaging arena; (3) high image qual- 91 ity; (4) compact size; (5) high IR transmission rate; (6) ease of integrating an IR-pass filter. We 92 adapted a 50 mm IR-optimized lens by placing a 0.3" extension tube between the lens and 93 the camera to achieve higher magnification ratio with minimum working distance. The space 94 between camera adapters and the extension tube allows us to fit a 25 mm IR-pass filter; the 95 extension tube gives a mount point to connect the module to the base ( Figure 1A). Using this 96 camera-lens module, we image an area~400 mm 2 ( Figure 1E, pink square) at 166 Hz with 97 1200×1216 pixels at a focal distance of~24 cm. 98 The final module is a rectangular arena optimized for vertical locomotion (i.e. parallel to the 99 focal plane). By design, the chamber size is larger than the imaging area, allowing stochastic 100 sampling of freely behaving animals in a large enough arena. The bottom of the chamber is 101 below the field of view so that animals sitting at the bottom will not be recorded. We assem- 102 bled custom-fabricated chambers from laser-cut acrylic by cementing transparent front and 103 back sides to a U-shaped piece that forms the narrower sides ( Figure 1E). We designed two 104 types of chambers with different inner widths to adapt to the needs of different experiments: 105 a wider standard chamber optimized for larger groups of animals and a narrower chamber for 106 1-3 animals ( Figure 1E). Chambers can be easily dropped into the holders ( Figure 1D) from the 107 top of the behavior box and secured in place for recording. 108 SAMPL includes a complete suite of open-source software for acquisition/real-time extraction saved as an AVI file. 118 SAMPL's modules and software were designed to scale, minimizing footprint and experimenter 119 time. We multiplex apparatus, providing three distinct compiled applications designed to run 120 simultaneously on one computer to reduce cost/footprint. A set of three SAMPL apparatus and 121 a computer case fit on one 24"x36" shelf ( Figure 1H). One SAMPL "rack" consists of four such 122 shelves (81.5" high) and costs~$40,000-$45,000 (December 2022, before volume discounts). 123 In our laboratory, trained experimenters can load such a rack for a typical 48 hr experiment in 124 30 minutes. Taken together, SAMPL's design is ideal to efficiently gather data describing pos- 125 ture and vertical locomotion. 126 127 SAMPL is well-suited to collect data from a wide range of small animals. We demonstrate the 128 flexibility of SAMPL's acquisition suite using three common model organisms. By changing 129 SAMPL's thresholds (Table 2), we could acquire data from three different organisms: Drosophila 130 melanogaster climbing behavior (Figures 2A and 2B), continuous locomotion in Caenorhabdi- 131 tis elegans ( Figures 2C and 2D), and swimming in Danio rerio ( Figures 2E and 2F). We present 132 raw video from the epochs in Figure 2 together with plots of real-time image processing (fly & 133 worms, Movie 2; fish, Movie 3). These results demonstrate SAMPL's excellent flexibility and ro- 134 bustness in real-time recording and analysis of vertical locomotion of small animals. 135 SAMPL validation: measuring postural and locomotor kinematics in real-time 136 Next, to demonstrate how SAMPL facilitates efficient collection of high-quality kinematic data, 137 we gathered a new dataset from larval zebrafish (7-9 days post-fertilization, dpf) that swam 138 freely in the dark. A typical experimental repeat consisted of two sequential 24-hour sessions 139 using 3 SAMPL boxes. Data were pooled across 27 repeats for subsequent analysis of kine- 140 matics. Each 24-hour behavior session yielded on average 1223±481 bouts per day for the 141 standard chamber (6-8 fish) and 1251±518 bouts per day for the narrow chamber (1-3 fish). 142 While not analyzed, running a single fish in the narrow chamber yielded 891±903 bouts over 143 24hrs. Based on the number of apparatus used, we estimate that a similar dataset (total n=121,979 144 bouts) could be collected in two weeks using a single SAMPL rack. 145 We first used our data to establish basic distributions of locomotion and posture. We used SAMPL's 146 processing algorithm to extract the following information in real-time: (1) pitch, defined as the 147 angle between the long axis of the fish's body and the horizon ( Figure 2E); (2) x (azimuth), z 148 (elevation) coordinates of the center of the pixels that correspond to the fish. After collection, we used SAMPL's processing suite to extract basic postural kinematics during swimming. Ze-150 brafish larvae swim in discrete periods of translation called "swim bouts" ( Figure 2F) 16,20 . We 151 defined swim bouts as periods where the instantaneous speed exceeds 5 mm/sec ( Figure 2F, 152 dashed line). The time of the peak speed was defined as t = 0 ms ( Figure 2F, cyan lines). Swim 153 bouts were aligned to peak speed for extraction of kinematic parameters; the period 250 ms 154 before and 200 ms after peak speed was reserved for future analysis. We observed that ze-155 brafish larvae swim predominantly at slower speeds with mean and standard deviation mea-156 sured 12.90±4.91 mm/s, on par with previous reports 16,[20][21][22] . Larvae showed a broad distri-157 bution of postures evaluated at peak speed (8.48°±15.23°) with a positive (nose-up) average, 158 suggesting that SAMPL detected a variation of nose-up and nose-down swim bouts. SAMPL 159 can thus rapidly acquire a rich dataset of spontaneous locomotor behavior and a wide range of 160 "natural" postures. 162 SAMPL includes data analysis and visualization code (Python source and sample datasets pro-163 vided) optimized to extract key parameters of balance and locomotion from larval zebrafish. 164 We use our "two-week" dataset to demonstrate that SAMPL can resolve these four parameters: We conclude that SAMPL's resolution and throughput allows rapid and deep insight into each 170 parameter, detailed below. Data analysis using the provided scripts on the provided dataset 171 runs in 30 minutes on a typical analysis computer (M1 processor, 16GB RAM). Full details of 172 analysis/visualization is provided in Appendix 4, and a step-by-step guide to set up the relevant 173 environment and to run experiments provided in Appendix 5. 174 Proper balance requires active stabilization. Zebrafish larvae are front-heavy and therefore sub-175 ject to destabilizing torques in the pitch (nose-up/nose-down) axis. Swim bouts counteract the 176 resultant forces, stabilizing the fish. Zebrafish larvae learn to initiate swim bouts when unsta-177 ble 16 . We first defined movement rate as the reciprocal of the inter-bout interval (Figures 3A   178 and 3B). More extreme postures were associated with higher movement rate ( Figure 3C), with 179 a parabolic relationship ( Figure 3D, R 2 = 0.14). We expect that the majority of the residual vari-180 ance reflects a previously-reported dependence of movement timing on angular velocity 16 .

181
. 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 March 27, 2023. The three coefficients of the parabola represent the baseline posture, the basal rate of move-182 ment, and -key to our analysis -the degree to which postural eccentricity relates to movement 183 rate, or "sensitivity," ( Figure 3D). SAMPL therefore permits efficient quantification of a crucial 184 posture-stabilizing behavior: the relationship between perceived instability and corrective be-185 havior. 186 Like most animals, larval zebrafish go where their head points. To adjust their vertical trajec-187 tory (i.e. to climb or dive) larvae must rotate their bodies away from their initial posture, point-188 ing in the direction they will travel ( Figures 4A and 4B) 17,23 . Previous work 17 established that 189 steering rotation in larvae swimming spontaneously occurs mostly before they reach the peak 190 speed ( Figure 4C). A larva's steering ability reflects the relationship between the change in pos-191 ture before the peak speed and the resultant deviation in trajectory ( Figure 4D). We parame-192 terized steering as the slope (gain) of the best-fit line between posture and trajectory evaluated 193 at the time of peak speed ( Figure 4E). A gain of 1 indicates that the observed trajectory could 194 be explained entirely by the posture at the time of peak speed ( Figure 4F). SAMPL revealed 195 that 7 dpf larvae exhibit an average steering gain at 0.67, suggesting an offset between pos-196 ture and trajectory at the time of peak speed ( Figure 4E, R 2 = 0.92). SAMPL allows us to infer 197 how effectively larvae steer using axial (trunk) musculature to navigate the water column. 198 To climb (Figures 5A and 5B) fish generate lift with their pectoral fins, assisting steering rota-199 tions and subsequent axial undulation 24,25 . Larval zebrafish learn to climb efficiently by coor-200 dinating their trunk and fins 18 . We defined the attack angle, or the additional lift associated 201 with each climb, as the difference between the steering-related changes and the resulting tra-202 jectory ( Figure 5C). We evaluated attack angle after pectoral fin loss, revealing a clear contribu-203 tion to climbs ( Figure 5D). Next, we demonstrate a positive correlation (with rectification and 204 asymptote) between steering-related rotations and fin-based attack angle ( Figure 5E, left). No-205 tably, after peak angular velocity, rotations are poorly correlated with attack angles (r = -0.17) 206 ( Figure 5E, right). These residuals reflect the initial angular deceleration as fish reach their peak 207 speed ( Figure 5A). We parameterize the relationship between the initial rotation and the attack 208 angle using logistic regression ( Figure 5F, R 2 = 0.31). The regression reveals the maximal slope 209 of the sigmoid relating steering and lift ( Figure 5G). We named this slope "fin-body ratio" as it 210 describes how larvae distribute labor between axial and appendicular muscles, i.e. between Larvae must actively maintain their preferred posture in the pitch axis. To do so, they rotate 214 partially towards their preferred orientation as they decelerate ( Figures 6A to 6C). The magni-215 tude of these rotations scales with the eccentricity of their posture before a swim bout 17 . We 216 estimated the slope (-0.17) of the line that related initial posture and the amount the fish ro-217 tated back toward the horizontal ( Figure 6D), R 2 = 0.56. As the behavior is corrective, the re-218 lationship is negative; we therefore define the gain of righting as the inverse of the slope (Fig-219 ure 6E). We further define the "set point" as the point where an initial posture would be ex-220 pected to produce a righting rotation of zero ( Figures 6E and 6F). SAMPL facilitates quantifi-221 cation of corrective reflex abilities (gain) and associated internal variables (set point). 222 Taken together, our estimates of key posture and locomotor parameters establish that SAMPL 223 can rapidly generate datasets that permit rich insight into the mechanisms of balance and ver-224 tical navigation. 225 SAMPL can resolve slight variations in posture control strategies across genetic backgrounds 226 To be useful SAMPL must resolve small but systematic differences in key measures of posture 227 and vertical locomotion. Even among isogenic animals reared in controlled environments, ge-228 netic differences contribute to behavioral variability [26][27][28][29][30][31][32][33] . The "two-week" dataset analyzed  Figure 7C). Correspondingly, 244 and righting gain ( Figure 7K), all of which contributes positively to their higher posture stability. 246 These results demonstrate that SAMPL is capable of detecting inter-strain variations in locomo-247 tion and balance behavior. 248 In contrast, larvae of different ages adopt different strategies to stabilize posture and navigate 249 in depth [16][17][18] To contextualize the magnitude of strain-related differences we gathered a lon-250 gitudinal dataset by measuring behavior from the same siblings of the AB genotype at three 251 timepoints: 4-6, 7-9, and 14-16 dpf (Table 3). We observed that the standard deviation of IBI 252 pitch for 4 and 14 dpf larvae was 38.1% higher and 11.3% lower, respectively, than the aver-253 age result of 7 dpf larvae (Table 3). Across strains at 7 dpf, the variation was much smaller: from 254 11.8% higher to 11.2% lower. Similarly, relative to 7 dpf larvae, sensitivity of 4 dpf larvae was 255 considerably lower (-42.5%), and increased to 23.6% higher by 14 dpf (Table 3); variations among 256 7 dpf strains were up to 10.0% lower and 15.4% higher. 257 Our analysis of new data supports three key conclusions. First, SAMPL can uncover small, sys-258 tematic differences in the way fish swim and stabilize posture. Second, SAMPL can make lon-259 gitudinal measures of the same complement of animals as they develop. Third, relative to de-260 velopment, the effect of genetic background is small. We conclude that SAMPL's capacity to 261 resolve small differences supports its usefulness as a tool screen for modifiers of postural con-262 trol and vertical locomotor strategies.

263
Estimating SAMPL's resolution 264 Our dataset establishes SAMPL's ability to resolve small kinematic differences between cohorts. 265 How does SAMPL's power change as a function of the size of the dataset? We used resampling 266 statistics to estimate SAMPL's resolution as a function of the number of the bouts (Methods). 267 To ensure our most conservative estimate, we resampled data combined across AB, SAT and 268 WT genotypes at 7dpf. 269 As expected, the width of the confidence interval for any estimated parameter decreased with 270 the number of bouts ( Figure 8A). The most challenging parameter to estimate is coordination 271 between fin and trunk (fin-body ratio) The steepness with which the confidence interval width 272 decreases follows the number of regression coefficients necessary for each measure: fin-body 273 ratio (4 parameters); bout timing (3 parameters); and steering or righting (2 parameters). We 274 therefore propose that these particular measures can serve as a general guide for the chal-275 lenge of estimating parameters within a SAMPL dataset. 276 reject the null hypothesis 34 . We address this question by asking how much data one would 278 need to gather in order to detect meaningful effects. We simulated difference of particular 279 magnitudes by imposing an offset on each parameter (sensitivity, steering gain, fin-body ra-280 tio, and righting gain) while preserving the original variance (Methods). Offsets were expressed 281 as a fractional difference, and resampling was used to estimate the effect size one would see 282 as a function of the number of bouts/IBIs when comparing kinematic parameters between the 283 original dataset and the dataset with an imposed effect (Methods). 284 Broadly, we find that for all kinematic parameters, the smaller the percent change, the larger 285 the required sample size ( Figure 8B). Steering and righting gains require the fewest bouts to 286 detect a 1-2% change with an effect size > 0.5 ( Figure 8B, green and red). However, sensitivity 287 and fin-body ratio require relatively larger datasets to confidently discriminate small changes 288 ( Figure 8B, brown and magenta). We conclude that the full "two-week" dataset we generated 289 using SAMPL (n = 121,979 bouts) is sufficient to reveal any biologically-relevant differences be-290 tween two conditions. 291 In summary, these simulations demonstrate that a single SAMPL rack divided into two condi-292 tions (6 apparatus / each) could, in two standard 48-hour runs, generate sufficient data to re-293 solve meaningful differences in postural and locomotor kinematics between two conditions. 294 We provide detailed instructions in Appendix 5 addressing experimental design strategies to 295 maximize SAMPL's resolution. 297 We present SAMPL, a scalable solution to measure posture and locomotion in small, freely-298 moving animals. We start with a brief overview of the hardware and software, with compre-299 hensive guides to every aspect of SAMPL's hardware and software included in the Appendices. Danio rerio (zebrafish). To illustrate the depth of insight accessible using SAMPL we explored a 303 new dataset -consisting of two weeks worth of data -that illuminates four key parameters of 304 zebrafish navigation in depth: bout timing, steering, fin-body coordination, and righting. We 305 made two discoveries using SAMPL's analysis suite: (1) systematic changes to zebrafish pos-306 ture and locomotion across genetic backgrounds and (2) that these changes were small rela-307 tive to variation across developmental time. Finally, we use our new dataset to define SAMPL's 308 resolution: how much data an experimenter would need to collect to detect meaningful ef-309 . 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  SAMPL was designed to scale efficiently. Data is gathered by a compiled executable, allowing 333 SAMPL to run three apparatus off a single computer, reducing costs and space. A SAMPL rack 334 consists of 12 apparatus running off four computers with a footprint of 24"x36"x81.5" (LxWxH). 335 The key components such as the camera are readily available from multiple suppliers. Taken 336 together, SAMPL can be used immediately to screen and/or to provide videographic data from 337 freely moving animals at scale. 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

DISCUSSION
The copyright holder for this preprint this version posted March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 doi: bioRxiv preprint between the body and the horizon. As we demonstrate here, this small set of parameters de-342 fines behaviors larval zebrafish use to swim and balance in depth: bout timing (Figure 3), steer-343 ing ( Figure 4), fin-body coordination ( Figure 5), and righting ( Figure 6). While each parameter 344 has been previously defined [16][17][18] , the new data we present here illustrates differences across 345 genetic backgrounds and development and allows granular estimation of statistical sensitivity. 346 Taken together, SAMPL's focus facilitates exploration of unconstrained vertical behavior.

347
Comparisons with other approaches 348 Here, we discuss SAMPL's advantages by comparing it with other available tools for measuring 349 Drosophila, C. elegans, and zebrafish behavior.  (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 simple nervous system of C. elegans is a powerful model to study neural circuits that con-373 trol posture and movement. C. elegans possess a rich and tractable repertoire of motor con-374 trol 53 . For example a pattern generator creates sinusoidal waves of muscle contraction that 375 propel C. elegans on a solid substrate, and these sinusoidal movements are sculpted by pro-376 prioceptive feedback 54 . Proprioceptive feedback also controls transitions between sinusoidal 377 crawling and non-sinusoidal bending that can propel animals in a liquid environment 55-57 . 378 Other sensory stimuli elicit coordinated motor responses that are critical for navigation. De-379 creasing concentrations of attractive odorants and gustants trigger reversals followed by a pirou-380 ette or omega bend, which results in a large-angle turn that reorients animals 58,59  (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 Further information and requests for resources and reagents should be directed to and will be 477 fulfilled by the lead contact, Dr. David Schoppik ( schoppik@gmail.com ).

Materials availability 479
This study did not generate new unique reagents. at densities under 20 larvae per 10 cm petri dish and were fed cultured rotifers (Reed Mari-494 culture) daily. Larvae that had their behavior measured at 14 dpf were raised as stated above 495 before being moved to 2 L tanks with 300 ml of cultured rotifers at 9 dpf. At 13 dpf, they were 496 transferred back to petri dishes with E3 medium for adaptation. 497 Larvae with different strains were achieved by crossing Schoppik lab strain with a mixed AB, TU, 498 and WIK background to three different wild-type strains: AB (Zebrafish International Resource 499 Center), mixed background of AB/WIK/TU, or SAT (Zebrafish International Resource Center). 500 Reference parameter values in Table 3 for 4, 7, 14 dpf fish were gathered using the AB strain 501 fish. 502 Drosophila melanogaster (w 1118 ) were raised at 23°on standard cornmeal-agar food under a 503 12/12 light/dark cycle.

504
. 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 Caenorhabditis elegans (C. elegans) were grown at 20°on nematode growth medium agar 505 plates seeded with Escherichia coli OP50 as previously described 129 . For Drosophila recording, four flies were transfered to a narrow chamber. A small piece of 520 water-dampened kimwipe was put at the bottom of the chamber to maintain humidity. A n 521 acrylic plug was secured at the top to prevent them from escaping the chamber. We secured 522 the chamber with the flies in the SAMPL apparatus and performed the standard SAMPL exper-523 iment using recording parameters provided in Table 2. 524 To image swimming C. elegans, eight starved N2 adult hermaphrodites were transferred to a 525 narrow chamber filled with 15 ml M9 buffer (3 g/l KH 2 PO 4 ; 6 g/l Na 2 HPO 4 ; 0.5 g/l NaCl; 1 g/l 526 NH 4 Cl) which was secured in the SAMPL apparatus as described above. Behavior recording 527 was started immediately afterwards. Refer to Table 2 for SAMPL thresholds for C. elegans de-528 tection.
529 Fin amputation 530 6 dpf zebrafish larvae were anesthetized in 0.02% tricaine methanesulfonate (Syndel) and 531 transferred to 3% Methylcellulose (Sigma). Fin amputation was done by removing pectoral 532 fins using fine forceps (FST). Specifically, one pair of forceps was used to stabilize the head of 533 the fish and a second pair was used to grab the joint and pull off the fins. Finless larvae were 534 washed three-times in E3 and fed with cultured rotifers before behavior assessment at 7 dpf. 535 . 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 Behavior data was analyzed using the Python analysis pipeline SAMPL_analysis_visualization.

543
SAMPL_analysis() function was used to calculate swim parameters, extract bouts and inter-bout 544 intervals (IBIs) from the raw data, and align swim bouts by the time of the peak speed. 545 Each run of the experiment (recording from "start" to "stop") generates one data file ( * .dlm) 546 containing recorded raw parameters including time stamp, fish body coordinates, fish head co- To extract bouts from the raw data, first, swim features, such as speed, distance, trajectory, an-551 gular velocity, etc., were calculated using basic parameters and time interval. Next, epochs that 552 were longer than 2.5 s, contain maximum swim speed greater than 5 mm/s, and pass various 553 quality-control filters were selected for bout extraction. Epochs containing multiple bouts were 554 segmented and truncated so that each detected bout contains data from 500 ms before to 555 300 ms after the time of the peak speed. Then, bouts containing 800 ms of swim data were 556 aligned by the time of the peak speed and saved for further analysis. 557 All further quantification was performed on data during zeitgeber day, namely the 14 h light 558 time for fish raising under 14/10 h light/dark cycle. 559 To calculate IBIs, epochs with multiple bouts are selected and the duration of swim speed be-560 low the 5 mm/s threshold between two consecutive bouts is calculated. A 100 ms buffer win-561 dow is then deducted from each end of the duration to account for errors of swim detection 562 ( Figure 3A). Pitch angles during each IBI were averaged to generate an IBI pitch ( Figure 3B). 563 Definition of other bout parameters can be found in Table 3. All bout parameters (except for ki-564 netic parameters explained in the next section) reported in the main text and Table 3 are mean   565 values across swim bouts collected from multiple experimental repeats. One experimental re-566 . 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 To calculate larvae sensitivity to pitch changes (Figure 3), we plotted bout frequency as a func-570 tion of IBI pitch. The data was modeled using a quadratic polynomial regression (least squares) 571 defined by function: where E k and E h are the mean of k and h with V k and V h being their respective variance. Next, 619 the standard errors of the fin-body ratio were calculated and used to estimate CI widths at 0.95 620 significance level. The primary differences that we've encountered are whether a particular model implements 665 binning or other on-camera computations, heat management, and different manufacturer- 666 provided APIs. When we use multiple cameras from the same manufacturer on the same 667 computer, we have also noticed that certain cameras will throw timeout errors on some USB 668 ports but not others; shuffling cameras and ports has worked to solve this problem. At the 669 time of writing, supply chain issues mean that most major camera companies quote long lead 670 times, but cameras ordered directly through alibaba.com all shipped within two weeks.

671
Illumination 672 Image quality is proportional to available light. Further, the size of the illuminated area defines 673 the size of the field that can be imaged. Finally it is imperative for our experiments that from 674 the fish's perspective that the "dark" period is completely dark. We therefore chose 940nm 675 LEDs as our source of infrared illumination. This left us with three options to build our illumina-676 tion source: LEDs mounted on adhesive strips, "star" style LEDs with 1-4 dies on a single PCB, 677 and a high-power LED array. The LED strips had too little illuminance for our purposes due pri-678 marily to the spacing of the LEDs. The high-power LED array had ample illuminance but gen-679 erated so much heat that it required active cooling. 680 We developed a simple illumination module to provide diffuse IR light across a 50mm circle 681 An LED mounted on a "star" PCB (Opulent LST-01F09-IR04-00, Mouser) provided ample light. 682 We mount each "star" LED with thermal adhesive to a small heat sink (Ohmite SV-LED-325E) 683 which in turn is glued to a Thorlabs adapter (SM1A6FW) to allows the wires to exit and the 684 LED/heatsink to connect to collimation and diffusion optics. The heat sink is machined (either 685 with a Dremel hand-held tool or a mill) on one side to allow the wires that power the LED to lie 686 flat against the heatsink. We power multiple illumination modules in series using a constant 687 current LED driver (LuxDrive BuckBlock 1000mA). Our illumination setup generates negligible 688 heat and our modules run continuously for years. 689 Our imaging parameters are fixed across experiments and optimized to give the highest qual-690 ity data we can achieve with our hardware. The gain of the camera is set either to its lowest 691 value or just above to minimize noise. Our exposure time is either 750 µsec or 1msec, allowing 692 for a crisp image in the face of the fastest movements that fish can make. The illuminated area 693 is circular, but the image sensor size is rectangular. We therefore crop the sides of the image to 694 produce a square that fits within the illuminated area.

695
. 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 choice of lens was guided by the need to balance different demands: 697 1. The longer the working distance, the greater the space needed between the sample and 698 the lens. We wanted our apparatus to fit length-wise on a 24 inch shelf, and so we needed 699 to minimize the working distance. 700 2. The entire depth of the tank needs to be in focus, but not beyond that because we'd like to 701 blur our LED. (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 top is a steel tray fabricated to order by MetalsCut4U (Avon Lake OH). Our current enclosure 728 took roughly three months to prototype before settling on the final design. 729 Shelving and fleet organization 730 We have organized our fleet of apparatus to sit on mobile wire shelving. Currently, we use 731 36"x24"x81.5" adjustable wire shelving units (McMaster Carr, Robbinsville NJ). We prefer to 732 have the shelving on casters as it makes accessing the back of the units considerably easier. 733 Shelving is organized such that one computer and three apparatus sit on a single shelf. En-734 closures on a given shelf are color-coded (blue, gold, and red) so that each apparatus can be 735 uniquely identified by a color/shelf/module combination; this also facilitates wire labeling. Each 736 shelf has its own power strip that controls the computer, the IR lights, and the white LEDs; all 737 strips plug into a single uninterruptible power supply (APC SmartUPS 1000C). 738 Our aim in specifying module size was to ensure that multiple investigators could set up ex-739 periments simultaneously, and to minimize the cost One unit has four shelves so that a sin-  What we don't measure 758 To extract the maximum amount of useful information about posture and locomotion with 759 the minimum amount of overhead we had to be selective about what we measure. Our imag-760 ing field is located in the center of the arena; fish that swim at the bottom, top, or sides of the 761 tank where there is a boundary are excluded from tracking. While multiple fish swim in the 762 same arena, we do not take data when more than one fish is in the imaging field to sidestep 763 the need to track fish identity. Our arena is sized to allow fish to swim freely but its shape (a 764 rectangular solid) encourages fish to swim in line with the imaging plane; we exclude frames 765 where fish turn away from the field of view (i.e. are swimming towards/away from the camera). 766 Finally, capturing the full range of rapid propulsive undulations of the fish tail requires a frame 767 rate of 500Hz-1kHz 130,131 . As changes to posture and locomotion are much slower, we opted 768 to record at 160Hz. Together, these choices allowed us to optimize our algorithms to achieve 769 the speed necessary to process video in real-time. image. 775 2. Threshold the difference image such that all small differences are set to zero. 776 3. Dilate the image three times in succession to remove any larger clumps that are still smaller 777 than a fish. 778 4. Extract and quantify all particles in the image. 779 Real-time video processing allows efficient data extraction during video acquisition. Our design 780 of the architecture is further discussed in the section: Optimizations for speed. 781 Below we detail a number of additional processing and optimization steps to ensure that we 782 maximize useful data. 783 Measuring the pitch of the fish 784 To extract the pitch (the angle of the fish with respect to the horizon), we perform the following 785 steps to ensure that the sign and magnitude of the angle is correctly assigned: 786 1. Fit the particle with an ellipse and extract the angle of the long axis with respect to the hori-787 zon.

788
. 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   These steps ensure that the data saved follows a simple and intuitive convention for posture. 795 Optimizations for speed 796 To optimize our code for speed, we use a set of thresholds to rapidly evaluate and reject frames 797 1. Before any processing, we sum the pixel values in the frame. If it is too low (no fish in frame) 798 or too high (more than one fish in the frame) we reject the frame. 799 2. After the particles are identified we reject the frame if a particle is touching the edge (fish 800 partially out of frame), if there is more than one particle (multiple fish) or if the length of the 801 particle is too short (fish bending in/out of the field of view). We define an epoch as a set of 802 continuous frames that pass all our exclusion criteria (i.e. that contain a single fish in frame). 803 Epoch duration is tracked and, when too short, can be rejected. 804 In addition to optimizing the algorithm, we adopted a producer-consumer architecture to de-805 couple video acquisition from video processing and saving data. Our software runs two rou-806 tines: the "producer," which acquires frames from the camera and places them in a queue 807 in memory, and the "consumer" that extracts each frame from the queue and processes it in 808 turn. Our program monitors the size of the consumer buffer and, if it has less that 10% free, 809 pauses the producer routine for 15 seconds to allow the buffer to clear. In this configuration, 810 the performance ceiling shifts from CPU speed (i.e. how quickly can a frame be processed) to 811 the amount of RAM available (i.e. how many frames can be queued). At the time of writing this, 812 doubling the amount of RAM is considerably less expensive than doubling CPU performance. 813 The choice of architecture thus brings down the cost of the computer. 814 Saving raw video 815 While the bulk of our experiments rely on real-time processing of video it is often useful to save 816 the actual data. Further, we wanted to be able to set user-defined criteria to determine in real-817 time which videos were worth saving. Leveraging the producer-consumer architecture, our 818 software contains a routine that independently buffers the frames being analyzed and, if, the 819 video to be saved meets user-defined criteria, will pass the frames to an independent program 820 . 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 algorithm relies on common and mature image processing routines and could be instan-828 tiated in any modern programming language. Since we had run this algorithm for the better 829 part of a decade we were confident that it was sufficiently stable to compile into a distributed  User interface 838 We designed the interface to enable easy initialization of experiments, rapid graphical and 839 quantitative visualization of video processing and performance, and to minimize error. Launch-840 ing the executable starts the program, which allows the user to fill out various text, numeric, 841 and drop-down fields that describe the experiment. The user then monitors the video feed un-842 til no fish are in frame and then selects that image as the background. We have found that this 843 initial bit of monitoring both compensates for slight day-to-day differences in arena placement. 844 More importantly, it forces the user to monitor the live feed at the beginning of each experi-845 ment, a useful bit of mindfulness that minimizes lost data. Once running, the user can: mon-846 itor the output of each step in the processing algorithm graphically, monitor the number of 847 times the consumer buffer has overflowed (usually zero), update the text fields, and stop the 848 program. Hardware parameters are stored in a text file that can be easily edited directly. Experi-849 ment parameters are similarly saved to text files and can be reloaded to save time. 850 We have implemented a number of user interface items to minimize confusion in the face of a 851 fleet of instruments. First, we have color-coded versions of the executable (blue, gold, and red) 852 . 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 where the background clearly differentiates the version. Each version has its own configura-853 tion file that, during setup, is coded to a particular apparatus. Thus the user is always aware of 854 which apparatus they are interfacing with based on color cues. Next, we added a "debug" but-855 ton to the front panel that allows for direct monitoring and editing of all program variables. In 856 "debug" mode the user has the option to save raw video.

858
In this Appendix, we walk through box assembly and recording settings. Refer to Appendix 5 859 for executing experiments and SAMPL data analysis.    Solder 2x 9" wires onto the IR LED "star." Attach IR LEDs to heatsinks using HexaTherm tape. 901 Note that in order to pass the wires through the heatsinks and the SM1A6FW adapter on the 902 opposite end, the ears of the Ohmite heatsink need to be trimmed down a little. When done, 903 attach the heatsink to the adapter using thermal epoxy. To simplify light wiring, we use one 904 1000 mA BuckBlock to dirve 3 IR lights in series for 3 boxes on the same level of the shelf. To 905 do this, one needs 2x 7" wires to connect adjacent IR cables and 1x 22" wire connecting the 906 further IR to the BuckBlock. Use another 8" wire to connect the closest IR to the BuckBlock. 907 We recommend using XT60H connectos to link these wires to the IR light cables and connect 908 wires to the BuckBlock for the ease of troubleshooting and replacement. Finally, connect the 909 BuckBlock to 12 V 2 A power supply through pigtail adaptors.  Network setup 964 We use a Synology data server as a repository to store behavior data. Hard drives are setup 965 as RAID 10. Each SAMPL rack has its own ethernet switch, which can be connected to other 966 switches as necessary.

968
In this appendix, we discuss algorithms for the data analysis and plotting software. We assume 969 that the user is working with data from larval zebrafish here. If not, the specific parameters 970 identified here are unlikely to translate as other organisms move differently but can nonethe-971 less be used as a starting point. Refer to Appendix 5 for instruction for use. Refer to the Key Re-972 sources

996
All the processes above can be found in the script src/SAMPL_analysis/preprocessing/analyze_dlm_v4.py. 998 Epochs that pass the quality control are used to extract swim bouts using function 999 grab_fish_angle() under src/SAMPL_analysis/bout_ 1000 analysis/grab_fish_angle_v4.py. 1001 We use a swim speed threshold of 5 mm/s to determine swim windows. Adjacent swim win-1002 dows with intervals smaller than 100 ms are combined. Next, we find the time of the peak 1003 speed for each swim window and extract frames in a range of 500 ms before to 300 ms af-1004 ter that. Inter-bout intervals (IBI) are determined as time between adjacent swim bouts with 1005 a 100 ms buffer window deducted from both the beginning and the end and IBI data is ex-1006 tracted accordingly. Baseline is considered the time during which larvae swim slower than 2 1007 mm/s and baseline parameters are extracted accordingly.

1008
. 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 To calculate fin-body coordination, users need to determine how the rotation is calculated. 1057 One way is to use rotation to time of peak angular velocity which requires estimation of 1058 time of peak angular velocity. To do this, we first calculate angular velocity using smoothed 1059 pitch angles and adjust the signs so that values are positive before time of the peak speed. 1060 Median of angular velocity time series from the same experimental repeat (see Appendix 5 1061 for data organization) is used to find time of peak angular velocity. Lastly, we average results 1062 across experimental repeats to determine the peak angular velocity time. However, this calcu-1063 lation requires a large amount of bout data. Alternatively, one may use a fixed value for time of 1064 peak angular velocity. Generally, we found -50 ms (50 ms before time of peak speed) to be a 1065 good value to use. Once the time of peak angular velocity is determined, rotation is calculated 1066 by pitch change from 250 ms before peak speed to time of peak angular velocity. Some scripts 1067 have the option to sample data from each experimental repeats. See Appendix 5 for instruc-1068 tion.

1069
. 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 doi: bioRxiv preprint ber of bouts, which will specify the number of boxes and larvae per box required. Outlier boxes 1130 with too few or too many bouts (e.g. more/less than 2SD) can then be excluded from further 1131 analysis according to pre-determined criteria. Finally, we recommend running the full "exper-1132 iment" multiple times to ensure that the findings are reproducible, and to report the variance 1133 across estimated parameters. Certain circumstances may be ill-suited to this approach: for ex-1134 ample, if particular genotypes of larvae are especially rare, such as in the case of doubly biallelic 1135 mutants, or genotypes that simply swim drastically less. In such cases one can combine swim 1136 bouts across experimental repeats, and report the estimated error in pararmeter estimates us-1137 ing statistical resampling techniques such as the jackknife. (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 rectory of the root folder) and the frame rate as instructed. This function aligns bouts in .dlm 1200 files within a directory so that peak speed is at time 0 ms, with 500 ms of activity before and 1201 300ms of activity after. It is important to note that all files in the same subfolders under the in-1202 put directory will be combined to extract bout parameters. The analysis script will take the sub-1203 mitted directory and analyze all data files within it, including all subfolders in its search, regard-1204 less of depth. Subfolders can be used to separate analyses, experimental conditions, or repeats. 1205 Data with different frame rates should be analyzed separately to ensure proper parameter cal-1206 culation, as only one can be used at a time. 1207 The program will skip the current .dlm file if it fails to detect a bout in it. However, errors are ex-1208 pected if files contain too little recorded data to extract a bout. Therefore, we suggest removing 1209 . 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 If the data size from a single repeat is not adequate for parameter calculation, we suggest com-1219 bining data from multiple repeats and use sampling techniques such as Jackknifing for error 1220 estimation.

1221
. 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 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  . 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 doi: bioRxiv preprint (A) An inter-bout interval (IBI, brown area) is defined as the duration when swim speed is below the 5 mm/s homeostasis threshold (dashed line) between two consecutive bouts with a 100 ms buffer window (grey area) deducted from each end. (B) Distribution of IBI duration (left) and mean pitch angle during IBI (right). (C) Bivariate histogram of bout frequency and IBI pitch. Bout frequency is the reciprocal of IBI duration. (D) Bout frequency plotted as a function of IBI pitch and modeled with a parabola (black line, R 2 = 0.14). Brown dots indicate binned average of IBI pitch and bout frequencies calculated by sorting IBI pitch into 3°-wide bins. For all panels, n = 109593 IBIs from 537 fish over 27 repeats.
. 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 doi: bioRxiv preprint (C) Illustration of components that contribute to trajectory deviation. Larvae rotate their bodies starting from bout initial (blue) and reach peak angular velocity (asterisk) before peak speed. Any rotation generated during decrease of angular velocity is considered residual (grey). At time of peak speed, there is an offset between the pitch angle (dashed line) and bout trajectory (arrow) which is termed attack angle (orange). Body rotations, residual, and attack angle add up to trajectory deviation. (D) Distribution of attack angles in control fish (left) and fish after fin amputation (right). Dashed lines indicate 0 attack angle. (E) Attack angles plotted as a function of body rotations (left, blue) or residual rotations (right). Rotations and residuals are sorted into 0.5°-wide bins for calculation of binned average attack angles. Swim bouts with negative attack angles while having steering rotations greater the 50th percentile (hollow squares) were excluded for binned-average calculation. (F) Attack angles plotted as a function of body rotations (blue line) and fitted with a logistic model (black line, R 2 = 0.31). Fin-body ratio is determined by the slope of the maximal slope of the fitted sigmoid (magenta). Rotations are sorted into 0.8°-wide bins for calculation of binned average rotations and attack angles (blue line). Swim bouts with negative attack angles while having steering rotations greater the 50th percentile were excluded for sigmoid modeling. (G) Schematic illustration of how fin-body ratio reflect climbing mechanics. For all panels, n = 121979 bouts from 537 fish over 27 repeats.
. 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 doi: bioRxiv preprint Figure S1: Custom breadboard for SAMPL base (A) Custom aluminum breadboard, not anodized, 0.5" thick. All holes (8 total) counterbored for 1/4"-20 cap screw. Grooves to be cut on the side of the breadboard OPPOSITE to the counterbore.
. 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 March 27, 2023. ; https://doi.org/10.1101/2023.01.07.523102 doi: bioRxiv preprint