Coverage for contrast/tests/test_barten1999.py: 100%

159 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.contrast.barten1999` module.""" 

2 

3from __future__ import annotations 

4 

5from itertools import product 

6 

7import numpy as np 

8 

9from colour.constants import TOLERANCE_ABSOLUTE_TESTS 

10from colour.contrast import ( 

11 contrast_sensitivity_function_Barten1999, 

12 maximum_angular_size_Barten1999, 

13 optical_MTF_Barten1999, 

14 pupil_diameter_Barten1999, 

15 retinal_illuminance_Barten1999, 

16 sigma_Barten1999, 

17) 

18from colour.utilities import ignore_numpy_errors 

19 

20__author__ = "Colour Developers" 

21__copyright__ = "Copyright 2013 Colour Developers" 

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

23__maintainer__ = "Colour Developers" 

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

25__status__ = "Production" 

26 

27__all__ = [ 

28 "TestOpticalMTFBarten1999", 

29 "TestPupilDiameterBarten1999", 

30 "TestSigmaBarten1999", 

31 "TestRetinalIlluminanceBarten1999", 

32 "TestMaximumAngularSizeBarten1999", 

33 "TestContrastSensitivityFunctionBarten1999", 

34] 

35 

36 

37class TestOpticalMTFBarten1999: 

38 """ 

39 Define :func:`colour.contrast.barten1999.optical_MTF_Barten1999` 

40 definition unit tests methods. 

41 """ 

42 

43 def test_optical_MTF_Barten1999(self) -> None: 

44 """ 

45 Test :func:`colour.contrast.barten1999.optical_MTF_Barten1999` 

46 definition. 

47 """ 

48 

49 np.testing.assert_allclose( 

50 optical_MTF_Barten1999(4, 0.01), 

51 0.968910791191297, 

52 atol=TOLERANCE_ABSOLUTE_TESTS, 

53 ) 

54 

55 np.testing.assert_allclose( 

56 optical_MTF_Barten1999(8, 0.01), 

57 0.881323136669471, 

58 atol=TOLERANCE_ABSOLUTE_TESTS, 

59 ) 

60 

61 np.testing.assert_allclose( 

62 optical_MTF_Barten1999(4, 0.05), 

63 0.454040738727245, 

64 atol=TOLERANCE_ABSOLUTE_TESTS, 

65 ) 

66 

67 def test_n_dimensional_optical_MTF_Barten1999(self) -> None: 

68 """ 

69 Test :func:`colour.contrast.barten1999.optical_MTF_Barten1999` 

70 definition n-dimensional support. 

71 """ 

72 

73 u = np.array([4, 8, 12]) 

74 sigma = np.array([0.01, 0.05, 0.1]) 

75 M_opt = optical_MTF_Barten1999(u, sigma) 

76 

77 u = np.tile(u, (6, 1)) 

78 sigma = np.tile(sigma, (6, 1)) 

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

80 np.testing.assert_allclose( 

81 optical_MTF_Barten1999(u, sigma), 

82 M_opt, 

83 atol=TOLERANCE_ABSOLUTE_TESTS, 

84 ) 

85 

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

87 sigma = np.reshape(sigma, (2, 3, 3)) 

88 M_opt = np.reshape(M_opt, (2, 3, 3)) 

89 np.testing.assert_allclose( 

90 optical_MTF_Barten1999(u, sigma), 

91 M_opt, 

92 atol=TOLERANCE_ABSOLUTE_TESTS, 

93 ) 

94 

95 @ignore_numpy_errors 

96 def test_nan_optical_MTF_Barten1999(self) -> None: 

97 """ 

98 Test :func:`colour.contrast.barten1999.optical_MTF_Barten1999` 

99 definition nan support. 

100 """ 

101 

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

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

104 optical_MTF_Barten1999(cases, cases) 

105 

106 

107class TestPupilDiameterBarten1999: 

108 """ 

109 Define :func:`colour.contrast.barten1999.pupil_diameter_Barten1999` 

110 definition unit tests methods. 

111 """ 

112 

113 def test_pupil_diameter_Barten1999(self) -> None: 

114 """ 

115 Test :func:`colour.contrast.barten1999.pupil_diameter_Barten1999` 

116 definition. 

117 """ 

118 

119 np.testing.assert_allclose( 

120 pupil_diameter_Barten1999(20, 60), 

121 3.262346170373243, 

122 atol=TOLERANCE_ABSOLUTE_TESTS, 

123 ) 

124 

125 np.testing.assert_allclose( 

126 pupil_diameter_Barten1999(0.2, 600), 

127 3.262346170373243, 

128 atol=TOLERANCE_ABSOLUTE_TESTS, 

129 ) 

130 

131 np.testing.assert_allclose( 

132 pupil_diameter_Barten1999(20, 60, 30), 

133 3.519054451149336, 

134 atol=TOLERANCE_ABSOLUTE_TESTS, 

135 ) 

136 

137 def test_n_dimensional_pupil_diameter_Barten1999(self) -> None: 

138 """ 

139 Test :func:`colour.contrast.barten1999.pupil_diameter_Barten1999` 

140 definition n-dimensional support. 

141 """ 

142 

143 L = np.array([0.2, 20, 100]) 

144 X_0 = np.array([60, 120, 240]) 

145 Y_0 = np.array([60, 30, 15]) 

146 d = pupil_diameter_Barten1999(L, X_0, Y_0) 

147 

148 L = np.tile(L, (6, 1)) 

149 X_0 = np.tile(X_0, (6, 1)) 

150 d = np.tile(d, (6, 1)) 

151 np.testing.assert_allclose( 

152 pupil_diameter_Barten1999(L, X_0, Y_0), 

153 d, 

154 atol=TOLERANCE_ABSOLUTE_TESTS, 

155 ) 

156 

157 L = np.reshape(L, (2, 3, 3)) 

158 X_0 = np.reshape(X_0, (2, 3, 3)) 

159 d = np.reshape(d, (2, 3, 3)) 

160 np.testing.assert_allclose( 

161 pupil_diameter_Barten1999(L, X_0, Y_0), 

162 d, 

163 atol=TOLERANCE_ABSOLUTE_TESTS, 

164 ) 

165 

166 @ignore_numpy_errors 

167 def test_nan_pupil_diameter_Barten1999(self) -> None: 

168 """ 

169 Test :func:`colour.contrast.barten1999.pupil_diameter_Barten1999` 

170 definition nan support. 

171 """ 

172 

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

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

175 pupil_diameter_Barten1999(cases, cases, cases) 

176 

177 

178class TestSigmaBarten1999: 

179 """ 

180 Define :func:`colour.contrast.barten1999.sigma_Barten1999` definition unit 

181 tests methods. 

182 """ 

183 

184 def test_sigma_Barten1999(self) -> None: 

185 """Test :func:`colour.contrast.barten1999.sigma_Barten1999` definition.""" 

186 

187 np.testing.assert_allclose( 

188 sigma_Barten1999(0.5 / 60, 0.08 / 60, 2.1), 

189 0.008791157173231, 

190 atol=TOLERANCE_ABSOLUTE_TESTS, 

191 ) 

192 

193 np.testing.assert_allclose( 

194 sigma_Barten1999(0.75 / 60, 0.08 / 60, 2.1), 

195 0.012809761902549, 

196 atol=TOLERANCE_ABSOLUTE_TESTS, 

197 ) 

198 

199 np.testing.assert_allclose( 

200 sigma_Barten1999(0.5 / 60, 0.16 / 60, 2.1), 

201 0.010040141654601, 

202 atol=TOLERANCE_ABSOLUTE_TESTS, 

203 ) 

204 

205 np.testing.assert_allclose( 

206 sigma_Barten1999(0.5 / 60, 0.08 / 60, 2.5), 

207 0.008975274678558, 

208 atol=TOLERANCE_ABSOLUTE_TESTS, 

209 ) 

210 

211 def test_n_dimensional_sigma_Barten1999(self) -> None: 

212 """ 

213 Test :func:`colour.contrast.barten1999.sigma_Barten1999` definition 

214 n-dimensional support. 

215 """ 

216 

217 sigma_0 = np.array([0.25 / 60, 0.5 / 60, 0.75 / 60]) 

218 C_ab = np.array([0.04 / 60, 0.08 / 60, 0.16 / 60]) 

219 d = np.array([2.1, 2.5, 5.0]) 

220 sigma = sigma_Barten1999(sigma_0, C_ab, d) 

221 

222 sigma_0 = np.tile(sigma_0, (6, 1)) 

223 C_ab = np.tile(C_ab, (6, 1)) 

224 sigma = np.tile(sigma, (6, 1)) 

225 np.testing.assert_allclose( 

226 sigma_Barten1999(sigma_0, C_ab, d), 

227 sigma, 

228 atol=TOLERANCE_ABSOLUTE_TESTS, 

229 ) 

230 

231 sigma_0 = np.reshape(sigma_0, (2, 3, 3)) 

232 C_ab = np.reshape(C_ab, (2, 3, 3)) 

233 sigma = np.reshape(sigma, (2, 3, 3)) 

234 np.testing.assert_allclose( 

235 sigma_Barten1999(sigma_0, C_ab, d), 

236 sigma, 

237 atol=TOLERANCE_ABSOLUTE_TESTS, 

238 ) 

239 

240 @ignore_numpy_errors 

241 def test_nan_sigma_Barten1999(self) -> None: 

242 """ 

243 Test :func:`colour.contrast.barten1999.sigma_Barten1999` 

244 definition nan support. 

245 """ 

246 

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

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

249 sigma_Barten1999(cases, cases, cases) 

250 

251 

252class TestRetinalIlluminanceBarten1999: 

253 """ 

254 Define :func:`colour.contrast.barten1999.retinal_illuminance_Barten1999` 

255 definition unit tests methods. 

256 """ 

257 

258 def test_retinal_illuminance_Barten1999(self) -> None: 

259 """ 

260 Test :func:`colour.contrast.barten1999.retinal_illuminance_Barten1999` 

261 definition. 

262 """ 

263 

264 np.testing.assert_allclose( 

265 retinal_illuminance_Barten1999(20, 2.1, True), 

266 66.082316060529919, 

267 atol=TOLERANCE_ABSOLUTE_TESTS, 

268 ) 

269 

270 np.testing.assert_allclose( 

271 retinal_illuminance_Barten1999(20, 2.5, True), 

272 91.815644777503664, 

273 atol=TOLERANCE_ABSOLUTE_TESTS, 

274 ) 

275 

276 np.testing.assert_allclose( 

277 retinal_illuminance_Barten1999(20, 2.1, False), 

278 69.272118011654939, 

279 atol=TOLERANCE_ABSOLUTE_TESTS, 

280 ) 

281 

282 def test_n_dimensional_retinal_illuminance_Barten1999(self) -> None: 

283 """ 

284 Test :func:`colour.contrast.barten1999.retinal_illuminance_Barten1999` 

285 definition n-dimensional support. 

286 """ 

287 

288 L = np.array([0.2, 20, 100]) 

289 d = np.array([2.1, 2.5, 5.0]) 

290 E = retinal_illuminance_Barten1999(L, d) 

291 

292 L = np.tile(L, (6, 1)) 

293 d = np.tile(d, (6, 1)) 

294 E = np.tile(E, (6, 1)) 

295 np.testing.assert_allclose( 

296 retinal_illuminance_Barten1999(L, d), 

297 E, 

298 atol=TOLERANCE_ABSOLUTE_TESTS, 

299 ) 

300 

301 L = np.reshape(L, (2, 3, 3)) 

302 d = np.reshape(d, (2, 3, 3)) 

303 E = np.reshape(E, (2, 3, 3)) 

304 np.testing.assert_allclose( 

305 retinal_illuminance_Barten1999(L, d), 

306 E, 

307 atol=TOLERANCE_ABSOLUTE_TESTS, 

308 ) 

309 

310 @ignore_numpy_errors 

311 def test_nan_retinal_illuminance_Barten1999(self) -> None: 

312 """ 

313 Test :func:`colour.contrast.barten1999.retinal_illuminance_Barten1999` 

314 definition nan support. 

315 """ 

316 

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

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

319 retinal_illuminance_Barten1999(cases, cases) 

320 

321 

322class TestMaximumAngularSizeBarten1999: 

323 """ 

324 Define :func:`colour.contrast.barten1999.maximum_angular_size_Barten1999` 

325 definition unit tests methods. 

326 """ 

327 

328 def test_maximum_angular_size_Barten1999(self) -> None: 

329 """ 

330 Test :func:`colour.contrast.barten1999.\ 

331maximum_angular_size_Barten1999` definition. 

332 """ 

333 

334 np.testing.assert_allclose( 

335 maximum_angular_size_Barten1999(4, 60, 12, 15), 

336 3.572948005052482, 

337 atol=TOLERANCE_ABSOLUTE_TESTS, 

338 ) 

339 

340 np.testing.assert_allclose( 

341 maximum_angular_size_Barten1999(8, 60, 12, 15), 

342 1.851640199545103, 

343 atol=TOLERANCE_ABSOLUTE_TESTS, 

344 ) 

345 

346 np.testing.assert_allclose( 

347 maximum_angular_size_Barten1999(4, 120, 12, 15), 

348 3.577708763999663, 

349 atol=TOLERANCE_ABSOLUTE_TESTS, 

350 ) 

351 

352 np.testing.assert_allclose( 

353 maximum_angular_size_Barten1999(4, 60, 24, 15), 

354 3.698001308168194, 

355 atol=TOLERANCE_ABSOLUTE_TESTS, 

356 ) 

357 

358 np.testing.assert_allclose( 

359 maximum_angular_size_Barten1999(4, 60, 12, 30), 

360 6.324555320336758, 

361 atol=TOLERANCE_ABSOLUTE_TESTS, 

362 ) 

363 

364 def test_n_dimensional_maximum_angular_size_Barten1999(self) -> None: 

365 """ 

366 Test :func:`colour.contrast.barten1999.\ 

367maximum_angular_size_Barten1999` definition n-dimensional support. 

368 """ 

369 

370 u = np.array([4, 8, 12]) 

371 X_0 = np.array([60, 120, 240]) 

372 X_max = np.array([12, 14, 16]) 

373 N_max = np.array([15, 20, 25]) 

374 X = maximum_angular_size_Barten1999(u, X_0, X_max, N_max) 

375 

376 u = np.tile(u, (6, 1)) 

377 X_0 = np.tile(X_0, (6, 1)) 

378 X = np.tile(X, (6, 1)) 

379 np.testing.assert_allclose( 

380 maximum_angular_size_Barten1999(u, X_0, X_max, N_max), 

381 X, 

382 atol=TOLERANCE_ABSOLUTE_TESTS, 

383 ) 

384 

385 u = np.reshape(u, (2, 3, 3)) 

386 X_0 = np.reshape(X_0, (2, 3, 3)) 

387 X = np.reshape(X, (2, 3, 3)) 

388 np.testing.assert_allclose( 

389 maximum_angular_size_Barten1999(u, X_0, X_max, N_max), 

390 X, 

391 atol=TOLERANCE_ABSOLUTE_TESTS, 

392 ) 

393 

394 @ignore_numpy_errors 

395 def test_nan_maximum_angular_size_Barten1999(self) -> None: 

396 """ 

397 Test :func:`colour.contrast.barten1999.\ 

398maximum_angular_size_Barten1999` definition nan support. 

399 """ 

400 

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

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

403 maximum_angular_size_Barten1999(cases, cases, cases, cases) 

404 

405 

406class TestContrastSensitivityFunctionBarten1999: 

407 """ 

408 Define :func:`colour.contrast.barten1999.\ 

409contrast_sensitivity_function_Barten1999` definition unit tests methods. 

410 """ 

411 

412 def test_contrast_sensitivity_function_Barten1999(self) -> None: 

413 """ 

414 Test :func:`colour.contrast.barten1999.\ 

415contrast_sensitivity_function_Barten1999` definition. 

416 """ 

417 

418 np.testing.assert_allclose( 

419 contrast_sensitivity_function_Barten1999( 

420 u=4, 

421 sigma=0.01, 

422 E=65, 

423 X_0=60, 

424 X_max=12, 

425 Y_0=60, 

426 Y_max=12, 

427 p=1.2 * 10**6, 

428 ), 

429 352.761342126727020, 

430 atol=TOLERANCE_ABSOLUTE_TESTS, 

431 ) 

432 

433 np.testing.assert_allclose( 

434 contrast_sensitivity_function_Barten1999( 

435 u=8, 

436 sigma=0.01, 

437 E=65, 

438 X_0=60, 

439 X_max=12, 

440 Y_0=60, 

441 Y_max=12, 

442 p=1.2 * 10**6, 

443 ), 

444 177.706338840717340, 

445 atol=TOLERANCE_ABSOLUTE_TESTS, 

446 ) 

447 

448 np.testing.assert_allclose( 

449 contrast_sensitivity_function_Barten1999( 

450 u=4, 

451 sigma=0.02, 

452 E=65, 

453 X_0=60, 

454 X_max=12, 

455 Y_0=60, 

456 Y_max=12, 

457 p=1.2 * 10**6, 

458 ), 

459 320.872401634215750, 

460 atol=TOLERANCE_ABSOLUTE_TESTS, 

461 ) 

462 

463 np.testing.assert_allclose( 

464 contrast_sensitivity_function_Barten1999( 

465 u=4, 

466 sigma=0.01, 

467 E=130, 

468 X_0=60, 

469 X_max=12, 

470 Y_0=60, 

471 Y_max=12, 

472 p=1.2 * 10**6, 

473 ), 

474 455.171315756946400, 

475 atol=TOLERANCE_ABSOLUTE_TESTS, 

476 ) 

477 

478 np.testing.assert_allclose( 

479 contrast_sensitivity_function_Barten1999( 

480 u=4, 

481 sigma=0.01, 

482 E=65, 

483 X_0=120, 

484 X_max=12, 

485 Y_0=60, 

486 Y_max=12, 

487 p=1.2 * 10**6, 

488 ), 

489 352.996281545740660, 

490 atol=TOLERANCE_ABSOLUTE_TESTS, 

491 ) 

492 

493 np.testing.assert_allclose( 

494 contrast_sensitivity_function_Barten1999( 

495 u=4, 

496 sigma=0.01, 

497 E=65, 

498 X_0=60, 

499 X_max=24, 

500 Y_0=60, 

501 Y_max=12, 

502 p=1.2 * 10**6, 

503 ), 

504 358.881580104493650, 

505 atol=TOLERANCE_ABSOLUTE_TESTS, 

506 ) 

507 

508 np.testing.assert_allclose( 

509 contrast_sensitivity_function_Barten1999( 

510 u=4, 

511 sigma=0.01, 

512 E=65, 

513 X_0=240, 

514 X_max=12, 

515 Y_0=60, 

516 Y_max=12, 

517 p=1.2 * 10**6, 

518 ), 

519 contrast_sensitivity_function_Barten1999( 

520 u=4, 

521 sigma=0.01, 

522 E=65, 

523 X_0=60, 

524 X_max=12, 

525 Y_0=240, 

526 Y_max=12, 

527 p=1.2 * 10**6, 

528 ), 

529 atol=TOLERANCE_ABSOLUTE_TESTS, 

530 ) 

531 

532 np.testing.assert_allclose( 

533 contrast_sensitivity_function_Barten1999( 

534 u=4, 

535 sigma=0.01, 

536 E=65, 

537 X_0=60, 

538 X_max=12, 

539 Y_0=60, 

540 Y_max=12, 

541 p=1.4 * 10**6, 

542 ), 

543 374.791328640476140, 

544 atol=TOLERANCE_ABSOLUTE_TESTS, 

545 ) 

546 

547 def test_n_dimensional_contrast_sensitivity_function_Barten1999(self) -> None: 

548 """ 

549 Test :func:`colour.contrast.barten1999.\ 

550contrast_sensitivity_function_Barten1999` definition n-dimensional support. 

551 """ 

552 

553 u = np.array([4, 8, 12]) 

554 sigma = np.array([0.01, 0.02, 0.04]) 

555 E = np.array([0.65, 90, 1500]) 

556 X_0 = np.array([60, 120, 240]) 

557 S = contrast_sensitivity_function_Barten1999(u=u, sigma=sigma, E=E, X_0=X_0) 

558 

559 u = np.tile(u, (6, 1)) 

560 E = np.tile(E, (6, 1)) 

561 S = np.tile(S, (6, 1)) 

562 np.testing.assert_allclose( 

563 contrast_sensitivity_function_Barten1999(u=u, sigma=sigma, E=E, X_0=X_0), 

564 S, 

565 atol=TOLERANCE_ABSOLUTE_TESTS, 

566 ) 

567 

568 u = np.reshape(u, (2, 3, 3)) 

569 E = np.reshape(E, (2, 3, 3)) 

570 S = np.reshape(S, (2, 3, 3)) 

571 np.testing.assert_allclose( 

572 contrast_sensitivity_function_Barten1999(u=u, sigma=sigma, E=E, X_0=X_0), 

573 S, 

574 atol=TOLERANCE_ABSOLUTE_TESTS, 

575 ) 

576 

577 @ignore_numpy_errors 

578 def test_nan_contrast_sensitivity_function_Barten1999(self) -> None: 

579 """ 

580 Test :func:`colour.contrast.barten1999.\ 

581contrast_sensitivity_function_Barten1999` definition nan support. 

582 """ 

583 

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

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

586 contrast_sensitivity_function_Barten1999( 

587 u=cases, sigma=cases, E=cases, X_0=cases 

588 )