-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin_frames_evaluate.py
171 lines (146 loc) · 7.84 KB
/
join_frames_evaluate.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
import pandas as pd
import numpy as np
import os, sys
import core.main as main
from sklearn import preprocessing
gname_map = {'participant': 'Participant_id', 'video': 'Experiment_id'}
gname_sortmap = {'participant': 'Experiment_id', 'video': 'Participant_id'}
CLASSIFIER = 'classifier'
LINEAR = 'linear'
BEST_FEATURES = 'bestscore'
ALL_FEATURES = 'allfeatures'
REGRESSION = 'regression'
FOREST = 'forest'
XGBOOST = 'xgboost'
facet_group_map = {'participant': 'video', 'video': 'participant'}
def run_tests_for_emotions(clean_frame, group_name, estimator):
feature_importance_data = []
for video_id, group_data in clean_frame.groupby([group_name]):
feats = group_data.drop(columns=['participant', 'video'])
# scale values
scaler = preprocessing.StandardScaler()
feats[feats.columns] = scaler.fit_transform(feats[feats.columns])
input_data = feats.values.tolist()
feature_names = feats.columns.values
print(feature_names)
for emotion in ['Valence', 'Arousal', 'Dominance', 'Liking']:
ratings = pd.read_csv('metadata_csv/participant_ratings.csv')
target_emotion = ratings[(ratings[gname_map[group_name]] == 1)].sort_values(
by=[gname_sortmap[group_name]])[emotion].to_list()
test_score, importance = getattr(main, estimator)(input_data, target_emotion)
metric_names = list(test_score.keys())
metric_values = list(test_score.values())
feature_importance_data.append([video_id, emotion] + metric_values
# + importance
)
results_frame = pd.DataFrame(feature_importance_data,
columns=['video_id', 'emotion'] + metric_names
# + feature_names.tolist()
)
return results_frame
def test_alldata_emotions(clean_frame, estimator, best_features=True, is_classifier=True):
feature_importance_data = []
frame_columns = ['emotion', 'mae', 'best_features']
scaler = preprocessing.StandardScaler()
clean_frame = clean_frame.sort_values(['participant', 'video'])
feats = clean_frame.drop(columns=['participant', 'video'])
print(feats.shape)
feats[feats.columns] = scaler.fit_transform(feats[feats.columns])
input_data = feats.values.tolist()
feature_names = feats.columns.values
for emotion in ['Valence', 'Arousal', 'Dominance', 'Liking']:
ratings = pd.read_csv('metadata_csv/participant_ratings.csv')
target_emotion = ratings[~ratings.participant.isin(main.exclude_participant)].sort_values(['participant', 'video'])[emotion].to_list()
if is_classifier:
target_emotion = [0 if (float(e) < 5.0) else 1 for e in target_emotion]
test_score, feat_idx_or_weight = getattr(main, estimator)(input_data, target_emotion)
if best_features:
best_features_list = [', '.join([feature_names[i] for i in feat_idx_or_weight])]
feature_importance_data.append([emotion, test_score, best_features_list])
else:
feature_importance_data.append([emotion, str(test_score)])
frame_columns = ['emotion', 'acc']
results_frame = pd.DataFrame(feature_importance_data, columns=frame_columns)
return results_frame
def run_tests_for_emotions_feat_selector(clean_frame, group_name, estimator, is_classifier, best_features=True):
feature_importance_data = []
frame_columns = ['video_id', 'emotion', 'mae', 'best_features']
for video_id, group_data in clean_frame.groupby([group_name]):
print("{0} {1}".format(group_name, video_id))
feats = group_data.drop(columns=['participant', 'video'])
# scale values
scaler = preprocessing.StandardScaler()
feats[feats.columns] = scaler.fit_transform(feats[feats.columns])
input_data = feats.values.tolist()
feature_names = feats.columns.values
# print(feature_names)
for emotion in ['Valence', 'Arousal', 'Dominance', 'Liking']:
exclude = True if group_name == 'video' else False
target_emotion = get_ratings(emotion, group_name, video_id, facet_group_map[group_name],
exclude, binary=is_classifier)
if best_features:
test_score, feat_idx = getattr(main, estimator)(input_data, target_emotion)
best_features = [', '.join([feature_names[i] for i in feat_idx])]
feature_importance_data.append([video_id, emotion, test_score, best_features])
else:
test_score, importance = getattr(main, estimator)(input_data, target_emotion)
metric_names = list(test_score.keys())
metric_values = list(test_score.values())
feature_importance_data.append([video_id, emotion] + metric_values)
frame_columns = ['video_id', 'emotion'] + metric_names
results_frame = pd.DataFrame(feature_importance_data, columns=frame_columns)
return results_frame
def get_ratings(emotion, target_col, target_val, groupby, exclude, binary=False):
target_val = int(target_val) + 1
ratings = pd.read_csv('metadata_csv/participant_ratings.csv')
target_emotion = ratings[(ratings[target_col] == target_val)].sort_values(
by=[groupby])[emotion].to_list()
if exclude:
target_emotion = [t for i, t in enumerate(target_emotion) if i not in main.exclude_participant]
if binary:
target_emotion = [0 if (float(e) < 5.0) else 1 for e in target_emotion]
return target_emotion
estimator_maps = {
CLASSIFIER: {
ALL_FEATURES: {REGRESSION: 'run_classify_regression', FOREST: 'run_classify_forest', XGBOOST: 'run_classify_xgboost'},
BEST_FEATURES: {REGRESSION: 'run_classify_regression_best_features', FOREST: 'run_classify_forest_best_features', XGBOOST: 'run_classify_xgboost_best_features'}
},
LINEAR: {
ALL_FEATURES: {REGRESSION: 'run_test_regression', FOREST: 'run_test_forest', XGBOOST: 'run_test_xgboost'},
BEST_FEATURES: {REGRESSION: 'run_test_regression_feature_selector', FOREST: 'run_test_forest_feature_selector', XGBOOST: 'run_test_xgboost_feature_selector'},
}}
hr_path = "hr_features.pkl"
eda_path = "eda_features.pkl"
if os.path.isfile(hr_path):
hr_features_frame = pd.read_pickle(hr_path)
if os.path.isfile(eda_path):
eda_features_frame = pd.read_pickle(eda_path)
eda_features_frame = eda_features_frame.drop(columns=['participant', 'video'])
full_feature_data = pd.concat([hr_features_frame, eda_features_frame], axis=1)
df = full_feature_data
correlation_matrix = df.corr()
# what do about missing/failing feature values
clean_frame = df.fillna(0)
print(clean_frame)
# for f in [ALL_FEATURES, BEST_FEATURES]:
# for m in [REGRESSION, FOREST, XGBOOST]:
# for g in ['participant', 'video']:
#
# path = os.path.join('results', '_'.join([g, f, LINEAR, m]) + '.csv')
# if os.path.isfile(path): continue
#
# best_features = True if f == BEST_FEATURES else False
# results_frame = run_tests_for_emotions_feat_selector(clean_frame, g, estimator_maps[LINEAR][f][m],
# is_classifier=False, best_features=best_features)
#
# results_frame.to_csv(path)
# for f in [ALL_FEATURES, BEST_FEATURES]:
# for m in [REGRESSION, FOREST, XGBOOST]:
for f in [ALL_FEATURES]:
for m in [FOREST]:
path = os.path.join('results', '_'.join(['alldata', f, CLASSIFIER, m]) + '.csv')
if os.path.isfile(path): continue
best_features = True if f == BEST_FEATURES else False
results_frame = test_alldata_emotions(clean_frame, estimator_maps[CLASSIFIER][f][m],
best_features=best_features, is_classifier=True)
results_frame.to_csv(path)