annotate plot_edge_bundling.py @ 1:7dc4b20f5974 draft default tip

Uploaded
author bornea
date Wed, 18 Oct 2017 15:32:49 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
1 import sys
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
2 def readTab(infile): # read in txt file
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
3 with open(infile, 'r') as input_file:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
4 # read in tab-delim text
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
5 output = []
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
6 for input_line in input_file:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
7 input_line = input_line.strip()
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
8 temp = input_line.split('\t')
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
9 output.append(temp)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
10 return output
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
11 def network2JSON(node_attr_file,edge_file):
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
12 nodes = readTab(node_attr_file)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
13 edges = readTab(edge_file)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
14
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
15 connections = {}
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
16 communities = {}
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
17 for i in nodes[1:]:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
18 communities[i[0]] = i[1]
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
19 for i in nodes[1:]:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
20 temp = []
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
21 for j in edges:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
22 if i[0] in j:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
23 if i[0] != j[0]:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
24 temp.append(str(communities[j[0]]) + "." + str(j[0]))
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
25 if i[0] != j[1]:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
26 temp.append(str(communities[j[1]])+"."+str(j[1]))
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
27 connections[i[0]] = temp
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
28 json = "["
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
29 for i in nodes[1:]:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
30 json = json + '{"name":"' + i[1] + '.' + str(i[0]) + '" ,"imports":'+str(connections[i[0]])+'},'
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
31 json = json[:-1]
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
32 json = json + "]"
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
33 json = json.replace("'",'"')
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
34 return json
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
35 ############################################
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
36 top = """<!DOCTYPE html>
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
37 <meta charset="utf-8">
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
38 <style>
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
39
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
40 .node {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
41 font: 300 11px "Arial", Arial, Arial, Arial;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
42 fill: #bbb;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
43 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
44
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
45 .node:hover {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
46 fill: #d62728;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
47 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
48
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
49 .link {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
50 stroke: steelblue;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
51 stroke-opacity: .4;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
52 fill: none;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
53 pointer-events: none;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
54 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
55
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
56 .node:hover,
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
57 .node--source,
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
58 .node--target {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
59 font-weight: 700;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
60 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
61
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
62 .node--source {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
63 fill: black;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
64 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
65
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
66 .node--target {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
67 fill: black;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
68 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
69
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
70 .link--source,
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
71 .link--target {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
72 stroke-opacity: 1;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
73 stroke-width: 2px;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
74 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
75
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
76 .link--source {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
77 stroke: #d62728;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
78 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
79
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
80 .link--target {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
81 stroke: #d62728;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
82 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
83
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
84 </style>
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
85 <body>
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
86 <script src="http://d3js.org/d3.v3.min.js"></script>
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
87 <script>
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
88
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
89 var diameter = 960,
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
90 radius = diameter / 2,
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
91 innerRadius = radius - 120;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
92
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
93 var cluster = d3.layout.cluster()
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
94 .size([360, innerRadius])
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
95 .sort(null)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
96 .value(function(d) { return d.size; });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
97
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
98 var bundle = d3.layout.bundle();
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
99
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
100 var line = d3.svg.line.radial()
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
101 .interpolate("bundle")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
102 .tension(.85)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
103 .radius(function(d) { return d.y; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
104 .angle(function(d) { return d.x / 180 * Math.PI; });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
105
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
106 var svg = d3.select("body").append("svg")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
107 .attr("width", diameter)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
108 .attr("height", diameter)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
109 .append("g")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
110 .attr("transform", "translate(" + radius + "," + radius + ")");
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
111
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
112 var link = svg.append("g").selectAll(".link"),
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
113 node = svg.append("g").selectAll(".node");"""
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
114 ############################################
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
115
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
116 middle = "\njsondata = '" + network2JSON(sys.argv[1],sys.argv[2]) + "'\n"
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
117
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
118 ############################################
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
119 bottom = """var data = JSON.parse(jsondata);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
120 function bundle_edges(classes){
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
121
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
122 var nodes = cluster.nodes(packageHierarchy(classes)),
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
123 links = packageImports(nodes);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
124
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
125 link = link
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
126 .data(bundle(links))
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
127 .enter().append("path")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
128 .each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
129 .attr("class", "link")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
130 .attr("d", line);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
131
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
132 node = node
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
133 .data(nodes.filter(function(n) { return !n.children; }))
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
134 .enter().append("text")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
135 .attr("class", "node")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
136 .attr("dy", ".31em")
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
137 .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
138 .style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
139 .text(function(d) { return d.key; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
140 .on("mouseover", mouseovered)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
141 .on("mouseout", mouseouted);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
142 };
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
143
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
144 function mouseovered(d) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
145 node
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
146 .each(function(n) { n.target = n.source = false; });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
147
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
148 link
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
149 .classed("link--target", function(l) { if (l.target === d) return l.source.source = true; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
150 .classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
151 .filter(function(l) { return l.target === d || l.source === d; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
152 .each(function() { this.parentNode.appendChild(this); });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
153
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
154 node
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
155 .classed("node--target", function(n) { return n.target; })
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
156 .classed("node--source", function(n) { return n.source; });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
157 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
158
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
159 function mouseouted(d) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
160 link
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
161 .classed("link--target", false)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
162 .classed("link--source", false);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
163
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
164 node
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
165 .classed("node--target", false)
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
166 .classed("node--source", false);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
167 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
168
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
169 d3.select(self.frameElement).style("height", diameter + "px");
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
170
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
171 // Lazily construct the package hierarchy from class names.
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
172 function packageHierarchy(classes) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
173 var map = {};
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
174
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
175 function find(name, data) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
176 var node = map[name], i;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
177 if (!node) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
178 node = map[name] = data || {name: name, children: []};
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
179 if (name.length) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
180 node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
181 node.parent.children.push(node);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
182 node.key = name.substring(i + 1);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
183 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
184 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
185 return node;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
186 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
187
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
188 classes.forEach(function(d) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
189 find(d.name, d);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
190 });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
191
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
192 return map[""];
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
193 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
194
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
195 // Return a list of imports for the given array of nodes.
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
196 function packageImports(nodes) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
197 var map = {},
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
198 imports = [];
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
199
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
200 // Compute a map from name to node.
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
201 nodes.forEach(function(d) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
202 map[d.name] = d;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
203 });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
204
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
205 // For each import, construct a link from the source to target node.
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
206 nodes.forEach(function(d) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
207 if (d.imports) d.imports.forEach(function(i) {
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
208 imports.push({source: map[d.name], target: map[i]});
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
209 });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
210 });
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
211
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
212 return imports;
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
213 }
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
214 bundle_edges(data);
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
215 </script>"""
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
216 ############################################
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
217 with open(sys.argv[3],"w") as x:
7dc4b20f5974 Uploaded
bornea
parents:
diff changeset
218 x.write(top + middle + bottom)