Path: blob/main/tests/complex/tutorial/san_pablo_dam/data/analyzeData.py
169708 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file analyzeData.py14# @author Daniel Krajzewicz15# @author Laura Bieker16# @date 2011-09-301718from __future__ import absolute_import19from __future__ import print_function2021import sys22import os23import numpy as np242526def getAttr(line, which):27beg = line.find(which)28beg = line.find('"', beg)29end = line.find('"', beg + 1)30return line[beg + 1:end]3132# this is from here: http://code.activestate.com/recipes/389639333435class Ddict(dict):3637def __init__(self, default=None):38self.default = default3940def __getitem__(self, key):41if key not in self:42self[key] = self.default()43return dict.__getitem__(self, key)4445# os.system('run-an-external-command')46# os.getcwd()47# os.chdir()484950f = open(sys.argv[1], 'r')51data = f.readlines()52f.close()5354dd = Ddict(lambda: Ddict(lambda: 0))55# f1 = open('raw-results.txt','w')56f1 = open('tmp.txt', 'w')5758for i in range(1, len(data)):59if data[i].find('<interval') != -1:60ll = data[i].split('"')61nn = int(getAttr(data[i], "nVehContrib")) # int(ll[7])62lane = int(getAttr(data[i], "id")[-1:]) # int(ll[5])63tt = float(getAttr(data[i], "begin")) # float(ll[1])64itt = int(tt)65if nn > 0:66print(tt, lane, nn, ll[9], ll[11], ll[13], ll[15], file=f1)67dd[itt][lane] = nn6869f1.close()70maxLanes = 071dt2OneHour = 6.07273for t in dd.keys():74if len(dd[t]) > maxLanes:75maxLanes = len(dd[t])7677tVec = np.zeros(len(dd), dtype=int)78QVec = np.zeros(len(dd), dtype=int)79xVec = np.zeros((len(dd), maxLanes), dtype=float)80qVec = np.zeros((len(dd), maxLanes), dtype=float)81vecIndx = 08283f = open('lane-shares.txt', 'w')84# for t,v in dd.items():85for t in sorted(dd.keys()):86# qTot = math.fsum(dd[t])87qTot = sum(dd[t].values())88nrm = 0.089if qTot:90nrm = 1.0 / qTot91s = repr(t) + ' ' + repr(qTot) + ' '92tVec[vecIndx] = t93QVec[vecIndx] = dt2OneHour * qTot94for lane in range(maxLanes):95share = 0.096if lane in dd[t]:97share = nrm * dd[t][lane]98s = s + repr(share) + ' '99xVec[vecIndx, lane] = share100qVec[vecIndx, lane] = dt2OneHour * dd[t][lane]101# print >> f,t,qTot,lane,share102vecIndx += 1103print(s, file=f)104f.close()105106try:107import matplotlib.pyplot as plt108plt.rcParams['xtick.direction'] = 'out'109plt.rcParams['ytick.direction'] = 'out'110# y =111n = len(qVec)112for lane in range(maxLanes):113desc = 'lane: ' + repr(lane)114plt.plot(tVec, qVec[range(n), lane], label=desc)115# plt.plot(tVec, qVec[range(n),0], 'r-',tVec, qVec[range(n),1], 'g-',tVec, qVec[range(n),2], 'b-')116# plt.plot(tVec, QVec, 'r-')117plt.ylabel('lane flows')118plt.xlabel('time [s]')119plt.legend()120bname = 'flows-over-time-' + repr(maxLanes)121plt.savefig(bname + '.eps')122plt.savefig(bname + '.pdf')123plt.savefig(bname + '.png')124plt.savefig(bname + '.svg')125# try:126# import pyemf127# plt.savefig('shares-over-time.emf')128# except :129# print '# no emf support'130# plt.show()131plt.close()132# next plot:133for lane in range(maxLanes):134desc = 'lane: ' + repr(lane)135plt.plot(QVec, xVec[range(n), lane], 'o', markersize=10, label=desc)136# plt.plot(tVec, qVec[range(n),0], 'r-',tVec, qVec[range(n),1], 'g-',tVec, qVec[range(n),2], 'b-')137# plt.plot(tVec, QVec, 'r-')138plt.ylabel('lane shares')139plt.xlabel('total flow [veh/h]')140plt.legend()141bname = 'shares-vs-flow-' + repr(maxLanes)142plt.savefig(bname + '.eps')143plt.savefig(bname + '.pdf')144plt.savefig(bname + '.png')145plt.savefig(bname + '.svg')146# plt.show()147plt.close()148except ImportError:149print('no matplotlib, falling back to gnuplot')150os.system('gnuplot do-some-plots.gnu')151152153