<commit_before>from PIL import Image
from numpy import ones, zeros, linspace, argmin, nonzero, column_stack, exp, array
from numpy.random import rand, randint

from .ModelFitter import ModelFitter

class GibbsSamplerFitter(ModelFitter):
    def __init__(self, image, dimensions=199, model=None, steps=None,
                 max_loops=1):
        self.__steps = ones(dimensions, dtype='i') if steps is None else steps
        self.__current_step = None
        self.__parameters = zeros(dimensions, dtype='f')

        self.__errors = None
        self.__values = None
        self.__max_loops = max_loops

        super(GibbsSamplerFitter, self).__init__(image, dimensions, model)

    def start(self):
        self.__loop = 0
        self.request_normals(zeros(self._dimensions, dtype='f'), 'init')
        self.__get_parameter(0)

    def __get_parameter(self, i):
        self.__current_step = i

        if self.__steps[i] % 2 == 0:
            self.__values = linspace(-4, 4, self.__steps[i] + 1, dtype='f')
        else:
            self.__values = linspace(-4, 4, self.__steps[i] + 2, dtype='f')

        self.__errors = [None] * self.__values.size
        for i, value in enumerate(self.__values):
            self.__values[i] = value
            # parameters = self.__parameters.copy()
            # parameters[self.__current_step] = value
            # self.request_normals(parameters, i)
            self.request_normals((self.__current_step, value), i)

    def receive_normals(self, normals, index=None):
        if index == 'init':
            return
        elif index == 'pre':
            self.__get_parameter(self.__current_step + 1)
            return

        light = self.estimate_light(normals)
        shadows = normals.dot(light)

        if index is None:
            self.finish(normals, shadows)
            return

        self.__errors[index] = self.get_image_deviation(shadows, normals)

        if None in self.__errors:
            return

        errors = array(self.__errors)
        max_error = errors.max()
        if self.__loop >= 0:
            X = exp(- errors / max_error).sum()
            v = rand()
            best_index = -1
            t = 0
            for i, error in enumerate(errors):
                e = exp(error / max_error) / X
                t += e
                if v <= e:
                    best_index = i
                    break
                else:
                    v -= e
            if best_index == -1:<commit_msg>Added customization of random loops in Gibbs Fitter<commit_after>from PIL import Image
from numpy import ones, zeros, linspace, argmin, nonzero, column_stack, exp, array
from numpy.random import rand, randint

from .ModelFitter import ModelFitter

class GibbsSamplerFitter(ModelFitter):
    def __init__(self, image, dimensions=199, model=None, steps=None,
                 max_loops=1, determined_loops=0):
        self.__steps = ones(dimensions, dtype='i') if steps is None else steps
        self.__current_step = None
        self.__parameters = zeros(dimensions, dtype='f')

        self.__errors = None
        self.__values = None
        self.__max_loops = max_loops
        self.__determined_loops = determined_loops

        super(GibbsSamplerFitter, self).__init__(image, dimensions, model)

    def start(self):
        self.__loop = 0
        self.request_normals(zeros(self._dimensions, dtype='f'), 'init')
        self.__get_parameter(0)

    def __get_parameter(self, i):
        self.__current_step = i

        if self.__steps[i] % 2 == 0:
            self.__values = linspace(-4, 4, self.__steps[i] + 1, dtype='f')
        else:
            self.__values = linspace(-4, 4, self.__steps[i] + 2, dtype='f')

        self.__errors = [None] * self.__values.size
        for i, value in enumerate(self.__values):
            self.__values[i] = value
            # parameters = self.__parameters.copy()
            # parameters[self.__current_step] = value
            # self.request_normals(parameters, i)
            self.request_normals((self.__current_step, value), i)

    def receive_normals(self, normals, index=None):
        if index == 'init':
            return
        elif index == 'pre':
            self.__get_parameter(self.__current_step + 1)
            return

        light = self.estimate_light(normals)
        shadows = normals.dot(light)

        if index is None:
            self.finish(normals, shadows)
            return

        self.__errors[index] = self.get_image_deviation(shadows, normals)

        if None in self.__errors:
            return

        errors = array(self.__errors)
        max_error = errors.max()
        if self.__loop >= self.__determined_loops:
            X = exp(- errors / max_error).sum()
            v = rand()
            best_index = -1
            t = 0
            for i, error in enumerate(errors):
                e = exp(error / max_error) / X
                t += e
                if v <= e:
                    best_index = i
                    break
                else:
                    v -= e
            if best_index == -1:
{'fraction_non_alphanumeric': 0.10166503428011753, 'fraction_numerical': 0.0074436826640548485, 'mean_word_length': 1.8912797281993206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '342555', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1430, 'n_words': 367}