from __future__ import print_function
import pandas as pd
import pickle as pk
import numpy as np
import json
import cPickle
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.grid_search import GridSearchCV
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('seaborn-dark')

def get_score(model):
    predictions = model.predict(X_test)
    
    score = 0
    for i in range(predictions.shape[0]):
        if predictions[i]==y_test.iloc[i]:
            score+=1
    print(model.score(X_test, y_test))
    print(score)

# load configuration
with open('../config.json', 'rb') as f:
    config = json.load(f)

mainImPath = config['image_dir']
pDir = config['pickle_dir']

# load pca fit
pcaFG = pk.load(open(pDir + 'pcaFG.pk', 'rb'))
# load training data
print('[INFO] using 3-dim Haralick PCA training data')
training_data = pk.load(open(pDir + 'training_data-13dimHaraFG-PCA.pd.pk', 'rb'))

Tdata = training_data.drop('breed', 1)
data = []
for i in range(Tdata.shape[0]):
    # only use the first 3 components
    data.append([i for i in Tdata.iloc[i, :3]])

labels = training_data.breed

X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.33, stratify=labels, random_state=42)


# first try of SVC
clf = SVC(random_state=42)
params = {
          'C': [0.05, 0.1, 0.2, 0.5, 1.0, 2.0],
         }
print('[INFO] SVC gridsearching', params)
model = GridSearchCV(clf, params, cv=3, refit=False)
model.fit(data, labels)
print('[INFO] best hyperparameters: {}'.format(model.best_params_))
model = SVC(C=0.05, random_state=42) # best params
model.fit(X_train, y_train)
print('[INFO] svc score:')
get_score(model)

# try further refinement of the SVC
params = {
          'kernel': ['rbf', 'sigmoid'],
          'C': [0.05, 0.1, 0.2, 0.5, 1.0, 2.0],
          'gamma': ['auto', 1, 1/2.]
         }
print('[INFO] SVC gridsearching', params)
model = GridSearchCV(clf, params, cv=3, refit=False)
model.fit(data, labels)
print('[INFO] best hyperparameters: {}'.format(model.best_params_))
model = SVC(C=0.5, gamma=1, random_state=42) # best params
model.fit(X_train, y_train)
print('[INFO] svc score:')