<commit_before>from unittest import TestCase
from numpy import array, allclose

from src import ModelFitter


class ModelFitterTest(TestCase):
    def test_constructor(self):
        self.assertIsInstance(ModelFitter(array([])), ModelFitter)

    def test_check(self):
        N = array([[1, 0, 1, 1],
                   [0, 1, 1, 1],
                   [1, 2, 2, 1]])
        light = array([.5, .1, 1., 0.])
        image = N.dot(light)

        fit = ModelFitter(image)
        x = fit.check(N)
        self.assertTrue(allclose(N.dot(x), N.dot(light)))<commit_msg>Implemented Fitter unit test for face model light fitting<commit_after>from unittest import TestCase
from numpy import array, allclose, load
from PIL import Image

from src import ModelFitter
from data import get_datafile_path


class ModelFitterTest(TestCase):
    def test_constructor(self):
        self.assertIsInstance(ModelFitter(array([])), ModelFitter)

    def test_check(self):
        N = array([[1, 0, 1, 1],
                   [0, 1, 1, 1],
                   [1, 2, 2, 1]])
        light = array([.5, .1, 1., 0.])
        image = N.dot(light)

        fit = ModelFitter(image)
        x = fit.check(N)
        self.assertTrue(allclose(N.dot(x), N.dot(light)))

    def test_check_face(self):
        real_light = [0.337397462814, -0.496287279542,
                     -0.560942790202, -0.050649723592442575]
        normals_filename = get_datafile_path('test.npy')
        model_filename = get_datafile_path('test.png')

        normals = load(normals_filename)

        image = Image.open(model_filename).convert('L')
        shadows = array(image.getdata()).astype('f') / 255
        image.close()

        fit = ModelFitter(shadows)
        x = fit.check(normals)

        result = get_image(normals, x)
        real_result = get_image(normals, real_light)

        data = get_difference(result, shadows, normals)
        real_data = get_difference(real_result, shadows, normals)

        self.assertLessEqual(data.sum(), real_data.sum())

def get_image(normals, light):
    result = normals.dot(light)
    # TODO: results should be cut to these borders
    # result[result < 0.] = 0.
    # result[result > 1.] = 1.
    return result

def get_difference(result, shadows, normals):
    difference = (result - shadows)**2
    difference[normals[:, 3] == 0.] = 0.
    return difference
{'fraction_non_alphanumeric': 0.12558139534883722, 'fraction_numerical': 0.0427061310782241, 'mean_word_length': 2.6012176560121767, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '6579505', 'n_tokens_mistral': 833, 'n_tokens_neox': 756, 'n_words': 183}