Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/san_pablo_dam/data/analyzeData.py
169708 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file analyzeData.py
15
# @author Daniel Krajzewicz
16
# @author Laura Bieker
17
# @date 2011-09-30
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
22
import sys
23
import os
24
import numpy as np
25
26
27
def getAttr(line, which):
28
beg = line.find(which)
29
beg = line.find('"', beg)
30
end = line.find('"', beg + 1)
31
return line[beg + 1:end]
32
33
# this is from here: http://code.activestate.com/recipes/389639
34
35
36
class Ddict(dict):
37
38
def __init__(self, default=None):
39
self.default = default
40
41
def __getitem__(self, key):
42
if key not in self:
43
self[key] = self.default()
44
return dict.__getitem__(self, key)
45
46
# os.system('run-an-external-command')
47
# os.getcwd()
48
# os.chdir()
49
50
51
f = open(sys.argv[1], 'r')
52
data = f.readlines()
53
f.close()
54
55
dd = Ddict(lambda: Ddict(lambda: 0))
56
# f1 = open('raw-results.txt','w')
57
f1 = open('tmp.txt', 'w')
58
59
for i in range(1, len(data)):
60
if data[i].find('<interval') != -1:
61
ll = data[i].split('"')
62
nn = int(getAttr(data[i], "nVehContrib")) # int(ll[7])
63
lane = int(getAttr(data[i], "id")[-1:]) # int(ll[5])
64
tt = float(getAttr(data[i], "begin")) # float(ll[1])
65
itt = int(tt)
66
if nn > 0:
67
print(tt, lane, nn, ll[9], ll[11], ll[13], ll[15], file=f1)
68
dd[itt][lane] = nn
69
70
f1.close()
71
maxLanes = 0
72
dt2OneHour = 6.0
73
74
for t in dd.keys():
75
if len(dd[t]) > maxLanes:
76
maxLanes = len(dd[t])
77
78
tVec = np.zeros(len(dd), dtype=int)
79
QVec = np.zeros(len(dd), dtype=int)
80
xVec = np.zeros((len(dd), maxLanes), dtype=float)
81
qVec = np.zeros((len(dd), maxLanes), dtype=float)
82
vecIndx = 0
83
84
f = open('lane-shares.txt', 'w')
85
# for t,v in dd.items():
86
for t in sorted(dd.keys()):
87
# qTot = math.fsum(dd[t])
88
qTot = sum(dd[t].values())
89
nrm = 0.0
90
if qTot:
91
nrm = 1.0 / qTot
92
s = repr(t) + ' ' + repr(qTot) + ' '
93
tVec[vecIndx] = t
94
QVec[vecIndx] = dt2OneHour * qTot
95
for lane in range(maxLanes):
96
share = 0.0
97
if lane in dd[t]:
98
share = nrm * dd[t][lane]
99
s = s + repr(share) + ' '
100
xVec[vecIndx, lane] = share
101
qVec[vecIndx, lane] = dt2OneHour * dd[t][lane]
102
# print >> f,t,qTot,lane,share
103
vecIndx += 1
104
print(s, file=f)
105
f.close()
106
107
try:
108
import matplotlib.pyplot as plt
109
plt.rcParams['xtick.direction'] = 'out'
110
plt.rcParams['ytick.direction'] = 'out'
111
# y =
112
n = len(qVec)
113
for lane in range(maxLanes):
114
desc = 'lane: ' + repr(lane)
115
plt.plot(tVec, qVec[range(n), lane], label=desc)
116
# plt.plot(tVec, qVec[range(n),0], 'r-',tVec, qVec[range(n),1], 'g-',tVec, qVec[range(n),2], 'b-')
117
# plt.plot(tVec, QVec, 'r-')
118
plt.ylabel('lane flows')
119
plt.xlabel('time [s]')
120
plt.legend()
121
bname = 'flows-over-time-' + repr(maxLanes)
122
plt.savefig(bname + '.eps')
123
plt.savefig(bname + '.pdf')
124
plt.savefig(bname + '.png')
125
plt.savefig(bname + '.svg')
126
# try:
127
# import pyemf
128
# plt.savefig('shares-over-time.emf')
129
# except :
130
# print '# no emf support'
131
# plt.show()
132
plt.close()
133
# next plot:
134
for lane in range(maxLanes):
135
desc = 'lane: ' + repr(lane)
136
plt.plot(QVec, xVec[range(n), lane], 'o', markersize=10, label=desc)
137
# plt.plot(tVec, qVec[range(n),0], 'r-',tVec, qVec[range(n),1], 'g-',tVec, qVec[range(n),2], 'b-')
138
# plt.plot(tVec, QVec, 'r-')
139
plt.ylabel('lane shares')
140
plt.xlabel('total flow [veh/h]')
141
plt.legend()
142
bname = 'shares-vs-flow-' + repr(maxLanes)
143
plt.savefig(bname + '.eps')
144
plt.savefig(bname + '.pdf')
145
plt.savefig(bname + '.png')
146
plt.savefig(bname + '.svg')
147
# plt.show()
148
plt.close()
149
except ImportError:
150
print('no matplotlib, falling back to gnuplot')
151
os.system('gnuplot do-some-plots.gnu')
152
153