@@ -53,9 +53,7 @@ def supported_methods() -> List[str]:
5353
5454 @staticmethod
5555 def all_methods () -> List [str ]:
56- return [
57- 'leastsq' ,
58- ]
56+ return ['leastsq' ]
5957
6058 def fit (
6159 self ,
@@ -65,8 +63,8 @@ def fit(
6563 model : Optional [Callable ] = None ,
6664 parameters : Optional [List [Parameter ]] = None ,
6765 method : str = None ,
68- xtol : float = 1e-6 ,
69- ftol : float = 1e-8 ,
66+ tolerance : Optional [ float ] = None ,
67+ max_evaluations : Optional [ int ] = None ,
7068 ** kwargs ,
7169 ) -> FitResults :
7270 """
@@ -110,6 +108,8 @@ def fit(
110108 stack_status = global_object .stack .enabled
111109 global_object .stack .enabled = False
112110
111+ kwargs = self ._prepare_kwargs (tolerance , max_evaluations , ** kwargs )
112+
113113 try :
114114 model_results = self ._dfo_fit (self ._cached_pars , model , ** kwargs )
115115 self ._set_parameter_fit_result (model_results , stack_status )
@@ -239,7 +239,11 @@ def _gen_fit_results(self, fit_results, weights, **kwargs) -> FitResults:
239239 return results
240240
241241 @staticmethod
242- def _dfo_fit (pars : Dict [str , Parameter ], model : Callable , ** kwargs ):
242+ def _dfo_fit (
243+ pars : Dict [str , Parameter ],
244+ model : Callable ,
245+ ** kwargs ,
246+ ):
243247 """
244248 Method to convert EasyScience styling to DFO-LS styling (yes, again)
245249
@@ -261,13 +265,23 @@ def _dfo_fit(pars: Dict[str, Parameter], model: Callable, **kwargs):
261265 np .array ([par .max for par in pars .values ()]),
262266 )
263267 # https://numericalalgorithmsgroup.github.io/dfols/build/html/userguide.html
264- if np .isinf (bounds ).any ():
265- results = dfols .solve (model , pars_values , bounds = bounds , ** kwargs )
266- else :
268+ if not np .isinf (bounds ).any ():
267269 # It is only possible to scale (normalize) variables if they are bound (different from inf)
268- results = dfols .solve (model , pars_values , bounds = bounds , scaling_within_bounds = True , ** kwargs )
270+ kwargs ['scaling_within_bounds' ] = True
271+
272+ results = dfols .solve (model , pars_values , bounds = bounds , ** kwargs )
269273
270274 if 'Success' not in results .msg :
271275 raise FitError (f'Fit failed with message: { results .msg } ' )
272276
273277 return results
278+
279+ @staticmethod
280+ def _prepare_kwargs (tolerance : Optional [float ] = None , max_evaluations : Optional [int ] = None , ** kwargs ) -> dict [str :str ]:
281+ if max_evaluations is not None :
282+ kwargs ['maxfun' ] = max_evaluations # max number of function evaluations
283+ if tolerance is not None :
284+ if 0.1 < tolerance : # dfo module throws errer if larger value
285+ raise ValueError ('Tolerance must be equal or smaller than 0.1' )
286+ kwargs ['rhoend' ] = tolerance # size of the trust region
287+ return kwargs
0 commit comments