# Path: preimage/features/string_feature_space.py
# def build_feature_space_without_positions(alphabet, n, Y):
#     """Create the feature space without considering the position of the n-gram in the strings
# 
#     Parameters
#     ----------
#     alphabet : list
#         List of letters.
#     n : int
#         N-gram length.
#     Y : array, [n_samples, ]
#         Training strings.
# 
#     Returns
#     -------
#     feature_space : sparse matrix, shape = [n_samples, len(alphabet)**n]
#         Sparse matrix representation of the n-grams in each training string, where n_samples is the number of samples
#         in Y.
#     """
#     n = int(n)
#     n_examples = numpy.array(Y).shape[0]
#     n_gram_to_index = get_n_gram_to_index(alphabet, n)
#     index_pointers, indexes, data = __initialize_pointers_indexes_data(n_examples)
#     __build_y_pointers_indexes_data(index_pointers, indexes, data, n, n_gram_to_index, Y)
#     feature_space = __build_csr_matrix(index_pointers, indexes, data, n_examples, len(n_gram_to_index))
#     return feature_space
# 
# Path: preimage/features/string_feature_space.py
# def build_feature_space_with_positions(alphabet, n, Y):
#     """Create the feature space by considering the position of the n-gram in the strings
# 
#     Parameters
#     ----------
#     alphabet : list
#         list of letters
#     n : int
#         n-gram length
#     Y : array, [n_samples, ]
#         The training strings.
# 
#     Returns
#     -------
#     feature_space : sparse matrix, shape = [n_samples, max_n_gram_count * len(alphabet)**n]
#         Sparse matrix representation of the n-grams in each string of Y, where n_samples is the number of training
#         samples and max_n_gram_count is the number of n-gram in the highest length string of Y.
#     """
#     n = int(n)
#     n_examples = numpy.array(Y).shape[0]
#     n_gram_to_index = get_n_gram_to_index(alphabet, n)
#     index_pointers, indexes, data = __initialize_pointers_indexes_data(n_examples)
#     __build_pointers_indexes_data_with_positions(index_pointers, indexes, data, n, n_gram_to_index, Y)
#     n_columns = __get_n_columns(n, len(n_gram_to_index), Y)
#     feature_space = __build_csr_matrix(index_pointers, indexes, data, n_examples, n_columns)
#     return feature_space
# 
# Path: preimage/exceptions/n_gram.py
# class InvalidNGramError(ValueError):
#     def __init__(self, n, n_gram):
#         self.n = n
#         self.n_gram = n_gram
# 
#     def __str__(self):
#         error_message = "{} is not a possible {:d}_gram for this alphabet".format(self.n_gram, self.n)
#         return error_message


import unittest2
import numpy.testing
from mock import patch
from preimage.features.string_feature_space import build_feature_space_without_positions
from preimage.features.string_feature_space import build_feature_space_with_positions
from preimage.exceptions.n_gram import InvalidNGramError
    def test_two_gram_not_in_alphabet_n_gram_feature_space_raises_error(self):
        self.n_gram_to_index_patch.start().return_value = self.two_gram_to_index_without_bb

        with self.assertRaises(InvalidNGramError):
            build_feature_space_without_positions(alphabet=self.alphabet, n=2, Y=self.abb)

    def test_one_gram_one_letter_y_weighted_degree_feature_space_has_one_n_gram(self):
        self.n_gram_to_index_patch.start().return_value = self.one_gram_to_index

        feature_space = build_feature_space_with_positions(n=1, alphabet=self.alphabet, Y=self.b)

        numpy.testing.assert_array_equal(feature_space.toarray(), self.feature_space_one_gram_b)

    def test_one_gram_three_letter_y_weighted_degree_feature_space_has_one_n_gram_at_each_position(self):
        self.n_gram_to_index_patch.start().return_value = self.one_gram_to_index

        feature_space = build_feature_space_with_positions(n=1, alphabet=self.alphabet, Y=self.abb)

        numpy.testing.assert_array_equal(feature_space.toarray(), self.weighted_degree_one_gram_abb)

    def test_two_gram_five_letter_y_weighted_degree_feature_space_has_four_two_grams(self):
        self.n_gram_to_index_patch.start().return_value = self.two_gram_to_index

        feature_space = build_feature_space_with_positions(n=2, alphabet=self.alphabet, Y=self.abaaa)

        numpy.testing.assert_array_equal(feature_space.toarray(), self.weighted_degree_two_gram_abaaa)

    def test_two_gram_two_y_weighted_degree_feature_space_builds_expected_feature_space(self):
        self.n_gram_to_index_patch.start().return_value = self.two_gram_to_index


# Path: preimage/features/string_feature_space.py
# def build_feature_space_without_positions(alphabet, n, Y):
#     """Create the feature space without considering the position of the n-gram in the strings
# 
#     Parameters
#     ----------
#     alphabet : list
#         List of letters.
#     n : int
#         N-gram length.
#     Y : array, [n_samples, ]
#         Training strings.
# 
#     Returns
#     -------
#     feature_space : sparse matrix, shape = [n_samples, len(alphabet)**n]
#         Sparse matrix representation of the n-grams in each training string, where n_samples is the number of samples
#         in Y.
#     """
#     n = int(n)
#     n_examples = numpy.array(Y).shape[0]
#     n_gram_to_index = get_n_gram_to_index(alphabet, n)
#     index_pointers, indexes, data = __initialize_pointers_indexes_data(n_examples)
#     __build_y_pointers_indexes_data(index_pointers, indexes, data, n, n_gram_to_index, Y)
#     feature_space = __build_csr_matrix(index_pointers, indexes, data, n_examples, len(n_gram_to_index))
#     return feature_space
# 
# Path: preimage/features/string_feature_space.py
# def build_feature_space_with_positions(alphabet, n, Y):
#     """Create the feature space by considering the position of the n-gram in the strings
# 
#     Parameters
#     ----------
#     alphabet : list
#         list of letters
#     n : int
#         n-gram length
#     Y : array, [n_samples, ]
#         The training strings.
# 
#     Returns
#     -------
#     feature_space : sparse matrix, shape = [n_samples, max_n_gram_count * len(alphabet)**n]
#         Sparse matrix representation of the n-grams in each string of Y, where n_samples is the number of training
#         samples and max_n_gram_count is the number of n-gram in the highest length string of Y.
#     """
#     n = int(n)
#     n_examples = numpy.array(Y).shape[0]
#     n_gram_to_index = get_n_gram_to_index(alphabet, n)
#     index_pointers, indexes, data = __initialize_pointers_indexes_data(n_examples)
#     __build_pointers_indexes_data_with_positions(index_pointers, indexes, data, n, n_gram_to_index, Y)
#     n_columns = __get_n_columns(n, len(n_gram_to_index), Y)
#     feature_space = __build_csr_matrix(index_pointers, indexes, data, n_examples, n_columns)
#     return feature_space
# 
# Path: preimage/exceptions/n_gram.py
# class InvalidNGramError(ValueError):
#     def __init__(self, n, n_gram):
#         self.n = n
#         self.n_gram = n_gram
# 
#     def __str__(self):
#         error_message = "{} is not a possible {:d}_gram for this alphabet".format(self.n_gram, self.n)
#         return error_message


# Path: preimage/tests/features/test_string_feature_space.py
import unittest2
import numpy.testing
from mock import patch
from preimage.features.string_feature_space import build_feature_space_without_positions
from preimage.features.string_feature_space import build_feature_space_with_positions
from preimage.exceptions.n_gram import InvalidNGramError

    def test_two_gram_not_in_alphabet_n_gram_feature_space_raises_error(self):
        self.n_gram_to_index_patch.start().return_value = self.two_gram_to_index_without_bb

        with self.assertRaises(InvalidNGramError):
            build_feature_space_without_positions(alphabet=self.alphabet, n=2, Y=self.abb)

    def test_one_gram_one_letter_y_weighted_degree_feature_space_has_one_n_gram(self):
        self.n_gram_to_index_patch.start().return_value = self.one_gram_to_index

        feature_space = build_feature_space_with_positions(n=1, alphabet=self.alphabet, Y=self.b)

        numpy.testing.assert_array_equal(feature_space.toarray(), self.feature_space_one_gram_b)

    def test_one_gram_three_letter_y_weighted_degree_feature_space_has_one_n_gram_at_each_position(self):
        self.n_gram_to_index_patch.start().return_value = self.one_gram_to_index

        feature_space = build_feature_space_with_positions(n=1, alphabet=self.alphabet, Y=self.abb)

        numpy.testing.assert_array_equal(feature_space.toarray(), self.weighted_degree_one_gram_abb)

    def test_two_gram_five_letter_y_weighted_degree_feature_space_has_four_two_grams(self):
        self.n_gram_to_index_patch.start().return_value = self.two_gram_to_index

        feature_space = build_feature_space_with_positions(n=2, alphabet=self.alphabet, Y=self.abaaa)

        numpy.testing.assert_array_equal(feature_space.toarray(), self.weighted_degree_two_gram_abaaa)

    def test_two_gram_two_y_weighted_degree_feature_space_builds_expected_feature_space(self):
        self.n_gram_to_index_patch.start().return_value = self.two_gram_to_index


        feature_space = build_feature_space_with_positions(n=2, alphabet=self.alphabet, Y=self.abb_abaaa)