#!/usr/bin/python

import matplotlib
matplotlib.use('cairo')
import matplotlib.pyplot as plt

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

from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor,GradientBoostingRegressor
from sklearn.svm import SVR
from sklearn.learning_curve import validation_curve
from sklearn.preprocessing import StandardScaler
from sklearn.grid_search import GridSearchCV

from joblib import Parallel, delayed
import joblib
from scipy.stats.mstats import mquantiles

import collection

def process_result( gid, transform=0 ):
    location = "/home/rkjha/Documents/Copy/CS365/project/data/fakedata/"
    f = location + "images_training_rev1/"+gid+".jpg"
    return collection.get_ImageFeatures(f,transform=transform)

def assessmentResult(estimator,X,y):
    predictions = estimator.predict(X)
    scaler1 = joblib.load(collection.getCurrentFolderLocation()+"/scaler1")
    predictions = scaler1.inverse_transform(predictions)
    
    predictions[predictions<1e-7] = 0
    y = scaler1.inverse_transform(y)
    MSE = (predictions - y)**2
    mse = np.mean(MSE,axis=1)
    rmse = math.sqrt(mse.mean())
    rmse2 = np.sqrt(mse)

    indices = np.argsort(mse)[::-1]
    
    for i in indices[:5]:
        if rmse2[i]>0.1:
            print rmse2[i], int(X[i,0])
        
    return rmse

def shuffle(a, b):
    assert len(a) == len(b)
    p = np.random.permutation(len(a))
    return a[p], b[p]

f = "/home/rkjha/Documents/Copy/CS365/project/data/fakedata/training_solutions_rev1.csv"
responsesId, ids = collection.read_responses( f )

scaler1 = StandardScaler()
responsesId = scaler1.fit_transform(responsesId)
joblib.dump( scaler1, collection.getCurrentFolderLocation()+"/scaler1" )

responsesId,ids = shuffle(responsesId,np.array(ids))

allPointsId = []
allResponsesId = []
transform = 0
for transform in xrange(2):
    points = [process_result(gid, transform=transform)
                                 for gid in ids]
    allPointsId.extend(points)
    allResponsesId.extend(responsesId)

allPointsId = np.array(allPointsId)
allResponsesId = np.array(allResponsesId)
    
scaler2 = StandardScaler()
allPointsId = scaler2.fit_transform(allPointsId)

joblib.dump( scaler2, collection.getCurrentFolderLocation()+"/scaler2")

forest = ExtraTreesRegressor(n_estimators=2000, min_samples_leaf=3, max_depth=60, n_jobs=-1)

forest.fit(allPointsId, allResponsesId)

if not os.path.exists(collection.getCurrentFolderLocation()):
    os.makedirs(collection.getCurrentFolderLocation())

joblib.dump(forest, collection.getCurrentFolderLocation() + "/galaxy_forest")
