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

1"""Define the unit tests for the :mod:`colour.algebra.extrapolation` module.""" 

2 

3from __future__ import annotations 

4 

5from itertools import product 

6 

7import numpy as np 

8 

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 

17 

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" 

24 

25__all__ = [ 

26 "TestExtrapolator", 

27] 

28 

29 

30class TestExtrapolator: 

31 """ 

32 Define :class:`colour.algebra.extrapolation.Extrapolator` class unit 

33 tests methods. 

34 """ 

35 

36 def test_required_attributes(self) -> None: 

37 """Test the presence of required attributes.""" 

38 

39 required_attributes = ("interpolator",) 

40 

41 for attribute in required_attributes: 

42 assert attribute in dir(Extrapolator) 

43 

44 def test_required_methods(self) -> None: 

45 """Test the presence of required methods.""" 

46 

47 required_methods = ("__init__",) 

48 

49 for method in required_methods: # pragma: no cover 

50 assert method in dir(Extrapolator) 

51 

52 def test_interpolator(self) -> None: 

53 """ 

54 Test :attr:`colour.algebra.extrapolation.Extrapolator.interpolator` 

55 property. 

56 """ 

57 

58 extrapolator = Extrapolator( 

59 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])) 

60 ) 

61 assert isinstance(extrapolator.interpolator, LinearInterpolator) 

62 

63 def test_method(self) -> None: 

64 """ 

65 Test :attr:`colour.algebra.extrapolation.Extrapolator.method` 

66 property. 

67 """ 

68 

69 extrapolator = Extrapolator( 

70 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])) 

71 ) 

72 assert extrapolator.method == "linear" 

73 

74 extrapolator = Extrapolator( 

75 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])), 

76 method="Constant", 

77 ) 

78 assert extrapolator.method == "constant" 

79 

80 def test_left(self) -> None: 

81 """ 

82 Test :attr:`colour.algebra.extrapolation.Extrapolator.left` 

83 property. 

84 """ 

85 

86 extrapolator = Extrapolator( 

87 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])), 

88 left=0, 

89 ) 

90 assert extrapolator.left == 0 

91 

92 def test_right(self) -> None: 

93 """ 

94 Test :attr:`colour.algebra.extrapolation.Extrapolator.right` 

95 property. 

96 """ 

97 

98 extrapolator = Extrapolator( 

99 LinearInterpolator(np.array([5, 6, 7]), np.array([5, 6, 7])), 

100 right=0, 

101 ) 

102 assert extrapolator.right == 0 

103 

104 def test__call__(self) -> None: 

105 """ 

106 Test :meth:`colour.algebra.extrapolation.Extrapolator.__call__` 

107 method. 

108 """ 

109 

110 if not is_scipy_installed(): # pragma: no cover 

111 return 

112 

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 

118 

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 

125 

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 

133 

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 

141 

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 

151 

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 

161 

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 """ 

168 

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])