-
Notifications
You must be signed in to change notification settings - Fork 0
/
highwayAnalyze.py
289 lines (286 loc) · 14.2 KB
/
highwayAnalyze.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
__author__ = 'thomas'
from helpers import *
from wafThread import *
import os
import tarfile
import tempfile
from PySide.QtGui import QWidget, QLabel, QVBoxLayout, QGroupBox, QPushButton, QMessageBox, QTextBrowser, QFont, QPixmap, QImage, QTextOption
from PySide.QtCore import Slot, QThread, Qt
class HighwayAnalyzeWidget(QWidget):
def __init__(self):
super(HighwayAnalyzeWidget,self).__init__()
self.averageSimulationTime = 325.0
mainLayout = QVBoxLayout()
mainLayout.setAlignment(Qt.AlignTop|Qt.AlignHCenter)
mainLayout.setSpacing(0)
self.setLayout(mainLayout)
topWidget = QWidget()
self.topLayout = QHBoxLayout()
self.topLayout.setSpacing(0)
topWidget.setLayout(self.topLayout)
topLeftWidget = QWidget()
self.topLeftLayout = QVBoxLayout()
topLeftWidget.setLayout(self.topLeftLayout)
self.setupTopGroup()
self.setupFilterGroup()
self.topLayout.addWidget(topLeftWidget)
self.layout().addWidget(topWidget)
# Button and log
buttonAndLogWidget = QWidget()
buttonAndLogWidget.setLayout(QVBoxLayout())
# Analyze the results BUTTON
self.analyzeResultsButton = QPushButton('Analyze the results')
self.analyzeResultsButton.clicked.connect(self.analyzeResults)
self.analyzeResultsButton.setEnabled(False)
buttonAndLogWidget.layout().addWidget(self.analyzeResultsButton)
# LOG
self.logText = QTextBrowser()
self.logText.setFont(QFont('Century Gothic', 7))
self.logText.setWordWrapMode(QTextOption.NoWrap)
buttonAndLogWidget.layout().addWidget(self.logText)
self.topLayout.addWidget(buttonAndLogWidget)
self.results = []
self.logFile = os.path.join(self.resultsPath(), 'analyze_'+os.uname()[1]+'.log')
# Image
self.picLabel = QLabel()
self.picLabel.setAlignment(Qt.AlignHCenter)
#self.picLabel.resize(800,600)
self.picLabel.setMinimumSize(800,600)
self.layout().addWidget(self.picLabel)
def setupTopGroup(self):
topGroup = QGroupBox("Simulation Results")
self.resultsPathLineEdit = SimpleOption('resultsPath','Results Path','/home/thomas/Dropbox/Keio/research/results/')
topGroupLayout = QVBoxLayout()
topGroupLayout.setSpacing(0)
topGroupLayout.setAlignment(Qt.AlignTop)
topGroupLayout.addWidget(self.resultsPathLineEdit)
self.loadResultsLabel = QLabel()
self.loadResultsLabel.setAlignment(Qt.AlignHCenter)
topGroupLayout.addWidget(self.loadResultsLabel)
self.loadResultsButton = QPushButton("Load the results")
self.loadResultsButton.clicked.connect(self.loadResults)
topGroupLayout.addWidget(self.loadResultsButton)
topGroup.setLayout(topGroupLayout)
self.topLeftLayout.addWidget(topGroup)
def resultsPath(self):
return self.resultsPathLineEdit.getValue()
def setupFilterGroup(self):
filterGroup = QGroupBox('Filter the results')
filterGroupLayout = QVBoxLayout()
filterGroupLayout.setSpacing(0)
filterGroupLayout.setAlignment(Qt.AlignTop)
# Distribution Model
self.filterDistribution = SimpleComboboxOption('dis','Speed Distribution Model',3, True, 'Uniform','Exponential','Normal','Log Normal')
filterGroupLayout.addWidget(self.filterDistribution)
self.filterDistribution.setVisible(False)
# Number of results per point
self.filterNb = SimpleSpinOption('simuNbMin', 'Minimum results for a given setting', 10, integer=True, checkable=True)
self.filterNb.checkBox.setChecked(True)
filterGroupLayout.addWidget(self.filterNb)
# Filter the date
dateWidget = QWidget()
dateWidget.setLayout(QHBoxLayout())
self.filterDate = QCheckBox('After')
self.filterDate.setChecked(QSettings().value('filterDate','0')=='1')
dateWidget.layout().addWidget(self.filterDate)
self.filterDateYear = QComboBox()
for m in xrange(2010,2012):
self.filterDateYear.addItem(str(m))
self.filterDateYear.setCurrentIndex(int(QSettings().value('dateYear', 1)))
dateWidget.layout().addWidget(self.filterDateYear)
dateWidget.layout().addWidget(QLabel('Year'))
self.filterDateMonth = QComboBox()
for m in xrange(1,13):
self.filterDateMonth.addItem(str(m))
self.filterDateMonth.setCurrentIndex(int(QSettings().value('dateMonth', 4)))
dateWidget.layout().addWidget(self.filterDateMonth)
dateWidget.layout().addWidget(QLabel('Month'))
self.filterDateDay = QComboBox()
for d in xrange(1,32):
self.filterDateDay.addItem(str(d))
self.filterDateDay.setCurrentIndex(int(QSettings().value('dateDay', 0)))
dateWidget.layout().addWidget(self.filterDateDay)
dateWidget.layout().addWidget(QLabel('Day'))
filterGroupLayout.addWidget(dateWidget)
filterGroup.setLayout(filterGroupLayout)
self.topLeftLayout.addWidget(filterGroup)
# Filter the scenario
self.filterScenar = SimpleComboboxOption('scenar','Scenario',1, True, 'vanet-highway-test-thomas','vanet-highway-scenario2')
filterGroupLayout.addWidget(self.filterScenar)
# Filter gap
self.filterGap = SimpleSpinOption('avgdistanalyze','Average Distance (m)',100, checkable=True)
filterGroupLayout.addWidget(self.filterGap)
def loadResults(self):
self.loadResultsButton.setText('Loading the results...')
self.loadResultsButton.setEnabled(False)
self.results = []
self.resultsThread = LoadResults(resultsPath=self.resultsPath())
self.resultsThread.done.connect(self.loadResultsDone)
self.resultsThread.error.connect(self.loadResultsError)
self.resultsThread.start()
@Slot(str)
def loadResultsError(self, errorMsg):
QMessageBox.critical(self, 'Error!', errorMsg)
self.analyzeResultsButton.setEnabled(False)
@Slot(dict)
def loadResultsDone(self, results):
self.results = results
resultsNb = len(self.results)
msg= 'Found %d Simulation Results\n(%d invalid files, %d simulations failed)' % (resultsNb, self.resultsThread.fileUnloadable, self.resultsThread.simulationFailed)
self.loadResultsLabel.setText(msg)
#self.calculateAverageSimulationTime()
self.loadResultsButton.setText('Reload the results')
self.loadResultsButton.setEnabled(True)
self.analyzeResultsButton.setEnabled(True)
if resultsNb>0:
self.analyzeResults()
def calculateAverageSimulationTime(self):
totalTime = 0
nb = 0
for r in self.results:
if 'simulationTime' in r:
#print 'simu time: %s' % r['simulationTime']
totalTime += int(r['simulationTime'])
nb += 1
#print 'from %s: [%3.3f,%3.3f]' % (r['filename'], r['settings']['prate'],r['results']['timeToReachDest'])
if nb<=0:
errorMsg= 'No simulation found with simulationTime'
QMessageBox.critical(self, 'Error!', errorMsg)
self.averageSimulationTime = 290.0
else:
self.averageSimulationTime= totalTime/nb
self.loadResultsLabel.setText(self.loadResultsLabel.text()+'\nAverage simulation time: %3.0f'%self.averageSimulationTime)
def fil(self, r):
return self.checkDis(r) and self.checkDate(r) and self.checkGap(r) and self.checkScenario(r)
def checkDis(self,r):
return (not self.filterDistribution.checkBox.isChecked()) or r['settings']['dis']==self.filterDistribution.getValue()
def checkDate(self, r):
return (not self.filterDate.isChecked()) or (r['date'] >= datetime(int(self.filterDateYear.currentText()), int(self.filterDateMonth.currentText()), int(self.filterDateDay.currentText())))
def checkScenario(self, r):
return (not self.filterScenar.checkBox.isChecked()) or r['scenario']==self.filterScenar.getName()
def checkGap(self, r):
return (not self.filterGap.checkBox.isChecked()) or float(r['settings']['avgdist'])==float(self.filterGap.getValue())
def analyzeResults(self):
self.saveSettings()
if len(self.results)<=0:
QMessageBox.critical(self, 'Error!', 'No results loaded :s')
return
self.log("=== ANALYZING RESULTS ===")
self.logText.clear()
p=0
meanValues = {}
uniqueName = dateToFilename()
gnuPlotDataFile =os.path.join(self.resultsPath(),'graphs/' + uniqueName+'.dat')
with open(gnuPlotDataFile,'w') as data:
while p<=100:
nb = 0
totalTimeToReachDest = 0
totalSimulationTime = 0
for result in filter(lambda x: x['settings']['prate']==p and self.fil(x), self.results):
totalSimulationTime += float(result['simulationTime'])
totalTimeToReachDest += float(result['results']['timeToReachDest'])
nb += 1
if self.filterNb.checkBox.isChecked():
minNb = self.filterNb.getValue()
else:
minNb = 0
if nb>minNb:
meanSimulationTime = float(1.0*totalSimulationTime/nb)
meanTimeToReachDest = float(1.0*totalTimeToReachDest/nb)
meanValues[p] = {'prate':p,
'simuLationTime':meanSimulationTime,
'simulationsNb':nb,
'timeToReachDest':meanTimeToReachDest}
self.log(meanValues[p])
toPlot = '%s %s' % (p, meanValues[p]['timeToReachDest'])
#print toPlot
data.write(toPlot+'\n')
p += 1
data.close()
if len(meanValues)>0:
outputPic = 'graphs/' + uniqueName
s = subprocess.Popen(['./toPlot.sh', outputPic, gnuPlotDataFile], cwd=self.resultsPath())
s.wait()
outputPicPath = os.path.join(self.resultsPath(),outputPic+'.svg')
pic = QImage(outputPicPath)
#pic = pic.scaled(QSize(640,480))
self.picLabel.setPixmap(QPixmap(pic))
#os.system(os.path.join(self.resultsPath(),'toPlot.sh'))
else:
QMessageBox.critical(self, 'Error!', 'No simulation satisfies the criteria...')
def log(self, txt):
toLog = str(txt)
toLogFile = '%s | %s' % (datetime.now(), txt)
with open(self.logFile, 'a') as logFile:
logFile.write(toLogFile+'\n')
self.logText.append(toLog)
def saveSettings(self):
QSettings().setValue('simuNbMin', self.filterNb.getValue())
QSettings().setValue('avgdistanalyze', self.filterGap.getValue())
QSettings().setValue('scenar', self.filterScenar.getValue())
QSettings().setValue('dis', self.filterDistribution.getValue())
QSettings().setValue('dateDay', self.filterDateDay.currentIndex())
QSettings().setValue('dateMonth', self.filterDateMonth.currentIndex())
QSettings().setValue('dateYear', self.filterDateYear.currentIndex())
if self.filterDate.isChecked():
QSettings().setValue('filterDate', '1')
else:
QSettings().setValue('filterDate', '0')
class LoadResults(QThread):
done = Signal(list)
error = Signal(str)
def __init__(self, resultsPath):
super(LoadResults,self).__init__()
self.resultsPath = resultsPath
self.fileUnloadable = 0
self.simulationFailed = 0
self.results = []
def run(self):
results = []
self.fileUnloadable = 0
self.simulationFailed = 0
if os.path.exists(self.resultsPath):
tempdir = tempfile.mkdtemp(prefix='results_tmpdir',dir=self.resultsPath)
# temporary extract the tar.gz files in this folder
for filename in filter(lambda x: x.endswith('.tar.gz') and x.startswith('results'), os.listdir(self.resultsPath)):
tar = tarfile.open(os.path.join(self.resultsPath,filename), "r:gz")
tar.extractall(tempdir)
tar.close()
for path, subdirs, files in os.walk(self.resultsPath):
for name in filter(lambda x: x.endswith('.txt') and not x.endswith('_ambu.txt'), files):
#for name in files:
filePath = os.path.join(path,name)
with open(filePath) as result:
try:
r = json.loads(result.read())
except:
#print 'errra with file %s' % name
self.fileUnloadable += 1
else:
#print r['date']
if 'results' in r and 'timeToReachDest' in r['results'] and 'succeed' in r and r['succeed'] == 1:
r['filename'] = name
fileDate = datetime.fromtimestamp(os.path.getmtime(filePath))
r['date'] = fileDate
if 'avgdist' not in r['settings']:
r['settings']['avgdist'] = 100
if 'scenario' not in r:
if fileDate >= datetime(2011, 6, 17):
r['scenario'] = 'vanet-highway-scenario2'
else:
r['scenario'] = 'vanet-highway-test-thomas'
# print fileDate
# print '--> %s' % r['scenario']
results.append(r)
else:
self.simulationFailed += 1
result.close()
# remove the temporary folder
for filename in os.listdir(tempdir):
os.remove(os.path.join(tempdir,filename))
os.rmdir(tempdir)
self.results = results
self.done.emit(results)
else:
errorMsg = 'The directory containing the results (%s) does not exist' % self.resultsPath
self.error.emit(errorMsg)