-
Notifications
You must be signed in to change notification settings - Fork 2
Tests for DFO minimizer #40
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
Changes from all commits
a04d5b1
731d392
d6b8eb3
c7e3c1a
38b6990
d9137c2
e97bac8
b57938a
7fda83f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| from .fitter import Fitter # noqa: F401, E402 | ||
| from .minimizers.minimizer_base import FitResults # noqa: F401, E402 | ||
| from .fitter import Fitter | ||
| from .minimizers.utils import FitResults | ||
|
|
||
| # Causes circular import | ||
| # from .multi_fitter import MultiFitter # noqa: F401, E402 | ||
|
|
||
| all = [Fitter, FitResults] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,8 @@ | |
|
|
||
| import numpy as np | ||
|
|
||
| #causes circular import when Parameter is imported | ||
| #from easyscience.Objects.ObjectClasses import BaseObj | ||
| # causes circular import when Parameter is imported | ||
| # from easyscience.Objects.ObjectClasses import BaseObj | ||
| from easyscience.Objects.Variable import Parameter | ||
|
|
||
| from ..Constraints import ObjConstraint | ||
|
|
@@ -31,7 +31,9 @@ class MinimizerBase(metaclass=ABCMeta): | |
|
|
||
| wrapping: str = None | ||
|
|
||
| def __init__(self, obj, fit_function: Callable, method: Optional[str] = None): # todo after constraint changes, add type hint: obj: BaseObj # noqa: E501 | ||
| def __init__( | ||
| self, obj, fit_function: Callable, method: Optional[str] = None | ||
| ): # todo after constraint changes, add type hint: obj: BaseObj # noqa: E501 | ||
| if method not in self.available_methods(): | ||
| raise FitError(f'Method {method} not available in {self.__class__}') | ||
| self._object = obj | ||
|
|
@@ -59,32 +61,17 @@ def add_fit_constraint(self, constraint: ObjConstraint): | |
| def remove_fit_constraint(self, index: int) -> None: | ||
| del self._constraints[index] | ||
|
|
||
| @abstractmethod | ||
| def make_model(self, pars: List[Parameter] = None): | ||
| """ | ||
| Generate an engine model from the supplied `fit_function` and parameters in the base object. | ||
|
|
||
| :return: Callable model | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def _generate_fit_function(self) -> Callable: | ||
| """ | ||
| Using the user supplied `fit_function`, wrap it in such a way we can update `Parameter` on | ||
| iterations. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def fit( | ||
| self, | ||
| x: np.ndarray, | ||
| y: np.ndarray, | ||
| weights: Optional[np.ndarray] = None, | ||
| model=None, | ||
| parameters=None, | ||
| method=None, | ||
| model: Optional[Callable] = None, | ||
| parameters: Optional[Parameter] = None, | ||
| method: Optional[str] = None, | ||
| **kwargs, | ||
| ): | ||
| ) -> FitResults: | ||
| """ | ||
| Perform a fit using the engine. | ||
|
|
||
|
|
@@ -119,7 +106,7 @@ def evaluate(self, x: np.ndarray, minimizer_parameters: dict[str, float] = None, | |
| if minimizer_parameters is None: | ||
| minimizer_parameters = {} | ||
| if not isinstance(minimizer_parameters, dict): | ||
| raise TypeError("minimizer_parameters must be a dictionary") | ||
| raise TypeError('minimizer_parameters must be a dictionary') | ||
|
|
||
| if self._fit_function is None: | ||
| # This will also generate self._cached_pars | ||
|
|
@@ -129,26 +116,6 @@ def evaluate(self, x: np.ndarray, minimizer_parameters: dict[str, float] = None, | |
|
|
||
| return self._fit_function(x, **minimizer_parameters, **kwargs) | ||
|
|
||
| def _prepare_parameters(self, parameters: dict[str, float]) -> dict[str, float]: | ||
|
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. Moved below |
||
| """ | ||
| Prepare the parameters for the minimizer. | ||
|
|
||
| :param parameters: Dict of parameters for the minimizer with names as keys. | ||
| """ | ||
| pars = self._cached_pars | ||
|
|
||
| for name, item in pars.items(): | ||
| parameter_name = MINIMIZER_PARAMETER_PREFIX + str(name) | ||
| if parameter_name not in parameters.keys(): | ||
| ## TODO clean when full move to new_variable | ||
| from easyscience.Objects.new_variable import Parameter as NewParameter | ||
|
|
||
| if isinstance(item, NewParameter): | ||
| parameters[parameter_name] = item.value | ||
| else: | ||
| parameters[parameter_name] = item.raw_value | ||
| return parameters | ||
|
|
||
| @abstractmethod | ||
| def convert_to_pars_obj(self, par_list: Optional[Union[list]] = None): | ||
| """ | ||
|
|
@@ -170,30 +137,31 @@ def available_methods(self) -> List[str]: | |
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def convert_to_par_object(obj): # todo after constraint changes, add type hint: obj: BaseObj | ||
| def convert_to_par_object(obj): # todo after constraint changes, add type hint: obj: BaseObj | ||
| """ | ||
| Convert an `EasyScience.Objects.Base.Parameter` object to an engine Parameter object. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
|
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. removed |
||
| def _set_parameter_fit_result(self, fit_result): | ||
| def _prepare_parameters(self, parameters: dict[str, float]) -> dict[str, float]: | ||
| """ | ||
| Update parameters to their final values and assign a std error to them. | ||
| Prepare the parameters for the minimizer. | ||
|
|
||
| :param fit_result: Fit object which contains info on the fit | ||
| :return: None | ||
| :rtype: noneType | ||
| :param parameters: Dict of parameters for the minimizer with names as keys. | ||
| """ | ||
| pars = self._cached_pars | ||
|
|
||
| @abstractmethod | ||
|
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. removed |
||
| def _gen_fit_results(self, fit_results, **kwargs) -> FitResults: | ||
| """ | ||
| Convert fit results into the unified `FitResults` format. | ||
| # TODO clean when full move to new_variable | ||
| from easyscience.Objects.new_variable import Parameter as NewParameter | ||
|
|
||
| :param fit_result: Fit object which contains info on the fit | ||
| :return: fit results container | ||
| :rtype: FitResults | ||
| """ | ||
| for name, item in pars.items(): | ||
| parameter_name = MINIMIZER_PARAMETER_PREFIX + str(name) | ||
| if parameter_name not in parameters.keys(): | ||
| # TODO clean when full move to new_variable | ||
| if isinstance(item, NewParameter): | ||
| parameters[parameter_name] = item.value | ||
| else: | ||
| parameters[parameter_name] = item.raw_value | ||
| return parameters | ||
|
|
||
| @staticmethod | ||
| def _error_from_jacobian(jacobian: np.ndarray, residuals: np.ndarray, confidence: float = 0.95) -> np.ndarray: | ||
|
|
@@ -210,4 +178,3 @@ def _error_from_jacobian(jacobian: np.ndarray, residuals: np.ndarray, confidence | |
| z = stats.norm.pdf(z) | ||
| error_matrix = z * np.sqrt(error_matrix) | ||
| return error_matrix | ||
|
|
||
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.
removed