diff options
| author | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2014-11-28 14:21:22 -0500 |
|---|---|---|
| committer | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2014-11-28 14:21:22 -0500 |
| commit | 6f092c5d1f6272049855257b85a924382724e1c6 (patch) | |
| tree | 6ef3f4fb7bef75711fdbc362c4ff5d3a93cf48ec /jpa_test/cascade_creation.py | |
| parent | e1961c8b7e2a5e50cd9e48983931dd47a69187a7 (diff) | |
| download | cascades-6f092c5d1f6272049855257b85a924382724e1c6.tar.gz | |
influence-cascades
Diffstat (limited to 'jpa_test/cascade_creation.py')
| -rw-r--r-- | jpa_test/cascade_creation.py | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/jpa_test/cascade_creation.py b/jpa_test/cascade_creation.py index cdcfd32..ac094b9 100644 --- a/jpa_test/cascade_creation.py +++ b/jpa_test/cascade_creation.py @@ -1,31 +1,35 @@ import networkx as nx import numpy as np -def icc_cascade(G, p_init): +class InfluenceGraph(nx.Graph): """ - 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 + Inherits from the graph class + with new init function and other attributes """ - susceptible = np.ones(G.number_of_nodes(), dtype=bool) - active = np.random.rand(G.number_of_nodes()) < p_init - susceptible = susceptible - active - cascade = [] - while sum(active) and sum(susceptible): - cascade.append(active) - tmp = np.zeros(G.number_of_nodes(), dtype=bool) - for node in np.where(active)[0]: - for edge in G.edges(node, data=True): - tmp[edge[1]] += np.random.rand() < edge[2]["weight"] \ - and susceptible[edge[1]] - active = tmp - susceptible = susceptible - active - cascade.append(active) - return cascade + 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()) + + @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_2(G, p_init): +def icc_cascade(G, p_init): """ input: graph with prob as edge attr returns: 2D boolean matrix with indep. casc. @@ -45,22 +49,28 @@ def icc_cascade_2(G, p_init): return cascade +def concat_cascades(cascades): + """ + Concatenate list of cascades into matrix + """ + + 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 - print len(icc_cascade_2(G, p_init=.1)) - print time.time() - t1 if __name__ == "__main__": test() |
