From 46a1c7461b4ce5f1bfc165b16e25728dfd439a2d Mon Sep 17 00:00:00 2001 From: James Cook Date: Wed, 7 Mar 2012 15:55:24 -0800 Subject: [PATCH] GraphProperties: log scale. --- src/recommend/analysis/GraphProperties.java | 11 ++++-- viz/GraphProperties.js | 44 +++++++++++++++++++-- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/recommend/analysis/GraphProperties.java b/src/recommend/analysis/GraphProperties.java index 865fb92..589703a 100644 --- a/src/recommend/analysis/GraphProperties.java +++ b/src/recommend/analysis/GraphProperties.java @@ -45,10 +45,13 @@ void report(String msg) { appendHtml("

" + StringEscapeUtils.escapeHtml(msg) + "

\n"); } - void reportPlot(DoubleMatrix1D values) { + void reportPlot(DoubleMatrix1D values, boolean logX, boolean logY) { appendHtml("
"); - for (int i = 0; i < values.size(); ++i) - appendHtml(" " + values.get(i)); + if (logX) appendHtml("s:logX "); + if (logY) appendHtml("s:logY "); + for (int i = 0; i < values.size(); ++i) { + appendHtml((0 == i ? "" : " ") + i + "," + (values.get(i) + 2.0)); + } appendHtml("
\n"); } @@ -73,7 +76,7 @@ void dynMain(String[] argv) throws Throwable { DoubleMatrix1D termFreqs = algebra.mult(termDoc, ones(numDocs)); report("Sorted term frequencies:"); - reportPlot(termFreqs.viewSorted()); + reportPlot(termFreqs.viewSorted(), false, true); endReport(); } diff --git a/viz/GraphProperties.js b/viz/GraphProperties.js index defd9bc..697a5d8 100644 --- a/viz/GraphProperties.js +++ b/viz/GraphProperties.js @@ -1,12 +1,48 @@ var GraphProperties = (function(){ - main = function() { + var logt = function (x) { return Math.log(x); }; + var expt = function (x) { return Math.exp(x); }; + var logTicks = function(axis) { + var t; + var res = []; + var logMin = Math.log(axis.min); + var logMax = Math.log(axis.max); + for (t = 0.0; t <= 1.0; t += 0.25) { + res.push(Math.exp(logMin * (1.0 - t) + logMax * t)); + } + return res; + } + + var main = function() { var toPlot = $(".plot_me"); toPlot.css("width", "512px"); toPlot.css("height", "256px"); toPlot.each(function (index, div) { - valueStrs = $(div).text().split(" ").filter(function (x) { return 0 != x.length; }); - values = valueStrs.map(function (x, index) { return [index, parseFloat(x)]; }); - $.plot($(div), [values]); + var pointStrs = $(div).text().split(" "); + var points = []; + var mins = [Infinity, Infinity]; + var maxs = [-Infinity, -Infinity]; + var logX = false; + var logY = false; + var i; + for (i = 0; i < pointStrs.length; ++i) { + s = pointStrs[i]; + if ("s:logX" == s) + logX = true; + else if ("s:logY" == s) + logY = true; + else { + p = s.split(",").map(function (x, i) { + mins[i] = Math.min(mins[i], x); + maxs[i] = Math.max(maxs[i], x); + return parseFloat(x); + }); + points.push(p); + } + } + $.plot($(div), [points], { + xaxis: logX ? {transform: logt, inverseTransform: expt, min: mins[0], max: maxs[0], ticks: logTicks} : {}, + yaxis: logY ? {transform: logt, inverseTransform: expt, min: mins[1], max: maxs[1], ticks: logTicks} : {} + }); }); }