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
|
from csv import DictReader
import sys
from cPickle import dump
from os.path import splitext
def build_network(filename):
victims = {}
non_victims = {}
age = 0.
with open(filename) as fh:
reader = DictReader(fh)
for row in reader:
from_, to = int(float(row["from"])), int(float(row["to"]))
if int(float(row["dist"])) > 2:
continue
if row["t2"] != "NA":
dt = int(row["t2"]) - int(row["t1"])
parent = (int(row["dist"]), dt)
if to not in victims:
age += int(row["t2"])
victims[to] = []
victims[to].append(parent)
if from_ not in victims:
age += int(row["t1"])
victims[from_] = []
else:
from_, to = int(float(row["from"])), int(float(row["to"]))
parent = (int(row["dist"]), 3012 - int(row["t1"]))
if to not in victims:
age += 3012
non_victims[to] = []
non_victims[to].append(parent)
if from_ not in victims:
age += int(row["t1"])
victims[from_] = []
root_victims = {}
for victim in victims.keys():
if not victims[victim]:
del victims[victim]
root_victims[victim] = []
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"))
|