-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHistogramPanel.py
157 lines (132 loc) · 5.21 KB
/
HistogramPanel.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
import wx
import os
import bisect
import Utils
import Model
from ReadSignOnSheet import ExcelLink
from FixCategories import FixCategories
from GetResults import GetResults
from Histogram import Histogram
class GetCategoryName:
def __init__( self, categories ):
self.categories = categories
self.lastCategory = categories[0]
self.catName = {c:c.fullname for c in categories}
def __call__( self, bib ):
race = Model.race
if race.inCategory( bib, self.lastCategory ):
return self.catName[self.lastCategory]
for c in self.categories:
if race.inCategory( bib, c ):
self.lastCategory = c
return self.catName[c]
return ''
class HistogramPanel( wx.Panel ):
def __init__( self, parent, id=wx.ID_ANY, size=wx.DefaultSize ):
super().__init__( parent, id, size=size )
self.category = None
self.hbs = wx.BoxSizer( wx.HORIZONTAL )
self.categoryLabel = wx.StaticText( self, label = _('Category:') )
self.categoryChoice = wx.Choice( self )
self.Bind(wx.EVT_CHOICE, self.doChooseCategory, self.categoryChoice)
self.hbs.Add( self.categoryLabel, flag=wx.LEFT | wx.ALIGN_CENTRE_VERTICAL, border=4 )
self.hbs.Add( self.categoryChoice, flag=wx.ALL, border=4 )
self.hbs.Add( wx.StaticText(self, label=_('Lap')+':'), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=24 )
self.lapOption = wx.Choice(self, choices=[_('Last')])
self.lapOption.SetSelection( 0 )
self.lapOption.Bind( wx.EVT_CHOICE, self.doChooseLap )
self.hbs.Add( self.lapOption, flag=wx.ALL, border=4 )
self.lap = 0
self.hbs.Add( wx.StaticText(self, label=_('Bin by')+':'), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=24 )
self.binOption = wx.Choice(self, choices=[_('Auto'), _('1 second'), _('30 seconds'), _('1 minute'), _('5 minutes')])
self.binOption.SetSelection( 0 )
self.binOption.Bind( wx.EVT_CHOICE, self.doChooseBinOption )
self.hbs.Add( self.binOption, flag=wx.ALL, border=4 )
self.hbs.Add( wx.StaticText(self, label=_('Bin width')+':'), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=24 )
self.binWidthLabel = wx.StaticText( self )
self.hbs.Add( self.binWidthLabel, 1, flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=4 )
self.histogram = Histogram( self )
bs = wx.BoxSizer( wx.VERTICAL )
bs.Add( self.hbs, 0, wx.EXPAND )
bs.Add( self.histogram, 1, wx.EXPAND )
self.SetDoubleBuffered( True )
self.SetSizer( bs )
def doChooseLap( self, event ):
self.lap = self.lapOption.GetSelection()
self.refresh()
def fixBinWidth( self ):
binWidthLabel = Utils.formatTime(self.histogram.binWidth or 0.0)
if binWidthLabel != self.binWidthLabel.GetLabel():
self.binWidthLabel.SetLabel( binWidthLabel )
self.GetSizer().Layout()
def doChooseBinOption( self, event ):
self.histogram.SetBinOption( self.binOption.GetSelection() )
self.fixBinWidth()
def doChooseCategory( self, event ):
Model.setCategoryChoice( self.categoryChoice.GetSelection(), 'histogramCategory' )
self.lap = 0
self.refresh()
def refresh( self ):
race = Model.race
if not race:
self.histogram.SetData( [], [], [] )
return
self.category = FixCategories( self.categoryChoice, getattr(race, 'histogramCategory', 0) )
if self.category is None:
categories = race.getCategories( startWaveOnly=True )
if not categories:
return
getCatName = GetCategoryName( categories )
elif self.category.catType == Model.Category.CatWave:
components = race.getComponentCategories( self.category )
if components:
getCatName = GetCategoryName( components )
else:
def getCatName( bib, name=self.category.fullname ):
return name
else:
def getCatName( bib, name=self.category.fullname ):
return name
results = GetResults( self.category )
Finisher = Model.Rider.Finisher
data, label, category = [], [], []
maxLaps = None
for rr in results:
if rr.status != Finisher or not rr.raceTimes or len(rr.raceTimes) < 2:
continue
if maxLaps is None:
if race.isRunning():
maxLaps = bisect.bisect_left( rr.raceTimes, race.curRaceTime(), 0, len(rr.raceTimes)-1 )
else:
maxLaps = len(rr.raceTimes) - 1
if self.lapOption.GetCount() != maxLaps + 1:
self.lapOption.SetItems( [_('Last')] + ['{}'.format(i) for i in range(1,maxLaps+1)] )
if self.lap >= self.lapOption.GetCount():
self.lap = 0
self.lapOption.SetSelection( self.lap )
if self.lap:
maxLaps = self.lap
if len(rr.raceTimes) - 1 < maxLaps:
continue
data.append( rr.raceTimes[self.lap or -1] )
label.append( '{}: {}'.format(rr.num, rr.short_name()) )
category.append( getCatName(rr.num) )
self.histogram.SetData( data, label, category, self.binOption.GetSelection() )
self.fixBinWidth()
if __name__ == '__main__':
app = wx.App(False)
app.SetAppName("CrossMgr")
Utils.disable_stdout_buffering()
race = Model.newRace()
race._populate()
fnameRiderInfo = os.path.join(Utils.getHomeDir(), 'SimulationRiderData.xlsx')
sheetName = 'RiderData'
race.excelLink = ExcelLink()
race.excelLink.setFileName( fnameRiderInfo )
race.excelLink.setSheetName( sheetName )
race.excelLink.setFieldCol( {'Bib#':0, 'LastName':1, 'FirstName':2, 'Team':3} )
mainWin = wx.Frame(None, title="Primes", size=(800,700) )
histogram = HistogramPanel( mainWin )
mainWin.Show()
histogram.refresh()
app.MainLoop()