#!/usr/bin/env python
from sys import argv
from os import popen, system, path, rename
from string import atof, join
from svmprob import getarg

if len(argv) < 3:
	print """Usage:
	%s [-couplers {ht,pkpd,markov,*minpair,vote,ass}][-s rndseed]
	[-pcv platt_cv_fold][-cv full_fold]<train_file> <testing_file>
	this program will automatically determine best (c,g)""" % argv[0]
	raise SystemExit
#processing argument
train_pathname = argv[-2]
test_pathname = argv[-1]
log_pathname = path.split(train_pathname)[1] + '-cv'
root_path = path.split(argv[0])[0]
couplers_list = getarg('-couplers','minpair').split(',')
rndseed = int(getarg('-s', 0))
platt_cv_folds = int(getarg('-pcv', 5))
full_cv_folds = int(getarg('-cv', 5))

#cleanup last log
cmd = 'mv %s %s-last >& /dev/null; rm -f %s.*' % ((log_pathname,)*3)
system(cmd)

#model selection for all methods simultaneously
cmd = "%s/grid.py -out /dev/null -png /dev/null -svmtrain %s/multiprob.py -couplers %s -base %s -cv %d -logfile %s -seed %d  -svm \"-v %d\" %s" %\
	(root_path, root_path, join(couplers_list,','), 'svmplatt', full_cv_folds, log_pathname+'.@host', rndseed, platt_cv_folds, train_pathname)
print "Cross validation...[%s]" % cmd
system(cmd)

#merge data
cmd = 'cat %s.* > %s ; rm %s.*' % ((log_pathname,)*3)
print "mergin files:[%s]" % cmd
system(cmd)

#find best parameter for all methods
def sign(x):
	if x==0.0:
		return 0
	elif x>0:
		return 1
	else:
		return -1 
def cmpfun(x, y):
	#cost, gamma, accuracy, mse, logloss, method
	cx, gx, ax, sx, lx, mx = x.split()[0:6]
	cy, gy, ay, sy, lx, my = y.split()[0:6]
	if   mx!=my:		#methods ordered as listed
		return couplers_list.index(mx) - couplers_list.index(my)
	elif ax!=ay:	#accuracy larger, first
		return sign(atof(ay) - atof(ax))
	elif cx!=cy:	#c smaller, first
		return sign(atof(cx) - atof(cy))
	else:			#gamma larger, fisrt
		return sign(atof(gy) - atof(gx))

orderlog = open(log_pathname).readlines()
orderlog.sort(cmpfun)

#test with selection parameter
counter = 0
for coupler in couplers_list:
	while orderlog[counter].find(coupler) == -1:
		counter = counter + 1
	c,g = map(atof, orderlog[counter].split()[0:2])
	print 'Best (log2C,log2g) for ', coupler, c, g
	cmd = "%s/multiprob.py -couplers %s -base %s -logfile %s -seed %d  -svm \"-v %d -c %f -g %f\" -T %s %s" %\
	(root_path, coupler, 'svmplatt', log_pathname+'-test', rndseed, platt_cv_folds, 2**c, 2**g, test_pathname, train_pathname)
	print 'Testing [%s]:' % cmd
	system(cmd)
