<commit_before>from means.io.serialise import SerialisableObject
from means.util.decorators import memoised_property

class ConvergenceStatusBase(SerialisableObject):



    def __init__(self, convergence_achieved):
        self.__convergence_achieved = convergence_achieved

    @property
    def convergence_achieved(self):
        return self.__convergence_achieved

    @property
    def _convergence_achieved_str(self):
        return 'convergence achieved' if self.convergence_achieved else 'convergence not achieved'

    def __unicode__(self):
        return u"<Inference {self._convergence_achieved_str}>".format(self=self)

    def __str__(self):
        return unicode(self).encode("utf8")

    def __repr__(self):
        return str(self)

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False

        return self.convergence_achieved == other.convergence_achieved

class NormalConvergenceStatus(ConvergenceStatusBase):

    yaml_tag = '!convergence-status'

    def __init__(self, warn_flag, iterations_taken, function_calls_made):
        convergence_achieved = warn_flag != 1 and warn_flag != 2
        super(NormalConvergenceStatus, self).__init__(convergence_achieved)

        self.__iterations_taken = iterations_taken
        self.__function_calls_made = function_calls_made
        self.__warn_flag = warn_flag

    @property
    def iterations_taken(self):
        return self.__iterations_taken

    @property
    def function_calls_made(self):
        return self.__function_calls_made

    @property
    def warn_flag(self):
        return self.__warn_flag

    def __unicode__(self):
        return u"<Inference {self._convergence_achieved_str} " \
               u"in {self.iterations_taken} iterations " \
               u"and {self.function_calls_made} function calls>".format(self=self)

    @classmethod
    def to_yaml(cls, dumper, data):
        mapping = [('warn_flag', data.warn_flag),
                   ('iterations_taken', data.iterations_taken),
                   ('function_calls_made', data.function_calls_made)]
        return dumper.represent_mapping(cls.yaml_tag, mapping)

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False

        return super(NormalConvergenceStatus, self).__eq__(other) \
            and self.iterations_taken == other.iterations_taken\
            and self.function_calls_made == other.function_calls_made \
            and self.warn_flag == other.warn_flag


class SolverErrorConvergenceStatus(ConvergenceStatusBase):

    yaml_tag = '!multiple-solver-errors'

    def __init__(self):
        super(SolverErrorConvergenceStatus, self).__init__(False)

    def __unicode__(self):
        return u"<Inference {self._convergence_achieved_str} " \
               u"as it was cancelled after maximum number of solver errors occurred.".format(self=self)

    @classmethod
    def to_yaml(cls, dumper, data):
        return dumper.represent_mapping(cls.yaml_tag, {})


    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False

        return super(SolverErrorConvergenceStatus, self).__eq__(other)

class InferenceResultsCollection(SerialisableObject):
    __inference_results = None

    yaml_tag = '!serialisable-results-collection'

    def __init__(self, inference_results):
        self.__inference_results = sorted(inference_results, key=lambda x: x.distance_at_minimum)

    @classmethod
    def to_yaml(cls, dumper, data):
        sequence = data.results
        return dumper.represent_sequence(cls.yaml_tag, sequence)

    @classmethod
    def from_yaml(cls, loader, node):
        sequence = loader.construct_sequence(node, deep=True)
        return cls(sequence)

    @property
    def results(self):
        """

        :return: The results of performed inferences
        :rtype: list[:class:`InferenceResult`]
        """
        return self.__inference_results

    @property
    def number_of_results(self):
        return len(self.results)

    @property
    def best(self):
        return self.__inference_results[0]

    def __unicode__(self):
        return u"""
        {self.__class__!r}

        Number of inference results in collection: {self.number_of_results}
        Best:
        {self.best!r}
        """.format(self=self)

    def __str__(self):
        return unicode(self).encode("utf8")
<commit_msg>iteration for ResultsCollection<commit_after>from means.io.serialise import SerialisableObject
from means.util.decorators import memoised_property

class ConvergenceStatusBase(SerialisableObject):



    def __init__(self, convergence_achieved):
        self.__convergence_achieved = convergence_achieved

    @property
    def convergence_achieved(self):
        return self.__convergence_achieved

    @property
    def _convergence_achieved_str(self):
        return 'convergence achieved' if self.convergence_achieved else 'convergence not achieved'

    def __unicode__(self):
        return u"<Inference {self._convergence_achieved_str}>".format(self=self)

    def __str__(self):
        return unicode(self).encode("utf8")

    def __repr__(self):
        return str(self)

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False

        return self.convergence_achieved == other.convergence_achieved

class NormalConvergenceStatus(ConvergenceStatusBase):

    yaml_tag = '!convergence-status'

    def __init__(self, warn_flag, iterations_taken, function_calls_made):
        convergence_achieved = warn_flag != 1 and warn_flag != 2
        super(NormalConvergenceStatus, self).__init__(convergence_achieved)

        self.__iterations_taken = iterations_taken
        self.__function_calls_made = function_calls_made
        self.__warn_flag = warn_flag

    @property
    def iterations_taken(self):
        return self.__iterations_taken

    @property
    def function_calls_made(self):
        return self.__function_calls_made

    @property
    def warn_flag(self):
        return self.__warn_flag

    def __unicode__(self):
        return u"<Inference {self._convergence_achieved_str} " \
               u"in {self.iterations_taken} iterations " \
               u"and {self.function_calls_made} function calls>".format(self=self)

    @classmethod
    def to_yaml(cls, dumper, data):
        mapping = [('warn_flag', data.warn_flag),
                   ('iterations_taken', data.iterations_taken),
                   ('function_calls_made', data.function_calls_made)]
        return dumper.represent_mapping(cls.yaml_tag, mapping)

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False

        return super(NormalConvergenceStatus, self).__eq__(other) \
            and self.iterations_taken == other.iterations_taken\
            and self.function_calls_made == other.function_calls_made \
            and self.warn_flag == other.warn_flag


class SolverErrorConvergenceStatus(ConvergenceStatusBase):

    yaml_tag = '!multiple-solver-errors'

    def __init__(self):
        super(SolverErrorConvergenceStatus, self).__init__(False)

    def __unicode__(self):
        return u"<Inference {self._convergence_achieved_str} " \
               u"as it was cancelled after maximum number of solver errors occurred.".format(self=self)

    @classmethod
    def to_yaml(cls, dumper, data):
        return dumper.represent_mapping(cls.yaml_tag, {})


    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return False

        return super(SolverErrorConvergenceStatus, self).__eq__(other)

class InferenceResultsCollection(SerialisableObject):
    __inference_results = None

    yaml_tag = '!serialisable-results-collection'

    def __init__(self, inference_results):
        self.__inference_results = sorted(inference_results, key=lambda x: x.distance_at_minimum)

    @classmethod
    def to_yaml(cls, dumper, data):
        sequence = data.results
        return dumper.represent_sequence(cls.yaml_tag, sequence)

    @classmethod
    def from_yaml(cls, loader, node):
        sequence = loader.construct_sequence(node, deep=True)
        return cls(sequence)

    @property
    def results(self):
        """

        :return: The results of performed inferences
        :rtype: list[:class:`InferenceResult`]
        """
        return self.__inference_results

    def __iter__(self):
        return iter(self.results)

    def __len__(self):
        return len(self.results)

    @property
    def number_of_results(self):
        return len(self.results)

    @property
    def best(self):
        return self.__inference_results[0]

    def __unicode__(self):
        return u"""
        {self.__class__!r}

        Number of inference results in collection: {self.number_of_results}
        Best:
        {self.best!r}
        """.format(self=self)

    def __str__(self):
        return unicode(self).encode("utf8")

{'fraction_non_alphanumeric': 0.08792912513842746, 'fraction_numerical': 0.0011074197120708748, 'mean_word_length': 3.0772009029345373, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '338436', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2554, 'n_words': 534}