-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix-correlation-diagram.html
420 lines (367 loc) · 17.3 KB
/
matrix-correlation-diagram.html
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<!doctype html>
<html>
<meta charset="utf-8">
<head>
<link rel="stylesheet" href="/libs/bootstrap/dist/css/bootstrap.min.css">
<script src="/source/d3.js"></script>
<style>
#img1{
margin-left: auto;
margin-right: auto;
}
#img2{
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<br>
<h1 style="text-align:center">Correlation (-1 to 1) of the brain waves with regards to each image</h1>
<div class="col-md-12">
<p>A person was shown 140 images and asked to try and remember each image. Images were abstract landscapes, which means the person viewing the images doesn't give a name to each image.
This allows us to see which images trigger which part of the brain, and how each part of the brain is linked with another.</p>
<p>
The correlation data was obtained through the following steps:
<ol>
<li>First read the binary file with python.</li>
<li>We have 64 channels of data, from the 64 nodes that were placed on the brain.</li>
<li>Data was read continuosly and each millisecond logged a brain wave in microvolts.</li>
<li>To gather correlation of the brain waves corresponding to the images, I took the average signal for each image.</li>
<li>Then for each image, I subtracted the average signal of that image from all the brain wave readings of that image (2000 datapoints for each image) </li>
<li>Then the final results I used the pearson correlation function to correlate the brain signals for every image.</li>
<li>We missed a step which was to filter the frequencies above 30 hz.</li>
<li>I also had to cluster the data in python for it so show nicely in the correlation matrix. I used scipy to do this.</li>
</ol>
</p>
<h2>Click on the graph below to display the diagram of the nodes on the head to see how strong the waves were.</h2>
</div>
<hr>
<br>
<div class="container">
<div class ="row">
<div class="col-md-6">
<div id="matrix"></div>
</div>
<div class="col-md-6">
<div id="correlation"></div>
</div>
</div>
<br>
<div class="row">
<div style="text-align:center;">
<button type="button" id="start_button">Play</button>
<button type="button" id="stop_button">Pause</button>
</div>
<br>
<div class="col-md-6">
<div id="slot1">
<input type="range" min="0" max="60" value=1 id="range_input1"/>
</div>
</div>
<div class="col-md-6">
<div id="slot2">
<input type="range" min="0" max="60" value=1 id="range_input2"/>
</div>
</div>
</div>
</div>
<script>
var json_frames;
d3.text('source/30fps_data.txt', function(error, data1){
json_frames = JSON.parse(data1);
})
var positions;
d3.text("source/positions.txt", function(dataa) {
positions = d3.csv.parseRows(dataa);
})
d3.json("python-scripts/data.json", function (data) {
var clustered_images_order = [5, 131, 40, 142, 165, 166, 212, 137, 80, 31, 21, 103, 138, 160, 35, 7, 173, 203, 119, 28, 54, 89, 130, 122, 123, 90, 44, 169, 26, 210, 168, 214, 19, 91, 2, 52, 13, 206, 196, 4, 208, 114, 22, 155, 8, 11, 167, 12, 33, 57, 98, 147, 87, 102, 127, 61, 39, 23, 47, 125, 78, 20, 141, 187, 84, 198, 70, 9, 135, 181, 144, 185, 101, 111, 58, 17, 194, 211, 153, 178, 143, 192, 94, 107, 216, 1, 139, 182, 159, 10, 85, 213, 95, 34, 175, 151, 112, 55, 42, 120, 64, 150, 92, 176, 171, 66, 43, 121, 145, 76, 18, 201, 3, 48, 136, 189, 156, 199, 163, 152, 79, 129, 172, 27, 96, 29, 204, 158, 46, 200, 32, 108, 113, 118, 67, 69, 190, 106, 162, 75];
var images_order = [136, 189, 3, 158, 87, 90, 54, 9, 10, 171, 129, 11, 103, 141, 216, 43, 156, 39, 2, 166, 58, 92, 67, 78, 26, 173, 101, 175, 32, 102, 208, 138, 48, 139, 18, 199, 167, 19, 40, 192, 120, 106, 127, 13, 165, 211, 95, 118, 57, 169, 203, 111, 112, 200, 96, 114, 35, 198, 182, 145, 152, 22, 168, 5, 178, 150, 162, 47, 122, 31, 181, 213, 204, 98, 44, 89, 135, 159, 66, 29, 8, 7, 187, 1, 121, 79, 23, 52, 212, 17, 176, 113, 20, 123, 137, 144, 151, 46, 147, 196, 28, 84, 85, 201, 172, 12, 214, 131, 94, 42, 190, 61, 206, 80, 153, 34, 69, 33, 210, 119, 185, 55, 108, 27, 4, 160, 70, 107, 76, 163, 155, 91, 142, 143, 64, 75, 125, 130, 21, 194];
//images are labelled by number so we can just make a hash
var images = {}
var createImages = function(){
for (var i=1; i<=216; i++){
images[i] = '/thumbs/' + i + '.jpg';
}
}
createImages();
var height = 500;
var width = height;
var pad = {
left:70,
top:40,
right:5,
bottom:70
};
var innerPad = 5;
var totalHeight = height + pad.top + pad.bottom;
var totalWidth = (width + pad.left + pad.right)*2;
var svg = d3.select("div#matrix")
.append("svg")
.attr("width", totalWidth)
.attr("height", totalHeight);
var corr_plot = svg.append("g")
.attr("id", "corr_plot")
.attr("transform", "translate(" + pad.left + "," + pad.right + ")");
//number of columns and rows respectively
var n_index = data.ind.length;
var n_var = data.var.length;
var corrXscale = d3.scale.ordinal().domain(d3.range(n_var)).rangeBands([0, width]);
var corrYscale = d3.scale.ordinal().domain(d3.range(n_var)).rangeBands([width, 0]);
var corrZscale = d3.scale.linear().domain([-1,0,1]).range(["darkslateblue", "white", "crimson"]);
//making data easier to work with
var i=0, j=0;
var corr = []
for (i in data.corr){
for (j in data.corr[i]){
corr.push({row:i, col:j, value:data.corr[i][j]});
}
};
var tooltip = d3.select("div#correlation")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("font-size", "large");
var format_corr = d3.format(".3f");
//each cell needs to be colored according to value
var cells = corr_plot.selectAll("empty")
.data(corr)
.enter()
.append("rect")
.attr("class", "cell")
.attr("x", function(d){ return corrXscale(d.col); })
.attr("y", function(d){ return corrYscale(d.row); })
.attr("width", corrXscale.rangeBand())
.attr("height", corrYscale.rangeBand())
.attr("fill", function(d){ return corrZscale(d.value); })
.attr("stroke", "none")
.attr("stroke-width", 2)
.on("mouseover", function(d) { var a = tooltip.style("visibility", "visible").text("Correlation: " + format_corr(d.value) + " | First Image: #" + data.ind[d.col] + " Second Image: #" + data.var[d.row])
.append("img")
.attr("src", "/thumbs/" + data.ind[d.col] + ".jpg")
.attr("style", "float:left");
tooltip
.append("img")
.attr("src", "/thumbs/" + data.var[d.row] + ".jpg")
.attr("style", "float:right");
corr_plot.append("text").attr("id", "corrtext").text(d3.format(".2f")(d.value))
.attr("x", function () {
var mult = -1;
if(d.col < n_var/2){
mult = 1;
}
return corrXscale(d.col) + mult * 30;
})
.attr("y", function () {
var mult = 1;
if(d.row < n_var/2){
mult = -1;
}
return corrYscale(d.row) + mult * 30;
})
.attr("fill", "black")
.attr("dominant-baseline", "middle")
.attr("text-anchor", "middle");
corr_plot.append("text")
.attr("class", "corrlabel")
.attr("x", corrXscale(d.col))
.attr("y", height + pad.bottom * 0.2)
.text(data["ind"][d.col])
.attr("dominant-baseline", "middle")
.attr("text-anchor", "middle");
corr_plot.append("text")
.attr("class", "corrlabel")
.attr("y", corrYscale(d.row))
.attr("x", -pad.left * 0.1)
.text(data["var"][d.row])
.attr("dominant-baseline", "middle")
.attr("text-anchor", "end");
return a;})
.on("mouseout", function(){
d3.selectAll("text.corrlabel").remove();
d3.selectAll("text#corrtext").remove();
d3.select(this).attr("stroke","none");
})
.on("click", function(d){ showBubble(data.ind[d.col], data.var[d.row]) });
corr_plot.append("rect")
.attr("height", height)
.attr("width", width)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("pointer-events", "none");
//integrating Matt's bubble diagram so that it displays two head diagrams on clickin inside of the matrix
function showBubble(firstImage, secondImage){
d3.selectAll("circle").remove();
drawBubble(firstImage, 1);
drawBubble(secondImage, 2);
}
function drawBubble(imageNumber, pos){
// 33 milleseconds * 60 = 1980 miliseconds < 2000 : in other words display 60 images in 33 milisecond intervals, which adds up to almost 2 seconds total
var interval = 33; //callback interval update every 33 miliseconds
var interval2 = 33; //this is how often callback gets executed
var counter = 0; //counts frames
var dx = 0; //change for times 0=stop 1=start
var frame = images_order.indexOf(imageNumber); //current picture displayed
var ctr2 = 0; //counter for each frame; for color change inbetween images 30fps depends on intervals
var z_list = [] //this is array of color scales for nodes
var min_glob = []; //this is global min value for all nodes at all times
var max_glob = []; //this is global max value for all nodes at all times
var PI = 3.141592653;
var location_picture = "pic_slot_" + pos;
var location_nodes = "nodes_slot_" + pos;
var div_tag = "div#slot" + pos;
d3.selectAll("img#img" + pos).remove();
d3.selectAll("svg#bubble" + pos).remove();
var body = d3.selectAll(div_tag).append("div").style("float","left");
var rang = d3.selectAll("#range_input")[0][0];
body.append("img")
.attr("src", "source/img/" + imageNumber + ".jpg")
.attr("id", "img" + pos)
.attr("align", "middle");
var margin = {top: -55, right: 0, bottom: 0, left: -30},
width = 1000 - margin.left - margin.right,
height = 1000 - margin.top - margin.bottom;
var svg = d3.select(div_tag).append("svg")
.attr("width", 476)
.attr("height", 476)
.attr("id", "bubble" + pos)
.style("background-image","url(source/nodes.jpeg)")
.append("g").attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
//this is for initial colors for current frame
var colors = [];
for(var i=0; i<64; i++){
colors[i] = json_frames[i][frame][30];
}
//find max and min for each channel and create a color scale for each
for(var i =0; i<64; i++){
var min_val =[];
var max_val = [];
for(var j = 0; j<140; j++){
var min1 = d3.min(json_frames[i][j]);
var max1 = d3.max(json_frames[i][j]);
min_val[j] = min1;
max_val[j] = max1;
}
var min = Math.ceil(d3.min(min_val));
var max = Math.ceil(d3.max(max_val));
z_list[i] = d3.scale.linear()
.domain([min,max])
.range(["white","red"]);
min_glob[i] = min;
max_glob[i] = max;
}
min_glob = Math.ceil(d3.min(min_glob));
max_glob = Math.ceil(d3.max(max_glob));
var size_scale = d3.scale.linear()
.domain([min_glob,max_glob])
.range([1,10000]);
var circles = svg.selectAll("circle")
.data(json_frames)
.enter()
.append("circle")
.attr("r",function (d,i){
var val = Math.sqrt(size_scale(d[frame][ctr2])/PI);
return val;
})
.attr("cx",function(d,i){ return ((positions[i*2])*.75)*.78;})
.attr("cy",function(d,i){ return ((positions[i*2+1])*.75)*.78;})
.attr("fill",function(d,i){ return z_list[i](colors[i]); })
.style("opacity", 0.9)
.on("mouseover", function(d,e,i){
d3.select(this)
.transition()
.style("opacity", 1);
tooltip.transition()
.duration(200)
.style("opacity", .9);
var e1 = e +1;
d3.select(this)
.transition()
.attr("stroke", "yellow");
tooltip.html("channel: "+ e1 + "<br/>val: "+ d[frame][ctr2])
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout",function(){
d3.select(this)
.transition()
.attr("stroke", "black");
tooltip.transition()
.duration(200).style("opacity", 0);
})
.on("click",function(d,e,i){
//this needs to be filled up by raj
})
.attr("stroke","black").attr("stroke-width",3);
var btn_click = d3.selectAll("#start_button").on("click", function(){
dx=1;
d3.timer(makeCallback(),interval);
});
var btn_click2 = d3.selectAll("#stop_button").on("click", function(){
dx=0;
});
var makeCallback = function() {
return function() {
if(dx>0){
if(counter>1960){
var vv = imageNumber ;
var im1 = "</br><img src='source/img/"+ vv + ".jpg'/>";
body.html(im1);
d3.selectAll("#input_label").html(rang.value);
rang.value = parseInt(rang.value) + dx;
counter=0;
//frame = rang.value;
ctr2 = 0;
}
ctr2 = ctr2 + 1;
d3.timer(makeCallback(),interval);
}
else{
//d3.timer(makeCallback(),0);
}
var cols = []
for(var i=0; i<64; i++){
cols[i] = json_frames[i][frame][ctr2-1];
}
circles.attr("fill",function(d,i){ return z_list[i](cols[i]); })
.attr("r",function (d,i){
var val = Math.sqrt(size_scale(d[frame][ctr2])/PI);
return val;
});
cols = [];
counter = counter+interval2;
return true;
}
};
d3.selectAll("#range_input")
.on("input", function(d,i){
threshold_filter = this.value;
d3.selectAll("#input_label")
.html(this.value);
var vv = parseInt(rang.value);
var im1 = "</br><img src='source/img/"+ vv+ ".jpg'/>";
frame = vv;
console.log("hi");
var cols = []
for(var i=0; i<64; i++){
cols[i] = json_frames[i][frame][30];
}
circles.attr("fill",function(d,i){ return z_list[i](cols[i]); })
.attr("r",function (d,i){
var val = Math.sqrt(size_scale(d[frame][ctr2])/PI);
return val;
});
});
}
})
</script>
<script type="text/javascript" src="/libs/jquery/dist/jquery.min.js"></script>
</body>
</html>