aboutsummaryrefslogtreecommitdiffstats
path: root/jpa_test/cascade_creation.py
diff options
context:
space:
mode:
authorjeanpouget-abadie <jean.pougetabadie@gmail.com>2014-12-01 21:14:27 -0500
committerjeanpouget-abadie <jean.pougetabadie@gmail.com>2014-12-01 21:14:27 -0500
commitf8678b1b37f9136814be0197aabf32831d0c013e (patch)
treed46e76b4b379ab9a88ad999ec124b1b93e204a79 /jpa_test/cascade_creation.py
parentb1a6d4397bd1dc784a68987fc645eabe2bbcc8ec (diff)
downloadcascades-f8678b1b37f9136814be0197aabf32831d0c013e.tar.gz
rip_condition
Diffstat (limited to 'jpa_test/cascade_creation.py')
-rw-r--r--jpa_test/cascade_creation.py41
1 files changed, 26 insertions, 15 deletions
diff --git a/jpa_test/cascade_creation.py b/jpa_test/cascade_creation.py
index 09f0c45..a5c38cd 100644
--- a/jpa_test/cascade_creation.py
+++ b/jpa_test/cascade_creation.py
@@ -1,8 +1,8 @@
import networkx as nx
import numpy as np
import collections
-
from itertools import izip
+from sklearn.preprocessing import normalize
class InfluenceGraph(nx.Graph):
"""
@@ -76,21 +76,32 @@ def icc_matrixvector_for_node(cascades, node):
Returns M, w in matrix form where rows of M are i = t + k.T
Excludes all (t,k) after node infection time; w = 1_{infected}
"""
- w = []
- M = []
- for cascade in cascades:
- t_i = cascade.infection_time(node)[0]
- if t_i != 0:
- indicator = np.zeros(len(cascade[:t_i]))
- if t_i > 0:
- indicator[-1] = 1
- w.append(indicator)
- M.append(np.array(cascade[:t_i]))
- M = np.vstack(M)
- w = np.hstack(w)
+ #TODO: you need to remove the variable corresponding to the node you are solving for!!!!
+ if node is None:
+ return np.vstack(cascades), None
+ else:
+ w = []
+ M = []
+ for cascade in cascades:
+ t_i = cascade.infection_time(node)[0]
+ if t_i != 0:
+ indicator = np.zeros(len(cascade[:t_i]))
+ if t_i > 0:
+ indicator[-1] = 1
+ w.append(indicator)
+ M.append(np.array(cascade[:t_i]))
+ M = np.vstack(M)
+ w = np.hstack(w)
return M, w
+def normalize_matrix(M):
+ """
+ returns M with normalized (L_1 norm) columns
+ """
+ return normalize(M.astype("float32"), axis=0, norm="l2")
+
+
def icc_cascade(G, p_init):
"""
Returns boolean vectors for one cascade
@@ -99,14 +110,14 @@ def icc_cascade(G, p_init):
"""
susceptible = np.ones(G.number_of_nodes(), dtype=bool)
active = np.random.rand(G.number_of_nodes()) < p_init
- susceptible = susceptible - active
+ susceptible = susceptible & np.logical_not(active)
cascade = Cascade()
while active.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
+ susceptible = susceptible & np.logical_not(active)
if not cascade:
print "Empty cascade, consider changing p_init or n_nodes. Retrying."
return icc_cascade(G, p_init)