Coverage for algebra/tests/test_extrapolation.py: 100%
60 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-16 22:49 +1300
1"""Define the unit tests for the :mod:`colour.algebra.extrapolation` module."""
3from __future__ import annotations
5from itertools import product
7import numpy as np
9from colour.algebra import (
10 CubicSplineInterpolator,
11 Extrapolator,
12 LinearInterpolator,
13 PchipInterpolator,
14)
15from colour.constants import TOLERANCE_ABSOLUTE_TESTS
16from colour.utilities import ignore_numpy_errors, is_scipy_installed
18__author__ = "Colour Developers"
19__copyright__ = "Copyright 2013 Colour Developers"
20__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
21__maintainer__ = "Colour Developers"
22__email__ = "colour-developers@colour-science.org"
23__status__ = "Production"
25__all__ = [
26 "TestExtrapolator",
27]
30class TestExtrapolator:
31 """
32 Define :class:`colour.algebra.extrapolation.Extrapolator` class unit
33 tests methods.
34 """
36 def test_required_attributes(self) -> None:
37 """Test the presence of required attributes."""
39 required_attributes = ("interpolator",)
41 for attribute in required_attributes:
42 assert attribute in dir(Extrapolator)
44 def test_required_methods(self) -> None:
45 """Test the presence of required methods."""
47 required_methods = ("__init__",)
49 for method in required_methods: # pragma: no cover
50 assert method in dir(Extrapolator)
52 def test_interpolator(self) -> None:
53 """
54 Test :attr:`colour.algebra.extrapolation.Extrapolator.interpolator`
55 property.
56 """
58 extrapolator = Extrapolator(
59 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7]))
60 )
61 assert isinstance(extrapolator.interpolator, LinearInterpolator)
63 def test_method(self) -> None:
64 """
65 Test :attr:`colour.algebra.extrapolation.Extrapolator.method`
66 property.
67 """
69 extrapolator = Extrapolator(
70 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7]))
71 )
72 assert extrapolator.method == "linear"
74 extrapolator = Extrapolator(
75 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])),
76 method="Constant",
77 )
78 assert extrapolator.method == "constant"
80 def test_left(self) -> None:
81 """
82 Test :attr:`colour.algebra.extrapolation.Extrapolator.left`
83 property.
84 """
86 extrapolator = Extrapolator(
87 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])),
88 left=0,
89 )
90 assert extrapolator.left == 0
92 def test_right(self) -> None:
93 """
94 Test :attr:`colour.algebra.extrapolation.Extrapolator.right`
95 property.
96 """
98 extrapolator = Extrapolator(
99 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])),
100 right=0,
101 )
102 assert extrapolator.right == 0
104 def test__call__(self) -> None:
105 """
106 Test :meth:`colour.algebra.extrapolation.Extrapolator.__call__`
107 method.
108 """
110 if not is_scipy_installed(): # pragma: no cover
111 return
113 extrapolator = Extrapolator(
114 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7]))
115 )
116 np.testing.assert_array_equal(extrapolator((4, 8)), (4, 8))
117 assert extrapolator(4) == 4
119 extrapolator = Extrapolator(
120 LinearInterpolator(np.array([3, 4, 5]), np.array([1, 2, 3])),
121 method="Constant",
122 )
123 np.testing.assert_array_equal(extrapolator((0.1, 0.2, 8, 9)), (1, 1, 3, 3))
124 assert extrapolator(0.1) == 1.0
126 extrapolator = Extrapolator(
127 LinearInterpolator(np.array([3, 4, 5]), np.array([1, 2, 3])),
128 method="Constant",
129 left=0,
130 )
131 np.testing.assert_array_equal(extrapolator((0.1, 0.2, 8, 9)), (0, 0, 3, 3))
132 assert extrapolator(0.1) == 0
134 extrapolator = Extrapolator(
135 LinearInterpolator(np.array([3, 4, 5]), np.array([1, 2, 3])),
136 method="Constant",
137 right=0,
138 )
139 np.testing.assert_array_equal(extrapolator((0.1, 0.2, 8, 9)), (1, 1, 0, 0))
140 assert extrapolator(9) == 0
142 extrapolator = Extrapolator(
143 CubicSplineInterpolator(np.array([3, 4, 5, 6]), np.array([1, 2, 3, 4]))
144 )
145 np.testing.assert_allclose(
146 extrapolator((0.1, 0.2, 8.0, 9.0)),
147 (-1.9, -1.8, 6.0, 7.0),
148 atol=TOLERANCE_ABSOLUTE_TESTS,
149 )
150 assert extrapolator(9) == 7
152 extrapolator = Extrapolator(
153 PchipInterpolator(np.array([3, 4, 5]), np.array([1, 2, 3]))
154 )
155 np.testing.assert_allclose(
156 extrapolator((0.1, 0.2, 8.0, 9.0)),
157 (-1.9, -1.8, 6.0, 7.0),
158 atol=TOLERANCE_ABSOLUTE_TESTS,
159 )
160 assert extrapolator(9) == 7.0
162 @ignore_numpy_errors
163 def test_nan__call__(self) -> None:
164 """
165 Test :method:`colour.algebra.extrapolation.Extrapolator.__call__`
166 method nan support.
167 """
169 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
170 cases = np.array(list(set(product(cases, repeat=3))))
171 for case in cases:
172 extrapolator = Extrapolator(LinearInterpolator(case, case))
173 extrapolator(case[0])