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
|
import networkx as nx
import numpy as np
import collections
from itertools import izip
class InfluenceGraph(nx.Graph):
"""
networkX graph with mat and logmat 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_edges_from(G.edges())
def import_from_file(self, file_name):
"""
Takes a file from the Stanford collection of networks
"""
with open(file_name, 'r') as f:
for edge in f:
if "#" not in edge:
u, v = [int(node) for node in edge.split()]
self.add_edge(u, v)
@property
def mat(self):
if not hasattr(self, '_mat'):
self._mat = (self.max_proba * np.random.rand(len(self), len(self))
* np.asarray(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
class Cascade(list):
"""
Cascade object: list with attributes
"""
def __init__(self, *args, **kwargs):
super(Cascade, self).__init__(*args, **kwargs)
def infection_time(self, node):
"""
Returns lists of infections times for node i in cascade
"""
infected_times = []
for t, infected_set in izip(xrange(len(self)), self):
if infected_set[node]:
infected_times.append(t)
return infected_times
def candidate_infectors(self, node):
"""
Returns Counter of nodes infected just before node i was
"""
candidate_infectors = collections.Counter()
for t in self.infection_time(node):
if t > 0:
candidate_infectors.update(np.where(self[t-1])[0])
return candidate_infectors
def icc_cascade(G, p_init):
"""
Returns boolean vectors for one cascade
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)
active = np.random.rand(G.number_of_nodes()) < p_init
susceptible = susceptible - active
cascade = 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 generate_cascades(G, p_init, n_cascades):
"""
returns list of cascades
"""""
return [icc_cascade(G,p_init) for i in xrange(n_cascades)]
def test():
"""
unit test
"""
G = InfluenceGraph(max_proba = .3)
G.erdos_init(n = 100, p = 1)
import time
t0 = time.time()
print len(icc_cascade(G, p_init=.1))
t1 = time.time()
print t1 - t0
if __name__ == "__main__":
test()
|