from sklearn import grid_search
from sklearn.cross_validation import StratifiedShuffleSplit
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from time import time

import numpy as np
import pandas as pd
import sys

"""
Explanation: Support vector machine (SVM) calibration experiment
End of explanation
"""


#Test each array of features in the training dataset
if __name__ == '__main__':
    
    csv_file_features = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 
                         [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13]]
    
    estimators_array = []
    
    print('Order of results')
    print('Training score, Test score, Time, Features, SVM parameters\n')
    
    C_range = [0.01, 0.1, 1, 10, 100]
    gamma_range = [0.01, 0.1, 1, 10, 100]

    #Test for all the features sets
    for i in csv_file_features:
        stdsc = StandardScaler()
        df = pd.read_csv("../../Dataset/Train/EEG_Train_Sorted.csv")

        #Separate Class labels anda data
        X = df.ix[:, i]
        y = df['Class']
        
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
        
        #Standarize data
        X_train_std = stdsc.fit_transform(X_train)
        X_test_std = stdsc.transform(X_test)
        
        #Create SVM and define parameters
        svm = SVC()
        param_grid = [{'C': C_range, 
                        'gamma': gamma_range, 
                        'kernel': ['rbf'], 
                        'decision_function_shape': ['ovr'], 
                        'random_state': [0]}]
            
        #Set Grid Params
        init = time()
        gscv = grid_search.GridSearchCV(svm, 
                                       param_grid, 
                                       n_jobs=6, 
                                       cv=StratifiedShuffleSplit(y=y_train,  
                                                                 n_iter=5, 
                                                                 test_size=0.4, 
                                                                 random_state=0))
        #Test classifier
        gscv.fit(X_train_std, y_train)
        sys.exit("Error message")
        #Save our estimator
        estimators_array.append(gscv)
        
        #Report testing results
        print('{:.5f}, {:.5f}, {:.5f}s, {}, {}'.format(gscv.best_score_, gscv.score(X_test_std, y_test), time() - init, i, gscv.best_params_))


"""
Explanation: SVM calibration
Here are defined the features to be evaluated and the parameters of the algorithm to be tested with, the best classifier is stored in 'estimators_array' for each one of the 'csv_file_features' values
End of explanation
"""


#Get best estimator parameters
print (estimators_array[0])

"""
Explanation: Now we check the parameters of the best estimator
End of explanation
"""


