-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_paragraph_correlations.py
196 lines (144 loc) · 7.42 KB
/
extract_paragraph_correlations.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
import pandas as pd
from collections import OrderedDict
from scipy.stats import spearmanr
from os import path, scandir
from utils import create_output_dir
INPUT_DIR = "output/normalized_attention_data/paragraphs/"
OUTPUT_DIR = "output/correlation_data/paragraphs/"
SOOD_DATASET = "sood_et_al_2020"
SARCASM_DATASET = "Mishra/Eye-tracking_and_SA-II_released_dataset"
GECO_DATASET = "GECO"
ZUCO_DATSET = "ZuCo"
PROVO_DATASET = "Provo"
def get_gaze_data(filepath):
gaze_df = pd.read_csv(filepath)
gaze_df = gaze_df.groupby("WORD_ID").mean()
return gaze_df
def get_transformer_data(filepath):
transformer_df = pd.read_csv(filepath).set_index("WORD_ID")
return transformer_df
def merge_data(gaze_df, transformer_df):
full_df = pd.merge(gaze_df, transformer_df, left_index=True, right_index=True)
full_df = full_df[full_df["L1_attention-mean_word-mean_norm"] < 1]
return full_df
def extract_correlations(df, eye_col, model):
mean_dict = OrderedDict()
max_dict = OrderedDict()
mean_cols = [col for col in df.columns if
all(include in col for include in ["_norm", "attention", "mean_word-mean"]) and all(
exclude not in col for exclude in ["cls", "min", "sum"])]
max_cols = [col for col in df.columns if
all(include in col for include in ["_norm", "attention", "max_word-max"]) and all(
exclude not in col for exclude in ["cls", "min", "sum"])]
for col in mean_cols:
mean_dict[col] = spearmanr(df[col], df[eye_col], nan_policy="omit")
for col in max_cols:
max_dict[col] = spearmanr(df[col], df[eye_col], nan_policy="omit")
mean_df = pd.DataFrame.from_dict(mean_dict)
mean_df.index = [f"{model}", f"{model} p-val"]
max_df = pd.DataFrame.from_dict(max_dict)
max_df.index = [f"{model}", f"{model} p-val"]
return mean_df, max_df
def create_datasets(gaze_data, transformer_files, eye_col, output_path):
mean_dfs = []
max_dfs = []
gaze_df = get_gaze_data(gaze_data)
for file in transformer_files:
model = file.split("/")[-1][:-4]
print(model)
transformer_df = get_transformer_data(file)
data_df = merge_data(gaze_df, transformer_df)
mean_df, max_df = extract_correlations(data_df, eye_col, model)
mean_dfs.append(mean_df)
max_dfs.append(max_df)
mean_df = pd.concat(mean_dfs)
mean_data = mean_df.loc[["p-val" not in index for index in mean_df.index]]
mean_p = mean_df.loc[["p-val" in index for index in mean_df.index]]
max_df = pd.concat(max_dfs)
max_data = max_df.loc[["p-val" not in index for index in max_df.index]]
max_p = max_df.loc[["p-val" in index for index in max_df.index]]
mean_data.to_csv(f"{output_path}/mean_df.csv")
max_data.to_csv(f"{output_path}/max_df.csv")
mean_p.to_csv(f"{output_path}/mean_p_val_df.csv")
max_p.to_csv(f"{output_path}/max_p_val_df.csv")
print("Done")
def create_sood_et_al_corr_table(dataset):
print(dataset)
eye_col = "Gaze event duration"
transformer_datapath = f"{INPUT_DIR}{dataset}/"
output_path = create_output_dir(f"{dataset}/Study_1/", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/sood_et_al_2020/normed_study1_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)
if "study_1" in file.name]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
output_path = create_output_dir(f"{dataset}/Study_2/", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/sood_et_al_2020/normed_study2_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)
if "study_2" in file.name]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
def create_mishra_sarcasm_corr_table(dataset):
print(dataset)
eye_col = "Fixation_Duration"
transformer_datapath = f"{INPUT_DIR}{dataset}/"
output_path = create_output_dir(f"{dataset}", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/Mishra/Eye-tracking_and_SA-II_released_dataset/normed_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
def create_geco_corr_table(dataset):
print(dataset)
eye_col = "WORD_GAZE_DURATION"
transformer_datapath = f"{INPUT_DIR}{dataset}/"
output_path = create_output_dir(f"{dataset}", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/GECO/normed_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
def create_zuco_corr_table(dataset):
print(dataset)
eye_col = "Fixation_Duration"
transformer_datapath = f"{INPUT_DIR}{dataset}/"
output_path = create_output_dir(f"{dataset}/Study_1/", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/ZuCo/normed_task1_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)
if "task_1" in file.name]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
output_path = create_output_dir(f"{dataset}/Study_2/", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/ZuCo/normed_task2_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)
if "task_2" in file.name]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
output_path = create_output_dir(f"{dataset}/Study_3/", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/ZuCo/normed_task3_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)
if "task_3" in file.name]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
def create_provo_corr_table(dataset):
print(dataset)
eye_col = "Fixation_Duration"
transformer_datapath = f"{INPUT_DIR}{dataset}/"
output_path = create_output_dir(f"{dataset}", OUTPUT_DIR)
gaze_data = "output/normalized_gaze_data/Provo/normed_paragraphs.csv"
transformer_files = [f"{transformer_datapath}{file.name}" for file in scandir(transformer_datapath)]
create_datasets(gaze_data, transformer_files, eye_col, output_path)
def main():
if not path.isdir(path.join(INPUT_DIR, SOOD_DATASET)):
print(f"Cannot find {SOOD_DATASET} - skipping creation")
else:
create_sood_et_al_corr_table(SOOD_DATASET)
if not path.isdir(path.join(INPUT_DIR, SARCASM_DATASET)):
print(f"Cannot find {SARCASM_DATASET} - skipping creation")
else:
create_mishra_sarcasm_corr_table(SARCASM_DATASET)
if not path.isdir(path.join(INPUT_DIR, GECO_DATASET)):
print(f"Cannot find {GECO_DATASET} - skipping creation")
else:
create_geco_corr_table(GECO_DATASET)
if not path.isdir(path.join(INPUT_DIR, ZUCO_DATSET)):
print(f"Cannot find {ZUCO_DATSET} - skipping creation")
else:
create_zuco_corr_table(ZUCO_DATSET)
if not path.isdir(path.join(INPUT_DIR, PROVO_DATASET)):
print(f"Cannot find {PROVO_DATASET} - skipping creation")
else:
create_provo_corr_table(PROVO_DATASET)
if __name__ == "__main__":
main()