-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_report.py
153 lines (139 loc) · 4.58 KB
/
generate_report.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
import argparse
import json
from pathlib import Path
import uuid
from loguru import logger
from tabulate import tabulate
from tqdm import tqdm
from metrics import calculate_all_metrics
def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--file_path", type=str)
group.add_argument("--files_dir", type=str)
parser.add_argument(
"--k",
type=int,
nargs="+",
default=[1, 3, 5, 10, 25, 50, 100],
help="One or more `k` value",
)
parser.add_argument("--small", action="store_true")
return parser.parse_args()
def evaluate_file(
name: str | Path,
k_list: list[int],
count_self: bool,
count_synonyms: bool,
small: bool,
) -> dict[str, int | float | str]:
logger.info(f"Reading {name}")
predictions = []
with open(name) as file:
for line in file:
predictions.append(json.loads(line))
logger.info(f"Total number of predictions: {len(predictions)}")
metrics = calculate_all_metrics(
predictions,
k_list,
count_self=count_self,
count_synonyms=count_synonyms,
small=small,
)
return metrics
def main():
args = parse_arguments()
reports_path = Path("./reports").resolve()
reports_path.mkdir(exist_ok=True)
report_base_name = str(uuid.uuid4())
size = "small" if args.small else "large"
if args.files_dir:
files_path = Path(args.files_dir).resolve()
files = [file for file in files_path.glob("*.jsonl") if file.is_file()]
only_syns_list = []
self_and_syns_list = []
only_self_list = []
for file in tqdm(files):
only_syns_list.append(
dict(name=file.stem)
| evaluate_file(
file,
k_list=args.k,
count_self=False,
count_synonyms=True,
small=args.small,
)
)
self_and_syns_list.append(
dict(name=file.stem)
| evaluate_file(
file,
k_list=args.k,
count_self=True,
count_synonyms=True,
small=args.small,
)
)
only_self_list.append(
dict(name=file.stem)
| evaluate_file(
file,
k_list=args.k,
count_self=True,
count_synonyms=False,
small=args.small,
)
)
only_syns_report_file = reports_path / f"{report_base_name}-syns-{size}.md"
self_and_syns_report_file = reports_path / f"{report_base_name}-all-{size}.md"
only_self_report_file = reports_path / f"{report_base_name}-self-{size}.md"
with open(only_syns_report_file, "w") as file:
file.write(
tabulate(only_syns_list, headers="keys", tablefmt="github") + "\n"
)
with open(self_and_syns_report_file, "w") as file:
file.write(
tabulate(self_and_syns_list, headers="keys", tablefmt="github") + "\n"
)
with open(only_self_report_file, "w") as file:
file.write(
tabulate(only_self_list, headers="keys", tablefmt="github") + "\n"
)
elif args.file_path:
file = Path(args.file_path).resolve()
only_syns = evaluate_file(
file,
k_list=args.k,
count_self=False,
count_synonyms=True,
small=args.small,
)
self_and_syns = evaluate_file(
file,
k_list=args.k,
count_self=True,
count_synonyms=True,
small=args.small,
)
only_self = evaluate_file(
file,
k_list=args.k,
count_self=True,
count_synonyms=False,
small=args.small,
)
keys = only_syns.keys()
transposed_data = [
[key] + [record[key] for record in [only_syns, self_and_syns, only_self]]
for key in keys
]
headers = ["", "syns", "all", "self"]
single_file_report = reports_path / f"{str(uuid.uuid4())}-{file.stem}-{size}.md"
with open(single_file_report, "w") as file:
file.write(
tabulate(transposed_data, tablefmt="github", headers=headers) + "\n"
)
else:
raise ValueError
if __name__ == "__main__":
main()