-
-
Notifications
You must be signed in to change notification settings - Fork 261
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
Conversation
Looks like this is well formed now. |
@KelSolaar Other than the issue with static type checking (looks like something related to a new version of pip and an old installation method from a dependency) this should be ready for review. |
Hey @tjdcs, Thank you! Two notes:
@property
def function(self) -> Callable:
"""
Getter property for the continuous signal callable.
Returns
-------
Callable
Continuous signal callable.
"""
if self._function is None:
self._create_function()
return cast(Callable, self._function)
@property
def function(self) -> Callable:
"""
Getter property for the continuous signal callable.
Returns
-------
Callable
Continuous signal callable.
"""
if self._function is None:
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
return cast(Callable, self._function) |
Learning the enforced coding style is hard.
Thank you! There are some issues with GH so I cannot comment on the changes directly, anyway, the |
Summary
This PR implements a performance improvement for
Signal
insignal.py
.The initialization of _function, the private backing attribute for the
function
property, is relatively expensive. This performance issue was discovered when creating an MSDS object from a long list of SpectralDistributions or large ndarrays. The _create_function is invoked frequently when not needed. This PR updates relavent code to always use the function property. the function property has been changed to only invoke _create_function if the _function property has been invalidated (by setting it equal to None). If the function property is never accessed, then the relevant initialization does not take place (lazy initialization)Under some conditions, this change results in a 90% reduction in Signal initialization run time
Preflight
Code Style and Quality
colour
,colour.models
.Documentation