# Path: implement/decisiontreemodel.py
# class DecisionTreeModel(BaseModel):
#     def __init__(self):
#         BaseModel.__init__(self)
#         self.save_final_model = False
#         self.do_cross_val = False
#         return
#     def setClf(self):
#         min_samples_split = 10
#         self.clf = DecisionTreeRegressor(random_state=0, min_samples_split= min_samples_split)
#         return
#     def after_test(self):
#         plt.show()
#         return
#     def getTunedParamterOptions(self):
# #         tuned_parameters = [{'min_samples_split': np.arange(2, 10, 1)}]
# #         tuned_parameters = [{'min_samples_split': [5, 8,10,12]}]
#         tuned_parameters = [{'min_samples_split': [10]}]
#         return tuned_parameters
# 
# Path: evaluation/sklearnmape.py
# def mean_absolute_percentage_error_scoring(estimator, X, y):
#     y_pred = estimator.predict(X)
#     return -mean_absolute_percentage_error(y, y_pred)
# 
# Path: utility/logger_tool.py
# class Logger:
#     def __init__(self, **kwargs):
#         logging.basicConfig(**kwargs)
# 
#         root = logging.getLogger()
#         ch = logging.StreamHandler(sys.stdout)
#         root.addHandler(ch)
#         return


import sys
import os
import logging
import numpy as np
from implement.decisiontreemodel import DecisionTreeModel
from sklearn.grid_search import GridSearchCV, RandomizedSearchCV
from evaluation.sklearnmape import mean_absolute_percentage_error_scoring
from utility.logger_tool import Logger
from datetime import datetime
from knnmodel import KNNModel
from utility.duration import Duration
from svmregressionmodel import SVMRegressionModel
from randomforestmodel import RandomForestModel
from gradientboostingmodel import GrientBoostingModel
from xgboost_sklearnmodel import XGBoostSklearnModel
        
        features,labels,cv = model.getFeaturesLabel()
        # do grid search
        if self.do_random_gridsearch:
            estimator = RandomizedSearchCV(model.clf, model.getTunedParamterOptions(), cv=cv, n_jobs=self.n_jobs,
                       scoring=mean_absolute_percentage_error_scoring, verbose = 500, n_iter=self.n_iter_randomsearch)
        else:
            estimator = GridSearchCV(model.clf, model.getTunedParamterOptions(), cv=cv,n_jobs=-self.n_jobs, 
                                     fit_params=model.get_fit_params(),
                       scoring=mean_absolute_percentage_error_scoring, verbose = 500)
        estimator.fit(features, labels)
        model.clf = estimator.best_estimator_
        model.save_final_model = True
        model.save_model()
        
#         model.dispFeatureImportance()
        logging.debug('estimaator parameters: {}'.format(estimator.get_params))
        logging.debug('Best parameters: {}'.format(estimator.best_params_))
        logging.debug('Best Scores: {}'.format(-estimator.best_score_))
        logging.debug('Score grid: {}'.format(estimator.grid_scores_ ))
        for i in estimator.grid_scores_ :
            logging.debug('parameters: {}'.format(i.parameters ))
            logging.debug('mean_validation_score: {}'.format(np.absolute(i.mean_validation_score)))
            logging.debug('cv_validation_scores: {}'.format(np.absolute(i.cv_validation_scores) ))

        
        
        return
    def get_model(self, model_id):
        model_dict = {}

# Path: implement/decisiontreemodel.py
# class DecisionTreeModel(BaseModel):
#     def __init__(self):
#         BaseModel.__init__(self)
#         self.save_final_model = False
#         self.do_cross_val = False
#         return
#     def setClf(self):
#         min_samples_split = 10
#         self.clf = DecisionTreeRegressor(random_state=0, min_samples_split= min_samples_split)
#         return
#     def after_test(self):
#         plt.show()
#         return
#     def getTunedParamterOptions(self):
# #         tuned_parameters = [{'min_samples_split': np.arange(2, 10, 1)}]
# #         tuned_parameters = [{'min_samples_split': [5, 8,10,12]}]
#         tuned_parameters = [{'min_samples_split': [10]}]
#         return tuned_parameters
# 
# Path: evaluation/sklearnmape.py
# def mean_absolute_percentage_error_scoring(estimator, X, y):
#     y_pred = estimator.predict(X)
#     return -mean_absolute_percentage_error(y, y_pred)
# 
# Path: utility/logger_tool.py
# class Logger:
#     def __init__(self, **kwargs):
#         logging.basicConfig(**kwargs)
# 
#         root = logging.getLogger()
#         ch = logging.StreamHandler(sys.stdout)
#         root.addHandler(ch)
#         return


# Path: implement/tunemodels.py
import sys
import os
import logging
import numpy as np
from implement.decisiontreemodel import DecisionTreeModel
from sklearn.grid_search import GridSearchCV, RandomizedSearchCV
from evaluation.sklearnmape import mean_absolute_percentage_error_scoring
from utility.logger_tool import Logger
from datetime import datetime
from knnmodel import KNNModel
from utility.duration import Duration
from svmregressionmodel import SVMRegressionModel
from randomforestmodel import RandomForestModel
from gradientboostingmodel import GrientBoostingModel
from xgboost_sklearnmodel import XGBoostSklearnModel

        
        features,labels,cv = model.getFeaturesLabel()
        # do grid search
        if self.do_random_gridsearch:
            estimator = RandomizedSearchCV(model.clf, model.getTunedParamterOptions(), cv=cv, n_jobs=self.n_jobs,
                       scoring=mean_absolute_percentage_error_scoring, verbose = 500, n_iter=self.n_iter_randomsearch)
        else:
            estimator = GridSearchCV(model.clf, model.getTunedParamterOptions(), cv=cv,n_jobs=-self.n_jobs, 
                                     fit_params=model.get_fit_params(),
                       scoring=mean_absolute_percentage_error_scoring, verbose = 500)
        estimator.fit(features, labels)
        model.clf = estimator.best_estimator_
        model.save_final_model = True
        model.save_model()
        
#         model.dispFeatureImportance()
        logging.debug('estimaator parameters: {}'.format(estimator.get_params))
        logging.debug('Best parameters: {}'.format(estimator.best_params_))
        logging.debug('Best Scores: {}'.format(-estimator.best_score_))
        logging.debug('Score grid: {}'.format(estimator.grid_scores_ ))
        for i in estimator.grid_scores_ :
            logging.debug('parameters: {}'.format(i.parameters ))
            logging.debug('mean_validation_score: {}'.format(np.absolute(i.mean_validation_score)))
            logging.debug('cv_validation_scores: {}'.format(np.absolute(i.cv_validation_scores) ))

        
        
        return
    def get_model(self, model_id):
        model_dict = {}

        model_dict[1] =DecisionTreeModel