Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/easyscience/fitting/minimizers/minimizer_bumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ def fit(
:type method: str
:return: Fit results
:rtype: ModelResult

For standard least squares, the weights should be 1/sigma, where
sigma is the standard deviation of the measurement. For
unweighted least squares, these should be 1.

"""
method_dict = self._get_method_kwargs(method)

Expand Down Expand Up @@ -191,6 +196,8 @@ def _make_model(self, parameters: Optional[List[BumpsParameter]] = None) -> Call
Generate a bumps model from the supplied `fit_function` and parameters in the base object.
Note that this makes a callable as it needs to be initialized with *x*, *y*, *weights*

Weights are converted to dy (standard deviation of y).

:return: Callable to make a bumps Curve model
:rtype: Callable
"""
Expand All @@ -205,7 +212,7 @@ def _make_func(x, y, weights):
else:
for par in parameters:
bumps_pars[MINIMIZER_PARAMETER_PREFIX + par.unique_name] = obj.convert_to_par_object(par)
return Curve(fit_func, x, y, dy=weights, **bumps_pars)
return Curve(fit_func, x, y, dy=1 / weights, **bumps_pars)

return _make_func

Expand Down
14 changes: 11 additions & 3 deletions src/easyscience/fitting/minimizers/minimizer_dfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def fit(
:type x: np.ndarray
:param y: measured points
:type y: np.ndarray
:param weights: Weights for supplied measured points
:param weights: Weights for supplied measured points.
:type weights: np.ndarray
:param model: Optional Model which is being fitted to
:type model: lmModel
Expand All @@ -85,6 +85,10 @@ def fit(
:type method: str
:return: Fit results
:rtype: ModelResult

For standard least squares, the weights should be 1/sigma, where
sigma is the standard deviation of the measurement. For
unweighted least squares, these should be 1.
"""
x, y, weights = np.asarray(x), np.asarray(y), np.asarray(weights)

Expand Down Expand Up @@ -163,7 +167,7 @@ def _make_func(x, y, weights):
def _residuals(pars_values: List[float]) -> np.ndarray:
for idx, par_name in enumerate(dfo_pars.keys()):
dfo_pars[par_name] = pars_values[idx]
return (y - fit_func(x, **dfo_pars)) / weights
return (y - fit_func(x, **dfo_pars)) * weights

return _residuals

Expand Down Expand Up @@ -267,7 +271,11 @@ def _dfo_fit(
return results

@staticmethod
def _prepare_kwargs(tolerance: Optional[float] = None, max_evaluations: Optional[int] = None, **kwargs) -> dict[str:str]:
def _prepare_kwargs(
tolerance: Optional[float] = None,
max_evaluations: Optional[int] = None,
**kwargs,
) -> dict[str:str]:
if max_evaluations is not None:
kwargs['maxfun'] = max_evaluations # max number of function evaluations
if tolerance is not None:
Expand Down
12 changes: 11 additions & 1 deletion src/easyscience/fitting/minimizers/minimizer_lmfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ def fit(
:param kwargs: Additional arguments for the fitting function.
:return: Fit results
:rtype: ModelResult

For standard least squares, the weights should be 1/sigma, where
sigma is the standard deviation of the measurement. For
unweighted least squares, these should be 1.

"""
x, y, weights = np.asarray(x), np.asarray(y), np.asarray(weights)

Expand Down Expand Up @@ -230,7 +235,12 @@ def _make_model(self, pars: Optional[LMParameters] = None) -> LMModel:
else:
value = item.value

model.set_param_hint(MINIMIZER_PARAMETER_PREFIX + str(name), value=value, min=item.min, max=item.max)
model.set_param_hint(
MINIMIZER_PARAMETER_PREFIX + str(name),
value=value,
min=item.min,
max=item.max,
)

# Cache the model for later reference
self._cached_model = model
Expand Down
Loading
Loading