-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplainer.js
620 lines (580 loc) · 16.7 KB
/
explainer.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
(function(window){
'use strict';
function define_library(){
var explainer = function explainer() {
this.svgW = 100;
this.svgH = 100;
this.binNum = 0;
this.rows = 30;
this.xpos = 0;
this.ypos = 0;
this.isCustomBinNum = false;
this.pathVisibility = true;
this.histVisibility = true;
this.boxplotVisibility = true;
this.genreBoxplotVisibility = true;
this.filename = "";
this.createSVG = true;
this.itemColName = "";
this.genreColName = "";
this.predicateColName = "";
this.explainerColNames = [];
this.lines = [];
this.isSetLines = false;
this.lineVisibility = true;
}
// helper function to draw lines/paths
var diagonal = d3.svg.diagonal()
.source(function(d) {return {"x":d[0].y, "y":d[0].x}; })
.target(function(d) {return {"x":d[1].y, "y":d[1].x}; })
.projection(function(d) { return [d.y, d.x]; });
// default genre colors
explainer.colors = [
"#8dd3c7",
"#ffffb3",
"#bebada",
"#fb8072",
"#80b1d3",
"#fdb462",
"#b3de69",
"#fccde5",
"#d9d9d9",
"#bc80bd",
"#ccebc5",
"#ffed6f"
];
explainer.prototype.setSVGSize = function(w, h) {
this.svgW = w;
this.svgH = h;
}
explainer.prototype.setItemColName = function(name) {
this.itemColName = name;
}
explainer.prototype.setGenreColName = function(name) {
this.genreColName = name;
}
explainer.prototype.setLines = function(linesArr) {
// expected input:
// if have multiple explainer values
// [["lineA1", "lineA2", "lineA3"], ["lineB1", "lineB2"]]
this.lines = linesArr;
this.isSetLines = true;
}
explainer.prototype.setLinesInvisible = function() {
this.lineVisibility = false;
}
explainer.prototype.setPredicateColName = function(name) {
this.predicateColName = name;
}
explainer.prototype.setExplainerColNames = function(nameArr) {
this.explainerColNames = nameArr;
}
explainer.prototype.setStartingCoord = function(x,y) {
this.xpos = x;
this.ypos = y;
}
explainer.prototype.unsetNewSVG = function() {
this.createSVG = false;
}
explainer.prototype.setMaxRows = function(n) {
this.rows = n;
}
explainer.prototype.setPathInvisible = function() {
this.pathVisibility = false;
}
explainer.prototype.setHistInvisible = function() {
this.histVisibility = false;
}
explainer.prototype.setBoxplotInvisible = function() {
this.boxplotVisibility = false;
}
explainer.prototype.setGenreBoxplotInvisible = function() {
this.genreBoxplotVisibility = false;
}
explainer.prototype.setCSV = function(name) {
this.filename = name;
}
explainer.prototype.setHistBinNum = function(num) {
this.binNum = num;
this.isCustomBinNum = true;
}
// box size for each element
var height = 20;
var width = 20;
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
// helper function to sort the dataset by its explainer value
function compare(a,b) {
var last = a.length - 2;
if (Number(a[last]) > Number(b[last]))
return -1;
if (Number(a[last]) < Number(b[last]))
return 1;
return 0;
}
// helper function to clone obj
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
function stackbox(
svg,
dataset,
x_position,
y_pos,
genreColor,
min,
max,
h1,
h2,
pathVisibility,
maxRows
) {
var last = dataset[0].length - 2;
var lineSet = 0;
var colsInRow = Math.ceil(dataset.length / maxRows);
for(var j = 0; j < dataset.length; j++) {
for(var k = 0; k < colsInRow; k++) {
if (j+k >= dataset.length) {
break;
}
var cname = dataset[j+k][last+1];
svg.append("rect")
.attr("height",height)
.attr("width",width)
.attr("fill",genreColor[dataset[j+k][1]])
.attr("id","sq")
.attr("stroke","black")
.attr("class", cname)
.attr("x",x_position + k*width)
.attr("y",y_pos+Math.floor(j/colsInRow)*height)
.attr("rx",5)
.attr("ry",5)
.on("mouseover", function() {
var c = "." + d3.select(this).attr("class");
var c1 = ".t" + d3.select(this).attr("class");
d3.selectAll(c)
.attr("stroke","red")
.attr("stroke-width","4px");
d3.selectAll(c1)
.moveToFront()
.style("visibility","visible");
})
.on("mouseout", function() {
var c = "." + d3.select(this).attr("class");
var c1 = ".t" + d3.select(this).attr("class");
var idd = "#" + d3.select(this).attr("id");
var col = d3.select(this).attr("fill");
d3.selectAll(c)
.attr("stroke",col)
.attr("stroke-width","1px");
d3.selectAll(c1)
.style("visibility","hidden");
d3.selectAll(idd)
.attr("stroke","black")
.attr("stroke-width","1px");
})
// lines connecting small boxes and histogram
if (pathVisibility) {
var y_new = (Number(dataset[j+k][last]) - min) / h1 * h2;
lineSet = 1;
var curveData = [
{x:x_position+width*colsInRow,
y:y_pos+Math.floor(j/colsInRow)*height+0.5*height
},
{x:x_position+width*colsInRow+100, y:y_pos+y_new}
];
svg.append("path")
.datum(curveData)
.attr("class", cname)
.attr("id",dataset[j+k][1])
.attr("d", diagonal)
.attr("stroke", genreColor[dataset[j+k][1]])
.attr("fill","none")
}
// tooltip to show item name
svg.append("rect")
.attr("height",18)
.attr("width",dataset[j+k][0].length * 8) // should fix this
.attr("fill","#e3e7ed")
.attr("rx",5)
.attr("ry",5)
.attr("class", "t" + cname)
.attr("x",x_position + k*width + 10)
.attr("y",y_pos+Math.floor(j/colsInRow)*height+2 + 5)
.style("opacity", 0.7)
.style("visibility","hidden");
svg.append("text")
.attr("class", "t" + cname)
.attr("x",x_position + k*width + 5 + 10)
.attr("y",y_pos+15+Math.floor(j/colsInRow)*height + 5)
.text(dataset[j+k][0])
.style("font-size","12px")
.style("visibility","hidden");
}
j+= colsInRow-1;
}
return x_position + colsInRow * width + lineSet*100;
}
function histogramplot(
svg,
dataset,
x_position,
y_pos,
genreColor,
maxWidth,
min,
max,
h1,
h2,
isSetBin,
inputBinNum
) {
var box_w = maxWidth / dataset.length;
var bin_num = isSetBin
? inputBinNum
: Math.floor(Math.sqrt(dataset.length-1)-1);
// bottom axis of the histogram
svg.append("line")
.attr("x1",x_position)
.attr("y1",y_pos+0)
.attr("x2",x_position)
.attr("y2",y_pos+h2)
.attr("stroke-width", 1)
.attr("stroke", "black")
// num of bins, might need to change it to user input
var box_h = h2 / bin_num;
var st = clone(genreColor);
for(var prop in st) {
st[prop] = 0;
}
var count = 1;
var last = dataset[0].length - 2;
var binEleNum = 0;
var maxNum = 0;
// max to min
for (var j = dataset.length -1; j >= -1; j--) {
if (
j >= 0 &&
Number(dataset[j][last]) <= max + Math.abs(h1*count / bin_num)
) {
st[dataset[j][1]] += 1;
binEleNum++;
} else {
if (maxNum < binEleNum) {
maxNum = binEleNum;
}
binEleNum = 0;
var u = 0;
var l = 0;
for(var prop in st) {
svg.append("rect")
.attr("x", x_position + u*box_w)
.attr("y", y_pos+(bin_num-(count))*box_h)
.attr("width", st[prop]*box_w)
.attr("height", box_h)
.attr("stroke","black")
.attr("fill", explainer.colors[l]);
u += st[prop];
l++;
}
for(var prop in st) {
st[prop] = 0;
}
if (j == -1) {
break;
}
count++;
j++;
}
}
return x_position + maxNum * box_w + 10;
}
function boxplot(svg, color, info, x_pos, y_pos, h1, h2, max, min) {
svg.append("rect")
.attr("height",Math.abs((info.Q3 - info.Q2)/ h1 * h2))
.attr("width",20)
.style("fill",color)
.attr("stroke","black")
.attr("x",x_pos)
.attr("y",y_pos+h2 - ((info.Q3 - max)/ (-h1) * h2))
svg.append("rect")
.attr("height",Math.abs((info.Q2 - info.Q1)/ h1 * h2))
.attr("width",20)
.style("fill",color)
.attr("stroke","black")
.attr("x",x_pos)
.attr("y",y_pos+h2 - ((info.Q2 - max)/ (-h1) * h2))
svg.append("line")
.attr("x1",x_pos)
.attr("y1",y_pos+h2 - ((info.min_val - max)/ (-h1) * h2))
.attr("x2",x_pos + 20)
.attr("y2",y_pos+h2 - ((info.min_val - max)/ (-h1) * h2))
.attr("stroke-width", 1)
.attr("stroke", "black")
svg.append("line")
.attr("x1",x_pos)
.attr("y1",y_pos+h2-((info.max_val - max)/ (-h1) * h2))
.attr("x2",x_pos+20)
.attr("y2",y_pos+h2-((info.max_val - max)/ (-h1) * h2))
.attr("stroke-width", 1)
.attr("stroke", "black")
svg.append("line")
.attr("x1",x_pos+10)
.attr("y1",y_pos+h2 - ((info.max_val - max)/ (-h1) * h2))
.attr("x2",x_pos+10)
.attr("y2",
y_pos+h2 - ((info.max_val - max)/ (-h1) * h2)
+ Math.abs((info.max_val - info.Q3)/ h1 * h2)
)
.attr("stroke-width", 1)
.attr("stroke", "black")
svg.append("line")
.attr("x1",x_pos+10)
.attr("y1",y_pos+h2 - ((info.Q1 - max)/ (-h1) * h2))
.attr("x2",x_pos+10)
.attr("y2",
y_pos+h2 - ((info.Q1 - max)/ (-h1) * h2)
+ Math.abs((info.Q1 - info.min_val)/ h1 * h2)
)
.attr("stroke-width", 1)
.attr("stroke", "black")
}
function boxdata(
svg,
dataset,
x_pos,
y_pos,
pred,
min,
max,
h1,
h2,
genreColor,
numGenre,
genreBoxplotVisibility
) {
var positive = [];
var negative = [];
var all_val = [];
var genre_val = [];
var last = dataset[0].length -2;
var st = clone(genreColor);
var c = 0;
var colors = [];
for(var prop in st) {
st[prop] = c;
colors.push(genreColor[prop]);
genre_val.push([]);
c++;
}
for (var j = 0;j < dataset.length; j++) {
if (pred > 0) {
if (Number(dataset[j][2]) > 0) {
positive.push(dataset[j][last]);
} else {
negative.push(dataset[j][last]);
}
}
genre_val[st[dataset[j][1]]].push(dataset[j][last]);
all_val.push(dataset[j][last]);
}
// plot box plot by genre
if (genreBoxplotVisibility) {
for (var m = 0; m < numGenre; m++) {
var tmp = genre_val[m].map(Number);
var info = {
max_val: tmp[0],
Q3: tmp[Math.floor(tmp.length * 1 / 4)],
Q2: tmp[Math.floor(tmp.length * 1 / 2)],
Q1: tmp[Math.floor(tmp.length * 3 / 4)],
min_val: tmp[tmp.length -1],
};
boxplot(svg, colors[m], info, x_pos+5, y_pos, h1, h2, max, min);
x_pos += 30;
}
// to separate genres and aggregated boxplot
x_pos += 15;
}
if (pred > 0) {
var p1 = positive.map(Number);
var n1 = negative.map(Number);
// this part is ugly...
var p_info = {
max_val: p1[0],
Q3: p1[Math.floor(p1.length * 1 / 4)],
Q2: p1[Math.floor(p1.length * 1 / 2)],
Q1: p1[Math.floor(p1.length * 3 / 4)],
min_val: p1[p1.length - 1],
};
var t1 = textures.circles()
.size(6)
.radius(2)
.fill("transparent")
.strokeWidth(1);
svg.call(t1);
boxplot(svg, t1.url(), p_info, x_pos, y_pos, h1, h2, max, min);
x_pos += 30;
var n_info = {
max_val: n1[0],
Q3: n1[Math.floor(n1.length * 1 / 4)],
Q2: n1[Math.floor(n1.length * 1 / 2)],
Q1: n1[Math.floor(n1.length * 3 / 4)],
min_val: n1[n1.length -1],
};
var t2 = textures.paths()
.size(15)
.d("crosses")
.lighter()
.thicker();
svg.call(t2);
boxplot(svg, t2.url(), n_info, x_pos, y_pos, h1, h2, max, min);
x_pos += 30;
}
var a1 = all_val.map(Number);
var a_info = {
max_val: a1[0],
Q3: a1[Math.floor(a1.length * 1 / 4)],
Q2: a1[Math.floor(a1.length * 1 / 2)],
Q1: a1[Math.floor(a1.length * 3 / 4)],
min_val: a1[a1.length -1],
};
var t = textures.lines();
svg.call(t);
boxplot(svg, t.url(), a_info, x_pos, y_pos, h1, h2, max, min);
x_pos += 30;
return x_pos;
}
explainer.prototype.draw = function () {
// since we cannot read 'this' inside d3.csv
// TODO: should separate d3.csv and other function
var svg = this.createSVG
? d3.select("body")
.append("svg")
.attr("width", this.svgW)
.attr("height", this.svgH)
: d3.select("svg");
var isBinNumSet = this.isCustomBinNum;
var binNumC = this.binNum;
var histVisibility = this.histVisibility;
var boxplotVisibility = this.boxplotVisibility;
var genreBoxplotVisibility = this.genreBoxplotVisibility;
var pathVisibility = this.pathVisibility;
var maxRows = this.rows;
var x_position = this.xpos;
var y_position = this.ypos;
var exp = this.explainerColNames;
var predicate = this.predicateColName;
var item = this.itemColName;
var genre = this.genreColName;
var pred = predicate !== "" ? 1 : 0;
var lines = this.isSetLines ? this.lines : [];
var lineVisibility = this.lineVisibility;
d3.csv(this.filename, function(data) {
// get column name
var dataset = [];
var genreColor = {};
var dataValues = d3.values(data)[0];
// get genre-color mapping
var k = 0; // number of genre
var genres = data.map(function(d) { return d[genre]; });
for (var j = 0; j < genres.length; j++) {
if (!(genres[j] in genreColor)) {
genreColor[genres[j]] = explainer.colors[k];
k++;
}
}
for (var i = 0; i < exp.length; i++) {
var init_pos = x_position;
if (pred == 1) {
dataset =
data.map(function(d,idx) {
return [d[item],
d[genre],
d[predicate],
d[exp[i]],
"d" + idx];
});
} else {
dataset =
data.map(function(d,idx) {
return [d[item], d[genre], d[exp[i]], "d" + idx];
});
}
var last = dataset[0].length - 2;
dataset.sort(compare);
var max = Number(dataset[dataset.length-1][last]);
var min = Number(dataset[0][last]);
var h1 = max - min;
var data_height =
Math.ceil(
dataset.length / Math.ceil(dataset.length / maxRows)
);
var h2 = data_height < maxRows ?
height * data_height: height * maxRows;
// stack box and lines
x_position = stackbox(
svg, dataset, x_position, y_position,
genreColor, min, max, h1, h2,
pathVisibility, maxRows
);
// histogram
if (histVisibility) {
var maxWidth = 400; // this should be a parameter
var bin_num = isBinNumSet
? binNumC
: Math.floor(Math.sqrt(dataset.length-1)-1);
x_position = histogramplot(
svg, dataset, x_position, y_position,
genreColor, maxWidth, min, max, h1, h2,
isBinNumSet, binNumC
);
}
// box
if (boxplotVisibility) {
x_position = boxdata(
svg, dataset, x_position, y_position,
pred, min, max,
h1, h2, genreColor, k, // number of genre
genreBoxplotVisibility
);
}
if (lineVisibility) {
if (lines.length > 0) {
for (var q = 0; q < lines[i].length; q++) {
svg.append("text")
.attr("x",(init_pos + x_position)/2)
.attr("y",h2 + 20 + q*20)
.text(lines[i][q])
.style("font-size","12px");
}
} else {
svg.append("text")
.attr("x",(init_pos + x_position)/2)
.attr("y",h2 + 20)
.text("Explainer: " + exp[i])
.style("font-size","12px");
}
}
// clean up
dataset = [];
}
});
}
return explainer;
}
//define globally if it doesn't already exist
if(typeof(explainer) === 'undefined'){
window.explainer = define_library();
}
else{
console.log("Library already defined.");
}
})(window);