Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/contributed/sumopy/plugins/hcprt/results_mpl.py
169689 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.
3
# SUMOPy module
4
# Copyright (C) 2012-2021 University of Bologna - DICAM
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file results_mpl.py
16
# @author Joerg Schweizer
17
# @date 2012
18
19
import os
20
import numpy as np
21
from collections import OrderedDict
22
import matplotlib as mpl
23
from matplotlib.patches import Arrow, Circle, Wedge, Polygon, FancyArrow
24
from matplotlib.collections import PatchCollection
25
import matplotlib.colors as colors
26
import matplotlib.cm as cmx
27
import matplotlib.pyplot as plt
28
import matplotlib.image as image
29
30
import agilepy.lib_base.classman as cm
31
import agilepy.lib_base.arrayman as am
32
from agilepy.lib_base.geometry import *
33
from agilepy.lib_base.processes import Process
34
from coremodules.misc.matplottools import *
35
36
37
class StopresultsPlotter(Process):
38
def __init__(self, results, name='Plot PRT stop results with Matplotlib',
39
info="Creates plots of PRT stop results using matplotlib",
40
logger=None, **kwargs):
41
42
self._init_common('stopresultsplotter', parent=results, name=name,
43
info=info, logger=logger)
44
45
print 'StopresultsPlotter.__init__', results, self.parent, len(self.get_stopresults())
46
attrsman = self.get_attrsman()
47
48
stops = self.get_stopresults().get_prtstops()
49
print ' prtstops', stops, len(stops), id(stops), self.get_stopresults().ids_stop.get_linktab(), id(self.get_stopresults().ids_stop.get_linktab())
50
choices_stop = {}
51
for id_stop in stops.get_ids():
52
choices_stop[str(id_stop)] = id_stop
53
54
self.id_stop_plot = attrsman.add(cm.AttrConf('id_stop_plot', kwargs.get('id_stop_plot', stops.get_ids()[0]),
55
groupnames=['options'],
56
choices=choices_stop,
57
name='ID stop plot',
58
info='Plot results of PRT stop with this ID.',
59
))
60
61
self.is_title = attrsman.add(cm.AttrConf('is_title', kwargs.get('is_title', False),
62
groupnames=['options'],
63
name='Show title',
64
info='Show title of diagrams.',
65
))
66
67
self.size_titlefont = attrsman.add(cm.AttrConf('size_titlefont', kwargs.get('size_titlefont', 32),
68
groupnames=['options'],
69
name='Title fontsize',
70
info='Title fontsize.',
71
))
72
73
self.size_labelfont = attrsman.add(cm.AttrConf('size_labelfont', kwargs.get('size_labelfont', 24),
74
groupnames=['options'],
75
name='Label fontsize',
76
info='Label fontsize.',
77
))
78
79
self.width_line = attrsman.add(cm.AttrConf('width_line', kwargs.get('width_line', 2),
80
groupnames=['options'],
81
perm='wr',
82
name='Line width',
83
info='Width of plotted lines.',
84
))
85
86
self.color_line = attrsman.add(cm.AttrConf('color_line', kwargs.get('color_line', np.array([0, 0, 1, 1], dtype=np.float32)),
87
groupnames=['options'],
88
perm='wr',
89
metatype='color',
90
name='Line color',
91
info='Color of line in various diagrams.',
92
))
93
94
self.printformat = attrsman.add(cm.AttrConf('printformat', kwargs.get('printformat', '%.1f'),
95
choices=OrderedDict([
96
('Show no values', ''),
97
('x', '%.d'),
98
('x.x', '%.1f'),
99
('x.xx', '%.2f'),
100
('x.xxx', '%.3f'),
101
('x.xxxx', '%.4f'),
102
]),
103
groupnames=['options'],
104
name='Label formatting',
105
info='Print formatting of value label in graphical representation.',
106
))
107
108
self.color_label = attrsman.add(cm.AttrConf('color_label', kwargs.get('color_label', np.array([0, 0, 0, 1], dtype=np.float32)),
109
groupnames=['options'],
110
perm='wr',
111
metatype='color',
112
name='Label color',
113
info='Color of value label in graphical representation.',
114
))
115
116
self.is_grid = attrsman.add(cm.AttrConf('is_grid', kwargs.get('is_grid', True),
117
groupnames=['options'],
118
name='Show grid?',
119
info='If True, shows a grid on the graphical representation.',
120
))
121
self.color_background = attrsman.add(cm.AttrConf('color_background', kwargs.get('color_background', np.array([1, 1, 1, 1], dtype=np.float32)),
122
groupnames=['options'],
123
perm='wr',
124
metatype='color',
125
name='Background color',
126
info='Background color of schematic network in the background.',
127
))
128
129
def get_stopresults(self):
130
return self.parent.hcprtstopresults
131
132
def show(self):
133
stopresults = self.get_stopresults()
134
print 'show', stopresults
135
# print ' dir(vehicleman)',dir(vehicleman)
136
137
print ' len(stopresults)', len(stopresults)
138
if len(stopresults) > 0:
139
i_fig = 0
140
plt.close("all")
141
142
#i_fig +=1;fig = plt.figure(i_fig)
143
# self.plot_waiting_person(fig)
144
145
i_fig += 1
146
fig = plt.figure(i_fig)
147
self.plot_waiting_person_number(fig)
148
149
#i_fig +=1;fig = plt.figure(i_fig)
150
# self.plot_waiting_person_time(fig)
151
152
i_fig += 1
153
fig = plt.figure(i_fig)
154
self.plot_waiting_person_number_stop(fig)
155
156
i_fig += 1
157
fig = plt.figure(i_fig)
158
self.plot_flow_stop(fig)
159
160
#i_fig +=1;fig = plt.figure(i_fig)
161
# self.plot_flows_compare(fig)
162
163
#i_fig +=1;fig = plt.figure(i_fig)
164
# self.plot_flows_compare_stop(fig)
165
166
plt.show()
167
168
def plot_flow_stop(self, fig):
169
print 'plot_flow_stop'
170
id_stop = self.id_stop_plot
171
stopresults = self.get_stopresults()
172
ax = fig.add_subplot(111)
173
174
n_stop, n_steps = stopresults.get_dimensions()
175
t_step = stopresults.time_step.get_value()
176
177
time = np.arange(n_steps, dtype=np.float32)*t_step
178
i = 0
179
180
flow_veh_av = np.mean(stopresults.inflows_veh[id_stop])*3600
181
flow_pers_av = np.mean(stopresults.inflows_person[id_stop])*3600
182
183
ax.plot(time, stopresults.inflows_veh[id_stop]*3600,
184
label='Effective vehicle',
185
color='b',
186
linestyle='--', linewidth=self.width_line,
187
marker='s', markersize=4*self.width_line,
188
)
189
190
ax.plot(time, stopresults.inflows_veh_sched[id_stop]*3600,
191
label='Scheduled vehicle',
192
color='c',
193
linestyle=':', linewidth=self.width_line,
194
marker='^', markersize=4*self.width_line,
195
)
196
197
ax.plot(time, stopresults.inflows_person[id_stop]*3600,
198
label='Effective pers.',
199
color='g',
200
linestyle='-', linewidth=self.width_line,
201
marker='o', markersize=4*self.width_line,
202
)
203
204
ax.plot([time[0], time[-1]], [flow_veh_av, flow_veh_av],
205
label='Average vehicle',
206
color='fuchsia',
207
linestyle='-', linewidth=2*self.width_line,
208
)
209
210
ax.plot([time[0], time[-1]], [flow_pers_av, flow_pers_av],
211
label='Average person',
212
color='darkorange',
213
linestyle='-', linewidth=2*self.width_line,
214
)
215
216
ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
217
ax.grid(self.is_grid)
218
if self.is_title:
219
ax.set_title('Vehicle and person in-flows over time at PRT stop ID=%d' %
220
id_stop, fontsize=self.size_titlefont)
221
ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)
222
ax.set_ylabel('In-flows [1/h]', fontsize=self.size_labelfont)
223
ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
224
ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
225
226
def plot_waiting_person_time(self, fig):
227
print 'plot_waiting_person_time'
228
stopresults = self.get_stopresults()
229
ax = fig.add_subplot(111)
230
231
n_stop, n_steps = stopresults.get_dimensions()
232
t_step = stopresults.time_step.get_value()
233
#inds_stop = np.arange(n_stop, dtype = np.int32)
234
time = np.arange(n_steps, dtype=np.float32)*t_step
235
# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))
236
i = 0
237
for id_stop in stopresults.ids_stop.get_value():
238
ax.plot(time, 1.0/60.0*stopresults.waittimes_tot[id_stop],
239
COLORS[i], linewidth=self.width_line,
240
label='PRT Stop ID=%d' % id_stop)
241
i += 1
242
243
if self.is_title:
244
ax.set_title('Number of waiting persons over time', fontsize=self.size_titlefont)
245
246
ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
247
ax.grid(self.is_grid)
248
ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)
249
ax.set_ylabel('Waiting times of passengers [min]', fontsize=self.size_labelfont)
250
ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
251
ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
252
253
def plot_waiting_person_number_stop(self, fig):
254
print 'plot_waiting_person_number_stop'
255
stopresults = self.get_stopresults()
256
#ax1 = fig.add_subplot(211)
257
#ax2 = fig.add_subplot(212)
258
ax = fig.add_subplot(111)
259
260
n_stop, n_steps = stopresults.get_dimensions()
261
t_step = stopresults.time_step.get_value()
262
#inds_stop = np.arange(n_stop, dtype = np.int32)
263
time = np.arange(n_steps, dtype=np.float32)*t_step
264
# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))
265
id_stop = self.id_stop_plot
266
ax.plot(time, stopresults.numbers_person_wait[id_stop],
267
'g', linewidth=self.width_line,
268
label='PRT Stop ID=%d' % id_stop)
269
270
ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
271
ax.grid(self.is_grid)
272
ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)
273
ax.set_ylabel('Number of waiting passengers', fontsize=self.size_labelfont)
274
ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
275
ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
276
277
def plot_waiting_person_number(self, fig):
278
print 'plot_waiting_person_number'
279
stopresults = self.get_stopresults()
280
#ax1 = fig.add_subplot(211)
281
#ax2 = fig.add_subplot(212)
282
ax = fig.add_subplot(111)
283
284
n_stop, n_steps = stopresults.get_dimensions()
285
t_step = stopresults.time_step.get_value()
286
#inds_stop = np.arange(n_stop, dtype = np.int32)
287
time = np.arange(n_steps, dtype=np.float32)*t_step
288
# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))
289
i = 0
290
for id_stop in stopresults.ids_stop.get_value():
291
print ' id_stop', id_stop
292
ax.plot(time, stopresults.numbers_person_wait[id_stop],
293
get_color(i), linewidth=self.width_line,
294
label='PRT Stop ID=%d' % id_stop)
295
i += 1
296
297
ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
298
ax.grid(self.is_grid)
299
ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)
300
ax.set_ylabel('Number of waiting passengers', fontsize=self.size_labelfont)
301
ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
302
ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
303
304
def plot_flows_compare(self, fig):
305
print 'plot_flows_compare'
306
stopresults = self.get_stopresults()
307
#time_update_flows = self.parent.vehicleman.time_update_flows.get_value()
308
time_update_flows = 10
309
ax = fig.add_subplot(111)
310
311
n_stop, n_steps = stopresults.get_dimensions()
312
t_step = stopresults.time_step.get_value()
313
314
time = np.arange(n_steps, dtype=np.float32)*t_step
315
i = 0
316
flowmatrix = np.zeros((10, 10), dtype=np.int32)
317
for id_stop in stopresults.ids_stop.get_value():
318
print ' id_stop', id_stop
319
# print ' sched',stopresults.inflows_veh_sched[id_stop]
320
# print ' eff ',stopresults.inflows_veh[id_stop]
321
flowmatrix[np.array(time_update_flows*stopresults.inflows_veh_sched[id_stop], dtype=np.int32),
322
np.array(time_update_flows*stopresults.inflows_veh[id_stop], dtype=np.int32)] += 1
323
# ax.plot(stopresults.inflows_veh_sched[id_stop]*3600,stopresults.inflows_veh[id_stop]*3600,
324
# get_color(i),linewidth =self.width_line,
325
# label = 'PRT Stop ID=%d (effective)'%id_stop)
326
327
i += 1
328
print 'flowmatrix', flowmatrix
329
# ax.matshow(flowmatrix)
330
331
cax = ax.matshow(flowmatrix, cmap=cmx.get_cmap('PuBu'))
332
cbar = fig.colorbar(cax)
333
#ax.legend(loc='best',shadow=True, fontsize=self.size_labelfont)
334
ax.grid(self.is_grid)
335
ax.set_xlabel('Scheduled arrivals per interval', fontsize=self.size_labelfont)
336
ax.set_ylabel('Effective arrivals per interval', fontsize=self.size_labelfont)
337
ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
338
ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
339
340
def plot_flows_compare_stop(self, fig):
341
print 'plot_flows_compare_stop'
342
stopresults = self.get_stopresults()
343
id_stop = self.id_stop_plot
344
#time_update_flows = self.parent.vehicleman.time_update_flows.get_value()
345
time_update_flows = 10
346
ax = fig.add_subplot(111)
347
348
n_stop, n_steps = stopresults.get_dimensions()
349
t_step = stopresults.time_step.get_value()
350
351
i = 0
352
flows_sched = stopresults.inflows_veh_sched[id_stop]
353
flows_eff = stopresults.inflows_veh[id_stop]
354
355
x = flows_sched # -np.mean(flows_sched)
356
y = flows_eff # -np.mean(flows_eff)
357
flowcorr = np.correlate(x, y, 'full')/np.sqrt(np.sum(x*x)*np.sum(y*y))
358
359
time = np.arange(-n_steps+1, n_steps, dtype=np.float32)*t_step
360
361
print ' len(flowcorr),n_steps', len(flowcorr), len(time), n_steps
362
363
ax.plot(time, flowcorr,
364
get_color(i), linewidth=self.width_line,
365
label='PRT Stop ID=%d' % id_stop)
366
367
ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
368
ax.grid(self.is_grid)
369
ax.set_xlabel('Time delay [s]', fontsize=self.size_labelfont)
370
ax.set_ylabel('Cross-Correlation', fontsize=self.size_labelfont)
371
ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
372
ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
373
374
def plot_flows(self, fig):
375
print 'plot_flows'
376
stopresults = self.get_stopresults()
377
ax = fig.add_subplot(111)
378
379
n_stop, n_steps = stopresults.get_dimensions()
380
t_step = stopresults.time_step.get_value()
381
382
time = np.arange(n_steps, dtype=np.float32)*t_step
383
i = 0
384
for id_stop in stopresults.ids_stop.get_value():
385
ax.plot(time, stopresults.inflows_veh[id_stop]*3600,
386
get_color(i), linewidth=self.width_line,
387
label='PRT Stop ID=%d (effective)' % id_stop)
388
389
ax.plot(time, stopresults.inflows_veh_sched[id_stop]*3600,
390
get_color(i), linestyle='--', linewidth=self.width_line,
391
label='PRT Stop ID=%d (scheduled)' % id_stop)
392
393
ax.plot(time, stopresults.inflows_person[id_stop]*3600,
394
get_color(i), linestyle=':', linewidth=self.width_line,
395
label='PRT Stop ID=%d (person)' % id_stop)
396
397
i += 1
398
399
ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
400
ax.grid(self.is_grid)
401
ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)
402
ax.set_ylabel('In-flows [1/h]', fontsize=self.size_labelfont)
403
ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
404
ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
405
406
#('inflows_veh', {'name':'Vehicle in-flows', 'unit':'1/s', 'dtype':np.float32, 'info':'Vehicle flow into the stop over time.'}),
407
# ('inflows_veh_sched', {'name':'Sched. vehicle in-flows', 'unit':'1/s', 'dtype':np.float32, 'info':'Scheduled vehicle flow into the stop over time.'}),
408
# ('inflows_person', {'name':'Person in-flows', 'unit':'1/s', 'dtype':np.float32, 'info':'Person flow into the stop over time.'}),
409
410
def plot_waiting_person(self, fig):
411
print 'plot_waiting_person'
412
stopresults = self.get_stopresults()
413
ax1 = fig.add_subplot(211)
414
ax2 = fig.add_subplot(212)
415
#ax = fig.add_subplot(111)
416
417
n_stop, n_steps = stopresults.get_dimensions()
418
t_step = stopresults.time_step.get_value()
419
#inds_stop = np.arange(n_stop, dtype = np.int32)
420
time = np.arange(n_steps, dtype=np.float32)*t_step
421
# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))
422
i = 0
423
for id_stop in stopresults.ids_stop.get_value():
424
ax1.plot(time, stopresults.numbers_person_wait[id_stop],
425
get_color(i), linewidth=self.width_line,
426
label='PRT Stop ID=%d' % id_stop)
427
ax2.plot(time, 1.0/60.0*stopresults.waittimes_tot[id_stop],
428
get_color(i), linewidth=self.width_line,
429
label='PRT Stop ID=%d' % id_stop)
430
i += 1
431
432
ax1.legend(loc='best', shadow=True, fontsize=14)
433
ax1.grid(self.is_grid)
434
ax1.set_xlabel('Time [s]', fontsize=14)
435
ax1.set_ylabel('Number of waiting passengers', fontsize=14)
436
ax1.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
437
ax1.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
438
439
ax2.legend(loc='best', shadow=True, fontsize=14)
440
ax2.grid(self.is_grid)
441
ax2.set_xlabel('Time [s]', fontsize=14)
442
ax2.set_ylabel('Waiting times of passengers [min]', fontsize=14)
443
ax2.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
444
ax2.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
445
446
def do(self):
447
# print 'do',self.edgeattrname
448
self.show()
449
return True
450
451
def get_scenario(self):
452
return self._scenario
453
454