annotate analysis/analysis.js @ 1868:ce5596e255d1

Created mean test/page plots, element histogram plots. Latex and tables included. Can download charts as PNG
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Wed, 24 Feb 2016 13:03:08 +0000
parents d9e9dd3cccf2
children cd064dc97583
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@1868 118 this.handleEvent = function() {
nickjillings@1868 119 // Only used to handle the chart.event.addListener(this,'ready') callback
nickjillings@1868 120 this.downloadDOM.innerHTML = '<a href="' + this.chart.getImageURI() + '">Download</a>';
nickjillings@1868 121 }
nickjillings@1868 122
nickjillings@1868 123 this.root.appendChild(this.chartDOM);
nickjillings@1868 124 this.root.appendChild(this.tableDOM);
nickjillings@1868 125 this.root.appendChild(this.latexDOM);
nickjillings@1868 126 this.root.appendChild(this.print);
nickjillings@1868 127 this.print.textContent = "Download";
nickjillings@1868 128 this.print.addEventListener("click",this);
nickjillings@1868 129 this.root.appendChild(this.downloadDOM);
nickjillings@1868 130 this.buildTable = function() {
nickjillings@1868 131 var table = document.createElement("table");
nickjillings@1868 132 table.border = "1";
nickjillings@1868 133 for (var rowIndex=0; rowIndex<this.data.If.length; rowIndex++) {
nickjillings@1868 134 var row = document.createElement("tr");
nickjillings@1868 135 table.appendChild(row);
nickjillings@1868 136 var rowTitle = document.createElement("td");
nickjillings@1868 137 rowTitle.textContent = this.data.If[rowIndex].label;
nickjillings@1868 138 row.appendChild(rowTitle);
nickjillings@1868 139 for (var cIndex=0; cIndex<this.data.cc.length; cIndex++) {
nickjillings@1868 140 var column = document.createElement("td");
nickjillings@1868 141 column.textContent = this.data.cc[cIndex][rowIndex].tf;
nickjillings@1868 142 row.appendChild(column);
nickjillings@1868 143 }
nickjillings@1868 144 }
nickjillings@1868 145 this.tableDOM.appendChild(table);
nickjillings@1868 146 };
nickjillings@1868 147 this.writeLatex = function() {
nickjillings@1868 148 var root = document.createElement("div");
nickjillings@1868 149 root.className = "code";
nickjillings@1868 150 var holder = document.createElement("pre");
nickjillings@1868 151 // Table start
nickjillings@1868 152 var start = document.createElement("p");
nickjillings@1868 153 start.textContent = "\\" + "begin{tabular}{|l|";
nickjillings@1868 154 holder.appendChild(start);
nickjillings@1868 155 for (var i=0; i<this.data.cc.length; i++) {
nickjillings@1868 156 start.textContent = start.textContent+"c|";
nickjillings@1868 157 }
nickjillings@1868 158 // Now write the rows:
nickjillings@1868 159 for (var rIndex=0; rIndex<this.data.If.length; rIndex++) {
nickjillings@1868 160 var row = document.createElement("p");
nickjillings@1868 161 row.textContent = this.data.If[rIndex].label.concat(" & ");
nickjillings@1868 162 for (var cIndex=0; cIndex<this.data.cc.length; cIndex++) {
nickjillings@1868 163 row.textContent = row.textContent.concat(this.data.cc[cIndex][rIndex].tf);
nickjillings@1868 164 if (cIndex < this.data.cc.length-1) {
nickjillings@1868 165 row.textContent = row.textContent.concat(" & ");
nickjillings@1868 166 }
nickjillings@1868 167 }
nickjillings@1868 168 holder.appendChild(row);
nickjillings@1868 169 }
nickjillings@1868 170 // Table end
nickjillings@1868 171 var end = document.createElement("p");
nickjillings@1868 172 end.textContent = "\\" + "end{tabular}";
nickjillings@1868 173 holder.appendChild(end);
nickjillings@1868 174 root.appendChild(holder);
nickjillings@1868 175 this.latexDOM.appendChild(root);
nickjillings@1868 176 }
nickjillings@1868 177 }
nickjillings@1868 178
nickjillings@1868 179 this.clear = function() {
nickjillings@1868 180 var inject = document.getElementById("test-pages");
nickjillings@1868 181 for (var chart of this.charts) {
nickjillings@1868 182 inject.removeChild(chart.root);
nickjillings@1868 183 }
nickjillings@1868 184 this.charts = [];
nickjillings@1868 185 }
nickjillings@1868 186
nickjillings@1868 187 this.drawTestMean = function() {
nickjillings@1868 188 // This draws one bargraph per axis with every test element on
nickjillings@1868 189 if (this.valueData == null) {
nickjillings@1868 190 console.log("Error - Data not loaded");
nickjillings@1868 191 return;
nickjillings@1868 192 }
nickjillings@1868 193 var chartList = [];
nickjillings@1868 194
nickjillings@1868 195 // Create the data table
nickjillings@1868 196 for (var page of this.valueData.pages) {
nickjillings@1868 197 for (var element of page.elements) {
nickjillings@1868 198 for (var axis of element.axis) {
nickjillings@1868 199 // Find the axis
nickjillings@1868 200 var axisChart = chartList.find(function(element,index,array){
nickjillings@1868 201 if (element.name == this) {return true;} else {return false;}
nickjillings@1868 202 },"mean-test-"+axis.id);
nickjillings@1868 203 if (axisChart == null) {
nickjillings@1868 204 axisChart = new this.chartObject("mean-test-"+axis.id);
nickjillings@1868 205 axisChart.options = {
nickjillings@1868 206 'title':'Mean of axis: '+axis.name,
nickjillings@1868 207 'width':window.innerWidth*0.9,
nickjillings@1868 208 'height':(window.innerWidth*0.9)/1.77
nickjillings@1868 209 }
nickjillings@1868 210 axisChart.data.addColumn('string','id');
nickjillings@1868 211 axisChart.data.addColumn('number',axis.name);
nickjillings@1868 212 chartList.push(axisChart);
nickjillings@1868 213 document.getElementById("test-pages").appendChild(axisChart.root);
nickjillings@1868 214 }
nickjillings@1868 215 var mean = arrayMean(axis.values);
nickjillings@1868 216 axisChart.data.addRow([element.id,mean]);
nickjillings@1868 217 }
nickjillings@1868 218 }
nickjillings@1868 219 }
nickjillings@1868 220
nickjillings@1868 221 // Build and push charts
nickjillings@1868 222 for (var chart of chartList) {
nickjillings@1868 223 chart.chart = new google.visualization.ColumnChart(chart.chartDOM);
nickjillings@1868 224 chart.chart.draw(chart.data,chart.options);
nickjillings@1868 225 chart.buildTable();
nickjillings@1868 226 chart.writeLatex();
nickjillings@1868 227 this.charts.push(chart);
nickjillings@1868 228 }
nickjillings@1868 229 }
nickjillings@1868 230
nickjillings@1868 231 this.drawPageMean = function() {
nickjillings@1864 232 // First we must get the value data
nickjillings@1867 233 if (this.valueData == null) {
nickjillings@1867 234 console.log("Error - Data not loaded");
nickjillings@1867 235 return;
nickjillings@1867 236 }
nickjillings@1867 237 // We create one plot per page
nickjillings@1867 238 for (var page of this.valueData.pages) {
nickjillings@1868 239
nickjillings@1868 240 // Create the chart resulting point
nickjillings@1868 241 var chart = new this.chartObject("mean-page-"+page.id);
nickjillings@1868 242 document.getElementById("test-pages").appendChild(chart.root);
nickjillings@1867 243
nickjillings@1867 244 // Create the data table
nickjillings@1868 245 chart.data.addColumn('string','id');
nickjillings@1867 246 // Get axis labels
nickjillings@1867 247 for (var axis of page.elements[0].axis) {
nickjillings@1868 248 chart.data.addColumn('number',axis.name);
nickjillings@1867 249 }
nickjillings@1867 250 var rows = []; // Rows is an array of tuples [col1, col2, col3 ... colN];
nickjillings@1867 251 for (var element of page.elements) {
nickjillings@1867 252 var entry = [element.id];
nickjillings@1867 253 for (var i=0; i<page.elements[0].axis.length; i++) {
nickjillings@1867 254 var mean =0;
nickjillings@1867 255 if (i < element.axis.length) {
nickjillings@1867 256 var axis = element.axis[i];
nickjillings@1868 257 mean = arrayMean(axis.values);
nickjillings@1867 258 }
nickjillings@1867 259 entry.push(mean);
nickjillings@1867 260 }
nickjillings@1867 261 rows.push(entry);
nickjillings@1867 262 }
nickjillings@1868 263 chart.data.addRows(rows);
nickjillings@1868 264 chart.options = {
nickjillings@1868 265 'title':'Mean of page: '+page.id,
nickjillings@1868 266 'width':800,
nickjillings@1868 267 'height':700
nickjillings@1868 268 }
nickjillings@1867 269 // Draw the chart
nickjillings@1868 270 chart.chart = new google.visualization.ColumnChart(chart.chartDOM);
nickjillings@1868 271 chart.chart.draw(chart.data,chart.options);
nickjillings@1868 272 chart.buildTable();
nickjillings@1868 273 chart.writeLatex();
nickjillings@1868 274 this.charts.push(chart);
nickjillings@1868 275 }
nickjillings@1868 276 }
nickjillings@1868 277
nickjillings@1868 278 this.drawElementHistogram = function() {
nickjillings@1868 279 // First we must get the value data
nickjillings@1868 280 if (this.valueData == null) {
nickjillings@1868 281 console.log("Error - Data not loaded");
nickjillings@1868 282 return;
nickjillings@1868 283 }
nickjillings@1868 284 // We create one plot per element, enjoy...
nickjillings@1868 285 for (var page of this.valueData.pages) {
nickjillings@1868 286 for (var element of page.elements) {
nickjillings@1868 287 // Build the chart object
nickjillings@1868 288 var chart = new this.chartObject("histogram-element-"+element.id);
nickjillings@1868 289 document.getElementById("test-pages").appendChild(chart.root);
nickjillings@1868 290 chart.data.addColumn('string','index');
nickjillings@1868 291 var histograms = [];
nickjillings@1868 292 for (var axis of element.axis) {
nickjillings@1868 293 chart.data.addColumn('number',axis.name);
nickjillings@1868 294 histograms.push(arrayHistogram(axis.values,0.125,0.0,1.0));
nickjillings@1868 295 }
nickjillings@1868 296 for (var axis of element.axis) {
nickjillings@1868 297 for (var i=0; i<histograms[0].length; i++)
nickjillings@1868 298 {
nickjillings@1868 299 var entry = [""+histograms[0][i].lt.toPrecision(2)+"-"+histograms[0][i].rt.toPrecision(3)]
nickjillings@1868 300 for (var histogram of histograms) {
nickjillings@1868 301 entry.push(histogram[i].count);
nickjillings@1868 302 }
nickjillings@1868 303 chart.data.addRow(entry);
nickjillings@1868 304 }
nickjillings@1868 305 }
nickjillings@1868 306 chart.options = {
nickjillings@1868 307 'title':'Histogram of element: '+element.id,
nickjillings@1868 308 'width':800,
nickjillings@1868 309 'height':700,
nickjillings@1868 310 'bar':{'groupWidth': '100%'}
nickjillings@1868 311 }
nickjillings@1868 312 // Draw the chart
nickjillings@1868 313 chart.chart = new google.visualization.ColumnChart(chart.chartDOM);
nickjillings@1868 314 chart.chart.draw(chart.data,chart.options);
nickjillings@1868 315 chart.buildTable();
nickjillings@1868 316 chart.writeLatex();
nickjillings@1868 317 this.charts.push(chart);
nickjillings@1868 318 }
nickjillings@1867 319 }
nickjillings@1864 320 }
nickjillings@1864 321 }