colour-science / colour

Colour Science for Python
https://www.colour-science.org
BSD 3-Clause "New" or "Revised" License
2.1k stars 260 forks source link

[BUG]: *nan* are generated by `colour.difference.delta_E_CIE1994` definition. #1206

Closed volrak closed 11 months ago

volrak commented 11 months ago

Description

Certain values passed into delta_E_CIE1994() cause an exception or nan result.

The cause looks like rounding of intermediate floating point values resulting in a square root of a negative number. For the example inputs in the reproduction section, the argument to sqrt is:

>>> delta_A**2 + delta_B**2 - delta_C**2
-2.5153490401663703e-17

A likely fix seems be to enforce a lower bound of 0 on this expression before the sqrt.

Code for Reproduction

from colour.difference import delta_E_CIE1994
v1 = [76.62311359, 0.81274256, 4.01990717]
v2 = [76.12591697, 0.82627158, 4.08682309]
delta_E_CIE1994(v1, v2, textiles=True)

Exception Message

~/.local/lib/python3.10/site-packages/colour/difference/delta_e.py:220: RuntimeWarning: invalid value encountered in sqrt(delta_H = np.sqrt(delta_A**2 + delta_B**2 - delta_C**2)

Environment Information

===============================================================================
*                                                                             *
*   Interpreter :                                                             *
*       python : 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]            *
*                                                                             *
*   colour-science.org :                                                      *
*       colour : 0.4.1                                                        *
*                                                                             *
*   Runtime :                                                                 *
*       imageio : 2.22.0                                                      *
*       matplotlib : 3.6.0                                                    *
*       networkx : 2.8.6                                                      *
*       numpy : 1.23.3                                                        *
*       pandas : 1.4.4                                                        *
*       scipy : 1.9.1                                                         *
*                                                                             *
===============================================================================
defaultdict(<class 'dict'>, {'Interpreter': {'python': '3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]'}, 'colour-science.org': {'colour': '0.4.1'}, 'Runtime': {'imageio': '2.22.0', 'matplotlib': '3.6.0', 'networkx': '2.8.6', 'numpy': '1.23.3', 'pandas': '1.4.4', 'scipy': '1.9.1'}})
KelSolaar commented 11 months ago

Hi @volrak,

Thank you, this is rather unfortunate the subtraction produces that. I'm wondering if we should not take the absolute value rather than clamping to zero. I will check if the CIE has some recommendations for that.

Cheers,

Thomas

KelSolaar commented 11 months ago

Implementation note from Bruce Lindbloom:

image

http://www.brucelindbloom.com

KelSolaar commented 11 months ago

Looked at most implementations and everybody seems to be clamping. I have seen only one implementation that takes the absolute value.