-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_user_level.py
More file actions
1467 lines (1215 loc) · 64.9 KB
/
train_user_level.py
File metadata and controls
1467 lines (1215 loc) · 64.9 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
"""
Question-Answer Pair Model Training and Output Saving with Question Strategies
Enhanced with proper hyperparameter tuning and user-level evaluation
"""
import os
# Torch 2.x optimizer initialization can import torch._dynamo, which imports
# optree. Preload optree's C-extension first to avoid late ABI surprises.
try:
import optree._C # noqa: F401
except Exception as e:
msg = str(e)
if "CXXABI_1.3.15" in msg or "libstdc++.so.6" in msg:
raise RuntimeError(
"Failed to load optree C-extension due to libstdc++ ABI mismatch. "
"Activate the conda environment and ensure its runtime libraries are used. "
"A common fix is: conda install -n maqua -c conda-forge libstdcxx-ng libgcc-ng"
) from e
raise
import torch
import torch.nn as nn
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler
from sklearn.model_selection import ParameterGrid
from sklearn.linear_model import LinearRegression, Ridge
from scipy.stats import pearsonr
from tqdm import tqdm
import json
import random
import argparse
import warnings
from maqua_paths import FORMATTED_DATA, MODEL_OUTPUTS, ensure_dir
warnings.filterwarnings('ignore')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class Config:
def __init__(self):
self.random_seed = 42
self.n_folds = 9
# Hyperparameter tuning configuration
self.hyperparam_tuning = True
self.max_epochs = 1000
self.patience = 20
# Model configuration
self.embedding_type = "full_embedding"
self.num_outputs = 10
self.aggregation_type = "all_question_aggregate"
# Model type configuration
self.model_type = "multitask" # Options: 'multitask', 'singletask', 'both'
# Question strategy configuration
self.question_strategy = "all_questions" # Options: 'specific_only', 'general_only', 'combined', 'all_questions'
# Outcome mapping
self.outcome_names = ["PHQ", "GAD", "MDQ", "RAADS", "DUDIT", "AUDIT", "BOCS", "ASRS", "NSE", "EDE_QS"]
self.singletask_outcomes = list(self.outcome_names)
# Hyperparameter search space (only Adam optimizer and MinMax scaler)
self.param_grid = {
'lr': [0.001, 0.01, 0.1],
'weight_decay': [0.0, 0.001, 0.01]
}
def get_config_name(self):
"""Generate a descriptive name for this configuration"""
if self.model_type == "multitask":
return f"{self.embedding_type}_multitask_{self.num_outputs}outputs_{self.aggregation_type}_{self.question_strategy}"
elif self.model_type == "singletask":
return f"{self.embedding_type}_singletask_{self.aggregation_type}_{self.question_strategy}"
else: # both
return f"{self.embedding_type}_both_{self.aggregation_type}_{self.question_strategy}"
class MultitaskLinearModel(nn.Module):
def __init__(self, input_dim, num_outcomes=10):
super().__init__()
self.linear = nn.Linear(input_dim, num_outcomes)
def forward(self, x):
return self.linear(x)
class SingletaskLinearModel(nn.Module):
def __init__(self, input_dim):
super().__init__()
self.linear = nn.Linear(input_dim, 1) # Single output
def forward(self, x):
return self.linear(x)
def init_weights(m):
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
class DataProcessor:
def __init__(self, config):
self.config = config
def load_data(self):
# Load question embeddings
question_data = pd.read_csv(FORMATTED_DATA / "question_embeddings.csv")
question_data.set_index("code", inplace=True)
# Load input data
input_essay = pd.read_csv(FORMATTED_DATA / "input_dataset_essay.csv")
input_words = pd.read_csv(FORMATTED_DATA / "input_dataset_words.csv")
input_data = pd.concat([input_words, input_essay], ignore_index=True)
# Load output data
output_data = pd.read_csv(FORMATTED_DATA / "output_questionnaire_outcomes.csv")
# Load question strategy mapping
questions_mapping = self.load_question_strategy_mapping()
return input_data, output_data, question_data, questions_mapping
def load_question_strategy_mapping(self):
"""Load question mapping with strategy filtering"""
try:
questions_df = pd.read_csv(FORMATTED_DATA / "questions_symptom_outcomes_type.csv")
except FileNotFoundError:
questions_df = pd.read_csv(FORMATTED_DATA / "questions.csv")
# Get all general questions (those with symptom == 'general')
general_questions = questions_df[questions_df['symptom'] == 'general']['code'].tolist()
# Get all questions regardless of symptom type
all_questions = questions_df['code'].tolist()
# Create mappings for specific symptoms for each outcome
outcome_to_specific_symptoms = {
'PHQ': ['depression'],
'GAD': ['anxiety'],
'MDQ': ['bipolar'],
'RAADS': ['autism'],
'DUDIT': ['substance abuse'],
'AUDIT': ['substance abuse'],
'BOCS': ['OCD'],
'ASRS': ['ADHD'],
'NSE': ['PTSD'],
'EDE_QS': ['eating']
}
# Create question sets for each outcome based on strategy
outcome_questions = {}
for outcome, specific_symptoms in outcome_to_specific_symptoms.items():
# Get specific questions for this outcome
specific_questions = questions_df[questions_df['symptom'].isin(specific_symptoms)]['code'].tolist()
# Apply strategy with special handling for outcomes without specific questions
if self.config.question_strategy == 'general_only':
outcome_questions[outcome] = general_questions
elif self.config.question_strategy == 'specific_only':
# For outcomes without specific questions (like DUDIT, AUDIT), fall back to general
if len(specific_questions) == 0:
print(f"WARNING: {outcome} has no specific questions, using general questions instead")
outcome_questions[outcome] = general_questions
else:
outcome_questions[outcome] = specific_questions
elif self.config.question_strategy == 'all_questions':
outcome_questions[outcome] = all_questions
else: # combined (default)
# For outcomes without specific questions, combined = general only
if len(specific_questions) == 0:
outcome_questions[outcome] = general_questions
else:
outcome_questions[outcome] = general_questions + specific_questions
# For multitask: use same strategy
if self.config.question_strategy == 'general_only':
outcome_questions['MULTITASK'] = general_questions
elif self.config.question_strategy == 'specific_only':
# For specific_only in multitask, use all specific questions from all outcomes
all_specific_questions = []
for symptoms in outcome_to_specific_symptoms.values():
specific_qs = questions_df[questions_df['symptom'].isin(symptoms)]['code'].tolist()
all_specific_questions.extend(specific_qs)
outcome_questions['MULTITASK'] = list(set(all_specific_questions)) # Remove duplicates
elif self.config.question_strategy == 'all_questions':
outcome_questions['MULTITASK'] = all_questions
else: # combined
outcome_questions['MULTITASK'] = all_questions
print(f"Question Strategy: {self.config.question_strategy}")
print(f"Total questions available: {len(all_questions)}")
print(f"General questions: {len(general_questions)}")
# Show detailed breakdown for each outcome
for outcome, questions in outcome_questions.items():
if outcome != 'MULTITASK':
specific_questions = questions_df[questions_df['symptom'].isin(outcome_to_specific_symptoms[outcome])]['code'].tolist()
if len(specific_questions) == 0:
print(f"{outcome} questions: {len(questions)} (no specific questions available)")
else:
print(f"{outcome} questions: {len(questions)} (has {len(specific_questions)} specific questions)")
print(f"MULTITASK questions: {len(outcome_questions['MULTITASK'])}")
return outcome_questions
def prepare_data(self, input_data, output_data, question_data, questions_mapping, target_outcome=None):
# Filter questions based on strategy
if target_outcome and target_outcome in questions_mapping:
# For singletask models, use questions specific to the target outcome
relevant_questions = questions_mapping[target_outcome]
input_data = input_data[input_data['question_code'].isin(relevant_questions)].copy()
print(f"Filtered to {len(input_data)} records for {target_outcome} using {self.config.question_strategy} strategy")
elif self.config.question_strategy != 'all_questions':
# For multitask models, use MULTITASK questions
relevant_questions = questions_mapping['MULTITASK']
input_data = input_data[input_data['question_code'].isin(relevant_questions)].copy()
print(f"Filtered to {len(input_data)} records using {self.config.question_strategy} strategy")
# Use all input columns
input_cols = [col for col in input_data.columns if col.startswith("input_")]
# Process question embeddings based on embedding_type
qu_embed_data = []
if self.config.embedding_type == "full_embedding":
for _, row in tqdm(input_data.iterrows(), total=len(input_data), desc="Processing question embeddings"):
embed_str = question_data.loc[row["question_code"], "question_embeddings"]
embed_values = [float(x) for x in embed_str.strip("[] ").split()][:64]
qu_embed_data.append(embed_values)
# Create final dataset
processed_data = pd.DataFrame({
'user_id': input_data['user_id'],
'question_code': input_data['question_code']
})
# Add input features
for col in input_cols:
processed_data[col] = input_data[col].values
# Add question embeddings if using full embedding
if self.config.embedding_type == "full_embedding":
qu_embed_df = pd.DataFrame(qu_embed_data, columns=[f"input_qu_embed_{i}" for i in range(64)])
processed_data = pd.concat([processed_data, qu_embed_df], axis=1)
# Process output data based on num_outputs
output_cols = [f'output_{i}' for i in range(self.config.num_outputs)]
combined_output_data = output_data[output_cols].copy()
# Merge data
merged_data = pd.merge(processed_data, combined_output_data, left_on="user_id", right_index=True)
merged_data.reset_index(drop=True, inplace=True)
return merged_data
def create_cv_folds(self, data):
# Aggregate by user, excluding user_id and question_code columns
agg_cols = {
col: (lambda x, c=col: np.mean(x) if c.startswith(('input_', 'output_')) else x.iloc[0])
for col in data.columns if col not in ['user_id', 'question_code']
}
user_data = data.groupby("user_id").agg(agg_cols).reset_index()
user_data["PHQ"] = user_data["output_0"]
user_data_sorted = user_data.sort_values(by="PHQ", ascending=False)
# Create folds
user_ids = user_data_sorted['user_id'].tolist()
bins = [[] for _ in range(self.config.n_folds)]
for i, user_id in enumerate(user_ids):
bins[i % self.config.n_folds].append(user_id)
return bins
class HyperparameterTuner:
def __init__(self, config):
self.config = config
def get_scaler(self, scaler_type='minmax'):
"""Always return MinMax scaler"""
return MinMaxScaler()
def train_single_model(self, train_data, val_data, params, target_outcome_idx=None):
"""Train a single model with given hyperparameters"""
# Prepare features and labels
input_cols = [col for col in train_data.columns if col.startswith("input_")]
output_cols = [col for col in train_data.columns if col.startswith("output_")]
# Aggregate by user
train_agg = train_data.groupby("user_id").agg({
**{col: 'mean' for col in input_cols},
**{col: 'first' for col in output_cols}
}).reset_index()
val_agg = val_data.groupby("user_id").agg({
**{col: 'mean' for col in input_cols},
**{col: 'first' for col in output_cols}
}).reset_index()
# Convert to numpy arrays
X_train = train_agg[input_cols].values
y_train = train_agg[output_cols].values
X_val = val_agg[input_cols].values
y_val = val_agg[output_cols].values
# Handle NaN/Inf
X_train = np.nan_to_num(X_train, nan=0.0, posinf=1.0, neginf=-1.0)
X_val = np.nan_to_num(X_val, nan=0.0, posinf=1.0, neginf=-1.0)
y_train = np.nan_to_num(y_train, nan=0.0, posinf=1.0, neginf=-1.0)
y_val = np.nan_to_num(y_val, nan=0.0, posinf=1.0, neginf=-1.0)
# Normalize (always use MinMax)
input_scaler = self.get_scaler()
output_scaler = self.get_scaler()
X_train_norm = input_scaler.fit_transform(X_train)
X_val_norm = input_scaler.transform(X_val)
# For singletask, select specific outcome
if target_outcome_idx is not None:
y_train = y_train[:, target_outcome_idx:target_outcome_idx+1]
y_val = y_val[:, target_outcome_idx:target_outcome_idx+1]
y_train_norm = output_scaler.fit_transform(y_train)
# Create model
train_inputs = torch.tensor(X_train_norm, dtype=torch.float32).to(device)
train_true = torch.tensor(y_train_norm, dtype=torch.float32).to(device)
val_inputs = torch.tensor(X_val_norm, dtype=torch.float32).to(device)
# Choose model based on task type
if target_outcome_idx is not None:
# Singletask model
model = SingletaskLinearModel(train_inputs.shape[1]).to(device)
else:
# Multitask model
model = MultitaskLinearModel(train_inputs.shape[1], train_true.shape[1]).to(device)
model.apply(init_weights)
# Create optimizer (always use Adam)
optimizer = torch.optim.Adam(
model.parameters(),
lr=params['lr'],
weight_decay=params['weight_decay']
)
criterion = nn.MSELoss()
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, patience=10, factor=0.5, min_lr=1e-6
)
# Training loop
best_val_loss = float('inf')
patience_counter = 0
for epoch in range(self.config.max_epochs):
# Training
model.train()
optimizer.zero_grad()
train_pred = model(train_inputs)
train_loss = criterion(train_pred, train_true)
if torch.isnan(train_loss) or torch.isinf(train_loss):
break
train_loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
# Validation check every 10 epochs.
# Note: patience_counter increments only at these checkpoints,
# so effective patience = self.config.patience × 10 epochs.
if epoch % 10 == 0:
model.eval()
with torch.no_grad():
val_pred = model(val_inputs)
val_loss = criterion(val_pred, torch.tensor(
output_scaler.transform(y_val), dtype=torch.float32
).to(device))
scheduler.step(val_loss)
if val_loss.item() < best_val_loss:
best_val_loss = val_loss.item()
patience_counter = 0
else:
patience_counter += 1
if patience_counter >= self.config.patience:
break
return model, input_scaler, output_scaler, best_val_loss
def tune_hyperparameters(self, train_folds_data, dev_folds_data, target_outcome_idx=None):
"""Perform hyperparameter tuning using cross-validation within train folds"""
print("Starting hyperparameter tuning...")
param_combinations = list(ParameterGrid(self.config.param_grid))
best_params = None
best_score = float('inf')
error_messages = []
results = []
# Use the number of train folds for cross-validation
num_train_folds = len(train_folds_data)
for i, params in enumerate(tqdm(param_combinations, desc="Hyperparameter tuning")):
print(f"\nTesting parameters {i+1}/{len(param_combinations)}: {params}")
fold_scores = []
# Cross-validation within training data
for fold_idx in range(num_train_folds):
# Use (num_train_folds-1) folds for training, 1 fold for validation
train_fold_data = []
val_fold_data = train_folds_data[fold_idx]
for j in range(num_train_folds):
if j != fold_idx:
train_fold_data.append(train_folds_data[j])
# Combine training folds
train_combined = pd.concat(train_fold_data, ignore_index=True)
try:
model, input_scaler, output_scaler, val_loss = self.train_single_model(
train_combined, val_fold_data, params, target_outcome_idx
)
fold_scores.append(val_loss)
except Exception as e:
err = f"param_idx={i}, fold={fold_idx}, error={e}"
error_messages.append(err)
print(f"Error in fold {fold_idx}: {e}")
fold_scores.append(float('inf'))
# Calculate average validation score
avg_score = np.mean(fold_scores)
std_score = np.std(fold_scores)
results.append({
'params': params,
'mean_val_loss': avg_score,
'std_val_loss': std_score,
'fold_scores': fold_scores
})
print(f"Average validation loss: {avg_score:.6f} ± {std_score:.6f}")
if avg_score < best_score:
best_score = avg_score
best_params = params
print(f"New best parameters found! Score: {best_score:.6f}")
print(f"\nBest hyperparameters: {best_params}")
print(f"Best validation score: {best_score:.6f}")
if best_params is None:
details = "\n".join(error_messages[:5])
if len(error_messages) > 5:
details += f"\n... and {len(error_messages) - 5} more errors"
raise RuntimeError(
"Hyperparameter tuning failed: no valid parameter set produced a finite "
"validation loss. This often indicates an environment/runtime failure."
+ (f"\nSample errors:\n{details}" if details else "")
)
return best_params, results
class Trainer:
def __init__(self, config):
self.config = config
self.tuner = HyperparameterTuner(config)
def train_model_with_hyperparams(self, train_folds_data, dev_folds_data, test_fold_data, target_outcome_idx=None):
"""Train model with hyperparameter tuning and evaluate at user level"""
# Step 1: Hyperparameter tuning using train folds
if self.config.hyperparam_tuning:
best_params, tuning_results = self.tuner.tune_hyperparameters(
train_folds_data, dev_folds_data, target_outcome_idx
)
else:
# Use default parameters (Adam optimizer only)
best_params = {
'lr': 0.01,
'weight_decay': 0.01
}
tuning_results = []
# Step 2: Train final model on all train folds with best hyperparameters
print("\nTraining final model with best hyperparameters...")
train_combined = pd.concat(train_folds_data, ignore_index=True)
dev_combined = pd.concat(dev_folds_data, ignore_index=True)
final_model, input_scaler, output_scaler, _ = self.tuner.train_single_model(
train_combined, dev_combined, best_params, target_outcome_idx
)
# Step 3: Evaluate at user level
print("Evaluating at user level...")
user_correlations, user_mse = self.evaluate_user_level(
final_model, input_scaler, output_scaler, test_fold_data, target_outcome_idx
)
return final_model, input_scaler, output_scaler, best_params, user_correlations, user_mse, tuning_results
def evaluate_user_level(self, model, input_scaler, output_scaler, test_data, target_outcome_idx=None):
"""Evaluate model at user level by aggregating predictions per user"""
# Get QA-level predictions
qa_predictions = self.predict_qa_pairs(model, input_scaler, output_scaler, test_data, target_outcome_idx)
# Aggregate to user level
if target_outcome_idx is not None:
# For singletask model
outcome_name = self.config.outcome_names[target_outcome_idx]
user_agg = qa_predictions.groupby('user_id').agg({
f'{outcome_name}_pred': 'mean',
f'output_{target_outcome_idx}': 'first'
}).reset_index()
# Calculate user-level metrics for single outcome
correlations = {}
mse_results = {}
try:
true_col = f'output_{target_outcome_idx}'
pred_col = f'{outcome_name}_pred'
if true_col in user_agg.columns and pred_col in user_agg.columns:
true_vals = user_agg[true_col].values
pred_vals = user_agg[pred_col].values
# Handle NaN/Inf
mask = np.isfinite(true_vals) & np.isfinite(pred_vals)
if np.sum(mask) > 1:
true_vals_clean = true_vals[mask]
pred_vals_clean = pred_vals[mask]
if np.var(true_vals_clean) > 1e-10 and np.var(pred_vals_clean) > 1e-10:
corr, _ = pearsonr(true_vals_clean, pred_vals_clean)
mse = np.mean((true_vals_clean - pred_vals_clean) ** 2)
correlations[outcome_name] = corr if np.isfinite(corr) else 0.0
mse_results[outcome_name] = mse if np.isfinite(mse) else float('inf')
else:
correlations[outcome_name] = 0.0
mse_results[outcome_name] = float('inf')
else:
correlations[outcome_name] = 0.0
mse_results[outcome_name] = float('inf')
else:
correlations[outcome_name] = 0.0
mse_results[outcome_name] = float('inf')
except Exception as e:
print(f"Error calculating metrics for {outcome_name}: {e}")
correlations[outcome_name] = 0.0
mse_results[outcome_name] = float('inf')
else:
# For multitask model (original behavior)
user_agg = qa_predictions.groupby('user_id').agg({
**{f'{outcome}_pred': 'mean' for outcome in self.config.outcome_names[:self.config.num_outputs]},
**{f'output_{i}': 'first' for i in range(self.config.num_outputs)}
}).reset_index()
# Calculate user-level metrics
correlations = {}
mse_results = {}
for i, outcome in enumerate(self.config.outcome_names[:self.config.num_outputs]):
try:
true_col = f'output_{i}'
pred_col = f'{outcome}_pred'
if true_col in user_agg.columns and pred_col in user_agg.columns:
true_vals = user_agg[true_col].values
pred_vals = user_agg[pred_col].values
# Handle NaN/Inf
mask = np.isfinite(true_vals) & np.isfinite(pred_vals)
if np.sum(mask) > 1:
true_vals_clean = true_vals[mask]
pred_vals_clean = pred_vals[mask]
if np.var(true_vals_clean) > 1e-10 and np.var(pred_vals_clean) > 1e-10:
corr, _ = pearsonr(true_vals_clean, pred_vals_clean)
mse = np.mean((true_vals_clean - pred_vals_clean) ** 2)
correlations[outcome] = corr if np.isfinite(corr) else 0.0
mse_results[outcome] = mse if np.isfinite(mse) else float('inf')
else:
correlations[outcome] = 0.0
mse_results[outcome] = float('inf')
else:
correlations[outcome] = 0.0
mse_results[outcome] = float('inf')
else:
correlations[outcome] = 0.0
mse_results[outcome] = float('inf')
except Exception as e:
print(f"Error calculating metrics for {outcome}: {e}")
correlations[outcome] = 0.0
mse_results[outcome] = float('inf')
return correlations, mse_results
def predict_qa_pairs(self, model, input_scaler, output_scaler, data, target_outcome_idx=None):
"""Generate QA-level predictions"""
input_cols = [col for col in data.columns if col.startswith("input_")]
output_cols = [col for col in data.columns if col.startswith("output_")]
X = data[input_cols].values
X = np.nan_to_num(X, nan=0.0, posinf=1.0, neginf=-1.0)
X_norm = input_scaler.transform(X)
X_tensor = torch.tensor(X_norm, dtype=torch.float32).to(device)
model.eval()
with torch.no_grad():
predictions_norm = model(X_tensor).cpu().numpy()
# Handle different model types
if target_outcome_idx is not None:
# Singletask model - predictions_norm is shape (n_samples, 1)
predictions = output_scaler.inverse_transform(predictions_norm)
# Create QA predictions dataframe for single outcome - only include target output
target_output_col = f'output_{target_outcome_idx}'
qa_predictions = data[['user_id', 'question_code', target_output_col]].copy()
outcome_name = self.config.outcome_names[target_outcome_idx]
qa_predictions[f'{outcome_name}_pred'] = predictions[:, 0]
else:
# Multitask model - predictions_norm is shape (n_samples, n_outcomes)
predictions = output_scaler.inverse_transform(predictions_norm)
# Create QA predictions dataframe
qa_predictions = data[['user_id', 'question_code'] + output_cols].copy()
for i, outcome in enumerate(self.config.outcome_names[:self.config.num_outputs]):
qa_predictions[f'{outcome}_pred'] = predictions[:, i]
return qa_predictions
class Analysis:
def __init__(self, question_strategy='all_questions', model_type='both'):
self.config = Config()
self.config.question_strategy = question_strategy
self.config.model_type = model_type
self.processor = DataProcessor(self.config)
self.trainer = Trainer(self.config)
def get_fold_split(self, n_folds):
"""
Calculate fold distribution: train folds + dev folds + 1 test fold
Ensure at least 1 fold for test, and train/dev are roughly equal
"""
test_folds = 1
remaining_folds = n_folds - test_folds
# Split remaining folds roughly equally between train and dev
train_folds = remaining_folds // 2
dev_folds = remaining_folds - train_folds
return train_folds, dev_folds, test_folds
def run_multitask(self, input_data, output_data, question_data, questions_mapping):
"""Run multitask model training"""
print("="*50)
print("RUNNING MULTITASK MODEL")
print("="*50)
merged_data = self.processor.prepare_data(input_data, output_data, question_data, questions_mapping)
cv_folds = self.processor.create_cv_folds(merged_data)
# Calculate fold distribution
train_folds_count, dev_folds_count, test_folds_count = self.get_fold_split(self.config.n_folds)
# Store all results
all_results = []
all_correlations = []
all_mse = []
all_best_params = []
all_tuning_results = []
print(f"\nStarting {self.config.n_folds}-fold cross validation...")
print(f"Model configuration: {self.config.get_config_name()}")
print(f"Fold structure: {train_folds_count} train + {dev_folds_count} dev + {test_folds_count} test")
# Run cross validation with proper fold structure
for test_fold_idx in range(self.config.n_folds):
print(f"\nFold {test_fold_idx + 1}/{self.config.n_folds}")
print("=" * 40)
# Test fold (1 fold)
test_users = cv_folds[test_fold_idx]
test_data = merged_data[merged_data['user_id'].isin(test_users)]
# Split remaining folds into train + dev
remaining_folds = [i for i in range(self.config.n_folds) if i != test_fold_idx]
# First train_folds_count folds for training
train_fold_indices = remaining_folds[:train_folds_count]
train_folds_data = []
train_users = []
for idx in train_fold_indices:
fold_users = cv_folds[idx]
train_users.extend(fold_users)
fold_data = merged_data[merged_data['user_id'].isin(fold_users)]
train_folds_data.append(fold_data)
# Remaining folds for dev
dev_fold_indices = remaining_folds[train_folds_count:]
dev_folds_data = []
dev_users = []
for idx in dev_fold_indices:
fold_users = cv_folds[idx]
dev_users.extend(fold_users)
fold_data = merged_data[merged_data['user_id'].isin(fold_users)]
dev_folds_data.append(fold_data)
print(f"Train folds: {train_fold_indices} ({len(train_users)} users)")
print(f"Dev folds: {dev_fold_indices} ({len(dev_users)} users)")
print(f"Test fold: {test_fold_idx} ({len(test_users)} users)")
# Train multitask model
(model, input_scaler, output_scaler, best_params,
user_correlations, user_mse, tuning_results) = self.trainer.train_model_with_hyperparams(
train_folds_data, dev_folds_data, test_data
)
all_correlations.append(user_correlations)
all_mse.append(user_mse)
all_best_params.append(best_params)
all_tuning_results.append(tuning_results)
# Generate predictions for all datasets
# Train data
train_combined = pd.concat(train_folds_data, ignore_index=True)
train_qa_predictions = self.trainer.predict_qa_pairs(
model, input_scaler, output_scaler, train_combined
)
train_qa_predictions['fold'] = test_fold_idx
train_qa_predictions['dataset'] = 'train'
all_results.append(train_qa_predictions)
# Dev data
dev_combined = pd.concat(dev_folds_data, ignore_index=True)
dev_qa_predictions = self.trainer.predict_qa_pairs(
model, input_scaler, output_scaler, dev_combined
)
dev_qa_predictions['fold'] = test_fold_idx
dev_qa_predictions['dataset'] = 'dev'
all_results.append(dev_qa_predictions)
# Test data
test_qa_predictions = self.trainer.predict_qa_pairs(
model, input_scaler, output_scaler, test_data
)
test_qa_predictions['fold'] = test_fold_idx
test_qa_predictions['dataset'] = 'test'
all_results.append(test_qa_predictions)
# Print fold results
print(f"\nFold {test_fold_idx + 1} Results:")
print(f"Best hyperparameters: {best_params}")
print("User-level correlations:")
for outcome, corr in user_correlations.items():
print(f" {outcome}: {corr:.4f}")
return all_results, all_correlations, all_mse, all_best_params, all_tuning_results
def run_singletask(self, input_data, output_data, question_data, questions_mapping):
"""Run singletask models for all outcomes"""
print("="*50)
print("RUNNING SINGLETASK MODELS")
print("="*50)
all_results = {}
all_correlations = {}
all_mse = {}
all_best_params = {}
all_tuning_results = {}
# Train a separate model for each selected outcome
selected_outcomes = self.config.singletask_outcomes
outcome_to_idx = {name: i for i, name in enumerate(self.config.outcome_names)}
for outcome_name in selected_outcomes:
outcome_idx = outcome_to_idx[outcome_name]
print(f"\n{'='*60}")
print(f"TRAINING SINGLETASK MODEL FOR {outcome_name} (Index {outcome_idx})")
print(f"{'='*60}")
# Prepare data specific to this outcome
merged_data = self.processor.prepare_data(
input_data, output_data, question_data, questions_mapping, outcome_name
)
cv_folds = self.processor.create_cv_folds(merged_data)
# Calculate fold distribution
train_folds_count, dev_folds_count, test_folds_count = self.get_fold_split(self.config.n_folds)
# Store results for this outcome
outcome_results = []
outcome_correlations = []
outcome_mse = []
outcome_best_params = []
outcome_tuning_results = []
print(f"Starting {self.config.n_folds}-fold cross validation for {outcome_name}...")
print(f"Fold structure: {train_folds_count} train + {dev_folds_count} dev + {test_folds_count} test")
# Run cross validation
for test_fold_idx in range(self.config.n_folds):
print(f"\nFold {test_fold_idx + 1}/{self.config.n_folds} - {outcome_name}")
print("-" * 40)
# Test fold (1 fold)
test_users = cv_folds[test_fold_idx]
test_data = merged_data[merged_data['user_id'].isin(test_users)]
# Split remaining folds into train + dev
remaining_folds = [i for i in range(self.config.n_folds) if i != test_fold_idx]
# First train_folds_count folds for training
train_fold_indices = remaining_folds[:train_folds_count]
train_folds_data = []
train_users = []
for idx in train_fold_indices:
fold_users = cv_folds[idx]
train_users.extend(fold_users)
fold_data = merged_data[merged_data['user_id'].isin(fold_users)]
train_folds_data.append(fold_data)
# Remaining folds for dev
dev_fold_indices = remaining_folds[train_folds_count:]
dev_folds_data = []
dev_users = []
for idx in dev_fold_indices:
fold_users = cv_folds[idx]
dev_users.extend(fold_users)
fold_data = merged_data[merged_data['user_id'].isin(fold_users)]
dev_folds_data.append(fold_data)
print(f"Train folds: {train_fold_indices} ({len(train_users)} users)")
print(f"Dev folds: {dev_fold_indices} ({len(dev_users)} users)")
print(f"Test fold: {test_fold_idx} ({len(test_users)} users)")
# Train singletask model for this outcome
(model, input_scaler, output_scaler, best_params,
user_correlations, user_mse, tuning_results) = self.trainer.train_model_with_hyperparams(
train_folds_data, dev_folds_data, test_data, outcome_idx
)
outcome_correlations.append(user_correlations)
outcome_mse.append(user_mse)
outcome_best_params.append(best_params)
outcome_tuning_results.append(tuning_results)
# Generate predictions for all datasets
# Train data
train_combined = pd.concat(train_folds_data, ignore_index=True)
train_qa_predictions = self.trainer.predict_qa_pairs(
model, input_scaler, output_scaler, train_combined, outcome_idx
)
train_qa_predictions['fold'] = test_fold_idx
train_qa_predictions['dataset'] = 'train'
outcome_results.append(train_qa_predictions)
# Dev data
dev_combined = pd.concat(dev_folds_data, ignore_index=True)
dev_qa_predictions = self.trainer.predict_qa_pairs(
model, input_scaler, output_scaler, dev_combined, outcome_idx
)
dev_qa_predictions['fold'] = test_fold_idx
dev_qa_predictions['dataset'] = 'dev'
outcome_results.append(dev_qa_predictions)
# Test data
test_qa_predictions = self.trainer.predict_qa_pairs(
model, input_scaler, output_scaler, test_data, outcome_idx
)
test_qa_predictions['fold'] = test_fold_idx
test_qa_predictions['dataset'] = 'test'
outcome_results.append(test_qa_predictions)
# Print fold results
print(f"\nFold {test_fold_idx + 1} Results for {outcome_name}:")
print(f"Best hyperparameters: {best_params}")
if outcome_name in user_correlations:
print(f"User-level correlation: {user_correlations[outcome_name]:.4f}")
# Store results for this outcome
all_results[outcome_name] = outcome_results
all_correlations[outcome_name] = outcome_correlations
all_mse[outcome_name] = outcome_mse
all_best_params[outcome_name] = outcome_best_params
all_tuning_results[outcome_name] = outcome_tuning_results
return all_results, all_correlations, all_mse, all_best_params, all_tuning_results
def run(self):
print("Loading data...")
torch.manual_seed(self.config.random_seed)
np.random.seed(self.config.random_seed)
random.seed(self.config.random_seed)
input_data, output_data, question_data, questions_mapping = self.processor.load_data()
all_results = []
original_model_type = self.config.model_type
if original_model_type in ['multitask', 'both']:
print("\n" + "="*80)
print("MULTITASK MODEL TRAINING")
print("="*80)
multitask_results, multitask_correlations, multitask_mse, multitask_best_params, multitask_tuning_results = self.run_multitask(
input_data, output_data, question_data, questions_mapping
)
# Analyze and save multitask results
self.config.model_type = 'multitask' # Temporarily set for saving
self.analyze_performance(multitask_correlations, multitask_mse, multitask_best_params)
self.save_results(multitask_results, multitask_best_params, multitask_tuning_results,
*self.get_fold_split(self.config.n_folds))
all_results.extend(multitask_results)
if original_model_type in ['singletask', 'both']:
print("\n" + "="*80)
print("SINGLETASK MODELS TRAINING")
print("="*80)
singletask_results, singletask_correlations, singletask_mse, singletask_best_params, singletask_tuning_results = self.run_singletask(
input_data, output_data, question_data, questions_mapping
)
# Analyze and save singletask results for each outcome
for outcome_name in self.config.singletask_outcomes:
self.config.model_type = 'singletask' # Temporarily set for saving
self.analyze_performance_singletask(
singletask_correlations[outcome_name],
singletask_mse[outcome_name],
singletask_best_params[outcome_name],
outcome_name
)
self.save_results_singletask(
singletask_results[outcome_name],
singletask_best_params[outcome_name],
singletask_tuning_results[outcome_name],
outcome_name,
*self.get_fold_split(self.config.n_folds)
)
self.config.model_type = original_model_type # restore original value
return all_results
def analyze_performance_singletask(self, fold_correlations, fold_mse, fold_params, outcome_name):
"""Analyze performance for singletask model"""
print(f"\nPerformance Analysis for {outcome_name} (Singletask)")
print("-" * 50)
# Extract correlations and MSE values
corr_values = [fold_corr[outcome_name] for fold_corr in fold_correlations if outcome_name in fold_corr]
mse_values = [fm[outcome_name] for fm in fold_mse if outcome_name in fm]
if corr_values:
correlation_stats = {
'mean': np.mean(corr_values),
'std': np.std(corr_values),
'values': corr_values
}
mse_stats = {
'mean': np.mean(mse_values),
'std': np.std(mse_values),
'values': mse_values
}
print(f"Correlation: {correlation_stats['mean']:.4f} ± {correlation_stats['std']:.4f}")
print(f"MSE: {mse_stats['mean']:.4f} ± {mse_stats['std']:.4f}")
# Analyze hyperparameter choices
hyperparameter_analysis = {}
for param_key in fold_params[0].keys():
param_values = [params[param_key] for params in fold_params]
unique_values, counts = np.unique(param_values, return_counts=True)
hyperparameter_analysis[param_key] = {}
print(f"Hyperparameter {param_key}:")
for val, count in zip(unique_values, counts):
hyperparameter_analysis[param_key][str(val)] = int(count)
print(f" {val}: {count} times ({count/len(fold_params)*100:.1f}%)")