forked from nkoub/multinetx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.md~
More file actions
executable file
·290 lines (173 loc) · 8.42 KB
/
README.md~
File metadata and controls
executable file
·290 lines (173 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
 multiNetX v1.0
=========
multiNetX is a python package for the manipulation and
study of multilayer networks. The core of this package
is a MultilayerGraph, a class that inherits all properties
from networkx.Graph().
This allows for:
- Creating networks with weighted or unweighted links (only undirected networks are supported in this version)
- Analysing the spectral properties of adjacency or Laplacian matrices
- Visualizing dynamical processes by coloring the nodes and links accordingly
How to install multiNetX
=========
multinetx does not need intallation.
You simply download the source files and save them into your file system.
Then you have to add that directory to your PYTHONPATH.
In Unix/Linux you can do this by writting in the terminal the following command:
export PYTHONPATH=path_to_your_python_libraries/multinetx:$PYTHONPATH
How to use multiNetX
=========
#### Import standard libraries for numerics
import numpy as np
#### Import the package MultiNetX
import multinetx as mx
#### Create three Erd"os- R'enyi networks with N nodes for each layer
N = 8
g1 = mx.generators.erdos_renyi_graph(N,0.5,seed=218)
g2 = mx.generators.erdos_renyi_graph(N,0.6,seed=211)
g3 = mx.generators.erdos_renyi_graph(N,0.7,seed=208)
#### Create an 3Nx3N lil sparse matrix. It will be used to describe the layers interconnection
adj_block = mx.lil_matrix(np.zeros((N*3,N*3)))
#### Define the type of interconnection among the layers (here we use identity matrices thus connecting one-to-one the nodes among layers)
adj_block[0: N, N:2*N] = np.identity(N) # L_12
adj_block[0: N,2*N:3*N] = np.identity(N) # L_13
adj_block[N:2*N,2*N:3*N] = np.identity(N) # L_23
# use symmetric inter-adjacency matrix
adj_block += adj_block.T
#### Create an instance of the MultilayerGraph class
mg = mx.MultilayerGraph(list_of_layers=[g1,g2,g3],
inter_adjacency_matrix=adj_block)
#### Weights can be added to the edges
mg.set_edges_weights(intra_layer_edges_weight=2,
inter_layer_edges_weight=3)
The object mg inherits all properties from Graph of networkX, so that
we can calculate adjacency or Laplacian matrices, their eigenvalues, etc.
How to plot multiplex networks
=========
##### Import standard libraries
import numpy as np
from scipy.sparse import lil_matrix
import matplotlib.pyplot as plt
##### Import the package NetworkX and MultiNetX
import networkx as nx
import multinetx as mx
##### Create three Erd"os- R'enyi networks with N nodes for each layer
N = 50
g1 = nx.erdos_renyi_graph(N,0.07,seed=218)
g2 = nx.erdos_renyi_graph(N,0.07,seed=211)
g3 = nx.erdos_renyi_graph(N,0.07,seed=208)
#### Edge colored nertwork (no inter-connected layers)
##### Create the multiplex network
MG = mx.MultilayerGraph(list_of_layers=[g1,g2,g3])
##### Create the multiplex network with a different way
MG = mx.MultilayerGraph() # empty graph
MG.add_layer(g1)
MG.add_layer(g2)
MG.add_layer(g3)
MG.layers_interconnect() # zero inter-adjacency matrix
##### Set weights to the edges
MG.set_intra_edges_weights(layer=0,weight=1)
MG.set_intra_edges_weights(layer=1,weight=2)
MG.set_intra_edges_weights(layer=2,weight=3)
##### Plot the adjacency matrix and the multiplex networks
fig = plt.figure(figsize=(15,5))
ax1 = fig.add_subplot(121)
ax1.imshow(nx.adjacency_matrix(MG,weight='weight').todense(),
origin='upper',interpolation='nearest',cmap=plt.cm.jet_r)
ax1.set_title('supra adjacency matrix')
ax2 = fig.add_subplot(122)
ax2.axis('off')
ax2.set_title('edge colored network')
pos = mx.get_position(MG,nx.fruchterman_reingold_layout(g1),
layer_vertical_shift=0.2,
layer_horizontal_shift=0.0,
proj_angle=47)
nx.draw_networkx(MG,pos=pos,ax=ax2,node_size=50,with_labels=False,
edge_color=[MG[a][b]['weight'] for a,b in MG.edges()],
edge_cmap=plt.cm.jet_r)
plt.show()

#### Regular interconnected multiplex
##### Define the type of interconnection between the layers
adj_block = lil_matrix(np.zeros((N*3,N*3)))
adj_block[0: N, N:2*N] = np.identity(N) # L_12
adj_block[0: N,2*N:3*N] = np.identity(N) # L_13
#adj_block[N:2*N,2*N:3*N] = np.identity(N) # L_23
adj_block += adj_block.T
##### Create an instance of the MultilayerGraph class
MG = mx.MultilayerGraph(list_of_layers=[g1,g2,g3],
inter_adjacency_matrix=adj_block)
MG.set_edges_weights(inter_layer_edges_weight=4)
MG.set_intra_edges_weights(layer=0,weight=1)
MG.set_intra_edges_weights(layer=1,weight=2)
MG.set_intra_edges_weights(layer=2,weight=3)
##### Plot the adjacency matrix and the multiplex networks
fig = plt.figure(figsize=(15,5))
ax1 = fig.add_subplot(121)
ax1.imshow(nx.adjacency_matrix(MG,weight='weight').todense(),
origin='upper',interpolation='nearest',cmap=plt.cm.jet_r)
ax1.set_title('supra adjacency matrix')
ax2 = fig.add_subplot(122)
ax2.axis('off')
ax2.set_title('regular interconnected network')
pos = mx.get_position(MG,nx.fruchterman_reingold_layout(MG.get_layer(0)),
layer_vertical_shift=1.4,
layer_horizontal_shift=0.0,
proj_angle=7)
nx.draw_networkx(MG,pos=pos,ax=ax2,node_size=50,with_labels=False,
edge_color=[MG[a][b]['weight'] for a,b in MG.edges()],
edge_cmap=plt.cm.jet_r)
plt.show()

#### General multiplex multiplex
##### Define the type of interconnection between the layers
adj_block = lil_matrix(np.zeros((N*4,N*4)))
adj_block[0 : N , N:2*N] = np.identity(N) # L_12
adj_block[0 : N , 2*N:3*N] = np.random.poisson(0.005,size=(N,N)) # L_13
adj_block[0 : N , 3*N:4*N] = np.random.poisson(0.006,size=(N,N)) # L_34
adj_block[3*N:4*N , 2*N:3*N] = np.random.poisson(0.008,size=(N,N)) # L_14
adj_block += adj_block.T
adj_block[adj_block>1] = 1
##### Create an instance of the MultilayerGraph class
MG = mx.MultilayerGraph(list_of_layers=[g1,g2,g3,g1],
inter_adjacency_matrix=adj_block)
MG.set_edges_weights(inter_layer_edges_weight=4)
MG.set_intra_edges_weights(layer=0,weight=1)
MG.set_intra_edges_weights(layer=1,weight=2)
MG.set_intra_edges_weights(layer=2,weight=3)
MG.set_intra_edges_weights(layer=3,weight=5)
##### Plot the adjacency matrix and the multiplex networks
fig = plt.figure(figsize=(15,5))
ax1 = fig.add_subplot(121)
ax1.imshow(nx.adjacency_matrix(MG,weight='weight').todense(),
origin='upper',interpolation='nearest',cmap=plt.cm.jet_r)
ax1.set_title('supra adjacency matrix')
ax2 = fig.add_subplot(122)
ax2.axis('off')
ax2.set_title('general multiplex network')
pos = mx.get_position(MG,nx.fruchterman_reingold_layout(MG.get_layer(0)),
layer_vertical_shift=.6,
layer_horizontal_shift=0.9,
proj_angle=.6)
nx.draw_networkx(MG,pos=pos,ax=ax2,node_size=50,with_labels=False,
edge_color=[MG[a][b]['weight'] for a,b in MG.edges()],
edge_cmap=plt.cm.jet_r)
plt.show()

Copyright
=========
(C) Copyright 2013-2015, Nikos E Kouvaris
Each file in this folder is part of the multiNetX package.
multiNetX is part of the deliverables of the LASAGNE project
(multi-LAyer SpAtiotemporal Generalized NEtworks),
EU/FP7-2012-STREP-318132 (http://complex.ffn.ub.es/~lasagne/)
multiNetX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
multiNetX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.