|
70 | 70 | from specparam.core.modutils import copy_doc_func_to_method |
71 | 71 | from specparam.core.utils import group_three, check_array_dim |
72 | 72 | from specparam.core.funcs import gaussian_function, get_ap_func, infer_ap_func |
| 73 | +from specparam.core.jacobians import jacobian_gauss |
73 | 74 | from specparam.core.errors import (FitError, NoModelError, DataError, |
74 | 75 | NoDataError, InconsistentDataError) |
75 | 76 | from specparam.core.strings import (gen_settings_str, gen_model_results_str, |
@@ -191,12 +192,17 @@ def __init__(self, peak_width_limits=(0.5, 12.0), max_n_peaks=np.inf, min_peak_h |
191 | 192 | self._gauss_overlap_thresh = 0.75 |
192 | 193 | # Parameter bounds for center frequency when fitting gaussians, in terms of +/- std dev |
193 | 194 | self._cf_bound = 1.5 |
194 | | - # The maximum number of calls to the curve fitting function |
195 | | - self._maxfev = 5000 |
196 | 195 | # The error metric to calculate, post model fitting. See `_calc_error` for options |
197 | 196 | # Note: this is for checking error post fitting, not an objective function for fitting |
198 | 197 | self._error_metric = 'MAE' |
199 | 198 |
|
| 199 | + ## PRIVATE CURVE_FIT SETTINGS |
| 200 | + # The maximum number of calls to the curve fitting function |
| 201 | + self._maxfev = 5000 |
| 202 | + # The tolerance setting for curve fitting (see scipy.curve_fit - ftol / xtol / gtol) |
| 203 | + # Here reduce tolerance to speed fitting. Set value to 1e-8 to match curve_fit default |
| 204 | + self._tol = 0.00001 |
| 205 | + |
200 | 206 | ## RUN MODES |
201 | 207 | # Set default debug mode - controls if an error is raised if model fitting is unsuccessful |
202 | 208 | self._debug = False |
@@ -400,7 +406,7 @@ def report(self, freqs=None, power_spectrum=None, freq_range=None, |
400 | 406 | Only relevant / effective if `freqs` and `power_spectrum` passed in in this call. |
401 | 407 | **plot_kwargs |
402 | 408 | Keyword arguments to pass into the plot method. |
403 | | - Plot options with a name conflict be passed by pre-pending 'plot_'. |
| 409 | + Plot options with a name conflict be passed by pre-pending `plot_`. |
404 | 410 | e.g. `freqs`, `power_spectrum` and `freq_range`. |
405 | 411 |
|
406 | 412 | Notes |
@@ -921,7 +927,9 @@ def _simple_ap_fit(self, freqs, power_spectrum): |
921 | 927 | warnings.simplefilter("ignore") |
922 | 928 | aperiodic_params, _ = curve_fit(get_ap_func(self.aperiodic_mode), |
923 | 929 | freqs, power_spectrum, p0=guess, |
924 | | - maxfev=self._maxfev, bounds=ap_bounds) |
| 930 | + maxfev=self._maxfev, bounds=ap_bounds, |
| 931 | + ftol=self._tol, xtol=self._tol, gtol=self._tol, |
| 932 | + check_finite=False) |
925 | 933 | except RuntimeError as excp: |
926 | 934 | error_msg = ("Model fitting failed due to not finding parameters in " |
927 | 935 | "the simple aperiodic component fit.") |
@@ -978,7 +986,9 @@ def _robust_ap_fit(self, freqs, power_spectrum): |
978 | 986 | warnings.simplefilter("ignore") |
979 | 987 | aperiodic_params, _ = curve_fit(get_ap_func(self.aperiodic_mode), |
980 | 988 | freqs_ignore, spectrum_ignore, p0=popt, |
981 | | - maxfev=self._maxfev, bounds=ap_bounds) |
| 989 | + maxfev=self._maxfev, bounds=ap_bounds, |
| 990 | + ftol=self._tol, xtol=self._tol, gtol=self._tol, |
| 991 | + check_finite=False) |
982 | 992 | except RuntimeError as excp: |
983 | 993 | error_msg = ("Model fitting failed due to not finding " |
984 | 994 | "parameters in the robust aperiodic fit.") |
@@ -1124,7 +1134,9 @@ def _fit_peak_guess(self, guess): |
1124 | 1134 | # Fit the peaks |
1125 | 1135 | try: |
1126 | 1136 | gaussian_params, _ = curve_fit(gaussian_function, self.freqs, self._spectrum_flat, |
1127 | | - p0=guess, maxfev=self._maxfev, bounds=gaus_param_bounds) |
| 1137 | + p0=guess, maxfev=self._maxfev, bounds=gaus_param_bounds, |
| 1138 | + ftol=self._tol, xtol=self._tol, gtol=self._tol, |
| 1139 | + check_finite=False, jac=jacobian_gauss) |
1128 | 1140 | except RuntimeError as excp: |
1129 | 1141 | error_msg = ("Model fitting failed due to not finding " |
1130 | 1142 | "parameters in the peak component fit.") |
|
0 commit comments