diff options
| author | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2014-12-05 12:09:54 -0500 |
|---|---|---|
| committer | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2014-12-05 12:09:54 -0500 |
| commit | 67772e10f297d23e8d99b3901d044a3bb3345214 (patch) | |
| tree | 75ef6f67d7279a64c9ce1cb425e2edcb4f38ce11 /jpa_test | |
| parent | 011a965b4d36ef4a9d42ab945a402f8cc602f496 (diff) | |
| download | cascades-67772e10f297d23e8d99b3901d044a3bb3345214.tar.gz | |
timeout file added
Diffstat (limited to 'jpa_test')
| -rw-r--r-- | jpa_test/algorithms.py | 24 | ||||
| -rw-r--r-- | jpa_test/cascade_creation.py | 63 | ||||
| -rw-r--r-- | jpa_test/timeout.py | 22 |
3 files changed, 78 insertions, 31 deletions
diff --git a/jpa_test/algorithms.py b/jpa_test/algorithms.py index 2ed015a..f3bf917 100644 --- a/jpa_test/algorithms.py +++ b/jpa_test/algorithms.py @@ -4,6 +4,7 @@ import cascade_creation from collections import Counter from itertools import izip import convex_optimization +import timeout def greedy_prediction(G, cascades): @@ -33,7 +34,7 @@ def greedy_prediction(G, cascades): unaccounted[t] = False return G_hat - +@timeout.timeout(10) def recovery_l1obj_l2constraint(G, cascades): """ Returns estimated graph from following convex program: @@ -43,11 +44,15 @@ def recovery_l1obj_l2constraint(G, cascades): G_hat = cascade_creation.InfluenceGraph(max_proba=None) G_hat.add_nodes_from(G.nodes()) for node in G_hat.nodes(): + print node M, w = cascade_creation.icc_matrixvector_for_node(cascades, node) p_node, __ = convex_optimization.l1obj_l2constraint(M,w) + G_hat = cascade_creation.add_edges_from_proba_vector(G=G_hat, + p_node=p_node, node=node, floor_cstt=.01) + return G_hat -def correctness_measure(G, G_hat): +def correctness_measure(G, G_hat, print_values=False): """ Measures correctness of estimated graph G_hat to ground truth G """ @@ -56,6 +61,10 @@ def correctness_measure(G, G_hat): fp = edges_hat - edges fn = edges - edges_hat gp = edges | edges_hat + if print_values: + print "False Positives: {}".format(len(fp)) + print "False Negatives: {}".format(len(fn)) + print "Good Positives: {}".format(len(gp)) return fp, fn, gp @@ -63,13 +72,16 @@ def test(): """ unit test """ - G = cascade_creation.InfluenceGraph(max_proba = .3) - G.erdos_init(n = 100, p = .5) + G = cascade_creation.InfluenceGraph(max_proba = .5) + G.erdos_init(n = 100, p = .3) import time t0 = time.time() - A = cascade_creation.generate_cascades(G, .2, 2) + A = cascade_creation.generate_cascades(G, .2, 50) + + G_hat = recovery_l1obj_l2constraint(G, A) + + correctness_measure(G, G_hat, print_values=True) - recovery_l1obj_l2constraint(G, A) if __name__=="__main__": test() diff --git a/jpa_test/cascade_creation.py b/jpa_test/cascade_creation.py index cc84c70..dbb9e54 100644 --- a/jpa_test/cascade_creation.py +++ b/jpa_test/cascade_creation.py @@ -70,6 +70,35 @@ class Cascade(list): 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 & 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 & np.logical_not(active) + if not cascade: + print "Empty cascade, consider changing p_init or n_nodes. Retrying." + return icc_cascade(G, p_init) + 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 icc_matrixvector_for_node(cascades, node): """ for the ICC model: @@ -103,33 +132,17 @@ def normalize_matrix(M): return normalize(M.astype("float32"), axis=0, norm="l2") -def icc_cascade(G, p_init): +def add_edges_from_proba_vector(G, p_node, node, floor_cstt): """ - Returns boolean vectors for one cascade - True means node was active at that time step - p_init: proba that node in seed set + Takes proba vector, node and adds edges to G by flooring very small + probabilities + Also updates G's mat matrix """ - susceptible = np.ones(G.number_of_nodes(), dtype=bool) - active = np.random.rand(G.number_of_nodes()) < p_init - 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 & np.logical_not(active) - if not cascade: - print "Empty cascade, consider changing p_init or n_nodes. Retrying." - return icc_cascade(G, p_init) - 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)] + floor_parent = np.nonzero(p_node*(p_node > floor_cstt)) + for parent in floor_parent[0]: + G.add_edge(parent, node) + #TODO: update G's mat matrix + return G def test(): diff --git a/jpa_test/timeout.py b/jpa_test/timeout.py new file mode 100644 index 0000000..52d7d92 --- /dev/null +++ b/jpa_test/timeout.py @@ -0,0 +1,22 @@ +from functools import wraps +import errno +import os +import signal + +def timeout(seconds=10, error_message=os.strerror(errno.ETIME)): + def decorator(func): + def _handle_timeout(signum, frame): + raise Exception(error_message) + + def wrapper(*args, **kwargs): + signal.signal(signal.SIGALRM, _handle_timeout) + signal.alarm(seconds) + try: + result = func(*args, **kwargs) + finally: + signal.alarm(0) + return result + + return wraps(func)(wrapper) + + return decorator |
