Coverage for colour/quality/tests/test_ssi.py: 100%
29 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
« 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.quality.ssi` module."""
3from __future__ import annotations
5import numpy as np
7from colour.colorimetry import (
8 SDS_ILLUMINANTS,
9 SpectralDistribution,
10 sd_single_led,
11 sds_and_msds_to_msds,
12)
13from colour.constants import TOLERANCE_ABSOLUTE_TESTS
14from colour.quality import spectral_similarity_index
15from colour.utilities import is_scipy_installed
17__author__ = "Colour Developers"
18__copyright__ = "Copyright 2013 Colour Developers"
19__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
20__maintainer__ = "Colour Developers"
21__email__ = "colour-developers@colour-science.org"
22__status__ = "Production"
24__all__ = [
25 "TestSpectralSimilarityIndex",
26]
28DATA_HMI: dict = {
29 300: 0.000000000000000,
30 301: 0.000000000000000,
31 302: 0.000000000000000,
32 303: 0.000000000000000,
33 304: 0.000000000000000,
34 305: 0.000000000000000,
35 306: 0.000000000000000,
36 307: 0.000000000000000,
37 308: 0.000000000000000,
38 309: 0.000000000000000,
39 310: 0.000000000000000,
40 311: 0.000000000000000,
41 312: 0.000000000000000,
42 313: 0.000000000000000,
43 314: 0.000000000000000,
44 315: 0.000000000000000,
45 316: 0.000000000000000,
46 317: 0.000000000000000,
47 318: 0.000000000000000,
48 319: 0.000000000000000,
49 320: 0.000000000000000,
50 321: 0.000000000000000,
51 322: 0.000000000000000,
52 323: 0.000000000000000,
53 324: 0.000000000000000,
54 325: 0.000000000000000,
55 326: 0.000000000000000,
56 327: 0.000000000000000,
57 328: 0.000000000000000,
58 329: 0.000000000000000,
59 330: 0.000000000000000,
60 331: 0.000000000000000,
61 332: 0.000000000000000,
62 333: 0.000000000000000,
63 334: 0.000000000000000,
64 335: 0.000000000000000,
65 336: 0.000000000000000,
66 337: 0.000000000000000,
67 338: 0.000000000000000,
68 339: 0.000000000000000,
69 340: 0.000000000000000,
70 341: 0.000000000000000,
71 342: 0.000000000000000,
72 343: 0.000000000000000,
73 344: 0.000000000000000,
74 345: 0.000000000000000,
75 346: 0.000000000000000,
76 347: 0.000000000000000,
77 348: 0.000000000000000,
78 349: 0.000000000000000,
79 350: 0.000000000000000,
80 351: 0.000000000000000,
81 352: 0.000000000000000,
82 353: 0.000000000000000,
83 354: 0.000000000000000,
84 355: 0.000000000000000,
85 356: 0.000000000000000,
86 357: 0.000000000000000,
87 358: 0.000000000000000,
88 359: 0.000000000000000,
89 360: 0.000000000000000,
90 361: 0.000000000000000,
91 362: 0.000000000000000,
92 363: 0.000000000000000,
93 364: 0.000000000000000,
94 365: 0.000000000000000,
95 366: 0.000000000000000,
96 367: 0.000000000000000,
97 368: 0.000000000000000,
98 369: 0.000000000000000,
99 370: 0.000000000000000,
100 371: 0.000000000000000,
101 372: 0.000000000000000,
102 373: 0.000000000000000,
103 374: 0.000000000000000,
104 375: 0.000000000000000,
105 376: 0.000000000000000,
106 377: 0.000000000000000,
107 378: 0.000000000000000,
108 379: 0.000000000000000,
109 380: 1.204633204633200,
110 381: 1.101029601029600,
111 382: 0.997425997425997,
112 383: 1.008365508365510,
113 384: 1.019305019305020,
114 385: 1.070141570141570,
115 386: 1.120978120978120,
116 387: 1.177606177606180,
117 388: 1.234234234234230,
118 389: 1.162162162162160,
119 390: 1.090090090090090,
120 391: 1.153796653796660,
121 392: 1.217503217503220,
122 393: 1.166023166023170,
123 394: 1.114543114543110,
124 395: 1.180823680823680,
125 396: 1.247104247104250,
126 397: 1.190476190476190,
127 398: 1.133848133848130,
128 399: 1.137065637065640,
129 400: 1.140283140283140,
130 401: 1.220077220077220,
131 402: 1.299871299871300,
132 403: 1.325611325611320,
133 404: 1.351351351351350,
134 405: 1.415701415701410,
135 406: 1.480051480051480,
136 407: 1.486486486486490,
137 408: 1.492921492921490,
138 409: 1.544401544401540,
139 410: 1.595881595881600,
140 411: 1.583011583011580,
141 412: 1.570141570141570,
142 413: 1.608751608751610,
143 414: 1.647361647361650,
144 415: 1.679536679536680,
145 416: 1.711711711711710,
146 417: 1.718146718146710,
147 418: 1.724581724581720,
148 419: 1.743886743886740,
149 420: 1.763191763191760,
150 421: 1.776061776061770,
151 422: 1.788931788931790,
152 423: 1.711711711711710,
153 424: 1.634491634491630,
154 425: 1.550836550836550,
155 426: 1.467181467181470,
156 427: 1.409266409266410,
157 428: 1.351351351351350,
158 429: 1.318532818532820,
159 430: 1.285714285714290,
160 431: 1.299227799227800,
161 432: 1.312741312741310,
162 433: 1.396396396396400,
163 434: 1.480051480051480,
164 435: 1.518661518661520,
165 436: 1.557271557271560,
166 437: 1.486486486486490,
167 438: 1.415701415701420,
168 439: 1.277348777348780,
169 440: 1.138996138996140,
170 441: 1.046975546975550,
171 442: 0.954954954954955,
172 443: 0.939510939510940,
173 444: 0.924066924066924,
174 445: 0.935006435006435,
175 446: 0.945945945945946,
176 447: 0.962676962676962,
177 448: 0.979407979407979,
178 449: 0.984555984555984,
179 450: 0.989703989703990,
180 451: 1.014800514800520,
181 452: 1.039897039897040,
182 453: 1.069498069498070,
183 454: 1.099099099099100,
184 455: 1.133848133848140,
185 456: 1.168597168597170,
186 457: 1.195624195624200,
187 458: 1.222651222651220,
188 459: 1.212998712998710,
189 460: 1.203346203346200,
190 461: 1.182110682110680,
191 462: 1.160875160875160,
192 463: 1.135135135135140,
193 464: 1.109395109395110,
194 465: 1.102960102960110,
195 466: 1.096525096525100,
196 467: 1.075289575289580,
197 468: 1.054054054054050,
198 469: 1.059202059202060,
199 470: 1.064350064350060,
200 471: 1.064993564993570,
201 472: 1.065637065637070,
202 473: 1.057915057915060,
203 474: 1.050193050193050,
204 475: 1.049549549549550,
205 476: 1.048906048906050,
206 477: 1.042471042471050,
207 478: 1.036036036036040,
208 479: 1.048262548262550,
209 480: 1.060489060489060,
210 481: 1.052767052767050,
211 482: 1.045045045045050,
212 483: 1.023166023166020,
213 484: 1.001287001287000,
214 485: 1.001287001287000,
215 486: 1.001287001287000,
216 487: 1.003217503217510,
217 488: 1.005148005148010,
218 489: 1.013513513513510,
219 490: 1.021879021879020,
220 491: 1.014800514800510,
221 492: 1.007722007722010,
222 493: 1.005791505791510,
223 494: 1.003861003861000,
224 495: 0.994208494208493,
225 496: 0.984555984555985,
226 497: 0.986486486486487,
227 498: 0.988416988416988,
228 499: 1.009009009009010,
229 500: 1.029601029601030,
230 501: 1.054697554697550,
231 502: 1.079794079794080,
232 503: 1.099099099099100,
233 504: 1.118404118404120,
234 505: 1.105534105534110,
235 506: 1.092664092664090,
236 507: 1.068854568854570,
237 508: 1.045045045045050,
238 509: 1.030244530244540,
239 510: 1.015444015444020,
240 511: 1.014157014157020,
241 512: 1.012870012870010,
242 513: 1.010939510939510,
243 514: 1.009009009009010,
244 515: 0.992277992277993,
245 516: 0.975546975546976,
246 517: 0.956241956241957,
247 518: 0.936936936936937,
248 519: 0.924710424710425,
249 520: 0.912483912483913,
250 521: 0.894465894465895,
251 522: 0.876447876447876,
252 523: 0.884813384813385,
253 524: 0.893178893178893,
254 525: 0.907979407979408,
255 526: 0.922779922779923,
256 527: 0.946589446589447,
257 528: 0.970398970398970,
258 529: 0.983268983268983,
259 530: 0.996138996138996,
260 531: 0.992921492921493,
261 532: 0.989703989703990,
262 533: 0.969111969111970,
263 534: 0.948519948519949,
264 535: 0.933719433719434,
265 536: 0.918918918918919,
266 537: 0.929214929214929,
267 538: 0.939510939510939,
268 539: 0.959459459459459,
269 540: 0.979407979407979,
270 541: 1.083011583011580,
271 542: 1.186615186615190,
272 543: 1.416988416988420,
273 544: 1.647361647361650,
274 545: 1.724581724581720,
275 546: 1.801801801801800,
276 547: 1.756756756756760,
277 548: 1.711711711711710,
278 549: 1.505791505791510,
279 550: 1.299871299871300,
280 551: 1.150579150579150,
281 552: 1.001287001287000,
282 553: 0.985199485199485,
283 554: 0.969111969111969,
284 555: 0.965250965250966,
285 556: 0.961389961389962,
286 557: 0.963320463320464,
287 558: 0.965250965250965,
288 559: 0.982625482625483,
289 560: 1.000000000000000,
290 561: 1.047619047619050,
291 562: 1.095238095238100,
292 563: 1.131274131274140,
293 564: 1.167310167310170,
294 565: 1.187902187902190,
295 566: 1.208494208494210,
296 567: 1.196267696267700,
297 568: 1.184041184041180,
298 569: 1.171814671814670,
299 570: 1.159588159588160,
300 571: 1.176962676962680,
301 572: 1.194337194337190,
302 573: 1.305019305019310,
303 574: 1.415701415701420,
304 575: 1.518661518661520,
305 576: 1.621621621621620,
306 577: 1.647361647361640,
307 578: 1.673101673101670,
308 579: 1.589446589446590,
309 580: 1.505791505791510,
310 581: 1.394465894465890,
311 582: 1.283140283140280,
312 583: 1.212355212355210,
313 584: 1.141570141570140,
314 585: 1.167953667953670,
315 586: 1.194337194337190,
316 587: 1.222651222651220,
317 588: 1.250965250965250,
318 589: 1.251608751608750,
319 590: 1.252252252252250,
320 591: 1.232303732303730,
321 592: 1.212355212355210,
322 593: 1.205920205920200,
323 594: 1.199485199485200,
324 595: 1.231016731016730,
325 596: 1.262548262548260,
326 597: 1.281209781209780,
327 598: 1.299871299871300,
328 599: 1.274774774774770,
329 600: 1.249678249678250,
330 601: 1.198198198198200,
331 602: 1.146718146718150,
332 603: 1.105534105534110,
333 604: 1.064350064350060,
334 605: 1.064993564993570,
335 606: 1.065637065637070,
336 607: 1.079150579150580,
337 608: 1.092664092664090,
338 609: 1.092020592020590,
339 610: 1.091377091377090,
340 611: 1.085585585585580,
341 612: 1.079794079794080,
342 613: 1.072072072072070,
343 614: 1.064350064350060,
344 615: 1.059202059202060,
345 616: 1.054054054054050,
346 617: 1.051480051480050,
347 618: 1.048906048906050,
348 619: 1.028314028314030,
349 620: 1.007722007722010,
350 621: 1.007078507078510,
351 622: 1.006435006435010,
352 623: 1.012226512226520,
353 624: 1.018018018018020,
354 625: 1.006435006435010,
355 626: 0.994851994851995,
356 627: 0.965250965250966,
357 628: 0.935649935649936,
358 629: 0.903474903474903,
359 630: 0.871299871299871,
360 631: 0.855212355212355,
361 632: 0.839124839124839,
362 633: 0.837194337194337,
363 634: 0.835263835263835,
364 635: 0.839768339768340,
365 636: 0.844272844272844,
366 637: 0.848777348777348,
367 638: 0.853281853281853,
368 639: 0.862290862290862,
369 640: 0.871299871299871,
370 641: 0.868725868725869,
371 642: 0.866151866151866,
372 643: 0.869369369369369,
373 644: 0.872586872586873,
374 645: 0.863577863577864,
375 646: 0.854568854568855,
376 647: 0.837194337194338,
377 648: 0.819819819819820,
378 649: 0.802445302445302,
379 650: 0.785070785070785,
380 651: 0.776061776061776,
381 652: 0.767052767052767,
382 653: 0.781209781209781,
383 654: 0.795366795366795,
384 655: 0.823680823680824,
385 656: 0.851994851994852,
386 657: 0.864864864864865,
387 658: 0.877734877734878,
388 659: 0.887387387387387,
389 660: 0.897039897039897,
390 661: 0.888674388674388,
391 662: 0.880308880308880,
392 663: 0.871299871299871,
393 664: 0.862290862290862,
394 665: 0.871943371943372,
395 666: 0.881595881595882,
396 667: 0.909266409266410,
397 668: 0.936936936936937,
398 669: 0.953667953667954,
399 670: 0.970398970398970,
400 671: 0.960746460746461,
401 672: 0.951093951093951,
402 673: 0.923423423423424,
403 674: 0.895752895752896,
404 675: 0.852638352638353,
405 676: 0.809523809523810,
406 677: 0.787644787644788,
407 678: 0.765765765765766,
408 679: 0.770270270270271,
409 680: 0.774774774774775,
410 681: 0.803088803088803,
411 682: 0.831402831402831,
412 683: 0.842985842985843,
413 684: 0.854568854568855,
414 685: 0.835263835263836,
415 686: 0.815958815958816,
416 687: 0.776061776061776,
417 688: 0.736164736164736,
418 689: 0.704633204633204,
419 690: 0.673101673101673,
420 691: 0.679536679536680,
421 692: 0.685971685971686,
422 693: 0.718790218790219,
423 694: 0.751608751608752,
424 695: 0.791505791505791,
425 696: 0.831402831402831,
426 697: 0.841698841698842,
427 698: 0.851994851994852,
428 699: 0.810810810810811,
429 700: 0.769626769626770,
430 701: 0.716216216216216,
431 702: 0.662805662805663,
432 703: 0.622265122265123,
433 704: 0.581724581724582,
434 705: 0.546975546975547,
435 706: 0.512226512226512,
436 707: 0.503217503217503,
437 708: 0.494208494208494,
438 709: 0.480051480051480,
439 710: 0.465894465894466,
440 711: 0.460746460746461,
441 712: 0.455598455598456,
442 713: 0.449806949806950,
443 714: 0.444015444015444,
444 715: 0.437580437580438,
445 716: 0.431145431145431,
446 717: 0.439510939510940,
447 718: 0.447876447876448,
448 719: 0.454954954954955,
449 720: 0.462033462033462,
450 721: 0.471685971685971,
451 722: 0.481338481338481,
452 723: 0.488416988416988,
453 724: 0.495495495495495,
454 725: 0.490347490347490,
455 726: 0.485199485199485,
456 727: 0.476190476190476,
457 728: 0.467181467181467,
458 729: 0.462676962676963,
459 730: 0.458172458172458,
460 731: 0.442084942084942,
461 732: 0.425997425997426,
462 733: 0.422136422136422,
463 734: 0.418275418275418,
464 735: 0.411196911196911,
465 736: 0.404118404118404,
466 737: 0.415701415701415,
467 738: 0.427284427284427,
468 739: 0.427927927927928,
469 740: 0.428571428571429,
470 741: 0.415701415701416,
471 742: 0.402831402831403,
472 743: 0.415057915057915,
473 744: 0.427284427284427,
474 745: 0.413770913770913,
475 746: 0.400257400257400,
476 747: 0.404761904761904,
477 748: 0.409266409266409,
478 749: 0.421492921492922,
479 750: 0.433719433719434,
480 751: 0.443371943371944,
481 752: 0.453024453024453,
482 753: 0.494208494208494,
483 754: 0.535392535392535,
484 755: 0.544401544401544,
485 756: 0.553410553410553,
486 757: 0.566924066924066,
487 758: 0.580437580437580,
488 759: 0.569498069498069,
489 760: 0.558558558558559,
490 761: 0.547619047619048,
491 762: 0.536679536679537,
492 763: 0.584298584298585,
493 764: 0.631917631917632,
494 765: 0.656370656370657,
495 766: 0.680823680823681,
496 767: 0.659588159588160,
497 768: 0.638352638352638,
498 769: 0.646718146718146,
499 770: 0.655083655083655,
500 771: 0.617760617760617,
501 772: 0.580437580437580,
502 773: 0.545045045045045,
503 774: 0.509652509652510,
504 775: 0.488416988416988,
505 776: 0.467181467181467,
506 777: 0.441441441441441,
507 778: 0.415701415701416,
508 779: 0.433075933075933,
509 780: 0.450450450450450,
510 781: 0.000000000000000,
511 782: 0.000000000000000,
512 783: 0.000000000000000,
513 784: 0.000000000000000,
514 785: 0.000000000000000,
515 786: 0.000000000000000,
516 787: 0.000000000000000,
517 788: 0.000000000000000,
518 789: 0.000000000000000,
519 790: 0.000000000000000,
520 791: 0.000000000000000,
521 792: 0.000000000000000,
522 793: 0.000000000000000,
523 794: 0.000000000000000,
524 795: 0.000000000000000,
525 796: 0.000000000000000,
526 797: 0.000000000000000,
527 798: 0.000000000000000,
528 799: 0.000000000000000,
529 800: 0.000000000000000,
530 801: 0.000000000000000,
531 802: 0.000000000000000,
532 803: 0.000000000000000,
533 804: 0.000000000000000,
534 805: 0.000000000000000,
535 806: 0.000000000000000,
536 807: 0.000000000000000,
537 808: 0.000000000000000,
538 809: 0.000000000000000,
539 810: 0.000000000000000,
540 811: 0.000000000000000,
541 812: 0.000000000000000,
542 813: 0.000000000000000,
543 814: 0.000000000000000,
544 815: 0.000000000000000,
545 816: 0.000000000000000,
546 817: 0.000000000000000,
547 818: 0.000000000000000,
548 819: 0.000000000000000,
549 820: 0.000000000000000,
550 821: 0.000000000000000,
551 822: 0.000000000000000,
552 823: 0.000000000000000,
553 824: 0.000000000000000,
554 825: 0.000000000000000,
555 826: 0.000000000000000,
556 827: 0.000000000000000,
557 828: 0.000000000000000,
558 829: 0.000000000000000,
559 830: 0.000000000000000,
560}
563class TestSpectralSimilarityIndex:
564 """
565 Define :func:`colour.quality.ssi.spectral_similarity_index`
566 definition unit tests methods.
567 """
569 def test_spectral_similarity_index(self) -> None:
570 """Test :func:`colour.quality.ssi.spectral_similarity_index` definition."""
572 if not is_scipy_installed(): # pragma: no cover
573 return
575 assert (
576 spectral_similarity_index(SDS_ILLUMINANTS["C"], SDS_ILLUMINANTS["D65"])
577 == 94.0
578 )
579 assert (
580 spectral_similarity_index(
581 SpectralDistribution(DATA_HMI), SDS_ILLUMINANTS["D50"]
582 )
583 == 72.0
584 )
586 np.testing.assert_allclose(
587 spectral_similarity_index(
588 SDS_ILLUMINANTS["C"],
589 SDS_ILLUMINANTS["D65"],
590 round_result=False,
591 ),
592 94.182971057336000,
593 atol=TOLERANCE_ABSOLUTE_TESTS,
594 )
596 np.testing.assert_allclose(
597 spectral_similarity_index(
598 SpectralDistribution(DATA_HMI),
599 SDS_ILLUMINANTS["D50"],
600 round_result=False,
601 ),
602 71.775054824255550,
603 atol=TOLERANCE_ABSOLUTE_TESTS,
604 )
606 sd_led_1 = sd_single_led(520, half_spectral_width=45)
607 sd_led_2 = sd_single_led(540, half_spectral_width=55)
608 sd_led_3 = sd_single_led(560, half_spectral_width=50)
610 msds = sds_and_msds_to_msds([sd_led_1, sd_led_2, sd_led_3])
611 sd_reference = sd_single_led(535, half_spectral_width=48)
613 np.testing.assert_array_equal(
614 spectral_similarity_index(msds, msds), [100.0, 100.0, 100.0]
615 )
617 np.testing.assert_allclose(
618 spectral_similarity_index(msds, msds, round_result=False),
619 [100.0, 100.0, 100.0],
620 atol=TOLERANCE_ABSOLUTE_TESTS,
621 )
623 np.testing.assert_allclose(
624 spectral_similarity_index(msds, sd_reference),
625 [52.0, 82.0, 18.0],
626 atol=TOLERANCE_ABSOLUTE_TESTS,
627 )
629 np.testing.assert_allclose(
630 spectral_similarity_index(sd_reference, msds),
631 [50.0, 84.0, 20.0],
632 atol=TOLERANCE_ABSOLUTE_TESTS,
633 )