summaryrefslogtreecommitdiffstats
path: root/experiments/old_stuff/distances.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2015-03-30 15:01:56 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2015-03-30 15:01:56 -0400
commitb84dddecf2eab982941704a43663cf643be027d3 (patch)
tree273afacb4e2d2e043a6bbf75f774d8752e9fc202 /experiments/old_stuff/distances.py
parent68187fcfb505e87e5853c5a1b2e1dc073278a2ba (diff)
downloadcriminal_cascades-b84dddecf2eab982941704a43663cf643be027d3.tar.gz
Archive old code
Diffstat (limited to 'experiments/old_stuff/distances.py')
-rw-r--r--experiments/old_stuff/distances.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/experiments/old_stuff/distances.py b/experiments/old_stuff/distances.py
new file mode 100644
index 0000000..1834d36
--- /dev/null
+++ b/experiments/old_stuff/distances.py
@@ -0,0 +1,42 @@
+import networkx as nx
+import sys
+from collections import Counter
+
+
+def build_graph(filename):
+ g = nx.Graph()
+ with open(filename) as fh:
+ fh.readline()
+ for line in fh:
+ i, j = map(lambda x: int(float(x)), line.strip().split(",")[1:])
+ g.add_edge(i, j)
+ return g
+
+
+def distances(g):
+ victims = set()
+ with open("vics.csv") as fh:
+ fh.readline()
+ for line in fh:
+ victims.add(int(line.strip().split(",")[1]))
+ level = 0
+ seen = {}
+ nextlevel = {v: 1 for v in victims}
+ while level <= 3:
+ thislevel = nextlevel
+ nextlevel = {}
+ for v in thislevel:
+ if v not in seen:
+ seen[v] = level
+ nextlevel.update(g[v])
+ level += 1
+ return seen
+
+
+if __name__ == "__main__":
+ g = build_graph(sys.argv[1])
+ print g.number_of_nodes(), g.number_of_edges()
+ cnt = Counter()
+ for v, d in distances(g).iteritems():
+ cnt[d] += 1
+ print cnt.most_common()