-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataset.js
executable file
·217 lines (177 loc) · 6.42 KB
/
Dataset.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
// Clairvoyant.js Dataset class ----------------------------------------------------------------
function Dataset(variables) {
'use strict';
var i;
//an array containing the variable names in order:
this.variables = variables;
//last variable is include / disinclude flag:
this.variables.push('accepted');
//prepare an array of arrays to hold the data. First index correponds to variable, second index
//labels data point:
this.data = [];
for (i = 0; i < this.variables.length; i++) {
this.data[i] = [];
}
// function definitions: ---------------------------------------------------------------------
// dump data table to screen:
this.dump = function () {
var d, i;
for (i = 0; i < this.variables.length; i++) {
document.write(this.variables[i] + "\t");
}
document.write("</br>");
for (d = 0; d < this.data[0].length; d++) {
for (i = 0; i < this.variables.length; i++) {
document.write(this.data[i][d] + "\t");
}
document.write("</br>");
}
};
// add a new entry to the table. <entry> is an Array containing all the values for this entry
// in the order defined in <variables> in the class definition
this.addEntry = function (entry) {
var i;
if (!(entry instanceof Array)) {
alert ('fee!');
return;
}
if (entry.length !== this.variables.length - 1) {
alert ('fie!');
return;
}
for (i = 0; i < entry.length; i++) {
this.data[i].push(entry[i]);
}
//accept new data point by default:
this.data[entry.length].push(1);
};
// delete an entry from the table at row index <index>
this.deleteEntry = function (index) {
var i;
if (index >= this.data[0].length) {
alert('index for dataset entry deletion out of range');
return;
}
for (i = 0; i < this.data.length; i++) {
this.data[i].splice(index,1);
}
};
// change accepted status to <flag> for an entry at row index <index>
this.accept = function (index, flag) {
var i;
if(flag !== 0 && flag !== 1) {
alert('Dataset flags must either be 0 or 1.');
return;
}
if (index >= this.data[0].length) {
alert('index for acceptance flag setting out of range');
return;
}
this.data[this.data.length-1][index] = flag;
};
// change all acceptance flags to <flag>:
this.setAll = function(flag) {
var i;
for (i = 0; i < this.data[0].length; i++) {
this.data[this.data.length - 1][i] = flag;
}
};
//change acceptance flags to 1 based on a boolean condition:
this.filter = function(variable, operator, value) {
var d, i;
i = this.variables.indexOf(variable);
if (i === -1) {
alert(variable+' is not present in this dataset.');
return;
}
switch (operator) {
case '==':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] == value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
case '===':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] === value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
case '!=':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] != value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
case '!==':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] !== value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
case '>':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] > value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
case '<':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] < value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
case '>=':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] >= value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
case '<=':
for (d = 0; d < this.data[i].length; d++) {
if (this.data[i][d] <= value) {
this.data[this.data.length - 1][d] = 1;
}
}
break;
default:
alert('<operator> must be one of ==, ===, !=, !==, <, >, <=, >=');
return;
}
};
//produce a subset of this Dataset based on some boolean conditions
this.subset = function (conditions) {
var condArray, condString, parsePosition, tokenizedCond, tokens;
//remove whitespace:
condString = conditions.replace(" ", "");
//tab
//newline
//tokenize the conditions string:
condArray = condString.split("");
tokens = this.variables;
tokens[tokens.length] = '(';
tokens[tokens.length] = ')';
tokens[tokens.length] = '&&';
tokens[tokens.length] = '||';
tokens[tokens.length] = '==';
tokens[tokens.length] = '===';
tokens[tokens.length] = '!=';
tokens[tokens.length] = '!==';
tokens[tokens.length] = '>';
tokens[tokens.length] = '<';
tokens[tokens.length] = '>=';
tokens[tokens.length] = '<=';
//parse the conditions:
parsePosition = 0;
tokenizedCond = [];
while (parsePosition < condArray.length) {
}
};
}