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