#! python

"""This little script helps decide which targets are predicted most
successfully."""


import os
import sys
import pylab
from gmisclib import avio
from gmisclib import dictops


def process_one_file(fn, accumulator):
	assert isinstance(accumulator, dictops.dict_of_averages)
	data, c = avio.read(open(fn, 'r'))
	for datum in data:
		if 'effect_size2' in datum:
			accumulator.add((int(datum['N']), datum['target']), float(datum['effect_size2']))


def process_dir(dn):
	accumulator = dictops.dict_of_averages()
	for f in os.listdir(dn):
		fn = os.path.join(dn, f)
		process_one_file(fn, accumulator)
	ns = set()
	targets = set()
	for (n,target) in accumulator.keys():
		ns.add(n)
		targets.add(target)
	wins = dictops.dict_of_accums()
	for n in ns:
		for t1 in targets:
			for t2 in targets:
				if t1 < t2:
					if accumulator.get_avg((n,t1)) > accumulator.get_avg((n,t2)):
						wins.add(t1, 1)
					else:
						wins.add(t2, 1)
	print 'Winners:'
	for (k, v) in sorted(wins.items(), key=lambda x: x[1]):
		print '%s wins %d times' % (k, v)
	

if __name__ == '__main__':
	process_dir(sys.argv[1])
