-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataProcess-csvfile.py
executable file
·543 lines (443 loc) · 13.3 KB
/
dataProcess-csvfile.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/home/chris/anaconda2/bin/python2
#
# Data collection and processing.
# Main tasks:
# 1. scan target directory to get file name
# 2. read each JSON file
# 3. save data to dictionary
# 4. calculate statistic, e.g. mean, variance, confidence intervals, CDF
# 5. save data to file for GNU Plot or matplot
#
#
# @file dataProcess.py
# @author Chris Shen
# @date 2018-08-15
# @version $Id$
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import optparse
# import subprocess
# import random
# import time
import numpy as np
import matplotlib.pyplot as plt
# pretty printer
import pprint
# for debuging
# import logging
# for JSON parsing
# import json
# for CSV file parsing
import csv
# for parsing XML
# import xml.etree.ElementTree as ET
def optionsSet():
optParser = optparse.OptionParser()
optParser.add_option("-p", "--ScriptPrefix",
type=str,
dest="prefix",
default="ESTN",
help="Provide the prefix for this script for file generation."
)
optParser.add_option("-d", "--Directory",
type=str,
dest="directory",
default="/home/chris/usr/CTR_TVT/ESTN/sim/result",
help="Directory to scan"
)
optParser.add_option("-o", "--Output",
type=str,
dest="outputDir",
default="",
help="output data to a directory"
)
# simulation mode option
optParser.add_option("-x",
type=str,
dest="xaxis",
default="density",
help="x axis parameter for simulation [default: %default] [density, BGTI]"
)
# simulation mode option
optParser.add_option("-m",
type="int",
dest="mode",
default=3,
metavar="NUM",
help="mode for simulation [default: %default] [0:Dijkstra, 1:CTR, 2:SAINT, 3:SAINT+CTR, 4:Actuated, 5:SAINT+Actuated, 6:Dijkstra+CTR, 7:Dijkstra+Actuated, 8:StaticTL]"
)
# CTR mode
# 0: compatible mode, compatible lanes pass
# 1: maximum mode, maximum lanes pass
# 2: combimed mode of 0 and 1
# 3: original CTR in the 2013 paper, group CTT comparison
optParser.add_option("--CTRMode",
dest="CTRMode",
type="int",
default=2
)
options, args = optParser.parse_args()
return options
def cdfMaking(inDic, outCDFData):
''' Compute CDF, return to cdfData
args:
inDic (dict):
'''
# if not isinstance(sublist, str): to remove string in the list, which is file name
dataList = [item for sublist in inDic.values() if not isinstance(sublist, str) for item in sublist]
# dataList
dataList = sorted(dataList)
# print("dataList: ", dataList)
actFreq = []
for i in dataList:
if actFreq:
actFreq.append( 1.0/len(dataList) + actFreq[len(actFreq)-1])
else:
actFreq.append( 1.0/len(dataList) )
# print("actFreq: ", actFreq)
# cdfData = []
for ind, i in enumerate(dataList):
# print(actFreq[dataList.index(i)])
outCDFData.append([i, round(actFreq[ind], 6)])
# print("outCDFData: ", outCDFData)
# if inDic.has_key('filename'):
# outCDFData.append(['filename', inDic['filename']])
# return cdfData
def readData(xAxis, yAxisDataPoint, targetDic):
''' Save data from each file, save it to the targetDic
args:
xAxis (str, int): x axis point
yAxisDataPoint (float): raw data point
return:
targetDic (dict):
'''
key = xAxis
valEle = 0
if yAxisDataPoint:
valEle = float(yAxisDataPoint)
if targetDic.has_key(key):
if valEle:
targetDic[key].append(valEle)
else:
if valEle:
targetDic[key]=[valEle]
def readRawData(xAxis, yAxisDataPoint, targetDic):
''' Save data from each file, save it to the targetDic
args:
xAxis (str, int): x axis point
yAxisDataPoint (float): raw data point
return:
targetDic (dict):
'''
key = xAxis
valEle = float(yAxisDataPoint)
if targetDic.has_key(key):
targetDic[key].append(valEle)
else:
targetDic[key]=[valEle]
def meanCompute(inputDic, retDic):
''' Compute mean, return to retDic with list value
args:
inputDic (dict): raw data
retDic (dict): return mean
return:
'''
for key, valList in inputDic.viewitems():
if not isinstance(key, str):
retDic[key] = [sum(valList)/len(valList)]
def variCompute(inDic, inDicMean, retDic):
''' Compute variance, return to retDic
args:
inDic (dict): raw data
inDicMean (dict): mean data
retDic (dict): return variance
'''
# print(inDic)
# print(inDicMean)
for key, valList in inDic.viewitems():
if not isinstance(key, str):
sum_var = 0.0
for value in valList:
sum_var = sum_var + pow(value-inDicMean.get(key)[0], 2)
if len(valList) > 1:
# print(valList)
vari = sum_var / (len(valList)-1)
else:
vari = 0
retDic[key] = vari
def confCompute(inDic, inVari, retDic):
''' Compute confidence interval, return to retDic
args:
inDic (dict): raw input data
inVari (dict): input variance data
retDic (dict): return confience interval data
'''
for key, val in inVari.viewitems():
# student t dis., T(10-1, 90%) = 1.833, T(6-1, 90%) = 2.015
stuT_9 = 1.833
stuT_5 = 2.015
retDic[key] = stuT_9 * pow(inVari[key]/len(inDic[key]), 1/2)
def mergeMeanAndConfi(inDicMean, inDicConf):
''' Compute confidence interval, return to retDic
args:
inDic (dict): raw input data
inVari (dict): input variance data
retDic (dict): return confience interval data
'''
for key, val in inDicMean.viewitems():
if not isinstance(key, str):
inDicMean[key].append(inDicConf[key])
def outData(inputDicDataRaw, outputDicMeanData, outputCDF):
''' output data, mean and CDF data
args:
inputDicDataRaw (dict): input raw data
outputDicMeanData (dict): output mean and confidence interval data
outputCDF (list): output CDF data
'''
dicVari = {}
variCompute(inDic=inputDicDataRaw,
inDicMean=outputDicMeanData,
retDic=dicVari)
dicConf = {}
confCompute(inDic=inputDicDataRaw,
inVari=dicVari,
retDic=dicConf)
mergeMeanAndConfi(inDicMean=outputDicMeanData,
inDicConf=dicConf)
cdfMaking(inDic=inputDicDataRaw,
outCDFData=outputCDF)
def processData():
''' Process collected data, generate confidence interval and CDF
args:
inDic (dict): raw input data
inVari (dict): input variance data
retDic (dict): return confience interval data
'''
# M3CTR0
# PDR
outData(inputDicDataRaw=g_dicPDRRaw_BGTI,
outputDicMeanData=g_dicMeanPDR_BGTI,
outputCDF=g_cdfPDR_BGTI)
# mean
outData(inputDicDataRaw=g_dicMeanE2ERaw_BGTI,
outputDicMeanData=g_dicMeanE2E_BGTI,
outputCDF=g_cdfMeanE2E_BGTI)
# max
outData(inputDicDataRaw=g_dicMaxE2ERaw_BGTI,
outputDicMeanData=g_dicMeanMaxE2E_BGTI,
outputCDF=g_cdfMaxE2E_BGTI)
# # Print for debug
# print("M3CTR0")
# pprint.pprint(g_dicMeanPDR_BGTI)
# pprint.pprint(g_dicMeanE2E_BGTI)
# pprint.pprint(g_dicMeanMaxE2E_BGTI)
# pprint.pprint(g_cdfPDR_BGTI)
# pprint.pprint(g_cdfMeanE2E_BGTI)
# pprint.pprint(g_cdfMaxE2E_BGTI)
def writeFilename(peType, mode, targetDic):
key = 'filename'
value = '-'+str(peType)+'-m-'+str(mode)
if key not in targetDic:
targetDic[key] = value
def saveDataToFile(filename, inputDicData):
inputDataKeySorted = sorted(inputDicData.keys())
print(filename)
with open(filename, 'w') as f:
for key in inputDataKeySorted:
value = inputDicData[key]
length = len(value)
if length == 2:
# key: x point, value[0]: y point, value[1]: error bar
f.write(str(key)+' '+str(value[0])+' '+str(value[1])+'\n')
elif length == 1:
# key: x point, value[0]: y point
f.write(str(key)+' '+str(value[0])+'\n')
def saveCDFDataToFile(filename, inputCDFData):
with open(filename, 'w') as f:
for cdf in inputCDFData:
# cdf[0]: x point; cdf[1]: y point
f.write(str(cdf[0])+' '+str(cdf[1])+'\n')
def saveData(prefix1, savePath):
prefix0 = 'plotData-'
# prefix1 = scheme name
if savePath[len(savePath)-1] != '/':
savePath = savePath + '/'
filename = ''
# CTR, M = 3 and 6
# E2E
if g_dicMeanPDR_BGTI:
if g_dicMeanPDR_BGTI.has_key('filename'):
filename = prefix0+prefix1+g_dicMeanPDR_BGTI['filename']
filename = savePath+filename
saveDataToFile(filename, g_dicMeanPDR_BGTI)
if g_dicMeanE2E_BGTI:
if g_dicMeanE2E_BGTI.has_key('filename'):
filename = prefix0+prefix1+g_dicMeanE2E_BGTI['filename']
filename = savePath+filename
saveDataToFile(filename, g_dicMeanE2E_BGTI)
if g_dicMeanMaxE2E_BGTI:
if g_dicMeanMaxE2E_BGTI.has_key('filename'):
filename = prefix0+prefix1+g_dicMeanMaxE2E_BGTI['filename']
filename = savePath+filename
saveDataToFile(filename, g_dicMeanMaxE2E_BGTI)
#cdf E2E
if g_cdfPDR_BGTI:
if g_dicPDRRaw_BGTI.has_key('filename'):
filename = prefix0+prefix1+g_dicPDRRaw_BGTI['filename']
filename = savePath+filename
saveCDFDataToFile(filename, g_cdfPDR_BGTI)
if g_cdfMeanE2E_BGTI:
if g_dicMeanE2ERaw_BGTI.has_key('filename'):
filename = prefix0+prefix1+g_dicMeanE2ERaw_BGTI['filename']
filename = savePath+filename
saveCDFDataToFile(filename, g_cdfMeanE2E_BGTI)
if g_cdfMaxE2E_BGTI:
if g_dicMaxE2ERaw_BGTI.has_key('filename'):
filename = prefix0+prefix1+g_dicMaxE2ERaw_BGTI['filename']
filename = savePath+filename
saveCDFDataToFile(filename, g_cdfMaxE2E_BGTI)
def collectData(directory):
mode = ''
global schemeName
# scan the directory to read every data point
for root, dirs, files in os.walk(directory):
# print(root, files)
for filename in files:
# e.g., EDCA-s1Persis-BGTI-0.010-s-9.csv
fnSplitList=filename.split('-')
with open(root+filename, 'r') as csvf:
print('.', end="")
# bBGTI=False
xAxisValue=0.0
seed=-1
# density=0.0
# preprocess of prefix and parameters by file name
# e.g., EDCA-s1Persis-BGTI-0.010-s-9.csv
# ['EDCA', 's1Persis', 'BGTI', '0.020', 's', '2.csv']
# schemeName = fnSplitList[1]
# mode = fnSplitList[2]
# xAxisValue = float(fnSplitList[3])
# seed = int(fnSplitList[5].split('.')[0])
# print(schemeName, mode, xAxisValue, seed)
delayDataRowInd = -1
delayDataRow = []
csvreader=csv.reader(csvf)
for indRow, row in enumerate(csvreader):
print(row)
for ind, ele in enumerate(row):
# prefix discovery
if ele == 'scheme':
schemeName = row[ind+1]
if ele == 'seed':
seed = int(row[ind+1])
if ele == 'BGTI' and float(row[ind+1]):
if xaxis == ele:
# bBGTI = True
mode = ele
xAxisValue = float(row[ind+1])
if ele == 'density' and float(row[ind+1]):
if xaxis == ele:
mode = ele
xAxisValue = float(row[ind+1]) * 0.01
# if ele == 'pktSendInterval':
# mode = ele
# xAxisValue = float(row[ind+1])
# Data collection
# elif ele == 'SuccessfulRatio': # PDR
if ele == 'PDR': # PDR
readData(xAxis=xAxisValue,
yAxisDataPoint=row[ind+1],
targetDic=g_dicPDRRaw_BGTI)
if ele == 'maxE2EDelay':
readData(xAxis=xAxisValue,
yAxisDataPoint=row[ind+1],
targetDic=g_dicMaxE2ERaw_BGTI)
if ele == 'meanE2EDelay':
readData(xAxis=xAxisValue,
yAxisDataPoint=row[ind+1],
targetDic=g_dicMeanE2ERaw_BGTI)
# if row[0] == "E2E Delay":
# delayDataRowInd = indRow+1
# if delayDataRowInd != -1:
# delayDataRow = row
if delayDataRowInd != -1:
for ind, ele in enumerate(delayDataRow):
readData(xAxis=xAxisValue,
yAxisDataPoint=ele,
targetDic=g_dicMeanE2ERaw_BGTI)
meanCompute(inputDic=g_dicPDRRaw_BGTI,
retDic=g_dicMeanPDR_BGTI)
meanCompute(inputDic=g_dicMaxE2ERaw_BGTI,
retDic=g_dicMeanMaxE2E_BGTI)
meanCompute(inputDic=g_dicMeanE2ERaw_BGTI,
retDic=g_dicMeanE2E_BGTI)
writeFilename(peType='MeanPDR', mode=mode,
targetDic=g_dicMeanPDR_BGTI)
writeFilename(peType='MeanMaxE2E', mode=mode,
targetDic=g_dicMeanMaxE2E_BGTI)
writeFilename(peType='MeanE2E', mode=mode,
targetDic=g_dicMeanE2E_BGTI)
writeFilename(peType='cdfPDR', mode=mode,
targetDic=g_dicPDRRaw_BGTI)
writeFilename(peType='cdfMaxE2E', mode=mode,
targetDic=g_dicMaxE2ERaw_BGTI)
writeFilename(peType='cdfMeanE2E', mode=mode,
targetDic=g_dicMeanE2ERaw_BGTI)
pprint.pprint(g_dicPDRRaw_BGTI)
pprint.pprint(g_dicMaxE2ERaw_BGTI)
pprint.pprint(g_dicMeanE2ERaw_BGTI)
pprint.pprint(g_dicMeanPDR_BGTI)
pprint.pprint(g_dicMeanMaxE2E_BGTI)
pprint.pprint(g_dicMeanE2E_BGTI)
pprint.pprint(g_cdfPDR_BGTI)
pprint.pprint(g_cdfMeanE2E_BGTI)
pprint.pprint(g_cdfMaxE2E_BGTI)
if __name__ == "__main__":
options = optionsSet()
directory = options.directory
outputDir = options.outputDir
global schemeName
schemeName = ''
global xaxis;
xaxis = options.xaxis
# raw
global g_dicPDRRaw_BGTI
g_dicPDRRaw_BGTI={}
global g_dicMeanE2ERaw_BGTI
g_dicMeanE2ERaw_BGTI={}
global g_dicMaxE2ERaw_BGTI
g_dicMaxE2ERaw_BGTI={}
# mean
global g_dicMeanPDR_BGTI
g_dicMeanPDR_BGTI={}
global g_dicMeanE2E_BGTI
g_dicMeanE2E_BGTI={}
global g_dicMeanMaxE2E_BGTI
g_dicMeanMaxE2E_BGTI={}
# CDF
global g_cdfPDRRaw_BGTI
g_cdfPDRRaw_BGTI = []
global g_cdfMeanE2ERaw_BGTI
g_cdfMeanE2ERaw_BGTI = []
global g_cdfMaxE2ERaw_BGTI
g_cdfMaxE2ERaw_BGTI = []
global g_cdfPDR_BGTI
g_cdfPDR_BGTI = []
global g_cdfMeanE2E_BGTI
g_cdfMeanE2E_BGTI = []
global g_cdfMaxE2E_BGTI
g_cdfMaxE2E_BGTI = []
if directory[len(directory)-1] != '/':
directory = directory + '/'
print("Scan:", directory)
collectData(directory)
processData()
if schemeName:
saveData(prefix1=schemeName, savePath=outputDir)
else:
exit("schemeName is empty!")
print("Process Finished!")
print("The data are saved at: " + outputDir)