# Path: preimage/models/weighted_degree_model.py
# class WeightedDegreeModel(Model):
#     def __init__(self, alphabet, n, is_using_length=True):
#         self._graph_builder = GraphBuilder(alphabet, n)
#         self._is_normalized = True
#         Model.__init__(self, alphabet, n, is_using_length)
# 
#     def fit(self, inference_parameters):
#         Model.fit(self, inference_parameters)
#         self._feature_space_ = WeightedDegreeFeatureSpace(self._alphabet, self._n, inference_parameters.Y_train,
#                                                           self._is_normalized)
# 
#     def predict(self, Y_weights, y_lengths):
#         if self._is_using_length:
#             self._verify_y_lengths_is_not_none_when_use_length(y_lengths)
#             Y_predictions = self._predict_with_length(Y_weights, y_lengths)
#         else:
#             Y_predictions = self._predict_without_length(Y_weights)
#         return Y_predictions
# 
#     def _predict_with_length(self, Y_weights, y_lengths):
#         Y_predictions = []
#         for y_weights, y_length in zip(Y_weights, y_lengths):
#             n_gram_weights = self._feature_space_.compute_weights(y_weights, y_length)
#             y_predicted = self._graph_builder.find_max_string(n_gram_weights, y_length)
#             Y_predictions.append(y_predicted)
#         return Y_predictions
# 
#     def _predict_without_length(self, Y_weights):
#         Y_predictions = []
#         for y_weights in Y_weights:
#             n_gram_weights = self._feature_space_.compute_weights(y_weights, self._max_length_)
#             y_predicted = self._graph_builder.find_max_string_in_length_range(n_gram_weights, self._min_length_,
#                                                                               self._max_length_, self._is_normalized)
#             Y_predictions.append(y_predicted)
#         return Y_predictions
# 
# Path: preimage/learners/structured_krr.py
# class InferenceFitParameters:
#     """Parameters for the inference model.
# 
#     That way inference_model.fit(parameters) doesn't have unused parameters but only access the one it needs
#     .
#     Attributes
#     ----------
#     weights : array, shape = [n_samples, n_samples]
#         Learned weights, where n_samples is the number of training samples.
#     gram_matrix : array, shape = [n_samples, n_samples]
#         Gram_matrix of the training samples.
#     Y_train : array, shape = [n_samples, ]
#         Training strings.
#     y_lengths : array, shape = [n_samples]
#         Length of each training string in Y_train.
#     """
#     def __init__(self, weights, gram_matrix, Y, y_lengths):
#         self.weights = weights
#         self.gram_matrix = gram_matrix
#         self.Y_train = Y
#         self.y_lengths = y_lengths
# 
# Path: preimage/exceptions/n_gram.py
# class NoYLengthsError(ValueError):
#     def __str__(self):
#         error_message = "y_lengths can't be None if is_using_length = True"
#         return error_message


import unittest2
import numpy
import numpy.testing
from mock import patch, Mock
from preimage.models.weighted_degree_model import WeightedDegreeModel
from preimage.learners.structured_krr import InferenceFitParameters
from preimage.exceptions.n_gram import NoYLengthsError
__author__ = 'amelie'




class TestWeightedDegreeModel(unittest2.TestCase):
    def setUp(self):
        self.setup_feature_space()
        self.setup_fit_parameters()
        self.setup_graph_builder()
        self.alphabet = ['a', 'b', 'c']
        self.model_with_length = WeightedDegreeModel(self.alphabet, n=2, is_using_length=True)
        self.model_no_length = WeightedDegreeModel(self.alphabet, n=2, is_using_length=False)
        self.Y_weights = [[1, 2, 3], [4, 5, 6]]
        self.y_lengths = [3, 4]

    def setup_fit_parameters(self):
        self.max_train_length = 2
        self.min_train_length = 1
        self.weights = numpy.array([[1, 2]])
        self.gram_matrix = numpy.array([[1, 0], [0, 1]])
        self.fit_parameters = InferenceFitParameters(self.weights, self.gram_matrix, Y=['a', 'ab'],
                                                     y_lengths=[1, 2])

# Path: preimage/models/weighted_degree_model.py
# class WeightedDegreeModel(Model):
#     def __init__(self, alphabet, n, is_using_length=True):
#         self._graph_builder = GraphBuilder(alphabet, n)
#         self._is_normalized = True
#         Model.__init__(self, alphabet, n, is_using_length)
# 
#     def fit(self, inference_parameters):
#         Model.fit(self, inference_parameters)
#         self._feature_space_ = WeightedDegreeFeatureSpace(self._alphabet, self._n, inference_parameters.Y_train,
#                                                           self._is_normalized)
# 
#     def predict(self, Y_weights, y_lengths):
#         if self._is_using_length:
#             self._verify_y_lengths_is_not_none_when_use_length(y_lengths)
#             Y_predictions = self._predict_with_length(Y_weights, y_lengths)
#         else:
#             Y_predictions = self._predict_without_length(Y_weights)
#         return Y_predictions
# 
#     def _predict_with_length(self, Y_weights, y_lengths):
#         Y_predictions = []
#         for y_weights, y_length in zip(Y_weights, y_lengths):
#             n_gram_weights = self._feature_space_.compute_weights(y_weights, y_length)
#             y_predicted = self._graph_builder.find_max_string(n_gram_weights, y_length)
#             Y_predictions.append(y_predicted)
#         return Y_predictions
# 
#     def _predict_without_length(self, Y_weights):
#         Y_predictions = []
#         for y_weights in Y_weights:
#             n_gram_weights = self._feature_space_.compute_weights(y_weights, self._max_length_)
#             y_predicted = self._graph_builder.find_max_string_in_length_range(n_gram_weights, self._min_length_,
#                                                                               self._max_length_, self._is_normalized)
#             Y_predictions.append(y_predicted)
#         return Y_predictions
# 
# Path: preimage/learners/structured_krr.py
# class InferenceFitParameters:
#     """Parameters for the inference model.
# 
#     That way inference_model.fit(parameters) doesn't have unused parameters but only access the one it needs
#     .
#     Attributes
#     ----------
#     weights : array, shape = [n_samples, n_samples]
#         Learned weights, where n_samples is the number of training samples.
#     gram_matrix : array, shape = [n_samples, n_samples]
#         Gram_matrix of the training samples.
#     Y_train : array, shape = [n_samples, ]
#         Training strings.
#     y_lengths : array, shape = [n_samples]
#         Length of each training string in Y_train.
#     """
#     def __init__(self, weights, gram_matrix, Y, y_lengths):
#         self.weights = weights
#         self.gram_matrix = gram_matrix
#         self.Y_train = Y
#         self.y_lengths = y_lengths
# 
# Path: preimage/exceptions/n_gram.py
# class NoYLengthsError(ValueError):
#     def __str__(self):
#         error_message = "y_lengths can't be None if is_using_length = True"
#         return error_message


# Path: preimage/tests/models/test_weighted_degree_model.py
import unittest2
import numpy
import numpy.testing
from mock import patch, Mock
from preimage.models.weighted_degree_model import WeightedDegreeModel
from preimage.learners.structured_krr import InferenceFitParameters
from preimage.exceptions.n_gram import NoYLengthsError

__author__ = 'amelie'




class TestWeightedDegreeModel(unittest2.TestCase):
    def setUp(self):
        self.setup_feature_space()
        self.setup_fit_parameters()
        self.setup_graph_builder()
        self.alphabet = ['a', 'b', 'c']
        self.model_with_length = WeightedDegreeModel(self.alphabet, n=2, is_using_length=True)
        self.model_no_length = WeightedDegreeModel(self.alphabet, n=2, is_using_length=False)
        self.Y_weights = [[1, 2, 3], [4, 5, 6]]
        self.y_lengths = [3, 4]

    def setup_fit_parameters(self):
        self.max_train_length = 2
        self.min_train_length = 1
        self.weights = numpy.array([[1, 2]])
        self.gram_matrix = numpy.array([[1, 0], [0, 1]])
        self.fit_parameters = InferenceFitParameters(self.weights, self.gram_matrix, Y=['a', 'ab'],
                                                     y_lengths=[1, 2])

        self.fit_parameters_no_length = InferenceFitParameters(self.weights, self.gram_matrix, Y=['a', 'ab'],