annotate plotForce_EV.py @ 1:bfe4b93b611e draft default tip

Uploaded
author bornea
date Wed, 18 Oct 2017 15:35:41 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
1 import sys
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
2 def readTab(infile): # read in txt file
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
3 with open(infile, 'r') as input_file:
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
4 # read in tab-delim text
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
5 output = []
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
6 for input_line in input_file:
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
7 input_line = input_line.strip()
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
8 temp = input_line.split('\t')
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
9 output.append(temp)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
10 return output
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
11 def network2JSON(nodes_att_file, edge_file):
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
12 nodes = readTab(nodes_att_file)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
13 edges = readTab(edge_file)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
14
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
15 start = """{"nodes": ["""
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
16 node_numbers = {}
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
17 cnt=0
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
18 scalar = 100
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
19 if nodes[0][2] == "page":
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
20 scalar = 5000
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
21 if nodes[0][2] == "betweenness":
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
22 scalar = 0.1
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
23 if nodes[0][2] == "closeness":
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
24 scalar = 100
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
25 for i in nodes[1:]:
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
26 node_numbers[i[0]] = cnt
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
27 cnt+=1
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
28 start = start + '{"id":'+'"'+i[0]+'", "size":'+str(float(i[2])*scalar)+', "score":'+i[1]+',"type":"circle"},'
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
29 start = start[:-1]
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
30 start = start+"""], "links": ["""
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
31 for i in edges:
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
32 start = start + """{"source":"""+str(node_numbers[i[0]])+""","target":"""+str(node_numbers[i[1]])+"""},"""
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
33 start = start[:-1]
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
34 start = start + """]}"""
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
35 return start
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
36
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
37
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
38 output = """<!DOCTYPE html>
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
39 <meta charset="utf-8">
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
40 <style>
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
41 body {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
42 overflow:hidden;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
43 margin:0;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
44 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
45
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
46 text {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
47 font-family: sans-serif;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
48 pointer-events: none;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
49 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
50
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
51 </style>
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
52 <body>
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
53 <script src="http://d3js.org/d3.v3.min.js"></script>
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
54 <script>
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
55 jsondata='"""
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
56
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
57 output = output + network2JSON(sys.argv[1],sys.argv[2]) + "'\n"
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
58
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
59 output = output + """var w = window.innerWidth;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
60 var h = window.innerHeight;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
61
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
62 var keyc = true, keys = true, keyt = true, keyr = true, keyx = true, keyd = true, keyl = true, keym = true, keyh = true, key1 = true, key2 = true, key3 = true, key0 = true
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
63
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
64 var focus_node = null, highlight_node = null;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
65
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
66 var text_center = false;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
67 var outline = false;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
68
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
69 var min_score = 0;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
70 var max_score = 1;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
71
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
72 var color = d3.scale.category20()
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
73 .range(["green", "orange", "red","black","steelblue","purple","gray","blue","yellow","lime"]);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
74
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
75 var highlight_color = "red";
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
76 var highlight_trans = 0.1;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
77
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
78 var size = d3.scale.pow().exponent(1)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
79 .domain([1,100])
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
80 .range([8,24]);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
81
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
82 var force = d3.layout.force()
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
83 .linkDistance(60)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
84 .charge(-700)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
85 .size([w,h]);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
86
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
87 var default_node_color = "#000";
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
88 //var default_node_color = "rgb(3,190,100)";
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
89 var default_link_color = "#888";
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
90 var nominal_base_node_size = 8;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
91 var nominal_text_size = 10;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
92 var max_text_size = 24;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
93 var nominal_stroke = 1.5;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
94 var max_stroke = 4.5;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
95 var max_base_node_size = 36;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
96 var min_zoom = 0.1;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
97 var max_zoom = 7;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
98 var svg = d3.select("body").append("svg");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
99 var zoom = d3.behavior.zoom().scaleExtent([min_zoom,max_zoom])
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
100 var g = svg.append("g");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
101 svg.style("cursor","move");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
102
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
103 var data = JSON.parse(jsondata);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
104 function plot_graph(graph) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
105
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
106 var linkedByIndex = {};
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
107 graph.links.forEach(function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
108 linkedByIndex[d.source + "," + d.target] = true;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
109 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
110
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
111 function isConnected(a, b) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
112 return linkedByIndex[a.index + "," + b.index] || linkedByIndex[b.index + "," + a.index] || a.index == b.index;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
113 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
114
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
115 function hasConnections(a) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
116 for (var property in linkedByIndex) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
117 s = property.split(",");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
118 if ((s[0] == a.index || s[1] == a.index) && linkedByIndex[property]) return true;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
119 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
120 return false;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
121 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
122
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
123 force
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
124 .nodes(graph.nodes)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
125 .links(graph.links)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
126 .start();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
127
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
128 var link = g.selectAll(".link")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
129 .data(graph.links)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
130 .enter().append("line")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
131 .attr("class", "link")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
132 .style("stroke-width",nominal_stroke)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
133 .style("stroke", function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
134 if (isNumber(d.score) && d.score>=0) return color(d.score);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
135 else return default_link_color; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
136
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
137
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
138 var node = g.selectAll(".node")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
139 .data(graph.nodes)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
140 .enter().append("g")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
141 .attr("class", "node")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
142
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
143 .call(force.drag)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
144
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
145
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
146 node.on("dblclick.zoom", function(d) { d3.event.stopPropagation();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
147 var dcx = (window.innerWidth/2-d.x*zoom.scale());
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
148 var dcy = (window.innerHeight/2-d.y*zoom.scale());
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
149 zoom.translate([dcx,dcy]);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
150 g.attr("transform", "translate("+ dcx + "," + dcy + ")scale(" + zoom.scale() + ")");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
151
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
152
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
153 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
154
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
155
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
156
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
157
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
158 var tocolor = "fill";
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
159 var towhite = "stroke";
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
160 if (outline) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
161 tocolor = "stroke"
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
162 towhite = "fill"
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
163 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
164
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
165
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
166
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
167 var circle = node.append("path")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
168
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
169
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
170 .attr("d", d3.svg.symbol()
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
171 .size(function(d) { return Math.PI*Math.pow(size(d.size)||nominal_base_node_size,2); })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
172 .type(function(d) { return d.type; }))
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
173
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
174 .style(tocolor, function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
175 if (isNumber(d.score) && d.score>=0) return color(d.score);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
176 else return default_node_color; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
177 //.attr("r", function(d) { return size(d.size)||nominal_base_node_size; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
178 .style("stroke-width", nominal_stroke)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
179 .style(towhite, "white");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
180
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
181
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
182 var text = g.selectAll(".text")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
183 .data(graph.nodes)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
184 .enter().append("text")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
185 .attr("dy", ".35em")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
186 .style("font-size", nominal_text_size + "px")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
187
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
188 if (text_center)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
189 text.text(function(d) { return d.id; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
190 .style("text-anchor", "middle");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
191 else
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
192 text.attr("dx", function(d) {return (size(d.size)||nominal_base_node_size);})
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
193 .text(function(d) { return '\u2002'+d.id; });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
194
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
195 node.on("mouseover", function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
196 set_highlight(d);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
197 })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
198 .on("mousedown", function(d) { d3.event.stopPropagation();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
199 focus_node = d;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
200 set_focus(d)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
201 if (highlight_node === null) set_highlight(d)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
202
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
203 } ).on("mouseout", function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
204 exit_highlight();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
205
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
206 } );
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
207
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
208 d3.select(window).on("mouseup",
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
209 function() {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
210 if (focus_node!==null)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
211 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
212 focus_node = null;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
213 if (highlight_trans<1)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
214 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
215
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
216 circle.style("opacity", 1);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
217 text.style("opacity", 1);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
218 link.style("opacity", 1);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
219 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
220 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
221
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
222 if (highlight_node === null) exit_highlight();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
223 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
224
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
225 function exit_highlight()
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
226 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
227 highlight_node = null;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
228 if (focus_node===null)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
229 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
230 svg.style("cursor","move");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
231 if (highlight_color!="white")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
232 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
233 circle.style(towhite, "white");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
234 text.style("font-weight", "normal");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
235 link.style("stroke", function(o) {return (isNumber(o.score) && o.score>=0)?color(o.score):default_link_color});
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
236 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
237
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
238 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
239 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
240
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
241 function set_focus(d)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
242 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
243 if (highlight_trans<1) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
244 circle.style("opacity", function(o) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
245 return isConnected(d, o) ? 1 : highlight_trans;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
246 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
247
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
248 text.style("opacity", function(o) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
249 return isConnected(d, o) ? 1 : highlight_trans;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
250 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
251
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
252 link.style("opacity", function(o) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
253 return o.source.index == d.index || o.target.index == d.index ? 1 : highlight_trans;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
254 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
255 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
256 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
257
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
258
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
259 function set_highlight(d)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
260 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
261 svg.style("cursor","pointer");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
262 if (focus_node!==null) d = focus_node;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
263 highlight_node = d;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
264
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
265 if (highlight_color!="white")
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
266 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
267 circle.style(towhite, function(o) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
268 return isConnected(d, o) ? highlight_color : "white";});
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
269 text.style("font-weight", function(o) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
270 return isConnected(d, o) ? "bold" : "normal";});
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
271 link.style("stroke", function(o) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
272 return o.source.index == d.index || o.target.index == d.index ? highlight_color : ((isNumber(o.score) && o.score>=0)?color(o.score):default_link_color);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
273
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
274 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
275 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
276 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
277
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
278
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
279 zoom.on("zoom", function() {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
280
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
281 var stroke = nominal_stroke;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
282 if (nominal_stroke*zoom.scale()>max_stroke) stroke = max_stroke/zoom.scale();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
283 link.style("stroke-width",stroke);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
284 circle.style("stroke-width",stroke);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
285
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
286 var base_radius = nominal_base_node_size;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
287 if (nominal_base_node_size*zoom.scale()>max_base_node_size) base_radius = max_base_node_size/zoom.scale();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
288 circle.attr("d", d3.svg.symbol()
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
289 .size(function(d) { return Math.PI*Math.pow(size(d.size)*base_radius/nominal_base_node_size||base_radius,2); })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
290 .type(function(d) { return d.type; }))
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
291
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
292 //circle.attr("r", function(d) { return (size(d.size)*base_radius/nominal_base_node_size||base_radius); })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
293 if (!text_center) text.attr("dx", function(d) { return (size(d.size)*base_radius/nominal_base_node_size||base_radius); });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
294
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
295 var text_size = nominal_text_size;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
296 if (nominal_text_size*zoom.scale()>max_text_size) text_size = max_text_size/zoom.scale();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
297 text.style("font-size",text_size + "px");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
298
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
299 g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
300 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
301
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
302 svg.call(zoom);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
303
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
304 resize();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
305 //window.focus();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
306 d3.select(window).on("resize", resize).on("keydown", keydown);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
307
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
308 force.on("tick", function() {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
309
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
310 node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
311 text.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
312
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
313 link.attr("x1", function(d) { return d.source.x; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
314 .attr("y1", function(d) { return d.source.y; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
315 .attr("x2", function(d) { return d.target.x; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
316 .attr("y2", function(d) { return d.target.y; });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
317
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
318 node.attr("cx", function(d) { return d.x; })
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
319 .attr("cy", function(d) { return d.y; });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
320 });
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
321
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
322 function resize() {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
323 var width = window.innerWidth, height = window.innerHeight;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
324 svg.attr("width", width).attr("height", height);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
325
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
326 force.size([force.size()[0]+(width-w)/zoom.scale(),force.size()[1]+(height-h)/zoom.scale()]).resume();
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
327 w = width;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
328 h = height;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
329 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
330
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
331 function keydown() {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
332 if (d3.event.keyCode==32) { force.stop();}
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
333 else if (d3.event.keyCode>=48 && d3.event.keyCode<=90 && !d3.event.ctrlKey && !d3.event.altKey && !d3.event.metaKey)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
334 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
335 switch (String.fromCharCode(d3.event.keyCode)) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
336 case "C": keyc = !keyc; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
337 case "S": keys = !keys; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
338 case "T": keyt = !keyt; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
339 case "R": keyr = !keyr; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
340 case "X": keyx = !keyx; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
341 case "D": keyd = !keyd; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
342 case "L": keyl = !keyl; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
343 case "M": keym = !keym; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
344 case "H": keyh = !keyh; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
345 case "1": key1 = !key1; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
346 case "2": key2 = !key2; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
347 case "3": key3 = !key3; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
348 case "0": key0 = !key0; break;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
349 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
350
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
351 link.style("display", function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
352 var flag = vis_by_type(d.source.type)&&vis_by_type(d.target.type)&&vis_by_node_score(d.source.score)&&vis_by_node_score(d.target.score)&&vis_by_link_score(d.score);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
353 linkedByIndex[d.source.index + "," + d.target.index] = flag;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
354 return flag?"inline":"none";});
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
355 node.style("display", function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
356 return (key0||hasConnections(d))&&vis_by_type(d.type)&&vis_by_node_score(d.score)?"inline":"none";});
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
357 text.style("display", function(d) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
358 return (key0||hasConnections(d))&&vis_by_type(d.type)&&vis_by_node_score(d.score)?"inline":"none";});
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
359
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
360 if (highlight_node !== null)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
361 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
362 if ((key0||hasConnections(highlight_node))&&vis_by_type(highlight_node.type)&&vis_by_node_score(highlight_node.score)) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
363 if (focus_node!==null) set_focus(focus_node);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
364 set_highlight(highlight_node);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
365 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
366 else {exit_highlight();}
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
367 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
368
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
369 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
370 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
371
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
372 };
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
373
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
374 function vis_by_type(type)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
375 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
376 switch (type) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
377 case "circle": return keyc;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
378 case "circle": return keys;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
379 case "triangle-up": return keyt;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
380 case "diamond": return keyr;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
381 case "cross": return keyx;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
382 case "triangle-down": return keyd;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
383 default: return true;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
384 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
385 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
386 function vis_by_node_score(score)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
387 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
388 if (isNumber(score))
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
389 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
390 if (score>=0.666) return keyh;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
391 else if (score>=0.333) return keym;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
392 else if (score>=0) return keyl;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
393 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
394 return true;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
395 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
396
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
397 function vis_by_link_score(score)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
398 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
399 if (isNumber(score))
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
400 {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
401 if (score>=0.666) return key3;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
402 else if (score>=0.333) return key2;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
403 else if (score>=0) return key1;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
404 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
405 return true;
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
406 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
407
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
408 function isNumber(n) {
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
409 return !isNaN(parseFloat(n)) && isFinite(n);
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
410 }
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
411 plot_graph(data)
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
412
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
413 </script>"""
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
414
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
415 with open(sys.argv[3],"w") as x:
bfe4b93b611e Uploaded
bornea
parents:
diff changeset
416 x.write(output)