PR: Improve initialization time of "Signal", a core component of "Colour".#1057
Merged
KelSolaar merged 4 commits intocolour-science:developfrom Oct 27, 2022
tjdcs:performance/signal-2
Merged
PR: Improve initialization time of "Signal", a core component of "Colour".#1057KelSolaar merged 4 commits intocolour-science:developfrom tjdcs:performance/signal-2
KelSolaar merged 4 commits intocolour-science:developfrom
tjdcs:performance/signal-2
Conversation
Contributor
Author
|
Looks like this is well formed now. |
Contributor
Author
|
@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. |
Member
|
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.
Member
|
Thank you! There are some issues with GH so I cannot comment on the changes directly, anyway, the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements a performance improvement for
Signalinsignal.py.The initialization of _function, the private backing attribute for the
functionproperty, 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