-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresults_analysis.py
292 lines (247 loc) · 10.6 KB
/
results_analysis.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 31 11:44:47 2023
"""
import os
import pandas as pd
from fnmatch import fnmatch
import json
import math
import numpy as np
#PARSE_FUNCS = {'utt_holdout': parse_results_utt, 'formula_holdout': parse_results_formula, 'type_holdout': parse_results_type}
SYMBOLIC_MODEL_TYPES = ['finetuned_gpt3', 'pretrained_gpt3','s2s_pt_transformer']
SYMBOLIC_TEST_TYPES = ['utt_holdout','formula_holdout','type_holdout']
MODEL_NAMES = {'finetuned_gpt3': 'Finetuned GPT3', 'pretrained_gpt3': 'Prompt GPT3', 's2s_pt_transformer': 'Seq2Seq'}
TEST_NAMES = {'utt_holdout': 'Utterance','formula_holdout':'Formula','type_holdout':'Type'}
OSM_MODEL_NAMES = ['Lang2LTL', 'CopyNet']
def get_full_system_piechart(test_type):
filepath = os.path.join('.','results','lang2ltl','osm')
cities = get_osm_cities()
symbolic_errors = 0
RER_errors = 0
for city in cities:
city_accs = []
filepath1 = os.path.join(filepath, city, test_type+'_batch12')
if os.path.exists(filepath1):
files = [file for file in os.listdir(filepath1) if fnmatch(file, 'acc*.csv')]
for file in files:
df = pd.read_csv(os.path.join(filepath1,file))
if 'False' in df['Accuracy'].value_counts().index:
symbolic_errors = symbolic_errors + df['Accuracy'].value_counts()['False']
if 'RER or Grounding Error' in df['Accuracy'].value_counts().index:
RER_errors = RER_errors + df['Accuracy'].value_counts()['RER or Grounding Error']
return symbolic_errors, RER_errors
def parse_per_city_accs(test_type):
filepath = os.path.join('.','results','lang2ltl','osm')
cities = get_osm_cities()
accs = []
df = {}
entry_id = 0
for city in cities:
city_accs = []
filepath1 = os.path.join(filepath, city, test_type+'_batch12')
if os.path.exists(filepath1):
files = [file for file in os.listdir(filepath1) if fnmatch(file, 'acc*.json')]
for file in files:
with open(os.path.join(filepath1, file), 'r') as f:
data = json.load(f)
acc = data['Accumulated Accuracy']
city_accs.append(acc)
if len(city_accs):
accs.append(city_accs)
for (model_id, acc) in enumerate(city_accs):
entry = {}
entry['Model ID'] = model_id
entry['Accuracy'] = acc
entry['City'] = city
df[entry_id] = entry
entry_id = entry_id + 1
return pd.DataFrame.from_dict(df, orient = 'index')
def create_symbolic_accuracies_table(test_types = SYMBOLIC_TEST_TYPES, model_types = SYMBOLIC_MODEL_TYPES):
entries = []
for test_type in test_types:
for model_type in model_types:
entries.extend(parse_symbolic_accuracies(test_type, model_type))
#Conver to dataframe
df = {}
for (i, entry) in enumerate(entries):
df[i] = entry
df = pd.DataFrame.from_dict(df, orient = 'index')
return df
def create_osm_accuracies_table(test_types = SYMBOLIC_TEST_TYPES, model_types = OSM_MODEL_NAMES):
entries = []
for test_type in test_types:
for model_type in model_types:
entries.extend(parse_osm_acc(test_type, model_type))
df = {}
for (i,entry) in enumerate(entries):
df[i] = entry
df = pd.DataFrame.from_dict(df, orient = 'index')
return df
def parse_osm_acc(test_type, model_type):
#returns a list of dicts
if model_type == 'Lang2LTL':
accs = get_lang2ltl_accuracies(test_type)
else:
accs = get_copynet_accuracies(test_type)
entries = []
for (i,acc) in enumerate(accs):
entry = {}
entry['Test Type'] = TEST_NAMES[test_type]
entry['Model'] = model_type
entry['Model ID'] = i
entry['Accuracy'] = acc
entries.append(entry)
return entries
def get_copynet_accuracies(test_type):
test_strings = {'utt_holdout':'utt','formula_holdout':'formula','type_holdout':'type'}
test_string = test_strings[test_type]
filepath = os.path.join('.','results','CopyNet')
relevant_dirs = [os.path.join(filepath,dirname) for dirname in os.listdir(filepath) if test_string in dirname and os.path.isdir(os.path.join(filepath,dirname))]
accs = []
for dirname in relevant_dirs:
#print(dirname)
filenames = [file for file in os.listdir(dirname) if 'aggregate' in file]
#print(filenames)
if filenames:
filename = os.path.join(dirname, filenames[0])
csv_data = pd.read_csv(filename)
acc = pd.np.mean(csv_data['accuracy'])
accs.append(acc)
return accs
def get_lang2ltl_accuracies(test_type, output='means'):
filepath = os.path.join('.','results','lang2ltl','osm')
cities = get_osm_cities()
accs = []
for city in cities:
city_accs = []
filepath1 = os.path.join(filepath, city, test_type+'_batch12')
if os.path.exists(filepath1):
files = [file for file in os.listdir(filepath1) if fnmatch(file, 'acc*.json')]
for file in files:
with open(os.path.join(filepath1, file), 'r') as f:
data = json.load(f)
acc = data['Accumulated Accuracy']
city_accs.append(acc)
if len(city_accs):
accs.append(city_accs)
maxlen = max([len(acc) for acc in accs])
#replace all missing values with nan
for (i,acc) in enumerate(accs):
if len(acc) < maxlen:
acc = acc + [math.nan]*(maxlen - len(acc))
accs[i] = acc
accs = np.array(accs)
if output == 'means':
return np.nanmean(accs, axis=0)
else:
return accs
def parse_n_prop_accuracies(test_type, model_type):
filenames = resolve_filenames(test_type, model_type)
dataframes = []
for (i,file) in enumerate(filenames):
csv_data = pd.read_csv(file)
dataframes.append(get_n_prop_accuracies(csv_data, i, test_type))
return pd.concat(dataframes, axis=0, ignore_index=True)
def get_n_prop_accuracies(csv_data, model_id, test_type):
#Assumes csv data provided as pandas file
#Get a pandas table per file and then merge it
row_dicts = []
#csv_data = csv_data.loc[csv_data['Accuracy'] != 'no valid data']
formula_types = np.unique(csv_data['Number of Propositions'])
for formula_type in formula_types:
#print(formula_type)
data = csv_data.loc[csv_data['Number of Propositions'] == formula_type]
data = data.loc[data['Accuracy'] != 'no valid data']
#print(len(data))
if len(data) > 0:
acc = np.sum(data['Number of Utterances'] * data['Accuracy'].astype(float))/np.sum(data['Number of Utterances'])
entry = {}
entry['Model ID'] = model_id
entry['Test Type'] = test_type
entry['N Propositions'] = formula_type
entry['Accuracy'] = acc
row_dicts.append(entry)
#print(entry)
df = {}
for (i,entry) in enumerate(row_dicts):
df[i] = entry
df = pd.DataFrame.from_dict(df, orient='index')
return df
def parse_per_type_accuracies(test_type, model_type):
filenames = resolve_filenames(test_type, model_type)
dataframes = []
for (i,file) in enumerate(filenames):
csv_data = pd.read_csv(file)
dataframes.append(get_per_type_accuracies(csv_data, i, test_type))
return pd.concat(dataframes, axis=0, ignore_index=True)
def get_per_type_accuracies(csv_data, model_id, test_type):
#Assumes csv data provided as pandas file
#Get a pandas table per file and then merge it
row_dicts = []
#csv_data = csv_data.loc[csv_data['Accuracy'] != 'no valid data']
formula_types = np.unique(csv_data['LTL Type'])
for formula_type in formula_types:
#print(formula_type)
data = csv_data.loc[csv_data['LTL Type'] == formula_type]
data = data.loc[data['Accuracy'] != 'no valid data']
#print(len(data))
if len(data) > 0:
acc = np.sum(data['Number of Utterances'] * data['Accuracy'].astype(float))/np.sum(data['Number of Utterances'])
entry = {}
entry['Model ID'] = model_id
entry['Test Type'] = test_type
entry['Formula Type'] = formula_type
entry['Accuracy'] = acc
row_dicts.append(entry)
#print(entry)
df = {}
for (i,entry) in enumerate(row_dicts):
df[i] = entry
df = pd.DataFrame.from_dict(df, orient='index')
return df
def parse_symbolic_accuracies(test_type, model_type):
filenames = resolve_filenames(test_type, model_type)
row_dicts = []
for (i,file) in enumerate(filenames):
#parse_func = get_parse_funcs(test_type)
acc = parse_acc(pd.read_csv(file))
entry = {}
entry['Model'] = MODEL_NAMES[model_type]
entry['Test Type'] = TEST_NAMES[test_type]
entry['Model ID'] = i
entry['Accuracy'] = acc
row_dicts.append(entry)
return row_dicts
def resolve_filenames(test_type, model_type):
#returns a list of csv filepaths that need to be parsed by the appropriate
filepath = 'results'
#print(model_type)
if model_type in SYMBOLIC_MODEL_TYPES:
filepath = os.path.join(filepath, model_type)
else:
raise Exception('Unknown model type')
if test_type in SYMBOLIC_TEST_TYPES:
filepath = os.path.join(filepath, test_type+'_batch12_perm')
else:
raise Exception('Unknown test type')
# print(os.listdir(filepath))
filenames = [file for file in os.listdir(filepath) if fnmatch(file,'*.csv') and 'aggregated' not in file and 'acc' in file and 'num_prop' not in file and 'formula_type' not in file]
# print(filenames)
filepaths = [os.path.join(filepath, file) for file in filenames]
return filepaths
def read_csv_data(filepath):
return pd.read_csv(filepath)
def parse_acc(csv_data):
csv_data = csv_data.loc[csv_data['Accuracy']!='no valid data']
num = pd.np.sum(csv_data['Number of Utterances']*csv_data['Accuracy'].astype(float))
den = pd.np.sum(csv_data['Number of Utterances'])
return num/den
def get_osm_cities():
filepath = os.path.join('.','results','lang2ltl','osm')
cities = os.listdir(filepath)
cities = [city for city in cities if 'boston' not in city]
return cities
if __name__ == '__main__':
#df = create_symbolic_accuracies_table(test_types = SYMBOLIC_TEST_TYPES, model_types = ['finetuned_gpt3','s2s_pt_transformer'])
a=1