Skip to content

Commit e89aa35

Browse files
committed
add option to tweak curve_fit tolerance and set new default
1 parent a1e1479 commit e89aa35

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

fooof/objs/fit.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,16 @@ def __init__(self, peak_width_limits=(0.5, 12.0), max_n_peaks=np.inf, min_peak_h
192192
self._gauss_overlap_thresh = 0.75
193193
# Parameter bounds for center frequency when fitting gaussians, in terms of +/- std dev
194194
self._cf_bound = 1.5
195-
# The maximum number of calls to the curve fitting function
196-
self._maxfev = 5000
197195
# The error metric to calculate, post model fitting. See `_calc_error` for options
198196
# Note: this is for checking error post fitting, not an objective function for fitting
199197
self._error_metric = 'MAE'
200198

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+
self._tol = 0.00001
204+
201205
## RUN MODES
202206
# Set default debug mode - controls if an error is raised if model fitting is unsuccessful
203207
self._debug = False
@@ -947,6 +951,7 @@ def _simple_ap_fit(self, freqs, power_spectrum):
947951
aperiodic_params, _ = curve_fit(get_ap_func(self.aperiodic_mode),
948952
freqs, power_spectrum, p0=guess,
949953
maxfev=self._maxfev, bounds=ap_bounds,
954+
ftol=self._tol, xtol=self._tol, gtol=self._tol,
950955
check_finite=False)
951956
except RuntimeError as excp:
952957
error_msg = ("Model fitting failed due to not finding parameters in "
@@ -1005,6 +1010,7 @@ def _robust_ap_fit(self, freqs, power_spectrum):
10051010
aperiodic_params, _ = curve_fit(get_ap_func(self.aperiodic_mode),
10061011
freqs_ignore, spectrum_ignore, p0=popt,
10071012
maxfev=self._maxfev, bounds=ap_bounds,
1013+
ftol=self._tol, xtol=self._tol, gtol=self._tol,
10081014
check_finite=False)
10091015
except RuntimeError as excp:
10101016
error_msg = ("Model fitting failed due to not finding "
@@ -1152,6 +1158,7 @@ def _fit_peak_guess(self, guess):
11521158
try:
11531159
gaussian_params, _ = curve_fit(gaussian_function, self.freqs, self._spectrum_flat,
11541160
p0=guess, maxfev=self._maxfev, bounds=gaus_param_bounds,
1161+
ftol=self._tol, xtol=self._tol, gtol=self._tol,
11551162
check_finite=False)
11561163
except RuntimeError as excp:
11571164
error_msg = ("Model fitting failed due to not finding "

fooof/tests/objs/test_fit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def test_fooof_fit_failure():
391391

392392
## Induce a runtime error, and check it runs through
393393
tfm = FOOOF(verbose=False)
394-
tfm._maxfev = 5
394+
tfm._maxfev = 2
395395

396396
tfm.fit(*gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2, 20, 0.3, 4]))
397397

@@ -417,7 +417,7 @@ def test_fooof_debug():
417417
"""Test FOOOF in debug mode, including with fit failures."""
418418

419419
tfm = FOOOF(verbose=False)
420-
tfm._maxfev = 5
420+
tfm._maxfev = 2
421421

422422
tfm.set_debug_mode(True)
423423
assert tfm._debug is True

0 commit comments

Comments
 (0)