-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHistogram.py
345 lines (290 loc) · 10.9 KB
/
Histogram.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import wx
import random
import bisect
import sys
import math
def makeColourGradient(frequency1, frequency2, frequency3,
phase1, phase2, phase3,
center = 128, width = 127, len = 50 ):
fp = [(frequency1,phase1), (frequency2,phase2), (frequency3,phase3)]
grad = [wx.Colour(*[round(math.sin(f*i + p) * width + center) for f, p in fp]) for i in range(len+1)]
return grad[1:]
def makePastelColours( len = 50 ):
return makeColourGradient(2.4,2.4,2.4,0,2,4,128,127,len+1)
def lighterColour( c ):
rgb = c.Get( False )
return wx.Colour( *[round(v + (255 - v) * 0.6) for v in rgb] )
def ShimazakiMethod( data, minN = 2, maxN = None ):
# From [email protected]
dataMin = float(min(data))
T = float(max(data)) - dataMin
dataCount = float(len(data))
# Default return: all data points in one bin.
best = ([len(data)], sys.float_info.max, 1, T)
for N in range(minN, min(len(data), maxN or len(data))):
width = T / N
bins = [0] * N
for x in data:
try:
bins[int((x-dataMin) / width)] += 1
except IndexError:
bins[-1] += 1
kBar = dataCount / N
v = math.fsum((k - kBar)**2 for k in bins) / N
cost = (2.0 * kBar - v) / (width ** 2)
if cost < best[1]:
best = (bins, cost, N, width)
return best
def BinByInterval( data, width, minN = 2 ):
dataMin = float(min(data))
N = int((float(max(data)) - dataMin) / width) + 1
bins = [0 for i in range(N)]
for x in data:
try:
bins[int((x-dataMin) / width)] += 1
except IndexError:
bins[-1] += 1
return bins, 0.0, N, width
def BinBySecond( data, minN = 2, maxN = None ):
return BinByInterval( data, 1.0, minN )
def BinBy30Second( data, minN = 2, maxN = None ):
return BinByInterval( data, 30.0, minN )
def BinByMinute( data, minN = 2, maxN = None ):
return BinByInterval( data, 60.0, minN )
def BinBy5Minute( data, minN = 2, maxN = None ):
return BinByInterval( data, 60.0*5.0, minN )
class Histogram(wx.Control):
BinFunc = [ShimazakiMethod, BinBySecond, BinBy30Second, BinByMinute, BinBy5Minute]
BinOptionAuto, BinOptionBySecond, BinOptionBy30Second, BinOptionByMinute, BinOptionBy5Minute = list(range(len(BinFunc)))
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator,
name="HistogramGraph"):
super().__init__(parent, id, pos, size, style, validator, name)
self.SetBackgroundColour('white')
self.binOption = self.BinOptionAuto
self.data = None
self.bins = None
self.binWidth = None
self.rectField = None
self.iSelect = None
# Bind the events related to our control: first of all, we use a
# combination of wx.BufferedPaintDC and an empty handler for
# wx.EVT_ERASE_BACKGROUND (see later) to reduce flicker
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_MOTION, self.OnMove )
def DoGetBestSize(self):
return wx.Size(100, 50)
def SetBinOption( self, option ):
if option < 0:
option = 0
if option >= len(self.BinFunc):
option = len(self.BinFunc) - 1
self.binOption = option
self.setBins()
self.Refresh()
def SetForegroundColour(self, colour):
wx.Control.SetForegroundColour(self, colour)
self.Refresh()
def SetBackgroundColour(self, colour):
wx.Control.SetBackgroundColour(self, colour)
self.Refresh()
def GetDefaultAttributes(self):
return wx.StaticText.GetClassDefaultAttributes()
def ShouldInheritColours(self):
return True
def getIData( self, x, y ):
if not self.bins or not self.rectField:
return None
boxWidth = self.rectField[2] / len(self.bins)
boxHeight = self.rectField[3] / self.barMax
b = int((x - self.rectField[0]) / boxWidth)
h = self.barMax - 1 - int((y - self.rectField[1]) / boxHeight)
return self.coords.get( (b, h), None )
def OnMove( self, event ):
iSelectNew = self.getIData( event.GetX(), event.GetY() )
if iSelectNew != self.iSelect:
self.iSelect = iSelectNew
self.Refresh()
def setBins( self ):
if not self.data:
return
self.dataMax = max(self.data)
self.dataMin = min(self.data)
self.bins, _, _, self.binWidth = self.BinFunc[self.binOption]( self.data )
self.barMax = max(self.bins)
def SetData( self, data, label, category, binOption=BinOptionAuto ):
self.binWidth = 60.0
self.binOption = binOption
self.bins = None
self.iSelect = None
self.data = []
self.label = ['{}'.format(lab) for lab in label]
self.category = ['{}'.format(cat) for cat in category]
if data:
self.data = [float(x) for x in data]
self.setBins()
while len(self.label) < len(self.data):
self.label.append( '' )
while len(self.category) < len(self.data):
self.category.append( '' )
self.categoryColor = makePastelColours( len(set(category)) )
self.categoryMap = {}
i = 0
for c in self.category:
if c not in self.categoryMap:
self.categoryMap[c] = i
i += 1
self.Refresh()
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.Draw(dc)
def OnSize(self, event):
self.Refresh()
event.Skip()
def Draw(self, dc):
size = self.GetClientSize()
width = size.width
height = size.height
tickBorder = 4
backColour = self.GetBackgroundColour()
backBrush = wx.Brush(backColour, wx.SOLID)
dc.SetBackground(backBrush)
dc.Clear()
if not self.bins or width < 50 or height < 50 or self.dataMax == self.dataMin:
return
textWidth, textHeight = dc.GetTextExtent( '00:00' if self.dataMax < 60*60 else '00:00:00' )
xLeft = dc.GetTextExtent( '{}'.format(self.barMax) )[0] + 4 + tickBorder
xRight = width - 8 - tickBorder
yBottom = height - textHeight - 8
yTop = textHeight + 8
# Draw the horizontal label.
# Find some reasonable tickmarks for the x axis.
numlabel = (xRight - xLeft) / (textWidth * 1.5)
d = (self.dataMax - self.dataMin) / float(numlabel)
intervals = [1, 2, 5, 10, 15, 20, 30, 1*60, 2*60, 5*60, 10*60, 15*60, 20*60, 30*60, 1*60*60, 2*60*60, 4*60*60, 8*60*60, 12*60*60, 24*60*60]
d = intervals[bisect.bisect_left(intervals, d, 0, len(intervals)-1)]
dFactor = (xRight - xLeft) / (self.dataMax - self.dataMin)
tStart = int(self.dataMin)
tStart -= tStart % d
if tStart < int(self.dataMin):
tStart += d
dc.SetPen(wx.Pen('light gray', 1))
for t in range(tStart, int(self.dataMax), d):
x = round(xLeft + (t - self.dataMin) * dFactor)
if x < xLeft:
continue
if t < 60*60:
s = '{}:{:02d}'.format((t // 60), t%60)
else:
s = '{}:{:02d}:{:02d}'.format(t//(60*60), (t // 60)%60, t%60)
w, h = dc.GetTextExtent(s)
dc.DrawText( s, round(x - w/2), yBottom + 4)
dc.DrawText( s, round(x - w/2), 0 + 4 )
dc.DrawLine( x, yBottom+2, x, yTop )
# Find some reasonable tickmarks for the y axis.
numlabel = float(yBottom - yTop) / (textHeight * 3)
d = self.barMax / numlabel
intervals = [1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500]
d = intervals[bisect.bisect_left(intervals, d, 0, len(intervals)-1)]
dFactor = (yBottom - yTop) / float(self.barMax)
dc.SetPen(wx.Pen('light gray', 1))
for i in range(0, self.barMax+1, d):
s = '{}'.format(i)
y = yBottom - round(i * dFactor)
w, h = dc.GetTextExtent(s)
dc.DrawText( s, xLeft - w - 2 - tickBorder, round(y-textHeight/2) - 1)
dc.DrawLine( xLeft, y, xRight + tickBorder, y )
# Draw the data rectangles.
pen = wx.Pen('dark gray', 1)
dc.SetPen( pen )
rSelect = None
self.rectField = (xLeft, yTop, float(xRight-xLeft), float(yBottom-yTop))
boxWidth = self.rectField[2] / len(self.bins)
boxHeight = self.rectField[3] / self.barMax
lenBins = len(self.bins)
iBin = [0 for i in range(lenBins)]
xBin = [xLeft + int(i * boxWidth) for i in range(len(self.bins)+1)]
yHeight = [yBottom - int(i * boxHeight) for i in range(self.barMax+1)]
self.coords = {}
brushes = [wx.Brush(c) for c in self.categoryColor]
for i, (v, lab, cat) in enumerate(zip(self.data, self.label, self.category)):
dc.SetBrush( brushes[self.categoryMap[cat]] )
b = min(lenBins-1, int((v-self.dataMin) / self.binWidth))
self.coords[(b, iBin[b])] = i
iBin[b] += 1
r = wx.Rect( xBin[b], yHeight[iBin[b]], xBin[b+1] - xBin[b], yHeight[iBin[b]-1] - yHeight[iBin[b]] )
if i == self.iSelect:
rSelect = r
dc.SetPen( wx.Pen(wx.Colour(255,255,0), 1) )
dc.DrawRectangle( r )
if i == self.iSelect:
dc.SetPen( pen )
# draw the baseline
dc.SetPen(wx.Pen(wx.BLACK, 2))
dc.DrawLine(xLeft-tickBorder, yBottom, xRight+tickBorder, yBottom)
# draw the hoverhelp
if rSelect:
t = self.data[self.iSelect]
if t < 60:
s = '{:.2f}'.format( t )
else:
t = int(t)
if t < 60*60:
s = '{}:{:02d}'.format((t//60), t%60)
else:
s = '{}:{:02d}:{:02d}'.format(t//(60*60), (t//60)%60, t%60)
label = self.label[self.iSelect]
category = self.category[self.iSelect]
margin = 4
widthMax = max( dc.GetTextExtent(v)[0] for v in (s, label, category) ) + margin * 2
textWidth, textHeight = dc.GetTextExtent( s )
heightMax = textHeight * 3 + margin * 2
rHover = wx.Rect( rSelect.GetX() + rSelect.GetWidth() // 2 - widthMax, rSelect.GetY(), widthMax, heightMax )
if rHover.GetLeft() < 0:
rHover.SetX( 0 )
if rHover.GetBottom() > height:
rHover.SetY( height - rHover.GetHeight() )
dc.SetPen( wx.Pen(wx.BLACK, 1) )
dc.SetBrush( wx.Brush(wx.Colour(255,255,153)) )
dc.DrawRectangle( rHover )
xLeft = rHover.GetLeft() + margin
yCur = rHover.GetY() + margin
for v in (s, label, category):
dc.DrawText( v, xLeft, yCur )
yCur += textHeight
def OnEraseBackground(self, event):
pass
if __name__ == '__main__':
# Data from Old Faithful
data = [float(x) for x in
'''4.37 3.87 4.00 4.03 3.50 4.08 2.25 4.70 1.73 4.93 1.73 4.62
3.43 4.25 1.68 3.92 3.68 3.10 4.03 1.77 4.08 1.75 3.20 1.85
4.62 1.97 4.50 3.92 4.35 2.33 3.83 1.88 4.60 1.80 4.73 1.77
4.57 1.85 3.52 4.00 3.70 3.72 4.25 3.58 3.80 3.77 3.75 2.50
4.50 4.10 3.70 3.80 3.43 4.00 2.27 4.40 4.05 4.25 3.33 2.00
4.33 2.93 4.58 1.90 3.58 3.73 3.73 1.82 4.63 3.50 4.00 3.67
1.67 4.60 1.67 4.00 1.80 4.42 1.90 4.63 2.93 3.50 1.97 4.28
1.83 4.13 1.83 4.65 4.20 3.93 4.33 1.83 4.53 2.03 4.18 4.43
4.07 4.13 3.95 4.10 2.72 4.58 1.90 4.50 1.95 4.83 4.12'''.split()]
data = sorted(60.0*60.0 + random.normalvariate(10.0*60.0, 5.0*60.0) for i in range(100))
optimal = ShimazakiMethod( data )
sys.stdout.write( '{}\n'.format(optimal) )
bins, cost, N, width = optimal
xMin = min(data)
hMax = max(bins)
hFactor = 1 if hMax < 40 else 40.0 / hMax
for i, h in enumerate(bins):
sys.stdout.write( '{:9.3f} {:9.3f}: {:6d}: {}\n'.format(xMin + i * width, xMin + (i+1) * width, h, '*' * int(h * hFactor)) )
app = wx.App(False)
mainWin = wx.Frame(None,title="Histogram", size=(600,400))
histogram = Histogram(mainWin)
mainWin.SetDoubleBuffered(True)
random.seed( 10 )
t = 55*60
tVar = t * 0.15
#histogram.SetData( [random.normalvariate(t, tVar) for x in range(90)] )
histogram.SetData( data, [], [random.randint(0,5) for d in data] )
mainWin.Show()
app.MainLoop()