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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2021-2023 German Aerospace Center (DLR) and others.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
# @file macrOutput.py
# @author Amirhosein Karbasi
# @date 2021-04-20
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import matplotlib.pyplot as plt
import pandas_read_xml as pdx
import pandas as pd
import numpy as np
from collections import Counter
if "SUMO_HOME" in os.environ:
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
import sumolib # noqa
def main(args):
ap = sumolib.options.ArgumentParser()
ap.add_argument("file", dest="file", category="input", type=ap.file, required=True, help="An XML input file")
options = ap.parse_args(args=args)
if args is None or len(args) < 2:
print("Error: An xml file must be given as input")
sys.exit(1)
df = pdx.read_xml(options.file, ['meandata'])
df = pdx.flatten(df)
df = df.pipe(pdx.flatten)
df = df.pipe(pdx.flatten)
df = df.pipe(pdx.flatten)
df = df.rename({'interval|@begin': 'begin', 'interval|@end': 'end',
'interval|edge|@sampledSeconds': 'sampledSeconds', 'interval|edge|@density': 'density',
'interval|edge|@laneDensity': 'laneDensity', 'interval|edge|@speed': 'speed'}, axis=1)
df['begin'] = df['begin'].astype(float)
df['end'] = df['end'].astype(float)
df["sampledSeconds"] = df["sampledSeconds"].astype(float)
df["density"] = df["density"].astype(float)
df["laneDensity"] = df["laneDensity"].astype(float)
df["speed"] = df["speed"].astype(float)
df = df.replace(np.NaN, 0)
df['begin'] = df['begin'].astype(int)
# calculation time interval
bft = df.begin.iloc[0]
eft = df.end.iloc[0]
time_interval = eft - bft
time_interval = int(time_interval)
# The end time of the last interval
_lastsimulationperiod_ = (df.end.iat[-1]).astype(int)
# creating a list of all end time intervals
_beginvalues_ = list(range(time_interval, _lastsimulationperiod_+time_interval, time_interval))
# detecting number of segments
counter1 = Counter(df.begin)
_seg = counter1[0]
# calculating total length of network
length = df['sampledSeconds'] / (df['end']-df['begin']) / df['density']
df['Length'] = length.replace(np.NaN, 0).replace(np.inf, 0)
i = 0
j = 0
__net = []
while _beginvalues_[i] < (_lastsimulationperiod_):
_net = sum(df.Length.iloc[j:j+_seg])
__net.append(_net)
i = i+1
j = j+_seg
# calculating meandensity,meanflow,meanspeed (density=density)
i = 0
j = 0
MD = []
MS = []
MF = []
while _beginvalues_[i] < (_lastsimulationperiod_):
numofveh = (1/time_interval)*(sum(df.sampledSeconds.iloc[j:j+_seg]))
speedznumofveh = (1/time_interval)*(sum(df.sampledSeconds.iloc[j:j+_seg]*df.speed.iloc[j:j+_seg]))
if numofveh > 0:
meanspeed_ = 3.6*speedznumofveh/numofveh
else:
meanspeed_ = 0
meandensity_ = (sum(df.density.iloc[j:j+_seg]*df.Length.iloc[j:j+_seg]))/_net
meanflow_ = (sum(df.density.iloc[j:j+_seg]*df.Length.iloc[j:j+_seg]*df.speed.iloc[j:j+_seg]*3.6))/_net
MD.append(meandensity_)
MS.append(meanspeed_)
MF.append(meanflow_)
i = i+1
j = j+_seg
# plot
plt.scatter(MD, MS)
plt.xlabel("Density (Veh/km)")
plt.ylabel("Speed (Km/hr)")
plt.show()
plt.scatter(MD, MF)
plt.xlabel("Density (Veh/km)")
plt.ylabel("Flow (Veh/hr)")
plt.show()
plt.scatter(MS, MF)
plt.xlabel("Speed (Km/hr)")
plt.ylabel("Flow (Veh/hr)")
plt.show()
# calculating meandensity,meanflow,meanspeed (density=laneDensity)
i = 0
j = 0
lMD = []
lMS = []
lMF = []
while _beginvalues_[i] <= (_lastsimulationperiod_ - time_interval):
numofveh = (1/time_interval)*(sum(df.sampledSeconds.iloc[j:j+_seg]))
speedznumofveh = (1/time_interval)*(sum(df.sampledSeconds.iloc[j:j+_seg]*df.speed.iloc[j:j+_seg]))
if numofveh > 0:
meanspeed_ = 3.6*speedznumofveh/numofveh
else:
meanspeed_ = 0
meandensity_ = (sum(df.laneDensity.iloc[j:j+_seg]*df.Length.iloc[j:j+_seg]))/_net
meanflow_ = (sum(df.laneDensity.iloc[j:j+_seg]*df.Length.iloc[j:j+_seg]*df.speed.iloc[j:j+_seg]*3.6))/_net
lMD.append(meandensity_)
lMS.append(meanspeed_)
lMF.append(meanflow_)
i = i+1
j = j+_seg
# plot
plt.scatter(lMD, lMS)
plt.xlabel("Density (Veh/km)")
plt.ylabel("Speed (Km/hr)")
plt.show()
plt.scatter(lMD, lMF)
plt.xlabel("Density (Veh/km)")
plt.ylabel("Flow (Veh/hr)")
plt.show()
plt.scatter(lMS, lMF)
plt.xlabel("Speed (Km/hr)")
plt.ylabel("Flow (Veh/hr)")
plt.show()
# Build a csv file
Macro_Features = {'Density': MD,
'Speed': MS,
'Flow': MF,
}
df = pd.DataFrame(Macro_Features, columns=['Density', 'Speed', 'Flow'])
df.to_csv('Macro_density.csv')
# Build a csv file
Macro_Features = {'Density': lMD,
'Speed': lMS,
'Flow': lMF,
}
df = pd.DataFrame(Macro_Features, columns=['Density', 'Speed', 'Flow'])
df.to_csv('Macro_lanedensity.csv')
if __name__ == "__main__":
sys.exit(main())
|