-
Notifications
You must be signed in to change notification settings - Fork 0
/
human_eval.py
70 lines (53 loc) · 2.2 KB
/
human_eval.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
import os
import io
import random
import json
import pandas as pd
def create_eval_files():
#test_file = 'data/rest_e2e/devset_e2e.csv'
test_file = 'data/rest_e2e/testset_e2e.csv'
#num_instances = 547 # for devset_e2e.csv
num_instances = 630 # for testset_e2e.csv
num_samples = 40
prediction_files = os.listdir('eval/predictions')
header = ['sample #', 'MR']
data = []
# generate random sample of indexes
sample_idxs = random.sample(range(0, num_instances), num_samples)
data.append(sample_idxs)
# map the filenames onto random numbers
file_idxs_random = [i for i in range(len(prediction_files))]
random.shuffle(file_idxs_random)
files_dict = {}
for i, filename in enumerate(prediction_files):
files_dict[file_idxs_random[i]] = filename
# store the file index map into a file to be used as a key
with open('eval/file_map.json', 'w') as f_file_map:
json.dump(files_dict, f_file_map, indent=4, sort_keys=True)
# sample the MRs
data_frame_test = pd.read_csv(test_file, header=0, encoding='utf8')
mrs = data_frame_test.iloc[:, 0].tolist()
mrs_reduced = []
for i in range(len(mrs)):
if i == 0 or mrs[i] != mrs[i - 1]:
mrs_reduced.append(mrs[i])
mrs_sampled = [mrs_reduced[idx] for idx in sample_idxs]
data.append(mrs_sampled)
# sample the predictions
for i, filename in enumerate(prediction_files):
header.append(file_idxs_random[i])
with io.open(os.path.join('eval', 'predictions', filename), 'r', encoding='utf8') as f_predictions:
predictions = f_predictions.read().splitlines()
predictions_sampled = [predictions[idx] for idx in sample_idxs]
data.append(predictions_sampled)
# create a data frame
df = pd.DataFrame(data)
df = df.transpose()
df.columns = header
# reorder the columns so the file indexes are in an increasing order
header_reordered = header[:2] + [i for i in range(len(prediction_files))]
df = df[header_reordered]
# store the data frame into a CSV file
df.to_csv('eval/human_evaluation.csv', index=False, encoding='utf8')
if __name__ == "__main__":
create_eval_files()