forked from jorgevc/epi-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobilityNetwork.py
122 lines (95 loc) · 3.04 KB
/
MobilityNetwork.py
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
# MobilityNetwork.py
#
# Copyright 2018 Jorge Velazquez Castro
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import random as rand
class MobilityNetwork:
def __init__(self):
self.matrix = None
self.network = None
def weight_edges(self, Net, min_residential):
"""
Give weights to the edges of the of a networkx network, it also
adds the corresponding weight to the diagonal in order to
represent a mobility network where the rows are normalized.
"""
n=nx.number_of_nodes(Net)
for i in range (0,n):
Net.add_edge(i,i)
for u,v,d in Net.edges(data=True):
if u == v:
d['weight']=rand.uniform(min_residential,1)
else:
d['weight']=rand.uniform(0,min_residential)
B=nx.to_numpy_matrix(Net,weight='weight')
for i in range (0,n):
suma=0
for j in range (0,n):
suma=suma+B[i,j]
B[i,:]=B[i,:]/suma
NewNet=nx.from_numpy_matrix(B)
self.matrix = np.squeeze(np.asarray(B))
self.network = NewNet
return B
def binomial(self,n,p,min_residential):
"""
Se crea un matriz en el cual se muestra la interaccion de los parches
Parameters
--------
n : int
Numero de nodos en la red
p : float
Parametro de la matriz binomial
min_residential: float
Minima fraccion de tiempo que los individuos estan en sus propios parches.
Es decir la diagonal tendra numeros mayores o iguales a este valor
Returns
------
numpy matriz of size : n x n
"""
G=nx.binomial_graph(n,p)
self.weight_edges(G, min_residential)
def barabsi_albert(self,n,m,min_residential):
"""
Se crea un matriz en el cual se muestra la interaccion de los
parches con el algoritmo de Barabasi albert (red de mundo pequeno)
Parameters
--------
n : int
Numero de nodos en la red
m : float
Number of edges to attach from a new node to exiting nodes
min_residential: float
Minima fraccion de tiempo que los individuos estan en sus propios parches.
Es decir la diagonal tendra numeros mayores o iguales a este valor
Returns
------
numpy matriz of size : n x n
"""
G=nx.barabasi_albert_graph(n,m)
self.weight_edges(G, min_residential)
def random(self,n,m,min_residential):
G=nx.gnm_random_graph(n,m)
self.weight_edges(G, min_residential)
def draw_network():
nx.draw_networkx(self.network)
plt.show()