Skip to content
Merged
Show file tree
Hide file tree
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
91 changes: 91 additions & 0 deletions src/easyscience/fitting/minimizers/minimizer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Copy link
Contributor Author

@andped10 andped10 Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetched from bumps / dfo

"""
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:
Copy link
Contributor Author

@andped10 andped10 Aug 6, 2024

Choose a reason for hiding this comment

The 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
Expand Down
86 changes: 0 additions & 86 deletions src/easyscience/fitting/minimizers/minimizer_bumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# © 2021-2023 Contributors to the EasyScience project <https://github.com/easyScience/EasyScience

import copy
import inspect
from typing import Callable
from typing import List
from typing import Optional
Expand Down Expand Up @@ -54,7 +53,6 @@ def __init__(
:type fit_function: Callable
"""
super().__init__(obj=obj, fit_function=fit_function, method=method)
self._cached_pars_order = ()
self._p_0 = {}

def available_methods(self) -> List[str]:
Expand Down Expand Up @@ -204,90 +202,6 @@ def _make_func(x, y, weights):

return _outer(self)

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
:rtype: Callable
"""
# Original fit function
func = self._original_fit_function
# Get a list of `Parameters`
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 a bumps 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 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):
if self._cached_pars[par_name].value != value:
self._cached_pars[par_name].value = value
else:
if self._cached_pars[par_name].raw_value != value:
self._cached_pars[par_name].value = value

# update_fun = self._cached_pars[par_name]._callback.fset
# if update_fun:
# update_fun(value)
# TODO Pre processing here
for constraint in self.fit_constraints():
constraint()
return_data = func(x)
# TODO Loading or manipulating data here
return return_data

# Fake 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.

## TODO clean when full move to new_variable
from easyscience.Objects.new_variable import Parameter

if isinstance(parameter, Parameter):
default_value = parameter.value
else:
default_value = parameter.raw_value

self._cached_pars_order = tuple(self._cached_pars.keys())
params = [
inspect.Parameter('x', inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=inspect._empty),
*[
inspect.Parameter(
MINIMIZER_PARAMETER_PREFIX + str(name),
inspect.Parameter.POSITIONAL_OR_KEYWORD,
annotation=inspect._empty,
default=default_value,
)
for name in self._cached_pars_order
],
]
# Sign the function
_fit_function.__signature__ = inspect.Signature(params)
self._fit_function = _fit_function
return _fit_function

def _set_parameter_fit_result(self, fit_result, stack_status: bool):
"""
Update parameters to their final values and assign a std error to them.
Expand Down
58 changes: 0 additions & 58 deletions src/easyscience/fitting/minimizers/minimizer_dfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,64 +174,6 @@ def _residuals(pars_values: List[float]) -> np.ndarray:

return _outer(self)

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
:rtype: Callable
"""
# 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

self._fit_function = _fit_function
return _fit_function

def _set_parameter_fit_result(self, fit_result, stack_status, ci: float = 0.95) -> None:
"""
Update parameters to their final values and assign a std error to them.
Expand Down
Loading