|
| 1 | +# Analysis recipes |
| 2 | + |
| 3 | +**Applies to:** Track2Data v0.1.0 and later. Column names are as exported; check |
| 4 | +`codebook.csv` for units. |
| 5 | + |
| 6 | +Four worked designs, from a finished export to a fitted model. Each one names the metrics to |
| 7 | +tick on the **Metrics** screen, the columns they produce, the model the design calls for, and |
| 8 | +the mistake that specific design invites. They are starting points for common assays, not |
| 9 | +recommendations for your experiment. |
| 10 | + |
| 11 | +Runnable versions of the loading and modelling code ship with the repo: |
| 12 | +[`examples/analysis/analyse.R`](https://github.com/mbel-tech/Track2Data/blob/main/examples/analysis/analyse.R) |
| 13 | +and |
| 14 | +[`examples/analysis/analyse.py`](https://github.com/mbel-tech/Track2Data/blob/main/examples/analysis/analyse.py). |
| 15 | +The statistical reasoning behind the random-effects structure used throughout is in |
| 16 | +[[Statistics and Pseudoreplication]]. |
| 17 | + |
| 18 | +## Loading the export (all recipes start here) |
| 19 | + |
| 20 | +`metrics_long.csv` is one row per value: `session_id`, `individual_id`, `zone_name`, |
| 21 | +`metric_id`, `column`, `value`, `unit`. Reshape the rows you need and nothing else. |
| 22 | + |
| 23 | +```r |
| 24 | +library(dplyr); library(tidyr); library(readr) |
| 25 | + |
| 26 | +run <- "out" |
| 27 | +long <- list.files(run, "^metrics_long\\.csv$", recursive = TRUE, full.names = TRUE) |> |
| 28 | + lapply(read_csv, show_col_types = FALSE) |> bind_rows() |
| 29 | +sessions <- read_csv(file.path(run, "sessions.csv"), show_col_types = FALSE) |
| 30 | +codebook <- read_csv(file.path(run, "codebook.csv"), show_col_types = FALSE) |
| 31 | + |
| 32 | +# Per-individual metrics, excluding zone rows and the provenance diagnostic. |
| 33 | +individual <- long |> |
| 34 | + filter(is.na(zone_name), !is.na(individual_id), metric_id != "D-11") |> |
| 35 | + select(session_id, individual_id, column, value) |> |
| 36 | + pivot_wider(names_from = column, values_from = value) |
| 37 | + |
| 38 | +# Coverage, to weight or exclude by. |
| 39 | +quality <- long |> filter(metric_id == "D-11") |> |
| 40 | + select(session_id, individual_id, column, value) |> |
| 41 | + pivot_wider(names_from = column, values_from = value) |
| 42 | +``` |
| 43 | + |
| 44 | +Join your treatment labels from the metadata you supplied on the **Metadata** screen (they |
| 45 | +travel through to every table), or from `sessions.csv` if the treatment is per session. |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## Recipe 1 — Open-field thigmotaxis |
| 50 | + |
| 51 | +**Question.** Do treated animals spend more time near the wall? |
| 52 | + |
| 53 | +**Metrics to select.** Z-1 (time in each zone), Z-8 (Jacobs' D), IL-14 (wall-distance |
| 54 | +thigmotaxis), IL-3 (distance from arena centre). Draw `wall` and `centre` zones on the |
| 55 | +**Zones** screen, or use IL-3/IL-14, which need no zones. |
| 56 | + |
| 57 | +**Columns.** `time_pct` and `time_s` per `zone_name` (Z-1); `jacobs_d` (Z-8); |
| 58 | +`wall_contact_time_pct`, `mean_wall_distance_px` (IL-14); `time_in_centre_pct`, |
| 59 | +`mean_centre_distance` (IL-3). |
| 60 | + |
| 61 | +```r |
| 62 | +wall <- long |> |
| 63 | + filter(metric_id == "Z-1", zone_name == "wall", column == "time_pct") |> |
| 64 | + select(session_id, individual_id, wall_time = value) |> |
| 65 | + inner_join(filter(quality, frac_frames_used >= 0.80) |> |
| 66 | + select(session_id, individual_id), by = c("session_id", "individual_id")) |> |
| 67 | + left_join(treatments, by = "session_id") |
| 68 | + |
| 69 | +# A fraction in [0, 1]: beta, not Gaussian. |
| 70 | +glmmTMB::glmmTMB(wall_time ~ treatment + (1 | session_id), |
| 71 | + family = glmmTMB::beta_family(), data = wall) |
| 72 | +``` |
| 73 | + |
| 74 | +**The trap.** `time_pct` is a fraction, and zone occupancy sums to 1 across zones — so wall |
| 75 | +and centre time are not two independent outcomes, they are one. Pick the zone you |
| 76 | +pre-registered, or use `jacobs_d`, which is already corrected for how much of the arena each |
| 77 | +zone occupies. Zone fractions also depend on where you drew the boundary: keep the zone |
| 78 | +definition identical across sessions, and check the resolution warning if you reused zones |
| 79 | +drawn on a different image size. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Recipe 2 — Shoaling cohesion across a treatment |
| 84 | + |
| 85 | +**Question.** Does the treatment change how tightly the group swims together? |
| 86 | + |
| 87 | +**Metrics to select.** GL-1 (NND), GL-2 (IID), GL-3 (polarisation), GL-6 (cohesion index); |
| 88 | +optionally GL-4 (hull area), GL-10 (group spread), GL-11 (order state). |
| 89 | + |
| 90 | +**Columns.** `mean_nnd_px` / `_cm` / `_bl`, `mean_iid_*`, `mean_polarisation`, |
| 91 | +`cohesion_index`, `mean_hull_area_px2`, `mean_group_spread_*`, `polarised_time_pct`, |
| 92 | +`milling_time_pct`, `swarm_time_pct`. |
| 93 | + |
| 94 | +```r |
| 95 | +group <- long |> |
| 96 | + filter(metric_id %in% c("GL-1", "GL-3", "GL-6"), |
| 97 | + column %in% c("mean_nnd_bl", "mean_polarisation", "cohesion_index")) |> |
| 98 | + select(session_id, column, value) |> |
| 99 | + pivot_wider(names_from = column, values_from = value) |> |
| 100 | + left_join(select(sessions, session_id, n_animals, fps, duration_s), by = "session_id") |> |
| 101 | + left_join(treatments, by = "session_id") |
| 102 | + |
| 103 | +stopifnot(dplyr::n_distinct(group$n_animals) == 1) # see below |
| 104 | +lm(mean_nnd_bl ~ treatment, data = group) # n = sessions, not animals |
| 105 | +``` |
| 106 | + |
| 107 | +**The trap.** These are **one value per session**. A tank of eight fish is one observation, |
| 108 | +so the design needs replicate tanks; with four tanks per treatment you have *n* = 4. Group |
| 109 | +size is the second trap: NND falls as animals are added, for geometric reasons alone, so an |
| 110 | +unmatched group size will look like a treatment effect. Match group size, or model |
| 111 | +`n_animals` explicitly and expect to defend it. Prefer `mean_nnd_bl` to `mean_nnd_cm` when |
| 112 | +body size differs between groups — distance in body lengths is the comparable quantity. |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## Recipe 3 — Activity and freezing |
| 117 | + |
| 118 | +**Question.** Does the treatment change how much animals move, and how their freezing is |
| 119 | +organised in time? |
| 120 | + |
| 121 | +**Metrics to select.** IL-4 (activity / freezing fraction), IL-7 (freezing-bout statistics), |
| 122 | +IL-2 (speed), IL-1 (path length). |
| 123 | + |
| 124 | +**Columns.** `active_fraction`, `freezing_fraction`, `threshold_px_s` (IL-4); |
| 125 | +`freezing_bout_count`, `mean_freezing_duration_s`, `total_freezing_duration_s`, |
| 126 | +`min_bout_frames_used`, `bout_criterion_effective` (IL-7); `mean_speed_cm_s`, |
| 127 | +`median_speed_cm_s`, `max_speed_cm_s` (IL-2); `path_length_cm` (IL-1). |
| 128 | + |
| 129 | +```r |
| 130 | +act <- individual |> |
| 131 | + left_join(select(sessions, session_id, duration_s), by = "session_id") |> |
| 132 | + left_join(treatments, by = "session_id") |
| 133 | + |
| 134 | +# A rate: model it directly. |
| 135 | +lme4::lmer(mean_speed_cm_s ~ treatment + (1 | session_id), data = act) |
| 136 | + |
| 137 | +# A count: exposure offset, not a ratio. |
| 138 | +glmmTMB::glmmTMB(freezing_bout_count ~ treatment + offset(log(duration_s)) + (1 | session_id), |
| 139 | + family = glmmTMB::nbinom2, data = act) |
| 140 | +``` |
| 141 | + |
| 142 | +**The trap.** "Freezing" is a threshold decision, not an observation: IL-4 reports the speed |
| 143 | +threshold it used (`threshold_px_s`), and IL-7 reports the minimum bout length actually |
| 144 | +applied (`min_bout_frames_used`, `bout_criterion_effective`). Both must be reported, and both |
| 145 | +must be identical across the groups you compare. Smoothing matters more here than for any |
| 146 | +other family — a wider window suppresses the small displacements that decide whether a frame |
| 147 | +counts as freezing — so check `track2data sensitivity` before publishing a bout result. |
| 148 | +`path_length_cm` is a total: use speed, or an offset. |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## Recipe 4 — Habituation over time within a session |
| 153 | + |
| 154 | +**Question.** Does activity decline across the session, and does the treatment change the |
| 155 | +slope? |
| 156 | + |
| 157 | +**Setup.** Set **time bins** on the Metrics screen (for example 60 s). Summary tables then |
| 158 | +carry one row per animal per bin, with `bin_index`, `bin_start_s`, `bin_end_s`. |
| 159 | + |
| 160 | +**Metrics to select.** IL-2, IL-1, IL-4, plus Z-1 if the question is zone use over time. |
| 161 | + |
| 162 | +```r |
| 163 | +bins <- read_csv(file.path(run, "tiny_session", "trial_activity_summary.csv"), |
| 164 | + show_col_types = FALSE) |> |
| 165 | + filter(!is.na(bin_index)) |> |
| 166 | + left_join(treatments, by = "session_id") |
| 167 | + |
| 168 | +lme4::lmer(mean_speed_cm_s ~ treatment * bin_index + (1 | session_id/individual_id), |
| 169 | + data = bins) |
| 170 | +``` |
| 171 | + |
| 172 | +**The trap.** Bins are repeated measures within a session, not extra sessions — the random |
| 173 | +structure has to keep both levels, and bins are autocorrelated, so a residual correlation |
| 174 | +structure (`glmmTMB` with `ar1(...)`, or `nlme::lme(correlation = corAR1())`) is often the |
| 175 | +honest model. Metrics that are not meaningful per window keep a single row with empty bin |
| 176 | +columns; filter on `!is.na(bin_index)` rather than assuming every row is binned. Short bins |
| 177 | +make every per-bin ratio noisy: check `n_frames_used` per bin before reading a trend. |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## Before you report any of these |
| 182 | + |
| 183 | +Confirm the sessions were poolable (`fps`, `n_animals`, segmentation settings — see |
| 184 | +`PROJECT_SUMMARY.md`), state your coverage threshold and the preprocessing settings from |
| 185 | +`manifest.json`, and record the `app_version` that produced the numbers. The full reporting |
| 186 | +checklist is at the end of [[Statistics and Pseudoreplication]]; what can still change before |
| 187 | +1.0 is in [[Known Issues]]. |
0 commit comments