-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
58 lines (40 loc) · 1.75 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
52
53
54
55
56
57
58
import json
import warnings
import sys
def eval_function(target_dict, test_dict):
'''
This function calculates accuracy based on the participant's answer and true values.
Pay attention, that if for one key several values are given in participant's suggestion, then accuracy equals 0.
Parameters
----------
target_dict: dictionary with true values
test_dict: dictionary with participant's answer
Returns
-------
float
accuracy score
'''
correct_matches_count = 0
if len(target_dict) > len(test_dict):
warnings.warn("Not all contexts are mapped with sketches.")
elif len(target_dict) < len(test_dict):
warnings.warn("There are additional contexts given, which are not presented in the test data")
for target_key in target_dict:
if (target_key in test_dict) and (type(test_dict[
target_key]) != str): # Checks if participant tried to match one context with several sketches
raise Exception('More then one sketch found for one context.')
if (target_key in test_dict) and (target_dict[target_key] == test_dict[target_key]):
correct_matches_count += 1
accuracy = correct_matches_count / len(target_dict)
return accuracy
if __name__ == '__main__':
with open(sys.argv[1], 'r') as myfile:
if len(myfile.readlines()) != 0:
myfile.seek(0)
trial = json.load(myfile)
with open(sys.argv[2], 'r') as myfile:
if len(myfile.readlines()) != 0:
myfile.seek(0)
test = json.load(myfile)
accuracy = eval_function(trial, test)
print ('Your accuracy score is', accuracy)