himoto / hillfit

Fitting the Hill Equation to Experimental Data
MIT License
11 stars 4 forks source link

Getting a RuntimeError: maximum number of function evaluations is exceeded. #22

Open martinspurr89 opened 2 years ago

martinspurr89 commented 2 years ago
hf = hillfit.HillFit(x_data, y_data)`
hf.fitting(x_label = calib_par, y_label = col, title = 'Fitted Hill equation', sigfigs = 6,
           log_x = False, print_r_sqr = True, view_figure = True)

gives for certain data:

RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_27968/2020781688.py in <module>
     10     hf = hillfit.HillFit(x_data, y_data)
     11     hf.fitting(x_label = calib_par, y_label = col, title = 'Fitted Hill equation', sigfigs = 6,
---> 12            log_x = False, print_r_sqr = True, view_figure = True)

~\Python\PythonLib\.python\Python37\site-packages\hillfit\fitting.py in fitting(self, x_label, y_label, title, sigfigs, log_x, print_r_sqr, view_figure)
    119             np.log10(self.x_data[0]), np.log10(self.x_data[-1]), len(self.y_data)
    120         )
--> 121         params = self._get_param()
    122         self.y_fit = self._equation(self.x_fit, *params)
    123         self.equation = f"{round(self.bottom, sigfigs)} + ({round(self.top, sigfigs)}-{round(self.bottom, sigfigs)})*x**{(round(self.nH, sigfigs))} / ({round(self.ec50, sigfigs)}**{(round(self.nH, sigfigs))} + x**{(round(self.nH, sigfigs))})"

~\Python\PythonLib\.python\Python37\site-packages\hillfit\fitting.py in _get_param(self)
     53             self.y_data,
     54             p0=param_initial,
---> 55             bounds=param_bounds,
     56         )
     57         if not self.bottom_param:

~\Python\PythonLib\.python\Python37\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
    802 
    803         if not res.success:
--> 804             raise RuntimeError("Optimal parameters not found: " + res.message)
    805 
    806         ysize = len(res.fun)

RuntimeError: Optimal parameters not found: The maximum number of function evaluations is exceeded.

Can we get a maxfev option like in scipy curve_fit?

himoto commented 2 years ago

Hi @martinspurr89, I have added curve_fit_kws (dict) argument to hf.fitting() function (#23). To try this out, please use hillfit>=0.1.6. You can pass any options to scipy.optimize.curve_fit:

hf = hillfit.HillFit(x_data, y_data)
hf.fitting(x_label = calib_par, y_label = col, title = 'Fitted Hill equation', sigfigs = 6,
           log_x = False, print_r_sqr = True, view_figure = True, curve_fit_kws={"maxfev": 1000})