#!/usr/bin/python

from time import time
import pylab as pl
import logging

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import RandomizedPCA
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.grid_search import GridSearchCV

import sys, os
import cv2
import numpy as np
import scipy.ndimage as nd
import csv

from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor

from joblib import Parallel, delayed
import joblib

import collection

import logging
from time import time

from numpy.random import RandomState

from sklearn import decomposition

from scipy.stats.mstats import mquantiles

tnail = "img"

def process_result(gid, transform=0):
    location = "/home/rkjha/Documents/Copy/CS365/project/data/fakedata/"
    f = location + "images_training_rev1/"+gid+".jpg"
    if tnail=="img":
        return collection.get_ImageFeatures(f,tiny_img=True,transform=transform)
    elif tnail=="grad":
        return collection.get_ImageFeatures(f,tiny_grad=True,transform=transform)
    
f = "/home/rkjha/Documents/Copy/CS365/project/data/fakedata/training_solutions_rev1.csv"
responsesId, ids = collection.read_responses( f )

mapping = collection.getNameClasses()

for Class in xrange(1,12):
    classes = np.nonzero(mapping==Class)[0]

    X = []
    Y = []
    svm_class = 0
    
    for c in classes:
        q = 0.95
        if Class == 8 or Class == 11:
            q = 0.98
        threshold = mquantiles( responsesId[:,c], q )
        
        selectionID = np.nonzero(responsesId[:,c]>=threshold)[0]

        sids = []

        for i in selectionID:
            sids.append(ids[i])

        for transform in [0]:
            points = [process_result(gid,transform=transform)
                                         for gid in sids]
        X.extend(points)
        Y.extend([svm_class for i in xrange(len(points))])
        svm_class += 1

    X = np.array(X,dtype='float')
    Y = np.array(Y,dtype='int')
        
    n_components = 50

    t0 = time()
    pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X)
    t0 = time()
    X_pca = pca.transform(X)

    joblib.dump(pca, collection.getCurrentFolderLocation()+"/pca_"+tnail+"_Class"+ str(Class)+"_")

    clf = SVC( kernel='rbf', class_weight='auto', probability=True, C=50000.0, gamma=5e-05)
    clf.fit(X_pca, Y)
    joblib.dump(clf, collection.getCurrentFolderLocation()+"/pca_"+tnail+"_SVM_Class" + str(Class)+"_")
exit(0)
