-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_metrics_polygone.py
306 lines (231 loc) · 9.43 KB
/
plot_metrics_polygone.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import matplotlib.pyplot as plt
from typing import Tuple
import numpy as np
import os
from matplotlib.patches import RegularPolygon
from matplotlib.path import Path
from matplotlib.projections import register_projection
from matplotlib.projections.polar import PolarAxes
from matplotlib.spines import Spine
from matplotlib.transforms import Affine2D
import pandas as pd
map_metric_names = {
"fid": "FID",
"wpd": "WPD",
"mms": "MMS",
"aog": "AOG",
"coverage": "Coverage",
"acpd": "ACPD",
"apd": "APD",
"density": "Density",
# "fid-mean" : "FID",
# "wpd-mean" : "WPD",
# "mms-mean" : "MMS",
# "aog-mean" : "AOG",
# "coverage-mean" : "Coverage",
# "acpd-mean" : "ACPD",
# "apd-mean" : "APD",
# "density-mean" : "Density",
}
def get_csv_files_with_titles(prefix, dir="./"):
csv_files = []
titles = []
for filename in os.listdir(dir):
if filename.endswith(".csv") and filename.startswith(prefix):
csv_files.append(filename)
titles.append(filename[:-4])
return csv_files, titles
def _regisrer_radar_projection(
numberOfMetrics: int = None,
frame: str = "polygon",
):
"""
based on the Matplotlib tutorial on radar charts
with some modifications done by us:
https://matplotlib.org/stable/gallery/specialty_plots/radar_chart.html
"""
angles = np.linspace(0, 2 * np.pi, numberOfMetrics, endpoint=False)
class RadarTransform(PolarAxes.PolarTransform):
def transform_path_non_affine(self, path):
# Paths with non-unit interpolation steps correspond to gridlines,
# in which case we force interpolation (to defeat PolarTransform's
# autoconversion to circular arcs).
if path._interpolation_steps > 1:
path = path.interpolated(numberOfMetrics)
return Path(self.transform(path.vertices), path.codes)
class RadarAxes(PolarAxes):
name = "poylgone-chart"
PolarTransform = RadarTransform
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# rotate plot such that the first axis is at the top
self.set_theta_zero_location("N")
def fill(self, *args, closed=True, **kwargs):
"""Override fill so that line is closed by default"""
return super().fill(closed=closed, *args, **kwargs)
def plot(self, *args, **kwargs):
"""Override plot so that line is closed by default"""
lines = super().plot(*args, **kwargs)
for line in lines:
self._close_line(line)
def _close_line(self, line):
x, y = line.get_data()
# FIXME: markers at x[0], y[0] get doubled-up
if x[0] != x[-1]:
x = np.append(x, x[0])
y = np.append(y, y[0])
line.set_data(x, y)
def set_varlabels(self, labels):
self.set_thetagrids(np.degrees(angles), labels)
def _gen_axes_patch(self):
# The Axes patch must be centered at (0.5, 0.5) and of radius 0.5
# in axes coordinates.
if frame == "polygon":
return RegularPolygon(
(0.5, 0.5), numberOfMetrics, radius=0.5, edgecolor="k"
)
def draw(self, renderer):
"""Draw. If frame is polygon, make gridlines polygon-shaped"""
if frame == "polygon":
gridlines = self.yaxis.get_gridlines()
for gl in gridlines:
gl.get_path()._interpolation_steps = numberOfMetrics
super().draw(renderer)
def _gen_axes_spines(self):
if frame == "polygon":
# spine_type must be 'left'/'right'/'top'/'bottom'/'circle'.
spine = Spine(
axes=self,
spine_type="circle",
path=Path.unit_regular_polygon(numberOfMetrics),
)
# unit_regular_polygon gives a polygon of radius 1 centered at
# (0, 0) but we want a polygon of radius 0.5 centered at (0.5,
# 0.5) in axes coordinates.
spine.set_transform(
Affine2D().scale(0.5).translate(0.5, 0.5) + self.transAxes
)
return {"polar": spine}
else:
raise ValueError("Unknown value for 'frame': %s" % frame)
register_projection(RadarAxes)
return angles
def _normalize_metrics(df_metrics: pd.DataFrame):
models_column = df_metrics[list(df_metrics.columns)[0]]
metrics_columns = df_metrics.drop(list(df_metrics.columns)[0], axis=1)
normalized_metrics = metrics_columns.divide(metrics_columns.max())
normalized_dataframe = pd.DataFrame(
normalized_metrics, columns=metrics_columns.columns
)
return pd.concat([models_column, normalized_dataframe], axis=1)
def _transform_metrics(df_metrics, usedMetrics):
df_copy = df_metrics.copy()
model_names = list(df_metrics["Model"])
model_names.remove("Real")
for _metric in usedMetrics:
df_metric_real = df_copy.loc[df_copy["Model"] == "Real"]
_metric_real = df_metric_real[_metric].iloc[0]
for model_name in model_names:
df_metric_row = df_copy.loc[df_copy["Model"] == model_name]
_metric_model = df_metric_row[_metric].iloc[0]
if "fid" in _metric:
if _metric_model > _metric_real:
df_metrics.loc[df_metrics["Model"] == model_name, _metric] = 1.0 - (
_metric_model - _metric_real
)
else:
df_metrics.loc[df_metrics["Model"] == model_name, _metric] = 1.0 + (
_metric_real - _metric_model
)
else:
if _metric_model < _metric_real:
df_metrics.loc[df_metrics["Model"] == model_name, _metric] = 1.0 - (
_metric_real - _metric_model
)
else:
df_metrics.loc[df_metrics["Model"] == model_name, _metric] = 1.0 + (
_metric_model - _metric_real
)
df_metrics.loc[df_metrics["Model"] == "Real", _metric] = 1.0
return df_metrics
# def _transform_density_metric(df_metrics):
# df_density = df_metrics[["Model","density-mean"]].copy()
# model_names = list(df_metrics["Model"])
# model_names.remove("Real")
# df_density_real = df_density.loc[df_density["Model"] == "Real"]
# _density_real = df_density_real["density-mean"].iloc[0]
# for model_name in model_names:
# df_density_row = df_density.loc[df_density["Model"] == model_name]
# _density = df_density_row["density-mean"].iloc[0]
# if _density > _density_real:
# df_metrics.loc[df_metrics["Model"] == model_name, "fid-mean"] = 1.0 - (_density - _density_real)
# else:
# df_metrics.loc[df_metrics["Model"] == model_name, "fid-mean"] = 1.0 + (_fid_real - _fid)
# df_metrics.loc[df_metrics["Model"] == "Real", "fid-mean"] = 1.0
# return df_metrics
def plot_metrics_on_polygone(
df_metrics,
usedMetrics: list = None,
usedModels: list = None,
frame: str = "polygon",
title: str = None,
figsize: Tuple[int, int] = (5, 5),
):
df_metrics = _normalize_metrics(df_metrics=df_metrics)
if usedMetrics is None:
metrics_ = list(df_metrics.columns)
metrics_.remove(list(df_metrics.columns)[0])
metrics = [_metric for _metric in metrics_ if not "std" in _metric]
else:
metrics = usedMetrics
numberOfMetrics = len(metrics)
df_metrics = _transform_metrics(df_metrics=df_metrics, usedMetrics=metrics)
# print(df_metrics[["Model","coverage-mean"]])
if usedModels is None:
models = list(df_metrics[list(df_metrics.columns)[0]])
else:
models = usedModels
numberOfModels = len(models)
angles = _regisrer_radar_projection(
numberOfMetrics=numberOfMetrics,
frame=frame,
)
fig, ax = plt.subplots(
figsize=figsize, nrows=1, ncols=1, subplot_kw=dict(projection="poylgone-chart")
)
colors = plt.get_cmap("Dark2")(np.linspace(start=0.0, stop=1.0, num=numberOfModels))
ax.set_rgrids(
np.linspace(
start=0.1,
stop=df_metrics.select_dtypes(include=["number"]).max().max(),
num=5,
)
)
ax.set_title(
title,
weight="bold",
size="medium",
position=(0.5, 1.1),
horizontalalignment="center",
verticalalignment="center",
)
for i in range(numberOfModels):
modelName = models[i]
color = colors[i]
metricData = df_metrics.loc[df_metrics["Model"] == modelName][metrics]
_metricData = []
for _metric in metrics:
if not "std" in _metric:
_metricData.append(float(metricData[_metric].iloc[0]))
ax.plot(angles, _metricData, color=color, label=modelName)
ax.fill(angles, _metricData, facecolor=color, alpha=0.25, label="_nolegend_")
ax.set_varlabels([map_metric_names[_metric] for _metric in metrics])
ax.legend(loc="upper right", bbox_to_anchor=(1.1, 1.15))
fig.savefig(title + ".pdf")
if __name__ == "__main__":
csv_file = "metrics_models.csv"
title = "Metrics"
plot_metrics_on_polygone(
df_metrics=pd.read_csv(csv_file),
title=title,
)