-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhelpers.js
134 lines (124 loc) · 4.49 KB
/
helpers.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
function renderTest (model, samples, features, targets, $el) {
$el.append("<tr style='background:#eeeeee;'><td>"+features.join('</td><td>')+'</td><td>Prediction</td><td>Actual</td></tr>');
//_.each(samples,function(s) {
for(var i=0;i<samples.length;i++) {
//var vals_for_sample = _.map(features,function(x){return s[x]});
var s = samples[i];
var target = targets[i];
var vals_for_sample = s;
$el.append("<tr><td>"+vals_for_sample.join('</td><td>')+"</td><td><b>"+model.predict(s)+"</b></td><td> "+target+"</td></tr>");
}
}
function processTestFile(f) {
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
loadString(e.target.result, function(D) {
var start = (new Date).getTime();
var tree = new learningjs.tree();
if(typeof trained_model === 'undefined') {
alert('You need to train a model first.');
} else {
$("#status").append('Testing in process</br>');
console.log('model:',trained_model);
var elapsed = 0.00;
var diff = (new Date).getTime() - start;
console.log('Testing took ' + diff.toFixed(0) + " ms.");
trained_model.calcAccuracy(D.data, D.targets, function(acc, correct, total){
console.log( 'Testing: got '+correct +' correct out of '+total+' examples. accuracy:'+(acc*100.0).toFixed(2)+'%');
$("#status").append( 'Testing: got '+correct +' correct out of '+total+' examples. accuracy:'+(acc*100.0).toFixed(2)+'%<br/>');
});
renderTest(trained_model, D.data,D.featureNames, D.targets, $("#samples"));
}
});
};
})(f);
reader.readAsText(f);
}
function processTrainFile(f) {
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
loadString(e.target.result, function(D) {
//console.log(D);
var start = (new Date).getTime();
var tree = new learningjs.tree();
$("#status").append('Training in process</br>');
tree.train(D, function(model, err){
if(err) {
console.log(err);
} else {
console.log('drawing tree');
tree.drawGraph(model,'canvas', function(err){
if(err)
$("#status").append('Error.'+err+'<br/>');
});
console.log('model:',model);
trained_model = model;
var elapsed = 0.00;
var diff = (new Date).getTime() - start;
console.log('training took ' + diff.toFixed(0) + " ms.");
model.calcAccuracy(D.data, D.targets, function(acc, correct, total){
console.log( 'training: got '+correct +' correct out of '+total+' examples. accuracy:'+(acc*100.0).toFixed(2)+'%');
});
$("#status").append('Model training has finished. Now drop your test file.</br>');
}
});
});
};
})(f);
reader.readAsText(f);
}
function handleTestFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
this.className='holder';
var f = evt.dataTransfer.files[0]; // FileList object
console.log(f.type);
processTestFile(f);
}
function handleTrainFileSelect(evt) {
$('#canvas').html('');
$('#status').html('');
$('#samples').html('');
evt.stopPropagation();
evt.preventDefault();
this.className='holder';
var f = evt.dataTransfer.files[0]; // FileList object
console.log(f.type);
processTrainFile(f);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
this.className='holder hover';
}
function handleLeave(evt) {
this.className='holder';
}
$(function(){
var dropZone = document.getElementById('drop_train_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('dragleave', handleLeave, false);
dropZone.addEventListener('drop', handleTrainFileSelect, false);
var dropZone2 = document.getElementById('drop_test_zone');
dropZone2.addEventListener('dragover', handleDragOver, false);
dropZone2.addEventListener('dragleave', handleLeave, false);
dropZone2.addEventListener('drop', handleTestFileSelect, false);
function deleteVars() {
$('#demoScript').remove();
delete _training_data;
delete training_data;
delete features;
delete classlabel;
delete test_data;
}
$('#reset').click(function() {
deleteVars();
$('#canvas').html('');
$('#status').html('');
$('#samples').html('');
});
});
var trained_model = undefined;