Skip to content

Commit

Permalink
Add argument for calculating continuous spectral_similarity_index v…
Browse files Browse the repository at this point in the history
…alues. (#1117)
  • Loading branch information
tjdcs authored Mar 14, 2023
1 parent 82e0390 commit 35424bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
17 changes: 12 additions & 5 deletions colour/quality/ssi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from colour.algebra import LinearInterpolator, sdiv, sdiv_mode
from colour.colorimetry import SpectralDistribution, SpectralShape, reshape_sd
from colour.hints import NDArrayFloat
from colour.utilities import as_float_scalar, zeros
from colour.utilities import zeros

__author__ = "Colour Developers"
__copyright__ = "Copyright 2013 Colour Developers"
Expand All @@ -44,8 +44,10 @@


def spectral_similarity_index(
sd_test: SpectralDistribution, sd_reference: SpectralDistribution
) -> float:
sd_test: SpectralDistribution,
sd_reference: SpectralDistribution,
round_results: bool = True,
) -> NDArrayFloat:
"""
Return the *Academy Spectral Similarity Index* (SSI) of given test
spectral distribution with given reference spectral distribution.
Expand All @@ -56,6 +58,9 @@ def spectral_similarity_index(
Test spectral distribution.
sd_reference
Reference spectral distribution.
round_results
Controls rounding the results. Particularly useful when using SSI in an
optimization routine. Default True.
Returns
-------
Expand Down Expand Up @@ -143,6 +148,8 @@ def spectral_similarity_index(
c_wdr_i = convolve1d(np.hstack([0, wdr_i, 0]), [0.22, 0.56, 0.22])
m_v = np.sum(c_wdr_i**2)

SSI = np.around(100 - 32 * np.sqrt(m_v))
SSI = 100 - 32 * np.sqrt(m_v)

return as_float_scalar(SSI)
if round_results:
return np.around(SSI)
return SSI
26 changes: 26 additions & 0 deletions colour/quality/tests/test_ssi.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,32 @@ def test_spectral_similarity_index(self):
72.0,
)

def test_spectral_similarity_rounding(self):
"""
Test :func:`colour.quality.ssi.spectral_similarity_index` for
producing continuous values.
"""

# Test values were computed at ed2e90
self.assertAlmostEqual(
spectral_similarity_index(
SDS_ILLUMINANTS["C"],
SDS_ILLUMINANTS["D65"],
round_results=False,
),
94.178,
places=2,
)
self.assertAlmostEqual(
spectral_similarity_index(
SpectralDistribution(DATA_HMI),
SDS_ILLUMINANTS["D50"],
round_results=False,
),
71.772,
places=2,
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 35424bc

Please sign in to comment.