Incidence and Adjacency Matrices¶
In this example, we take a look at calculation of incidence and adjacency matrices with OOPNET and NetworkX.
We first have to import the required packages:
os
is used for specifying the path to the EPANET input filenetworkx
is responsible for the calculation of the matricesmatplotlib.pyplot
plots the matricesoopnet
provides the means to use EPANET models for the matrix calculations
import os
import networkx as nx
from matplotlib import pyplot as plt
import oopnet as on
We are using the “Anytown” model in this example. We specify the path to the model and read it:
filename = os.path.join('data', 'anytown.inp')
net = on.Network.read(filename)
Next, we create a networkx.Graph
object from our network by using the oopnet.Graph
factory, get a list of all node IDs and convert the link objects in the model to networkx
links:
G = on.Graph(net)
nodes = on.get_node_ids(net)
links = on.onlinks2nxlinks(net)
We can now calculate the incidence matrix for our network and show it in the console:
A = nx.incidence_matrix(G, nodelist=nodes, edgelist=links)
print('Incidence Matrix - not oriented')
print(A)
Next, we use the oriented
argument to get the oriented incidence matrix:
B = nx.incidence_matrix(G, nodelist=nodes, edgelist=links, oriented=True)
print('Incidence matrix - oriented')
print(B)
Getting the adjacency matrix works very similar:
C = nx.adjacency_matrix(G, nodelist=nodes)
print('Adjacency matrix; undirected graph')
print(C)
We can not only create a simple networkx.Graph
object from our network, we can also create networkx.DiGraph
, networkx.MultiGraph
and networkx.MultiDiGraph
objects. OOPNET provides factories for all of these graph types (oopnet.Graph
, oopnet.DiGraph
, oopnet.MultiGraph
and oopnet.MultiDiGraph
). Here, we create a new networkx.DiGraph
and calculate the adjacency matrix:
G = on.DiGraph(net)
D = nx.adjacency_matrix(G, nodelist=nodes)
print('Adjacency matrix; directed graph')
print(D)
Finally, we can use Matplotlib to plot the different matrices:
plt.figure(1)
plt.imshow(A.todense(), cmap='viridis', interpolation='nearest')
plt.title('Incidence matrix - not oriented')
plt.colorbar()
plt.figure(2)
plt.imshow(B.todense(), cmap='viridis', interpolation='nearest')
plt.title('Incidence matrix - oriented')
plt.colorbar()
plt.figure(3)
plt.imshow(C.todense(), cmap='viridis', interpolation='nearest')
plt.title('Adjacency matrix; undirected graph')
plt.colorbar()
plt.figure(4)
plt.imshow(D.todense(), cmap='viridis', interpolation='nearest')
plt.title('Adjacency matrix; directed graph')
plt.colorbar()
Use plt.show()
to show the plots:
plt.show()
Summary¶
import os
import networkx as nx
from matplotlib import pyplot as plt
import oopnet as on
filename = os.path.join('data', 'anytown.inp')
net = on.Network.read(filename)
G = on.Graph(net)
nodes = on.get_node_ids(net)
links = on.onlinks2nxlinks(net)
A = nx.incidence_matrix(G, nodelist=nodes, edgelist=links)
print('Incidence Matrix - not oriented')
print(A)
B = nx.incidence_matrix(G, nodelist=nodes, edgelist=links, oriented=True)
print('Incidence matrix - oriented')
print(B)
C = nx.adjacency_matrix(G, nodelist=nodes)
print('Adjacency matrix; undirected graph')
print(C)
G = on.DiGraph(net)
D = nx.adjacency_matrix(G, nodelist=nodes)
print('Adjacency matrix; directed graph')
print(D)
plt.figure(1)
plt.imshow(A.todense(), cmap='viridis', interpolation='nearest')
plt.title('Incidence matrix - not oriented')
plt.colorbar()
plt.figure(2)
plt.imshow(B.todense(), cmap='viridis', interpolation='nearest')
plt.title('Incidence matrix - oriented')
plt.colorbar()
plt.figure(3)
plt.imshow(C.todense(), cmap='viridis', interpolation='nearest')
plt.title('Adjacency matrix; undirected graph')
plt.colorbar()
plt.figure(4)
plt.imshow(D.todense(), cmap='viridis', interpolation='nearest')
plt.title('Adjacency matrix; directed graph')
plt.colorbar()
plt.show()