Exemple #1
0
    def test_HPminDE(self):
        c_data = data.Points(space.cielab, cube + np.array([0, -5, -5]))
        g = gamut.Gamut(space.cielab, c_data)

        points = np.array([[0, 8, 8], [4, 0, 9], [4, 4, 3], [0, 10, 0],
                           [15, 1, 0]])
        fasit = np.array([[0, 5, 5], [4, 0, 5], [4, 4, 3], [0, 5, 0],
                          [10, 1, 0]])
        c_data = data.Points(space.cielab, points)
        re_data = g.HPminDE(c_data)
        re_data = re_data.get_flattened(space.cielab)
        self.assertTrue(np.allclose(fasit, re_data))
Exemple #2
0
 def test_implicit_convert(self):
     lab1 = d1.get(space.cielab)
     lab2 = d2.get(space.cielab)
     lab3 = d3.get(space.cielab)
     lab4 = d4.get(space.cielab)
     dd1 = data.Points(space.cielab, lab1)
     dd2 = data.Points(space.cielab, lab2)
     dd3 = data.Points(space.cielab, lab3)
     dd4 = data.Points(space.cielab, lab4)
     self.assertTrue(np.allclose(col1, dd1.get(space.xyz)))
     self.assertTrue(np.allclose(col2, dd2.get(space.xyz)))
     self.assertTrue(np.allclose(col3, dd3.get(space.xyz)))
     self.assertTrue(np.allclose(col4, dd4.get(space.xyz)))
Exemple #3
0
    def test_minDE(self):
        sphere = self.generate_sphere(6, 10)
        sphere = sphere + np.array([5, 5, 5])
        c_sphere = data.Points(space.cielab, sphere)

        g_cube = data.Points(space.cielab, cube)
        g = gamut.Gamut(space.cielab, g_cube)
        mapped_im = g.minDE(c_sphere)

        result = True
        for index, value in np.ndenumerate(
                mapped_im.get_flattened(space.cielab)):
            if value > 10:
                result = False
        self.assertTrue(result)
Exemple #4
0
 def test_gamut_initialize(self):
     c_data = data.Points(space.srgb,
                          cube)  # Generating the colour Points object
     g = gamut.Gamut(space.srgb, c_data)
     self.assertTrue(np.allclose(
         cube_vertices,
         g.vertices))  # Check that the gamut's vertices are correct.
Exemple #5
0
    def test_true_shape(self):

        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        a = np.array([[0, 0, 0], [2, 2, 2], [2, 2, 2]])
        g.true_shape(a)

        # Test remove duplicates
        a = np.array([[0, 0, 0], [2, 2, 2], [0, 0, 0], [2, 2, 2]])
        self.assertEqual(2, g.true_shape(a).shape[0])

        # Test 3 points on the same line should return outer points
        a = np.array([[0, 0, 0], [2, 2, 2], [3, 3, 3]])
        self.assertTrue(
            np.allclose(g.true_shape(a), np.array([[0, 0, 0], [3, 3, 3]])))

        # Test 4 points that are actually a triangle
        a = np.array([[0, 0, 0], [0, 3, 0], [3, 0, 0], [1, 1, 0]])
        self.assertTrue(
            np.allclose(g.true_shape(a),
                        np.array([[0, 0, 0], [0, 3, 0], [3, 0, 0]])))

        # Test 4 points that are all other vertices in a convex polygon
        a = np.array([[0, 0, 0], [0, 3, 0], [3, 0, 0], [5, 5, 0]])
        self.assertTrue(
            np.allclose(g.true_shape(a),
                        np.array([[0, 0, 0], [0, 3, 0], [3, 0, 0], [5, 5,
                                                                    0]])))
Exemple #6
0
    def test_compress(self):
        c_data = data.Points(space.srgb,
                             cube)  # Generating the colour Points object.
        g = gamut.Gamut(space.srgb, c_data)  # Creates a new gamut.

        col_data = data.Points(
            space.srgb,
            np.array([[15, 15, 15], [8, 8, 8], [5, 5, 5], [1, 1, 1],
                      [-5, -5, -5]]))
        re_data = g.compress_axis(space.srgb, col_data,
                                  2).get_flattened(space.srgb)

        fasit_data = np.array([[15, 15, 10], [8, 8, 6], [5, 5, 5], [1, 1, 3],
                               [-5, -5, 0]])

        self.assertTrue(np.allclose(fasit_data, re_data))
Exemple #7
0
    def test_clip_nearest(self):
        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        points = np.array([[5, 5, 15], [5, 5, 15], [5, 5,
                                                    15]])  # points to map
        mod_points = np.array([[5, 5, 10], [5, 5, 10], [5, 5,
                                                        10]])  # wanted result

        c_data = data.Points(space.srgb, points)  # data.Points object
        re_data = g.clip_nearest(space.srgb,
                                 c_data)  # data.Points object returned

        self.assertTrue(
            np.allclose(re_data.get_flattened(space.srgb),
                        mod_points))  # assert that the points are changed
Exemple #8
0
    def test_in_line(self):
        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        self.assertTrue(
            g.in_line(np.array([[2, 2, 2], [2, 2, 2]]),
                      np.array([2, 2, 2])))  # All points equal.
        self.assertFalse(g.in_line(line,
                                   point_not_in_line))  # Point in NOT on line
        self.assertFalse(g.in_line(line, point_opposite_direction_than_line)
                         )  # Point opposite dir then line
        self.assertFalse(g.in_line(
            line,
            point_further_away_than_line))  # Point is is further then line
        self.assertTrue(g.in_line(line, point_in_line))  # Point is on line
        self.assertFalse(
            g.in_line(np.array([[3, 3, 3], [4, 4, 4]]),
                      np.array([5, 5, 5])))  # Point is on line

        self.assertFalse(g.interior(line,
                                    point_not_in_line))  # Point in NOT on line
        self.assertFalse(g.interior(line, point_opposite_direction_than_line)
                         )  # Point opposite dir then line
        self.assertFalse(g.interior(
            line,
            point_further_away_than_line))  # Point is is further then line
        self.assertTrue(g.interior(line, point_in_line))  # Point is on line
        self.assertFalse(
            g.interior(np.array([[3, 3, 3], [4, 4, 4]]),
                       np.array([5, 5, 5])))  # Point is on line
Exemple #9
0
    def test_get_vertices(self):
        c_data = data.Points(space.srgb,
                             cube)  # Generating the colour Points object
        g = gamut.Gamut(space.srgb, c_data)
        n1_data = np.array([
            [
                0, 0, 0
            ],  # 0  vertices     Array with just the vertices used for comparison.
            [10, 0, 0],  # 1  vertices
            [10, 10, 0],  # 2  vertices
            [0, 10, 0],  # 3  vertices
            [10, 10, 10],  # 6  vertices
            [10, 0, 10],  # 8  vertices
            [0, 0, 10],  # 9  vertices
            [0, 10, 10]
        ])  # 10 vertices

        vertices = g.get_vertices(cube)
        self.assertTrue(np.array_equiv(
            n1_data,
            vertices))  # Compares return array with the known vertices array.

        vertices = g.get_vertices(
            cube)  # Calls the function and add the vertices to the array.
        self.assertTrue(
            np.array_equiv(n1_data, vertices)
        )  # Compares returned array with the known vertices array.
Exemple #10
0
    def test_modified_convex_hull(self):

        # c_data = data.Points(space.srgb, cube)
        # g = gamut.Gamut(space.srgb, c_data)

        test_points = np.array([
            [
                0, 0, 0
            ],  # 0  vertices  # Array with just the vertices used for comparison.
            [10, 0, 0],  # 1  vertices
            [10, 10, 0],  # 2  vertices
            [0, 10, 0],  # 3  vertices
            [10, 10, 10],  # 6  vertices
            [10, 0, 10],  # 8  vertices
            [0, 0, 10],  # 9  vertices
            [0, 10, 10],  # 10 vertices
            [4.999, 4.999, 0]
        ])  # Only a vertex in modified hull

        c_data = data.Points(space.srgb, test_points)
        g = gamut.Gamut(space.srgb,
                        c_data,
                        gamma=0.2,
                        center=np.array([5, 5, 5]))

        self.assertTrue(g.vertices.shape[0] == 9)
Exemple #11
0
 def test_get_alpha(self):
     c_data = data.Points(space.srgb,
                          cube)  # Generating the colour Points object.
     g = gamut.Gamut(space.srgb, c_data)  # Creates a new gamut.
     d = [0.001, 0.2, 0.2]
     center = [10, 11, 14]
     n = [5, 3, 2, 9]
     a = g.get_alpha(d, center, n)
Exemple #12
0
    def test_center_of_mass(self):
        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        cm = g.center_of_mass(g.get_vertices(
            g.hull.points))  # Get coordinate for center of the cube
        cp = np.array([5., 5., 5.])  # Point in center of cube.
        self.assertEqual(cp.all(),
                         cm.all())  # Assert true that the points are the same.
Exemple #13
0
    def test_find_plane(self):
        p_data = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        c_data = data.Points(space.srgb,
                             cube)  # Generating the colour Points object.
        g = gamut.Gamut(space.srgb, c_data)  # Creates a new gamut.

        d = g.find_plane(p_data)
        r = np.array([-0.57735027, -0.57735027, -0.57735027, -0.57735027])
        np.alltrue(d == r)
Exemple #14
0
    def test_constructor(self):
        g = gamut.Gamut(space.srgb, data.Points(space.srgb, cube), gamma=1)
        self.assertTrue(isinstance(g, gamut.Gamut))

        g = gamut.Gamut(space.srgb, data.Points(space.srgb, cube), gamma=.2)
        self.assertTrue(isinstance(g, gamut.Gamut))

        g = gamut.Gamut(space.srgb,
                        data.Points(space.srgb, cube),
                        center=[.5, .5, .5],
                        gamma=1)
        self.assertTrue(isinstance(g, gamut.Gamut))

        g = gamut.Gamut(space.srgb,
                        data.Points(space.srgb, cube),
                        center=[.5, .5, .5],
                        gamma=.2)
        self.assertTrue(isinstance(g, gamut.Gamut))
Exemple #15
0
    def test_is_coplanar(self):
        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        points = np.array([[0, 0, 0], [2, 2, 0], [3, 3, 0],
                           [1, 1, 0]])  # coplanar points
        self.assertTrue(True, g.is_coplanar(points))

        points = np.array([[0, 0, 1], [2, 2, 0], [3, 3, 0],
                           [1, 1, 0]])  # non-coplanar points
        self.assertFalse(False, g.is_coplanar(points))
Exemple #16
0
 def test_predefined_space_jacobians(self):
     col_data = data.Points(space.xyz, col)
     test_spaces = [
         space.xyz, space.xyY, space.cielab, space.cieluv, space.cielch,
         space.ipt, space.ciede00lab, space.din99, space.din99b,
         space.din99c, space.din99d, space.srgb, space.lgj_osa, space.lgj_e,
         _test_space_cartesian, _test_space_poincare_disk, _test_space_gamma
     ]
     for sp in test_spaces:
         jac1 = sp.jacobian_XYZ(col_data)
         jac2 = sp.inv_jacobian_XYZ(col_data)
         prod = np.zeros(np.shape(jac1))
         for i in range(np.shape(jac1)[0]):
             prod[i] = np.dot(jac1[i], jac2[i])
             prod[i] = np.abs(prod[i] - np.eye(3))
         err = np.max(prod)
         self.assertTrue(err < 1e-6)
Exemple #17
0
    def test_in_tetrahedron(self):
        c_data = data.Points(space.srgb, tetrahedron)
        g = gamut.Gamut(space.srgb, c_data)

        self.assertTrue(g.in_tetrahedron(
            tetrahedron, tetra_p_inside))  # Point is on the tetrahedron
        self.assertFalse(g.in_tetrahedron(
            tetrahedron, tetra_p_not_inside))  # Point is NOT on tetrahedron
        self.assertTrue(g.in_tetrahedron(
            tetrahedron,
            tetra_p_on_surface))  # Point is on a simplex(counts as inside)

        self.assertTrue(g.interior(
            tetrahedron, tetra_p_inside))  # Point is on the tetrahedron
        self.assertFalse(g.interior(
            tetrahedron, tetra_p_not_inside))  # Point is NOT on tetrahedron
        self.assertTrue(g.interior(tetrahedron, tetra_p_on_surface))
Exemple #18
0
    def test_sign(self):
        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        # The tetrahedron should have a positive signed volume.
        self.assertEqual(
            1, g.sign(np.array([[-2, 0, 0], [0, -2, 0], [0, 0, 0], [0, 0,
                                                                    2]])))
        # The tetrahedron should have no volume.
        self.assertEqual(
            0,
            g.sign(np.array([[0, 0, 0], [2, 0, 0], [0, 2, 0], [0.5, 0.5, 0]])))
        # The tetrahedron should have a negative signed volume.
        self.assertEqual(
            -1,
            g.sign(
                np.array([[10, 10, 10], [0, 10, 10], [10, 0, 10], [10, 10,
                                                                   0]])))
Exemple #19
0
    def test_in_triangle(self):
        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        self.assertFalse(g.in_triangle(triangle, triangle_point_not_coplanar))
        self.assertFalse(
            g.in_triangle(triangle, triangle_point_coplanar_but_outside))
        self.assertTrue(g.in_triangle(triangle, triangle_point_inside))

        self.assertFalse(g.in_triangle(triangle2,
                                       triangle2_point_not_coplanar))
        self.assertFalse(
            g.in_triangle(triangle2, triangle2_point_coplanar_but_outside))
        self.assertTrue(g.in_triangle(triangle2, triangle2_point_inside))

        self.assertFalse(g.interior(triangle, triangle_point_not_coplanar))
        self.assertFalse(
            g.interior(triangle, triangle_point_coplanar_but_outside))
        self.assertTrue(g.interior(triangle, triangle_point_inside))

        self.assertFalse(g.interior(triangle2, triangle2_point_not_coplanar))
        self.assertFalse(
            g.interior(triangle2, triangle2_point_coplanar_but_outside))
        self.assertTrue(g.interior(triangle2, triangle2_point_inside))
Exemple #20
0
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import unittest
import numpy as np
from colourlab import data, space, metric, tensor, statistics

# Data

d1 = data.d_regular(space.cielab, np.linspace(0, 100, 10),
                    np.linspace(-100, 100, 21), np.linspace(-100, 100, 21))
d2 = data.Points(space.cielab, d1.get(space.cielab) + 1)
diff = metric.dE_ab(d1, d2)

d3 = data.d_regular(space.cielab, np.linspace(0, 100, 3),
                    np.linspace(-100, 100, 3), np.linspace(-100, 100, 3))

t3 = tensor.dE_ab(d3)
t4 = data.Tensors(space.cielab, t3.get(space.cielab) * 2, t3.points)

R, scale = statistics.pant_R_values(space.cielab, t3, t4)
R_plane, scale = statistics.pant_R_values(space.cielab,
                                          t3,
                                          t4,
                                          plane=t3.plane_01)
R_nonopt, scale = statistics.pant_R_values(space.cielab,
                                           t3,
Exemple #21
0
    def test_is_inside(self):
        c_data = data.Points(space.srgb, cube)
        g = gamut.Gamut(space.srgb, c_data)

        c_data = data.Points(space.srgb, points_3d)
        a = g.is_inside(space.srgb, c_data, t=True)
        self.assertEqual(
            a.shape,
            points_3d.shape[:-1])  # Asserts if shape is reduced by 1dim
        self.assertEqual(a.dtype,
                         bool)  # Asserts is data type in the array is boolean
        self.assertTrue(np.allclose(
            a, bool_3d))  # Asserts that the returned values are co

        c_data = data.Points(space.srgb, points_3d)
        a = g.is_inside(space.srgb, c_data, t=False)
        self.assertEqual(
            a.shape,
            points_3d.shape[:-1])  # Asserts if shape is reduced by 1dim
        self.assertEqual(a.dtype,
                         bool)  # Asserts is data type in the array is boolean
        self.assertTrue(np.allclose(
            a, bool_3d))  # Asserts that the returned values are correct

        c_data = data.Points(space.srgb, points_2d)
        a = g.is_inside(space.srgb, c_data, t=False)
        self.assertEqual(
            a.shape,
            points_2d.shape[:-1])  # Asserts if shape is reduced by 1dim
        self.assertEqual(a.dtype,
                         bool)  # Asserts is data type in the array is boolean
        self.assertTrue(np.allclose(
            a, bool_2d))  # Asserts that the returned values are correct

        c_data = data.Points(space.srgb, points_1d)
        a = g.is_inside(space.srgb, c_data, t=False)
        self.assertEqual(
            1, a.size)  # When only one point is sent, still returned a array
        self.assertEqual(a.dtype,
                         bool)  # Asserts is data type in the array is boolean
        self.assertTrue(np.allclose(
            a, bool_1d))  # Asserts that the returned values are co

        c_data = data.Points(space.srgb, points_1d)
        a = g.is_inside(space.srgb, c_data, t=True)
        self.assertEqual(
            1, a.size)  # When only one point is sent, still returned a array
        self.assertEqual(a.dtype,
                         bool)  # Asserts is data type in the array is boolean
        self.assertTrue(np.allclose(
            a, bool_1d))  # Asserts that the returned values are correct

        c_data = data.Points(space.srgb, self.generate_sphere(15, 100))
        g = gamut.Gamut(space.srgb, c_data)

        c_data = data.Points(space.srgb, self.generate_sphere(
            10, 15))  # Points lie within the sphere(inclusion = true)
        a = g.is_inside(space.srgb, c_data)
        self.assertTrue(np.allclose(a, np.ones(
            a.shape)))  # Assert that all points lie within the gamut

        c_data = data.Points(space.srgb, self.generate_sphere(
            20, 15))  # Points lie outside the sphere(inclusion = true)
        a = g.is_inside(space.srgb, c_data)
        self.assertTrue(np.allclose(a, np.zeros(
            a.shape)))  # Assert that all points lie without the gamut
Exemple #22
0
col3 = np.array([[1e-10, 1e-10, 1e-10], [.95, 1., 1.08], [.5, .5, .5]])
col4 = np.array([[[1e-10, 1e-10, 1e-10], [.95, 1., 1.08], [.5, .5, .5]],
                 [[1e-10, 1e-10, 1e-10], [.95, 1., 1.08], [.5, .5, .5]]])

vec1 = .1 * np.random.rand(col1.shape[0])
vec2 = .1 * np.random.rand(col2.shape[0], col2.shape[1])
vec3 = .1 * np.random.rand(col3.shape[0], col3.shape[1])
vec4 = .1 * np.random.rand(col4.shape[0], col4.shape[1], col4.shape[2])

tens1 = np.eye(3)
tens2 = np.array([np.eye(3)])
tens3 = np.array([np.eye(3), np.eye(3), np.eye(3)])
tens4 = np.array([[np.eye(3), np.eye(3), np.eye(3)],
                  [np.eye(3), np.eye(3), np.eye(3)]])

d1 = data.Points(space.xyz, col1)
d2 = data.Points(space.xyz, col2)
d3 = data.Points(space.xyz, col3)
d4 = data.Points(space.xyz, col4)

v1 = data.Vectors(space.xyz, vec1, d1)
v2 = data.Vectors(space.xyz, vec2, d2)
v3 = data.Vectors(space.xyz, vec3, d3)
v4 = data.Vectors(space.xyz, vec4, d4)

t1 = data.Tensors(space.xyz, tens1, d1)
t2 = data.Tensors(space.xyz, tens2, d2)
t3 = data.Tensors(space.xyz, tens3, d3)
t4 = data.Tensors(space.xyz, tens4, d4)

# Tests
Exemple #23
0
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import unittest
import numpy as np
from colourlab import metric, data, space, tensor

d1 = data.d_regular(space.cielab, np.linspace(20, 80, 10),
                    np.linspace(-50, 50, 11), np.linspace(-50, 50, 11))
d2 = data.Points(space.cielab, d1.get(space.cielab) + 1 / np.sqrt(3))

poincare_space = space.TransformPoincareDisk(space.cielab, 100)


class TestMetrics(unittest.TestCase):
    def test_metrics(self):
        for met in [
                metric.dE_ab, metric.dE_uv, metric.dE_00, metric.dE_DIN99,
                metric.dE_DIN99b, metric.dE_DIN99c, metric.dE_DIN99d,
                metric.dE_E
        ]:
            self.assertTrue(np.max(met(d1, d2) < 5))
        self.assertTrue(
            np.max(metric.linear(space.cielab, d1, d2, tensor.dE_ab)) < 2)
        self.assertTrue(