File: validate.py

package info (click to toggle)
sumo 0.15.0~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 31,740 kB
  • sloc: cpp: 109,360; xml: 49,743; ansic: 41,570; python: 20,769; java: 17,071; sh: 10,413; makefile: 1,377; perl: 450
file content (105 lines) | stat: -rw-r--r-- 3,442 bytes parent folder | download
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
import sys, math, subprocess

dDay = 1
obsTimes = {}
verbose = False
    
def readTimes(obsfile):
    times = []
    for line in open(obsfile):
        ll = line.split(':')
        if ll:
            times.append(3600*int(ll[0]) + 60*int(ll[1]) + int(float(ll[2])))
    return times

def parseObsTimes():
    for i in range (0,9): 
       obsTimes[i] = []
    for i in range(1,8):
        if dDay==1 and i==5:
            continue
        if dDay==2 and i==6:
            continue
        obsTimes[i] = readTimes('data/obstimes_%s_%s.txt' % (dDay, i))

    # convert obsTimes[][] into travel-times:
    for i in range(1,8):
        ni = len(obsTimes[i])
        if ni == len(obsTimes[i+1]) and ni > 100: 
            for j in range(ni):
                obsTimes[i][j] = obsTimes[i+1][j] - obsTimes[i][j]

def validate(sumoBinary):
    retcode = subprocess.call([sumoBinary, "-c", "data/spd-road.sumocfg"], stdout=sys.stdout, stderr=sys.stderr)
    sys.stdout.flush()
    sys.stderr.flush()

    # analyzing the results...
    # read the empirical times
    simTimes = {}
    for i in range (0,9): 
       simTimes[i] = []

    # read the simulated times
    obs2Nr = {'obs1': 1, 'obs2': 2, 'obs3': 3, 'obs4': 4, 'obs5': 5, 'obs6': 6, 'obs7': 7}  

    for line in open('data/detector.xml'):
        if line.find('<interval')!=-1:
            ll = line.split('"')
            iObs = obs2Nr[ll[5]]
            if int(ll[7]) > 0:
                simTimes[iObs].append(float(ll[1]))

    # convert simTimes[][] into travel-times:
    for i in range(1,8):
        ni = len(simTimes[i])
        if ni == len(simTimes[i+1]) and ni>100:
            for j in range(ni):
                simTimes[i][j] = simTimes[i+1][j] - simTimes[i][j]

    # compute final statistics
    err = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    averTT = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    errAll = 0.0
    cntAll = 0
    if verbose:
        f = open('data/sumo-obs-error.txt','w')
    for i in range(1,7):
        if len(obsTimes[i])<=100 or len(obsTimes[i+1])<=100:
            continue
        if len(obsTimes[i]) == len(simTimes[i]):
            tmp = 0.0
            for o, s in zip(obsTimes[i], simTimes[i]):
                d = o - s
                tmp += d*d
            err[i] = math.sqrt(tmp/len(obsTimes[i]))
            if verbose:
                print >> f, "%s %s" %(i, err[i])
            errAll += err[i]
            cntAll += 1
    if verbose:
        f.close()

    # finally, write the individual travel times into a csv-file
    # this is not really needed when validate is called from calibrate as an intermediate
    # step, but it makes analyzing of the result more simple.
    # first the header
    if verbose:
        c = open('data/compare-tt.csv', 'w')
        c.write('# indx;')
        for i in range(1,7):
            if len(obsTimes[i])>100 and len(obsTimes[i+1])>100:
                c.write('obs%s;sim%s;' % (i,i))
        c.write('\n')

        # then the data, of course on the ones which are useable
        for line in range(len(simTimes[1])):
            c.write(repr(line)+';')
            for i in range(1,7):
                if len(obsTimes[i])>100 and len(obsTimes[i+1])>100:
                    ttObs = int(obsTimes[i][line])
                    ttSim = int(simTimes[i][line])
                    c.write(repr(ttObs)+';'+repr(ttSim)+';')
            c.write('\n')
        c.close()
    return errAll/cntAll