-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport_boxplot_significance.py
More file actions
545 lines (512 loc) · 24.3 KB
/
report_boxplot_significance.py
File metadata and controls
545 lines (512 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/usr/bin/env python3
"""
Collect data used in all boxplot analyses, run statistical tests (Kruskal-Wallis for 3 groups,
Mann-Whitney U for 2 groups; post-hoc with Bonferroni), and write SIGNIFICANCE_REPORT.md to
trajectory_analysis/<animal>/.
"""
from pathlib import Path
import argparse
import json
import sys
import numpy as np
import pandas as pd
from scipy import stats
_script_dir = Path(__file__).resolve().parent
if str(_script_dir) not in sys.path:
sys.path.insert(0, str(_script_dir))
import analyze_trajectories as at
from plot_head_direction import (
get_animal_trials,
_split_phase_trials,
_get_reward_frame_by_trial,
_resolve_calib_path,
load_calib,
load_head_direction_per_trial,
load_body_direction_per_trial,
_load_trial_start_to_reward_with_head,
_load_trial_start_to_reward_with_body,
_load_trial_start_to_reward_with_head_and_body,
_parse_trial_frames,
_crossing_events_with_frame,
_first_goal_entries,
_build_body_vs_head_dataframe,
_point_goal_region_local,
)
def _bout_region_name(u: float, v: float, params: dict) -> str:
lab = _point_goal_region_local(u, v, params)
if lab == 1:
return "Goal 1"
if lab == 2:
return "Goal 2"
v_mid = params.get("v_mid")
if v_mid is not None and v < v_mid:
return "Above midline"
return "Below midline"
PHASE_NAMES = ["Early", "Mid", "Late"]
ALPHA = 0.05
def _kruskal_posthoc(groups: list[np.ndarray], names: list[str]) -> list[tuple[str, str, float, str]]:
"""Pairwise Mann-Whitney U with Bonferroni. groups = [early, mid, late]. Returns list of (g1, g2, p, sig)."""
n_pairs = 3 # Early-Mid, Early-Late, Mid-Late
bonferroni_alpha = ALPHA / n_pairs
results = []
for i in range(len(groups)):
for j in range(i + 1, len(groups)):
if len(groups[i]) < 2 or len(groups[j]) < 2:
results.append((names[i], names[j], float("nan"), "—"))
continue
stat, p = stats.mannwhitneyu(groups[i], groups[j], alternative="two-sided")
sig = "Yes" if p < bonferroni_alpha else "No"
results.append((names[i], names[j], float(p), sig))
return results
def run_tests_and_report(
trials: list,
early: list,
mid: list,
late: list,
reward_frame_by_trial: dict,
params: dict,
calib_root: Path | None,
camera: str,
out_dir: Path,
animal: str,
) -> None:
trial_to_phase = {}
for t in early:
trial_to_phase[t[1]] = 0
for t in mid:
trial_to_phase[t[1]] = 1
for t in late:
trial_to_phase[t[1]] = 2
v_mid = params.get("v_mid") if params else None
g1_u = params.get("goal1_u") if params else None
lines: list[str] = [
"# Significance report: boxplot analyses",
"",
f"**Animal:** {animal} ",
f"**Trials:** {len(trials)} (Early: {len(early)}, Mid: {len(mid)}, Late: {len(late)}) ",
"",
"**Tests:** Kruskal-Wallis for 3 groups (Early / Mid / Late); Mann-Whitney U for 2 groups. ",
"Post-hoc after significant Kruskal-Wallis: pairwise Mann-Whitney with Bonferroni (α = 0.05/3). ",
f"Significance: α = {ALPHA}. ",
"",
"---",
"",
]
summary_sig: list[str] = []
# ----- Head direction -----
crossing_head: dict[int, list[float]] = {0: [], 1: [], 2: []}
toward1_head: list[float] = []
toward2_head: list[float] = []
goal1_head: dict[int, list[float]] = {0: [], 1: [], 2: []}
goal2_head: dict[int, list[float]] = {0: [], 1: [], 2: []}
for csv_path, trial_id, _ in trials:
reward_frame = reward_frame_by_trial.get(trial_id)
if reward_frame is None:
continue
frames = _parse_trial_frames(trial_id)
if not frames:
continue
frame_start, _ = frames
calib_path = _resolve_calib_path(csv_path.parent, calib_root, camera)
if calib_path is None or not calib_path.exists():
continue
try:
calib = load_calib(calib_path)
except Exception:
continue
df = _load_trial_start_to_reward_with_head(csv_path, trial_id, reward_frame, csv_path.parent, calib, frame_start)
if df is None or len(df) < 2:
continue
phase_id = trial_to_phase.get(trial_id, 0)
for (frame, u, v, toward) in _crossing_events_with_frame(df, params):
row = df[df["frame_number"] == frame]
if len(row) > 0:
crossing_head[phase_id].append(float(row["head_angle_deg"].iloc[0]))
if toward == 1:
toward1_head.append(float(row["head_angle_deg"].iloc[0]))
else:
toward2_head.append(float(row["head_angle_deg"].iloc[0]))
f1, u1, v1, f2, u2, v2 = _first_goal_entries(df, params)
if f1 is not None:
r = df[df["frame_number"] == f1]
if len(r) > 0:
goal1_head[phase_id].append(float(r["head_angle_deg"].iloc[0]))
if f2 is not None:
r = df[df["frame_number"] == f2]
if len(r) > 0:
goal2_head[phase_id].append(float(r["head_angle_deg"].iloc[0]))
lines.append("## 1. Head direction")
lines.append("")
if any(crossing_head[i] for i in range(3)):
g = [crossing_head[i] for i in range(3)]
try:
stat, p = stats.kruskal(*g)
lines.append(f"### 1.1 Head at midline crossing (by phase)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant difference across phases:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
for a, b, pval, sig in _kruskal_posthoc(g, PHASE_NAMES):
lines.append(f" - {a} vs {b}: p = {pval:.4f} ({sig}) ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
if toward1_head and toward2_head:
try:
stat, p = stats.mannwhitneyu(toward1_head, toward2_head, alternative="two-sided")
lines.append(f"### 1.2 Head at midline crossing (toward goal 1 vs 2)")
lines.append(f"- Mann-Whitney U, p = {p:.4f} ")
lines.append(f"- **Significant difference:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **1.2** Head at midline: toward goal 1 vs 2 differ (p = {p:.4f}). ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
for goal_name, gdict in [("Goal 1", goal1_head), ("Goal 2", goal2_head)]:
g = [gdict[i] for i in range(3)]
if not any(g):
continue
try:
stat, p = stats.kruskal(*g)
lines.append(f"### 1.3 Head when entering {goal_name} (by phase)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
for a, b, pval, sig in _kruskal_posthoc(g, PHASE_NAMES):
lines.append(f" - {a} vs {b}: p = {pval:.4f} ({sig}) ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
# ----- Body direction -----
crossing_body: dict[int, list[float]] = {0: [], 1: [], 2: []}
toward1_body: list[float] = []
toward2_body: list[float] = []
goal1_body: dict[int, list[float]] = {0: [], 1: [], 2: []}
goal2_body: dict[int, list[float]] = {0: [], 1: [], 2: []}
for csv_path, trial_id, _ in trials:
reward_frame = reward_frame_by_trial.get(trial_id)
if reward_frame is None:
continue
frames = _parse_trial_frames(trial_id)
if not frames:
continue
frame_start, _ = frames
calib_path = _resolve_calib_path(csv_path.parent, calib_root, camera)
if calib_path is None or not calib_path.exists():
continue
try:
calib = load_calib(calib_path)
except Exception:
continue
df = _load_trial_start_to_reward_with_body(csv_path, trial_id, reward_frame, csv_path.parent, calib, frame_start)
if df is None or len(df) < 2:
continue
phase_id = trial_to_phase.get(trial_id, 0)
for (frame, u, v, toward) in _crossing_events_with_frame(df, params):
row = df[df["frame_number"] == frame]
if len(row) > 0:
crossing_body[phase_id].append(float(row["body_angle_deg"].iloc[0]))
if toward == 1:
toward1_body.append(float(row["body_angle_deg"].iloc[0]))
else:
toward2_body.append(float(row["body_angle_deg"].iloc[0]))
f1, u1, v1, f2, u2, v2 = _first_goal_entries(df, params)
if f1 is not None:
r = df[df["frame_number"] == f1]
if len(r) > 0:
goal1_body[phase_id].append(float(r["body_angle_deg"].iloc[0]))
if f2 is not None:
r = df[df["frame_number"] == f2]
if len(r) > 0:
goal2_body[phase_id].append(float(r["body_angle_deg"].iloc[0]))
lines.append("## 2. Body direction")
lines.append("")
if any(crossing_body[i] for i in range(3)):
g = [crossing_body[i] for i in range(3)]
try:
stat, p = stats.kruskal(*g)
lines.append(f"### 2.1 Body at midline crossing (by phase)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
for a, b, pval, sig in _kruskal_posthoc(g, PHASE_NAMES):
lines.append(f" - {a} vs {b}: p = {pval:.4f} ({sig}) ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
if toward1_body and toward2_body:
try:
stat, p = stats.mannwhitneyu(toward1_body, toward2_body, alternative="two-sided")
lines.append(f"### 2.2 Body at midline crossing (toward goal 1 vs 2)")
lines.append(f"- Mann-Whitney U, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **2.2** Body at midline: toward goal 1 vs 2 differ (p = {p:.4f}). ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
for goal_name, gdict in [("Goal 1", goal1_body), ("Goal 2", goal2_body)]:
g = [gdict[i] for i in range(3)]
if not any(g):
continue
try:
stat, p = stats.kruskal(*g)
lines.append(f"### 2.3 Body when entering {goal_name} (by phase)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **2.3** Body when entering Goal 2: differs by phase (p = {p:.4f}). ")
for a, b, pval, sig in _kruskal_posthoc(g, PHASE_NAMES):
lines.append(f" - {a} vs {b}: p = {pval:.4f} ({sig}) ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
# ----- Body vs head (need full df and bout counts) -----
df_bv = _build_body_vs_head_dataframe(trials, early, mid, late, reward_frame_by_trial, params, calib_root, camera, fps=180.0)
if len(df_bv) > 0:
lines.append("## 3. Body vs head")
lines.append("")
by_phase_abs = [df_bv[df_bv["phase_id"] == i]["abs_head_body_diff"].values for i in range(3)]
try:
stat, p = stats.kruskal(*by_phase_abs)
lines.append(f"### 3.1 |Head − body| by phase (decoupling)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **3.1** |Head − body| by phase: differs across Early/Mid/Late (p = {p:.4f}). ")
for a, b, pval, sig in _kruskal_posthoc(by_phase_abs, PHASE_NAMES):
lines.append(f" - {a} vs {b}: p = {pval:.4f} ({sig}) ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
regions_order = ["Above midline", "Below midline", "Goal 1", "Goal 2"]
by_region = [df_bv[df_bv["region"] == r]["abs_head_body_diff"].values for r in regions_order]
by_region = [x for x in by_region if len(x) >= 2]
if len(by_region) >= 2:
try:
stat, p = stats.kruskal(*by_region)
lines.append(f"### 3.2 |Head − body| by region")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **3.2** |Head − body| by region: differs across regions (p = {p:.4f}). ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
goal_entry_align: list[tuple[int, int, float]] = []
for csv_path, trial_id, _ in trials:
reward_frame = reward_frame_by_trial.get(trial_id)
if reward_frame is None:
continue
frames = _parse_trial_frames(trial_id)
if not frames:
continue
frame_start, _ = frames
calib_path = _resolve_calib_path(csv_path.parent, calib_root, camera)
if calib_path is None or not calib_path.exists():
continue
try:
calib = load_calib(calib_path)
except Exception:
continue
tdf = _load_trial_start_to_reward_with_head_and_body(csv_path, trial_id, reward_frame, csv_path.parent, calib, frame_start)
if tdf is None or len(tdf) < 2:
continue
phase_id = trial_to_phase.get(trial_id, 0)
f1, u1, v1, f2, u2, v2 = _first_goal_entries(tdf, params)
for goal_idx, f in enumerate([f1, f2], start=1):
if f is None:
continue
row = tdf[tdf["frame_number"] == f]
if len(row) == 0:
continue
head_deg = float(row["head_angle_deg"].iloc[0])
body_deg = float(row["body_angle_deg"].iloc[0])
diff = (head_deg - body_deg + 180) % 360 - 180
abs_align = min(abs(diff), 360 - abs(diff))
goal_entry_align.append((phase_id, goal_idx, abs_align))
if goal_entry_align:
align_df = pd.DataFrame(goal_entry_align, columns=["phase_id", "goal_idx", "abs_head_body_deg"])
for goal_idx in [1, 2]:
sub = align_df[align_df["goal_idx"] == goal_idx]
g = [sub[sub["phase_id"] == i]["abs_head_body_deg"].values for i in range(3)]
if not all(len(x) >= 2 for x in g):
continue
try:
stat, p = stats.kruskal(*g)
lines.append(f"### 3.3 Head–body alignment at first entry to goal {goal_idx} (by phase)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **3.3** Head–body alignment at goal {goal_idx} entry: differs by phase (p = {p:.4f}). ")
for a, b, pval, sig in _kruskal_posthoc(g, PHASE_NAMES):
lines.append(f" - {a} vs {b}: p = {pval:.4f} ({sig}) ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
# ----- Scanning bout count by phase and by region -----
body_still_deg, head_scanning_deg = 15.0, 25.0
regions_order = ["Above midline", "Below midline", "Goal 1", "Goal 2"]
bout_count_per_trial: list[tuple[str, int, int]] = []
bout_count_per_trial_region: list[dict] = []
for csv_path, trial_id, _ in trials:
reward_frame = reward_frame_by_trial.get(trial_id)
if reward_frame is None:
continue
frames = _parse_trial_frames(trial_id)
if not frames:
continue
frame_start, _ = frames
calib_path = _resolve_calib_path(csv_path.parent, calib_root, camera)
if calib_path is None or not calib_path.exists():
continue
try:
calib = load_calib(calib_path)
except Exception:
continue
tdf = _load_trial_start_to_reward_with_head_and_body(csv_path, trial_id, reward_frame, csv_path.parent, calib, frame_start)
if tdf is None or len(tdf) < 3:
continue
phase_id = trial_to_phase.get(trial_id, 0)
head_deg = tdf["head_angle_deg"].values.astype(float)
body_deg = tdf["body_angle_deg"].values.astype(float)
u_vals = tdf["u"].values.astype(float)
v_vals = tdf["v"].values.astype(float)
def wrap_deg(d: float) -> float:
d = (d + 180) % 360 - 180
return min(abs(d), 360 - abs(d))
is_scan = np.zeros(len(tdf), dtype=bool)
for i in range(1, len(tdf)):
if wrap_deg(body_deg[i] - body_deg[i - 1]) < body_still_deg and wrap_deg(head_deg[i] - head_deg[i - 1]) > head_scanning_deg:
is_scan[i] = True
i, count_bouts = 0, 0
region_counts: dict[str, int] = {r: 0 for r in regions_order}
while i < len(tdf):
if not is_scan[i]:
i += 1
continue
j = i
while j < len(tdf) and is_scan[j]:
j += 1
reg = _bout_region_name(float(u_vals[i]), float(v_vals[i]), params)
region_counts[reg] = region_counts.get(reg, 0) + 1
count_bouts += 1
i = j
bout_count_per_trial.append((trial_id, phase_id, count_bouts))
for r in regions_order:
bout_count_per_trial_region.append({"trial_id": trial_id, "phase_id": phase_id, "region": r, "bout_count": region_counts[r]})
if bout_count_per_trial:
bc_df = pd.DataFrame(bout_count_per_trial, columns=["trial_id", "phase_id", "bout_count"])
by_phase_bouts = [bc_df[bc_df["phase_id"] == i]["bout_count"].values for i in range(3)]
try:
stat, p = stats.kruskal(*by_phase_bouts)
lines.append("### 3.4 Scanning bout count per trial (by phase)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **3.4** Scanning bout count per trial: differs by phase (p = {p:.4f}). ")
for a, b, pval, sig in _kruskal_posthoc(by_phase_bouts, PHASE_NAMES):
lines.append(f" - {a} vs {b}: p = {pval:.4f} ({sig}) ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
if bout_count_per_trial_region:
bc_region_df = pd.DataFrame(bout_count_per_trial_region)
by_region_only = [bc_region_df[bc_region_df["region"] == r]["bout_count"].values for r in regions_order]
by_region_only = [x for x in by_region_only if len(x) >= 2]
if len(by_region_only) >= 2:
try:
stat, p = stats.kruskal(*by_region_only)
lines.append("### 3.5 Scanning bout count per trial by region only (all phases)")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **3.5** Scanning bout count by region only: differs across regions (p = {p:.4f}). ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
for phase_id in range(3):
sub = bc_region_df[bc_region_df["phase_id"] == phase_id]
by_region = [sub[sub["region"] == r]["bout_count"].values for r in regions_order]
by_region = [x for x in by_region if len(x) >= 2]
if len(by_region) < 2:
continue
try:
stat, p = stats.kruskal(*by_region)
lines.append(f"### 3.6 Scanning bout count by phase (comparing regions): {PHASE_NAMES[phase_id]}")
lines.append(f"- Kruskal-Wallis H = {stat:.4f}, p = {p:.4f} ")
lines.append(f"- **Significant:** {'Yes' if p < ALPHA else 'No'} ")
if p < ALPHA:
summary_sig.append(f"- **3.6** Scanning bout count in {PHASE_NAMES[phase_id]}: differs by region (p = {p:.4f}). ")
lines.append("")
except Exception as e:
lines.append(f"- Error: {e} ")
lines.append("")
# Insert summary after first "---"
idx = next(i for i, s in enumerate(lines) if s.strip() == "---")
summary_block = ["## Summary (significant findings)", ""] + (summary_sig if summary_sig else ["No significant differences at α = 0.05."]) + ["", "---", ""]
lines = lines[: idx + 1] + [""] + summary_block + lines[idx + 1 :]
lines.append("---")
lines.append("")
lines.append("*Report generated by report_boxplot_significance.py. Non-parametric tests; no correction for multiple comparisons across sections.*")
out_dir.mkdir(parents=True, exist_ok=True)
report_path = out_dir / "SIGNIFICANCE_REPORT.md"
report_path.write_text("\n".join(lines), encoding="utf-8")
print(f" Significance report -> {report_path}")
def main():
parser = argparse.ArgumentParser(description="Run significance tests on boxplot analyses and write SIGNIFICANCE_REPORT.md")
parser.add_argument("--animal", type=str, default="rory")
parser.add_argument("--predictions-root", type=Path, default=Path("/home/user/src/JARVIS-HybridNet/projects/mouseClimb4/predictions/predictions3D"))
parser.add_argument("-o", "--output-dir", type=Path, default=Path("trajectory_analysis"))
parser.add_argument("--calib-root", type=Path, default=None)
parser.add_argument("--camera", type=str, default="Cam2005325")
parser.add_argument("--midline-goals-json", type=Path, default=None)
parser.add_argument("--reward-times", type=Path, default=None)
parser.add_argument("--logs-dir", type=Path, default=None)
args = parser.parse_args()
animal = args.animal.strip()
predictions_root = Path(args.predictions_root).resolve()
out_root = Path(args.output_dir).resolve()
out_dir = out_root / animal
trials = get_animal_trials(predictions_root, animal)
if not trials:
print(f"No {animal} trials found.")
return
early, mid, late = _split_phase_trials(trials)
params = None
if args.midline_goals_json and args.midline_goals_json.exists():
with open(args.midline_goals_json) as f:
params = json.load(f)
if params is None and (out_root / animal / "midline_and_goals" / "midline_and_goals.json").exists():
with open(out_root / animal / "midline_and_goals" / "midline_and_goals.json") as f:
params = json.load(f)
if params is None:
print("No params (midline_and_goals); skipping significance report.")
return
calib_root = Path(args.calib_root).resolve() if args.calib_root else None
reward_times_path = args.reward_times or out_root / "reward_times.csv"
logs_dir = Path(args.logs_dir).resolve() if args.logs_dir else None
reward_frame_by_trial = _get_reward_frame_by_trial(trials, Path(reward_times_path).resolve(), logs_dir, animal)
if not reward_frame_by_trial:
print("No reward frames; skipping.")
return
run_tests_and_report(
trials, early, mid, late,
reward_frame_by_trial, params,
calib_root=calib_root,
camera=args.camera,
out_dir=out_dir,
animal=animal,
)
if __name__ == "__main__":
main()