summaryrefslogtreecommitdiffstats
path: root/ic_experiments/build_network.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2015-09-14 23:08:02 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2015-09-14 23:08:02 -0400
commitab0b1f3cefedb35327a19ec1b6afd560bfdf802d (patch)
treeb777f3e2c0ac0e712d8c5faab5107b1d236e2c3a /ic_experiments/build_network.py
parent960676226862d2d68c7a9c04c56d4f8157803025 (diff)
downloadcriminal_cascades-ab0b1f3cefedb35327a19ec1b6afd560bfdf802d.tar.gz
Import supplements and repo reorganization
Diffstat (limited to 'ic_experiments/build_network.py')
-rw-r--r--ic_experiments/build_network.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/ic_experiments/build_network.py b/ic_experiments/build_network.py
new file mode 100644
index 0000000..c58d5cf
--- /dev/null
+++ b/ic_experiments/build_network.py
@@ -0,0 +1,58 @@
+from csv import DictReader
+import sys
+from cPickle import dump
+from os.path import splitext
+
+
+def build_network(filename):
+ victims = {}
+ non_victims = {}
+ age = 0
+ t_max = 1000
+ with open(filename) as fh:
+ reader = DictReader(fh)
+ for row in reader:
+ from_, to = int(float(row["from"])), int(float(row["to"]))
+ dist = int(row["dist"])
+ w1, w2, w3 = float(row["w1"]), float(row["w2"]), float(row["w3"])
+ # if int(float(row["dist"])) > 1:
+ # continue
+ # 'to' is a victim
+ if row["t2"] != "NA":
+ dt = int(row["t2"]) - int(row["t1"])
+ parent = (from_, dist, dt, w1, w2, w3)
+ if to not in victims:
+ age += int(row["t2"]) - int(row["spawn2"])
+ victims[to] = []
+ victims[to].append(parent)
+ if from_ not in victims:
+ age += int(row["t1"]) - int(row["spawn1"])
+ victims[from_] = []
+ # 'to' is not a victim
+ else:
+ dt = t_max - int(row["t1"])
+ parent = (from_, dist, dt, w1, w2, w3)
+ if to not in non_victims:
+ # age += 3012 - int(row["spawn2"])
+ non_victims[to] = []
+ non_victims[to].append(parent)
+ if from_ not in victims:
+ age += int(row["t1"]) - int(row["spawn1"])
+ victims[from_] = []
+ root_victims = {}
+ for victim in victims.keys():
+ if not victims[victim]:
+ del victims[victim]
+ root_victims[victim] = []
+ print len(root_victims), len(victims), len(non_victims)
+ return root_victims, victims, non_victims, age
+
+
+if __name__ == "__main__":
+ if len(sys.argv) < 2:
+ sys.exit("usage: {0} <file>".format(sys.argv[0]))
+
+ filename = sys.argv[1]
+ root, _ = splitext(filename)
+ root_victims, victims, non_victims, age = build_network(filename)
+ dump((root_victims, victims, non_victims, age), open(root + ".pickle", "w"))