.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/metro_simulation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_metro_simulation.py: Metro simulation. ================ Example of a simulation of the evolution of a graph signal over the Santiago Metro network. The initial condition is a graph signal with only one positive integer value bigger.The plots or animation show how this signal distributes over the network using: .. math:: y = AD^-1x To run this example, you need to download three files and place them in the same directory as this script. 1. Download the file `Tablas de subidas y bajadas nov23.zip` from this link: https://www.dtpm.cl/descargas/modelos_y_matrices/Tablas%20de%20subidas%20y%20bajadas%20nov23.zip Then, uncompress the zip file and copy `2023.11 Matriz_baj_SS_MH.xlsb` to the same location as this script. 2. Download the file `santiago_metro_stations_coords.geojson` from this link: https://zenodo.org/records/11637462/files/santiago_metro_stations_coords.geojson 3. Download the file `santiago_metro_stations_connections.txt` from this link: https://zenodo.org/records/11637462/files/santiago_metro_stations_connections.txt .. GENERATED FROM PYTHON SOURCE LINES 33-50 .. code-block:: Python import os import matplotlib.pyplot as plt import networkx as nx import numpy as np from matplotlib import animation import pygsp2 as pg from pygsp2.utils_examples import fetch_data, make_metro_graph current_dir = os.getcwd() os.chdir(current_dir) # If set to true make animation, # otherwise store each frame as png MAKE_ANIMATION = True .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python assets_dir = os.path.join(current_dir, 'data') fetch_data(assets_dir, 'metro') .. GENERATED FROM PYTHON SOURCE LINES 55-62 .. code-block:: Python G, pos = make_metro_graph(edgesfile=os.path.join(assets_dir, 'santiago_metro_stations_connections.txt'), coordsfile=os.path.join(assets_dir, 'santiago_metro_stations_coords.geojson')) W = nx.adjacency_matrix(G).toarray() D = np.diag(W.sum(1)) D_inv = np.linalg.inv(D) .. GENERATED FROM PYTHON SOURCE LINES 63-68 .. code-block:: Python NSTEPS = 30 # Arbitrary units, steps INIT_VALUE = 5000 # Initial conditions signal = np.zeros([NSTEPS, len(W)]) signal[0, np.random.randint(0, len(W), 1)] = INIT_VALUE .. GENERATED FROM PYTHON SOURCE LINES 69-70 As a physical distribution network .. GENERATED FROM PYTHON SOURCE LINES 70-78 .. code-block:: Python print('Prerforming simulation...') for i in np.arange(1, NSTEPS): signal[i, :] = W @ D_inv @ signal[i - 1, :] print('Finished.') .. rst-class:: sphx-glr-script-out .. code-block:: none Prerforming simulation... Finished. .. GENERATED FROM PYTHON SOURCE LINES 79-141 .. code-block:: Python if MAKE_ANIMATION: # Initiate figure fig, ax = plt.subplots(1, 1, figsize=(10, 9)) cmap = plt.get_cmap('viridis') # Draw edges and nodes im = nx.draw_networkx_edges(G, pos, node_size=20, ax=ax) im = nx.draw_networkx_nodes(G, pos, node_color='gray', node_size=20, ax=ax) ax.set_title('T = 0') cbar = plt.colorbar(im, ax=ax, ticks=[0, 0.5, 1], label='Signal') cbar.set_ticklabels([0, np.amax(signal) / 2, np.amax(signal)]) # Define function for animation def update(frame): """Function that defines what happens in each frame.""" idxs = ((np.where(signal[frame, :] > 0)[0]).astype(int)) nodelist = list(np.array(G)[idxs]) colors = cmap(signal[frame, idxs] / INIT_VALUE) nx.draw_networkx_nodes(G, pos, node_color='gray', node_size=20, ax=ax) nx.draw_networkx_nodes(G, pos, node_color=colors, nodelist=nodelist, node_size=20, ax=ax) ax.set_title(f'T = {frame}') anim = animation.FuncAnimation(fig, update, frames=np.arange(0, len(signal)), interval=50) # saving to gif using ffmpeg writer writervideo = animation.PillowWriter(fps=5) anim.save('metro_simulation.gif', writer=writervideo) plt.show() else: try: os.mkdir('metro_simulation/') except FileExistsError: print('Warning: It seems like this folder already exists. Overwritting...') # Initiate figure fig, ax = plt.subplots(1, 1, figsize=(10, 9)) cmap = plt.get_cmap('viridis') # Draw edges and nodes im = nx.draw_networkx_edges(G, pos, node_size=20, ax=ax) im = nx.draw_networkx_nodes(G, pos, node_color='gray', node_size=20, ax=ax) ax.set_title('T = 0') cbar = plt.colorbar(im, ax=ax, ticks=[0, 0.5, 1], label='Signal') cbar.set_ticklabels([0, np.amax(signal) / 2, np.amax(signal)]) # Iterate through each graph signal for i, s in enumerate(signal): idxs = (np.where(s > 0)[0]).astype(int) nodelist = list(np.array(G)[idxs]) colors = cmap(s[idxs] / INIT_VALUE) im = nx.draw_networkx_nodes(G, pos, node_color='gray', node_size=20, ax=ax) im = nx.draw_networkx_nodes(G, pos, node_color=colors, nodelist=nodelist, node_size=20, ax=ax) ax.set_title(f'T = {i}') fig.savefig(f'metro_simulation/{i}.png') .. image-sg:: /examples/images/sphx_glr_metro_simulation_001.png :alt: T = 29 :srcset: /examples/images/sphx_glr_metro_simulation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.927 seconds) **Estimated memory usage:** 331 MB .. _sphx_glr_download_examples_metro_simulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: metro_simulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: metro_simulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: metro_simulation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_