-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.js
278 lines (240 loc) · 9.21 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
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
function DataTable() {
this.init = function(parent_node, data) {
// container to control scrolling, sizing, and such
var table_div = document.createElement("div");
table_div.setAttribute("class", "table-container");
parent_node.appendChild(table_div);
// data table
var table = document.createElement("table");
table.setAttribute("class", "data-table");
table.setAttribute("cellpadding", "0");
table.setAttribute("cellspacing", "0");
table_div.appendChild(table);
var thead = document.createElement("thead");
table.appendChild(thead);
var hrow = document.createElement("tr");
thead.appendChild(hrow);
var keys = Object.keys(data);
for(var i = 0; i < keys.length; i++) {
var th = document.createElement("th");
th.textContent = keys[i];
th.setAttribute("class", "data-table-header-cell");
hrow.appendChild(th);
}
var tbody = document.createElement("tbody");
table.appendChild(tbody);
for(var j = 0; j < data[keys[0]].length; j++) {
var tr = document.createElement("tr");
tbody.appendChild(tr);
for(var i = 0; i < keys.length; i++) {
var td = document.createElement("td");
tr.appendChild(td);
td.setAttribute("class", "data-table-cell");
td.textContent = data[keys[i]][j];
}
}
}
this.init.apply(this, arguments);
}
function Dataset() {
this.init = function(id, fname, csv_data, parent_node, genFcn) {
this.name = id;
this.load_data(csv_data);
this.genFcn = genFcn;
// first, make the dataset block
this.root_node = document.createElement("div");
this.root_node.setAttribute("id", id);
this.root_node.setAttribute("class", "sheet");
parent_node.appendChild(this.root_node);
this.tab = document.createElement("div");
this.tab.setAttribute("class", "tab");
this.tab.textContent = fname;
this.tab.addEventListener("click", function() {
load_tab(this, id);
}, false);
document.getElementById("tabs").appendChild(document.createTextNode(" ")); // put a little space between tabs
document.getElementById("tabs").appendChild(this.tab);
// activate it immediately - incidentally, this will fix the BBox issues
load_tab(this.tab, id);
// delete button
var del = document.createElement("button");
del.setAttribute("class", "dataset_delete");
del.setAttribute("type", "button");
del.setAttribute("style", "float:right");
del.textContent = "x";
this.root_node.appendChild(del);
// internal data structure
this.columns = Object.keys(this.data);
this.first_col = this.data[this.columns[0]];
this.data_table = new DataTable(this.root_node, this.data);
// selectors for multi-dataset analysis
for(var s = 0; s < 2; s++) {
var series = document.getElementById("dataSeries" + s);
for(var i = 0; i < this.columns.length; i++) {
var option = document.createElement("option");
var col_id = this.name + "/" + this.columns[i];
option.textContent = col_id;
option.setAttribute("value", col_id);
option.setAttribute("class", col_id);
series.appendChild(option);
}
}
// create statistics elements
var stats_elem = document.createElement("span");
stats_elem.setAttribute("class", "agg_stats");
/*
var icdf_input = document.createElement("input");
icdf_input.setAttribute("type", "range");
icdf_input.setAttribute("min", 0.0);
icdf_input.setAttribute("value", 0.5);
icdf_input.setAttribute("max", 1.0);
icdf_input.setAttribute("step", 0.01);
icdf_input.setAttribute("style", "width:200px; margin-right:20px");
*/
var icdf_input = new Slider(0.0, 1.0, 0.2);
icdf_input.width(200);
icdf_input.height(20);
icdf_input.node().style.marginRight = "20px";
var icdf_btn = document.createElement("button");
icdf_btn.setAttribute("type", "button");
icdf_btn.textContent = "iCDF";
if (this.columns.length>1) {
var fit_btn = document.createElement("button");
fit_btn.setAttribute("type", "button");
fit_btn.textContent = "Fit!";
}
if (this.genFcn != null) {
var sample_btn = document.createElement("button");
sample_btn.setAttribute("type", "button");
sample_btn.textContent = "Get more data";
}
this.root_node.appendChild(document.createTextNode("Aggregate stats: "));
this.root_node.appendChild(document.createElement("br"));
this.root_node.appendChild(stats_elem);
this.root_node.appendChild(document.createElement("br"));
this.root_node.appendChild(document.createElement("br"));
this.root_node.appendChild(icdf_input.node());
this.root_node.appendChild(icdf_btn);
this.root_node.appendChild(document.createElement("br"));
this.root_node.appendChild(document.createElement("br"));
if (this.columns.length>1) {
this.root_node.appendChild(fit_btn);
this.root_node.appendChild(document.createElement("br"));
this.root_node.appendChild(document.createElement("br"));
}
if (this.genFcn != null) {
this.root_node.appendChild(sample_btn);
this.root_node.appendChild(document.createElement("br"));
this.root_node.appendChild(document.createElement("br"));
}
// initialize data statistics
for(col in this.data) {
stats_elem.appendChild(document.createTextNode(col + ": \u03bc" + mean(this.data[col]).toString() + " \u03c3" + stdv(this.data[col]).toString()));
stats_elem.appendChild(document.createElement("br"));
}
// scatter plot/histogram
if (this.columns.length>1) {
this.second_col = this.data[this.columns[1]];
var scatter = new Scatter(this.data, d3.select(this.root_node));
} else {
// initialize chart and mean histogram
var arMax = getMaxOfArray(this.first_col);
var arMin = getMinOfArray(this.first_col);
var nBins = 5;
var arSpan = arMax-arMin;
var binSz = arSpan/(nBins-1);
this.centers = new Array(nBins);
for (var i=0; i<nBins; i++)
this.centers[i] = arMin + i*binSz;
this.edges = new Array(nBins+1);
for (var i=0; i<nBins+1; i++)
this.edges[i] = arMin - binSz/2 + i*binSz;
var hist_data = histogram(this.first_col, this.edges);
var barchart = new BarChart(this.centers, hist_data, this.root_node);
}
// ------ set up button callbacks ------ //
// callback for delete button
del.addEventListener("click", function(){
parent_node.removeChild(this.root_node)
document.getElementById("tabs").removeChild(this.tab); // and get rid of tab
// selectors for multi-dataset analysis
for (var i = 0; i < this.columns.length; i++) {
var col_id = this.name + "/" + this.columns[i];
var options = document.getElementsByClassName(col_id);
for (var j = options.length-1; j>-1; j--) {
options[j].parentNode.removeChild(options[j]);
}
}
console.log("deleted.")
current_sheet = document.getElementById("sheet1");
current_sheet.style.display = "block";
current_tab = document.getElementById("tab1");
current_tab.style.borderBottom = "1px solid #FFFFFF";
return false;
}.bind(this), false);
// callback for iCDF
$(icdf_btn).click(function(){
var val = icdf_input.values();
var start = val.start;
var end = val.end;
console.log(icdf(this.first_col, start), icdf(this.first_col, end))
return false;
}.bind(this));
// callback for linear fit
if (this.columns.length>1) {
$(fit_btn).click(function(){
var fit = linearFit(this.first_col, this.second_col);
console.log("y = {1}x + {0}".format(fit[0],fit[1]))
console.log("rmse: {0}".format(rmse(this.first_col, this.second_col, fit)))
console.log("r2: {0}".format(r2(this.first_col, this.second_col, fit)))
scatter.drawLine(fit)
return false;
}.bind(this));
}
if (this.genFcn != null) {
$(sample_btn).click(function(){
console.log(this.genFcn())
}.bind(this));
}
}
this.sampleMean = function() {
this.meanSamples.push(mean(bootstrap(this.first_col, this.first_col.length)));
}
this.load_data = function(csv_data) {
var header;
if(isNaN(parseInt(csv_data[0][0]))) {
// header is present, maybe
header = csv_data.splice(0, 1)[0]; // remove first row
} else {
header = [];
for(var i = 1; i <= csv_data[0].length; i++) {
header.push("col"+i);
}
}
this.data = {};
// initialize dictionary of arrays
for (var j = 0; j < header.length; j++) {
this.data[header[j]] = [];
}
var nans = 0;
for (var i = 0; i < csv_data.length; i++) {
for (var j = 0; j < csv_data[i].length; j++) {
if(typeof(csv_data[i][j])=='number') {
this.data[header[j]].push(csv_data[i][j]);
} else if(csv_data[i][j].trim().length == 0) {
this.data[header[j]].push(NaN);
nans++;
} else {
this.data[header[j]].push(Number(csv_data[i][j]));
if (isNaN(this.data[header[j]][i])) {
nans++;
}
}
}
}
if(nans > 0) {
console.error(nans + " values were not numeric, now they are NaN.");
}
}
this.init.apply(this, arguments);
}