Skip to content

Commit

Permalink
Further optimize "colour.SpectralDistribution.shape" computation by e…
Browse files Browse the repository at this point in the history
…xpecting and assuming that the domain is monotonic.
  • Loading branch information
KelSolaar committed Nov 17, 2022
1 parent f454229 commit 85f6a57
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
19 changes: 5 additions & 14 deletions colour/colorimetry/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions colour/continuous/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion colour/utilities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
Boolean,
Callable,
Dict,
DTypeBoolean,
Generator,
Integer,
Iterable,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 85f6a57

Please sign in to comment.