-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathevaluation.py
52 lines (41 loc) · 2.32 KB
/
evaluation.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
#!/usr/bin/env python
import json
import os.path
import sys
# as per the metadata file, input and output directories are the arguments
from sklearn.metrics import precision_recall_fscore_support
[_, ground_truth_path, input_json_path] = sys.argv
# unzipped submission data is always in the 'res' subdirectory
# https://github.com/codalab/codalab-competitions/wiki/User_Building-a-Scoring-Program-for-a-Competition#directory-structure-for-submissions
# submission_file_name = 'predictions.json'
# submission_dir = os.path.join(input_dir, 'res')
# submission_path = os.path.join(submission_dir, submission_file_name)
# if not os.path.exists(input_json_path):
# message = "Expected submission file '{0}'"
# sys.exit(message.format(input_json_path))
submission = json.load(open(input_json_path))
submission_id_label_map = {}
for doc in submission:
for annotation in doc['annotations'][0]['result']:
submission_id_label_map[annotation['id']] = annotation['value']['labels'][
0] ## consider only the first element in labels
# unzipped reference data is always in the 'ref' subdirectory
# https://github.com/codalab/codalab-competitions/wiki/User_Building-a-Scoring-Program-for-a-Competition#directory-structure-for-submissions
ground_truth = json.load(open(ground_truth_path))
ground_truth_labels = []
submission_lables = []
for doc in ground_truth:
for annotation in doc['annotations'][0]['result']:
ground_truth_label = annotation['value']['labels'][0] ## consider only the first element in labels
if submission_id_label_map.get(annotation['id']) is None:
submission_label = "No_Prediction"
else:
submission_label = submission_id_label_map[annotation['id']]
ground_truth_labels.append(ground_truth_label)
submission_lables.append(submission_label)
precision, recall, f1, _ = precision_recall_fscore_support(ground_truth_labels, submission_lables,
average='weighted')
# the scores for the leaderboard must be in a file named "scores.txt"
# https://github.com/codalab/codalab-competitions/wiki/User_Building-a-Scoring-Program-for-a-Competition#directory-structure-for-submissions
with open(os.path.join('result.json'), 'w') as output_file:
json.dump({"Weighted-F1": f1}, output_file)