#!/usr/bin/env python
import arff
import numpy as np
import sys
from sklearn import preprocessing
from sklearn.linear_model import RidgeClassifier
from sklearn.feature_selection import RFE
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import StratifiedKFold
from sklearn import cross_validation
from sklearn import metrics

#Parameters:
filename = sys.argv[1]
n_featuresToSelect = [2,3,5,10,20]
solvers = ['svd','cholesky','sparse_cg','lsqr','sag']
verboseLevel=10
n_jobs=10
n_crossValidation=10
n_accuracy=10
n_folds=10
# 10% removal of features
stepSize=0.1

print "Load dataset"
# Load dataset
arffDecoder = arff.ArffDecoder()
dataset = arffDecoder.decode(open(filename, 'rb'), encode_nominal=True)

print "Preprocess dataset"
# Get categorical features
categoricals = []
# NOTE: skip last (class) 'feature'
for feature in range(0,len(dataset['attributes'])-1):
    if isinstance(dataset['attributes'][feature][1], list):
        categoricals.append(feature)
print "Categorical indices: {0}".format(categoricals)

# Apply OneHotEncoder
oneHotEncoder = preprocessing.OneHotEncoder(categorical_features=categoricals, sparse=False)
print "Number of features: {0}".format(len(dataset['data'][0]))
print "Number of samples: {0}".format(len(dataset['data']))
binData = oneHotEncoder.fit_transform(np.array(dataset['data']))
print "n-values: {0}".format(oneHotEncoder.n_values_)
print "feature indices: {0}".format(oneHotEncoder.feature_indices_)
print "Number of binarised features: {0}".format(len(binData[0]))
print "Number of binarised samples: {0}".format(len(binData))

# Setting up input and outputs
inputs = binData[:,:-1]
output = binData[:,-1]

print "Start grid search"
# Setup experimental pipeline
scaler = preprocessing.RobustScaler()
classifier = RidgeClassifier(alpha=1.0, fit_intercept=True, normalize=False, copy_X=True, max_iter=None, tol=0.001, class_weight=None, solver='auto', random_state=None)
selector = RFE(classifier,n_features_to_select=n_featuresToSelect[0],step=stepSize)
pipeline = Pipeline([("scaler",scaler),("RFE",selector),("classifier",classifier)])
paramGrid = dict(RFE__n_features_to_select=n_featuresToSelect, classifier__solver=solvers)

# Do grid search
gridSearch = GridSearchCV(pipeline,param_grid=paramGrid,verbose=verboseLevel,n_jobs=n_jobs,cv=n_crossValidation)
gridSearch.fit(inputs,output)
estimator = gridSearch.best_estimator_
print "Results: "
print "Selected features: {0}".format(estimator.named_steps['RFE'].n_features_to_select)
print "Solver: {0}".format(estimator.named_steps['classifier'].solver)

# Calculate accuracies
print "Calculate accuracies"
accuracy = []
for count in range(0,n_accuracy):
    cv = StratifiedKFold(output,n_folds=n_folds,shuffle=True)
    predicted = cross_validation.cross_val_predict(estimator,inputs,output,cv=cv,verbose=verboseLevel,n_jobs=n_jobs,)
    score = metrics.accuracy_score(output,predicted,normalize=True)
    accuracy.append(score)
print "Accuracy array: {0}".format(accuracy)
print "Cross-validation accuracy of final model {0}".format(np.mean(accuracy))
