-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_trajectories.py
More file actions
1651 lines (1543 loc) · 77.8 KB
/
analyze_trajectories.py
File metadata and controls
1651 lines (1543 loc) · 77.8 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Study filtered trajectory CSVs: compute basic stats and make illustrative plots.
Reads trajectory_filtered.csv from each trial folder (frame_number, x, y, z, u, v, segment_id),
computes per-trial statistics (path length, duration, speed, segments), and produces
summary CSV + plots for exploration.
Outputs include flow_field_low_to_high.png: two panels (elevation + flow direction;
flow speed + flow direction). See docs/FLOW_FIELD_PLOTS.md for how these are computed.
"""
from pathlib import Path
import argparse
import re
import sys
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.collections import LineCollection
def _kmeans2_xyz(pts: np.ndarray, max_iter: int = 50) -> np.ndarray:
"""2-means in 3D so the 2 centroids end up on opposite ends. pts shape (N, 3). Returns (2, 3)."""
pts = np.asarray(pts, dtype=float)
if len(pts) < 2:
return pts
mid = np.median(pts[:, 0])
left = pts[pts[:, 0] <= mid]
right = pts[pts[:, 0] > mid]
if len(left) == 0:
left = pts[:1]
if len(right) == 0:
right = pts[-1:]
c0 = left.mean(axis=0)
c1 = right.mean(axis=0)
for _ in range(max_iter):
d0 = np.linalg.norm(pts - c0, axis=1)
d1 = np.linalg.norm(pts - c1, axis=1)
assign = (d0 > d1).astype(int)
n0 = (assign == 0).sum()
n1 = (assign == 1).sum()
if n0 == 0 or n1 == 0:
break
new_c0 = pts[assign == 0].mean(axis=0)
new_c1 = pts[assign == 1].mean(axis=0)
if np.allclose(new_c0, c0) and np.allclose(new_c1, c1):
break
c0, c1 = new_c0, new_c1
return np.array([c0, c1])
_script_dir = Path(__file__).resolve().parent
sys.path.insert(0, str(_script_dir))
from plot_trajectory_on_frame import load_calib, project_3d_to_2d
# Trajectory elevation/region filters (see docs/TRAJECTORY_FILTERS.md)
MAX_PEAK_Z = 150.0 # drop points with z > 150
U_LOW_THRESHOLD = 1250.0 # px; in low-u region cap z
Z_CAP_WHEN_U_LOW = 50.0 # when u < U_LOW_THRESHOLD, drop points with z > this
# In all_trials_xy plot: exclude points where y > Y_FOR_Z_CAP and z > Z_CAP_IN_Y_REGION (physically inconsistent)
Y_FOR_Z_CAP = -100.0
Z_CAP_IN_Y_REGION = 120.0
# Video frame rate for time-in-seconds (path to peak, duration, etc.)
FPS = 180
# Session folder pattern (e.g. rory_2025_12_23_16_57_09) for nested JARVIS predictions root
_SESSION_DIR_PATTERN = re.compile(r"^[a-z]+_\d{4}_\d{2}_\d{2}_\d{2}_\d{2}_\d{2}$")
_TRIAL_DIR_PATTERN = re.compile(r"^Predictions_3D_trial_\d+_\d+-\d+$")
def find_trajectory_csvs(
predictions_dir: Path,
) -> list[tuple[Path, str, str | None]]:
"""Return list of (path_to_csv, trial_id, session_folder) for each trial with trajectory_filtered.csv.
session_folder is the session dir name (e.g. rory_2025_12_23_16_57_09) when using nested JARVIS layout,
or None when trials sit directly under predictions_dir.
Supports (1) a dir that directly contains Predictions_3D_trial_* folders, and
(2) a root that contains session subdirs (e.g. rory_2025_12_23_16_57_09) each
containing Predictions_3D_trial_* folders (JARVIS layout).
"""
predictions_dir = Path(predictions_dir)
out: list[tuple[Path, str, str | None]] = []
for d in sorted(predictions_dir.iterdir()):
if not d.is_dir():
continue
if _TRIAL_DIR_PATTERN.match(d.name):
csv_path = d / "trajectory_filtered.csv"
if csv_path.exists():
out.append((csv_path, d.name, None))
continue
if _SESSION_DIR_PATTERN.match(d.name):
for t in sorted(d.iterdir()):
if not t.is_dir() or not _TRIAL_DIR_PATTERN.match(t.name):
continue
csv_path = t / "trajectory_filtered.csv"
if csv_path.exists():
out.append((csv_path, t.name, d.name))
return out
def _session_to_animal_and_date(session_folder: str | None) -> tuple[str, str]:
"""Parse session_folder (e.g. rory_2025_12_23_16_57_09) -> (animal, session_date).
If session_folder is None, return ('unknown', '').
"""
if not session_folder or "_" not in session_folder:
return ("unknown", "")
parts = session_folder.split("_", 1)
return (parts[0].lower(), parts[1] if len(parts) > 1 else "")
def _assign_phase_per_trial(
trial_list: list[tuple[Path, str, str | None]],
) -> dict[tuple[str, str], str]:
"""
For each (trial_id, session_folder) determine phase: early, mid, or late.
Sessions are ordered chronologically per animal; phase is by tertile (first third=early, etc.).
Returns dict keyed by (trial_id, session_folder) -> phase. For flat layout (session_folder None),
key (trial_id, '') is used and phase is 'unknown'.
"""
# Collect unique (animal, session_folder) and sort sessions per animal
by_animal: dict[str, list[str]] = {}
for _csv_path, _trial_id, session_folder in trial_list:
if session_folder is None:
continue
animal, _ = _session_to_animal_and_date(session_folder)
if animal not in by_animal:
by_animal[animal] = []
if session_folder not in by_animal[animal]:
by_animal[animal].append(session_folder)
for animal in by_animal:
by_animal[animal] = sorted(by_animal[animal])
# session_folder -> phase per animal
phase_by_animal_session: dict[tuple[str, str], str] = {}
for animal, sessions in by_animal.items():
n = len(sessions)
for i, session_folder in enumerate(sessions):
if n <= 1:
phase = "single"
elif n == 2:
phase = "early" if i == 0 else "late"
else:
t = (i + 0.5) / n
if t <= 1 / 3:
phase = "early"
elif t <= 2 / 3:
phase = "mid"
else:
phase = "late"
phase_by_animal_session[(animal, session_folder)] = phase
out: dict[tuple[str, str], str] = {}
for _csv_path, trial_id, session_folder in trial_list:
if session_folder is None:
out[(trial_id, "")] = "unknown"
continue
animal, _ = _session_to_animal_and_date(session_folder)
phase = phase_by_animal_session.get((animal, session_folder), "unknown")
out[(trial_id, session_folder)] = phase
return out
def load_trajectory_csv(csv_path: Path) -> pd.DataFrame:
"""Load trajectory CSV; apply elevation and region filters (see docs/TRAJECTORY_FILTERS.md)."""
df = pd.read_csv(csv_path)
if "z" not in df.columns or len(df) == 0:
return df
keep = (df["z"] >= 0) & (df["z"] <= MAX_PEAK_Z)
if "u" in df.columns:
keep = keep & ((df["u"] >= U_LOW_THRESHOLD) | (df["z"] <= Z_CAP_WHEN_U_LOW))
df = df.loc[keep].copy()
return df
def _use_uv_for_path_plots(trial_list: list) -> tuple[str, str, bool, str, str]:
"""If trajectory_filtered CSVs have u,v columns, use them for 2D path plots (camera view).
Returns (col_a, col_b, invert_y, xlabel, ylabel). Otherwise use world (x, y)."""
if not trial_list:
return "x", "y", False, "x", "y"
sample = load_trajectory_csv(trial_list[0][0])
if sample is not None and len(sample) and "u" in sample.columns and "v" in sample.columns:
return "u", "v", True, "u (px)", "v (px)"
return "x", "y", False, "x", "y"
def path_length_3d(df: pd.DataFrame) -> float:
"""Total 3D path length (sum of consecutive Euclidean distances in x, y, z)."""
if len(df) < 2:
return 0.0
d = np.diff(df[["x", "y", "z"]].values.astype(float), axis=0)
return float(np.sqrt((d ** 2).sum(axis=1)).sum())
def _ordered_xy_path(df: pd.DataFrame) -> np.ndarray:
"""Return (N, 2) array of (x, y) along path: segments in order, points by frame_number."""
rows = []
for seg_id in sorted(df["segment_id"].unique()):
seg = df[df["segment_id"] == seg_id].sort_values("frame_number")
rows.append(seg[["x", "y"]].values.astype(float))
if not rows:
return np.zeros((0, 2))
return np.vstack(rows)
def _ordered_xyz_path(df: pd.DataFrame) -> np.ndarray:
"""Return (N, 3) array of (x, y, z) along path: segments in order, points by frame_number."""
xyzf = _ordered_xyz_frame_path(df)
return xyzf[:, :3] if len(xyzf) > 0 else np.zeros((0, 3))
def _ordered_xyz_frame_path(df: pd.DataFrame) -> np.ndarray:
"""Return (N, 4) array of (x, y, z, frame_number) along path: segments in order, points by frame_number."""
rows = []
for seg_id in sorted(df["segment_id"].unique()):
seg = df[df["segment_id"] == seg_id].sort_values("frame_number")
rows.append(seg[["x", "y", "z", "frame_number"]].values.astype(float))
if not rows:
return np.zeros((0, 4))
return np.vstack(rows)
def _resample_path_waypoints(xy: np.ndarray, n_waypoints: int) -> np.ndarray:
"""Resample path to n_waypoints (x,y) by cumulative path length. xy shape (N, 2)."""
if len(xy) < 2 or n_waypoints <= 0:
return np.zeros((n_waypoints, 2))
d = np.zeros(len(xy))
d[1:] = np.cumsum(np.sqrt(((np.diff(xy, axis=0)) ** 2).sum(axis=1)))
total = d[-1]
if total <= 0:
return np.tile(xy[0], (n_waypoints, 1))
out = []
for i in range(n_waypoints):
t = (i + 0.5) / n_waypoints * total
idx = np.searchsorted(d, t, side="right") - 1
idx = max(0, min(idx, len(d) - 2))
w = (t - d[idx]) / (d[idx + 1] - d[idx] + 1e-12)
pt = (1 - w) * xy[idx] + w * xy[idx + 1]
out.append(pt)
return np.array(out)
def _resample_path_waypoints_3d(xyz: np.ndarray, n_waypoints: int) -> np.ndarray:
"""Resample 3D path to n_waypoints (x,y,z) by cumulative 3D path length. xyz shape (N, 3)."""
if len(xyz) < 2 or n_waypoints <= 0:
return np.zeros((n_waypoints, 3))
d = np.zeros(len(xyz))
d[1:] = np.cumsum(np.sqrt(((np.diff(xyz, axis=0)) ** 2).sum(axis=1)))
total = d[-1]
if total <= 0:
return np.tile(xyz[0], (n_waypoints, 1))
out = []
for i in range(n_waypoints):
t = (i + 0.5) / n_waypoints * total
idx = np.searchsorted(d, t, side="right") - 1
idx = max(0, min(idx, len(d) - 2))
w = (t - d[idx]) / (d[idx + 1] - d[idx] + 1e-12)
pt = (1 - w) * xyz[idx] + w * xyz[idx + 1]
out.append(pt)
return np.array(out)
def compute_trial_stats(csv_path: Path, trial_id: str) -> dict:
"""Load one trajectory CSV and return a dict of summary stats (points with z >= 0 only)."""
df = load_trajectory_csv(csv_path)
if len(df) == 0:
return {"trial_id": trial_id, "n_points": 0}
frame = df["frame_number"].values.astype(int)
n_points = len(df)
frame_start = int(frame.min())
frame_end = int(frame.max())
duration_frames = frame_end - frame_start + 1 # span; actual points may be sparse
path_len = path_length_3d(df)
# Speed = path length per frame (units/frame); if frame rate known, can convert to units/sec
mean_speed = path_len / max(1, n_points - 1) # per step (consecutive points)
n_segments = int(df["segment_id"].max()) + 1
points_per_seg = df.groupby("segment_id").size()
seg_lengths = points_per_seg.tolist()
return {
"trial_id": trial_id,
"n_points": n_points,
"frame_start": frame_start,
"frame_end": frame_end,
"duration_frames": duration_frames,
"path_length_3d": round(path_len, 4),
"mean_speed_per_step": round(mean_speed, 4),
"n_segments": n_segments,
"min_segment_points": int(points_per_seg.min()) if len(points_per_seg) else 0,
"max_segment_points": int(points_per_seg.max()) if len(points_per_seg) else 0,
}
def aggregate_all_trials(predictions_dir: Path) -> pd.DataFrame:
"""Build a stats DataFrame for all trials with trajectory_filtered.csv.
Includes animal, session_folder, session_date, session_rank (1-based per animal), and phase (early/mid/late).
"""
trial_list = find_trajectory_csvs(predictions_dir)
phase_map = _assign_phase_per_trial(trial_list)
# Session order per animal for session_rank
by_animal_sessions: dict[str, list[str]] = {}
for _csv_path, _trial_id, session_folder in trial_list:
if session_folder is None:
continue
animal, _ = _session_to_animal_and_date(session_folder)
if animal not in by_animal_sessions:
by_animal_sessions[animal] = []
if session_folder not in by_animal_sessions[animal]:
by_animal_sessions[animal].append(session_folder)
for animal in by_animal_sessions:
by_animal_sessions[animal] = sorted(by_animal_sessions[animal])
session_to_rank: dict[tuple[str, str], int] = {}
for animal, sessions in by_animal_sessions.items():
for rank, session_folder in enumerate(sessions, start=1):
session_to_rank[(animal, session_folder)] = rank
rows = []
for csv_path, trial_id, session_folder in trial_list:
row = compute_trial_stats(csv_path, trial_id)
animal, session_date = _session_to_animal_and_date(session_folder)
row["animal"] = animal
row["session_folder"] = session_folder if session_folder else ""
row["session_date"] = session_date
key_phase = (trial_id, session_folder or "")
row["phase"] = phase_map.get(key_phase, "unknown")
rank_key = (animal, session_folder) if session_folder else (animal, "")
row["session_rank"] = session_to_rank.get(rank_key, 0)
rows.append(row)
return pd.DataFrame(rows)
def compute_peak_points_and_path_to_peak(predictions_dir: Path) -> tuple[pd.DataFrame, pd.DataFrame]:
"""
For each trial, compute peak point and path-to-peak; exclude only the peak point when z > MAX_PEAK_Z (outlier).
Returns (peak_points_df, path_to_peak_df). Both include animal, session_folder, phase when available.
"""
trial_list = find_trajectory_csvs(predictions_dir)
phase_map = _assign_phase_per_trial(trial_list)
peak_rows = []
path_to_peak_rows = []
for csv_path, trial_id, session_folder in trial_list:
df = load_trajectory_csv(csv_path)
if len(df) < 1:
continue
idx_max = df["z"].idxmax()
row_peak = df.loc[idx_max]
if row_peak["z"] > MAX_PEAK_Z:
continue # exclude only this 3D point (outlier) from peak/path-to-peak outputs
animal, _ = _session_to_animal_and_date(session_folder)
phase = phase_map.get((trial_id, session_folder or ""), "unknown")
peak_rows.append({
"trial_id": trial_id,
"animal": animal,
"session_folder": session_folder if session_folder else "",
"phase": phase,
"x": row_peak["x"],
"y": row_peak["y"],
"z": row_peak["z"],
"frame_number": int(row_peak["frame_number"]),
})
pos_max = df.index.get_loc(idx_max)
segment = df.iloc[0 : pos_max + 1]
frame_start = int(segment["frame_number"].iloc[0])
frame_peak = int(segment["frame_number"].iloc[-1])
frames_to_peak = frame_peak - frame_start
path_to_peak_rows.append({
"trial_id": trial_id,
"animal": animal,
"session_folder": session_folder if session_folder else "",
"phase": phase,
"frame_start": frame_start,
"frame_peak": frame_peak,
"frames_to_peak": frames_to_peak,
"seconds_to_peak": round(frames_to_peak / FPS, 4),
"path_length_to_peak": round(path_length_3d(segment), 4),
"n_points_to_peak": len(segment),
})
return pd.DataFrame(peak_rows), pd.DataFrame(path_to_peak_rows)
def _flow_field_two_panels(
trials_list: list[tuple[Path, str]],
x_min: float,
x_max: float,
y_min: float,
y_max: float,
out_path: Path,
title_suffix: str = "",
) -> None:
"""Compute elevation + flow direction and flow speed + flow direction for the given trials, save two-panel figure to out_path. title_suffix is appended to panel titles."""
if len(trials_list) == 0:
return
n_bins = 40
x_edges = np.linspace(x_min, x_max, n_bins + 1)
y_edges = np.linspace(y_min, y_max, n_bins + 1)
sum_z_elev = np.zeros((n_bins, n_bins))
count_z = np.zeros((n_bins, n_bins))
sum_dx = np.zeros((n_bins, n_bins))
sum_dy = np.zeros((n_bins, n_bins))
sum_w = np.zeros((n_bins, n_bins)) + 1e-12
sum_z = np.zeros((n_bins, n_bins))
sum_vel = np.zeros((n_bins, n_bins))
for csv_path, _, _ in trials_list:
df = load_trajectory_csv(csv_path)
if len(df) < 2:
continue
mask = (df["z"] <= MAX_PEAK_Z) & ((df["y"] <= Y_FOR_Z_CAP) | (df["z"] <= Z_CAP_IN_Y_REGION))
df = df.loc[mask]
if len(df) < 2:
continue
xyzf = _ordered_xyz_frame_path(df)
if len(xyzf) < 2:
continue
xyz = xyzf[:, :3]
frames = xyzf[:, 3]
for k in range(len(xyz)):
px, py, pz = xyz[k, 0], xyz[k, 1], xyz[k, 2]
ix = np.searchsorted(x_edges, px, side="right") - 1
iy = np.searchsorted(y_edges, py, side="right") - 1
ix = max(0, min(ix, n_bins - 1))
iy = max(0, min(iy, n_bins - 1))
sum_z_elev[iy, ix] += pz
count_z[iy, ix] += 1
for i in range(len(xyz) - 1):
dx = xyz[i + 1, 0] - xyz[i, 0]
dy = xyz[i + 1, 1] - xyz[i, 1]
dz = xyz[i + 1, 2] - xyz[i, 2]
dt = (frames[i + 1] - frames[i]) / FPS
if dt <= 0:
continue
raw_vel = np.sqrt(dx * dx + dy * dy + dz * dz) / dt
mid_x = (xyz[i, 0] + xyz[i + 1, 0]) / 2
mid_y = (xyz[i, 1] + xyz[i + 1, 1]) / 2
mid_z = (xyz[i, 2] + xyz[i + 1, 2]) / 2
if mid_y > Y_FOR_Z_CAP and mid_z > Z_CAP_IN_Y_REGION:
continue
weight = max(0.0, dz)
if weight <= 0:
continue
ix = np.searchsorted(x_edges, mid_x, side="right") - 1
iy = np.searchsorted(y_edges, mid_y, side="right") - 1
ix = max(0, min(ix, n_bins - 1))
iy = max(0, min(iy, n_bins - 1))
sum_dx[iy, ix] += dx * weight
sum_dy[iy, ix] += dy * weight
sum_w[iy, ix] += weight
sum_z[iy, ix] += mid_z * weight
sum_vel[iy, ix] += raw_vel * weight
with np.errstate(divide="ignore", invalid="ignore"):
u = sum_dx / sum_w
v = sum_dy / sum_w
mean_raw_vel = np.where(sum_w > 1e-10, sum_vel / sum_w, 0.0)
elev_grid = np.where(count_z > 0, sum_z_elev / count_z, np.nan)
cx = (x_edges[:-1] + x_edges[1:]) / 2
cy = (y_edges[:-1] + y_edges[1:]) / 2
XX, YY = np.meshgrid(cx, cy)
speed = np.sqrt(u ** 2 + v ** 2)
scale = np.percentile(speed[speed > 0], 95) / (0.05 * (x_max - x_min)) if (speed > 0).any() else 1.0
scale = max(scale, 1e-6) * 2.5
x_tips = XX + u / scale
y_tips = YY + v / scale
plot_x_min = min(x_min, np.nanmin(x_tips))
plot_x_max = max(x_max, np.nanmax(x_tips))
plot_y_min = min(y_min, np.nanmin(y_tips))
plot_y_max = max(y_max, np.nanmax(y_tips))
center_x = (x_min + x_max) / 2
center_y = (y_min + y_max) / 2
half_range = max(
plot_x_max - center_x, center_x - plot_x_min,
plot_y_max - center_y, center_y - plot_y_min,
)
pad_frac = 0.02
half_range *= 1 + pad_frac
plot_x_min = center_x - half_range
plot_x_max = center_x + half_range
plot_y_min = center_y - half_range
plot_y_max = center_y + half_range
vel_finite = mean_raw_vel[(mean_raw_vel > 0) & np.isfinite(mean_raw_vel)]
vmin_vel = np.percentile(vel_finite, 10) if len(vel_finite) > 0 else 0.0
vmax_vel = np.percentile(vel_finite, 95) if len(vel_finite) > 0 else 1.0
elev_vmin = np.nanmin(elev_grid) if np.any(np.isfinite(elev_grid)) else 0.0
elev_vmax = np.nanmax(elev_grid) if np.any(np.isfinite(elev_grid)) else 1.0
fig2, (ax_left, ax_right) = plt.subplots(1, 2, figsize=(18, 8))
for ax in (ax_left, ax_right):
ax.set_xlim(plot_x_min, plot_x_max)
ax.set_ylim(plot_y_min, plot_y_max)
ax.set_aspect("equal")
ax.set_xlabel("x")
ax.set_ylabel("y")
if np.any(np.isfinite(elev_grid)):
im = ax_left.pcolormesh(x_edges, y_edges, elev_grid, cmap="turbo", shading="flat", vmin=elev_vmin, vmax=elev_vmax, alpha=0.9, zorder=0)
plt.colorbar(im, ax=ax_left, label="elevation z", shrink=0.7)
q_left = ax_left.quiver(XX, YY, u, v, color="black", alpha=0.9, scale_units="xy", scale=scale, width=0.006, headwidth=3, headlength=4, edgecolor="white", linewidth=0.2, zorder=10)
ax_left.quiverkey(q_left, 0.88, 0.96, np.percentile(speed[speed > 0], 50) if (speed > 0).any() else 1.0, "flow", coordinates="axes")
ax_left.set_title("Elevation + flow direction" + title_suffix)
speed_display = np.where(mean_raw_vel > 0, mean_raw_vel, np.nan)
cmap_turbo = plt.cm.turbo.copy()
cmap_turbo.set_bad("white")
im_speed = ax_right.pcolormesh(x_edges, y_edges, speed_display, cmap=cmap_turbo, shading="flat", vmin=vmin_vel, vmax=vmax_vel, alpha=0.9, zorder=0)
plt.colorbar(im_speed, ax=ax_right, label="raw velocity (3D, dist/s)", shrink=0.7)
q_right = ax_right.quiver(XX, YY, u, v, color="black", alpha=0.9, scale_units="xy", scale=scale, width=0.006, headwidth=3, headlength=4, edgecolor="white", linewidth=0.2, zorder=10)
ax_right.quiverkey(q_right, 0.88, 0.96, np.percentile(speed[speed > 0], 50) if (speed > 0).any() else 1.0, "flow", coordinates="axes")
ax_right.set_title("Flow speed + flow direction" + title_suffix)
plt.tight_layout()
fig2.savefig(out_path, dpi=150, bbox_inches="tight")
plt.close()
def _time_spent_heatmap(
trials_list: list[tuple[Path, str]],
x_min: float,
x_max: float,
y_min: float,
y_max: float,
out_path: Path,
title_suffix: str = "",
) -> None:
"""Compute time spent in each spatial bin (same grid as flow field) for the given trials; save single-panel heatmap to out_path."""
if len(trials_list) == 0:
return
n_bins = 40
x_edges = np.linspace(x_min, x_max, n_bins + 1)
y_edges = np.linspace(y_min, y_max, n_bins + 1)
time_in_cell = np.zeros((n_bins, n_bins))
for csv_path, _, _ in trials_list:
df = load_trajectory_csv(csv_path)
if len(df) < 2:
continue
mask = (df["z"] <= MAX_PEAK_Z) & ((df["y"] <= Y_FOR_Z_CAP) | (df["z"] <= Z_CAP_IN_Y_REGION))
df = df.loc[mask]
if len(df) < 2:
continue
xyzf = _ordered_xyz_frame_path(df)
if len(xyzf) < 2:
continue
xyz = xyzf[:, :3]
frames = xyzf[:, 3]
for i in range(len(xyz) - 1):
dt = (frames[i + 1] - frames[i]) / FPS
if dt <= 0:
continue
mid_x = (xyz[i, 0] + xyz[i + 1, 0]) / 2
mid_y = (xyz[i, 1] + xyz[i + 1, 1]) / 2
mid_z = (xyz[i, 2] + xyz[i + 1, 2]) / 2
if mid_y > Y_FOR_Z_CAP and mid_z > Z_CAP_IN_Y_REGION:
continue
ix = np.searchsorted(x_edges, mid_x, side="right") - 1
iy = np.searchsorted(y_edges, mid_y, side="right") - 1
ix = max(0, min(ix, n_bins - 1))
iy = max(0, min(iy, n_bins - 1))
time_in_cell[iy, ix] += dt
fig, ax = plt.subplots(figsize=(10, 9))
time_display = np.where(time_in_cell > 0, time_in_cell, np.nan)
cmap = plt.cm.viridis.copy()
cmap.set_bad("white", alpha=0.3)
im = ax.pcolormesh(x_edges, y_edges, time_display, cmap=cmap, shading="flat", alpha=0.9)
plt.colorbar(im, ax=ax, label="Time spent (s)", shrink=0.7)
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.set_aspect("equal")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Time spent per region" + title_suffix)
for v in [x_min + (x_max - x_min) * k / 4 for k in range(5)]:
ax.axvline(v, color="k", linewidth=0.4, alpha=0.4)
for v in [y_min + (y_max - y_min) * k / 4 for k in range(5)]:
ax.axhline(v, color="k", linewidth=0.4, alpha=0.4)
plt.tight_layout()
fig.savefig(out_path, dpi=150, bbox_inches="tight")
plt.close()
def make_plots(
stats_df: pd.DataFrame,
predictions_dir: Path,
out_dir: Path,
*,
included_trial_ids: set[str] | None = None,
) -> None:
"""Generate illustrative plots and save to out_dir.
If included_trial_ids is set, only those trials are used (e.g. when filtering by --animal).
"""
out_dir = Path(out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
def _trial_list() -> list[tuple[Path, str, str | None]]:
lst = find_trajectory_csvs(predictions_dir)
if included_trial_ids is not None:
lst = [x for x in lst if x[1] in included_trial_ids]
return lst
trials = stats_df["trial_id"].values
n_trials = len(stats_df)
# 1) Path length (3D) per trial
fig, ax = plt.subplots(figsize=(max(8, n_trials * 0.25), 5))
ax.bar(range(n_trials), stats_df["path_length_3d"], color="steelblue", edgecolor="navy", alpha=0.8)
ax.set_xticks(range(n_trials))
ax.set_xticklabels([t.replace("Predictions_3D_", "") for t in trials], rotation=45, ha="right")
ax.set_ylabel("Path length (3D, a.u.)")
ax.set_title("Trajectory path length per trial")
plt.tight_layout()
fig.savefig(out_dir / "path_length_per_trial.png", dpi=150, bbox_inches="tight")
plt.close()
# 2) Duration (frames) per trial
fig, ax = plt.subplots(figsize=(max(8, n_trials * 0.25), 5))
ax.bar(range(n_trials), stats_df["duration_frames"], color="coral", edgecolor="darkred", alpha=0.8)
ax.set_xticks(range(n_trials))
ax.set_xticklabels([t.replace("Predictions_3D_", "") for t in trials], rotation=45, ha="right")
ax.set_ylabel("Duration (frames)")
ax.set_title("Trial duration (frame span) per trial")
plt.tight_layout()
fig.savefig(out_dir / "duration_per_trial.png", dpi=150, bbox_inches="tight")
plt.close()
# 3) Path length vs duration (scatter)
fig, ax = plt.subplots(figsize=(6, 5))
ax.scatter(stats_df["duration_frames"], stats_df["path_length_3d"], alpha=0.7, s=40)
ax.set_xlabel("Duration (frames)")
ax.set_ylabel("Path length (3D)")
ax.set_title("Path length vs duration")
plt.tight_layout()
fig.savefig(out_dir / "path_length_vs_duration.png", dpi=150, bbox_inches="tight")
plt.close()
# 4) Number of segments per trial
fig, ax = plt.subplots(figsize=(max(8, n_trials * 0.25), 5))
ax.bar(range(n_trials), stats_df["n_segments"], color="seagreen", edgecolor="darkgreen", alpha=0.8)
ax.set_xticks(range(n_trials))
ax.set_xticklabels([t.replace("Predictions_3D_", "") for t in trials], rotation=45, ha="right")
ax.set_ylabel("Number of segments")
ax.set_title("Trajectory segments per trial")
plt.tight_layout()
fig.savefig(out_dir / "n_segments_per_trial.png", dpi=150, bbox_inches="tight")
plt.close()
# 5) Histogram: path length distribution
fig, ax = plt.subplots(figsize=(5, 4))
ax.hist(stats_df["path_length_3d"], bins=min(20, max(5, n_trials // 3)), color="steelblue", edgecolor="white")
ax.set_xlabel("Path length (3D)")
ax.set_ylabel("Number of trials")
ax.set_title("Distribution of path length across trials")
plt.tight_layout()
fig.savefig(out_dir / "path_length_histogram.png", dpi=150, bbox_inches="tight")
plt.close()
# 5b) Path length by phase (early / mid / late) per animal — for multi-session comparison
if "animal" in stats_df.columns and "phase" in stats_df.columns:
df_phase = stats_df[stats_df["phase"].isin(["early", "mid", "late", "single"])]
if len(df_phase) > 0:
animals = sorted(df_phase["animal"].unique())
n_animals = len(animals)
if n_animals > 0:
phase_order = ["early", "mid", "late", "single"]
fig, axes = plt.subplots(1, n_animals, figsize=(5 * max(1, n_animals), 5))
if n_animals == 1:
axes = [axes]
for ax, animal in zip(axes, animals):
sub = df_phase[df_phase["animal"] == animal]
phases = [p for p in phase_order if p in sub["phase"].values]
if not phases:
phases = sub["phase"].unique().tolist()
data = [sub[sub["phase"] == p]["path_length_3d"].values for p in phases]
bp = ax.boxplot(data, tick_labels=phases, patch_artist=True)
for patch in bp["boxes"]:
patch.set_facecolor("steelblue")
patch.set_alpha(0.8)
ax.set_ylabel("Path length (3D)")
ax.set_xlabel("Session phase")
ax.set_title(f"{animal}")
fig.suptitle("Path length by session phase (early / mid / late) per animal")
plt.tight_layout()
fig.savefig(out_dir / "path_length_by_phase.png", dpi=150, bbox_inches="tight")
plt.close()
# 5c) Path length vs session rank (session 1, 2, 3, ...) per animal — trend over sessions
df_rank = stats_df[(stats_df["session_rank"] >= 1) & (stats_df["animal"] != "unknown")]
if len(df_rank) > 0:
animals = sorted(df_rank["animal"].unique())
n_animals = len(animals)
if n_animals > 0:
fig, axes = plt.subplots(1, n_animals, figsize=(5 * max(1, n_animals), 5))
if n_animals == 1:
axes = [axes]
for ax, animal in zip(axes, animals):
sub = df_rank[df_rank["animal"] == animal].sort_values("session_rank")
ax.scatter(sub["session_rank"], sub["path_length_3d"], alpha=0.6, s=30)
if len(sub) > 1:
z = np.polyfit(sub["session_rank"], sub["path_length_3d"], 1)
p = np.poly1d(z)
x_line = np.linspace(sub["session_rank"].min(), sub["session_rank"].max(), 50)
ax.plot(x_line, p(x_line), "r-", alpha=0.8, label="linear fit")
ax.set_xlabel("Session rank (1 = first session)")
ax.set_ylabel("Path length (3D)")
ax.set_title(f"{animal}")
ax.legend(loc="best", fontsize=8)
fig.suptitle("Path length vs session order (early → late)")
plt.tight_layout()
fig.savefig(out_dir / "path_length_vs_session_rank.png", dpi=150, bbox_inches="tight")
plt.close()
# 6) Example top-down trajectories for first 6 trials — shared axis scale (u-v camera view or x-y world)
csv_list = _trial_list()[:6]
n_ex = len(csv_list)
col_a, col_b, invert_y, xlabel_2d, ylabel_2d = _use_uv_for_path_plots(_trial_list())
if n_ex > 0:
# Compute global limits across all example trials so every panel has the same scale
all_a, all_b = [], []
for csv_path, _, _ in csv_list:
df = load_trajectory_csv(csv_path)
if len(df) >= 1:
all_a.extend(df[col_a].tolist())
all_b.extend(df[col_b].tolist())
if all_a and all_b:
a_min, a_max = min(all_a), max(all_a)
b_min, b_max = min(all_b), max(all_b)
# Small margin and ensure equal aspect range so scale is comparable
da = a_max - a_min or 1
db = b_max - b_min or 1
margin = 0.05
a_min = a_min - margin * da
a_max = a_max + margin * da
b_min = b_min - margin * db
b_max = b_max + margin * db
else:
a_min = b_min = -100
a_max = b_max = 100
fig, axes = plt.subplots(2, 3, figsize=(12, 8))
axes = axes.flatten()
for i, (csv_path, trial_id, _) in enumerate(csv_list):
if i >= len(axes):
break
df = load_trajectory_csv(csv_path)
ax = axes[i]
if len(df) >= 2:
for seg_id in df["segment_id"].unique():
seg = df[df["segment_id"] == seg_id]
ax.plot(seg[col_a], seg[col_b], alpha=0.8, linewidth=1)
ax.set_xlim(a_min, a_max)
ax.set_ylim(b_min, b_max)
if invert_y:
ax.invert_yaxis()
ax.set_aspect("equal")
ax.set_title(trial_id.replace("Predictions_3D_", ""), fontsize=9)
ax.set_xlabel(xlabel_2d)
ax.set_ylabel(ylabel_2d)
for j in range(i + 1, len(axes)):
axes[j].set_visible(False)
fig.suptitle(f"Example trajectories (top-down {col_a}-{col_b}, same scale)")
plt.tight_layout()
fig.savefig(out_dir / "example_trajectories_xy.png", dpi=150, bbox_inches="tight")
plt.close()
# 6b) All trials in one plot (top-down u-v or x-y), colored by elevation z
all_trials_xy = _trial_list()
if len(all_trials_xy) > 0:
all_a_lim, all_b_lim = [], []
all_x_lim, all_y_lim = [], [] # world coords for flow field / time_spent
all_z = []
for csv_path, _, _ in all_trials_xy:
df = load_trajectory_csv(csv_path)
if len(df) >= 1:
all_a_lim.extend(df[col_a].tolist())
all_b_lim.extend(df[col_b].tolist())
all_x_lim.extend(df["x"].tolist())
all_y_lim.extend(df["y"].tolist())
# z <= 150 and exclude (y > -100 and z > 120)
mask = (df["z"] <= MAX_PEAK_Z) & ((df["y"] <= Y_FOR_Z_CAP) | (df["z"] <= Z_CAP_IN_Y_REGION))
all_z.extend(df.loc[mask, "z"].tolist())
if all_a_lim and all_b_lim:
a_min, a_max = min(all_a_lim), max(all_a_lim)
b_min, b_max = min(all_b_lim), max(all_b_lim)
da = a_max - a_min or 1
db = b_max - b_min or 1
margin = 0.05
a_min = a_min - margin * da
a_max = a_max + margin * da
b_min = b_min - margin * db
b_max = b_max + margin * db
else:
a_min = b_min = -100
a_max = b_max = 100
if all_x_lim and all_y_lim:
x_min, x_max = min(all_x_lim), max(all_x_lim)
y_min, y_max = min(all_y_lim), max(all_y_lim)
dx, dy = x_max - x_min or 1, y_max - y_min or 1
margin = 0.05
x_min = x_min - margin * dx
x_max = x_max + margin * dx
y_min = y_min - margin * dy
y_max = y_max + margin * dy
else:
x_min = y_min = -100
x_max = y_max = 100
z_min = min(all_z) if all_z else 0
z_max = max(all_z) if all_z else 100
norm = plt.Normalize(vmin=z_min, vmax=z_max)
cmap = plt.colormaps.get_cmap("viridis")
fig, ax = plt.subplots(figsize=(10, 10))
for csv_path, trial_id, _ in all_trials_xy:
df = load_trajectory_csv(csv_path)
if len(df) < 2:
continue
for seg_id in df["segment_id"].unique():
seg = df[df["segment_id"] == seg_id]
seg = seg[(seg["z"] <= MAX_PEAK_Z) & ((seg["y"] <= Y_FOR_Z_CAP) | (seg["z"] <= Z_CAP_IN_Y_REGION))]
if len(seg) < 2:
continue
a_vals = seg[col_a].values.astype(float)
b_vals = seg[col_b].values.astype(float)
z = seg["z"].values.astype(float)
n = len(a_vals)
segments = np.stack([np.column_stack([a_vals[:-1], b_vals[:-1]]), np.column_stack([a_vals[1:], b_vals[1:]])], axis=1)
z_seg = (z[:-1] + z[1:]) / 2
lc = LineCollection(segments, array=z_seg, cmap=cmap, norm=norm, linewidth=1.2, alpha=0.9)
ax.add_collection(lc)
ax.set_xlim(a_min, a_max)
ax.set_ylim(b_min, b_max)
if invert_y:
ax.invert_yaxis()
ax.set_aspect("equal")
ax.set_xlabel(xlabel_2d)
ax.set_ylabel(ylabel_2d)
ax.set_title(f"All trials — {col_a}-{col_b} (top-down), colored by elevation z")
cbar = plt.colorbar(lc, ax=ax, label="z (elevation)")
# Peak centroids (world x,y,z): only overlay on plot when using world x-y (not u-v)
high_z_pct = 92.0
if all_z and len(all_z) >= 10 and col_a == "x":
z_thresh = np.percentile(all_z, high_z_pct)
pts_x, pts_y, pts_z = [], [], []
for csv_path, _, _ in all_trials_xy:
df = load_trajectory_csv(csv_path)
if len(df) < 1:
continue
mask = (df["z"] <= MAX_PEAK_Z) & ((df["y"] <= Y_FOR_Z_CAP) | (df["z"] <= Z_CAP_IN_Y_REGION))
mask = mask & (df["z"].values >= z_thresh)
pts_x.extend(df.loc[mask, "x"].tolist())
pts_y.extend(df.loc[mask, "y"].tolist())
pts_z.extend(df.loc[mask, "z"].tolist())
pts = np.column_stack([pts_x, pts_y, pts_z]).astype(float)
if len(pts) >= 2:
centroids = _kmeans2_xyz(pts)
ax.scatter(centroids[:, 0], centroids[:, 1], s=40, c="red", marker="o", zorder=5)
peak_df = pd.DataFrame({"peak_id": [1, 2], "x": centroids[:, 0], "y": centroids[:, 1], "z": centroids[:, 2]})
peak_path = out_dir / "high_peak_centroids.csv"
peak_df.to_csv(peak_path, index=False)
print(f"Saved 2 peak centroids (x,y,z) to {peak_path}")
plt.tight_layout()
fig.savefig(out_dir / "all_trials_xy.png", dpi=150, bbox_inches="tight")
plt.close()
# 6b1b) All trials by animal × phase (rory, wilfred × early, mid, late) — always when we have animal/phase
if "animal" in stats_df.columns and "phase" in stats_df.columns:
trial_to_animal = stats_df.set_index("trial_id")["animal"].to_dict()
trial_to_phase = stats_df.set_index("trial_id")["phase"].to_dict()
animals_ordered = ["rory", "wilfred"]
phases_ordered = ["early", "mid", "late"]
by_ap: dict[tuple[str, str], list] = {}
for tr in all_trials_xy:
trial_id = tr[1]
a = trial_to_animal.get(trial_id, "unknown")
p = trial_to_phase.get(trial_id, "unknown")
if a not in animals_ordered or p not in phases_ordered:
continue
key = (a, p)
if key not in by_ap:
by_ap[key] = []
by_ap[key].append(tr)
if by_ap:
norm_all = plt.Normalize(vmin=z_min, vmax=z_max)
fig_ap, axes_ap = plt.subplots(2, 3, figsize=(14, 10))
for ri, animal in enumerate(animals_ordered):
for ci, phase in enumerate(phases_ordered):
ax_ap = axes_ap[ri, ci]
sub = by_ap.get((animal, phase), [])
if len(sub) == 0:
ax_ap.set_xlim(a_min, a_max)
ax_ap.set_ylim(b_min, b_max)
if invert_y:
ax_ap.invert_yaxis()
ax_ap.set_aspect("equal")
ax_ap.set_xlabel(xlabel_2d)
ax_ap.set_ylabel(ylabel_2d)
ax_ap.set_title(f"{animal} — {phase} (n=0)")
continue
for csv_path, _tid, _ in sub:
df = load_trajectory_csv(csv_path)
if len(df) < 2:
continue
for seg_id in df["segment_id"].unique():
seg = df[df["segment_id"] == seg_id]
seg = seg[(seg["z"] <= MAX_PEAK_Z) & ((seg["y"] <= Y_FOR_Z_CAP) | (seg["z"] <= Z_CAP_IN_Y_REGION))]
if len(seg) < 2:
continue
a_vals = seg[col_a].values.astype(float)
b_vals = seg[col_b].values.astype(float)
z = seg["z"].values.astype(float)
segments = np.stack([np.column_stack([a_vals[:-1], b_vals[:-1]]), np.column_stack([a_vals[1:], b_vals[1:]])], axis=1)
z_seg = (z[:-1] + z[1:]) / 2
lc_ap = LineCollection(segments, array=z_seg, cmap=cmap, norm=norm_all, linewidth=1.2, alpha=0.9)
ax_ap.add_collection(lc_ap)
ax_ap.set_xlim(a_min, a_max)
ax_ap.set_ylim(b_min, b_max)
if invert_y:
ax_ap.invert_yaxis()
ax_ap.set_aspect("equal")
ax_ap.set_xlabel(xlabel_2d)
ax_ap.set_ylabel(ylabel_2d)
ax_ap.set_title(f"{animal} — {phase} (n={len(sub)})")
plt.colorbar(
plt.cm.ScalarMappable(norm=norm_all, cmap=cmap),
ax=axes_ap,
label="z (elevation)",
shrink=0.6,
)
fig_ap.suptitle(f"All trials — {col_a}-{col_b} (top-down) by animal × phase")
plt.tight_layout()
fig_ap.savefig(out_dir / "trajectories_xy_by_animal_phase.png", dpi=150, bbox_inches="tight")
plt.close()
# 6b2) Trajectories for trials where vertical is on the left (left_angle_deg == 360)
trial_types_path = out_dir / "trial_types.csv"
if trial_types_path.is_file():
tt = pd.read_csv(trial_types_path)
vertical_left_ids = set(tt.loc[(tt["left_angle_deg"] == 360.0) & (tt["right_angle_deg"] != 360.0), "trial_id"].astype(str))
vertical_left_trials = [x for x in all_trials_xy if x[1] in vertical_left_ids]
if len(vertical_left_trials) > 0:
all_z_vl = []
all_x_lim_vl, all_y_lim_vl = [], [] # world coords for flow/time_spent
all_a_lim_vl, all_b_lim_vl = [], [] # path coords (u,v or x,y) for trajectory plot
for csv_path, _, _ in vertical_left_trials:
df = load_trajectory_csv(csv_path)
if len(df) >= 1:
all_x_lim_vl.extend(df["x"].tolist())
all_y_lim_vl.extend(df["y"].tolist())
all_a_lim_vl.extend(df[col_a].tolist())
all_b_lim_vl.extend(df[col_b].tolist())
mask = (df["z"] <= MAX_PEAK_Z) & ((df["y"] <= Y_FOR_Z_CAP) | (df["z"] <= Z_CAP_IN_Y_REGION))
all_z_vl.extend(df.loc[mask, "z"].tolist())
if all_x_lim_vl and all_y_lim_vl:
x_min_vl = min(all_x_lim_vl) - 0.05 * (max(all_x_lim_vl) - min(all_x_lim_vl) or 1)
x_max_vl = max(all_x_lim_vl) + 0.05 * (max(all_x_lim_vl) - min(all_x_lim_vl) or 1)
y_min_vl = min(all_y_lim_vl) - 0.05 * (max(all_y_lim_vl) - min(all_y_lim_vl) or 1)
y_max_vl = max(all_y_lim_vl) + 0.05 * (max(all_y_lim_vl) - min(all_y_lim_vl) or 1)
else:
x_min_vl, x_max_vl, y_min_vl, y_max_vl = a_min, a_max, b_min, b_max
if all_a_lim_vl and all_b_lim_vl:
a_min_vl = min(all_a_lim_vl) - 0.05 * (max(all_a_lim_vl) - min(all_a_lim_vl) or 1)
a_max_vl = max(all_a_lim_vl) + 0.05 * (max(all_a_lim_vl) - min(all_a_lim_vl) or 1)
b_min_vl = min(all_b_lim_vl) - 0.05 * (max(all_b_lim_vl) - min(all_b_lim_vl) or 1)
b_max_vl = max(all_b_lim_vl) + 0.05 * (max(all_b_lim_vl) - min(all_b_lim_vl) or 1)
else:
a_min_vl, a_max_vl, b_min_vl, b_max_vl = a_min, a_max, b_min, b_max
z_min_vl = min(all_z_vl) if all_z_vl else 0
z_max_vl = max(all_z_vl) if all_z_vl else 100
norm_vl = plt.Normalize(vmin=z_min_vl, vmax=z_max_vl)
fig_vl, ax_vl = plt.subplots(figsize=(10, 10))
for csv_path, trial_id, _ in vertical_left_trials:
df = load_trajectory_csv(csv_path)
if len(df) < 2:
continue
for seg_id in df["segment_id"].unique():
seg = df[df["segment_id"] == seg_id]
seg = seg[(seg["z"] <= MAX_PEAK_Z) & ((seg["y"] <= Y_FOR_Z_CAP) | (seg["z"] <= Z_CAP_IN_Y_REGION))]
if len(seg) < 2:
continue
a_vals = seg[col_a].values.astype(float)
b_vals = seg[col_b].values.astype(float)
z = seg["z"].values.astype(float)
segments = np.stack([np.column_stack([a_vals[:-1], b_vals[:-1]]), np.column_stack([a_vals[1:], b_vals[1:]])], axis=1)
z_seg = (z[:-1] + z[1:]) / 2
lc_vl = LineCollection(segments, array=z_seg, cmap=cmap, norm=norm_vl, linewidth=1.2, alpha=0.9)
ax_vl.add_collection(lc_vl)
ax_vl.set_xlim(a_min_vl, a_max_vl)
ax_vl.set_ylim(b_min_vl, b_max_vl)
if invert_y:
ax_vl.invert_yaxis()
ax_vl.set_aspect("equal")
ax_vl.set_xlabel(xlabel_2d)
ax_vl.set_ylabel(ylabel_2d)
ax_vl.set_title(f"Trials with vertical on left — {col_a}-{col_b} (top-down), n = {len(vertical_left_trials)}")
plt.colorbar(lc_vl, ax=ax_vl, label="z (elevation)")
plt.tight_layout()
fig_vl.savefig(out_dir / "trajectories_xy_vertical_on_left.png", dpi=150, bbox_inches="tight")