-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_automl.py
More file actions
1717 lines (1422 loc) · 75.3 KB
/
run_automl.py
File metadata and controls
1717 lines (1422 loc) · 75.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
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
"""
AutoML Training and Inference for GMA Score Prediction
Integrates with pose processing pipeline for end-to-end prediction.
"""
import os
import pickle
import warnings
from pathlib import Path
from datetime import datetime
from typing import Optional, Tuple, Dict
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import (
roc_curve, auc, precision_recall_curve,
average_precision_score, confusion_matrix,
ConfusionMatrixDisplay, balanced_accuracy_score
)
from sklearn.inspection import permutation_importance
import autosklearn
from autosklearn.experimental.askl2 import AutoSklearn2Classifier
import autosklearn.metrics
warnings.filterwarnings("ignore")
class GMAAutoMLPipeline:
"""AutoML pipeline for GMA score prediction with comprehensive logging."""
def __init__(self,
data_path: str = './data',
output_path: str = './automl_output',
feature_type: str = 'total',
feature_file: Optional[str] = None,
apply_exclusions: bool = True,
prereg: bool = False,
time_limit: int = 300,
per_run_limit: int = 30):
"""
Initialize AutoML pipeline.
Args:
data_path: Path to data directory (for splits, scores, exclusions)
output_path: Path for outputs
feature_type: 'total' or 'windows' features
feature_file: Path to feature file (if None, auto-detects from pose pipeline output)
apply_exclusions: Whether to apply exclusions from all_excluded_videos.csv
prereg: Whether to apply pre-registered model feature name mapping
time_limit: Total time for AutoML (seconds)
per_run_limit: Time per model run (seconds)
"""
self.data_path = Path(data_path)
self.feature_type = feature_type
self.feature_file = Path(feature_file) if feature_file else None
self.apply_exclusions = apply_exclusions
self.prereg = prereg
self.time_limit = time_limit
self.per_run_limit = per_run_limit
# If prereg is enabled, automatically save to 'prereg' subfolder
if prereg:
self.output_path = Path(output_path) / 'prereg'
else:
self.output_path = Path(output_path)
# Setup directories
self.output_path.mkdir(parents=True, exist_ok=True)
self.log_path = self.output_path / 'logs'
self.log_path.mkdir(exist_ok=True)
self.plots_path = self.output_path / 'plots'
self.plots_path.mkdir(exist_ok=True)
self.models_path = self.output_path / 'models'
self.models_path.mkdir(exist_ok=True)
# Initialize logging
self.log_file = self.log_path / f'automl_{datetime.now():%Y%m%d_%H%M%S}.log'
self.data_loss_log = {
'stage': [],
'expected_count': [],
'actual_count': [],
'lost_count': [],
'lost_ids': [],
'timestamp': []
}
# Feature name mapping (new -> old format for pre-registered models)
self.feature_mapping = {
"Cross-corr_wrist_pos": "Wrist_lrCorr_x",
"Cross-corr_knee_angle": "Knee_lrCorr_angle",
"Med_elbow_angle_vel": "Elbow_median_vel_angle",
"Med_wrist_pos_x": "Wrist_medianx",
"Med_wrist_pos_y": "Wrist_mediany",
"Cross-corr_elbow_angle": "Elbow_lrCorr_angle",
"Med_wrist_angle_vel": "Wrist_median_vel_angle",
"Med_ankle_pos_x": "Ankle_medianx",
"Med_ankle_pos_y": "Ankle_mediany",
"Med_knee_angle_vel": "Knee_median_vel_angle",
"Entropy_elbow_angle": "Elbow_entropy_angle",
"Med_wrist_vel_x": "Wrist_medianvelx",
"IQR_wrist_vel_x": "Wrist_IQRvelx",
"IQR_ankle_vel_y": "Ankle_IQRvely",
"IQR_wrist_pos_x": "Wrist_IQRx",
"IQR_wrist_pos_y": "Wrist_IQRy",
"IQR_wrist_accel_x": "Wrist_IQRaccx",
"Mean_elbow_angle": "Elbow_mean_angle",
"IQR_elbow_angle_vel": "Elbow_IQR_vel_angle",
"IQR_elbow_angle_accel": "Elbow_IQR_acc_angle",
"IQR_ankle_vel_x": "Ankle_IQRvelx",
"Med_ankle_vel_y": "Ankle_medianvely",
"Cross-corr_ankle_pos": "Ankle_lrCorr_x",
"Med_ankle_vel_x": "Ankle_medianvelx",
"IQR_knee_angle_vel": "Knee_IQR_vel_angle",
"IQR_ankle_pos_y": "Ankle_IQRy",
"Mean_knee_angle": "Knee_mean_angle",
"IQR_knee_angle_accel": "Knee_IQR_acc_angle",
"Entropy_ankle_pos": "Ankle_meanent",
"Entropy_wrist_pos": "Wrist_meanent",
"Stdev_elbow_angle": "Elbow_stdev_angle",
"IQR_ankle_pos_x": "Ankle_IQRx",
"Entropy_knee_angle": "Knee_entropy_angle",
"IQR_wrist_accel_y": "Wrist_IQRaccy",
"IQR_wrist_vel_y": "Wrist_IQRvely",
"Stdev_knee_angle": "Knee_stdev_angle",
"Med_wrist_vel_y": "Wrist_medianvely",
"IQR_ankle_accel_x": "Ankle_IQRaccx",
"IQR_ankle_accel_y": "Ankle_IQRaccy"
}
self.automl = None
self.log("AutoML Pipeline initialized")
self.log(f"Exclusions: {'ENABLED' if apply_exclusions else 'DISABLED (--no-exclusions flag)'}")
self.log(f"Pre-registered model mapping: {'ENABLED' if prereg else 'DISABLED'}")
if prereg:
self.log(f"Output directory (prereg mode): {self.output_path}")
def log(self, message: str):
"""Write to log file with timestamp."""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_msg = f"[{timestamp}] {message}\n"
with open(self.log_file, 'a') as f:
f.write(log_msg)
print(message)
def track_data_loss(self, stage: str, expected_ids, actual_ids,
context: Optional[str] = None):
"""Track data loss at each stage."""
expected_set = set(str(x) for x in expected_ids)
actual_set = set(str(x) for x in actual_ids)
lost = expected_set - actual_set
self.data_loss_log['stage'].append(stage)
self.data_loss_log['expected_count'].append(len(expected_set))
self.data_loss_log['actual_count'].append(len(actual_set))
self.data_loss_log['lost_count'].append(len(lost))
self.data_loss_log['lost_ids'].append(list(lost))
self.data_loss_log['timestamp'].append(datetime.now())
loss_pct = len(lost) / len(expected_set) * 100 if expected_set else 0
msg = f"{stage}: Expected {len(expected_set)} | Got {len(actual_set)} | Lost {len(lost)} ({loss_pct:.1f}%)"
if context:
msg += f" - {context}"
self.log(msg)
if lost and len(lost) <= 10:
self.log(f" Lost IDs: {sorted(list(lost))}")
elif lost:
self.log(f" Lost IDs (first 10): {sorted(list(lost))[:10]}")
def save_data_loss_report(self):
"""Save comprehensive data loss report."""
df = pd.DataFrame(self.data_loss_log)
report_path = self.log_path / 'data_loss_report.csv'
df.to_csv(report_path, index=False)
self.log(f"Data loss report saved to {report_path}")
# Create visualization
self._plot_data_loss(df)
def _plot_data_loss(self, df: pd.DataFrame):
"""Create data loss visualization."""
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10))
# Plot absolute counts
x = range(len(df))
ax1.plot(x, df['expected_count'], 'o-', label='Expected',
linewidth=2, markersize=8, color='#2ecc71')
ax1.plot(x, df['actual_count'], 'o-', label='Actual',
linewidth=2, markersize=8, color='#3498db')
ax1.fill_between(x, df['actual_count'], df['expected_count'],
alpha=0.3, color='#e74c3c')
ax1.set_xticks(x)
ax1.set_xticklabels(df['stage'], rotation=45, ha='right')
ax1.set_ylabel('Sample Count', fontsize=12)
ax1.set_title('Data Flow Through AutoML Pipeline', fontsize=14, fontweight='bold')
ax1.legend(fontsize=11)
ax1.grid(True, alpha=0.3)
# Plot loss percentage
loss_pct = (df['lost_count'] / df['expected_count'] * 100).fillna(0)
bars = ax2.bar(x, loss_pct, color='#e74c3c', alpha=0.6, edgecolor='black')
# Add value labels on bars
for i, (bar, pct) in enumerate(zip(bars, loss_pct)):
if pct > 0:
ax2.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5,
f'{pct:.1f}%', ha='center', va='bottom', fontsize=9)
ax2.set_xticks(x)
ax2.set_xticklabels(df['stage'], rotation=45, ha='right')
ax2.set_ylabel('Loss (%)', fontsize=12)
ax2.set_title('Data Loss Percentage by Stage', fontsize=14, fontweight='bold')
ax2.grid(True, alpha=0.3, axis='y')
plt.tight_layout()
plt.savefig(self.plots_path / 'data_loss_visualization.png', dpi=150, bbox_inches='tight')
plt.close()
self.log("Data loss visualization saved")
def load_data(self) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame,
pd.DataFrame, pd.DataFrame, pd.DataFrame]:
"""Load all required data with loss tracking."""
self.log("="*70)
self.log("Loading data...")
self.log("="*70)
# Load split definitions
train_ids = pd.read_csv(self.data_path / 'train.csv')
val_ids = pd.read_csv(self.data_path / 'val.csv')
test_ids = pd.read_csv(self.data_path / 'test.csv')
test_holdout_ids = pd.read_csv(self.data_path / 'holdout.csv') # Renamed from holdout
self.log(f"Original split sizes:")
self.log(f" Train: {len(train_ids)}, Val: {len(val_ids)} → Combined Train: {len(train_ids) + len(val_ids)}")
self.log(f" Test: {len(test_ids)}")
self.log(f" Test_Holdout (LockBox): {len(test_holdout_ids)}")
# Load exclusions (TAKES PRECEDENCE if enabled)
excluded_ids = pd.DataFrame()
exclusion_file = self.data_path / 'all_excluded_videos.csv'
if not self.apply_exclusions:
self.log(f"\n⚠️ EXCLUSIONS DISABLED (--no-exclusions flag)")
self.log(f" Skipping {exclusion_file}")
self.log(f" All videos will be included (except those without features/scores)")
elif exclusion_file.exists():
excluded_ids = pd.read_csv(exclusion_file)
self.log(f"\n⚠️ EXCLUSIONS LOADED: {len(excluded_ids)} videos will be excluded")
# Check what columns exist
self.log(f" Exclusion file columns: {list(excluded_ids.columns)}")
# Determine the ID column name
id_col = None
for possible_name in ['gma_id', 'infant', 'video', 'id', 'video_id']:
if possible_name in excluded_ids.columns:
id_col = possible_name
break
if id_col is None:
self.log(" WARNING: Could not identify ID column in exclusion file")
self.log(f" Available columns: {list(excluded_ids.columns)}")
else:
excluded_set = set(excluded_ids[id_col])
# Check overlap with splits
all_split_ids = (set(train_ids['gma_id']) | set(val_ids['gma_id']) |
set(test_ids['gma_id']) | set(test_holdout_ids['gma_id']))
overlap = excluded_set & all_split_ids
if overlap:
self.log(f" ⚠️ {len(overlap)} excluded IDs are in train/val/test/test_holdout")
self.log(f" These will be removed (exclusion takes precedence)")
self.log(f" Excluded from splits: {sorted(list(overlap))[:10]}...")
else:
self.log(f" ✓ No excluded IDs found in splits")
else:
self.log(f"\nNote: No exclusion file found at {exclusion_file}")
# Check for overlaps between splits
self._check_split_overlaps(train_ids, val_ids, test_ids, test_holdout_ids)
# Load scores
scores = pd.read_csv(self.data_path / 'gma_score_prediction_scores.csv')
# Normalize scores to 0-indexed
if scores['score'].min() != 0:
original_scores = scores['score'].copy()
scores = scores[scores['score'].isin([1, 2, 3])].copy()
scores['score'] = scores['score'].apply(lambda x: int(x) - 1)
self.log(f"\nNormalized scores from {original_scores.unique()} to {scores['score'].unique()}")
# Load features
features = self._load_features()
self.log(f"\nLoaded {self.feature_type} features: {features.shape}")
self.log(f"Feature columns: {list(features.columns[:5])}... ({len(features.columns)} total)")
return train_ids, val_ids, test_ids, test_holdout_ids, excluded_ids, scores, features
def _load_features(self) -> pd.DataFrame:
"""Load features from specified file or auto-detect from pose pipeline output."""
# If explicit feature file provided, use it
if self.feature_file and self.feature_file.exists():
self.log(f"\nLoading features from: {self.feature_file}")
features = pd.read_csv(self.feature_file)
else:
# Auto-detect from pose pipeline output
self.log("\nAuto-detecting feature file from pose pipeline output...")
# Try standard pose pipeline output location
pose_output_paths = [
Path(f'./pose_estimates/PANDA2_pose_estimates/features_total_consolidated.csv'),
Path(f'./pose_estimates/*/features_total_consolidated.csv'),
self.data_path / 'final_total_features.csv', # Fallback to old location
self.data_path / 'final_window_features.csv',
]
feature_file_found = None
for path_pattern in pose_output_paths:
# Handle glob patterns
if '*' in str(path_pattern):
matches = list(Path('.').glob(str(path_pattern)))
if matches:
feature_file_found = matches[0]
break
elif path_pattern.exists():
feature_file_found = path_pattern
break
if feature_file_found:
self.log(f" Found features at: {feature_file_found}")
features = pd.read_csv(feature_file_found)
else:
# Last resort: try legacy naming
self.log(" Could not auto-detect pose pipeline output, trying legacy locations...")
if self.feature_type == 'windows':
legacy_path = self.data_path / 'final_window_features.csv'
else:
legacy_path = self.data_path / 'final_total_features.csv'
if legacy_path.exists():
self.log(f" Found features at: {legacy_path}")
features = pd.read_csv(legacy_path)
else:
raise FileNotFoundError(
f"Could not find feature file. Tried:\n" +
"\n".join([f" - {p}" for p in pose_output_paths]) +
f"\n - {legacy_path}\n\n" +
"Please specify feature file explicitly with --feature-file argument"
)
# Clean column names
if 'Unnamed: 0' in features.columns:
features = features.drop(columns=['Unnamed: 0'])
# Rename 'video' column to 'infant' if needed for consistency
if 'video' in features.columns and 'infant' not in features.columns:
features = features.rename(columns={'video': 'infant'})
self.log(" Renamed 'video' column to 'infant' for consistency")
return features
def _check_split_overlaps(self, train_ids, val_ids, test_ids, test_holdout_ids):
"""Check for overlaps between data splits."""
self.log("\nChecking for overlaps between splits:")
splits = {
'Train': set(train_ids['gma_id']),
'Val': set(val_ids['gma_id']),
'Test': set(test_ids['gma_id']),
'Test_Holdout': set(test_holdout_ids['gma_id'])
}
overlaps_found = False
for name1, ids1 in splits.items():
for name2, ids2 in splits.items():
if name1 < name2: # Only check each pair once
overlap = ids1 & ids2
if overlap:
self.log(f" WARNING: {name1}-{name2} overlap: {len(overlap)} IDs")
overlaps_found = True
else:
self.log(f" {name1}-{name2}: No overlap ✓")
if not overlaps_found:
self.log(" All splits are properly separated ✓")
def _average_left_right_features(self, features: pd.DataFrame) -> pd.DataFrame:
"""Average left and right side features for pre-registered model compatibility."""
self.log("\n Averaging left/right side features...")
# Start with ID column
id_col = 'infant' if 'infant' in features.columns else 'video'
averaged_features = {id_col: features[id_col]}
# Find all unique feature patterns
all_cols = [col for col in features.columns if col not in ['infant', 'video']]
# Track which columns we've processed
processed = set()
averaged_count = 0
for col in all_cols:
if col in processed:
continue
# Parse column name: metric_LSide or metric_RSide format
if '_L' in col:
# This is a left-side feature
parts = col.split('_L')
if len(parts) == 2:
metric = parts[0]
body_part = parts[1]
left_col = f"{metric}_L{body_part}"
right_col = f"{metric}_R{body_part}"
if left_col in features.columns and right_col in features.columns:
# Average left and right
avg_col_name = f"{metric}_{body_part}"
averaged_features[avg_col_name] = (features[left_col] + features[right_col]) / 2
processed.add(left_col)
processed.add(right_col)
averaged_count += 1
else:
# Only left exists
averaged_features[col] = features[col]
processed.add(col)
elif '_R' in col:
# This is a right-side feature
parts = col.split('_R')
if len(parts) == 2:
metric = parts[0]
body_part = parts[1]
left_col = f"{metric}_L{body_part}"
right_col = f"{metric}_R{body_part}"
if left_col not in features.columns:
# Only right exists (left was already processed or doesn't exist)
if right_col not in processed:
averaged_features[col] = features[col]
processed.add(col)
else:
# Not a left/right feature (e.g., lrCorr_x_Ankle)
averaged_features[col] = features[col]
processed.add(col)
result_df = pd.DataFrame(averaged_features)
self.log(f" Averaged {averaged_count} left/right feature pairs")
self.log(f" Total features after averaging: {len(result_df.columns) - 1} (excluding ID column)")
return result_df
def _apply_prereg_name_mapping(self, features: pd.DataFrame) -> pd.DataFrame:
"""Map averaged feature names to pre-registered model format."""
self.log("\n Applying pre-registered model name mapping...")
# Mapping from averaged format to pre-registered format
# Format: averaged_name → prereg_name
prereg_mapping = {
# Ankle position features (after averaging L/R)
'IQRx_Ankle': 'Ankle_IQRx',
'IQRy_Ankle': 'Ankle_IQRy',
'medianx_Ankle': 'Ankle_medianx',
'mediany_Ankle': 'Ankle_mediany',
'meanent_Ankle': 'Ankle_meanent',
'medianvelx_Ankle': 'Ankle_medianvelx',
'medianvely_Ankle': 'Ankle_medianvely',
'IQRvelx_Ankle': 'Ankle_IQRvelx',
'IQRvely_Ankle': 'Ankle_IQRvely',
'IQRaccx_Ankle': 'Ankle_IQRaccx',
'IQRaccy_Ankle': 'Ankle_IQRaccy',
# Wrist position features (after averaging L/R)
'IQRx_Wrist': 'Wrist_IQRx',
'IQRy_Wrist': 'Wrist_IQRy',
'medianx_Wrist': 'Wrist_medianx',
'mediany_Wrist': 'Wrist_mediany',
'meanent_Wrist': 'Wrist_meanent',
'medianvelx_Wrist': 'Wrist_medianvelx',
'medianvely_Wrist': 'Wrist_medianvely',
'IQRvelx_Wrist': 'Wrist_IQRvelx',
'IQRvely_Wrist': 'Wrist_IQRvely',
'IQRaccx_Wrist': 'Wrist_IQRaccx',
'IQRaccy_Wrist': 'Wrist_IQRaccy',
# Elbow angle features (after averaging L/R)
'IQR_acc_angle_Elbow': 'Elbow_IQR_acc_angle',
'IQR_vel_angle_Elbow': 'Elbow_IQR_vel_angle',
'entropy_angle_Elbow': 'Elbow_entropy_angle',
'mean_angle_Elbow': 'Elbow_mean_angle',
'median_vel_angle_Elbow': 'Elbow_median_vel_angle',
'stdev_angle_Elbow': 'Elbow_stdev_angle',
# Knee angle features (after averaging L/R)
'IQR_acc_angle_Knee': 'Knee_IQR_acc_angle',
'IQR_vel_angle_Knee': 'Knee_IQR_vel_angle',
'entropy_angle_Knee': 'Knee_entropy_angle',
'mean_angle_Knee': 'Knee_mean_angle',
'median_vel_angle_Knee': 'Knee_median_vel_angle',
'stdev_angle_Knee': 'Knee_stdev_angle',
# Cross-correlation features (these don't have L/R, already averaged internally)
'lrCorr_x_Ankle': 'Ankle_lrCorr_x',
'lrCorr_x_Wrist': 'Wrist_lrCorr_x',
# 'lrCorr_x_Elbow': 'Elbow_lrCorr_x', # Added in case it exists
# 'lrCorr_x_Knee': 'Knee_lrCorr_x', # Added in case it exists
'lrCorr_angle_Elbow': 'Elbow_lrCorr_angle',
'lrCorr_angle_Knee': 'Knee_lrCorr_angle',
}
# Apply mapping
mapped_count = 0
for old_name, new_name in prereg_mapping.items():
if old_name in features.columns:
mapped_count += 1
features = features.rename(columns=prereg_mapping)
self.log(f" Mapped {mapped_count} feature names to pre-registered format")
# Check for any unmapped features (excluding ID columns)
unmapped = [col for col in features.columns
if col not in ['infant', 'video']
and col not in prereg_mapping.values()]
if unmapped:
self.log(f" ⚠️ Warning: {len(unmapped)} features were not mapped:")
self.log(f" Unmapped features: {sorted(unmapped)[:10]}")
if len(unmapped) > 10:
self.log(f" ... and {len(unmapped) - 10} more")
# Final feature count verification
feature_cols = [col for col in features.columns if col not in ['infant', 'video']]
self.log(f"\n ✓ Final feature count: {len(feature_cols)} features")
if len(feature_cols) != 38:
self.log(f" ⚠️ WARNING: Expected 38 features but got {len(feature_cols)}!")
self.log(f" Features present: {sorted(feature_cols)}")
else:
self.log(f" ✓ Confirmed: Exactly 38 features as expected for pre-registered model")
return features
def prepare_datasets(self, train_ids, val_ids, test_ids, test_holdout_ids,
excluded_ids, scores, features) -> Dict:
"""Prepare train/test/lockbox datasets with comprehensive loss tracking."""
self.log("\n" + "="*70)
self.log("Preparing datasets...")
self.log("="*70)
# Apply pre-registered feature mapping if enabled
if self.prereg:
self.log("\n🔄 Pre-registered model preprocessing...")
self.log(f" Features BEFORE preprocessing: {features.shape}")
self.log(f" Sample columns: {list(features.columns[:10])}")
# Step 1: Average left and right side features
features = self._average_left_right_features(features)
self.log(f" Features AFTER averaging L/R: {features.shape}")
self.log(f" Sample columns: {list(features.columns[:10])}")
# Step 2: Apply feature name mapping to match pre-registered model format
features = self._apply_prereg_name_mapping(features)
self.log(f" Features AFTER name mapping: {features.shape}")
self.log(f" Final columns: {list(features.columns[:10])}")
# Track where each LockBox ID goes (detailed forensics)
lockbox_tracking = self._initialize_lockbox_tracking(test_holdout_ids, features, scores, excluded_ids)
# Check for NaNs in features
self._log_nan_stats(features, "Raw features")
# CRITICAL: Apply exclusions FIRST (takes absolute precedence)
# Can be disabled with --no-exclusions flag
if not excluded_ids.empty and self.apply_exclusions:
# Determine ID column in exclusion file
id_col = None
for possible_name in ['gma_id', 'infant', 'video', 'id', 'video_id']:
if possible_name in excluded_ids.columns:
id_col = possible_name
break
if id_col:
excluded_set = set(excluded_ids[id_col])
before_exclusion = len(features)
# Track which lockbox IDs were excluded
lockbox_excluded = set(test_holdout_ids['gma_id']) & excluded_set
for vid in lockbox_excluded:
lockbox_tracking[vid]['excluded'] = True
if 'reason' in excluded_ids.columns:
reason_row = excluded_ids[excluded_ids[id_col] == vid]
if not reason_row.empty:
lockbox_tracking[vid]['exclusion_reason'] = reason_row['reason'].iloc[0]
# Remove excluded videos from features
features = features[~features['infant'].isin(excluded_set)].copy()
excluded_count = before_exclusion - len(features)
self.log(f"\n⚠️ EXCLUSION APPLIED: Removed {excluded_count} videos from features")
if excluded_count > 0:
actually_excluded = excluded_set - set(features['infant'])
self.log(f" Videos excluded from features: {len(actually_excluded)}")
self.log(f" Excluded IDs (first 10): {sorted(list(actually_excluded))[:10]}")
# Log lockbox-specific exclusions
if lockbox_excluded:
self.log(f" ⚠️ LockBox IDs excluded: {len(lockbox_excluded)}")
self.log(f" IDs: {sorted(list(lockbox_excluded))[:10]}")
# Also remove from scores
before_score_exclusion = len(scores)
scores = scores[~scores['infant'].isin(excluded_set)].copy()
score_excluded_count = before_score_exclusion - len(scores)
if score_excluded_count > 0:
self.log(f" Videos excluded from scores: {score_excluded_count}")
elif not self.apply_exclusions:
self.log(f"\n✓ EXCLUSIONS SKIPPED (--no-exclusions flag enabled)")
self.log(f" All {len(features)} videos with features will be processed")
# Track which lockbox IDs made it past exclusions
lockbox_after_exclusion = set(test_holdout_ids['gma_id']) & set(features['infant'])
for vid in lockbox_after_exclusion:
lockbox_tracking[vid]['in_features_after_exclusion'] = True
# Merge features with scores (only after exclusions applied)
original_feature_ids = features['infant'].unique()
features_with_scores = pd.merge(features, scores, on='infant', how='inner')
# Track which lockbox IDs have scores
lockbox_with_scores = set(test_holdout_ids['gma_id']) & set(features_with_scores['infant'])
lockbox_no_scores = lockbox_after_exclusion - lockbox_with_scores
for vid in lockbox_no_scores:
lockbox_tracking[vid]['missing_score'] = True
for vid in lockbox_with_scores:
lockbox_tracking[vid]['has_score'] = True
self.track_data_loss(
"Feature-Score Merge (after exclusions)",
original_feature_ids,
features_with_scores['infant'].unique(),
"Missing scores after exclusion"
)
# Handle missing values
before_dropna = len(features_with_scores)
# Define columns that are actually needed for modeling
feature_cols = [col for col in features_with_scores.columns
if col not in ['infant', 'age_corrected', 'age_chronological']]
required_cols = feature_cols + ['score'] # Only check features + score
# Drop rows with NaN only in required columns
features_clean = features_with_scores.dropna(subset=required_cols)
# Track which lockbox IDs were dropped due to NaN
lockbox_before_dropna = set(test_holdout_ids['gma_id']) & set(features_with_scores['infant'])
lockbox_after_dropna = set(test_holdout_ids['gma_id']) & set(features_clean['infant'])
lockbox_dropped_nan = lockbox_before_dropna - lockbox_after_dropna
for vid in lockbox_dropped_nan:
lockbox_tracking[vid]['dropped_nan'] = True
# Find which columns had NaN for this video
vid_row = features_with_scores[features_with_scores['infant'] == vid]
if not vid_row.empty:
nan_cols = vid_row.columns[vid_row.isnull().any()].tolist()
lockbox_tracking[vid]['nan_columns'] = nan_cols
if before_dropna > len(features_clean):
dropped_count = before_dropna - len(features_clean)
self.log(f"\nDropped {dropped_count} rows with missing values ({dropped_count/before_dropna*100:.1f}%)")
if lockbox_dropped_nan:
self.log(f" ⚠️ LockBox IDs dropped due to NaN: {len(lockbox_dropped_nan)}")
self.log(f" IDs: {sorted(list(lockbox_dropped_nan))}")
missing_counts = features_with_scores.isnull().sum()
top_missing = missing_counts[missing_counts > 0].sort_values(ascending=False).head(5)
self.log("Top 5 columns with missing values:")
for col, count in top_missing.items():
self.log(f" {col}: {count} missing ({count/before_dropna*100:.1f}%)")
# Apply preprocessing based on feature type
if self.feature_type == 'total':
self.log("\nApplying preprocessing for 'total' features:")
self.log(f" Initial samples: {len(features_clean)}")
len_orig = len(features_clean)
# Merge abnormal scores (2 -> 1)
# features_clean['score'] = features_clean['score'].replace({2: 1})
# self.log(f" Merged score 2 into score 1 (Abnormal category)")
#drop atypical scores (2)
features_clean = features_clean[features_clean['score'] != 2]
self.log(f" Removed {len_orig - len(features_clean)} score 2 (Atypical category)")
score_dist = features_clean['score'].value_counts().sort_index()
self.log(f" Final score distribution: {dict(score_dist)}")
# Split into Train (train+val), Test, LockBox (test_holdout)
datasets = {}
# Training set (train + val COMBINED - AutoML doesn't need separate validation)
train_data = features_clean[
features_clean['infant'].isin(train_ids['gma_id']) |
features_clean['infant'].isin(val_ids['gma_id'])
]
self.track_data_loss(
"Train Split (train+val combined)",
list(train_ids['gma_id']) + list(val_ids['gma_id']),
train_data['infant'].unique(),
"Missing features or scores"
)
# Test set
test_data = features_clean[features_clean['infant'].isin(test_ids['gma_id'])]
self.track_data_loss(
"Test Split",
test_ids['gma_id'],
test_data['infant'].unique(),
"Missing features or scores"
)
# LockBox set (formerly holdout, now test_holdout)
lockbox_data = features_clean[features_clean['infant'].isin(test_holdout_ids['gma_id'])]
# Mark which lockbox IDs made it to final dataset
final_lockbox_ids = set(lockbox_data['infant'])
for vid in final_lockbox_ids:
lockbox_tracking[vid]['in_final_dataset'] = True
self.track_data_loss(
"LockBox Split (test_holdout)",
test_holdout_ids['gma_id'],
lockbox_data['infant'].unique(),
"Missing features or scores"
)
# Save filtered IDs and scores
self._save_filtered_splits(train_data, test_data, lockbox_data)
# ADD IT HERE - INSIDE prepare_datasets, AFTER train_data/test_data/lockbox_data are created
self._save_comprehensive_split_summary(
train_data, test_data, lockbox_data,
train_ids, val_ids, test_ids, test_holdout_ids,
excluded_ids, scores
)
# Generate detailed LockBox forensics report
self._log_lockbox_forensics(lockbox_tracking, test_holdout_ids)
# Prepare X, y arrays
drop_cols = ['infant', 'score']
if 'age_corrected' in features_clean.columns:
drop_cols.append('age_corrected')
if 'age_chronological' in features_clean.columns:
drop_cols.append('age_chronological')
datasets['X_train'] = train_data.drop(columns=drop_cols)
datasets['y_train'] = train_data['score']
datasets['train_ids'] = train_data['infant']
datasets['X_test'] = test_data.drop(columns=drop_cols)
datasets['y_test'] = test_data['score']
datasets['test_ids'] = test_data['infant']
datasets['X_lockbox'] = lockbox_data.drop(columns=drop_cols)
datasets['y_lockbox'] = lockbox_data['score']
datasets['lockbox_ids'] = lockbox_data['infant']
features_to_drop = ['lrCorr_x_Elbow', 'lrCorr_x_Knee']
# Only drop if they exist
features_to_drop = [f for f in features_to_drop if f in datasets['X_train'].columns]
if features_to_drop:
self.log(f"\n🗑️ Dropping {len(features_to_drop)} lrCorr_x features: {features_to_drop}")
datasets['X_train'] = datasets['X_train'].drop(columns=features_to_drop)
datasets['X_test'] = datasets['X_test'].drop(columns=features_to_drop)
datasets['X_lockbox'] = datasets['X_lockbox'].drop(columns=features_to_drop)
self.log(f" Remaining features: {datasets['X_train'].shape[1]}")
# Log final sizes and NaN stats
self.log("\nFinal dataset sizes:")
self.log(f" Train (train+val): {len(datasets['X_train'])} samples, {datasets['X_train'].shape[1]} features")
self.log(f" Test: {len(datasets['X_test'])} samples")
self.log(f" LockBox: {len(datasets['X_lockbox'])} samples")
# Check NaNs in final datasets
self._log_nan_stats(datasets['X_train'], "Train features")
self._log_nan_stats(datasets['X_test'], "Test features")
self._log_nan_stats(datasets['X_lockbox'], "LockBox features")
# Log class distributions
for split in ['train', 'test', 'lockbox']:
y = datasets[f'y_{split}']
dist = y.value_counts().sort_index()
self.log(f" {split.capitalize()} score distribution: {dict(dist)}")
# Save filtered IDs and scores
self._save_filtered_splits(train_data, test_data, lockbox_data)
# Save lockbox tracking report
self._save_lockbox_tracking_report(lockbox_tracking)
return datasets
def _initialize_lockbox_tracking(self, test_holdout_ids, features, scores, excluded_ids) -> Dict:
"""Initialize tracking dictionary for each LockBox ID."""
tracking = {}
feature_ids = set(features['infant']) if 'infant' in features.columns else set()
score_ids = set(scores['infant']) if 'infant' in scores.columns else set()
# Determine exclusion ID column
excluded_set = set()
if not excluded_ids.empty:
for possible_name in ['gma_id', 'infant', 'video', 'id', 'video_id']:
if possible_name in excluded_ids.columns:
excluded_set = set(excluded_ids[possible_name])
break
for vid in test_holdout_ids['gma_id']:
tracking[vid] = {
'in_original_features': vid in feature_ids,
'in_original_scores': vid in score_ids,
'excluded': vid in excluded_set,
'exclusion_reason': None,
'in_features_after_exclusion': False,
'has_score': False,
'missing_score': False,
'dropped_nan': False,
'nan_columns': [],
'in_final_dataset': False,
}
return tracking
def _log_lockbox_forensics(self, lockbox_tracking: Dict, test_holdout_ids):
"""Generate detailed forensics report for LockBox IDs."""
self.log("\n" + "="*70)
self.log("LOCKBOX ID FORENSICS")
self.log("="*70)
total_lockbox = len(test_holdout_ids)
final_count = sum(1 for v in lockbox_tracking.values() if v['in_final_dataset'])
lost_count = total_lockbox - final_count
self.log(f"Total LockBox IDs: {total_lockbox}")
self.log(f"Made it to final dataset: {final_count}")
self.log(f"Lost during pipeline: {lost_count} ({lost_count/total_lockbox*100:.1f}%)")
# Categorize losses
never_in_features = [vid for vid, info in lockbox_tracking.items()
if not info['in_original_features']]
excluded_ids = [vid for vid, info in lockbox_tracking.items()
if info['excluded']]
missing_scores = [vid for vid, info in lockbox_tracking.items()
if not info['excluded'] and info['in_features_after_exclusion']
and info['missing_score']]
dropped_nan = [vid for vid, info in lockbox_tracking.items()
if info['dropped_nan']]
self.log("\nLoss breakdown:")
self.log(f" 1. Never in features file: {len(never_in_features)} IDs")
if never_in_features:
self.log(f" (Pose processing failed or features never generated)")
self.log(f" IDs: {sorted(never_in_features)[:10]}{'...' if len(never_in_features) > 10 else ''}")
self.log(f" 2. Excluded (all_excluded_videos.csv): {len(excluded_ids)} IDs")
if excluded_ids:
# Group by exclusion reason
reasons = {}
for vid in excluded_ids:
reason = lockbox_tracking[vid]['exclusion_reason'] or 'unknown'
reasons.setdefault(reason, []).append(vid)
for reason, ids in reasons.items():
self.log(f" Reason '{reason}': {len(ids)} IDs")
self.log(f" IDs: {sorted(ids)[:10]}{'...' if len(ids) > 10 else ''}")
self.log(f" 3. Missing GMA scores: {len(missing_scores)} IDs")
if missing_scores:
self.log(f" (Had features but no score in gma_score_prediction_scores.csv)")
self.log(f" IDs: {sorted(missing_scores)[:10]}{'...' if len(missing_scores) > 10 else ''}")
self.log(f" 4. Dropped due to NaN values: {len(dropped_nan)} IDs")
if dropped_nan:
self.log(f" (Had features and scores but contained missing values)")
# Show which features had NaN for first few IDs
for vid in sorted(dropped_nan)[:5]:
nan_cols = lockbox_tracking[vid]['nan_columns']
self.log(f" ID {vid}: NaN in {len(nan_cols)} columns: {nan_cols[:3]}{'...' if len(nan_cols) > 3 else ''}")
# Summary table
self.log("\nPer-ID status summary (first 20 LockBox IDs):")
self.log(f"{'ID':<8} {'InFeatures':<12} {'Excluded':<10} {'HasScore':<10} {'DroppedNaN':<12} {'Final':<8}")
self.log("-" * 70)
for vid in sorted(lockbox_tracking.keys())[:20]:
info = lockbox_tracking[vid]
self.log(
f"{vid:<8} "
f"{'✓' if info['in_original_features'] else '✗':<12} "
f"{'✓' if info['excluded'] else '✗':<10} "
f"{'✓' if info['has_score'] else '✗':<10} "
f"{'✓' if info['dropped_nan'] else '✗':<12} "
f"{'✓' if info['in_final_dataset'] else '✗':<8}"
)
if len(lockbox_tracking) > 20:
self.log(f"... (showing 20 of {len(lockbox_tracking)} total LockBox IDs)")
self.log("="*70 + "\n")
def _save_lockbox_tracking_report(self, lockbox_tracking: Dict):
"""Save detailed LockBox tracking report to CSV."""
tracking_data = []
for vid, info in lockbox_tracking.items():
tracking_data.append({
'lockbox_id': vid,
'in_original_features': info['in_original_features'],
'in_original_scores': info['in_original_scores'],
'excluded': info['excluded'],
'exclusion_reason': info['exclusion_reason'] or '',
'in_features_after_exclusion': info['in_features_after_exclusion'],
'has_score': info['has_score'],
'missing_score': info['missing_score'],
'dropped_nan': info['dropped_nan'],
'nan_columns': ','.join(map(str, info['nan_columns'])),
'in_final_dataset': info['in_final_dataset'],
})
df = pd.DataFrame(tracking_data)
report_path = self.log_path / 'lockbox_id_tracking.csv'
df.to_csv(report_path, index=False)
self.log(f"LockBox ID tracking report saved to: {report_path}")
# Also create summary stats
summary = {
'total_lockbox_ids': len(lockbox_tracking),
'in_final_dataset': df['in_final_dataset'].sum(),
'never_in_features': (~df['in_original_features']).sum(),
'excluded': df['excluded'].sum(),
'missing_score': df['missing_score'].sum(),
'dropped_nan': df['dropped_nan'].sum(),
}
summary_df = pd.DataFrame([summary])
summary_path = self.log_path / 'lockbox_tracking_summary.csv'
summary_df.to_csv(summary_path, index=False)
self.log(f"LockBox tracking summary saved to: {summary_path}")
def _save_filtered_splits(self, train_data, test_data, lockbox_data):
"""Save filtered IDs and scores as Train.csv, Test.csv, LockBox.csv"""
self.log("\nSaving filtered splits (after exclusions)...")
splits_output_path = self.output_path / 'filtered_splits'
splits_output_path.mkdir(exist_ok=True)
# Train (includes IDs and scores)
train_export = train_data[['infant', 'score']].copy()
train_export.columns = ['gma_id', 'score']
train_export.to_csv(splits_output_path / 'Train.csv', index=False)
self.log(f" Saved Train.csv: {len(train_export)} samples")
# Test
test_export = test_data[['infant', 'score']].copy()
test_export.columns = ['gma_id', 'score']
test_export.to_csv(splits_output_path / 'Test.csv', index=False)
self.log(f" Saved Test.csv: {len(test_export)} samples")
# LockBox
lockbox_export = lockbox_data[['infant', 'score']].copy()
lockbox_export.columns = ['gma_id', 'score']
lockbox_export.to_csv(splits_output_path / 'LockBox.csv', index=False)
self.log(f" Saved LockBox.csv: {len(lockbox_export)} samples")
self.log(f" All filtered splits saved to: {splits_output_path}")
def _save_comprehensive_split_summary(self, train_data, test_data, lockbox_data,
train_ids, val_ids, test_ids, test_holdout_ids,
excluded_ids, scores):