diff --git a/colour/continuous/signal.py b/colour/continuous/signal.py index 9a5cb5e692..229e4014f3 100644 --- a/colour/continuous/signal.py +++ b/colour/continuous/signal.py @@ -276,7 +276,7 @@ def __init__( "extrapolator_kwargs", self._extrapolator_kwargs ) - self._create_function() + self._function: Callable | None = None @property def dtype(self) -> Type[DTypeFloating]: @@ -349,7 +349,7 @@ def domain(self, value: ArrayLike): self._range = np.resize(self._range, value.shape) self._domain = value - self._create_function() + self._function = None # Invalidate internal _function @property def range(self) -> NDArray: @@ -389,7 +389,7 @@ def range(self, value: ArrayLike): ) self._range = value - self._create_function() + self._function = None # Invalidate internal _function @property def interpolator(self) -> Type[TypeInterpolator]: @@ -416,7 +416,7 @@ def interpolator(self, value: Type[TypeInterpolator]): # TODO: Check for interpolator compatibility. self._interpolator = value - self._create_function() + self._function = None # Invalidate internal _function @property def interpolator_kwargs(self) -> Dict: @@ -449,7 +449,7 @@ def interpolator_kwargs(self, value: dict): ) self._interpolator_kwargs = value - self._create_function() + self._function = None # Invalidate internal _function @property def extrapolator(self) -> Type[TypeExtrapolator]: @@ -476,7 +476,7 @@ def extrapolator(self, value: Type[TypeExtrapolator]): # TODO: Check for extrapolator compatibility. self._extrapolator = value - self._create_function() + self._function = None # Invalidate internal _function @property def extrapolator_kwargs(self) -> Dict: @@ -509,7 +509,7 @@ def extrapolator_kwargs(self, value: dict): ) self._extrapolator_kwargs = value - self._create_function() + self._function = None # Invalidate internal _function @property def function(self) -> Callable: @@ -522,7 +522,43 @@ def function(self) -> Callable: Continuous signal callable. """ - return self._function + if self._function is None: + # Create the underlying continuous function + + if self._domain.size != 0 and self._range.size != 0: + self._function = self._extrapolator( + self._interpolator( + self.domain, self.range, **self._interpolator_kwargs + ), + **self._extrapolator_kwargs, + ) + else: + + def _undefined_function(*args: Any, **kwargs: Any): + """ + Raise a :class:`ValueError` exception. + + Other Parameters + ---------------- + args + Arguments. + kwargs + Keywords arguments. + + Raises + ------ + ValueError + """ + + raise ValueError( + "Underlying signal interpolator function does not exists, " + "please ensure you defined both " + '"domain" and "range" variables!' + ) + + self._function = cast(Callable, _undefined_function) + + return cast(Callable, self._function) def __str__(self) -> str: """ @@ -677,7 +713,7 @@ def __getitem__( if isinstance(x, slice): return self._range[x] else: - return self._function(x) + return self.function(x) def __setitem__( self, x: Union[FloatingOrArrayLike, slice], y: FloatingOrArrayLike @@ -769,7 +805,7 @@ def __setitem__( self._domain = np.insert(self._domain, indexes, x_nm) self._range = np.insert(self._range, indexes, y[~mask]) - self._create_function() + self._function = None # Invalidate internal _function def __contains__(self, x: Union[FloatingOrArrayLike, slice]) -> bool: """ @@ -893,42 +929,6 @@ def __ne__(self, other: Any) -> bool: return not (self == other) - def _create_function(self): - """Create the continuous signal underlying function.""" - - if self._domain.size != 0 and self._range.size != 0: - self._function = self._extrapolator( - self._interpolator( - self.domain, self.range, **self._interpolator_kwargs - ), - **self._extrapolator_kwargs, - ) - else: - - def _undefined_function(*args: Any, **kwargs: Any): - """ - Raise a :class:`ValueError` exception. - - Other Parameters - ---------------- - args - Arguments. - kwargs - Keywords arguments. - - Raises - ------ - ValueError - """ - - raise ValueError( - "Underlying signal interpolator function does not exists, " - "please ensure you defined both " - '"domain" and "range" variables!' - ) - - self._function = _undefined_function - def _fill_domain_nan( self, method: Union[ @@ -955,7 +955,7 @@ def _fill_domain_nan( """ self._domain = fill_nan(self._domain, method, default) - self._create_function() + self._function = None # Invalidate internal _function def _fill_range_nan( self, @@ -983,7 +983,7 @@ def _fill_range_nan( """ self._range = fill_nan(self._range, method, default) - self._create_function() + self._function = None # Invalidate internal _function def arithmetical_operation( self,