Coverage for colour/colorimetry/tests/test_generation.py: 100%

76 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-15 19:01 +1300

1"""Define the unit tests for the :mod:`colour.colorimetry.generation` module.""" 

2 

3from __future__ import annotations 

4 

5import numpy as np 

6 

7from colour.colorimetry.generation import ( 

8 msds_constant, 

9 msds_ones, 

10 msds_zeros, 

11 sd_constant, 

12 sd_gaussian_fwhm, 

13 sd_gaussian_normal, 

14 sd_multi_leds_Ohno2005, 

15 sd_ones, 

16 sd_single_led_Ohno2005, 

17 sd_zeros, 

18) 

19from colour.constants import TOLERANCE_ABSOLUTE_TESTS 

20 

21__author__ = "Colour Developers" 

22__copyright__ = "Copyright 2013 Colour Developers" 

23__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" 

24__maintainer__ = "Colour Developers" 

25__email__ = "colour-developers@colour-science.org" 

26__status__ = "Production" 

27 

28__all__ = [ 

29 "TestSdConstant", 

30 "TestSdZeros", 

31 "TestSdOnes", 

32 "TestMsdsConstant", 

33 "TestMsdsZeros", 

34 "TestMsdsOnes", 

35 "TestSdGaussianNormal", 

36 "TestSdGaussianFwhm", 

37 "TestSdSingleLedOhno2005", 

38 "TestSdMultiLedsOhno2005", 

39] 

40 

41 

42class TestSdConstant: 

43 """ 

44 Define :func:`colour.colorimetry.generation.sd_constant` definition unit 

45 tests methods. 

46 """ 

47 

48 def test_sd_constant(self) -> None: 

49 """Test :func:`colour.colorimetry.generation.sd_constant` definition.""" 

50 

51 sd = sd_constant(np.pi) 

52 

53 np.testing.assert_allclose(sd[360], np.pi, atol=TOLERANCE_ABSOLUTE_TESTS) 

54 

55 np.testing.assert_allclose(sd[555], np.pi, atol=TOLERANCE_ABSOLUTE_TESTS) 

56 

57 np.testing.assert_allclose(sd[780], np.pi, atol=TOLERANCE_ABSOLUTE_TESTS) 

58 

59 

60class TestSdZeros: 

61 """ 

62 Define :func:`colour.colorimetry.generation.sd_zeros` definition unit 

63 tests methods. 

64 """ 

65 

66 def test_sd_zeros(self) -> None: 

67 """ 

68 Test :func:`colour.colorimetry.generation.sd_zeros` 

69 definition. 

70 """ 

71 

72 sd = sd_zeros() 

73 

74 assert sd[360] == 0 

75 

76 assert sd[555] == 0 

77 

78 assert sd[780] == 0 

79 

80 

81class TestSdOnes: 

82 """ 

83 Define :func:`colour.colorimetry.generation.sd_ones` definition unit 

84 tests methods. 

85 """ 

86 

87 def test_sd_ones(self) -> None: 

88 """Test :func:`colour.colorimetry.generation.sd_ones` definition.""" 

89 

90 sd = sd_ones() 

91 

92 assert sd[360] == 1 

93 

94 assert sd[555] == 1 

95 

96 assert sd[780] == 1 

97 

98 

99class TestMsdsConstant: 

100 """ 

101 Define :func:`colour.colorimetry.generation.msds_constant` definition unit 

102 tests methods. 

103 """ 

104 

105 def test_msds_constant(self) -> None: 

106 """Test :func:`colour.colorimetry.generation.msds_constant` definition.""" 

107 

108 msds = msds_constant(np.pi, labels=["a", "b", "c"]) 

109 

110 np.testing.assert_allclose( 

111 msds[360], 

112 np.array([np.pi, np.pi, np.pi]), 

113 atol=TOLERANCE_ABSOLUTE_TESTS, 

114 ) 

115 

116 np.testing.assert_allclose( 

117 msds[555], 

118 np.array([np.pi, np.pi, np.pi]), 

119 atol=TOLERANCE_ABSOLUTE_TESTS, 

120 ) 

121 

122 np.testing.assert_allclose( 

123 msds[780], 

124 np.array([np.pi, np.pi, np.pi]), 

125 atol=TOLERANCE_ABSOLUTE_TESTS, 

126 ) 

127 

128 

129class TestMsdsZeros: 

130 """ 

131 Define :func:`colour.colorimetry.generation.msds_zeros` definition unit 

132 tests methods. 

133 """ 

134 

135 def test_msds_zeros(self) -> None: 

136 """ 

137 Test :func:`colour.colorimetry.generation.msds_zeros` 

138 definition. 

139 """ 

140 

141 msds = msds_zeros(labels=["a", "b", "c"]) 

142 

143 np.testing.assert_equal(msds[360], np.array([0, 0, 0])) 

144 

145 np.testing.assert_equal(msds[555], np.array([0, 0, 0])) 

146 

147 np.testing.assert_equal(msds[780], np.array([0, 0, 0])) 

148 

149 

150class TestMsdsOnes: 

151 """ 

152 Define :func:`colour.colorimetry.generation.msds_ones` definition unit 

153 tests methods. 

154 """ 

155 

156 def test_msds_ones(self) -> None: 

157 """Test :func:`colour.colorimetry.generation.msds_ones` definition.""" 

158 

159 msds = msds_ones(labels=["a", "b", "c"]) 

160 

161 np.testing.assert_equal(msds[360], np.array([1, 1, 1])) 

162 

163 np.testing.assert_equal(msds[555], np.array([1, 1, 1])) 

164 

165 np.testing.assert_equal(msds[780], np.array([1, 1, 1])) 

166 

167 

168class TestSdGaussianNormal: 

169 """ 

170 Define :func:`colour.colorimetry.generation.sd_gaussian_normal` 

171 definition unit tests methods. 

172 """ 

173 

174 def test_sd_gaussian_normal(self) -> None: 

175 """ 

176 Test :func:`colour.colorimetry.generation.sd_gaussian_normal` 

177 definition. 

178 """ 

179 

180 sd = sd_gaussian_normal(555, 25) 

181 

182 np.testing.assert_allclose( 

183 sd[530], 0.606530659712633, atol=TOLERANCE_ABSOLUTE_TESTS 

184 ) 

185 

186 np.testing.assert_allclose(sd[555], 1, atol=TOLERANCE_ABSOLUTE_TESTS) 

187 

188 np.testing.assert_allclose( 

189 sd[580], 0.606530659712633, atol=TOLERANCE_ABSOLUTE_TESTS 

190 ) 

191 

192 

193class TestSdGaussianFwhm: 

194 """ 

195 Define :func:`colour.colorimetry.generation.sd_gaussian_fwhm` definition 

196 unit tests methods. 

197 """ 

198 

199 def test_sd_gaussian_fwhm(self) -> None: 

200 """ 

201 Test :func:`colour.colorimetry.generation.sd_gaussian_fwhm` definition. 

202 """ 

203 

204 sd = sd_gaussian_fwhm(555, 25) 

205 

206 np.testing.assert_allclose(sd[530], 0.0625, atol=TOLERANCE_ABSOLUTE_TESTS) 

207 

208 np.testing.assert_allclose(sd[555], 1, atol=TOLERANCE_ABSOLUTE_TESTS) 

209 

210 np.testing.assert_allclose( 

211 sd[580], 0.062499999999999, atol=TOLERANCE_ABSOLUTE_TESTS 

212 ) 

213 

214 np.testing.assert_allclose(sd[555 - 25 / 2], 0.5, atol=TOLERANCE_ABSOLUTE_TESTS) 

215 

216 

217class TestSdSingleLedOhno2005: 

218 """ 

219 Define :func:`colour.colorimetry.generation.sd_single_led_Ohno2005` 

220 definition unit tests methods. 

221 """ 

222 

223 def test_sd_single_led_Ohno2005(self) -> None: 

224 """ 

225 Test :func:`colour.colorimetry.generation.sd_single_led_Ohno2005` 

226 definition. 

227 """ 

228 

229 sd = sd_single_led_Ohno2005(555, 25) 

230 

231 np.testing.assert_allclose( 

232 sd[530], 0.127118445056538, atol=TOLERANCE_ABSOLUTE_TESTS 

233 ) 

234 

235 np.testing.assert_allclose(sd[555], 1, atol=TOLERANCE_ABSOLUTE_TESTS) 

236 

237 np.testing.assert_allclose( 

238 sd[580], 0.127118445056538, atol=TOLERANCE_ABSOLUTE_TESTS 

239 ) 

240 

241 

242class TestSdMultiLedsOhno2005: 

243 """ 

244 Define :func:`colour.colorimetry.generation.sd_multi_leds_Ohno2005` 

245 definition unit tests methods. 

246 """ 

247 

248 def test_sd_multi_leds_Ohno2005(self) -> None: 

249 """ 

250 Test :func:`colour.colorimetry.generation.sd_multi_leds_Ohno2005` 

251 definition. 

252 """ 

253 

254 sd = sd_multi_leds_Ohno2005( 

255 np.array([457, 530, 615]), 

256 np.array([20, 30, 20]), 

257 np.array([0.731, 1.000, 1.660]), 

258 ) 

259 

260 np.testing.assert_allclose( 

261 sd[500], 0.129513248576116, atol=TOLERANCE_ABSOLUTE_TESTS 

262 ) 

263 

264 np.testing.assert_allclose( 

265 sd[570], 0.059932156222703, atol=TOLERANCE_ABSOLUTE_TESTS 

266 ) 

267 

268 np.testing.assert_allclose( 

269 sd[640], 0.116433257970624, atol=TOLERANCE_ABSOLUTE_TESTS 

270 ) 

271 

272 sd = sd_multi_leds_Ohno2005( 

273 np.array([457, 530, 615]), 

274 np.array([20, 30, 20]), 

275 ) 

276 

277 np.testing.assert_allclose( 

278 sd[500], 0.130394510062799, atol=TOLERANCE_ABSOLUTE_TESTS 

279 ) 

280 

281 np.testing.assert_allclose( 

282 sd[570], 0.058539618824187, atol=TOLERANCE_ABSOLUTE_TESTS 

283 ) 

284 

285 np.testing.assert_allclose( 

286 sd[640], 0.070140708922879, atol=TOLERANCE_ABSOLUTE_TESTS 

287 )