-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcorrexplorer.js
301 lines (256 loc) · 9.95 KB
/
correxplorer.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
window.onload=function(){
d3.select("button#file_load").on("click", function() {
load_all();
});
var load_all = function(){
d3.csv(d3.select("input#file_path")[0][0].value, function(data){
// I de-dictionatize d3 stuff
// as of now assumes both columns and row labels
var label_col_full = Object.keys(data[0]);
var label_row = [];
var rows = [];
var row = [];
for(var i = 0; i < data.length; i++){
label_row.push(data[i][label_col_full[0]]);
row = [];
for(var j = 1; j < label_col_full.length; j++){
row.push(parseFloat(data[i][label_col_full[j]]));
}
rows.push(row);
}
d3.select("svg").remove();
if ( !d3.select("input#transpose")[0][0].checked){
main(rows, label_col_full.slice(1), label_row);
} else {
main(d3.transpose(rows), label_row, label_col_full.slice(1));
}
});
};
load_all(); // not to start with nothing
};
var main = function(corr, label_col, label_row){
var transition_time = 1500;
var body = d3.select('body');
var tooltip = body.select('div.tooltip');
// .style("opacity", 1e-6);
var svg = body.append('svg')
.attr('width', 3000)
.attr('height', 3000);
// Autodetect symmetric tables
d3.select("input#keep_symmetry")
.each(function(){ this.checked = JSON.stringify(label_col) === JSON.stringify(label_row); });
var keep_symmetry = d3.select("input#keep_symmetry")[0][0].checked;
d3.select("input#keep_symmetry").on("change", function() {
if (corr.length !== corr[0].length) {
this.checked = false;
// or I can disable it
}
keep_symmetry = this.checked;
if(keep_symmetry){ reorder_matrix(last_k, last_what); }
});
var sort_process = d3.select("select#sort_func")[0][0].value;
d3.select("select#sort_func").on("change", function() {
sort_process = this.value;
reorder_matrix(last_k, last_what);
});
var row = corr;
var col = d3.transpose(corr);
// converts a matrix into a sparse-like entries
// maybe 'expensive' for large matrices, but helps keeping code clean
var indexify = function(mat){
var res = [];
for(var i = 0; i < mat.length; i++){
for(var j = 0; j < mat[0].length; j++){
res.push({i:i, j:j, val:mat[i][j]});
}
}
return res;
};
var corr_data = indexify(corr);
var order_col = d3.range(label_col.length + 1);
var order_row = d3.range(label_row.length + 1);
var color = d3.scale.linear()
.domain([-1,0,1])
.range(['blue','white','red']);
var scale = d3.scale.linear()
.domain([0, d3.min([50, d3.max([label_col.length, label_row.length, 4])])])
.range([0, parseFloat(d3.select("input#zoom")[0][0].value) * 600]);
d3.select("input#zoom").on("change", function() {
scale = d3.scale.linear()
.domain([0, d3.min([50, d3.max([label_col.length, label_row.length, 4])])])
.range([0, parseFloat(this.value) * 600]);
tick_col.transition()
.duration(transition_time)
.attr('font-size', scale(0.8))
.attr('transform', function(d, i){return 'rotate(270 ' + scale(order_col[i] + 0.7) + ',0)';})
.attr('x', function(d, i){return scale(order_col[i] + 0.7);});
tick_row.transition()
.duration(transition_time)
.attr('font-size', scale(0.8))
.attr('y', function(d, i){return scale(order_row[i] + 0.7);});
pixel.transition()
.duration(transition_time)
.attr('width', scale(0.9))
.attr('height', scale(0.9))
.attr('y', function(d){return scale(order_row[d.i]);})
.attr('x', function(d){return scale(order_col[d.j]);});
// the below does not work, as
// refresh_order();
// tick_col.transition().duration(transition_time)
// .attr('font-size', scale(0.8));
// tick_row.transition().duration(transition_time)
// .attr('font-size', scale(0.8));
// pixel.transition().duration(transition_time)
// .attr('width', scale(0.9))
// .attr('height', scale(0.9));
});
var label_space = 225;
// I will make it also a function of scale and max label length
var matrix = svg.append('g')
.attr('class','matrix')
.attr('transform', 'translate(' + (label_space + 10) + ',' + (label_space + 10) + ')');
var pixel = matrix.selectAll('rect.pixel').data(corr_data);
// as of now, data not changable, only sortable
pixel.enter()
.append('rect')
.attr('class', 'pixel')
.attr('width', scale(0.9))
.attr('height', scale(0.9))
.style('fill',function(d){ return color(d.val);})
.on('mouseover', function(d){pixel_mouseover(d);})
.on('mouseout', function(d){mouseout(d);});
// .on('click', function(d){reorder_matrix(d.i, 'col'); reorder_matrix(d.j, 'row');});
//the last thing works only for symmetric matrices, but with asymmetric sorting
tick_col = svg.append('g')
.attr('class','ticks')
.attr('transform', 'translate(' + (label_space + 10) + ',' + (label_space) + ')')
.selectAll('text.tick')
.data(label_col);
tick_col.enter()
.append('text')
.attr('class','tick')
.style('text-anchor', 'start')
.attr('transform', function(d, i){return 'rotate(270 ' + scale(order_col[i] + 0.7) + ',0)';})
.attr('font-size', scale(0.8))
.text(function(d){ return d; })
.on('mouseover', function(d, i){tick_mouseover(d, i, col[i], label_row);})
.on('mouseout', function(d){mouseout(d);})
.on('click', function(d, i){reorder_matrix(i, 'col');});
tick_row = svg.append('g')
.attr('class','ticks')
.attr('transform', 'translate(' + (label_space) + ',' + (label_space + 10) + ')')
.selectAll('text.tick')
.data(label_row);
tick_row.enter()
.append('text')
.attr('class','tick')
.style('text-anchor', 'end')
.attr('font-size', scale(0.8))
.text(function(d){ return d; })
.on('mouseover', function(d, i){tick_mouseover(d, i, row[i], label_col);})
.on('mouseout', function(d){mouseout(d);})
.on('click', function(d, i){reorder_matrix(i, 'row');});
var pixel_mouseover = function(d){
tooltip.style("opacity", 0.8)
.style("left", (d3.event.pageX + 15) + "px")
.style("top", (d3.event.pageY + 8) + "px")
.html(d.i + ": " + label_row[d.i] + "<br>" + d.j + ": " + label_col[d.j] + "<br>" + "Value: " + (d.val > 0 ? "+" : " ") + d.val.toFixed(3));
};
var mouseout = function(d){
tooltip.style("opacity", 1e-6);
};
var tick_mouseover = function(d, i, vec, label){
// below can be optimezed a lot
var indices = d3.range(vec.length);
// also value/abs val?
indices.sort(function(a, b){ return Math.abs(vec[b]) - Math.abs(vec[a]); });
res_list = [];
for(var j = 0; j < Math.min(vec.length, 10); j++) {
res_list.push((vec[indices[j]] > 0 ? "+" : " ") + vec[indices[j]].toFixed(3) + " " + label[indices[j]]);
}
tooltip.style("opacity", 0.8)
.style("left", (d3.event.pageX + 15) + "px")
.style("top", (d3.event.pageY + 8) + "px")
.html("" + i + ": " + d + "<br><br>" + res_list.join("<br>"));
};
var refresh_order = function(){
tick_col.transition()
.duration(transition_time)
.attr('transform', function(d, i){return 'rotate(270 ' + scale(order_col[i] + 0.7) + ',0)';})
.attr('x', function(d, i){return scale(order_col[i] + 0.7);});
tick_row.transition()
.duration(transition_time)
.attr('y', function(d, i){return scale(order_row[i] + 0.7);});
pixel.transition()
.duration(transition_time)
.attr('y', function(d){return scale(order_row[d.i]);})
.attr('x', function(d){return scale(order_col[d.j]);});
};
refresh_order();
var last_k = 0;
var last_what = 'col';
var reorder_matrix = function(k, what){
last_k = k;
last_what = what;
var order = [];
var vec = [];
var labels = [];
var vecs = [];
if(what === 'row'){ //yes, we are sorting counterpart
vec = row[k];
vecs = row;
labels = label_col; //test is if it ok
} else if ( what === 'col' ) {
vec = col[k];
vecs = col;
labels = label_row;
}
var indices = d3.range(vec.length);
switch (sort_process) {
case "value":
indices = indices.sort(function(a,b){return vec[b] - vec[a];});
break;
case "abs_value":
indices = indices.sort(function(a,b){return Math.abs(vec[b]) - Math.abs(vec[a]);});
break;
case "original":
break;
case "alphabetic":
indices = indices.sort(function(a,b){return Number(labels[a] > labels[b]) - 0.5;});
break;
case "similarity":
// Ugly, but sometimes we want to sort the coordinate we have clicked
// Also, as of now with no normalization etc
indices = d3.range(vecs.length);
indices = indices.sort(function(a,b){
var s = 0;
for(var i = 0; i < vec.length; i++){
s += (vecs[b][i] - vecs[a][i]) * vec[i];
}
return s;
});
if(what === 'col' || keep_symmetry){
order_col = reverse_permutation(indices);
} //not else if!
if ( what === 'row' || keep_symmetry) {
order_row = reverse_permutation(indices);
}
refresh_order();
return undefined;
}
if(what === 'row' || keep_symmetry){
order_col = reverse_permutation(indices);
} //not else if!
if ( what === 'col' || keep_symmetry) {
order_row = reverse_permutation(indices);
}
refresh_order();
};
var reverse_permutation = function(vec){
var res = [];
for(var i = 0; i < vec.length; i++){
res[vec[i]] = i;
}
return res;
};
};