# Path: preimage/metrics/structured_output.py
# def zero_one_loss(Y_true, Y_predicted):
#     """Zero one loss.
# 
#     Returns the number of incorrectly predicted strings on the total number of strings.
# 
#     Parameters
#     ----------
#     Y_true : array, shape = [n_samples, ]
#         Ground truth (correct) strings.
#     Y_predicted : array, shape = [n_samples, ]
#         Predicted strings, as returned by the structured output predictor.
# 
#     Returns
#     -------
#     loss : float
#         The number of incorrectly predicted strings on the total number of strings.
#     """
#     Y_true = numpy.array(Y_true)
#     Y_predicted = numpy.array(Y_predicted)
#     __check_same_number_of_y(Y_true, Y_predicted)
#     n_errors = __get_n_errors_for_each_y_predicted(Y_true, Y_predicted)
#     loss = numpy.mean(n_errors)
#     return loss
# 
# def hamming_loss(Y_true, Y_predicted):
#     """Average hamming loss.
# 
#     Computes the average fraction of incorrectly predicted letters in the predicted strings. The ground truth and the
#     predicted strings must have the same length.
# 
#     Parameters
#     ----------
#     Y_true : array, shape = [n_samples, ]
#         Ground truth (correct) strings.
#     Y_predicted : array, shape = [n_samples, ]
#         Predicted strings, as returned by the structured output predictor.
# 
#     Returns
#     -------
#     loss : float
#         The average fraction of incorrectly predicted letters.
#     """
#     Y_true = numpy.array(Y_true)
#     Y_predicted = numpy.array(Y_predicted)
#     __check_same_number_of_y(Y_true, Y_predicted)
#     y_true_lengths = __get_length_of_each_y(Y_true)
#     y_predicted_lengths = __get_length_of_each_y(Y_predicted)
#     __check_each_tuple_y_true_y_predicted_has_same_length(y_true_lengths, y_predicted_lengths)
#     n_errors = __get_n_letter_errors_for_each_y_predicted(Y_true, Y_predicted, y_true_lengths)
#     loss = numpy.mean(n_errors / numpy.array(y_true_lengths, dtype=numpy.float))
#     return loss
# 
# def levenshtein_loss(Y_true, Y_predicted):
#     """Average levenshtein loss.
# 
#     Computes the average fraction of levenshtein distance between the ground truth strings and the predicted strings.
# 
#     Parameters
#     ----------
#     Y_true : array, shape = [n_samples, ]
#         Ground truth (correct) strings.
#     Y_predicted : array, shape = [n_samples, ]
#         Predicted strings, as returned by the structured output predictor.
# 
#     Returns
#     -------
#     loss : float
#         The average fraction of levenshtein distance.
#     """
#     Y_true = numpy.array(Y_true)
#     Y_predicted = numpy.array(Y_predicted)
#     __check_same_number_of_y(Y_true, Y_predicted)
#     max_lengths = __get_max_length_of_each_tuple_y_true_y_predicted(Y_true, Y_predicted)
#     distances = __get_levenshtein_distance_for_each_y_predicted(Y_true, Y_predicted)
#     loss = numpy.mean(distances / numpy.array(max_lengths, dtype=numpy.float))
#     return loss


import unittest2
from preimage.metrics.structured_output import zero_one_loss, hamming_loss, levenshtein_loss

    def test_all_wrong_letters_hamming_loss_is_one(self):
        loss = hamming_loss(self.unicorn, self.nicornu)

        self.assertEqual(loss, 1.)

    def test_two_words_with_errors_hamming_loss_is_mean_of_errors(self):
        loss = hamming_loss(self.unicorn_potato, self.unicarn_tomato)

        self.assertEqual(loss, self.hamming_loss_unicarn_potato)

    def test_not_same_word_count_hamming_loss_throws_value_error(self):
        with self.assertRaises(ValueError):
            hamming_loss(self.unicorn_potato, self.unicorn)

    def test_not_same_letter_count_hamming_loss_throws_value_error(self):
        with self.assertRaises(ValueError):
            hamming_loss(self.unicorn, self.unicor)

    def test_same_word_levenshtein_loss_is_zero(self):
        loss = levenshtein_loss(self.unicorn, self.unicorn)

        self.assertEqual(loss, 0.)

    def test_one_missing_letter_on_seven_levenshtein_loss_is_one_on_seven(self):
        loss = levenshtein_loss(self.unicorn, self.unicor)

        self.assertEqual(loss, 1. / 7)

    def test_one_extra_letter_on_seven_levenshtein_loss_is_one_on_seven(self):

# Path: preimage/metrics/structured_output.py
# def zero_one_loss(Y_true, Y_predicted):
#     """Zero one loss.
# 
#     Returns the number of incorrectly predicted strings on the total number of strings.
# 
#     Parameters
#     ----------
#     Y_true : array, shape = [n_samples, ]
#         Ground truth (correct) strings.
#     Y_predicted : array, shape = [n_samples, ]
#         Predicted strings, as returned by the structured output predictor.
# 
#     Returns
#     -------
#     loss : float
#         The number of incorrectly predicted strings on the total number of strings.
#     """
#     Y_true = numpy.array(Y_true)
#     Y_predicted = numpy.array(Y_predicted)
#     __check_same_number_of_y(Y_true, Y_predicted)
#     n_errors = __get_n_errors_for_each_y_predicted(Y_true, Y_predicted)
#     loss = numpy.mean(n_errors)
#     return loss
# 
# def hamming_loss(Y_true, Y_predicted):
#     """Average hamming loss.
# 
#     Computes the average fraction of incorrectly predicted letters in the predicted strings. The ground truth and the
#     predicted strings must have the same length.
# 
#     Parameters
#     ----------
#     Y_true : array, shape = [n_samples, ]
#         Ground truth (correct) strings.
#     Y_predicted : array, shape = [n_samples, ]
#         Predicted strings, as returned by the structured output predictor.
# 
#     Returns
#     -------
#     loss : float
#         The average fraction of incorrectly predicted letters.
#     """
#     Y_true = numpy.array(Y_true)
#     Y_predicted = numpy.array(Y_predicted)
#     __check_same_number_of_y(Y_true, Y_predicted)
#     y_true_lengths = __get_length_of_each_y(Y_true)
#     y_predicted_lengths = __get_length_of_each_y(Y_predicted)
#     __check_each_tuple_y_true_y_predicted_has_same_length(y_true_lengths, y_predicted_lengths)
#     n_errors = __get_n_letter_errors_for_each_y_predicted(Y_true, Y_predicted, y_true_lengths)
#     loss = numpy.mean(n_errors / numpy.array(y_true_lengths, dtype=numpy.float))
#     return loss
# 
# def levenshtein_loss(Y_true, Y_predicted):
#     """Average levenshtein loss.
# 
#     Computes the average fraction of levenshtein distance between the ground truth strings and the predicted strings.
# 
#     Parameters
#     ----------
#     Y_true : array, shape = [n_samples, ]
#         Ground truth (correct) strings.
#     Y_predicted : array, shape = [n_samples, ]
#         Predicted strings, as returned by the structured output predictor.
# 
#     Returns
#     -------
#     loss : float
#         The average fraction of levenshtein distance.
#     """
#     Y_true = numpy.array(Y_true)
#     Y_predicted = numpy.array(Y_predicted)
#     __check_same_number_of_y(Y_true, Y_predicted)
#     max_lengths = __get_max_length_of_each_tuple_y_true_y_predicted(Y_true, Y_predicted)
#     distances = __get_levenshtein_distance_for_each_y_predicted(Y_true, Y_predicted)
#     loss = numpy.mean(distances / numpy.array(max_lengths, dtype=numpy.float))
#     return loss


# Path: preimage/tests/metrics/test_structured_output.py
import unittest2
from preimage.metrics.structured_output import zero_one_loss, hamming_loss, levenshtein_loss


    def test_all_wrong_letters_hamming_loss_is_one(self):
        loss = hamming_loss(self.unicorn, self.nicornu)

        self.assertEqual(loss, 1.)

    def test_two_words_with_errors_hamming_loss_is_mean_of_errors(self):
        loss = hamming_loss(self.unicorn_potato, self.unicarn_tomato)

        self.assertEqual(loss, self.hamming_loss_unicarn_potato)

    def test_not_same_word_count_hamming_loss_throws_value_error(self):
        with self.assertRaises(ValueError):
            hamming_loss(self.unicorn_potato, self.unicorn)

    def test_not_same_letter_count_hamming_loss_throws_value_error(self):
        with self.assertRaises(ValueError):
            hamming_loss(self.unicorn, self.unicor)

    def test_same_word_levenshtein_loss_is_zero(self):
        loss = levenshtein_loss(self.unicorn, self.unicorn)

        self.assertEqual(loss, 0.)

    def test_one_missing_letter_on_seven_levenshtein_loss_is_one_on_seven(self):
        loss = levenshtein_loss(self.unicorn, self.unicor)

        self.assertEqual(loss, 1. / 7)

    def test_one_extra_letter_on_seven_levenshtein_loss_is_one_on_seven(self):

        loss = levenshtein_loss(self.unicor, self.unicorn)