Coverage for algebra/coordinates/tests/test_transformations.py: 100%

127 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-16 22:49 +1300

1""" 

2Define the unit tests for the 

3:mod:`colour.algebra.coordinates.transformations` module. 

4""" 

5 

6from __future__ import annotations 

7 

8from itertools import product 

9 

10import numpy as np 

11 

12from colour.algebra import ( 

13 cartesian_to_cylindrical, 

14 cartesian_to_polar, 

15 cartesian_to_spherical, 

16 cylindrical_to_cartesian, 

17 polar_to_cartesian, 

18 spherical_to_cartesian, 

19) 

20from colour.constants import TOLERANCE_ABSOLUTE_TESTS 

21from colour.utilities import ignore_numpy_errors 

22 

23__author__ = "Colour Developers" 

24__copyright__ = "Copyright 2013 Colour Developers" 

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

26__maintainer__ = "Colour Developers" 

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

28__status__ = "Production" 

29 

30__all__ = [ 

31 "TestCartesianToSpherical", 

32 "TestSphericalToCartesian", 

33 "TestCartesianToPolar", 

34 "TestPolarToCartesian", 

35 "TestCartesianToCylindrical", 

36 "TestCylindricalToCartesian", 

37] 

38 

39 

40class TestCartesianToSpherical: 

41 """ 

42 Define :func:`colour.algebra.coordinates.transformations.\ 

43cartesian_to_spherical` definition unit tests methods. 

44 """ 

45 

46 def test_cartesian_to_spherical(self) -> None: 

47 """ 

48 Test :func:`colour.algebra.coordinates.transformations.\ 

49cartesian_to_spherical` definition. 

50 """ 

51 

52 np.testing.assert_allclose( 

53 cartesian_to_spherical(np.array([3, 1, 6])), 

54 np.array([6.78232998, 0.48504979, 0.32175055]), 

55 atol=TOLERANCE_ABSOLUTE_TESTS, 

56 ) 

57 

58 np.testing.assert_allclose( 

59 cartesian_to_spherical(np.array([-1, 9, 16])), 

60 np.array([18.38477631, 0.51501513, 1.68145355]), 

61 atol=TOLERANCE_ABSOLUTE_TESTS, 

62 ) 

63 

64 np.testing.assert_allclose( 

65 cartesian_to_spherical(np.array([6.3434, -0.9345, 18.5675])), 

66 np.array([19.64342307, 0.33250603, -0.14626640]), 

67 atol=TOLERANCE_ABSOLUTE_TESTS, 

68 ) 

69 

70 def test_n_dimensional_cartesian_to_spherical(self) -> None: 

71 """ 

72 Test :func:`colour.algebra.coordinates.transformations.\ 

73cartesian_to_spherical` definition n-dimensional arrays support. 

74 """ 

75 

76 a_i = np.array([3, 1, 6]) 

77 a_o = cartesian_to_spherical(a_i) 

78 

79 a_i = np.tile(a_i, (6, 1)) 

80 a_o = np.tile(a_o, (6, 1)) 

81 np.testing.assert_allclose( 

82 cartesian_to_spherical(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

83 ) 

84 

85 a_i = np.reshape(a_i, (2, 3, 3)) 

86 a_o = np.reshape(a_o, (2, 3, 3)) 

87 np.testing.assert_allclose( 

88 cartesian_to_spherical(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

89 ) 

90 

91 @ignore_numpy_errors 

92 def test_nan_cartesian_to_spherical(self) -> None: 

93 """ 

94 Test :func:`colour.algebra.coordinates.transformations.\ 

95cartesian_to_spherical` definition nan support. 

96 """ 

97 

98 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

99 cases = np.array(list(set(product(cases, repeat=3)))) 

100 cartesian_to_spherical(cases) 

101 

102 

103class TestSphericalToCartesian: 

104 """ 

105 Define :func:`colour.algebra.coordinates.transformations.\ 

106spherical_to_cartesian` definition unit tests methods. 

107 """ 

108 

109 def test_spherical_to_cartesian(self) -> None: 

110 """ 

111 Test :func:`colour.algebra.coordinates.transformations.\ 

112spherical_to_cartesian` definition. 

113 """ 

114 

115 np.testing.assert_allclose( 

116 spherical_to_cartesian(np.array([6.78232998, 0.48504979, 0.32175055])), 

117 np.array([3.00000000, 0.99999999, 6.00000000]), 

118 atol=TOLERANCE_ABSOLUTE_TESTS, 

119 ) 

120 

121 np.testing.assert_allclose( 

122 spherical_to_cartesian(np.array([18.38477631, 0.51501513, 1.68145355])), 

123 np.array([-1.00000003, 9.00000007, 15.99999996]), 

124 atol=TOLERANCE_ABSOLUTE_TESTS, 

125 ) 

126 

127 np.testing.assert_allclose( 

128 spherical_to_cartesian(np.array([19.64342307, 0.33250603, -0.14626640])), 

129 np.array([6.34339996, -0.93449999, 18.56750001]), 

130 atol=TOLERANCE_ABSOLUTE_TESTS, 

131 ) 

132 

133 def test_n_dimensional_spherical_to_cartesian(self) -> None: 

134 """ 

135 Test :func:`colour.algebra.coordinates.transformations.\ 

136spherical_to_cartesian` definition n-dimensional arrays support. 

137 """ 

138 

139 a_i = np.array([6.78232998, 0.48504979, 0.32175055]) 

140 a_o = spherical_to_cartesian(a_i) 

141 

142 a_i = np.tile(a_i, (6, 1)) 

143 a_o = np.tile(a_o, (6, 1)) 

144 np.testing.assert_allclose( 

145 spherical_to_cartesian(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

146 ) 

147 

148 a_i = np.reshape(a_i, (2, 3, 3)) 

149 a_o = np.reshape(a_o, (2, 3, 3)) 

150 np.testing.assert_allclose( 

151 spherical_to_cartesian(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

152 ) 

153 

154 @ignore_numpy_errors 

155 def test_nan_spherical_to_cartesian(self) -> None: 

156 """ 

157 Test :func:`colour.algebra.coordinates.transformations.\ 

158spherical_to_cartesian` definition nan support. 

159 """ 

160 

161 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

162 cases = np.array(list(set(product(cases, repeat=3)))) 

163 spherical_to_cartesian(cases) 

164 

165 

166class TestCartesianToPolar: 

167 """ 

168 Define :func:`colour.algebra.coordinates.transformations.\ 

169cartesian_to_polar` definition unit tests methods. 

170 """ 

171 

172 def test_cartesian_to_polar(self) -> None: 

173 """ 

174 Test :func:`colour.algebra.coordinates.transformations.\ 

175cartesian_to_polar` definition. 

176 """ 

177 

178 np.testing.assert_allclose( 

179 cartesian_to_polar(np.array([3, 1])), 

180 np.array([3.16227766, 0.32175055]), 

181 atol=TOLERANCE_ABSOLUTE_TESTS, 

182 ) 

183 

184 np.testing.assert_allclose( 

185 cartesian_to_polar(np.array([-1, 9])), 

186 np.array([9.05538514, 1.68145355]), 

187 atol=TOLERANCE_ABSOLUTE_TESTS, 

188 ) 

189 

190 np.testing.assert_allclose( 

191 cartesian_to_polar(np.array([6.3434, -0.9345])), 

192 np.array([6.41186508, -0.14626640]), 

193 atol=TOLERANCE_ABSOLUTE_TESTS, 

194 ) 

195 

196 def test_n_dimensional_cartesian_to_polar(self) -> None: 

197 """ 

198 Test :func:`colour.algebra.coordinates.transformations.\ 

199cartesian_to_polar` definition n-dimensional arrays support. 

200 """ 

201 

202 a_i = np.array([3, 1]) 

203 a_o = cartesian_to_polar(a_i) 

204 

205 a_i = np.tile(a_i, (6, 1)) 

206 a_o = np.tile(a_o, (6, 1)) 

207 np.testing.assert_allclose( 

208 cartesian_to_polar(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

209 ) 

210 

211 a_i = np.reshape(a_i, (2, 3, 2)) 

212 a_o = np.reshape(a_o, (2, 3, 2)) 

213 np.testing.assert_allclose( 

214 cartesian_to_polar(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

215 ) 

216 

217 @ignore_numpy_errors 

218 def test_nan_cartesian_to_polar(self) -> None: 

219 """ 

220 Test :func:`colour.algebra.coordinates.transformations.\ 

221cartesian_to_polar` definition nan support. 

222 """ 

223 

224 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

225 cases = np.array(list(set(product(cases, repeat=2)))) 

226 cartesian_to_polar(cases) 

227 

228 

229class TestPolarToCartesian: 

230 """ 

231 Define :func:`colour.algebra.coordinates.transformations.\ 

232polar_to_cartesian` definition unit tests methods. 

233 """ 

234 

235 def test_polar_to_cartesian(self) -> None: 

236 """ 

237 Test :func:`colour.algebra.coordinates.transformations.\ 

238polar_to_cartesian` definition. 

239 """ 

240 

241 np.testing.assert_allclose( 

242 polar_to_cartesian(np.array([0.32175055, 1.08574654])), 

243 np.array([0.15001697, 0.28463718]), 

244 atol=TOLERANCE_ABSOLUTE_TESTS, 

245 ) 

246 

247 np.testing.assert_allclose( 

248 polar_to_cartesian(np.array([1.68145355, 1.05578119])), 

249 np.array([0.82819662, 1.46334425]), 

250 atol=TOLERANCE_ABSOLUTE_TESTS, 

251 ) 

252 

253 np.testing.assert_allclose( 

254 polar_to_cartesian(np.array([-0.14626640, 1.23829030])), 

255 np.array([-0.04774323, -0.13825500]), 

256 atol=TOLERANCE_ABSOLUTE_TESTS, 

257 ) 

258 

259 def test_n_dimensional_polar_to_cartesian(self) -> None: 

260 """ 

261 Test :func:`colour.algebra.coordinates.transformations.\ 

262polar_to_cartesian` definition n-dimensional arrays support. 

263 """ 

264 

265 a_i = np.array([3.16227766, 0.32175055]) 

266 a_o = polar_to_cartesian(a_i) 

267 

268 a_i = np.tile(a_i, (6, 1)) 

269 a_o = np.tile(a_o, (6, 1)) 

270 np.testing.assert_allclose( 

271 polar_to_cartesian(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

272 ) 

273 

274 a_i = np.reshape(a_i, (2, 3, 2)) 

275 a_o = np.reshape(a_o, (2, 3, 2)) 

276 np.testing.assert_allclose( 

277 polar_to_cartesian(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

278 ) 

279 

280 @ignore_numpy_errors 

281 def test_nan_polar_to_cartesian(self) -> None: 

282 """ 

283 Test :func:`colour.algebra.coordinates.transformations.\ 

284polar_to_cartesian` definition nan support. 

285 """ 

286 

287 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

288 cases = np.array(list(set(product(cases, repeat=2)))) 

289 polar_to_cartesian(cases) 

290 

291 

292class TestCartesianToCylindrical: 

293 """ 

294 Define :func:`colour.algebra.coordinates.transformations.\ 

295cartesian_to_cylindrical` definition unit tests methods. 

296 """ 

297 

298 def test_cartesian_to_cylindrical(self) -> None: 

299 """ 

300 Test :func:`colour.algebra.coordinates.transformations.\ 

301cartesian_to_cylindrical` definition. 

302 """ 

303 

304 np.testing.assert_allclose( 

305 cartesian_to_cylindrical(np.array([3, 1, 6])), 

306 np.array([3.16227766, 0.32175055, 6.00000000]), 

307 atol=TOLERANCE_ABSOLUTE_TESTS, 

308 ) 

309 

310 np.testing.assert_allclose( 

311 cartesian_to_cylindrical(np.array([-1, 9, 16])), 

312 np.array([9.05538514, 1.68145355, 16.00000000]), 

313 atol=TOLERANCE_ABSOLUTE_TESTS, 

314 ) 

315 

316 np.testing.assert_allclose( 

317 cartesian_to_cylindrical(np.array([6.3434, -0.9345, 18.5675])), 

318 np.array([6.41186508, -0.14626640, 18.56750000]), 

319 atol=TOLERANCE_ABSOLUTE_TESTS, 

320 ) 

321 

322 def test_n_dimensional_cartesian_to_cylindrical(self) -> None: 

323 """ 

324 Test :func:`colour.algebra.coordinates.transformations.\ 

325cartesian_to_cylindrical` definition n-dimensional arrays support. 

326 """ 

327 

328 a_i = np.array([3, 1, 6]) 

329 a_o = cartesian_to_cylindrical(a_i) 

330 

331 a_i = np.tile(a_i, (6, 1)) 

332 a_o = np.tile(a_o, (6, 1)) 

333 np.testing.assert_allclose( 

334 cartesian_to_cylindrical(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

335 ) 

336 

337 a_i = np.reshape(a_i, (2, 3, 3)) 

338 a_o = np.reshape(a_o, (2, 3, 3)) 

339 np.testing.assert_allclose( 

340 cartesian_to_cylindrical(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

341 ) 

342 

343 @ignore_numpy_errors 

344 def test_nan_cartesian_to_cylindrical(self) -> None: 

345 """ 

346 Test :func:`colour.algebra.coordinates.transformations.\ 

347cartesian_to_cylindrical` definition nan support. 

348 """ 

349 

350 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

351 cases = np.array(list(set(product(cases, repeat=3)))) 

352 cartesian_to_cylindrical(cases) 

353 

354 

355class TestCylindricalToCartesian: 

356 """ 

357 Define :func:`colour.algebra.coordinates.transformations.\ 

358cylindrical_to_cartesian` definition unit tests methods. 

359 """ 

360 

361 def test_cylindrical_to_cartesian(self) -> None: 

362 """ 

363 Test :func:`colour.algebra.coordinates.transformations.\ 

364cylindrical_to_cartesian` definition. 

365 """ 

366 

367 np.testing.assert_allclose( 

368 cylindrical_to_cartesian(np.array([0.32175055, 1.08574654, 6.78232998])), 

369 np.array([0.15001697, 0.28463718, 6.78232998]), 

370 atol=TOLERANCE_ABSOLUTE_TESTS, 

371 ) 

372 

373 np.testing.assert_allclose( 

374 cylindrical_to_cartesian(np.array([1.68145355, 1.05578119, 18.38477631])), 

375 np.array([0.82819662, 1.46334425, 18.38477631]), 

376 atol=TOLERANCE_ABSOLUTE_TESTS, 

377 ) 

378 

379 np.testing.assert_allclose( 

380 cylindrical_to_cartesian(np.array([-0.14626640, 1.23829030, 19.64342307])), 

381 np.array([-0.04774323, -0.13825500, 19.64342307]), 

382 atol=TOLERANCE_ABSOLUTE_TESTS, 

383 ) 

384 

385 def test_n_dimensional_cylindrical_to_cartesian(self) -> None: 

386 """ 

387 Test :func:`colour.algebra.coordinates.transformations.\ 

388cylindrical_to_cartesian` definition n-dimensional arrays support. 

389 """ 

390 

391 a_i = np.array([3.16227766, 0.32175055, 6.00000000]) 

392 a_o = cylindrical_to_cartesian(a_i) 

393 

394 a_i = np.tile(a_i, (6, 1)) 

395 a_o = np.tile(a_o, (6, 1)) 

396 np.testing.assert_allclose( 

397 cylindrical_to_cartesian(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

398 ) 

399 

400 a_i = np.reshape(a_i, (2, 3, 3)) 

401 a_o = np.reshape(a_o, (2, 3, 3)) 

402 np.testing.assert_allclose( 

403 cylindrical_to_cartesian(a_i), a_o, atol=TOLERANCE_ABSOLUTE_TESTS 

404 ) 

405 

406 @ignore_numpy_errors 

407 def test_nan_cylindrical_to_cartesian(self) -> None: 

408 """ 

409 Test :func:`colour.algebra.coordinates.transformations.\ 

410cylindrical_to_cartesian` definition nan support. 

411 """ 

412 

413 cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan] 

414 cases = np.array(list(set(product(cases, repeat=3)))) 

415 cylindrical_to_cartesian(cases)