-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_FeatureExtractionInpatient_JustLinReg.py
executable file
·1394 lines (976 loc) · 50.4 KB
/
main_FeatureExtractionInpatient_JustLinReg.py
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
# Assumes you have already run main_FeatureExtractionInpatient.py and you just need linear regression!
PAT_NOW = "S23_199"
PAT_SHORT_NAME = "S_199"
print(f'[LOG] Patient Now: {PAT_NOW}')
MOOD_TRACKING_SHEET_PATH = f'/home/jgopal/NAS/Analysis/AudioFacialEEG/Behavioral Labeling/Mood_Tracking.xlsx'
BEHAVIORAL_LABELS_SHEET_PATH = f'/home/jgopal/NAS/Analysis/AudioFacialEEG/Behavioral Labeling/Behavior_Labeling.xlsx'
VIDEO_TIMESTAMPS_SHEET_PATH = f'/home/jgopal/NAS/Analysis/AudioFacialEEG/Behavioral Labeling/videoDateTimes/VideoDatetimes{PAT_SHORT_NAME[1:]}.xlsx'
OPENFACE_OUTPUT_DIRECTORY = f'/home/jgopal/NAS/Analysis/outputs_OpenFace/{PAT_NOW}/'
COMBINED_OUTPUT_DIRECTORY = f'/home/jgopal/NAS/Analysis/outputs_Combined/{PAT_NOW}/'
RUNTIME_VAR_PATH = '/home/jgopal/NAS/Analysis/AudioFacialEEG/Runtime_Vars/'
RESULTS_PATH_BASE = f'/home/jgopal/NAS/Analysis/AudioFacialEEG/Results/{PAT_SHORT_NAME}/'
FEATURE_VIS_PATH = f'/home/jgopal/NAS/Analysis/AudioFacialEEG/Feature_Visualization/{PAT_SHORT_NAME}/'
FEATURE_LABEL_PATH = '/home/jgopal/NAS/Analysis/AudioFacialEEG/Feature_Labels/'
QC_PATH = '/home/jgopal/NAS/Analysis/AudioFacialEEG/Quality_Control/'
EMO_FEATURE_SETTING = 2
# 0 - Our Custom AU --> Emotions, with all emotions
# 1 - Our Custom AU --> Emotions, with just OpenDBM's emotions
# 2 - OpenDBM's AU--> Emotions
STATS_FEATURE_SETTING = 3
# 0 - Our new features (including autocorrelation, kurtosis, etc.)
# 1 - Our new features, excluding extras like autocorrelation and kurtosis
# 2 - Just pres_pct
# 3 - Our new features, excluding extras. Do NOT threshold AUs before computing metrics. HSE gets 5 event features. OGAU gets num events and presence percent.
NORMALIZE_DATA = 0
# 0 - No time series normalization
# 1 - Yes time series normalization (for each time window)
import pandas as pd
import numpy as np
import os
import warnings
import pandas as pd
# Ignore all warnings
pd.options.mode.chained_assignment = None
pd.set_option('mode.chained_assignment', None)
warnings.filterwarnings('ignore')
# SAVE VARIABLES
import pickle
def get_var_name(our_variable):
namespace = globals()
for name, obj in namespace.items():
if obj is our_variable:
return name
return None
# Save the dictionary to a file using pickle
def save_var(our_variable, RUNTIME_VAR_PATH=RUNTIME_VAR_PATH, forced_name=None):
if forced_name is None:
name_now = get_var_name(our_variable)
else:
name_now = forced_name
# Construct the full path including the file name
full_path = os.path.join(RUNTIME_VAR_PATH, f'{name_now}.pkl')
# Ensure the directory exists, including any nested folders in name_now
os.makedirs(os.path.dirname(full_path), exist_ok=True)
# Save the variable
with open(full_path, 'wb') as file:
pickle.dump(our_variable, file)
def load_var(variable_name, RUNTIME_VAR_PATH=RUNTIME_VAR_PATH):
# Load from the file
with open(RUNTIME_VAR_PATH + f'{variable_name}.pkl', 'rb') as file:
return pickle.load(file)
print('[LOG] Starter Functions Defined')
df = pd.read_excel(MOOD_TRACKING_SHEET_PATH, sheet_name=f'{PAT_SHORT_NAME}')
## Preprocess the mood tracking sheet
# Replace the P_number mood headers with just the mood
# df.columns = df.columns.str.replace('P[0-9]+ ', '')
# Properly deal with the missing values
df = df.replace('', np.nan).replace(' ', np.nan).fillna(value=np.nan)
df_moodTracking = df
df_moodTracking = df_moodTracking.drop(columns=['Notes'], errors='ignore')
df_moodTracking['Datetime'] = pd.to_datetime(df_moodTracking['Datetime']).dt.strftime('%-m/%-d/%Y %H:%M:%S')
import numpy as np
# create lists to hold the positive and negative affect items
pos_items = [1, 3, 5, 9, 10, 12, 14, 16, 17, 19]
neg_items = [2, 4, 6, 7, 8, 11, 13, 15, 18, 20]
# get all columns that start with 'P' and split them into pos and neg groups
P_cols = [col for col in df_moodTracking.columns if col.startswith('P') and not(col.startswith('Pain')) and not(col.startswith('PANAS')) and not(col.startswith('Positive'))]
pos_cols = [col for col in P_cols if int(col[1:3]) in pos_items]
neg_cols = [col for col in P_cols if int(col[1:3]) in neg_items]
# create new columns for the summed scores
df_moodTracking['Positive Affect Score'] = df_moodTracking[pos_cols].fillna(0).astype(int).sum(axis=1, skipna=True)
df_moodTracking['Negative Affect Score'] = df_moodTracking[neg_cols].fillna(0).astype(int).sum(axis=1, skipna=True)
df_moodTracking['Overall Affect Score'] = df_moodTracking[['Positive Affect Score', 'Negative Affect Score']].fillna(0).astype(int).sum(axis=1, skipna=True)
# replace 0s with NaNs in columns 'Positive Affect Score' and 'Negative Affect Score'
df_moodTracking[['Positive Affect Score', 'Negative Affect Score', 'Overall Affect Score']] = \
df_moodTracking[['Positive Affect Score', 'Negative Affect Score', 'Overall Affect Score']].replace(0, np.nan)
# drop the original P columns used to create the scores
df_moodTracking.drop(columns=pos_cols + neg_cols, inplace=True)
from sklearn.preprocessing import MinMaxScaler, StandardScaler
import numpy as np
def normalize_columns(df, method=1):
# Create a copy of the DataFrame
normalized_df = df.copy()
# Get the column names excluding 'Datetime'
columns_to_normalize = [col for col in normalized_df.columns if col != 'Datetime']
if method == 1:
# No scaling or normalization
pass
elif method == 2:
# MinMax scaling to range 0 to 10
scaler = MinMaxScaler(feature_range=(0, 10))
normalized_df[columns_to_normalize] = scaler.fit_transform(normalized_df[columns_to_normalize])
elif method == 3:
# MinMax scaling to range 0 to 1
scaler = MinMaxScaler(feature_range=(0, 1))
normalized_df[columns_to_normalize] = scaler.fit_transform(normalized_df[columns_to_normalize])
elif method == 4:
# Log scaling
normalized_df[columns_to_normalize] = normalized_df[columns_to_normalize].astype(float)
normalized_df[columns_to_normalize] = np.log1p(normalized_df[columns_to_normalize])
elif method == 5:
# Standard normalization (Z-score normalization)
scaler = StandardScaler()
normalized_df[columns_to_normalize] = scaler.fit_transform(normalized_df[columns_to_normalize])
else:
raise ValueError("Invalid method. Choose a value between 1 and 5.")
return normalized_df
df_moodTracking = normalize_columns(df_moodTracking, method=2)
if PAT_SHORT_NAME == 'S_214':
df_moodTracking = df_moodTracking.drop(1).reset_index(drop=True)
df_videoTimestamps = pd.read_excel(VIDEO_TIMESTAMPS_SHEET_PATH, sheet_name=f'VideoDatetimes_{PAT_SHORT_NAME.split("_")[-1]}')
df_videoTimestamps['Filename'] = df_videoTimestamps['Filename'].str.replace('.m2t', '')
if PAT_SHORT_NAME == 'S_199':
# There's no H01 video, so let's drop that filename
df_videoTimestamps = df_videoTimestamps.drop(211)
print('[LOG] Labels Processed')
# Check for any missing videos!
def print_difference(list1, list2):
for item in list1:
if item not in list2:
print(item)
filenames_master_list = list(df_videoTimestamps['Filename'].values)
filenames_we_have = [i[:-4] for i in os.listdir(COMBINED_OUTPUT_DIRECTORY)]
print_difference(filenames_master_list, filenames_we_have)
# LOAD VARIABLES - EMOTION & AFFECT
openface_dict_list_dict = load_var(f'openface_dict_list_dict_{PAT_SHORT_NAME}')
opengraphau_dict_list_dict = load_var(f'opengraphau_dict_list_dict_{PAT_SHORT_NAME}')
hsemotion_dict_list_dict = load_var(f'hsemotion_dict_list_dict_{PAT_SHORT_NAME}')
# LOAD VARIABLES - EMOTION & AFFECT
ogauhsemotion_dict_list_dict = load_var(f'ogauhsemotion_dict_list_dict_{PAT_SHORT_NAME}')
print('[LOG] Loaded Processed Feature Vectors')
def flatten_dataframes_dict(dataframes_list):
# Initialize an empty dictionary to store the flattened data for each key
flattened_data_dict = {}
# Define the columns to ignore
ignore_columns = ['success', 'timestamp', 'AU', 'emotion']
for dataframes_dict in dataframes_list:
for key, df in dataframes_dict.items():
# Filter out the columns to be ignored
filtered_df = df.drop(columns=[col for col in ignore_columns if col in df.columns])
# Flatten the data by converting each DataFrame into a 1D array
flattened_array = filtered_df.select_dtypes(include=[np.number, int, float, complex, \
pd.Int64Dtype(), pd.Float64Dtype(), pd.Int32Dtype(), \
pd.Float32Dtype()]).values.flatten()
# Convert the flattened array to NumPy array and store it in the dictionary
if key in flattened_data_dict:
flattened_data_dict[key] = np.concatenate((flattened_data_dict[key], flattened_array))
else:
flattened_data_dict[key] = np.array(flattened_array)
return flattened_data_dict
openface_vectors_dict = {}
for key, openface_dict_list_now in openface_dict_list_dict.items():
openface_vectors_dict[key] = flatten_dataframes_dict(openface_dict_list_now)
opengraphau_vectors_dict = {}
for key, opengraphau_dict_list_now in opengraphau_dict_list_dict.items():
opengraphau_vectors_dict[key] = flatten_dataframes_dict(opengraphau_dict_list_now)
hsemotion_vectors_dict = {}
for key, hsemotion_dict_list_now in hsemotion_dict_list_dict.items():
hsemotion_vectors_dict[key] = flatten_dataframes_dict(hsemotion_dict_list_now)
ogauhsemotion_vectors_dict = {}
for key, ogauhsemotion_dict_list_now in ogauhsemotion_dict_list_dict.items():
ogauhsemotion_vectors_dict[key] = flatten_dataframes_dict(ogauhsemotion_dict_list_now)
def ts_to_str(timestamp):
return timestamp.strftime('%-m/%-d/%Y %H:%M:%S')
def str_to_ts(string_now):
temp_var = pd.to_datetime(pd.to_datetime(string_now).strftime('%d-%b-%Y %H:%M:%S'))
return pd.Timestamp(temp_var)
def ts_to_str_save(timestamp):
# shorter version bc xlsxwriter sheet name char limit
return timestamp.strftime('%-m_%-d %H_%M')
## Save our vectors to excel sheets!
def get_dict_name(dictionary):
namespace = globals()
for name, obj in namespace.items():
if isinstance(obj, dict) and obj is dictionary:
return name
return None
def save_dicts_to_excel(dict_list, output_path):
# Create an Excel writer object
writer = pd.ExcelWriter(output_path, engine='xlsxwriter')
# Iterate over the keys in the dictionaries
for key in dict_list[0].keys():
# Write each dataframe to a separate sheet with the corresponding key as the sheet name
for enum, dict_now in enumerate(dict_list):
name_var = f'Matrix_{enum}'
sheet_name_starter = f'{ts_to_str_save(key)}_{name_var}'
dict_now[key].to_excel(writer, sheet_name=sheet_name_starter[:31])
# Save the Excel file
writer.close()
return
os.makedirs(FEATURE_VIS_PATH, exist_ok=True)
for i in opengraphau_dict_list_dict.keys():
save_dicts_to_excel(openface_dict_list_dict[i], FEATURE_VIS_PATH + f'openface_{PAT_SHORT_NAME}_{int(i)}_minutes.xlsx')
save_dicts_to_excel(opengraphau_dict_list_dict[i], FEATURE_VIS_PATH + f'opengraphau_{PAT_SHORT_NAME}_{int(i)}_minutes.xlsx')
save_dicts_to_excel(hsemotion_dict_list_dict[i], FEATURE_VIS_PATH + f'hsemotion_{PAT_SHORT_NAME}_{int(i)}_minutes.xlsx')
save_dicts_to_excel(ogauhsemotion_dict_list_dict[i], FEATURE_VIS_PATH + f'ogauhse_{PAT_SHORT_NAME}_{int(i)}_minutes.xlsx')
print('[LOG] Feature Extraction Complete')
import random
def set_seed(x=5):
np.random.seed(x)
random.seed(x)
set_seed()
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from scipy.stats import pearsonr
import seaborn as sns
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.model_selection import LeaveOneOut, GridSearchCV
# def linRegOneMetric(vectors_dict, y, randShuffle=False, do_lasso=False, do_ridge=False, alpha=1.0):
# # runs simple linear regression via one-left-out
# # vectors_dict -- dictionary mapping time radius (in minutes) to features
# # y -- a numpy array with labels (self-reported metrics)
# # randShuffle -- do we shuffle the self-report labels?
# # if do_lasso, does lasso regression
# # if do_ridge, does ridge regression. Overrides do_lasso
# # alpha - this is the weighting of either lasso or ridge
# # returns a dictionary with several results:
# # scores -- dictionary mapping each time radius to list of MSEs from each one-left-out
# # preds -- dictionary mapping each time radius to a list of each one-left-out model's prediction
# # y -- returns y again for convenience
# # models -- dictionary mapping each time radius to a list of each one-left-out trained model (simple linear regression)
# scores = {}
# preds = {}
# models = {}
# if randShuffle:
# y_using = np.random.permutation(y)
# else:
# y_using = y
# for i in vectors_dict.keys():
# model = LinearRegression()
# if do_lasso:
# model = Lasso(alpha=alpha)
# if do_ridge:
# model = Ridge(alpha=alpha)
# # Compute MSEs via scikitlearn cross_val_score
# scores_temp = cross_val_score(model, vectors_dict[i], y_using, cv=vectors_dict[i].shape[0], scoring='neg_mean_squared_error')
# scores[i] = -1 * scores_temp
# # Predictions via cross_val_predict
# preds[i] = cross_val_predict(model, vectors_dict[i], y_using, cv=vectors_dict[i].shape[0])
# # Now we need to iterate through and actually save the models themselves, since cross_val_score doesn't let us do that!
# models_i_building = []
# for test_index in range(vectors_dict[i].shape[0]):
# X_train = np.delete(vectors_dict[i], test_index, axis=0)
# y_train = np.delete(y_using, test_index, axis=0)
# model = LinearRegression()
# if do_lasso:
# model = Lasso(alpha=alpha)
# if do_ridge:
# model = Ridge(alpha=alpha)
# model.fit(X_train, y_train)
# models_i_building.append(model)
# models[i] = models_i_building
# return scores, preds, y, models
def linRegOneMetric(vectors_dict, y, randShuffle=False, do_lasso=False, do_ridge=False, alpha=1.0, ALPHAS_FOR_SEARCH=None, num_permutations=0):
"""
Runs regression (LASSO/Ridge/Linear) with optional nested alpha search and permutation testing.
Args:
vectors_dict (dict): Dictionary mapping time radius to feature arrays (numpy arrays).
y (np.array): Labels (self-reported metrics).
randShuffle (bool): Shuffle labels for random testing (default False).
do_lasso (bool): Use LASSO regression (default False).
do_ridge (bool): Use Ridge regression (default False).
alpha (float): Regularization strength (default 1.0).
ALPHAS_FOR_SEARCH (list or np.array): List of alphas to search in nested cross-validation (optional).
num_permutations (int): Number of permutations for testing (default 0).
Returns:
dict: scores (MSE for each sample for each time radius),
dict: preds (predicted values for each sample for each time radius),
np.array: y (original or shuffled labels, based on randShuffle),
dict: models (trained model objects for each time radius).
"""
if randShuffle:
y = np.random.permutation(y)
# Default alpha search grid if not provided
if ALPHAS_FOR_SEARCH is None:
ALPHAS_FOR_SEARCH = np.arange(0.1, 5.0, 0.2)
scores = {}
preds = {}
models = {}
for time_radius, X in vectors_dict.items():
# Determine the model
if do_lasso:
model = Lasso()
param_grid = {'alpha': ALPHAS_FOR_SEARCH}
elif do_ridge:
model = Ridge()
param_grid = {'alpha': ALPHAS_FOR_SEARCH}
else:
raise ValueError("Only LASSO or Ridge regression is supported for nested alpha search.")
# Nested cross-validation for alpha search using LOOCV
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=LeaveOneOut(), scoring='neg_mean_squared_error')
grid_search.fit(X, y)
# Best model after alpha search
best_model = grid_search.best_estimator_
best_alpha = grid_search.best_params_['alpha']
# Predictions using LOOCV
preds[time_radius] = cross_val_predict(best_model, X, y, cv=LeaveOneOut())
# MSE scores using LOOCV
scores[time_radius] = -1 * cross_val_score(best_model, X, y, cv=LeaveOneOut(), scoring='neg_mean_squared_error')
# Save the trained model (one per time radius)
models[time_radius] = best_model
print(f"Time Radius: {time_radius}, Best Alpha: {best_alpha}")
# Optional: Permutation testing
if num_permutations > 0:
print(f"Running {num_permutations} permutations for statistical testing...")
permuted_r_list = []
for _ in range(num_permutations):
y_shuffled = np.random.permutation(y)
permuted_preds = cross_val_predict(best_model, X, y_shuffled, cv=LeaveOneOut())
permuted_r, _ = pearsonr(y_shuffled, permuted_preds)
permuted_r_list.append(permuted_r)
# Calculate actual Pearson's R
actual_r, _ = pearsonr(y, preds[time_radius])
# P-value computation
p_value = np.mean([abs(r) >= abs(actual_r) for r in permuted_r_list])
print(f"Permutation Test Pearson's R: {actual_r:.4f}, P-value: {p_value:.4f}")
return scores, preds, y, models
def plot_predictions(y, y_pred, randShuffleR=None, ax=None, time_rad=None, metric=None):
# Makes one scatterplot with Pearson's R and p value on it
# give it the randShuffle Pearson's R
# if you want to display that on the plot
# Compute Pearson's R
pearson_corr, p_val = pearsonr(y, y_pred)
# Create the scatter plot on the specified axes
if ax is None:
ax_original = None
fig, ax = plt.subplots()
# adjust fonts!
text_font = 16
else:
ax_original = ax
text_font = 16
ax.scatter(y, y_pred, label='Predicted vs. True', s=24)
# Add the correlation coefficient and p-value on the plot
ax.text(0.05, 0.90, f'Pearson\'s R: {pearson_corr:.2f}', transform=ax.transAxes, fontsize=text_font)
ax.text(0.05, 0.80, f'P Value: {p_val:.2f}', transform=ax.transAxes, fontsize=text_font)
if not(randShuffleR is None):
ax.text(0.05, 0.70, f'Random Shuffle R: {randShuffleR:.2f}', transform=ax.transAxes, fontsize=text_font)
# Set labels and title
ax.set_xlabel('Self-Reported Scores', fontsize=17)
ax.set_ylabel('Predicted Scores', fontsize=17)
if metric is None:
title_starter = 'Predicted vs. True'
else:
title_starter = metric
if time_rad is None:
ax.set_title(f'{title_starter} Scores', fontsize=17)
else:
num_hrs = int(time_rad) / 60
if num_hrs > 1:
ax.set_title(f'{title_starter}, Time Window = {num_hrs} Hours', fontsize=15)
else:
ax.set_title(f'{title_starter}, Time Window = {num_hrs} Hour', fontsize=15)
# Add the line of best fit
sns.regplot(x=y, y=y_pred, ax=ax, line_kws={'color': 'red', 'linestyle': '--'}, label='Line of Best Fit')
# Add the shaded region for the 95% confidence interval
#sns.regplot(x=y, y=y_pred, ax=ax, scatter=False, ci=95, color='gray', label='95% Confidence Interval')
# Adjust the font size of the tick labels on the axes
ax.tick_params(axis='both', labelsize=18)
ax.set_adjustable('box')
#set aspect ratio to 1
ratio = 1.0
x_left, x_right = ax.get_xlim()
y_low, y_high = ax.get_ylim()
ax.set_aspect(abs((x_right-x_left)/(y_low-y_high))*ratio)
if ax_original is None:
#plt.show()
return pearson_corr, p_val, fig
else:
return pearson_corr, p_val
def plot_scatterplots(preds_dict, y, overall_title, savepath, randShuffleR=None):
plt.rcParams['lines.markersize'] = 6
subplot_title_font = 16
full_title_font = 24
num_plots = len(list(preds_dict.keys()))
num_cols = 4
num_rows = (num_plots + num_cols - 1) // num_cols
r_list = []
p_list = []
# Calculate the desired figure size for larger plot
figsize = (28, 12)
# Create subplots with auto aspect ratio
fig, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
#axes.set_adjustable('box')
if num_rows == 1:
axes = axes.reshape((1, num_cols))
# Flatten the axes array if necessary
if num_plots == 1:
fig, axes = plt.subplots(1, 1, figsize=figsize)
axes = np.array([axes]).reshape(1, 1)
# Loop through the dictionaries
for i, (key, y_preds) in enumerate(preds_dict.items()):
y_list = np.array(y).astype(float)
y_pred = np.array(y_preds).astype(float)
#y_pred = np.array([i[0] for i in y_pred])
# Get the subplot coordinates
row = i // num_cols
col = i % num_cols
# Plot predictions on the subplot
if randShuffleR is None:
pearson_corr, p_val = plot_predictions(y_list, y_pred, randShuffleR=randShuffleR, ax=axes[row, col])
else:
pearson_corr, p_val = plot_predictions(y_list, y_pred, randShuffleR=randShuffleR[i], ax=axes[row, col])
r_list.append(pearson_corr)
p_list.append(p_val)
num_hrs = int(key) / 60
if num_hrs > 1:
axes[row, col].set_title(f'Time Window = {num_hrs} Hours', fontsize=subplot_title_font)
else:
axes[row, col].set_title(f'Time Window = {num_hrs} Hour', fontsize=subplot_title_font)
#axes[row, col].set_aspect('equal')
# Remove x-axis and y-axis labels from subplots
axes[row, col].set_xlabel('')
axes[row, col].set_ylabel('')
#axes[row, col].set_adjustable('box')
# Add overall title
fig.suptitle(overall_title, fontsize=30, y=1)
# Set shared x-axis and y-axis labels
fig.text(0.5, 0.00, 'Self-Reported Scores', ha='center', fontsize=full_title_font)
fig.text(-0.01, 0.5, 'Predicted Scores', va='center', rotation='vertical', fontsize=full_title_font)
# Adjust spacing and layout
fig.tight_layout()
plt.savefig(savepath, bbox_inches='tight')
#plt.show()
return r_list, p_list, fig
def make_mse_boxplot(scores, metric, savepath, ax=None, method_now='OpenFace'):
# scores -- dictionary that maps time radius (mins) to list of MSEs from one-left-out
# metric - e.g. Mood or Anxiety
# Combine the data into a single array
data = [MSE_list for MSE_list in list(scores.values())]
# Set the font sizes
plt.rcParams.update({'font.size': 15})
if ax is None:
fig, ax = plt.subplots()
else:
fig = None
# Create a box plot of the data
labels_now = [f'{int(key) / 60}' for key in scores.keys()]
ax.boxplot(data, labels=labels_now, showmeans=True, meanprops={'marker': 'o', 'markerfacecolor': 'red', 'markersize': 10})
# Determine the highest 75th percentile value among the four entries
max_value = np.max([np.percentile(entry, 75) for entry in data])
# Set the y-axis range conditionally
if max_value > 100:
ax.set_ylim(0, 100)
else:
ax.set_ylim(0, max_value)
# Set the labels and title
ax.set_xlabel('Time Window (Hours)')
ax.set_ylabel('Mean Squared Error')
ax.set_title(f'{metric} Prediction via {method_now}', y=1.1)
plt.xticks(rotation=45)
plt.savefig(savepath, bbox_inches='tight')
# Show the plot if fig is None
if fig is not None:
return fig
def make_r_barplot(r_list, time_radius_list, metric, savepath, ax=None, method_now='OpenFace'):
plt.rcParams.update({'font.size': 15})
x_labels = [f'{int(i) / 60}' for i in time_radius_list]
if ax is None:
original_ax = None
fig, ax = plt.subplots()
else:
original_ax = ax
ax.bar(x_labels, r_list)
# Set the y-axis range
ax.set_ylim(-0.5, 1)
# Set the labels and title
ax.set_xlabel('Time Window (Hours)')
ax.set_ylabel("Pearson's R")
ax.set_title(f'{metric} Prediction via {method_now}', y=1.1)
plt.xticks(rotation=45)
plt.savefig(savepath, bbox_inches='tight')
# Show the plot if ax is None
if original_ax is None:
#plt.show()
return fig
def get_label_from_index(index, spreadsheet_path=FEATURE_LABEL_PATH+'openface_0.5_hours.xlsx'):
if 'experimental' in spreadsheet_path:
matrices = ["Matrix_0", "Matrix_1", "Matrix_2", "Matrix_3", "Matrix_4", "Matrix_5", "Matrix_6", "Matrix_7", "Matrix_8", "Matrix_9"]
row_label_cols = ["AU", "emotion", "emotion", None, None, None, None, None, None, None]
elif 'hsemotion' in spreadsheet_path:
matrices = ["Matrix_0", "Matrix_1"]
row_label_cols = ["emotion", "emotion"]
elif 'opengraphau' in spreadsheet_path:
matrices = ["Matrix_0", "Matrix_1"]
row_label_cols = ["AU", "emotion"]
elif 'openface' in spreadsheet_path:
matrices = ["Matrix_0", "Matrix_1", "Matrix_2", "Matrix_3"]
row_label_cols = ["AU", "emotion", "emotion", None]
elif 'ofauhse' in spreadsheet_path:
matrices = ["Matrix_0", "Matrix_1"]
row_label_cols = ["AU", "emotion"]
elif 'ogauhse' in spreadsheet_path:
matrices = ["Matrix_0", "Matrix_1", "Matrix_2"]
row_label_cols = ["AU", "emotion", "emotion"]
elif 'all' in spreadsheet_path:
matrices = ["Matrix_0", "Matrix_1", "Matrix_2", "Matrix_3", "Matrix_4", "Matrix_5"]
row_label_cols = ["AU", "emotion", "emotion", None, "AU", "emotion"]
else:
print('BUG IN THE CODE! CHECK get_label_from_index')
print('spreadsheet path is ', spreadsheet_path)
index_orig = index
xls = pd.ExcelFile(spreadsheet_path)
for i, matrix in enumerate(matrices):
# Find the sheet ending with the current matrix name
sheet_name = next((s for s in xls.sheet_names if s.endswith(matrix)), None)
if sheet_name is not None:
# Load the sheet into a DataFrame, with the first row as column names
df = pd.read_excel(spreadsheet_path, sheet_name=sheet_name, header=0)
# Get the column labels from the DataFrame
col_labels = [col_now for col_now in df.columns.tolist() if not(col_now in ["AU", "emotion", "Unnamed: 0"])]
if not row_label_cols[i] == 'AU':
if 'emotion' in df.columns:
row_labels = df['emotion'].tolist()
else:
row_labels = df['Unnamed: 0'].tolist()
else:
row_labels = df['AU'].tolist()
# Get the numerical entries in the sheet excluding columns "AU" and "emotion" and "Unnamed: 0"
numerical_entries = df.loc[:, ~df.columns.isin(["AU", "emotion", "Unnamed: 0"])].values.flatten()
numerical_entries = numerical_entries[~pd.isnull(numerical_entries)]
# Check if the index is within the range of numerical entries
if index < len(numerical_entries):
# Find the label corresponding to the index
row_index, col_index = divmod(index, len(col_labels))
if row_label_cols[i] == 'AU':
return f"AU{row_labels[row_index]} {col_labels[col_index]}"
else:
if f'{row_labels[row_index]}' == '0':
return f"{col_labels[col_index]}"
else:
return f"{col_labels[col_index]} {row_labels[row_index]}"
else:
index -= len(numerical_entries)
# Return None if the index is out of range or no suitable sheets found
print('BUG IN THE CODE! INDEX TOO LARGE! CHECK get_label_from_index')
print(f'Original index was {index_orig}')
print('spreadsheet path is ', spreadsheet_path)
return 'Null'
def getTopFeaturesfromWeights(model_list, spreadsheet_path=FEATURE_LABEL_PATH+'openface_2.0_hours.xlsx'):
# given a list of linear regression models,
# returns their top 5 features (on average) from just weights!
coef_array = [model_now.coef_ for model_now in model_list]
coef_avg = np.mean(coef_array, axis=0)
top_5_features = np.argsort(np.abs(coef_avg))[::-1][:5]
top_5_english = [get_label_from_index(feat_ind, spreadsheet_path=spreadsheet_path) for feat_ind in top_5_features]
return top_5_english
def featureAblate(vectors_array, y, do_lasso=False, do_ridge=False):
# runs one-left-out linear regression,
# deleting one feature at a time to determine most important features
# vectors_array -- numpy array of feature vectors
# y -- self-reported labels (e.g. for Mood, Anxiety, or something else)
# if do_lasso, does lasso regression
# if do_ridge, does ridge regression. Overrides do_lasso
# returns scores, prs
# scores -- (n_features, n_timestamps) numpy array of MSEs
# prs -- (n_features,) numpy vector of pearson's R
num_features = vectors_array.shape[1]
num_timestamps = vectors_array.shape[0]
scores = np.zeros((num_features, num_timestamps))
prs = np.zeros((num_features,))
# loop through each feature (for openface, 0 through 144) and delete just that
for deleteNow in range(num_features):
data = np.delete(vectors_array, deleteNow, axis=1)
# make into dictionary to feed into our lin reg function
data = {'placeholder': data}
scores_temp, preds, y, _ = linRegOneMetric(data, y, do_lasso=do_lasso, do_ridge=do_ridge)
scores_temp = scores_temp['placeholder']
preds = preds['placeholder']
# save MSEs
scores[deleteNow, :] = scores_temp
# compute and save Pearson's R
pearson_corr, _ = pearsonr(y, preds)
prs[deleteNow] = pearson_corr
return scores, prs
def featureAblate2D(vectors_array, y):
# runs one-left-out linear regression,
# deleting TWO features at a time to determine most important features
# vectors_array -- numpy array of feature vectors
# y -- self-reported labels (e.g. for Mood, Anxiety, or something else)
# returns prs
# prs -- (n_features, n_features) numpy vector of pearson's R
# Note: ALWAYS index into prs with first index LOWER than second!
num_features = vectors_array.shape[1]
prs = np.zeros((num_features,num_features))
# loop through each feature (for openface, 0 through 144) and delete just that
for deleteNow in range(num_features):
# delete a second one!
for secondDelete in range(deleteNow+1, num_features):
data = np.delete(vectors_array, [deleteNow, secondDelete], axis=1)
# make into dictionary to feed into our lin reg function
data = {'placeholder': data}
_, preds, _, _ = linRegOneMetric(data, y)
preds = preds['placeholder']
# compute and save Pearson's R
pearson_corr, _ = pearsonr(y, preds)
prs[deleteNow, secondDelete] = pearson_corr
return prs
def featureAblate3D(vectors_array, y):
# runs one-left-out linear regression,
# deleting THREE features at a time to determine most important features
# vectors_array -- numpy array of feature vectors
# y -- self-reported labels (e.g. for Mood, Anxiety, or something else)
# returns prs
# prs -- (n_features, n_features, n_features) numpy vector of pearson's R
# Note: ALWAYS index into prs with earlier indices LOWER than subsequent ones.
num_features = vectors_array.shape[1]
prs = np.zeros((num_features, num_features, num_features))
# loop through each feature (for openface, 0 through 144) and delete just that
for deleteNow in range(num_features):
# delete a second one!
for secondDelete in range(deleteNow+1, num_features):
# delete a third one!
for thirdDelete in range(secondDelete+1, num_features):
data = np.delete(vectors_array, [deleteNow, secondDelete, thirdDelete], axis=1)
# make into dictionary to feed into our lin reg function
data = {'placeholder': data}
_, preds, _, _ = linRegOneMetric(data, y)
preds = preds['placeholder']
# compute and save Pearson's R
pearson_corr, _ = pearsonr(y, preds)
prs[deleteNow, secondDelete, thirdDelete] = pearson_corr
return prs
def plotFeatAbMSEs(feat_ab_scores, original_mse_list, metric, time_radius, savepath, top_n=5, ax=None, spreadsheet_path=FEATURE_LABEL_PATH+'openface_2.0_hours.xlsx'):
# takes feat_ab_scores, a numpy array (n_features, n_timestamps) of MSEs
# outputs box and whisker plot of top_n features for the model
# procedure: get the top_n features with lowest mse averaged across timestamps
# make a box and whisker plot with each feature on x axis and MSEs on y axis
# for x axis labels, convert the index of each feature to english label
# by calling get_label_from_index(feat_ind)
# Get the average MSE across timestamps for each feature
avg_mses = np.mean(feat_ab_scores, axis=1)
# avg MSEs minus original_avg_MSE (make it difference!)
avg_mses = avg_mses - np.mean(original_mse_list)
# Get the indices of the top_n features with the highest difference in MSEs from original
top_indices = np.argsort(avg_mses)[-top_n:]
top_indices = top_indices[::-1]
# Get the English labels for the top_n features
top_labels = [get_label_from_index(ind, spreadsheet_path=spreadsheet_path) for ind in top_indices]
# Get the MSE values for the top_n features
top_mses = feat_ab_scores[top_indices]
# Adjust so it's top mses minus original
original_list_repeated = np.repeat(np.array(original_mse_list).reshape(1, -1), top_n, axis=0)
top_mses = top_mses - original_list_repeated
# Create a box and whisker plot
if ax is None:
original_ax = None
fig, ax = plt.subplots()
else:
original_ax = ax
ax.boxplot(top_mses.T, labels=top_labels, showmeans=True, meanprops={'marker': 'o', 'markerfacecolor': 'red', 'markersize': 10})
# Rotate x-axis labels by 45 degrees
ax.set_xticklabels(top_labels, rotation=45)
# Set the axis labels
ax.set_xlabel('Features')
ax.set_ylabel('Ablated - Original MSEs')
# Set the title
num_hrs = int(time_radius) / 60
if num_hrs > 1:
ax.set_title(f'Top {top_n} Features: {metric}, Time Window = {num_hrs} Hours')
else:
ax.set_title(f'Top {top_n} Features: {metric}, Time Window = {num_hrs} Hour')
plt.savefig(savepath, bbox_inches='tight')
return top_indices, fig
def plotFeatAbPRs(feat_ab_prs, original_r_val, metric, time_radius, savepath, top_n=5, ax=None, spreadsheet_path=FEATURE_LABEL_PATH+'openface_2.0_hours.xlsx'):
# takes feat_ab_prs, a numpy array (n_features, ) of Pearson's R vals post-ablation
# outputs bar plot of top_n features TO REMOVE for the model
# procedure: get the top_n features with highest pearson's R
# make a bar plot with each feature on x axis and pearson's R from feat_ab_prs on y axis
# for x axis labels, convert the index of each feature to english label
# by calling get_label_from_index(feat_ind)
# if ax is given, plot on ax. If ax=None, make new fig, ax
# Get the top_n features with highest Pearson's R values
top_features_indices = np.argsort(feat_ab_prs)[-top_n:]
top_features_indices = top_features_indices[::-1]
# Get the labels for the top_n features
top_features_labels = [get_label_from_index(index, spreadsheet_path=spreadsheet_path) for index in top_features_indices]
# Get the corresponding Pearson's R values for the top_n features
top_features_prs = feat_ab_prs[top_features_indices]
# Plot the bar plot
if ax is None:
fig, ax = plt.subplots()
ax.bar(top_features_labels, top_features_prs)