Skip to content

Commit

Permalink
Implement unit tests for "colour.CIECAM02_to_XYZ" definition updated …
Browse files Browse the repository at this point in the history
…signature.
  • Loading branch information
KelSolaar committed Jun 17, 2017
1 parent d986ab6 commit 2a4a6f8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 44 deletions.
8 changes: 7 additions & 1 deletion colour/appearance/ciecam02.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ def CIECAM02_to_XYZ(CIECAM02_specification,
XYZ : ndarray
*CIE XYZ* tristimulus values.
Raises
------
ValueError
If neither *C* or *M* correlates have been defined in the
`CIECAM02_specification` argument.
Warning
-------
The output range of that definition is non standard!
Expand Down Expand Up @@ -374,7 +380,7 @@ def CIECAM02_to_XYZ(CIECAM02_specification,
if C is None and M is not None:
C = M / F_L ** 0.25
elif C is None:
raise ValueError('Either "C" or "M" correlate must be specified in '
raise ValueError('Either "C" or "M" correlate must be defined in '
'the "CIECAM02_specification" argument!')

# Converting *CIE XYZ* tristimulus values to *CMCCAT2000* transform
Expand Down
32 changes: 18 additions & 14 deletions colour/appearance/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import csv
import numpy as np
import os
from abc import abstractmethod
from abc import ABCMeta, abstractmethod
from collections import defaultdict

__author__ = 'Colour Developers'
Expand All @@ -28,7 +28,7 @@ class ColourAppearanceModelTest(object):
Defines the base class for tests of: mod:`colour.appearance` package.
Each colour appearance model is tested against a respective '.csv' file
from which content has been generated from data of the following file by
whose content has been generated from data of the following file by
*Fairchild (2013)*: http://rit-mcsl.org/fairchild//files/AppModEx.xls
Methods
Expand All @@ -40,6 +40,8 @@ class ColourAppearanceModelTest(object):
test_forward_examples
"""

__metaclass__ = ABCMeta

FIXTURE_BASENAME = None
"""
'.csv' file fixture path for the colour appearance model being tested,
Expand Down Expand Up @@ -138,17 +140,19 @@ def check_specification_attribute(self, case, data, attribute, expected):
'Expected: "{2}" \n'
'Received "{3}"').format(attribute, case, expected, value)

np.testing.assert_allclose(value,
expected,
err_msg=error_message,
rtol=0.01,
atol=0.01,
verbose=False)
np.testing.assert_allclose(
value,
expected,
err_msg=error_message,
rtol=0.01,
atol=0.01,
verbose=False)

np.testing.assert_almost_equal(value,
expected,
decimal=1,
err_msg=error_message)
np.testing.assert_almost_equal(
value,
expected,
decimal=1,
err_msg=error_message)

def check_model_consistency(self, data, output_attributes):
"""
Expand Down Expand Up @@ -202,8 +206,8 @@ def test_examples(self):
"""

for data in self.fixtures():
for test in self.check_model_consistency(data,
self.OUTPUT_ATTRIBUTES):
for test in self.check_model_consistency(
data, self.OUTPUT_ATTRIBUTES):
yield test

def test_n_dimensional_examples(self):
Expand Down
77 changes: 48 additions & 29 deletions colour/appearance/tests/tests_ciecam02.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
XYZ_to_CIECAM02,
CIECAM02_to_XYZ)
from colour.appearance.tests.common import ColourAppearanceModelTest
from colour.utilities import ignore_numpy_errors, tsplit, tstack
from colour.utilities import as_namedtuple, ignore_numpy_errors, tsplit, tstack

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2017 - Colour Developers'
Expand Down Expand Up @@ -88,27 +88,37 @@ class TestCIECAM02ColourAppearanceModelReverse(ColourAppearanceModelTest):
'Y': 1,
'Z': 2}

def output_specification_from_data(self, data):
def output_specification_from_data(self, data, correlates):
"""
Returns the *CIECAM02* colour appearance model output specification
from given data.
Returns the *CIE XYZ* tristimulus values from given *CIECAM02* colour
appearance model input data.
Parameters
----------
data : list
Fixture data.
correlates : array_like
Correlates used to build the input *CIECAM02* colour appearance
model specification.
Returns
-------
CIECAM02_Specification
*CIECAM02* colour appearance model specification.
array_like
*CIE XYZ* tristimulus values
Warning
-------
The method name does not reflect the underlying implementation.
"""

XYZ_w = tstack((data['X_w'], data['Y_w'], data['Z_w']))

XYZ = CIECAM02_to_XYZ(CIECAM02_Specification(data['J'],
data['C'],
data['h']),
i, j, k = correlates
CIECAM02_specification = as_namedtuple(
{i: data[i], j: data[j], k: data[k]},
CIECAM02_Specification)

XYZ = CIECAM02_to_XYZ(CIECAM02_specification,
XYZ_w,
data['L_A'],
data['Y_b'],
Expand All @@ -121,7 +131,8 @@ def output_specification_from_data(self, data):

def check_specification_attribute(self, case, data, attribute, expected):
"""
Tests given colour appearance model specification attribute value.
Tests *CIE XYZ* tristimulus values output from *CIECAM02* colour
appearance model input data.
Parameters
----------
Expand All @@ -133,27 +144,35 @@ def check_specification_attribute(self, case, data, attribute, expected):
Tested attribute name.
expected : float.
Expected attribute value.
Warning
-------
The method name does not reflect the underlying implementation.
"""

XYZ = self.output_specification_from_data(data)
value = tsplit(XYZ)[attribute]

error_message = (
'Parameter "{0}" in test case "{1}" does not match target value.\n'
'Expected: "{2}" \n'
'Received "{3}"').format(attribute, case, expected, value)

np.testing.assert_allclose(value,
expected,
err_msg=error_message,
rtol=0.01,
atol=0.01,
verbose=False)

np.testing.assert_almost_equal(value,
expected,
decimal=1,
err_msg=error_message)
for correlates in (('J', 'C', 'h'), ('J', 'M', 'h')):
XYZ = self.output_specification_from_data(data, correlates)
value = tsplit(XYZ)[attribute]

error_message = (
'Parameter "{0}" in test case "{1}" '
'does not match target value.\n'
'Expected: "{2}" \n'
'Received "{3}"').format(attribute, case, expected, value)

np.testing.assert_allclose(
value,
expected,
err_msg=error_message,
rtol=0.01,
atol=0.01,
verbose=False)

np.testing.assert_almost_equal(
value,
expected,
decimal=1,
err_msg=error_message)

@ignore_numpy_errors
def test_nan_XYZ_to_CIECAM02(self):
Expand Down

0 comments on commit 2a4a6f8

Please sign in to comment.