-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccuracy.py
64 lines (54 loc) · 2.09 KB
/
accuracy.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
def calculate_accuracy_from_json(json_file_path):
"""
Calculate accuracy from JSON results file
Args:
json_file_path (str): Path to the JSON file containing results
Returns:
float: Accuracy score between 0 and 1
"""
import json
import pandas as pd
from pathlib import Path
# Read the results JSON file
try:
with open(json_file_path, 'r') as f:
results = json.load(f)
results = pd.DataFrame(results)
except FileNotFoundError:
print(f"Error: Could not find results file at {json_file_path}")
return None
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in {json_file_path}")
return None
# Try to find the test data file
test_file_paths = [
'data/snli_1.0/snli_1.0_test.jsonl',
'../data/snli_1.0/snli_1.0_test.jsonl',
'../../data/snli_1.0/snli_1.0_test.jsonl',
]
true_labels = None
test_file_paths = [r'C:\Users\ymede\Desktop\test\LoLaTokenization\snli_1.0_test.jsonl']
#test_file_paths = [r'C:\Users\ymede\Desktop\test\LoLaTokenization\multinli_1.0_dev_mismatched.jsonl']
for path in test_file_paths:
try:
true_labels = pd.read_json(path, lines=True)['gold_label']
print(f"Found test data at: {path}")
break
except FileNotFoundError:
continue
if true_labels is None:
print("Error: Could not find test data file. Please ensure it exists in one of these locations:", *test_file_paths, sep='\n')
return None
# Calculate accuracy
predictions = results.iloc[0]
correct = sum([x == y for x, y in zip(list(predictions), list(true_labels))])
#correct = (results == true_labels).sum()
total = len(true_labels)
accuracy = correct / total
print(f"Total samples: {total}")
print(f"Correct predictions: {correct}")
print(f"Accuracy: {accuracy}")
return accuracy
# Usage example:
if __name__ == "__main__":
accuracy = calculate_accuracy_from_json(r"C:\Users\ymede\Desktop\test\LoLaTokenization\custom_results_snli.json")