annotate analysis/analysis.js @ 2140:14edd6b2557a

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