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
|
import networkx as nx
import numpy as np
class InfluenceGraph(nx.Graph):
"""
Inherits from the graph class
with new init function and other attributes
"""
def __init__(self, max_proba, *args, **kwargs):
self.max_proba = max_proba
super(InfluenceGraph, self).__init__(*args, **kwargs)
def erdos_init(self, n, p):
G = nx.erdos_renyi_graph(n, p, directed=True)
self.add_nodes_from(G.nodes())
self.add_edges_from(G.edges())
def createStanfordGraph(self, file):
"""
Takes a file from the Stanford collection of networks
Need to remove comments on top of the file
Graph still needs to be weighted on the edges
"""
f = open(file, 'r')
data = f.readlines()
G = nx.DiGraph()
for edge in data:
split1 = edge.split('\t')
split2 = split1[1].split('\n')
u = int(split1[0])
v = int(split2[0])
G.add_edge(u,v)
return G
@property
def mat(self):
if not hasattr(self, '_mat'):
self._mat = (self.max_proba * np.random.rand(len(self), len(self))
* nx.adjacency_matrix(self))
return self._mat
@property
def logmat(self):
if not hasattr(self, '_logmat'):
self._logmat = np.log(1 - self.mat)
return self._logmat
def icc_cascade(G, p_init):
"""
input: graph with prob as edge attr
returns: 2D boolean matrix with indep. casc.
where True means node was active at that time step
p_init: proba that node in seed set
"""
susceptible = np.ones(G.number_of_nodes(), dtype=bool)
print susceptible
active = np.random.rand(G.number_of_nodes()) < p_init
print active
susceptible = susceptible - active
cascade = []
while active.any() and susceptible.any():
cascade.append(active)
active = np.exp(np.dot(G.logmat, active)) \
< np.random.rand(G.number_of_nodes())
active = active & susceptible
susceptible = susceptible - active
return cascade
def concat_cascades(cascades):
"""
Concatenate list of cascades into matrix
"""
return np.vstack(cascades)
def createStanfordGraph(file):
"""
Takes a file from the Stanford collection of networks
Need to remove comments on top of the file
Graph still needs to be weighted on the edges
"""
f = open(file, 'r')
data = f.readlines()
G = nx.DiGraph()
for edge in data:
split1 = edge.split('\t')
split2 = split1[1].split('\n')
u = int(split1[0])
v = int(split2[0])
G.add_edge(u,v)
return G
def test():
"""
unit test
"""
G = nx.erdos_renyi_graph(1000, 1, directed=True)
G.logmat = np.zeros((G.number_of_nodes(), G.number_of_nodes()))
G.mat = G.logmat
for edge in G.edges(data=True):
edge[2]['weight'] = .3*np.random.rand()
G.logmat[edge[0],edge[1]] = np.log(1 - edge[2]["weight"])
G.mat[edge[0], edge[1]] = edge[2]["weight"]
import time
t0 = time.time()
print len(icc_cascade(G, p_init=.1))
t1 = time.time()
print t1 - t0
if __name__ == "__main__":
test()
|