-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_results.py
187 lines (157 loc) · 5.09 KB
/
show_results.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
179
180
181
182
183
184
185
186
187
import os
import json
import matplotlib.pyplot as plt
plt.rcParams['axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
# local imports
from constants import *
from lib.utils import *
results_overview_path = os.path.join(EVALUATION_FOLDER, f"results_overview.json")
"""
Methods for producing plots and statistics.
"""
# plot metric y=results for all models and x=genres
def plot_metric(
results_overview: dict,
min_genre: int,
max_genre: int,
x_tick_names: list,
models: list,
metric: str,
ylim: tuple,
y_ticks:list):
scaling = 1.0
figure = plt.figure(figsize=(scaling*11, scaling*4.5), dpi=300)
genre_list = list(range(min_genre, max_genre+1))
for model in models:
values = []
model_dict = results_overview[model]
for n_target_genres in genre_list:
genre_dict = model_dict[str(n_target_genres)]
values.append(genre_dict[metric])
plt.plot(genre_list, values, label=MODEL_2_NAME[model], marker=".", markersize=11, linewidth=2)
# x-axis
plt.xlabel("# Genres")
plt.xticks(np.arange(min_genre, max_genre+1, step=1))
plt.xticks(genre_list, x_tick_names)
# y-axis
plt.ylabel("%")
plt.yticks(y_ticks)
plt.title(METRIC_2_NAME[metric])
plt.legend()
plt.ylim(ylim)
plt.grid()
figure.savefig(
os.path.join(EVALUATION_FOLDER, f"plot_{metric}.png"),
dpi=300, bbox_inches = 'tight',
pad_inches=0.0)
# one line are statistic for a pair of (n_genres, model):
# macro P & R & F1, and weighted P & R & F1
def produce_latex_table_lines(n_genres: int, results_overview: dict):
print(f"\n----------Table lines for {n_genres} Genres: ----------")
for model in MODELS:
# build line string
model_line = MODEL_2_NAME[model] + " & "
for metric in METRIC:
value = results_overview[model][str(n_genres)][metric]
if metric != METRIC[-1]:
model_line += (str(value) + " & ")
else:
model_line += (str(value) + " \\\\")
print(model_line)
"""
Helping Methods.
"""
# read dict with all results
def read_dict():
with open(results_overview_path, 'r') as file:
results_overview = json.load(file)
return results_overview
# fill dict with all results with zeros, and fill it manually
def build_dict():
results_overview = {}
for model in MODELS:
model_dict = {}
for n_target_genres in range(2, 12+1):
genre_dict = {}
for metric in METRIC:
genre_dict[metric] = 0.00
model_dict[n_target_genres] = genre_dict
results_overview[model] = model_dict
with open(results_overview_path, 'w') as file:
json.dump(results_overview, file)
if __name__ == '__main__':
# build_dict()
results_overview = read_dict()
"""
Generate LaTeX tables.
"""
for n_genres in range(2, 12+1):
produce_latex_table_lines(n_genres=n_genres, results_overview=results_overview)
"""
Generate Plots.
"""
min_genre=2
max_genre=12
x_tick_names=["2\n[Pop, Rock]", "3\n+Rap", "4\n+Country", "5\n+Reggae", "6\n+Heavy\nMetal", \
"7\n+Blues", "8\n+Indie", "9\n+Hip Hop", "10\n+Jazz", "11\n+Folk", "12\n+Gospel/\nReligioso"]
# macro F1
plot_metric(
min_genre=min_genre,
max_genre=max_genre,
x_tick_names=x_tick_names,
results_overview=results_overview,
models=MODELS,
metric=MACRO_F1,
ylim=[43.0, 82.00],
y_ticks=[45, 50, 55, 60, 65, 70, 75, 80])
# macro Precision
plot_metric(
min_genre=min_genre,
max_genre=max_genre,
x_tick_names=x_tick_names,
results_overview=results_overview,
models=MODELS,
metric=MACRO_PRECISION,
ylim=[43.0, 82.00],
y_ticks=[45, 50, 55, 60, 65, 70, 75, 80])
# macro Recall
plot_metric(
min_genre=min_genre,
max_genre=max_genre,
x_tick_names=x_tick_names,
results_overview=results_overview,
models=MODELS,
metric=MACRO_RECALL,
ylim=[43.0, 82.00],
y_ticks=[45, 50, 55, 60, 65, 70, 75, 80])
# weighted F1
plot_metric(
min_genre=min_genre,
max_genre=max_genre,
x_tick_names=x_tick_names,
results_overview=results_overview,
models=MODELS,
metric=WEIGHTED_F1,
ylim=[43.0, 82.00],
y_ticks=[45, 50, 55, 60, 65, 70, 75, 80])
# weighted Precision
plot_metric(
min_genre=min_genre,
max_genre=max_genre,
x_tick_names=x_tick_names,
results_overview=results_overview,
models=MODELS,
metric=WEIGHTED_PRECISION,
ylim=[43.0, 82.00],
y_ticks=[45, 50, 55, 60, 65, 70, 75, 80])
# weighted Recall
plot_metric(
min_genre=min_genre,
max_genre=max_genre,
x_tick_names=x_tick_names,
results_overview=results_overview,
models=MODELS,
metric=WEIGHTED_RECALL,
ylim=[43.0, 82.00],
y_ticks=[45, 50, 55, 60, 65, 70, 75, 80])