-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranking_utils.py
178 lines (149 loc) · 6.31 KB
/
ranking_utils.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Various utilities for automatically generating LaTeX tables from the results of the experiments,
including reporting rank sums of the models on the different problems.
"""
def get_ranks(problem, methods, results_dict):
problem_entries = {
method: results_dict[(problem, method)] for method in methods
}
metrics = list(problem_entries[methods[0]].keys())
ranks = {
metric: {
method: 0 for method in methods
} for metric in metrics
}
for metric in metrics:
sorted_methods = sorted(methods, key=lambda method: problem_entries[method][metric])
for i, method in enumerate(sorted_methods):
ranks[metric][method] = i+1
return ranks
def sum_ranks(problem, methods, results_dict):
ranks = get_ranks(problem, methods, results_dict)
metrics = list(ranks.keys())
summed_ranks = {
method: 0 for method in methods
}
for metric in metrics:
for method in methods:
summed_ranks[method] += ranks[metric][method]
return summed_ranks
def sum_all_ranks(methods, results_dict):
summed_ranks = {
method: 0 for method in methods
}
problems_in_dict = set([problem for (problem, method) in results_dict.keys()])
for problem in problems_in_dict:
summed_ranks_problem = sum_ranks(problem, methods, results_dict)
for method in methods:
summed_ranks[method] += summed_ranks_problem[method]
return summed_ranks
def get_ranking(methods, results_dict):
summed_ranks = sum_all_ranks(methods, results_dict)
sorted_methods = sorted(methods, key=lambda method: summed_ranks[method])
return sorted_methods
method_to_nice_method = {
"euler": "Euler-Maruyama (EM)",
"lt": "Lie-Trotter (LT)",
"moment": "Moment (M) \cite{rackauckas2020universal}",
"wasserstein": "Wasserstein (W)",
"corr": "Auto-Correlation (C)",
"euler_moment": "EM + M",
"euler_wasserstein": "EM + W",
"euler_corr": "EM + C",
"lt_moment": "LT + M",
"lt_wasserstein": "LT + W",
"lt_corr": "LT + C",
"euler_moment_corr": "EM + M + C",
"euler_wasserstein_corr": "EM + W + C",
"lt_moment_corr": "LT + M + C",
"lt_wasserstein_corr": "LT + W + C",
"sdegan": "SDE-GAN \cite{kidger2021neural}"
}
problem_to_nice_problem = {
"ou": "OU",
"cir": "CIR",
"sin": "SIN1",
"sit": "SIN2",
"gfp": "GFP"
}
def write_latex_table(problems_listed, methods, results_dict, out_txt_file, cell="rank"):
# Rows: Methods
# Columns: Problems, plus Rank sum
# Inner cells: Rank sum for each pair of problem and method, summed over all metrics
summed_ranks = sum_all_ranks(methods, results_dict)
min_value = min(summed_ranks.values())
latex_table = ""
latex_table += "\\begin{tabular}{|l|"
for problem in problems_listed:
latex_table += "c|"
latex_table += "c|}\n"
latex_table += "\\hline\n"
latex_table += "Method & "
for problem in problems_listed:
latex_table += f"{problem_to_nice_problem[problem]} & "
latex_table += "\\textbf{Rank sum} \\\\\n"
latex_table += "\\hline\n"
for method in methods:
latex_table += f"{method_to_nice_method[method]} & "
for problem in problems_listed:
ranks = sum_ranks(problem, results_dict)
min_rank = min(ranks.values())
rank = ranks[method]
if rank == min_rank and rank == summed_ranks[method]:
latex_table += f"\\textbf{{{rank}}} & "
elif rank == min_rank:
latex_table += f"\\textbf{{{rank}}} & "
elif rank == summed_ranks[method]:
latex_table += f"\\textbf{{{rank}}} & "
else:
latex_table += f"{rank} & "
if summed_ranks[method] == min_value:
latex_table += f"\\textbf{{{summed_ranks[method]}}} \\\\\n"
else:
latex_table += f"{summed_ranks[method]} \\\\\n"
latex_table += "\\hline\n"
latex_table += "\\end{tabular}"
with open(out_txt_file, "w") as f:
f.write(latex_table)
from operator import itemgetter
def write_latex_table_content(problems_listed, methods, results_dict, out_txt_file, cell="rank", withsdegan=False):
# Same as above, except that results_dict only has the results for one metric,
# therefore if cell=="value", we can also print the actual value instead of a rank sum
only_key = list(results_dict[list(results_dict.keys())[0]].keys())[0]
summed_ranks = sum_all_ranks(methods, results_dict)
min_value = min(summed_ranks.values())
latex_table = ""
latex_table += "\\begin{tabular}{|l|"
for problem in problems_listed:
latex_table += "c|"
latex_table += "c|}\n"
latex_table += "\\hline\n"
latex_table += "Method & "
for problem in problems_listed:
latex_table += f"{problem_to_nice_problem[problem]} & "
latex_table += "\\textbf{Rank sum} \\\\\n"
latex_table += "\\hline\n"
for method in methods:
latex_table += f"{method_to_nice_method[method]} & "
for problem in problems_listed:
ranks = sum_ranks(problem, methods, results_dict)
min_rank = min(ranks.values())
rank = ranks[method]
value = results_dict[(problem, method)][only_key]
value_string = f"{value:.3f}" if value < 1000 else f"{value:.3e}"
if rank == min_rank and rank == summed_ranks[method]:
latex_table += f"\\textbf{{{rank}}} & " if cell=="rank" else f"\\textbf{{{value_string}}} & "
elif rank == min_rank:
latex_table += f"\\textbf{{{rank}}} & " if cell=="rank" else f"\\textbf{{{value_string}}} & "
elif rank == summed_ranks[method]:
latex_table += f"\\textbf{{{rank}}} & " if cell=="rank" else f"\\textbf{{{value_string}}} & "
else:
latex_table += f"{rank} & " if cell=="rank" else f"{value_string} & "
if summed_ranks[method] == min_value:
latex_table += f"\\textbf{{{summed_ranks[method]}}} \\\\\n"
else:
latex_table += f"{summed_ranks[method]} \\\\\n"
latex_table += "\\hline\n"
latex_table += "\\end{tabular}"
with open(out_txt_file, "w") as f:
f.write(latex_table)