annotate analysis/analysis.js @ 1878:7688d978807e

Removes navigation alert once test is completed. Closes #1647
author Giulio Moro <giuliomoro@users.noreply.github.com>
date Sun, 06 Mar 2016 19:24:19 +0000
parents f682341c8eee
children 9dcfd654abad 83b439322229
rev   line source
nickjillings@1864 1 /*
nickjillings@1864 2 * Analysis script for WAET
nickjillings@1864 3 */
nickjillings@1864 4
nickjillings@1864 5 var chartContext;
nickjillings@1864 6 window.onload = function() {
nickjillings@1864 7 // Load the Visualization API and the corechart package.
nickjillings@1864 8 google.charts.load('current', {'packages':['corechart']});
nickjillings@1864 9 chartContext = new Chart();
nickjillings@1864 10 }
nickjillings@1864 11
nickjillings@1868 12 function arrayMean(values) {
nickjillings@1868 13 var mean = 0;
nickjillings@1868 14 for (var value of values) {
nickjillings@1868 15 mean += value;
nickjillings@1868 16 }
nickjillings@1868 17 mean /= values.length;
nickjillings@1868 18 return mean;
nickjillings@1868 19 }
nickjillings@1868 20
nickjillings@1868 21 function percentile(values, n) {
nickjillings@1868 22 values.sort( function(a,b) {return a - b;} );
nickjillings@1868 23 // get ordinal rank
nickjillings@1868 24 var rank = Math.min(Math.floor(values.length*n/100), values.length-1);
nickjillings@1868 25 return values[rank];
nickjillings@1868 26 }
nickjillings@1868 27
nickjillings@1868 28 function arrayMin(array) {
nickjillings@1868 29 // Return the minimum value of an array
nickjillings@1868 30 var min = array[0];
nickjillings@1868 31 for (var value of array) {
nickjillings@1868 32 if (value < min) {
nickjillings@1868 33 min = value;
nickjillings@1868 34 }
nickjillings@1868 35 }
nickjillings@1868 36 return min;
nickjillings@1868 37 }
nickjillings@1868 38
nickjillings@1868 39 function arrayMax(array) {
nickjillings@1868 40 // Return the minimum value of an array
nickjillings@1868 41 var max = array[0];
nickjillings@1868 42 for (var value of array) {
nickjillings@1868 43 if (value > max) {
nickjillings@1868 44 max = value;
nickjillings@1868 45 }
nickjillings@1868 46 }
nickjillings@1868 47 return max;
nickjillings@1868 48 }
nickjillings@1868 49
nickjillings@1868 50 function arrayHistogram(values,steps,min,max) {
nickjillings@1868 51 if (steps == undefined) {
nickjillings@1868 52 steps = 0.25;
nickjillings@1868 53 console.log("Warning: arrayHistogram called without steps size set, default to 0.25");
nickjillings@1868 54 }
nickjillings@1868 55 if (min == undefined) {min = arrayMin(values);}
nickjillings@1868 56 if (max == undefined) {max = arrayMax(values);}
nickjillings@1868 57 var histogram = [];
nickjillings@1868 58 var index = min;
nickjillings@1868 59 while(index < max) {
nickjillings@1868 60 histogram.push({
nickjillings@1868 61 marker: index,
nickjillings@1868 62 lt: index,
nickjillings@1868 63 rt: index+steps,
nickjillings@1868 64 count: 0
nickjillings@1868 65 });
nickjillings@1868 66 index += steps;
nickjillings@1868 67 }
nickjillings@1868 68 for (var value of values) {
nickjillings@1868 69 for (var entry of histogram) {
nickjillings@1868 70 if (value >= entry.lt && value <= entry.rt) {
nickjillings@1868 71 entry.count++;
nickjillings@1868 72 break;
nickjillings@1868 73 }
nickjillings@1868 74 }
nickjillings@1868 75 }
nickjillings@1868 76 return histogram;
nickjillings@1868 77 }
nickjillings@1868 78
nickjillings@1864 79 function Chart() {
nickjillings@1867 80 this.valueData = null;
nickjillings@1867 81 this.commentData = null;
nickjillings@1867 82 this.loadStatus = 0;
nickjillings@1868 83 this.charts = [];
nickjillings@1864 84
nickjillings@1867 85 var XMLHttp = new XMLHttpRequest();
nickjillings@1867 86 XMLHttp.parent = this;
nickjillings@1867 87 XMLHttp.open("GET","../scripts/score_parser.php?format=JSON",true);
nickjillings@1867 88 XMLHttp.onload = function() {
nickjillings@1867 89 // Now we have the JSON data, extract
nickjillings@1867 90 this.parent.valueData = JSON.parse(this.responseText);
nickjillings@1867 91 this.parent.loadStatus++;
nickjillings@1864 92 }
nickjillings@1867 93 XMLHttp.send();
nickjillings@1867 94 var XMLHttp2 = new XMLHttpRequest();
nickjillings@1867 95 XMLHttp2.parent = this;
nickjillings@1867 96 XMLHttp2.open("GET","../scripts/comment_parser.php?format=JSON",true);
nickjillings@1867 97 XMLHttp2.onload = function() {
nickjillings@1867 98 // Now we have the JSON data, extract
nickjillings@1867 99 this.parent.commentData = JSON.parse(this.responseText);
nickjillings@1867 100 this.parent.loadStatus++;
nickjillings@1867 101 }
nickjillings@1867 102 XMLHttp2.send();
nickjillings@1864 103
nickjillings@1868 104 this.chartObject = function(name) {
nickjillings@1868 105 // Create the charting object
nickjillings@1868 106 this.name = name;
nickjillings@1868 107 this.root = document.createElement("div");
nickjillings@1868 108 this.root.className = "chart-holder";
nickjillings@1868 109 this.root.setAttribute("name",name);
nickjillings@1868 110 this.chartDOM = document.createElement("div");
nickjillings@1868 111 this.tableDOM = document.createElement("div");
nickjillings@1868 112 this.latexDOM = document.createElement("div");
nickjillings@1868 113 this.downloadDOM = document.createElement("div");
nickjillings@1868 114 this.chart = undefined;
nickjillings@1868 115 this.data = new google.visualization.DataTable();
nickjillings@1868 116 this.options = {};
nickjillings@1868 117 this.print = document.createElement("button");
nickjillings@1869 118 this.sortDataButton = document.createElement("button");
nickjillings@1869 119 this.sortDataButton.textContent = "Sort by Data";
nickjillings@1869 120 this.sortDataButton.addEventListener("click",this);
nickjillings@1869 121 this.sortDataButton.setAttribute("name","sort-data");
nickjillings@1869 122 this.sortNameButton = document.createElement("button");
nickjillings@1869 123 this.sortNameButton.textContent = "Sort by Name";
nickjillings@1869 124 this.sortNameButton.addEventListener("click",this);
nickjillings@1869 125 this.sortNameButton.setAttribute("name","sort-name");
nickjillings@1869 126 this.draw = function() {
nickjillings@1869 127 if (this.chart == undefined) {return;}
nickjillings@1869 128 this.tableDOM.innerHTML = null;
nickjillings@1869 129 this.latexDOM.innerHTML = null;
nickjillings@1869 130 this.buildTable();
nickjillings@1869 131 this.writeLatex();
nickjillings@1869 132 this.chart.draw(this.data,this.options);
nickjillings@1869 133 }
nickjillings@1869 134 this.sortData = function() {
nickjillings@1869 135
nickjillings@1869 136 var map = this.data.Jf.map(function(el,i){
nickjillings@1869 137 return {index: i, value: el.c[1].v};
nickjillings@1869 138 });
nickjillings@1869 139
nickjillings@1869 140 map.sort(function(a,b){
nickjillings@1869 141 if (a.value > b.value) {return -1;}
nickjillings@1869 142 if (a.value < b.value) {return 1;}
nickjillings@1869 143 return 0;
nickjillings@1869 144 })
nickjillings@1869 145
nickjillings@1869 146 var Jf = [];
nickjillings@1869 147 var cc = [];
nickjillings@1869 148 for (var i=0; i<map.length; i++) {
nickjillings@1869 149 Jf.push(this.data.Jf[map[i].index]);
nickjillings@1869 150 cc.push(this.data.cc[map[i].index]);
nickjillings@1869 151 }
nickjillings@1869 152 this.data.Jf = Jf;
nickjillings@1869 153 this.data.cc = cc;
nickjillings@1869 154 }
nickjillings@1869 155 this.sortName = function() {
nickjillings@1869 156 var map = this.data.Jf.map(function(el,i){
nickjillings@1869 157 return {index: i, value: el.c[0].v};
nickjillings@1869 158 });
nickjillings@1869 159
nickjillings@1869 160 map.sort(function(a,b){
nickjillings@1869 161 if (a.value < b.value) {return -1;}
nickjillings@1869 162 if (a.value > b.value) {return 1;}
nickjillings@1869 163 return 0;
nickjillings@1869 164 })
nickjillings@1869 165
nickjillings@1869 166 var Jf = [];
nickjillings@1869 167 var cc = [];
nickjillings@1869 168 for (var i=0; i<map.length; i++) {
nickjillings@1869 169 Jf.push(this.data.Jf[map[i].index]);
nickjillings@1869 170 cc.push(this.data.cc[map[i].index]);
nickjillings@1869 171 }
nickjillings@1869 172 this.data.Jf = Jf;
nickjillings@1869 173 this.data.cc = cc;
nickjillings@1869 174 }
nickjillings@1868 175 this.handleEvent = function() {
nickjillings@1868 176 // Only used to handle the chart.event.addListener(this,'ready') callback
nickjillings@1869 177 switch(event.currentTarget.getAttribute("name"))
nickjillings@1869 178 {
nickjillings@1869 179 case "download":
nickjillings@1869 180 window.open(this.chart.getImageURI());
nickjillings@1869 181 break;
nickjillings@1869 182 case "sort-data":
nickjillings@1869 183 this.sortData();
nickjillings@1869 184 this.draw();
nickjillings@1869 185 break;
nickjillings@1869 186 case "sort-name":
nickjillings@1869 187 this.sortName();
nickjillings@1869 188 this.draw();
nickjillings@1869 189 break;
nickjillings@1869 190 }
nickjillings@1868 191 }
nickjillings@1868 192
nickjillings@1868 193 this.root.appendChild(this.chartDOM);
nickjillings@1868 194 this.root.appendChild(this.tableDOM);
nickjillings@1868 195 this.root.appendChild(this.latexDOM);
nickjillings@1869 196 this.root.appendChild(this.sortDataButton);
nickjillings@1869 197 this.root.appendChild(this.sortNameButton);
nickjillings@1868 198 this.root.appendChild(this.print);
nickjillings@1868 199 this.print.textContent = "Download";
nickjillings@1869 200 this.print.setAttribute("name","download");
nickjillings@1868 201 this.print.addEventListener("click",this);
nickjillings@1868 202 this.root.appendChild(this.downloadDOM);
nickjillings@1868 203 this.buildTable = function() {
nickjillings@1868 204 var table = document.createElement("table");
nickjillings@1868 205 table.border = "1";
nickjillings@1868 206 for (var rowIndex=0; rowIndex<this.data.If.length; rowIndex++) {
nickjillings@1868 207 var row = document.createElement("tr");
nickjillings@1868 208 table.appendChild(row);
nickjillings@1868 209 var rowTitle = document.createElement("td");
nickjillings@1868 210 rowTitle.textContent = this.data.If[rowIndex].label;
nickjillings@1868 211 row.appendChild(rowTitle);
nickjillings@1868 212 for (var cIndex=0; cIndex<this.data.cc.length; cIndex++) {
nickjillings@1868 213 var column = document.createElement("td");
nickjillings@1868 214 column.textContent = this.data.cc[cIndex][rowIndex].tf;
nickjillings@1868 215 row.appendChild(column);
nickjillings@1868 216 }
nickjillings@1868 217 }
nickjillings@1868 218 this.tableDOM.appendChild(table);
nickjillings@1868 219 };
nickjillings@1868 220 this.writeLatex = function() {
nickjillings@1868 221 var root = document.createElement("div");
nickjillings@1868 222 root.className = "code";
nickjillings@1868 223 var holder = document.createElement("pre");
nickjillings@1868 224 // Table start
nickjillings@1868 225 var start = document.createElement("p");
nickjillings@1868 226 start.textContent = "\\" + "begin{tabular}{|l|";
nickjillings@1868 227 holder.appendChild(start);
nickjillings@1868 228 for (var i=0; i<this.data.cc.length; i++) {
nickjillings@1868 229 start.textContent = start.textContent+"c|";
nickjillings@1868 230 }
nickjillings@1869 231 start.textContent = start.textContent.concat("}");
nickjillings@1868 232 // Now write the rows:
nickjillings@1868 233 for (var rIndex=0; rIndex<this.data.If.length; rIndex++) {
nickjillings@1868 234 var row = document.createElement("p");
nickjillings@1868 235 row.textContent = this.data.If[rIndex].label.concat(" & ");
nickjillings@1868 236 for (var cIndex=0; cIndex<this.data.cc.length; cIndex++) {
nickjillings@1868 237 row.textContent = row.textContent.concat(this.data.cc[cIndex][rIndex].tf);
nickjillings@1868 238 if (cIndex < this.data.cc.length-1) {
nickjillings@1868 239 row.textContent = row.textContent.concat(" & ");
nickjillings@1868 240 }
nickjillings@1868 241 }
nickjillings@1868 242 holder.appendChild(row);
nickjillings@1868 243 }
nickjillings@1868 244 // Table end
nickjillings@1868 245 var end = document.createElement("p");
nickjillings@1868 246 end.textContent = "\\" + "end{tabular}";
nickjillings@1868 247 holder.appendChild(end);
nickjillings@1868 248 root.appendChild(holder);
nickjillings@1868 249 this.latexDOM.appendChild(root);
nickjillings@1868 250 }
nickjillings@1868 251 }
nickjillings@1868 252
nickjillings@1868 253 this.clear = function() {
nickjillings@1868 254 var inject = document.getElementById("test-pages");
nickjillings@1868 255 for (var chart of this.charts) {
nickjillings@1868 256 inject.removeChild(chart.root);
nickjillings@1868 257 }
nickjillings@1868 258 this.charts = [];
nickjillings@1868 259 }
nickjillings@1868 260
nickjillings@1868 261 this.drawTestMean = function() {
nickjillings@1868 262 // This draws one bargraph per axis with every test element on
nickjillings@1868 263 if (this.valueData == null) {
nickjillings@1868 264 console.log("Error - Data not loaded");
nickjillings@1868 265 return;
nickjillings@1868 266 }
nickjillings@1868 267 var chartList = [];
nickjillings@1868 268
nickjillings@1868 269 // Create the data table
nickjillings@1868 270 for (var page of this.valueData.pages) {
nickjillings@1868 271 for (var element of page.elements) {
nickjillings@1868 272 for (var axis of element.axis) {
nickjillings@1868 273 // Find the axis
nickjillings@1868 274 var axisChart = chartList.find(function(element,index,array){
nickjillings@1868 275 if (element.name == this) {return true;} else {return false;}
nickjillings@1868 276 },"mean-test-"+axis.id);
nickjillings@1868 277 if (axisChart == null) {
nickjillings@1868 278 axisChart = new this.chartObject("mean-test-"+axis.id);
nickjillings@1868 279 axisChart.options = {
nickjillings@1868 280 'title':'Mean of axis: '+axis.name,
nickjillings@1868 281 'width':window.innerWidth*0.9,
nickjillings@1868 282 'height':(window.innerWidth*0.9)/1.77
nickjillings@1868 283 }
nickjillings@1868 284 axisChart.data.addColumn('string','id');
nickjillings@1868 285 axisChart.data.addColumn('number',axis.name);
nickjillings@1868 286 chartList.push(axisChart);
nickjillings@1868 287 document.getElementById("test-pages").appendChild(axisChart.root);
nickjillings@1868 288 }
nickjillings@1868 289 var mean = arrayMean(axis.values);
nickjillings@1868 290 axisChart.data.addRow([element.id,mean]);
nickjillings@1868 291 }
nickjillings@1868 292 }
nickjillings@1868 293 }
nickjillings@1868 294
nickjillings@1868 295 // Build and push charts
nickjillings@1868 296 for (var chart of chartList) {
nickjillings@1868 297 chart.chart = new google.visualization.ColumnChart(chart.chartDOM);
nickjillings@1868 298 chart.chart.draw(chart.data,chart.options);
nickjillings@1868 299 chart.buildTable();
nickjillings@1868 300 chart.writeLatex();
nickjillings@1868 301 this.charts.push(chart);
nickjillings@1868 302 }
nickjillings@1868 303 }
nickjillings@1868 304
nickjillings@1868 305 this.drawPageMean = function() {
nickjillings@1864 306 // First we must get the value data
nickjillings@1867 307 if (this.valueData == null) {
nickjillings@1867 308 console.log("Error - Data not loaded");
nickjillings@1867 309 return;
nickjillings@1867 310 }
nickjillings@1867 311 // We create one plot per page
nickjillings@1867 312 for (var page of this.valueData.pages) {
nickjillings@1868 313
nickjillings@1868 314 // Create the chart resulting point
nickjillings@1868 315 var chart = new this.chartObject("mean-page-"+page.id);
nickjillings@1868 316 document.getElementById("test-pages").appendChild(chart.root);
nickjillings@1867 317
nickjillings@1867 318 // Create the data table
nickjillings@1868 319 chart.data.addColumn('string','id');
nickjillings@1867 320 // Get axis labels
nickjillings@1867 321 for (var axis of page.elements[0].axis) {
nickjillings@1868 322 chart.data.addColumn('number',axis.name);
nickjillings@1867 323 }
nickjillings@1867 324 var rows = []; // Rows is an array of tuples [col1, col2, col3 ... colN];
nickjillings@1867 325 for (var element of page.elements) {
nickjillings@1867 326 var entry = [element.id];
nickjillings@1867 327 for (var i=0; i<page.elements[0].axis.length; i++) {
nickjillings@1867 328 var mean =0;
nickjillings@1867 329 if (i < element.axis.length) {
nickjillings@1867 330 var axis = element.axis[i];
nickjillings@1868 331 mean = arrayMean(axis.values);
nickjillings@1867 332 }
nickjillings@1867 333 entry.push(mean);
nickjillings@1867 334 }
nickjillings@1867 335 rows.push(entry);
nickjillings@1867 336 }
nickjillings@1868 337 chart.data.addRows(rows);
nickjillings@1868 338 chart.options = {
nickjillings@1868 339 'title':'Mean of page: '+page.id,
nickjillings@1868 340 'width':800,
nickjillings@1868 341 'height':700
nickjillings@1868 342 }
nickjillings@1867 343 // Draw the chart
nickjillings@1868 344 chart.chart = new google.visualization.ColumnChart(chart.chartDOM);
nickjillings@1868 345 chart.chart.draw(chart.data,chart.options);
nickjillings@1868 346 chart.buildTable();
nickjillings@1868 347 chart.writeLatex();
nickjillings@1868 348 this.charts.push(chart);
nickjillings@1868 349 }
nickjillings@1868 350 }
nickjillings@1868 351
nickjillings@1868 352 this.drawElementHistogram = function() {
nickjillings@1868 353 // First we must get the value data
nickjillings@1868 354 if (this.valueData == null) {
nickjillings@1868 355 console.log("Error - Data not loaded");
nickjillings@1868 356 return;
nickjillings@1868 357 }
nickjillings@1868 358 // We create one plot per element, enjoy...
nickjillings@1868 359 for (var page of this.valueData.pages) {
nickjillings@1868 360 for (var element of page.elements) {
nickjillings@1868 361 // Build the chart object
nickjillings@1868 362 var chart = new this.chartObject("histogram-element-"+element.id);
nickjillings@1868 363 document.getElementById("test-pages").appendChild(chart.root);
nickjillings@1868 364 chart.data.addColumn('string','index');
nickjillings@1868 365 var histograms = [];
nickjillings@1868 366 for (var axis of element.axis) {
nickjillings@1868 367 chart.data.addColumn('number',axis.name);
nickjillings@1868 368 histograms.push(arrayHistogram(axis.values,0.125,0.0,1.0));
nickjillings@1868 369 }
nickjillings@1868 370 for (var axis of element.axis) {
nickjillings@1868 371 for (var i=0; i<histograms[0].length; i++)
nickjillings@1868 372 {
nickjillings@1868 373 var entry = [""+histograms[0][i].lt.toPrecision(2)+"-"+histograms[0][i].rt.toPrecision(3)]
nickjillings@1868 374 for (var histogram of histograms) {
nickjillings@1868 375 entry.push(histogram[i].count);
nickjillings@1868 376 }
nickjillings@1868 377 chart.data.addRow(entry);
nickjillings@1868 378 }
nickjillings@1868 379 }
nickjillings@1868 380 chart.options = {
nickjillings@1868 381 'title':'Histogram of element: '+element.id,
nickjillings@1868 382 'width':800,
nickjillings@1868 383 'height':700,
nickjillings@1868 384 'bar':{'groupWidth': '100%'}
nickjillings@1868 385 }
nickjillings@1868 386 // Draw the chart
nickjillings@1868 387 chart.chart = new google.visualization.ColumnChart(chart.chartDOM);
nickjillings@1868 388 chart.chart.draw(chart.data,chart.options);
nickjillings@1868 389 chart.buildTable();
nickjillings@1868 390 chart.writeLatex();
nickjillings@1868 391 this.charts.push(chart);
nickjillings@1868 392 }
nickjillings@1867 393 }
nickjillings@1864 394 }
nickjillings@1864 395 }