Skip to content

Commit

Permalink
Plotly for convergence; fixed term pair report
Browse files Browse the repository at this point in the history
  • Loading branch information
ihh committed Apr 5, 2016
1 parent 7e1256a commit 072c301
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ README.md: bin/wtfgenes.js
bin/wtfgenes.js -h | perl -pe 's/</&lt;/g;s/>/&gt;/g;' | perl -e 'open FILE,"<README.md";while(<FILE>){last if/<pre>/;print}close FILE;print"<pre><code>\n";while(<>){print};print"</code></pre>\n"' >temp.md
mv temp.md $@

webclient: jquery d3 web/wtfgenes.js
webclient: jquery plotly web/wtfgenes.js

jquery: web/jquery-1.12.2.min.js

d3: web/d3.v3.min.js
plotly: web/plotly-latest.min.js

web/d3.v3.min.js:
cd web; curl -O https://d3js.org/d3.v3.min.js
web/plotly-latest.min.js:
cd web; curl -O https://cdn.plot.ly/plotly-latest.min.js

web/jquery-1.12.2.min.js:
cd web; curl -O https://code.jquery.com/jquery-1.12.2.min.js
Expand Down
2 changes: 1 addition & 1 deletion bin/wtfgenes.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function runInference (genesJson) {
mcmc.logProgress()

if (logging('mixing'))
mcmc.analyzeMixing()
mcmc.logMixing()

var nSamples = samplesPerTerm * mcmc.nVariables()
if (logging('data'))
Expand Down
19 changes: 15 additions & 4 deletions lib/mcmc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
}
}

function logLogLikelihood() {
var mcmc = this
if (!mcmc.logLikelihoodTrace) {
mcmc.logLikelihoodTrace = []
mcmc.postMoveCallback.push (function (mcmc, move) {
mcmc.logLikelihoodTrace.push (mcmc.quickCollapsedLogLikelihood())
})
}
}

function logTermPairs() {
var mcmc = this
mcmc.termPairOccupancy = mcmc.models.map (function (model) {
Expand All @@ -75,7 +85,7 @@
})
}

function analyzeMixing() {
function logMixing() {
var mcmc = this
var startTime = Date.now(), moveStartMillisecs
mcmc.traceStats = { logLikelihood: [],
Expand All @@ -88,12 +98,12 @@
mcmc.traceStats.nProposedMoves[type] = 0
})
mcmc.logActiveTerms()
mcmc.logLogLikelihood()
mcmc.preMoveCallback.push (function (mcmc) {
moveStartMillisecs = (new Date).getTime()
})
mcmc.postMoveCallback.push (function (mcmc, move) {
mcmc.traceStats.moveElapsedMillisecs[move.type] += (new Date).getTime() - moveStartMillisecs
mcmc.traceStats.logLikelihood.push (mcmc.quickCollapsedLogLikelihood())
++mcmc.traceStats.nProposedMoves[move.type]
if (move.accepted)
++mcmc.traceStats.nAcceptedMoves[move.type]
Expand All @@ -103,7 +113,7 @@
summ.mcmc.samplesPerSecond = mcmc.samples / (endTime - startTime)
var points = util.iota (Math.ceil(Math.log2(mcmc.samples))).map (function(x) { return Math.pow(2,x) })
console.warn ("Computing log-likelihood autocorrelations")
summ.mcmc.logLikeAutoCorrelation = util.autocorrelation (mcmc.traceStats.logLikelihood, points)
summ.mcmc.logLikeAutoCorrelation = util.autocorrelation (mcmc.logLikelihoodTrace, points)
console.warn ("Computing term autocorrelations")
summ.mcmc.termAutoCorrelation = []
mcmc.models.forEach (function (model, m) {
Expand Down Expand Up @@ -361,7 +371,8 @@
logProgress: logProgress,
logActiveTerms: logActiveTerms,
logTermPairs: logTermPairs,
analyzeMixing: analyzeMixing,
logMixing: logMixing,
logLogLikelihood: logLogLikelihood,

run: run,
termSummary: termSummary,
Expand Down
39 changes: 30 additions & 9 deletions web/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
wtf.ui.samplesPerSec.text ((wtf.samplesPerRun / elapsedSecs).toPrecision(2))
}
wtf.lastRun = now
wtf.ui.totalSamples.text (wtf.mcmc.samples.toString())
wtf.ui.samplesPerTerm.text ((wtf.mcmc.samples / wtf.mcmc.nVariables()).toString())
if (wtf.redraw) {
var table = $('<table></table>')
Expand All @@ -67,12 +68,9 @@
if (termProb[t] > interactionThreshold)
topTerms.push (t)
})
var termPairProb, termPairMarginal
var termPairProb
if (wtf.showPairs) {
termPairProb = wtf.mcmc.termPairSummary (0, topTerms)
termPairMarginal = util.keyValListToObj (topTerms.map (function(t) {
return [t, util.sumList (util.values (termPairProb[t]))]
}))
}
table.append ($('<tr><th>Term</th><th>Probability</th>'
+ (wtf.showPairs
Expand All @@ -90,7 +88,7 @@
+ (wtf.showPairs
? (p > interactionThreshold
? topTerms.map (function(t2,i2) {
var ratio = termPairProb[t][t2] / (termPairMarginal[t] * termPairMarginal[t2])
var ratio = termPairProb[t][t2] / (termProb[t] * termProb[t2])
var rStyle = i==i2 ? blankStyle() : ratioStyle(ratio)
return '<td ' + rStyle + '>' + (i == i2 ? '' : ratioText(ratio) + '</td>')
}).join('')
Expand All @@ -107,6 +105,22 @@
}
}

function plotLogLikelihood() {
var wtf = this
Plotly.plot( wtf.ui.logLikePlot[0], [{
y: wtf.mcmc.logLikelihoodTrace }],
{ title: "Convergence",
xaxis: { title: "Number of samples" },
yaxis: { title: "Log-likelihood" } } )
setTimeout (redrawLogLikelihood.bind(wtf), 100)
}

function redrawLogLikelihood() {
var wtf = this
Plotly.redraw( wtf.ui.logLikePlot[0] )
setTimeout (redrawLogLikelihood.bind(wtf), 100)
}

function cancelStart(wtf,msg) {
alert (msg)
wtf.ui.startButton.prop('disabled',false)
Expand Down Expand Up @@ -148,24 +162,30 @@
seed: 123456789
})

wtf.mcmc.logLogLikelihood()

resumeAnalysis.call(wtf)
wtf.ui.startButton.prop('disabled',false)

wtf.ui.interButton = $('<button>Track pairs</button>')
wtf.ui.interButton = $('<button>Track co-occurence</button>')
wtf.ui.interButton.click (function() {
wtf.ui.interButton.prop('disabled',true)
wtf.mcmc.logTermPairs()
wtf.showPairs = true
if (wtf.paused)
resumeAnalysis.call(wtf)
})

wtf.ui.totalSamples = $('<span>0</span>')
wtf.ui.samplesPerTerm = $('<span>0</span>')
wtf.ui.samplesPerSec = $('<span>0</span>')
wtf.ui.mcmcStats = $('<span/>')
wtf.ui.mcmcStats.append (" ", wtf.ui.samplesPerTerm, " samples/term, ", wtf.ui.samplesPerSec, " samples/sec")
wtf.ui.mcmcStats.append (" ", wtf.ui.totalSamples, " samples (", wtf.ui.samplesPerTerm, " samples/term, ", wtf.ui.samplesPerSec, " samples/sec)")

wtf.ui.inputDiv.append (wtf.ui.interButton, wtf.ui.mcmcStats)

runMCMC.call(wtf)
plotLogLikelihood.call(wtf)
}
}

Expand Down Expand Up @@ -267,8 +287,9 @@
wtf.ui.startButton)

wtf.ui.resultsDiv = $('<div/>')
wtf.ui.logLikePlot = $('<div/>')
wtf.ui.tableParent = $('<div/>')
wtf.ui.resultsDiv.append (wtf.ui.tableParent)
wtf.ui.resultsDiv.append (wtf.ui.logLikePlot, wtf.ui.tableParent)

wtf.ui.parentDiv.append (wtf.ui.inputDiv,
'<br/>',
Expand Down
14 changes: 7 additions & 7 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<html>
<head>
<title>WTF genes?!?</title>
</head>
<body>
<h1>WTF genes?!?</h1>
<b>W</b>hat is <b>T</b>he <b>F</b>unction of these <b>genes</b>?
<title>WTF genes?</title>

<!-- JQuery -->
<script type="text/javascript" src="jquery-1.12.2.min.js"></script>

<!-- d3 -->
<script type="text/javascript" src="d3.v3.min.js"></script>
<!-- Plotly -->
<script type="text/javascript" src="plotly-latest.min.js"></script>

<!-- WTFgenes -->
<script type="text/javascript" src="wtfgenes.js"></script>
Expand All @@ -23,5 +19,9 @@ <h1>WTF genes?!?</h1>
})
</script>

</head>
<body>
<h1>WTF genes?</h1>
<b>W</b>hat is <b>T</b>he <b>F</b>unction of these <b>genes</b>?
</body>
</html>

0 comments on commit 072c301

Please sign in to comment.