# Path: wallace/predictive_models/predictive_model.py
# class PredictiveModel(object):
#     def __init__(self, settings, parameter_set, dependent_variable, independent_variables):
#         self.parameter_set = parameter_set
#         self.settings = settings
#         self.dependent_variable = dependent_variable
#         self.independent_variables = independent_variables
# 
#         self._validate_parameter_set(self.parameter_set)
# 
#     def model_name(self):
#         return self.__class__.__name__
# 
#     def train(self, dataset):
#         raise NotImplementedError()
# 
#     def get_dependent_variable_data(self, dataset):
#         return dataset.get_filtered_column(self.dependent_variable)
# 
#     def get_independent_variable_data(self, dataset):
#         return dataset.get_filtered_matrix(self.independent_variables)
# 
#     def get_parameters(self):
#         params = {}
#         for parameter_name in self.parameter_set.validity_check.list_parameter_names():
#             params[parameter_name] = self.parameter_set.get(parameter_name)
# 
#         return params
# 
#     @classmethod
#     def validity_check(klass):
#         return ParametersGeneralValidityCheck()
# 
#     def _validate_parameter_set(self, parameter_set):
#         for parameter in self.validity_check().list_parameter_names():
#             if not parameter_set.has_parameter(parameter):
#                 raise AssertionError("The model's parameter set does not have the required parameter '%s'." % parameter)
# 
# class TrainedPredictiveModel(object):
#     def __init__(self, predictive_model):
#         self.predictive_model = predictive_model
# 
#     def model_type(self):
#         return self.predictive_model.__class__.__name__
# 
#     def predict(self, dataset):
#         raise NotImplementedError()
# 
#     def predict_and_evaluate_fitness(self, dataset, evaluation_method):
#         predicted_results = self.predict(dataset)
#         actual_results = self.predictive_model.get_dependent_variable_data(dataset)
#         return evaluation_method.evaluate_fitness(predicted_results, actual_results)
# 
# Path: wallace/categorical_variable_encoder.py
# class CategoricalVariableEncoder(object):
#     def convert_categorical_variables(self, data_matrix, category_indices, category_value_mapping=None):
#         if len(category_indices) == 0:
#             return data_matrix
# 
#         if category_value_mapping == None:
#             category_value_mapping = self.get_category_value_mapping(data_matrix, category_indices)
# 
#         for i in xrange(len(data_matrix)):
#             for category_index in category_indices:
#                 current_data_value = data_matrix[i][category_index]
#                 updated_data_value = category_value_mapping[category_index][current_data_value]
#                 data_matrix[i][category_index] = updated_data_value
# 
#         return data_matrix
# 
#     def get_category_value_mapping(self, data_matrix, category_indices):
#         categories = self.get_category_values(data_matrix, category_indices)
# 
#         category_value_mapping = {}
#         for category_index, values_set in categories.iteritems():
#             category_value_mapping[category_index] = self.create_value_map(values_set)
# 
#         return category_value_mapping
# 
#     def get_category_values(self, data_matrix, category_indices):
#         categories = {}
#         for category_index in category_indices:
#             categories[category_index] = set()
# 
#         for i in xrange(len(data_matrix)):
#             for category_index in category_indices:
#                 category_value = data_matrix[i][category_index]
#                 categories[category_index].add(category_value)
# 
#         return categories
# 
#     @classmethod
#     def create_value_map(self, values_set):
#         sorted_values_set = sorted(values_set)
#         value_mapping = {}
#         for i in xrange(len(sorted_values_set)):
#             value_mapping[sorted_values_set[i]] = i
#         return value_mapping


from sklearn import preprocessing
from wallace.predictive_models.predictive_model import PredictiveModel, TrainedPredictiveModel
from wallace.categorical_variable_encoder import CategoricalVariableEncoder
    def get_dependent_variable_data(self, dataset):
        return dataset.get_filtered_column(self.dependent_variable)

    def get_independent_variable_data(self, dataset):
        filtered_matrix = dataset.get_filtered_matrix(self.independent_variables)
        filtered_data_types = dataset.get_filtered_data_types(self.independent_variables)
        categorical_features, category_value_mapping, num_category_values = self.get_categorical_features(filtered_data_types)

        variable_encoder = CategoricalVariableEncoder()
        filtered_matrix = variable_encoder.convert_categorical_variables(filtered_matrix, categorical_features, category_value_mapping)

        encoder = preprocessing.OneHotEncoder(categorical_features=categorical_features, n_values=num_category_values)
        data_result = encoder.fit_transform(filtered_matrix)

        # Take care of sparse and non-sparse matrices. If we have any categorical
        # variables, then it is likely we will get back a sparse matrix and will
        # need to convert it into an array.
        try:
            return data_result.toarray()
        except AttributeError:
            return data_result

    def get_categorical_features(self, filtered_data_types):
        categorical_features = []
        category_value_mapping = {}
        num_category_values = []
        for i in xrange(len(filtered_data_types)):
            data_type = filtered_data_types[i]
            if data_type.is_equal("string") or data_type.is_equal("boolean"):
                categorical_features.append(i)

# Path: wallace/predictive_models/predictive_model.py
# class PredictiveModel(object):
#     def __init__(self, settings, parameter_set, dependent_variable, independent_variables):
#         self.parameter_set = parameter_set
#         self.settings = settings
#         self.dependent_variable = dependent_variable
#         self.independent_variables = independent_variables
# 
#         self._validate_parameter_set(self.parameter_set)
# 
#     def model_name(self):
#         return self.__class__.__name__
# 
#     def train(self, dataset):
#         raise NotImplementedError()
# 
#     def get_dependent_variable_data(self, dataset):
#         return dataset.get_filtered_column(self.dependent_variable)
# 
#     def get_independent_variable_data(self, dataset):
#         return dataset.get_filtered_matrix(self.independent_variables)
# 
#     def get_parameters(self):
#         params = {}
#         for parameter_name in self.parameter_set.validity_check.list_parameter_names():
#             params[parameter_name] = self.parameter_set.get(parameter_name)
# 
#         return params
# 
#     @classmethod
#     def validity_check(klass):
#         return ParametersGeneralValidityCheck()
# 
#     def _validate_parameter_set(self, parameter_set):
#         for parameter in self.validity_check().list_parameter_names():
#             if not parameter_set.has_parameter(parameter):
#                 raise AssertionError("The model's parameter set does not have the required parameter '%s'." % parameter)
# 
# class TrainedPredictiveModel(object):
#     def __init__(self, predictive_model):
#         self.predictive_model = predictive_model
# 
#     def model_type(self):
#         return self.predictive_model.__class__.__name__
# 
#     def predict(self, dataset):
#         raise NotImplementedError()
# 
#     def predict_and_evaluate_fitness(self, dataset, evaluation_method):
#         predicted_results = self.predict(dataset)
#         actual_results = self.predictive_model.get_dependent_variable_data(dataset)
#         return evaluation_method.evaluate_fitness(predicted_results, actual_results)
# 
# Path: wallace/categorical_variable_encoder.py
# class CategoricalVariableEncoder(object):
#     def convert_categorical_variables(self, data_matrix, category_indices, category_value_mapping=None):
#         if len(category_indices) == 0:
#             return data_matrix
# 
#         if category_value_mapping == None:
#             category_value_mapping = self.get_category_value_mapping(data_matrix, category_indices)
# 
#         for i in xrange(len(data_matrix)):
#             for category_index in category_indices:
#                 current_data_value = data_matrix[i][category_index]
#                 updated_data_value = category_value_mapping[category_index][current_data_value]
#                 data_matrix[i][category_index] = updated_data_value
# 
#         return data_matrix
# 
#     def get_category_value_mapping(self, data_matrix, category_indices):
#         categories = self.get_category_values(data_matrix, category_indices)
# 
#         category_value_mapping = {}
#         for category_index, values_set in categories.iteritems():
#             category_value_mapping[category_index] = self.create_value_map(values_set)
# 
#         return category_value_mapping
# 
#     def get_category_values(self, data_matrix, category_indices):
#         categories = {}
#         for category_index in category_indices:
#             categories[category_index] = set()
# 
#         for i in xrange(len(data_matrix)):
#             for category_index in category_indices:
#                 category_value = data_matrix[i][category_index]
#                 categories[category_index].add(category_value)
# 
#         return categories
# 
#     @classmethod
#     def create_value_map(self, values_set):
#         sorted_values_set = sorted(values_set)
#         value_mapping = {}
#         for i in xrange(len(sorted_values_set)):
#             value_mapping[sorted_values_set[i]] = i
#         return value_mapping


# Path: wallace/predictive_models/sklearn_model.py
from sklearn import preprocessing
from wallace.predictive_models.predictive_model import PredictiveModel, TrainedPredictiveModel
from wallace.categorical_variable_encoder import CategoricalVariableEncoder

    def get_dependent_variable_data(self, dataset):
        return dataset.get_filtered_column(self.dependent_variable)

    def get_independent_variable_data(self, dataset):
        filtered_matrix = dataset.get_filtered_matrix(self.independent_variables)
        filtered_data_types = dataset.get_filtered_data_types(self.independent_variables)
        categorical_features, category_value_mapping, num_category_values = self.get_categorical_features(filtered_data_types)

        variable_encoder = CategoricalVariableEncoder()
        filtered_matrix = variable_encoder.convert_categorical_variables(filtered_matrix, categorical_features, category_value_mapping)

        encoder = preprocessing.OneHotEncoder(categorical_features=categorical_features, n_values=num_category_values)
        data_result = encoder.fit_transform(filtered_matrix)

        # Take care of sparse and non-sparse matrices. If we have any categorical
        # variables, then it is likely we will get back a sparse matrix and will
        # need to convert it into an array.
        try:
            return data_result.toarray()
        except AttributeError:
            return data_result

    def get_categorical_features(self, filtered_data_types):
        categorical_features = []
        category_value_mapping = {}
        num_category_values = []
        for i in xrange(len(filtered_data_types)):
            data_type = filtered_data_types[i]
            if data_type.is_equal("string") or data_type.is_equal("boolean"):
                categorical_features.append(i)

                category_value_mapping[i] = CategoricalVariableEncoder.create_value_map(data_type.categories)