
"""Reads in the output of l_classifier.py and prepares it for display.
NOTE: this only makes sense for two classes.
"""

import numpy
import read_classified


def ssv(a):
	return ' '.join( [ '%g' % x for x in a ] )


def doit(fn):
	fd = open(fn, 'r')
	hdr, classifiers = read_classified.read_classes(fd)
	dbar = None
	for classifier in classifiers:
		dsum = None
		for model in classifier.models.values():
			if dsum is None:
				dsum = numpy.array(model.dir, numpy.double)
			else:
				dsum += model.dir
		dsum /= len(classifier.models)
		for model in classifier.models.values():
			model.dir -= dsum

		if dbar is None:
			dbar = numpy.array(classifier.models['P'].dir, numpy.double)
		else:
			dbar += classifier.models['P'].dir

	print fn, hdr['K'], ssv(dbar/len(classifiers))



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