From 85f6a57aa604718b2c72e9d9ebb945cd712f9047 Mon Sep 17 00:00:00 2001 From: Thomas Mansencal Date: Thu, 17 Nov 2022 19:25:17 +1300 Subject: [PATCH] Further optimize "colour.SpectralDistribution.shape" computation by expecting and assuming that the domain is monotonic. --- colour/colorimetry/spectrum.py | 19 +++++-------------- colour/continuous/signal.py | 5 +++++ colour/utilities/common.py | 3 ++- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/colour/colorimetry/spectrum.py b/colour/colorimetry/spectrum.py index 96a1c6c8c4..83508f1709 100644 --- a/colour/colorimetry/spectrum.py +++ b/colour/colorimetry/spectrum.py @@ -836,26 +836,17 @@ def shape(self) -> SpectralShape: SpectralShape(500.0, 600.0, 10.0) """ - wavelengths_interval = interval(self.wavelengths) + wavelengths = self.wavelengths + wavelengths_interval = interval(wavelengths) if wavelengths_interval.size != 1: runtime_warning( f'"{self.name}" spectral distribution is not uniform, using ' f"minimum interval!" ) - # If wavelengths is sorted, then go fast. Otherwise compute the shape. - if np.all(self.domain[:-1] <= self.domain[1:]): - return SpectralShape( - self.domain[0], - self.domain[-1], - min(wavelengths_interval), - ) - else: - return SpectralShape( - min(self.domain), - max(self.domain), - min(wavelengths_interval), - ) + return SpectralShape( + wavelengths[0], wavelengths[-1], min(wavelengths_interval) + ) def interpolate( self, diff --git a/colour/continuous/signal.py b/colour/continuous/signal.py index e82f7a9514..4634b82204 100644 --- a/colour/continuous/signal.py +++ b/colour/continuous/signal.py @@ -343,6 +343,11 @@ def domain(self, value: ArrayLike): f'"{self.name}" new "domain" variable is not finite: {value}, ' f"unpredictable results may occur!" ) + else: + attest( + np.all(value[:-1] <= value[1:]), + "The new domain value is not monotonic! ", + ) if value.size != self._range.size: self._range = np.resize(self._range, value.shape) diff --git a/colour/utilities/common.py b/colour/utilities/common.py index 4cfe645866..f38b38ced8 100644 --- a/colour/utilities/common.py +++ b/colour/utilities/common.py @@ -36,6 +36,7 @@ Boolean, Callable, Dict, + DTypeBoolean, Generator, Integer, Iterable, @@ -366,7 +367,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any: return wrapper -def attest(condition: Boolean, message: str = ""): +def attest(condition: Union[Boolean, DTypeBoolean], message: str = ""): """ Provide the `assert` statement functionality without being disabled by optimised Python execution.