-
Notifications
You must be signed in to change notification settings - Fork 1
/
lfviz.html
1377 lines (1099 loc) · 41.4 KB
/
lfviz.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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Labeling Function Viz</title>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: Open sans, Arial, sans-serif;
font-size: 10pt;
font-weight: 400;
color: #606060;
background: #ffffff;
}
body a {
color: #808080;
}
body a:hover {
text-decoration: underline;
}
body h3 {
color: #000000;
}
button {
background-color: #a2a2a2;
border: none;
color: white;
padding: 2px 6px;
border-radius: 3px;
/*font-family: Open sans, Arial, sans-serif; */
text-align: center;
display: inline-block;
}
button:hover {
background-color: #808080;
}
.bold_text {
font-weight: bold;
}
.image_thumb {
border: 1px solid #808080;
margin: 1px;
}
.text_main_preview {
width: 250px;
height: 200px;
border: 1px solid #808080;
border-bottom: 5px solid #808080;
background-color:#e0e0e0;
padding: 4px;
overflow: scroll;
}
.text_similarity_preview {
border: 1px solid #808080;
background-color:#e0e0e0;
padding: 4px;
margin-top: 10px;
}
.datapoint_positive {
border-bottom: 5px solid #67bf5c;
}
.datapoint_negative {
border-bottom: 5px solid #ed665d;
}
.datapoint_unknown {
border-bottom: 5px solid #808080;
}
</style>
<script src="js/kmath.js"></script>
<script src="js/widgets/lfviz.js"></script>
<script src="js/widgets/anim_thumb.js"></script>
<script>
// FIXME(kayvonf): hacky enum.
// Need to figure out what is the right way to do this in Javascript
var LF_OUTPUT_NEGATIVE = -1;
var LF_OUTPUT_ABSTAIN = 0;
var LF_OUTPUT_POSITIVE = 1;
var LF_OUTPUT_VOTES = 2;
var LF_OUTPUT_AGREES = 3;
var LF_OUTPUT_DISAGREES = 4;
var LF_OUTPUT_EXISTS = 5;
var LF_OUTPUT_CORRECT = 6;
var LF_OUTPUT_INCORRECT = 7;
var LF_OUTPUT_UNKNOWN = 8;
var DATAPOINT_TYPE_UNKNOWN = 0;
var DATAPOINT_TYPE_TEXT = 1;
var DATAPOINT_TYPE_IMAGE_URL = 2;
var DATAPOINT_TYPE_IMAGE_URL_SEQ = 3;
// append part2 to part1 to create a path (similar to os.path.join() in Python)
function join_path(part1, part2) {
var prefix = part1;
if (prefix.charAt(prefix.length-1))
prefix = prefix + "/";
return prefix + part2;
}
// extract the directory part of a url
function get_url_dir(dump_url) {
var idx = dump_url.lastIndexOf('/');
if (idx == -1)
return "";
else
return dump_url.substr(0, idx+1);
}
// return true is a url string is an absolute url, false otherwise
function is_absolute_url(url) {
return (url.indexOf("http://") == 0 ||
url.indexOf("https://") == 0 ||
url.indexOf("/") == 0);
}
class FilterDef {
constructor(filter, options) {
this.filter = filter;
this.options = options;
}
}
///////////////////////////////////////////////////////////
// input data (from the debug dump)
///////////////////////////////////////////////////////////
var dump_name;
var dump_base_dir;
var num_train = 0;
var num_val = 0;
var num_lf = 0;
var has_extended_data;
var has_similarity_data;
// human-readable names for all the labeling functions
var lf_names;
// the source data type: text strings (if text), or an image url
var datapoint_type;
// information about each datapoint (used for previewing it)
var datapoints;
// table of labeling function outputs (pre and post LF extension)
var lf_matrix_noext;
var lf_matrix_ext;
// label model output (pre and post LF extension)
var prob_labels_noext;
var prob_labels_ext;
// ground truth labels
var ground_truth_labels;
// for each datapoint, ranking of all other datapoints in terms of closeness in feature space
// (in other words, the sorted distance matrix)
var similarity_rankings;
///////////////////////////////////////////////////////////
// viz application state
///////////////////////////////////////////////////////////
var LFVIZ_CONFIG_FILE_DIR = 'lfviz_config';
var lfviz_widget = new LFViz;
// index of datapoint in preview pane
var cur_preview_idx;
// index of datapoint whose similarity results are shown
var cur_similarity_idx;
// data structure managing the state of the possible data_filters
var available_filters;
// whether or not to show the results from the "extended" labeling functions
var display_ext_data;
// This mask denoting the rows that should be rendered in the visualizer
var row_filter_mask;
// the number of 1's in the row_filter_mask
var num_datapoints_pass_filter;
// sorting of the rows in the visualizer
var row_sorting;
// state of the async loads
var pending_data_load_events;
// key DOM elements
var loading_div; // where to render datapoint previews
var main_viz_div; // main div for all the UI elements
var main_preview_div; // div containing the main preview
var thumbnail_list_div; // div containing all the thumbnails
// handles thumbnail animation
var main_preview_image_widget = new AnimatedThumbnailWidget;
var thumbnail_list_widget = new AnimatedThumbnailWidget;
// hide any indication of ground truth data in the UI, even if it exists in the dataset
var hide_ground_truth = false;
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
// Entrypoint that initiates logic for loading data from the server via a sequence of async requests
//
// First, the app loads the dataset "info file", which contains a bunch of metadata about the dataset.
// After the info file has loaded, then the app fires off a collection of various async requests
// (via Javascript's Fetch API) to get all the associated data files.
//
// See handle_data_load_complete() for the logic that occurs when all data has loaded.
function load_data(dump_url) {
console.log("Loading weakdb data dump: " + dump_url);
fetch(dump_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
handle_info_file_loaded(data);
// determine how many async file loads are necessary. This depends on what data is available.
pending_data_load_events = 7;
if (!has_extended_data)
pending_data_load_events-=2;
if (!has_similarity_data)
pending_data_load_events--;
// FIXME(kayvonf): this is hacky. If the ground truth label array has already been filled in,
// they are filled in with "0's" corresponding to "unknown". That means that ground truth labels
// are are not in the dataset and need not be loaded.
var do_load_ground_truth = true;
if (ground_truth_labels.length == (num_train + num_val)) {
do_load_ground_truth = false;
pending_data_load_events--;
}
// now kick off all the individual data file loads
var prefix = join_path(dump_base_dir, dump_name);
var lf_matrix_noext_url = prefix + '_lfmatrix_noext.json';
var prob_labels_noext_url = prefix + '_prob_labels_noext.json';
var lf_matrix_ext_url = prefix + '_lfmatrix_ext.json';
var prob_labels_ext_url = prefix + '_prob_labels_ext.json';
var datapoints_url = prefix + '_datapoints.json';
var ground_truth_labels_url = prefix + '_ground_truth_labels.json';
var dist_ranking_url = prefix + '_sorted_dists.json';
fetch(lf_matrix_noext_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
lf_matrix_noext = data;
handle_data_load_event();
})
.catch(function(err) {
console.log('Error fetching LF matrix (noext) data: ', err);
});
fetch(prob_labels_noext_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
prob_labels_noext = data;
handle_data_load_event();
})
.catch(function(err) {
console.log('Error fetching LM (noext) output: ', err);
});
fetch(datapoints_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
datapoints = data;
handle_data_load_event();
})
.catch(function(err) {
console.log('Error fetching datapoints: ', err);
});
if (has_extended_data) {
fetch(lf_matrix_ext_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
lf_matrix_ext = data;
handle_data_load_event();
})
.catch(function(err) {
console.log('Error fetching LF matrix (ext) data: ', err);
});
fetch(prob_labels_ext_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
prob_labels_ext = data;
handle_data_load_event();
})
.catch(function(err) {
console.log('Error fetching LM (ext) output: ', err);
});
}
if (do_load_ground_truth) {
fetch(ground_truth_labels_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
ground_truth_labels = data;
handle_data_load_event();
})
.catch(function(err) {
console.log('Error fetching ground truth data: ', err);
});
}
if (has_similarity_data) {
fetch(dist_ranking_url)
.then(function(response) {
if (response.status !== 200) {
console.log("problem: " + response.status);
}
return response.json();
})
.then(function(data) {
similarity_rankings = data;
handle_data_load_event();
})
.catch(function(err) {
console.log('Error fetching ground truth data: ', err);
});
}
})
.catch(function(err) {
console.log('Error fetching dataset info file: ', err);
});
}
// called when the dataset info file has been downloaded
function handle_info_file_loaded(info) {
dump_name = info.name;
num_lf = info.num_lf;
num_train = info.num_train;
num_val = info.num_val;
dataset_description = info.description;
lf_names = info.lf_names;
if (info.datatype == "text")
datapoint_type = DATAPOINT_TYPE_TEXT;
else if (info.datatype == "image_url")
datapoint_type = DATAPOINT_TYPE_IMAGE_URL;
else if (info.datatype == "image_url_seq")
datapoint_type = DATAPOINT_TYPE_IMAGE_URL_SEQ;
else
datapoint_type = DATAPOINT_TYPE_UNKNOWN;
has_similarity_data = info.has_similarity_data;
has_extended_data = info.has_extended_data;
lf_matrix_noext = [];
prob_labels_noext = [];
lf_matrix_ext = [];
prob_labels_ext = [];
ground_truth_labels = [];
datapoints = [];
// if no ground truth data, fill in all 0's ("unknown")
if (!info.has_ground_truth) {
var num_total = num_train + num_val;
for (var i=0; i<num_total; i++) {
ground_truth_labels[i] = 0;
}
}
}
// called upon completion of a dataset data file download
function handle_data_load_event() {
pending_data_load_events--;
// all data is loaded, we can now take action to load the viz widget
if (pending_data_load_events == 0)
handle_data_load_complete();
}
// called when all dataset data has been loaded
// See load_data() for the logic used to initiate all the data load requests
function handle_data_load_complete() {
var num_total = num_train + num_val;
// if the type of the data is an image url, need to patch up the urls to make
// them relative to dump_base_dir (unless URL is absolute)
if (datapoint_type == DATAPOINT_TYPE_IMAGE_URL) {
for (var i=0; i<num_total; i++)
if (!is_absolute_url(datapoints[i]))
datapoints[i] = join_path(dump_base_dir, datapoints[i]);
} else if (datapoint_type == DATAPOINT_TYPE_IMAGE_URL_SEQ) {
for (var i=0; i<num_total; i++)
for (var j=0; j<datapoints[i].length; j++)
if (!is_absolute_url(datapoints[i][j]))
datapoints[i][j] = join_path(dump_base_dir, datapoints[i][j]);
}
// run a bunch of checks on the integrity of the data
if (!check_data_sanity())
alert("Data in dump file did not pass sanity check.");
console.log("Data load complete (train size=" + num_train + ", val size=" + num_val + ", num LFs=" + num_lf + ")");
// update the filter selection dropdown box to include options for the various labeling functions
setup_filters();
// update the DOM to display stats about the dataset
var data_info_el = document.getElementById('data_info');
var contents = "<p>";
contents += "<div>Description: " + dataset_description + "</div>";
contents += "<div>Num datapoints: " + (num_train + num_val) + "</div>";
contents += "<div>Num train: " + num_train + "</div>";
contents += "<div>Num val: " + num_val + "</div>";
contents += "<div>Num LFs: " + num_lf + "</div>";
contents += "<div>LF Names: ";
for (var i=0; i<num_lf; i++) {
contents += lf_names[i];
if (i < num_lf-1)
contents += ", "
}
contents += "</p>"
data_info_el.innerHTML = contents;
display_ext_data = false;
if (has_extended_data) {
var button = document.getElementById("toggle_extended_button");
button.style.visibility = "visible";
}
cur_preview_idx = -1;
cur_similarity_idx = -1;
loading_div.style.visibility = 'hidden';
main_viz_div.style.visibility = 'visible';
// communicate data to the viz widget
var lf_matrix = display_ext_data ? lf_matrix_ext : lf_matrix_noext;
var prob_labels = display_ext_data ? prob_labels_ext : prob_labels_noext;
lfviz_widget.set_data(num_total, num_lf, lf_matrix, prob_labels, handle_show_main_preview);
// data has been reset. Clear any filters, and recompute sort
// FIXME(kayvonf): code duplication with contents of handle_filter_change()
row_filter_mask = [];
for (var i=0; i<num_total; i++)
row_filter_mask[i] = true;
num_datapoints_pass_filter = num_train + num_val;
lfviz_widget.set_row_filter_mask(row_filter_mask);
handle_sort_change();
clear_datapoints_viz_list();
}
// Check consistency of dataset. Verifies that the size of downloaded
// data files matches what is reported in the metadata files of the
// dataset info file.
function check_data_sanity() {
var num_total = num_train + num_val;
var check_passed = true;
if (lf_names.length != num_lf) {
console.log("WARNING: Unexpected number of labeling function names. Expected " + num_lf + ", data has " + lf_names.length);
check_passed = false;
} else if (datapoints.length != num_total) {
console.log("WARNING: Unexpected number of datapoint values. Expected " + num_total + ", data has " + datapoints.length);
check_passed = false;
} else if (datapoint_type == DATAPOINT_TYPE_UNKNOWN) {
console.log("WARNING: Unknown datapoint type.");
check_passed = false;
} else if (ground_truth_labels.length != num_total) {
console.log("WARNING: Unexpected number of ground truth labels. Expected " + num_total + ", data has " + ground_truth_labels.length);
check_passed = false;
} else if (lf_matrix_noext.length != num_total * num_lf) {
console.log("WARNING: Unexpected number of lf matrix values (noext). Expected " + (num_total * num_lf) + ", data has " + lf_matrix_noext.length);
check_passed = false;
} else if (has_extended_data && lf_matrix_ext.length != num_total * num_lf) {
console.log("WARNING: Unexpected number of lf matrix values (ext). Expected " + (num_total * num_lf) + ", data has " + lf_matrix_ext.length);
check_passed = false;
} else if (has_extended_data && prob_labels_ext.length != num_total) {
console.log("WARNING: Unexpected number of label model values (ext). Expected " + num_total + ", data has " + label_model_ext.length);
check_passed = false;
} else if (prob_labels_noext.length != num_total) {
console.log("WARNING: Unexpected number of label model values (noext). Expected " + num_total + ", data has " + label_model_noext.length);
check_passed = false;
} else if (has_similarity_data && similarity_rankings.length != num_total) {
console.log("WARNING: Unexpected number of ranking lists. Expected " + num_total + ", data has " + similarity_rankings.length);
check_passed = false;
}
return check_passed;
}
function handle_select_datapoint(datapoint_idx) {
lfviz_widget.set_selection(datapoint_idx);
}
function handle_select_datapoint_from_input_box() {
var input_el = document.getElementById("select_input_box");
var value = parseInt(input_el.value);
if (!isNaN(value))
lfviz_widget.set_selection(value);
else
input_el.value = '';
}
function handle_thumbnail_mouseover(datapoint_idx) {
var img_el = document.getElementById("thumbnail_image_" + datapoint_idx);
thumbnail_list_widget.attach(img_el, datapoint_idx, datapoints[datapoint_idx], false);
}
function handle_thumbnail_mouseout(datapoint_idx) {
thumbnail_list_widget.detach();
}
function handle_datapoint_annotation(datapoint_idx) {
console.log('key pressed!');
}
function create_list_text(items) {
var num_items = items.length;
var str = "<div>";
for (var i=0; i<num_items; i++) {
var neighbor_idx = items[i];
var style_override = "";
if (!hide_ground_truth) {
if (ground_truth_labels[neighbor_idx] == 1)
style_override = "datapoint_positive";
else if (ground_truth_labels[neighbor_idx] == -1)
style_override = "datapoint_negative";
}
str += "<div class=\"text_similarity_preview " + style_override + "\">";
str += "<div>";
str += datapoints[neighbor_idx];
str += "</div>";
str += "<button onclick=\"handle_select_datapoint(" + neighbor_idx + ")\">Select</button>";
str += "</div>"
}
str += "</div>";
return str;
}
function create_list_images(items) {
num_items = items.length;
var str = "<div>";
for (var i=0; i<num_items; i++) {
var neighbor_idx = items[i];
var img_url;
var img_events_str;
// FIXME(kayvonf): I currently hardcoded index to '1' for IMAGE_URL_SEQ case
if (datapoint_type == DATAPOINT_TYPE_IMAGE_URL) {
img_url = datapoints[neighbor_idx];
img_events_str = "";
} else {
img_url = datapoints[neighbor_idx][1];
img_events_str = "onmouseover=\"handle_thumbnail_mouseover(" + neighbor_idx + ")\" onmouseout=\"handle_thumbnail_mouseout(" + neighbor_idx + ")\"";
}
var style_override = "datapoint_unknown";
if (!hide_ground_truth) {
if (ground_truth_labels[neighbor_idx] == 1)
style_override = "datapoint_positive";
else if (ground_truth_labels[neighbor_idx] == -1)
style_override = "datapoint_negative";
}
str += "<a href=\"#\" onclick=\"handle_select_datapoint(" + neighbor_idx + ")\">";
str += "<img id=\"thumbnail_image_" + neighbor_idx + "\" class=\"image_thumb " + style_override + "\" " + img_events_str + " src=\"" + img_url + "\" width=\"100\" height=\"100\" /></a>";
}
str += "</div>";
return str;
}
function handle_show_similar_datapoints(idx) {
cur_similarity_idx = idx;
if (cur_similarity_idx == -1) {
thumbnail_list_div.innerHTML = "";
} else {
var num_thumbs = Math.min(250, similarity_rankings[idx].length);
var item_list = [];
for (var i=0; i<num_thumbs; i++) {
item_list.push(similarity_rankings[idx][i]);
}
var str = "";
if (datapoint_type == DATAPOINT_TYPE_TEXT) {
str += "<h3>Top " + num_thumbs + " most similar items to datapoint " + idx + "</h3>";
str += create_list_text(item_list);
}
else {
str += "<h3>Top " + num_thumbs + " most similar images to datapoint " + idx + "</h3>";
str += create_list_images(item_list);
}
thumbnail_list_div.innerHTML = str;
}
}
function clear_datapoints_viz_list() {
// Don't clear list if it's a similar datapoints list. We only want to clear if the list is
// showing a visualization of all data in the visualizer matrix that passed the current filter
//FIXME(kayvonf): very hacky to look for there NOT being a similarity list
if (cur_similarity_idx == -1) {
thumbnail_list_div.innerHTML = "";
}
}
function handle_show_datapoints_viz_list() {
cur_similarity_idx = -1;
var datapoint_list = [];
var num_total = num_train + num_val;
for (var i=0; i<num_total; i++) {
var idx = row_sorting[i];
if (row_filter_mask[idx] == true)
datapoint_list.push(idx);
}
var str = "<h3>" + datapoint_list.length + " datapoints pass the current filter (displayed in sorted order)</h3>";
if (datapoint_type == DATAPOINT_TYPE_TEXT) {
str += create_list_text(datapoint_list);
} else {
str += create_list_images(datapoint_list);
}
thumbnail_list_div.innerHTML = str;
}
function handle_show_main_preview(idx) {
cur_preview_idx = idx;
if (idx == -1) {
main_preview_image_widget.detach();
main_preview_div.innerHTML = "";
} else {
if (datapoint_type == DATAPOINT_TYPE_IMAGE_URL_SEQ)
main_preview_image_widget.detach();
var num_total = num_train + num_val;
var lf_matrix = display_ext_data ? lf_matrix_ext : lf_matrix_noext;
var prob_labels = display_ext_data ? prob_labels_ext : prob_labels_noext;
var style_override = "datapoint_unknown";
if (!hide_ground_truth) {
if (ground_truth_labels[idx] == 1)
style_override = "datapoint_positive";
else if (ground_truth_labels[idx] == -1)
style_override = "datapoint_negative";
}
var str = "<p>Datapoint: " + idx + " of " + num_total + "<p/>";
if (datapoint_type == DATAPOINT_TYPE_TEXT) {
str += "<p><div class=\"text_main_preview " + style_override + "\">" + datapoints[idx] + "</div></p>";
} else if (datapoint_type == DATAPOINT_TYPE_IMAGE_URL || datapoint_type == DATAPOINT_TYPE_IMAGE_URL_SEQ) {
// FIXME(kayvonf): I currently hardcoded index for IMAGE_URL_SEQ case
var img_url = (datapoint_type == DATAPOINT_TYPE_IMAGE_URL_SEQ) ? datapoints[idx][1] : datapoints[idx];
str += "<p><img id=\"main_preview_image\" class=\"image_thumb " + style_override + "\" src=\"" + img_url + "\" width=\"" +
main_preview_div.clientWidth + "\" height=\"" + main_preview_div.clientWidth + "\" /></p>";
}
str += "<p>";
var value_str;
if (hide_ground_truth)
value_str = "unknown";
else {
if (ground_truth_labels[idx] == 1)
value_str = "true";
else if (ground_truth_labels[idx] == -1)
value_str = "false";
else
value_str = "unknown";
}
str += "<div>Ground truth: " + value_str + "</div>";
str += "<div>LM score: "+ prob_labels[idx].toPrecision(4) + "</div>"
str += "<div>LF votes: ";
var base = num_lf*idx;
for (var i=0;i<num_lf; i++) {
str += lf_matrix[base + i];
if (i < num_lf-1)
str += ", ";
}
str += "</div>";
str += "</p>";
// FIXME(kayvonf): terrible hack. Hardcoding this event handler for now
if (has_similarity_data)
str += "<p><a href=\"#\" onclick=\"handle_show_similar_datapoints(" + idx + ")\">View Similar Datapoints</p>"
main_preview_div.innerHTML = str;
if (datapoint_type == DATAPOINT_TYPE_IMAGE_URL_SEQ)
main_preview_image_widget.attach(document.getElementById("main_preview_image"), idx, datapoints[idx], true);
}
}
function setup_filters() {
// these are the options for any individual labeling function
var individual_lf_options = ["positive", "negative", "abstain", "votes", "votes+disagrees", "correct", "incorrect"];
available_filters = [];
available_filters.push( new FilterDef("[no filter]", []) );
available_filters.push( new FilterDef("All LFs", ["positive", "negative", "abstain", "votes"]) );
available_filters.push( new FilterDef("All voting LFs", ["positive", "negative", "agree"]) );
available_filters.push( new FilterDef("Any LF", ["positive", "negative", "abstain", "votes", "votes+disagrees", "correct", "incorrect"]) );
available_filters.push( new FilterDef("Label model", ["positive", "negative", "correct", "incorrect"]) );
available_filters.push( new FilterDef("Ground truth", ["positive", "negative", "exists"]) );
for (var i=0; i<num_lf; i++) {
available_filters.push( new FilterDef("LF: " + lf_names[i], individual_lf_options) );
}
// update the filter selection dropdown box with all the available filters
var select_box = document.getElementById("select_lf");
for (var i=0; i<available_filters.length; i++) {
var option = document.createElement("option");
option.text = available_filters[i].filter;
option.value = available_filters[i].filter;
select_box.appendChild(option);
}
}
// "part 1" is the select of the filter type. Note that most filters also need the user to
// select value along with this type in order to fully specify a filter. So this function
// only serves to modify the options in the filter options select box to be consistent with
// the filter chosen type.
function handle_filter_part1_change() {
var select_box = document.getElementById("select_lf");
var select_value_box = document.getElementById("select_lf_status");
var cur_option_val = select_value_box.value;
for (var i=0; i<available_filters.length; i++) {
if (available_filters[i].filter == select_box.value) {
// clear out the old
select_value_box.innerHTML = "";
for (var j=0; j<available_filters[i].options.length; j++) {
var option = document.createElement("option");
option.text = available_filters[i].options[j];
option.value = available_filters[i].options[j];
select_value_box.appendChild(option);
// for user convenince, if the option for the only filter is still valid for the new filter, keep it!
if (option.value == cur_option_val) {
option.selected = true;
}
}
handle_filter_change();
break;
}
}
}
// Filters rows according to a predicate and stores result in row_filter_mask
function handle_filter_change() {
var lf_select = 0;
var lf_value = 1;
var select_lf_box = document.getElementById("select_lf");
var select_value_box = document.getElementById("select_lf_status");
row_filter_mask = [];
var num_total = num_train + num_val;
var lf_matrix = (display_ext_data) ? lf_matrix_ext : lf_matrix_noext;
var prob_labels = (display_ext_data) ? prob_labels_ext : prob_labels_noext;
var criteria;
if (select_value_box.value == "positive")
criteria = LF_OUTPUT_POSITIVE;
else if (select_value_box.value == "negative")
criteria = LF_OUTPUT_NEGATIVE;
else if (select_value_box.value == "abstain")
criteria = LF_OUTPUT_ABSTAIN;
else if (select_value_box.value == "votes")
criteria = LF_OUTPUT_VOTES;
else if (select_value_box.value == "agree")
criteria = LF_OUTPUT_AGREES;
else if (select_value_box.value == "votes+disagrees")
criteria = LF_OUTPUT_DISAGREES;
else if (select_value_box.value == "exists")
criteria = LF_OUTPUT_EXISTS;
else if (select_value_box.value == "correct")
criteria = LF_OUTPUT_CORRECT;
else if (select_value_box.value == "incorrect")
criteria = LF_OUTPUT_INCORRECT;
else {
criteria = LF_OUTPUT_UNKNOWN;
}
if (select_lf_box.value == "[no filter]") {
for (var i=0; i<num_total; i++) {
row_filter_mask[i] = true;
}
}
else if (select_lf_box.value == "All LFs") {
// ["positive", "negative", "abstain", "votes"]
for (var i=0; i<num_total; i++) {
row_filter_mask[i] = true;
var idx = i*num_lf;
for (var j=0; j<num_lf; j++) {
if (criteria == LF_OUTPUT_VOTES && lf_matrix[idx + j] == 0)
row_filter_mask[i] = false;
if (criteria != LF_OUTPUT_VOTES && lf_matrix[idx+j] != criteria)
row_filter_mask[i] = false;
}
}
}
else if (select_lf_box.value == "All voting LFs") {
// ["positive", "negative", "agree"]
for (var i=0; i<num_total; i++) {
row_filter_mask[i] = true;
var idx = i*num_lf;
var num_votes = 0;
var agree_value;
for (var j=0; j<num_lf; j++) {
// LF does not vote
if (lf_matrix[idx + j] == LF_OUTPUT_ABSTAIN)
continue;
num_votes++;
// record the vote of the first LF that votes
if (num_votes == 1)
agree_value = lf_matrix[idx + j];
// require agreement
if (criteria == LF_OUTPUT_AGREES) {
if (num_votes > 1 && lf_matrix[idx + j] != agree_value) {
// disagreement detected
row_filter_mask[i] = false;
}
} else if (lf_matrix[idx + j] != criteria) {
row_filter_mask[i] = false;
}
}
// row shouldn't pass an "all voting LFs" filter if there are no voting LFs
// FIXME(kayvonf): We current allow a row to pass the filter "all voting LFs agree" even if
// there is only 1 voting LF. Is this the desired behavior?
if (num_votes == 0)
row_filter_mask[i] = false;
}
}
else if (select_lf_box.value == "Any LF") {
// ["positive", "negative", "abstain", "votes", "votes+disagrees", "correct", "incorrect"]
for (var i=0; i<num_total; i++) {
row_filter_mask[i] = false;
var idx = i*num_lf;
for (var j=0; j<num_lf; j++) {
if ( (criteria == LF_OUTPUT_POSITIVE || criteria == LF_OUTPUT_NEGATIVE || criteria == LF_OUTPUT_ABSTAIN) && lf_matrix[idx+j] == criteria) {
row_filter_mask[i] = true;
break;
}
else if (criteria == LF_OUTPUT_VOTES && lf_matrix[idx+j] != LF_OUTPUT_ABSTAIN) {
row_filter_mask[i] = true;
break;
}
else if (criteria == LF_OUTPUT_CORRECT && lf_matrix[idx+j] != LF_OUTPUT_ABSTAIN && lf_matrix[idx+j] == ground_truth_labels[i]) {
row_filter_mask[i] = true;
break;
}
else if (criteria == LF_OUTPUT_INCORRECT && lf_matrix[idx+j] != LF_OUTPUT_ABSTAIN && lf_matrix[idx+j] != ground_truth_labels[i]) {
row_filter_mask[i] = true;
break;
}
else if (criteria == LF_OUTPUT_DISAGREES) {
for (var k=0; k<num_lf; k++) {
if (j != k &&
lf_matrix[idx+j] != LF_OUTPUT_ABSTAIN &&
lf_matrix[idx+k] != LF_OUTPUT_ABSTAIN &&
lf_matrix[idx+j] != lf_matrix[idx+k]) {
row_filter_mask[i] = true;
break;
}
}