<commit_before>import abc

import numpy as np
import constraints as cons
from utils import poly_sat


class ModelError(Exception):
    pass


class ModelSpec(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def __init__(self):
        return

    @abc.abstractmethod
    def add(self, sub_model):
        return

    @abc.abstractmethod
    def find_sub_model(self, x):
        return

    @abc.abstractmethod
    def predict(self, x):
        return

    # Make the class iterable
    @abc.abstractmethod
    def __iter__(self):
        return

    @abc.abstractmethod
    def __repr__(self):
        return

    @abc.abstractmethod
    def __str__(self):
        return


class SubModelSpec():
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def sat(self, x):
        return

    @abc.abstractmethod
    def predict(self, x):
        return


class Partition(object):
    def __init__(self, C, d, part_id):
        '''
        Cx <= d
        '''
        self.C = C
        self.d = d
        self.ID = part_id
        return

    def __repr__(self):
        s = '({},{})'.format(self.C, self.d)
        return s

    def __str__(self):
        s = 'Pi ->(\n{},\n{})'.format(self.C, self.d)
        return s


class DiscreteAffineMap(object):
    def __init__(self, A, b, e):
        '''
        x' = Ax + b +- error
        '''
        self.A = A
        self.b = b
        assert(isinstance(e, cons.IntervalCons))
        self.error = e
        return

    def __repr__(self):
        s = '({},{},{})'.format(self.A, self.b, self.error)
        return s

    def __str__(self):
        s = 'Mi - >(\n{},\n{} + [{}, {}])'.format(
                self.A, self.b, self.error.l, self.error.h)
        return s


class PartitionedDiscreteAffineModel(SubModelSpec):
    def __init__(self, p, m):
        '''
        loc: p(x) => x' = m(x)
        '''
        self.p = p
        self.m = m
        self.ID = p.ID
        return

    def sat(self, x):
        return poly_sat(self.p, x)

    def predict(self, x):
        m = self.m
        return np.dot(m.A, x) + m.b

    def __repr__(self):
        s = '({},{})'.format(self.p, self.m)
        return s

    def __str__(self):
        s = 'SubModel ->(\n{},\n{})'.format(self.p, self.m)
        return s


class ModelGeneric(ModelSpec):
    def __init__(self):
        self.nlocs = 0
        self.sub_models = {}
        self.idx = 0
        #self.relation_ids = set()
        return

    def add(self, sub_model):
        part_id = self.idx
        self.sub_models[part_id] = sub_model
        self.idx += 1
        #self.relation_ids.add((sub_model.ID))

    #def get_sub_model(self, part_id):
    #    return self.sub_models[part_id]

    # returns the first sub_model whose parition the point x belongs
    # to
    # TODO: Brute force search, very inefficient
    def find_sub_model(self, x):
        for sub_model in self.sub_models.itervalues():
            if sub_model.sat(x):
                return sub_model
        raise ModelError('no appropriate submodel found')

    def predict(self, x):
        try:
            sub_model = self.find_sub_model(x)
        except ModelError:
            if __debug__:
                print('No sub-model found')
            return x

        return sub_model.predict(x)

    # Make the class iterable
    def __iter__(self):
        return self.sub_models.itervalues()

    def __repr__(self):
        return repr(self.sub_models)

    def __str__(self):
        s = [str(i) for i in self]
        return '{1}{0}{1}'.format('='*20, '\n').join(s)<commit_msg>changed prints to logs<commit_after>import abc
import logging

import numpy as np
import constraints as cons
from utils import poly_sat

logger = logging.getLogger(__name__)


class ModelError(Exception):
    pass


class ModelSpec(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def __init__(self):
        return

    @abc.abstractmethod
    def add(self, sub_model):
        return

    @abc.abstractmethod
    def find_sub_model(self, x):
        return

    @abc.abstractmethod
    def predict(self, x):
        return

    # Make the class iterable
    @abc.abstractmethod
    def __iter__(self):
        return

    @abc.abstractmethod
    def __repr__(self):
        return

    @abc.abstractmethod
    def __str__(self):
        return


class SubModelSpec():
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def sat(self, x):
        return

    @abc.abstractmethod
    def predict(self, x):
        return


class Partition(object):
    def __init__(self, C, d, part_id):
        '''
        Cx <= d
        '''
        self.C = C
        self.d = d
        self.ID = part_id
        return

    def __repr__(self):
        s = '({},{})'.format(self.C, self.d)
        return s

    def __str__(self):
        s = 'Pi ->(\n{},\n{})'.format(self.C, self.d)
        return s


class DiscreteAffineMap(object):
    def __init__(self, A, b, e):
        '''
        x' = Ax + b +- error
        '''
        self.A = A
        self.b = b
        assert(isinstance(e, cons.IntervalCons))
        self.error = e
        return

    def __repr__(self):
        s = '({},{},{})'.format(self.A, self.b, self.error)
        return s

    def __str__(self):
        s = 'Mi - >(\n{},\n{} + [{}, {}])'.format(
                self.A, self.b, self.error.l, self.error.h)
        return s


class PartitionedDiscreteAffineModel(SubModelSpec):
    def __init__(self, p, m):
        '''
        loc: p(x) => x' = m(x)
        '''
        self.p = p
        self.m = m
        self.ID = p.ID
        return

    def sat(self, x):
        return poly_sat(self.p, x)

    def predict(self, x):
        m = self.m
        return np.dot(m.A, x) + m.b

    def __repr__(self):
        s = '({},{})'.format(self.p, self.m)
        return s

    def __str__(self):
        s = 'SubModel ->(\n{},\n{})'.format(self.p, self.m)
        return s


class ModelGeneric(ModelSpec):
    def __init__(self):
        self.nlocs = 0
        self.sub_models = {}
        self.idx = 0
        #self.relation_ids = set()
        return

    def add(self, sub_model):
        part_id = self.idx
        self.sub_models[part_id] = sub_model
        self.idx += 1
        #self.relation_ids.add((sub_model.ID))

    #def get_sub_model(self, part_id):
    #    return self.sub_models[part_id]

    # returns the first sub_model whose parition the point x belongs
    # to
    # TODO: Brute force search, very inefficient
    def find_sub_model(self, x):
        for sub_model in self.sub_models.itervalues():
            if sub_model.sat(x):
                return sub_model
        raise ModelError('No appropriate submodel found')

    def predict(self, x):
        try:
            sub_model = self.find_sub_model(x)
        except ModelError:
            logger.debug('No sub-model found')
            return x

        return sub_model.predict(x)

    # Make the class iterable
    def __iter__(self):
        return self.sub_models.itervalues()

    def __repr__(self):
        return repr(self.sub_models)

    def __str__(self):
        s = [str(i) for i in self]
        return '{1}{0}{1}'.format('='*20, '\n').join(s)
{'fraction_non_alphanumeric': 0.125814050159346, 'fraction_numerical': 0.0022169876680060967, 'mean_word_length': 1.8825878594249201, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '3284090', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2351, 'n_words': 580}