#!/nfsmnt/malaria0/ssi/visintainer/local/bin/python

from numpy import *
from optparse import OptionParser
from mlpy import *
  
# Command line parsing
parser = OptionParser()
parser.add_option("-d", "--data", metavar = "FILE", action = "store", type = "string",
                  dest = "data", help = "data - required")
parser.add_option("-s", "--standardize", action = "store_true", default = False,
                  dest = "stand", help = "standardize data")
parser.add_option("-n", "--normalize", action = "store_true", default = False,
                  dest = "norm", help = "normalize data")

parser.add_option("-k", action = "store", type = "int",
                  dest = "k", help = "k for k-fold cross validation")
parser.add_option("-c", action = "store", type = "int", nargs = 2, metavar = "SETS PAIRS",
                  dest = "c", help = "sets and pairs for monte carlo cross validation")
parser.add_option("-S", "--stratified", action = "store_true", default = False,
                  dest = "strat", help = "for stratified cv")

parser.add_option("-m", "--min", action = "store", type = "float",
                  dest = "min", help = "min value for alpha parameter [default %default]", default = -6)
parser.add_option("-M", "--max", action = "store", type = "float",
                  dest = "max", help = "max value for alpha parameter [default %default]", default = 6)
parser.add_option("-p", "--steps", action = "store", type = "int",
                  dest = "steps", help = "steps for alpha parameter [default %default]", default = 13)
parser.add_option("-e", "--scale", action = "store", type = "string",
                  dest = "scale",  help = "scale for alpha parameter: 'lin' or 'log' [default %default]", default = "log")

parser.add_option("-l", "--lists", action = "store_true", default = False,
                  dest = "lists", help = "Canberra distance indicator")
parser.add_option("-a", "--auc", action = "store_true", default = False,
                  dest = "auc", help = "Wmw_auc metric computation")

(options, args) = parser.parse_args()
if not options.data:
    parser.error("option -d [data] is required")
if not (options.k or options.c):
    parser.error("option -k (k-fold) or -c (monte carlo) for resampling is required")
if (options.k and options.c):
    parser.error("option -k (k-fold) and -c (monte carlo) are mutually exclusive")
if not options.scale in ["lin", "log"]:
    parser.error("option -e (scale) should be 'lin' or 'log'")
    
# Alpha values
if options.scale == 'lin':
    alpha = linspace(options.min, options.max, options.steps)
elif options.scale == 'log':
    alpha = logspace(options.min, options.max, options.steps)

# Data
x, y = data_fromfile(options.data)
if options.stand:
    x = data_standardize(x)
if options.norm:
    x = data_normalize(x)

print "samples:", x.shape[0]
print "features:", x.shape[1]

# Resampling
if options.strat:
    if options.k:
        print "stratified %d-fold cv" % options.k
        res = kfoldS(cl = y, sets = options.k)
    elif options.c:
        print "stratified monte carlo cv (%d sets, %d pairs)" %(options.c[0], options.c[1])
        res = montecarloS(cl = y, sets = options.c[0], pairs = options.c[1])
else:
    if options.k:
        print "%d-fold cv" % options.k
        res = kfold(nsamples = y.shape[0], sets = options.k)
    elif options.c:
        print "monte carlo cv (%d sets, %d pairs)" %(options.c[0], options.c[1])
        res = montecarlo(nsamples = y.shape[0], sets = options.c[0], pairs = options.c[1])

if options.lists:
    R = Ranking(method='onestep')
    lp = empty((len(res), x.shape[1]), dtype = int)

# Compute
for a in alpha:
    s = Srda(alpha = a) # Initialize srda class
    ERR = 0.0 # Initialize error
    MCC = 0.0 # Initialize mcc
    if options.auc:
        AUC = 0.0 # Initialize auc


    for i, r in enumerate(res):
        xtr, ytr, xts, yts = x[r[0]], y[r[0]], x[r[1]], y[r[1]]
        s.compute(xtr, ytr)
        p = s.predict(xts)

        if options.lists:
            lp[i] = R.compute(xtr, ytr, s)[0].argsort()
        
        ERR += err(yts, p)
        MCC += mcc(yts, p)
        if options.auc:
            AUC += wmw_auc(yts, p)

    ERR /= float(len(res))
    MCC /= float(len(res))
    if options.auc:
        AUC /= float(len(res))
    else:
        AUC = nan


    if options.lists:
        DIST = canberra(lp, x.shape[1])
    else:
        DIST = nan

    print "alpha %e: error %f, mcc %f, auc %f, dist %f" \
	  % (a, ERR, MCC, AUC, DIST)
