-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathdataLoading.js
727 lines (585 loc) · 22.9 KB
/
dataLoading.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/**
* author: Alexander Lex - alex@seas.harvard.edu
* author: Nils Gehlenborg - nils@hms.harvard.edu
* author: Hendrik Srtobelt - strobelt@seas.harvard.edu
* author: Romain Vuillemot - romain.vuillemot@gmail.com
*/
var dataSetDescriptions = []
var queryParameters = {};
var initCallback; // function to call when dataset is loaded
var globalCtx;
function initData(ctx, callback, datasets) {
dataSetDescriptions = [];
retrieveQueryParameters();
setUpGUIElements();
initCallback = callback;
globalCtx = ctx;
if (!datasets) {
$.when($.ajax({ url: 'datasets.json', dataType: 'json' })).then(
function (data, textStatus, jqXHR) {
loadDataSetDescriptions(data);
},
function (data, textStatus, jqXHR) {
console.error('Error loading "' + this.url + '".');
});
} else {
loadDataSetDescriptions(datasets);
}
/// registering custom dataset function
$("#custom-dataset-submit").on('click', function () {
var url = $("#custom-dataset-url").val();
if (url != null) {
loadDataSetDescriptions([url], true);
queryParameters['dataset'] = dataSetDescriptions.length;
}
})
}
var handleDatasetDescription = function (result) {
if (result != undefined) {
dataSetDescriptions.push(result);
}
}
var loadDataAfterAjaxComplete = function () {
}
var populateDSSelector = function () {
// updating the drop-down box
d3.select("#header-ds-selector")
.selectAll('option').data(dataSetDescriptions).enter().append('option')
.attr('value', function (d, i) {
return i;
})
.attr('id', 'dataSetSelector')
.text(function (d) {
return d.name + ' ' + '(' + getNumberOfSets(d) + ' sets, ' + getNumberOfAttributes(d) + ' attributes' + ')';
})
.property('selected', function (d, i) {
return (i === queryParameters['dataset'])
});
d3.select("#header-ds-selector").on('change', setQueryParametersAndChangeDataset);
}
function loadDataSetDescriptions(dataSetList) {
var descriptions = [];
// var descriptionDeferreds = [];
var requests = [];
// launch requests to load data set descriptions
for (var i = 0; i < dataSetList.length; ++i) {
var url = dataSetList[i];
if ($.type(url) === 'string') {
requests.push($.ajax({ url: url, dataType: 'json', success: handleDatasetDescription}));
} else {
handleDatasetDescription(url);
}
}
$.when.apply(undefined, requests).then(loadDataSetFromQueryParameters, handleDataSetError);
}
var handleDataSetError = function (jqXHR, textStatus, errorThrown) {
alert("Could not load dataset. \n Error: " + errorThrown)
}
function loadDataSetFromQueryParameters() {
populateDSSelector();
$(EventManager).trigger("loading-dataset-started", { description: dataSetDescriptions[queryParameters['dataset']] });
//(queryParameters['dataset']);
changeDataset();
}
var setQueryParametersAndChangeDataset = function () {
queryParameters['dataset'] = this.options[this.selectedIndex].value;
changeDataset();
}
/**
* Replace or load a new dataset based on the dataset index in the query parameters
*/
var changeDataset = function () {
$(EventManager).trigger("loading-dataset-started", { description: dataSetDescriptions[queryParameters['dataset']] });
sets.length = 0;
subSets.length = 0;
usedSets.length = 0;
dataRows.length = 0;
depth = 0;
allItems.length = 0;
attributes.length = 0;
selectedAttributes = {};
previousState = undefined;
UpSetState.logicGroups = [];
UpSetState.logicGroupChanged = true;
loadDataSet(queryParameters['dataset']);
// updateQueryParameters();
clearSelections();
}
function loadDataSet(index) {
processDataSet(dataSetDescriptions[index]);
}
function processDataSet(dataSetDescription) {
if (dataSetDescription.file) {
d3.text(dataSetDescription.file, 'text/csv', function (data) {
parseDataSet(data, dataSetDescription);
run();
});
} else {
parseDataSet(dataSetDescription.data, dataSetDescription);
run();
}
}
/**
* Setting up the html GUI elements
*/
var setUpGUIElements = function () {
var maxCardSpinner = document.getElementById('maxCardinality');
var minCardSpinner = document.getElementById('minCardinality');
var updateCardinality = function (e) {
UpSetState.maxCardinality = maxCardSpinner.value;
UpSetState.minCardinality = minCardSpinner.value;
UpSetState.forceUpdate = true;
run();
};
maxCardSpinner.addEventListener('input', updateCardinality);
minCardSpinner.addEventListener('input', updateCardinality);
var hideEmptiesCheck = document.getElementById('hideEmpties');
var hideEmptiesFu = function (e) {
UpSetState.hideEmpties = hideEmptiesCheck.checked;
updateState();
// TODO: need to call updateTransition instead, but this needs to be exposed first
plot();
plotSetOverview();
};
hideEmptiesCheck.addEventListener('click', hideEmptiesFu);
var dataSelect = d3.select("#dataset-selector").append('div');
var select = dataSelect.append('select').attr("id", "header-ds-selector");
dataSelect.append('span').attr("class", "header-right").text('Choose Dataset');
}
function retrieveQueryParameters() {
// Variables from query string
var queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
// Creates a map with the query string parameters
while (m = re.exec(queryString)) {
queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
queryParameters['dataset'] = parseInt(queryParameters['dataset']) || 0;
queryParameters['duration'] = queryParameters['duration'] || 1000;
queryParameters['orderBy'] = queryParameters['orderBy'] || "subsetSize"; // deviation, intersection, specific set
queryParameters['grouping'] = queryParameters['grouping'] == "undefined" ? undefined : queryParameters['grouping'] || "groupBySet"; // groupByIntersectionSize,
queryParameters['selection'] = queryParameters['selection'] || "";
// Missing item space query..
}
function updateQueryParameters() {
var urlQueryString = "";
if (Object.keys(queryParameters).length > 0) {
urlQueryString = "?";
for (var q in queryParameters) {
urlQueryString += (q + "=" + queryParameters[q]) + "&";
}
urlQueryString = urlQueryString.substring(0, urlQueryString.length - 1);
}
history.replaceState({}, 'Upset', window.location.origin + window.location.pathname + urlQueryString);
}
function clearSelections() {
selections = new SelectionList();
}
function createInitialSelection() {
var selection = new Selection(allItems, new FilterCollection("#filters-controls", "#filters-list"));
selections.addSelection(selection, true);
selections.setActive(selection);
}
function run() {
elementViewers.reset();
setUpSubSets();
// setUpGroupings();
updateState();
initCallback.forEach(function (callback) {
callback();
})
// plot();
// plotSetSelection();
selections.setActive();
//createInitialSelection();
plotSetOverview({initialize: true});
$(EventManager).trigger("loading-dataset-finished", { });
}
function getNumberOfSets(dataSetDescription) {
var sets = 0;
for (var i = 0; i < dataSetDescription.sets.length; ++i) {
var setDefinitionBlock = dataSetDescription.sets[i];
if (setDefinitionBlock.format === 'binary') {
sets += setDefinitionBlock.end - setDefinitionBlock.start + 1;
}
else {
console.error('Set definition format "' + setDefinitionBlock.format + '" not supported');
}
}
return ( sets );
}
function getNumberOfAttributes(dataSetDescription) {
return ( dataSetDescription.meta.length );
}
function getIdColumn(dataSetDescription) {
for (var i = 0; i < dataSetDescription.meta.length; ++i) {
if (dataSetDescription.meta[i].type === "id") {
return dataSetDescription.meta[i].index;
}
}
// id column not defined, assume 0
return 0;
}
function parseDataSet(data, dataSetDescription) {
// the raw set arrays
var rawSets = [];
var setNames = [];
var file;
if ($.type(data) === 'string') {
var dsv = d3.dsv(dataSetDescription.separator, 'text/plain');
file = dsv.parseRows(data);
} else {
file = data;
}
// the names of the sets are in the columns
var header = file[dataSetDescription.header];
// remove header
file.splice(dataSetDescription.header, 1);
// load set assignments
var processedSetsCount = 0;
for (var i = 0; i < dataSetDescription.sets.length; ++i) {
var setDefinitionBlock = dataSetDescription.sets[i];
if (setDefinitionBlock.format === 'binary') {
var setDefinitionBlockLength = setDefinitionBlock.end - setDefinitionBlock.start + 1;
// initialize the raw set arrays
for (var setCount = 0; setCount < setDefinitionBlockLength; ++setCount) {
rawSets.push(new Array());
}
var rows = file.map(function (row, rowIndex) {
return row.map(function (value, columnIndex) {
if (columnIndex >= setDefinitionBlock.start && columnIndex <= setDefinitionBlock.end) {
var intValue = parseInt(value, 10);
if (isNaN(intValue)) {
console.error('Unable to convert "' + value + '" to integer (row ' + rowIndex + ', column ' + columnIndex + ')');
}
return intValue;
}
return null;
});
});
// iterate over columns defined by this set definition block
for (var r = 0; r < rows.length; r++) {
// increment number of items in data set
// only increment depth when we are processing the first set definition block (we will already iterate overall rows)
if (i === 0) {
allItems.push(depth++);
}
for (var s = 0; s < setDefinitionBlockLength; ++s) {
rawSets[processedSetsCount + s].push(rows[r][setDefinitionBlock.start + s]);
if (r === 1) {
setNames.push(header[setDefinitionBlock.start + s]);
}
}
}
processedSetsCount += setDefinitionBlockLength;
}
else {
console.error('Set definition format "' + setDefinitionBlock.format + '" not supported');
}
}
// initialize sets and set IDs
var setPrefix = "S_";
//var setID = 1;
for (var i = 0; i < rawSets.length; i++) {
var combinedSets = Array.apply(null, new Array(rawSets.length)).map(Number.prototype.valueOf, 0);
combinedSets[i] = 1;
var set = new USet(setPrefix + i, setNames[i], combinedSets, rawSets[i]);
setIdToSet[setPrefix + i] = set;
sets.push(set);
if (i < nrDefaultSets) {
set.isSelected = true;
usedSets.push(set);
}
// setID = setID << 1;
}
// initialize attribute data structure
attributes.length = 0;
for (var i = 0; i < dataSetDescription.meta.length; ++i) {
var metaDefinition = dataSetDescription.meta[i];
attributes.push({
name: metaDefinition.name || header[metaDefinition.index],
type: metaDefinition.type,
values: [],
sort: 1
});
}
// add implicit attributes
var setCountAttribute = {
name: 'Set Count',
type: 'integer',
values: [],
sort: 1,
min: 0
};
for (var d = 0; d < depth; ++d) {
var setCount = 0;
for (var s = 0; s < rawSets.length; s++) {
setCount += rawSets[s][d];
}
setCountAttribute.values[d] = setCount;
}
attributes.push(setCountAttribute);
var setsAttribute = {
name: 'Sets',
type: 'sets',
values: [],
sort: 1
};
for (var d = 0; d < depth; ++d) {
var setList = [];
for (var s = 0; s < rawSets.length; s++) {
if (rawSets[s][d] === 1) {
//setList.push(Math.floor(Math.pow(2, s)));
setList.push(sets[s].id)
}
}
setsAttribute.values[d] = setList;
}
attributes.push(setsAttribute);
// load meta data
for (var i = 0; i < dataSetDescription.meta.length; ++i) {
var metaDefinition = dataSetDescription.meta[i];
attributes[i].values = file.map(function (row, rowIndex) {
var value = row[metaDefinition.index];
switch (metaDefinition.type) {
case 'integer':
var intValue = parseInt(value, 10);
if (isNaN(intValue)) {
console.error('Unable to convert "' + value + '" to integer.');
return NaN;
}
return intValue;
case 'float':
var floatValue = parseFloat(value, 10);
if (isNaN(floatValue)) {
console.error('Unable to convert "' + value + '" to float.');
return NaN;
}
return floatValue;
case 'id':
// fall-through
case 'string':
// fall-through
default:
return value;
}
});
}
var max
// add meta data summary statistics
for (var i = 0; i < attributes.length; ++i) {
if (attributes[i].type === "float" || attributes[i].type === "integer") {
// explictly defined attributes might have user-defined ranges
if (i < dataSetDescription.meta.length) {
attributes[i].min = dataSetDescription.meta[i].min || Math.min.apply(null, attributes[i].values);
attributes[i].max = dataSetDescription.meta[i].max || Math.max.apply(null, attributes[i].values);
}
// implicitly defined attributes
else {
attributes[i].min = attributes[i].min || Math.min.apply(null, attributes[i].values);
attributes[i].max = attributes[i].max || Math.max.apply(null, attributes[i].values);
}
}
}
UpSetState.maxCardinality = attributes[attributes.length - 2].max;
if (isNaN(UpSetState.maxCardinality)) {
// fixme hack to make it work without attributes
UpSetState.maxCardinality = sets.length;
}
var maxCardSpinner = document.getElementById('maxCardinality');
maxCardSpinner.value = UpSetState.maxCardinality;
maxCardSpinner.max = UpSetState.maxCardinality;
var minCardSpinner = document.getElementById('minCardinality');
minCardSpinner.max = UpSetState.maxCardinality;
updateDatasetInformation(dataSetDescription)
}
var updateDatasetInformation = function (dataSetDescription) {
var infoBox = $('#dataset-info-content');
infoBox.empty();
//infoBox.append('<hr><br />');
infoBox.append('<p style="padding-bottom: 5px">');
infoBox.append("<b>Name:</b> " + dataSetDescription.name + "<br />");
infoBox.append("<b># Sets:</b> " + sets.length + "<br />");
infoBox.append("<b># Attributes</b>: " + attributes.length + "<br />");
infoBox.append("<b># Elements:</b> " + depth + "<br />");
infoBox.append('</p> <p style="padding-bottom: 10px">');
if (dataSetDescription.author) {
infoBox.append("<b>Author</b>: " + dataSetDescription.author + "<br />");
}
if (dataSetDescription.description) {
infoBox.append("<b>Description:</b> <br />" + dataSetDescription.description + "<br />");
}
if (dataSetDescription.source) {
if (dataSetDescription.source.indexOf("http://") == 0) {
var urlText = dataSetDescription.source;
var numCharacters = 22;
if (urlText.length > numCharacters) {
urlText = urlText.substring(0, numCharacters) + ".."
}
infoBox.append("<b>Source:</b> <br /><a href=\"" + dataSetDescription.source + "\">" + urlText + "</a><br />");
} else {
infoBox.append("<b>Source:</b> <br />" + dataSetDescription.source + "<br />");
}
}
infoBox.append('</p>');
}
function createSignature(listOfUsedSets, listOfSets) {
return listOfUsedSets.map(function (d) {
return (listOfSets.indexOf(d) > -1) ? 1 : 0
}).join("")
}
function setUpSubSets() {
$(EventManager).trigger("computing-subsets-started", undefined);
combinations = Math.pow(2, usedSets.length) - 1;
subSets.length = 0;
var aggregateIntersection = {}
var listOfUsedSets = usedSets.map(function (d) {
return d.id
})
var setsAttribute = attributes.filter(function (d) {
return d.type == "sets"
})[0];
var signature = "";
var itemList;
//HEAVY !!!
setsAttribute.values.forEach(function (listOfSets, index) {
signature = createSignature(listOfUsedSets, listOfSets)
itemList = aggregateIntersection[signature];
if (itemList == null) {
aggregateIntersection[signature] = [index];
} else {
itemList.push(index);
}
})
// used Variables for iterations
var tempBitMask = 0;
var usedSetLength = usedSets.length
var combinedSetsFlat = "";
var actualBit = -1;
var names = [];
if (usedSetLength > 20) { // TODo HACK !!!!
Object.keys(aggregateIntersection).forEach(function (key) {
var list = aggregateIntersection[key]
var combinedSets = key.split("");
//combinedSetsFlat = combinedSets.join("");
// if (card>UpSetState.maxCardinality) continue;//UpSetState.maxCardinality = card;
// if (card<UpSetState.minCardinality) continue;//UpSetState.minCardinality = card;
names = [];
var expectedValue = 1;
var notExpectedValue = 1;
// go over the sets
combinedSets.forEach(function (d, i) {
if (d == 1) { // if set is present
names.push(usedSets[i].elementName);
expectedValue = expectedValue * usedSets[i].dataRatio;
} else {
notExpectedValue = notExpectedValue * (1 - usedSets[i].dataRatio);
}
}
);
// console.log(expectedValue, notExpectedValue);
expectedValue *= notExpectedValue;
// console.log(combinedSetsFlat);
var name = "";
if (names.length > 0) {
name = names.reverse().join(" ") + " " // not very clever
}
// var arghhList = Array.apply(null,new Array(setsAttribute.values.length)).map(function(){return 0})
// list.forEach(function(d){arghhList[d]=1});
// console.log(parseInt(key,2), name, combinedSets, list, expectedValue);
var subSet = new SubSet(bitMask, name, combinedSets, list, expectedValue);
subSets.push(subSet);
})
} else {
for (var bitMask = 0; bitMask <= combinations; bitMask++) {
tempBitMask = bitMask;//originalSetMask
var card = 0;
var combinedSets = Array.apply(null, new Array(usedSetLength)).map(function () { //combinedSets
actualBit = tempBitMask % 2;
tempBitMask = (tempBitMask - actualBit) / 2;
card += actualBit;
return +actualBit
}).reverse() // reverse not necessary.. just to keep order
combinedSetsFlat = combinedSets.join("");
if (card > UpSetState.maxCardinality) continue;//UpSetState.maxCardinality = card;
if (card < UpSetState.minCardinality) continue;//UpSetState.minCardinality = card;
names = [];
var expectedValue = 1;
var notExpectedValue = 1;
// go over the sets
combinedSets.forEach(function (d, i) {
// console.log(usedSets[i]);
if (d == 1) { // if set is present
names.push(usedSets[i].elementName);
// expectedValue*=expectedValueForOneSet;
expectedValue = expectedValue * usedSets[i].dataRatio;
} else {
notExpectedValue = notExpectedValue * (1 - usedSets[i].dataRatio);
}
}
);
// console.log(expectedValue, notExpectedValue);
expectedValue *= notExpectedValue;
// console.log(combinedSetsFlat);
var list = aggregateIntersection[combinedSetsFlat];
if (list == null) {
list = [];
}
var name = "";
if (names.length > 0) {
name = names.reverse().join(" ") + " " // not very clever
}
// var arghhList = Array.apply(null,new Array(setsAttribute.values.length)).map(function(){return 0})
// list.forEach(function(d){arghhList[d]=1});
var subSet = new SubSet(bitMask, name, combinedSets, list, expectedValue);
subSets.push(subSet);
}
}
aggregateIntersection = {};
// var subSet = new SubSet(originalSetMask, name, combinedSets, combinedData, expectedValue);
// subSets.push(subSet);
//
// for (var i = 0; i <= combinations; i++) {
// makeSubSet(i)
// }
$(EventManager).trigger("computing-subsets-finished", undefined);
}
function updateSetContainment(set, refresh) {
if (!set.isSelected) {
set.isSelected = true;
usedSets.push(set);
$(EventManager).trigger("set-added", { set: set });
}
else {
set.isSelected = false;
var index = usedSets.indexOf(set);
if (index > -1) {
usedSets.splice(index, 1);
$(EventManager).trigger("set-removed", { set: set });
}
}
if (refresh) {
subSets.length = 0;
dataRows.length = 0;
setUpSubSets();
// setUpGroupings();
previousState = undefined;
updateState();
// ctx.updateHeaders();
//
// plot();
// plotSetSelection();
plotSetOverview();
initCallback.forEach(function (callback) {
callback();
})
// ctx.svg.attr("width", ctx.w)
// d3.selectAll(".svgGRows, .foreignGRows").attr("width", ctx.w)
// d3.selectAll(".backgroundRect").attr("width", ctx.w - ctx.leftOffset)
}
}
function addSet(set) {
}
function removeSet(set) {
console.log('Not implemented');
}