-
Notifications
You must be signed in to change notification settings - Fork 2
/
violin.py
112 lines (100 loc) · 3.42 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
import numpy as np
import json
from sklearn.neighbors.kde import KernelDensity
class Violin:
def __init__(self, center='median',
box=(25, 75), whiskers=(5, 95)):
self.center = center
self.box = box
self.whiskers = whiskers
self.series = []
self.seriesId = 0
def addSeries(self, data, name=None):
if name:
self.seriesName = name
else:
self.seriesName = str(self.seriesId)
self.np = np.array(data)
self.series.extend((self._density(), self._whiskers(),
self._box(), self._center()))
self.seriesId += 1
def _center(self):
if self.center == 'median':
c = np.median(self.np)
elif self.center == 'mean':
c = np.mean(self.np)
return {
'linkedTo': str(self.seriesId),
'type': 'scatter',
'name': self.center,
'marker': {
'symbol': 'circle'
},
'color': 'white',
'data': [{'x': c, 'y': self.seriesId, 'name': self.center}]
}
def _box(self):
b = (np.percentile(self.np, self.box[0]),
np.percentile(self.np, self.box[1]))
return {
'linkedTo': str(self.seriesId),
'type': 'line',
'name': 'box',
'marker': {
'symbol': 'circle',
'enabled': False
},
'lineWidth': 8,
'color': 'black',
'data': [{'x': b[0], 'y': self.seriesId,
'name': 'percentile ' + str(self.box[0])},
{'x': b[1], 'y': self.seriesId,
'name': 'percentile ' + str(self.box[1])}]
}
def _whiskers(self):
w = (np.percentile(self.np, self.whiskers[0]),
np.percentile(self.np, self.whiskers[1]))
return {
'linkedTo': str(self.seriesId),
'type': 'line',
'name': 'whiskers',
'marker': {
'symbol': 'circle',
'enabled': False
},
'lineWidth': 2,
'color': 'black',
'data': [{'x': w[0], 'y': self.seriesId,
'name': 'percentile ' + str(self.whiskers[0])},
{'x': w[1], 'y': self.seriesId,
'name': 'percentile ' + str(self.whiskers[1])}]
}
def _density(self):
d = list()
h = dict()
bw = self.np.size ** (-1./5)
kd = KernelDensity(kernel='gaussian',
bandwidth=bw).fit(self.np.reshape(-1, 1))
kd_vals = np.exp(kd.score_samples(self.np.reshape(-1, 1)))
for i, x in enumerate(kd_vals):
h[self.np[i]] = x
for x in sorted(h):
dp = h[x] * (0.4 / max(kd_vals))
d.append([x, self.seriesId - dp, self.seriesId + dp])
return {
'id': str(self.seriesId),
'name': self.seriesName,
'type': 'areasplinerange',
'enableMouseTracking': False,
'marker': {
'symbol': 'circle',
'enabled': False
},
'color': 'Highcharts.getOptions().colors[' +
str(self.seriesId) + ']',
'data': d
}
def series_list(self):
return self.series
def series_json(self):
return json.dumps(self.series)