Path: blob/main/tools/contributed/sumopy/plugins/hcprt/results_mpl.py
169689 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.2# SUMOPy module3# Copyright (C) 2012-2021 University of Bologna - DICAM4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file results_mpl.py15# @author Joerg Schweizer16# @date 20121718import os19import numpy as np20from collections import OrderedDict21import matplotlib as mpl22from matplotlib.patches import Arrow, Circle, Wedge, Polygon, FancyArrow23from matplotlib.collections import PatchCollection24import matplotlib.colors as colors25import matplotlib.cm as cmx26import matplotlib.pyplot as plt27import matplotlib.image as image2829import agilepy.lib_base.classman as cm30import agilepy.lib_base.arrayman as am31from agilepy.lib_base.geometry import *32from agilepy.lib_base.processes import Process33from coremodules.misc.matplottools import *343536class StopresultsPlotter(Process):37def __init__(self, results, name='Plot PRT stop results with Matplotlib',38info="Creates plots of PRT stop results using matplotlib",39logger=None, **kwargs):4041self._init_common('stopresultsplotter', parent=results, name=name,42info=info, logger=logger)4344print 'StopresultsPlotter.__init__', results, self.parent, len(self.get_stopresults())45attrsman = self.get_attrsman()4647stops = self.get_stopresults().get_prtstops()48print ' prtstops', stops, len(stops), id(stops), self.get_stopresults().ids_stop.get_linktab(), id(self.get_stopresults().ids_stop.get_linktab())49choices_stop = {}50for id_stop in stops.get_ids():51choices_stop[str(id_stop)] = id_stop5253self.id_stop_plot = attrsman.add(cm.AttrConf('id_stop_plot', kwargs.get('id_stop_plot', stops.get_ids()[0]),54groupnames=['options'],55choices=choices_stop,56name='ID stop plot',57info='Plot results of PRT stop with this ID.',58))5960self.is_title = attrsman.add(cm.AttrConf('is_title', kwargs.get('is_title', False),61groupnames=['options'],62name='Show title',63info='Show title of diagrams.',64))6566self.size_titlefont = attrsman.add(cm.AttrConf('size_titlefont', kwargs.get('size_titlefont', 32),67groupnames=['options'],68name='Title fontsize',69info='Title fontsize.',70))7172self.size_labelfont = attrsman.add(cm.AttrConf('size_labelfont', kwargs.get('size_labelfont', 24),73groupnames=['options'],74name='Label fontsize',75info='Label fontsize.',76))7778self.width_line = attrsman.add(cm.AttrConf('width_line', kwargs.get('width_line', 2),79groupnames=['options'],80perm='wr',81name='Line width',82info='Width of plotted lines.',83))8485self.color_line = attrsman.add(cm.AttrConf('color_line', kwargs.get('color_line', np.array([0, 0, 1, 1], dtype=np.float32)),86groupnames=['options'],87perm='wr',88metatype='color',89name='Line color',90info='Color of line in various diagrams.',91))9293self.printformat = attrsman.add(cm.AttrConf('printformat', kwargs.get('printformat', '%.1f'),94choices=OrderedDict([95('Show no values', ''),96('x', '%.d'),97('x.x', '%.1f'),98('x.xx', '%.2f'),99('x.xxx', '%.3f'),100('x.xxxx', '%.4f'),101]),102groupnames=['options'],103name='Label formatting',104info='Print formatting of value label in graphical representation.',105))106107self.color_label = attrsman.add(cm.AttrConf('color_label', kwargs.get('color_label', np.array([0, 0, 0, 1], dtype=np.float32)),108groupnames=['options'],109perm='wr',110metatype='color',111name='Label color',112info='Color of value label in graphical representation.',113))114115self.is_grid = attrsman.add(cm.AttrConf('is_grid', kwargs.get('is_grid', True),116groupnames=['options'],117name='Show grid?',118info='If True, shows a grid on the graphical representation.',119))120self.color_background = attrsman.add(cm.AttrConf('color_background', kwargs.get('color_background', np.array([1, 1, 1, 1], dtype=np.float32)),121groupnames=['options'],122perm='wr',123metatype='color',124name='Background color',125info='Background color of schematic network in the background.',126))127128def get_stopresults(self):129return self.parent.hcprtstopresults130131def show(self):132stopresults = self.get_stopresults()133print 'show', stopresults134# print ' dir(vehicleman)',dir(vehicleman)135136print ' len(stopresults)', len(stopresults)137if len(stopresults) > 0:138i_fig = 0139plt.close("all")140141#i_fig +=1;fig = plt.figure(i_fig)142# self.plot_waiting_person(fig)143144i_fig += 1145fig = plt.figure(i_fig)146self.plot_waiting_person_number(fig)147148#i_fig +=1;fig = plt.figure(i_fig)149# self.plot_waiting_person_time(fig)150151i_fig += 1152fig = plt.figure(i_fig)153self.plot_waiting_person_number_stop(fig)154155i_fig += 1156fig = plt.figure(i_fig)157self.plot_flow_stop(fig)158159#i_fig +=1;fig = plt.figure(i_fig)160# self.plot_flows_compare(fig)161162#i_fig +=1;fig = plt.figure(i_fig)163# self.plot_flows_compare_stop(fig)164165plt.show()166167def plot_flow_stop(self, fig):168print 'plot_flow_stop'169id_stop = self.id_stop_plot170stopresults = self.get_stopresults()171ax = fig.add_subplot(111)172173n_stop, n_steps = stopresults.get_dimensions()174t_step = stopresults.time_step.get_value()175176time = np.arange(n_steps, dtype=np.float32)*t_step177i = 0178179flow_veh_av = np.mean(stopresults.inflows_veh[id_stop])*3600180flow_pers_av = np.mean(stopresults.inflows_person[id_stop])*3600181182ax.plot(time, stopresults.inflows_veh[id_stop]*3600,183label='Effective vehicle',184color='b',185linestyle='--', linewidth=self.width_line,186marker='s', markersize=4*self.width_line,187)188189ax.plot(time, stopresults.inflows_veh_sched[id_stop]*3600,190label='Scheduled vehicle',191color='c',192linestyle=':', linewidth=self.width_line,193marker='^', markersize=4*self.width_line,194)195196ax.plot(time, stopresults.inflows_person[id_stop]*3600,197label='Effective pers.',198color='g',199linestyle='-', linewidth=self.width_line,200marker='o', markersize=4*self.width_line,201)202203ax.plot([time[0], time[-1]], [flow_veh_av, flow_veh_av],204label='Average vehicle',205color='fuchsia',206linestyle='-', linewidth=2*self.width_line,207)208209ax.plot([time[0], time[-1]], [flow_pers_av, flow_pers_av],210label='Average person',211color='darkorange',212linestyle='-', linewidth=2*self.width_line,213)214215ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)216ax.grid(self.is_grid)217if self.is_title:218ax.set_title('Vehicle and person in-flows over time at PRT stop ID=%d' %219id_stop, fontsize=self.size_titlefont)220ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)221ax.set_ylabel('In-flows [1/h]', fontsize=self.size_labelfont)222ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))223ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))224225def plot_waiting_person_time(self, fig):226print 'plot_waiting_person_time'227stopresults = self.get_stopresults()228ax = fig.add_subplot(111)229230n_stop, n_steps = stopresults.get_dimensions()231t_step = stopresults.time_step.get_value()232#inds_stop = np.arange(n_stop, dtype = np.int32)233time = np.arange(n_steps, dtype=np.float32)*t_step234# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))235i = 0236for id_stop in stopresults.ids_stop.get_value():237ax.plot(time, 1.0/60.0*stopresults.waittimes_tot[id_stop],238COLORS[i], linewidth=self.width_line,239label='PRT Stop ID=%d' % id_stop)240i += 1241242if self.is_title:243ax.set_title('Number of waiting persons over time', fontsize=self.size_titlefont)244245ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)246ax.grid(self.is_grid)247ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)248ax.set_ylabel('Waiting times of passengers [min]', fontsize=self.size_labelfont)249ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))250ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))251252def plot_waiting_person_number_stop(self, fig):253print 'plot_waiting_person_number_stop'254stopresults = self.get_stopresults()255#ax1 = fig.add_subplot(211)256#ax2 = fig.add_subplot(212)257ax = fig.add_subplot(111)258259n_stop, n_steps = stopresults.get_dimensions()260t_step = stopresults.time_step.get_value()261#inds_stop = np.arange(n_stop, dtype = np.int32)262time = np.arange(n_steps, dtype=np.float32)*t_step263# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))264id_stop = self.id_stop_plot265ax.plot(time, stopresults.numbers_person_wait[id_stop],266'g', linewidth=self.width_line,267label='PRT Stop ID=%d' % id_stop)268269ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)270ax.grid(self.is_grid)271ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)272ax.set_ylabel('Number of waiting passengers', fontsize=self.size_labelfont)273ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))274ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))275276def plot_waiting_person_number(self, fig):277print 'plot_waiting_person_number'278stopresults = self.get_stopresults()279#ax1 = fig.add_subplot(211)280#ax2 = fig.add_subplot(212)281ax = fig.add_subplot(111)282283n_stop, n_steps = stopresults.get_dimensions()284t_step = stopresults.time_step.get_value()285#inds_stop = np.arange(n_stop, dtype = np.int32)286time = np.arange(n_steps, dtype=np.float32)*t_step287# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))288i = 0289for id_stop in stopresults.ids_stop.get_value():290print ' id_stop', id_stop291ax.plot(time, stopresults.numbers_person_wait[id_stop],292get_color(i), linewidth=self.width_line,293label='PRT Stop ID=%d' % id_stop)294i += 1295296ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)297ax.grid(self.is_grid)298ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)299ax.set_ylabel('Number of waiting passengers', fontsize=self.size_labelfont)300ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))301ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))302303def plot_flows_compare(self, fig):304print 'plot_flows_compare'305stopresults = self.get_stopresults()306#time_update_flows = self.parent.vehicleman.time_update_flows.get_value()307time_update_flows = 10308ax = fig.add_subplot(111)309310n_stop, n_steps = stopresults.get_dimensions()311t_step = stopresults.time_step.get_value()312313time = np.arange(n_steps, dtype=np.float32)*t_step314i = 0315flowmatrix = np.zeros((10, 10), dtype=np.int32)316for id_stop in stopresults.ids_stop.get_value():317print ' id_stop', id_stop318# print ' sched',stopresults.inflows_veh_sched[id_stop]319# print ' eff ',stopresults.inflows_veh[id_stop]320flowmatrix[np.array(time_update_flows*stopresults.inflows_veh_sched[id_stop], dtype=np.int32),321np.array(time_update_flows*stopresults.inflows_veh[id_stop], dtype=np.int32)] += 1322# ax.plot(stopresults.inflows_veh_sched[id_stop]*3600,stopresults.inflows_veh[id_stop]*3600,323# get_color(i),linewidth =self.width_line,324# label = 'PRT Stop ID=%d (effective)'%id_stop)325326i += 1327print 'flowmatrix', flowmatrix328# ax.matshow(flowmatrix)329330cax = ax.matshow(flowmatrix, cmap=cmx.get_cmap('PuBu'))331cbar = fig.colorbar(cax)332#ax.legend(loc='best',shadow=True, fontsize=self.size_labelfont)333ax.grid(self.is_grid)334ax.set_xlabel('Scheduled arrivals per interval', fontsize=self.size_labelfont)335ax.set_ylabel('Effective arrivals per interval', fontsize=self.size_labelfont)336ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))337ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))338339def plot_flows_compare_stop(self, fig):340print 'plot_flows_compare_stop'341stopresults = self.get_stopresults()342id_stop = self.id_stop_plot343#time_update_flows = self.parent.vehicleman.time_update_flows.get_value()344time_update_flows = 10345ax = fig.add_subplot(111)346347n_stop, n_steps = stopresults.get_dimensions()348t_step = stopresults.time_step.get_value()349350i = 0351flows_sched = stopresults.inflows_veh_sched[id_stop]352flows_eff = stopresults.inflows_veh[id_stop]353354x = flows_sched # -np.mean(flows_sched)355y = flows_eff # -np.mean(flows_eff)356flowcorr = np.correlate(x, y, 'full')/np.sqrt(np.sum(x*x)*np.sum(y*y))357358time = np.arange(-n_steps+1, n_steps, dtype=np.float32)*t_step359360print ' len(flowcorr),n_steps', len(flowcorr), len(time), n_steps361362ax.plot(time, flowcorr,363get_color(i), linewidth=self.width_line,364label='PRT Stop ID=%d' % id_stop)365366ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)367ax.grid(self.is_grid)368ax.set_xlabel('Time delay [s]', fontsize=self.size_labelfont)369ax.set_ylabel('Cross-Correlation', fontsize=self.size_labelfont)370ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))371ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))372373def plot_flows(self, fig):374print 'plot_flows'375stopresults = self.get_stopresults()376ax = fig.add_subplot(111)377378n_stop, n_steps = stopresults.get_dimensions()379t_step = stopresults.time_step.get_value()380381time = np.arange(n_steps, dtype=np.float32)*t_step382i = 0383for id_stop in stopresults.ids_stop.get_value():384ax.plot(time, stopresults.inflows_veh[id_stop]*3600,385get_color(i), linewidth=self.width_line,386label='PRT Stop ID=%d (effective)' % id_stop)387388ax.plot(time, stopresults.inflows_veh_sched[id_stop]*3600,389get_color(i), linestyle='--', linewidth=self.width_line,390label='PRT Stop ID=%d (scheduled)' % id_stop)391392ax.plot(time, stopresults.inflows_person[id_stop]*3600,393get_color(i), linestyle=':', linewidth=self.width_line,394label='PRT Stop ID=%d (person)' % id_stop)395396i += 1397398ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)399ax.grid(self.is_grid)400ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)401ax.set_ylabel('In-flows [1/h]', fontsize=self.size_labelfont)402ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))403ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))404405#('inflows_veh', {'name':'Vehicle in-flows', 'unit':'1/s', 'dtype':np.float32, 'info':'Vehicle flow into the stop over time.'}),406# ('inflows_veh_sched', {'name':'Sched. vehicle in-flows', 'unit':'1/s', 'dtype':np.float32, 'info':'Scheduled vehicle flow into the stop over time.'}),407# ('inflows_person', {'name':'Person in-flows', 'unit':'1/s', 'dtype':np.float32, 'info':'Person flow into the stop over time.'}),408409def plot_waiting_person(self, fig):410print 'plot_waiting_person'411stopresults = self.get_stopresults()412ax1 = fig.add_subplot(211)413ax2 = fig.add_subplot(212)414#ax = fig.add_subplot(111)415416n_stop, n_steps = stopresults.get_dimensions()417t_step = stopresults.time_step.get_value()418#inds_stop = np.arange(n_stop, dtype = np.int32)419time = np.arange(n_steps, dtype=np.float32)*t_step420# works:ax.plot(time.reshape(n_steps,1),numbers_person_wait.reshape(n_steps,-1))421i = 0422for id_stop in stopresults.ids_stop.get_value():423ax1.plot(time, stopresults.numbers_person_wait[id_stop],424get_color(i), linewidth=self.width_line,425label='PRT Stop ID=%d' % id_stop)426ax2.plot(time, 1.0/60.0*stopresults.waittimes_tot[id_stop],427get_color(i), linewidth=self.width_line,428label='PRT Stop ID=%d' % id_stop)429i += 1430431ax1.legend(loc='best', shadow=True, fontsize=14)432ax1.grid(self.is_grid)433ax1.set_xlabel('Time [s]', fontsize=14)434ax1.set_ylabel('Number of waiting passengers', fontsize=14)435ax1.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))436ax1.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))437438ax2.legend(loc='best', shadow=True, fontsize=14)439ax2.grid(self.is_grid)440ax2.set_xlabel('Time [s]', fontsize=14)441ax2.set_ylabel('Waiting times of passengers [min]', fontsize=14)442ax2.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))443ax2.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))444445def do(self):446# print 'do',self.edgeattrname447self.show()448return True449450def get_scenario(self):451return self._scenario452453454