-
Notifications
You must be signed in to change notification settings - Fork 25
/
sagaActivityReport.py
executable file
·149 lines (124 loc) · 4.87 KB
/
sagaActivityReport.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
#!/usr/bin/env python3
""" From the Tripinfo file, generate the activities report.
Author: Lara CODECA
This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0.
"""
import argparse
import collections
import json
import os
from pprint import pformat
import sys
from lxml import etree
import numpy as np
def get_options(cmd_args=None):
"""Argument Parser."""
parser = argparse.ArgumentParser(
prog="{}".format(sys.argv[0]),
usage="%(prog)s [options]",
description="SAGA Live Monitoring",
)
parser.add_argument(
"--tripinfo", type=str, required=True, help="SUMO TripInfo file (XML)."
)
parser.add_argument("--out", type=str, required=True, help="Output file (CSV).")
return parser.parse_args(cmd_args)
class SAGAReport(object):
"""SAGA Activities Report"""
TRIPINFO_SCHEMA = os.path.join(
os.environ["SUMO_HOME"], "data/xsd/tripinfo_file.xsd"
)
def __init__(self, cfg):
self.tripinfo_file = cfg.tripinfo
self.output_file = cfg.out
self.tripinfo = collections.defaultdict(dict)
self.personinfo = collections.defaultdict(dict)
self.activity_stats = collections.defaultdict(list)
def load_tripinfo(self):
"""Load the Tripinfo file"""
# just in case..
self.tripinfo = collections.defaultdict(dict)
self.personinfo = collections.defaultdict(dict)
tree = None
try:
# Faster, but it may fail.
schema = etree.XMLSchema(file=self.TRIPINFO_SCHEMA)
parser = etree.XMLParser(schema=schema)
tree = etree.parse(self.tripinfo_file, parser)
except etree.XMLSyntaxError as excp:
print(
"Unable to use {} schema due to exception {}.".format(
self.TRIPINFO_SCHEMA, pformat(excp)
)
)
tree = etree.parse(self.tripinfo_file)
print("Loading {} tripinfo file.".format(self.tripinfo_file))
for element in tree.getroot():
if element.tag == "tripinfo":
self.tripinfo[element.attrib["id"]] = dict(element.attrib)
elif element.tag == "personinfo":
self.personinfo[element.attrib["id"]] = dict(element.attrib)
stages = []
for stage in element:
stages.append([stage.tag, dict(stage.attrib)])
self.personinfo[element.attrib["id"]]["stages"] = stages
else:
raise Exception("Unrecognized element in the tripinfo file.")
# print('TRIPINFO: \n{}'.format(self.tripinfo))
# print('PERSONINFO: \n{}'.format(self.personinfo))
def process_tripinfo(self):
"""Process the Tripinfo file"""
print("Processing {} tripinfo file.".format(self.tripinfo_file))
for person, data in self.personinfo.items():
for tag, stage in data["stages"]:
# print('[{}] {} \n{}'.format(person, tag, pformat(stage)))
if tag == "stop":
self.activity_stats[stage["actType"]].append(
{
"arrival": stage["arrival"],
"duration": stage["duration"],
}
)
def compute_stats(self):
"""Computing Stats from the Tripinfo file"""
print("Computing statistics..")
stats = dict()
for activity, data in self.activity_stats.items():
duration = list()
start = list()
for value in data:
start.append(float(value["arrival"]) - float(value["duration"]))
duration.append(float(value["duration"]))
stats[activity] = {
"duration": {
"min": min(duration),
"max": max(duration),
"mean": np.mean(duration),
"median": np.median(duration),
"std": np.std(duration),
},
"start": {
"min": min(start),
"max": max(start),
"mean": np.mean(start),
"median": np.median(start),
"std": np.std(start),
},
}
print("[{}] \n{}".format(activity, pformat(stats[activity])))
print("Saving statistics to {} file.".format(self.output_file))
with open(self.output_file, "w") as output:
json.dump(stats, output)
def main(cmd_args):
"""SAGA Activities Report"""
args = get_options(cmd_args)
print(args)
report = SAGAReport(args)
report.load_tripinfo()
report.process_tripinfo()
report.compute_stats()
print("Done.")
if __name__ == "__main__":
main(sys.argv[1:])