-
Notifications
You must be signed in to change notification settings - Fork 2
minimizer consolidation #45
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
Merged
Parent:
Preparation for 2.0.0 release
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ | |
|
|
||
| from abc import ABCMeta | ||
| from abc import abstractmethod | ||
| from inspect import Parameter as InspectParameter | ||
| from inspect import Signature | ||
| from inspect import _empty | ||
| from typing import Callable | ||
| from typing import Dict | ||
| from typing import List | ||
|
|
@@ -166,6 +169,94 @@ def _prepare_parameters(self, parameters: dict[str, float]) -> dict[str, float]: | |
| parameters[parameter_name] = item.raw_value | ||
| return parameters | ||
|
|
||
| def _generate_fit_function(self) -> Callable: | ||
| """ | ||
| Using the user supplied `fit_function`, wrap it in such a way we can update `Parameter` on | ||
| iterations. | ||
|
|
||
| :return: a fit function which is compatible with bumps models | ||
| """ | ||
| # Original fit function | ||
| func = self._original_fit_function | ||
| # Get a list of `Parameters` | ||
| self._cached_pars = {} | ||
| self._cached_pars_vals = {} | ||
| for parameter in self._object.get_fit_parameters(): | ||
| key = parameter.unique_name | ||
| self._cached_pars[key] = parameter | ||
| self._cached_pars_vals[key] = (parameter.value, parameter.error) | ||
|
|
||
| # Make a new fit function | ||
| def _fit_function(x: np.ndarray, **kwargs): | ||
| """ | ||
| Wrapped fit function which now has an EasyScience compatible form | ||
|
|
||
| :param x: array of data points to be calculated | ||
| :type x: np.ndarray | ||
| :param kwargs: key word arguments | ||
| :return: points calculated at `x` | ||
| :rtype: np.ndarray | ||
| """ | ||
| # Update the `Parameter` values and the callback if needed | ||
| # TODO THIS IS NOT THREAD SAFE :-( | ||
| # TODO clean when full move to new_variable | ||
| from easyscience.Objects.new_variable import Parameter | ||
|
|
||
| for name, value in kwargs.items(): | ||
| par_name = name[1:] | ||
| if par_name in self._cached_pars.keys(): | ||
| # TODO clean when full move to new_variable | ||
| if isinstance(self._cached_pars[par_name], Parameter): | ||
| # This will take into account constraints | ||
| if self._cached_pars[par_name].value != value: | ||
| self._cached_pars[par_name].value = value | ||
| else: | ||
| # This will take into account constraints | ||
| if self._cached_pars[par_name].raw_value != value: | ||
| self._cached_pars[par_name].value = value | ||
|
|
||
| # Since we are calling the parameter fset will be called. | ||
| # TODO Pre processing here | ||
| for constraint in self.fit_constraints(): | ||
| constraint() | ||
| return_data = func(x) | ||
| # TODO Loading or manipulating data here | ||
| return return_data | ||
|
|
||
| _fit_function.__signature__ = self._create_signature(self._cached_pars) | ||
| return _fit_function | ||
|
|
||
| @staticmethod | ||
| def _create_signature(parameters: Dict[int, Parameter]) -> Signature: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fetched from bumps / lmfit |
||
| """ | ||
| Wrap the function signature. | ||
| This is done as lmfit wants the function to be in the form: | ||
| f = (x, a=1, b=2)... | ||
| Where we need to be generic. Note that this won't hold for much outside of this scope. | ||
| """ | ||
| wrapped_parameters = [] | ||
| wrapped_parameters.append(InspectParameter('x', InspectParameter.POSITIONAL_OR_KEYWORD, annotation=_empty)) | ||
|
|
||
| ## TODO clean when full move to new_variable | ||
| from easyscience.Objects.new_variable import Parameter as NewParameter | ||
|
|
||
| for name, parameter in parameters.items(): | ||
| ## TODO clean when full move to new_variable | ||
| if isinstance(parameter, NewParameter): | ||
| default_value = parameter.value | ||
| else: | ||
| default_value = parameter.raw_value | ||
|
|
||
| wrapped_parameters.append( | ||
| InspectParameter( | ||
| MINIMIZER_PARAMETER_PREFIX + str(name), | ||
| InspectParameter.POSITIONAL_OR_KEYWORD, | ||
| annotation=_empty, | ||
| default=default_value, | ||
| ) | ||
| ) | ||
| return Signature(wrapped_parameters) | ||
|
|
||
| @staticmethod | ||
| def _error_from_jacobian(jacobian: np.ndarray, residuals: np.ndarray, confidence: float = 0.95) -> np.ndarray: | ||
| from scipy import stats | ||
|
|
||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetched from bumps / dfo