#!/usr/bin/python

"""Look at the output of mcmc_big.py and compare the random factors for accepted and rejected steps.
"""

import math
import pylab
from gmisclib import avio

def read(fn):
	A = []
	R = []
	for l in open(fn, 'r'):
		if not l.startswith("#INFO:"):
			continue
		if ' StepMultiD: ' not in l:
			continue
		a = l.split()
		if len(a) < 6:
			continue
		if a[3] not in ['Accepted', 'Rejected']:
			continue
		if ';' not in l:
			continue
		d = avio.parse(l[l.index(';')+1:])
		p = (math.log(float(d['FORCE_SIMILAR'])),
			math.log(float(d['lmbda'])), math.log(float(d['rscale'])))
		if a[3] == 'Accepted':
			A.append(p)
		else:
			R.append(p)
	lbls = ['FORCE_SIMILAR', 'lmbda', 'rscale']
	return (A, R, lbls)


def plot(A, R, lbls):
	for (s, i, j) in [(231, 0, 1), (232, 0, 2), (233, 1, 2)]:
		pylab.subplot(s)
		xa, ya, xr, yr = [], [], [], []
		for a in A:
			xa.append(a[i])
			ya.append(a[j])
		for r in R:
			xr.append(r[i])
			yr.append(r[j])
		pylab.plot(xa, ya, 'go')
		pylab.plot(xr, yr, 'rv')
		pylab.xlabel(lbls[i])
		pylab.ylabel(lbls[j])
	for i in range(3):
		pylab.subplot(234+i)
		pylab.hist([q[i] for q in A], bins=20, color='g')
		pylab.hist([q[i] for q in R], bins=20, color='r')
		pylab.xlabel(lbls[i])
	pylab.show()


if __name__ == '__main__':
	import sys
	A, R, lbls = read(sys.argv[1])
	plot(A, R, lbls)
	for p in A:
		print 'Accepted', ' '.join([str(q) for q in p])
	for p in R:
		print 'Rejected', ' '.join([str(q) for q in p])

