-
Notifications
You must be signed in to change notification settings - Fork 2
/
violin.py
282 lines (218 loc) · 8.69 KB
/
violin.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
# coding=utf-8
import ast
import itertools
import json
import os
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
import matplotlib.patches as mpatches
# 用于比较S个solution在E个environment上的性能差别. 不同的性能指标放在不同的图上, 不同的解决方案在各个环境下的同一指标值放在同一张图上
from matplotlib.ticker import FormatStrFormatter
data = {
'type': "violin",
'figWidth': 600,
'figHeight': 350,
'mainColors': ['#0072bc',
'#d95218',
'#edb021',
'#7a8cbf',
'#009d70',
'#979797',
'#53b2ea'],
'environmentList': ("Intel", "Rome"),
'solutionList': ('VERID', 'AAR', 'IntegriDB'),
'paddingLeft': 0.2,
'paddingRight': 0.2,
'marginGroups': 0.4,
'marginViolins': 0.02,
'yLog': True,
'yGrid': True,
'xFontSize': 20,
'yFontSize': 20,
'output': False,
'children': [
{
'name': "insertion",
'xTitle': '',
'yTitle': 'Insertion time (ms)',
'yLimit': [0, 1.4],
'samples': [(
(0.011, 0.203, 0.161, 0.513), # 同一个environment, 不同的sample
(0.428, 0.220, 0.161, 0.513), # 另一个environment
),
(
(0.0211, 0.203, 0.4161, 0.513), # 同一个environment, 不同的sample
(0.2428, 0.3220, 0.161, 0.513), # 另一个environment
),
(
(0.1211, 0.203, 0.4161, 0.513), # 同一个environment, 不同的sample
(0.2428, 0.3220, 0.161, 0.0513), # 另一个environment
),
]
},
]
}
if not os.path.exists('dist'):
os.makedirs('dist')
def nonEmptyIterable(obj):
"""return true if *obj* is iterable"""
try:
var = obj[0]
return True
except:
return False
dpi = 100
def adjacent_values(vals, q1, q3):
upper_adjacent_value = q3 + (q3 - q1) * 1.5
upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])
lower_adjacent_value = q1 - (q3 - q1) * 1.5
lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)
return lower_adjacent_value, upper_adjacent_value
def set_axis_style(ax, labels):
ax.get_xaxis().set_tick_params(direction='out')
ax.xaxis.set_ticks_position('bottom')
ax.set_xticks(np.arange(1, len(labels) + 1))
ax.set_xticklabels(labels)
ax.set_xlim(0.25, len(labels) + 0.75)
ax.set_xlabel('Overall load')
class Violin:
def draw(self, data, figure=None, axis=None):
if isinstance(data, str):
try:
data = json.loads(data)
except:
data = ast.literal_eval(data)
axes = []
for plotData in data['children']:
name = plotData['name']
print("---->" + name + "<----\n")
def get(key, default=None):
result = plotData.get(key, None)
if result is not None: return result
result = data.get(key, None)
if result is not None: return result
return default
envList = get('environmentList')
lenEnv = len(envList)
solList = get('solutionList', [''])
lenSol = len(solList)
components = get('components', ())
lenComp = max(1, len(components))
envIndex = np.arange(lenEnv) # the x locations for the groups
groupWidth = 1 - get('marginGroups', 0.2)
marginViolins = get('marginViolins', 0.02)
width = groupWidth / lenSol - marginViolins # the width of the violins
samples = get("samples")
colors = get('mainColors',
['#0072bc', '#d95218', '#edb021', '#7a8cbf', '#009d70', '#979797', '#53b2ea',
"#ee4c9c"] + ['C%d' % (i % 10) for i in range(100)])
if figure and axis:
fig, ax = figure, axis
else:
fig, ax = plt.subplots()
fig.set_size_inches(get('figWidth', 600) / dpi, get('figHeight', 350) / dpi)
fig.set_dpi(dpi)
fake_handles = []
for solIdx in range(lenSol):
solSamples = samples[solIdx]
color = colors[solIdx % len(colors)]
fake_handles.append(mpatches.Patch(color=color))
positions = envIndex - groupWidth / 2 + (width + marginViolins) * (solIdx + 0.5)
widths = [width - marginViolins] * lenEnv
parts = ax.violinplot(solSamples, positions=positions, widths=widths, showmeans=False, showmedians=False,
showextrema=False)
for i, pc in enumerate(parts['bodies']):
pc.set_facecolor(color)
pc.set_edgecolor('black')
pc.set_alpha(1)
quartile1, medians, quartile3 = np.percentile(solSamples, [25, 50, 75], axis=1)
whiskers = np.array(
[adjacent_values(sorted_array, q1, q3)
for sorted_array, q1, q3 in zip(solSamples, quartile1, quartile3)])
whiskersMin, whiskersMax = whiskers[:, 0], whiskers[:, 1]
ax.scatter(positions, medians, marker='o', color='white', s=20, zorder=3)
ax.vlines(positions, quartile1, quartile3, color='k', linestyle='-', lw=5)
ax.vlines(positions, whiskersMin, whiskersMax, color='k', linestyle='-', lw=1)
if get("showLegend", lenSol > 1):
font = FontProperties(weight='regular', size=get('legendFontSize', 20))
if get("legendLoc", None) is None and get("legendOutside", False):
ax.legend(fake_handles, solList, prop=font, bbox_to_anchor=(0, 1.02, 1, 0.2 * lenComp),
loc="lower left", mode="expand", borderaxespad=0, ncol=lenSol, handlelength=1)
else:
ax.legend(fake_handles, solList, frameon=False, loc=get('legendLoc', 'best'), prop=font,
ncol=get('legendColumn', 1), handlelength=1)
font = FontProperties(weight='regular', size=get('xFontSize', 20))
ax.set_xlabel(get('xTitle', ""), fontproperties=font)
paddingLeft = get('paddingLeft', 0.1)
paddingRight = get('paddingRight', 0.1)
ax.set_xlim([0 - groupWidth / 2 - paddingLeft, len(envList) - 1 + groupWidth / 2 + paddingRight])
ticks = get('xTicks&Labels', None)
if ticks:
ax.tick_params(which='minor', length=0)
if len(ticks) == 2 and nonEmptyIterable(ticks[0]) and nonEmptyIterable(ticks[1]):
ax.set_xticks(ticks[0])
ax.set_xticklabels(ticks[1])
else:
ax.set_xticks(ticks)
ax.set_xticklabels([str(i) for i in ticks])
else:
ax.set_xticks(envIndex)
ax.set_xticklabels(envList)
font = FontProperties('sans-serif', weight='regular', size=get('xFontSize', 20) - 4)
for tick in ax.xaxis.get_major_ticks():
tick.label.set_fontproperties(font)
if get('xTickRotate', False):
tick.label.set_rotation(45)
font = FontProperties(weight='regular', size=get('yFontSize', 20))
ax.set_ylabel(get('yTitle', ""), fontproperties=font)
font = FontProperties('sans-serif', weight='regular', size=get('yFontSize', 20) - 4)
for tick in ax.yaxis.get_major_ticks():
tick.label.set_fontproperties(font)
if get('ySci'):
ax.ticklabel_format(style='sci', axis='y', scilimits=get('ySci'))
else:
ax.ticklabel_format(style='plain', axis='y')
ticks = get('yTicks&Labels', None)
if ticks:
if len(ticks) == 2 and nonEmptyIterable(ticks[0]) and nonEmptyIterable(ticks[1]):
ax.set_yticks(ticks[0])
ax.set_yticklabels(ticks[1])
else:
ax.set_yticks(ticks)
ax.get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_yaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())
if get('yLog', False):
ax.set_yscale('log')
if get('yGrid', False):
ax.yaxis.grid(True)
lim = get('yLimit', [])
if len(lim) > 0:
realLimit = lim.copy()
for i in range(2):
if callable(lim[i]):
realLimit[i] = lim[i](ax.get_ylim()[i])
ax.set_ylim(realLimit)
lim = get('xLimit', [])
if len(lim) > 0:
ax.set_xlim(lim)
subAxes = []
for subfigure in get('subfigures', []):
from .plot import Ploter
subAxes.append(Ploter().plot(subfigure, fig, ax))
# TODO add subfigures
try:
fig.tight_layout()
except:
pass
if get('output', True):
fig.savefig('dist/' + name + '.pdf', format='pdf', dpi=dpi, bbox_inches="tight")
plt.show(block=False)
# plt.close('all')
axes.append(ax)
return axes
if __name__ == '__main__':
Violin().draw(data)
while True:
plt.pause(0.5)