Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Improve initialization time of "Signal", a core component of "Colour". #1057

Merged
merged 4 commits into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions colour/continuous/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand All @@ -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:
Expand Down Expand Up @@ -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]:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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[
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down