Skip to content

Commit 39021e7

Browse files
authored
Merge pull request #270 from fooof-tools/lints
[MNT] - Minor updates from linting
2 parents 59b48aa + 79f324d commit 39021e7

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

fooof/objs/fit.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def _reset_internal_settings(self):
252252

253253
# Bandwidth limits are given in 2-sided peak bandwidth
254254
# Convert to gaussian std parameter limits
255-
self._gauss_std_limits = tuple([bwl / 2 for bwl in self.peak_width_limits])
255+
self._gauss_std_limits = tuple(bwl / 2 for bwl in self.peak_width_limits)
256256

257257
# Otherwise, assume settings are unknown (have been cleared) and set to None
258258
else:
@@ -378,7 +378,8 @@ def add_results(self, fooof_result):
378378
self._check_loaded_results(fooof_result._asdict())
379379

380380

381-
def report(self, freqs=None, power_spectrum=None, freq_range=None, plt_log=False, **plot_kwargs):
381+
def report(self, freqs=None, power_spectrum=None, freq_range=None,
382+
plt_log=False, **plot_kwargs):
382383
"""Run model fit, and display a report, which includes a plot, and printed results.
383384
384385
Parameters
@@ -791,9 +792,10 @@ def _simple_ap_fit(self, freqs, power_spectrum):
791792
aperiodic_params, _ = curve_fit(get_ap_func(self.aperiodic_mode),
792793
freqs, power_spectrum, p0=guess,
793794
maxfev=self._maxfev, bounds=ap_bounds)
794-
except RuntimeError:
795-
raise FitError("Model fitting failed due to not finding parameters in "
796-
"the simple aperiodic component fit.")
795+
except RuntimeError as excp:
796+
error_msg = ("Model fitting failed due to not finding parameters in "
797+
"the simple aperiodic component fit.")
798+
raise FitError(error_msg) from excp
797799

798800
return aperiodic_params
799801

@@ -847,12 +849,14 @@ def _robust_ap_fit(self, freqs, power_spectrum):
847849
aperiodic_params, _ = curve_fit(get_ap_func(self.aperiodic_mode),
848850
freqs_ignore, spectrum_ignore, p0=popt,
849851
maxfev=self._maxfev, bounds=ap_bounds)
850-
except RuntimeError:
851-
raise FitError("Model fitting failed due to not finding "
852-
"parameters in the robust aperiodic fit.")
853-
except TypeError:
854-
raise FitError("Model fitting failed due to sub-sampling "
855-
"in the robust aperiodic fit.")
852+
except RuntimeError as excp:
853+
error_msg = ("Model fitting failed due to not finding "
854+
"parameters in the robust aperiodic fit.")
855+
raise FitError(error_msg) from excp
856+
except TypeError as excp:
857+
error_msg = ("Model fitting failed due to sub-sampling "
858+
"in the robust aperiodic fit.")
859+
raise FitError(error_msg) from excp
856860

857861
return aperiodic_params
858862

@@ -981,8 +985,8 @@ def _fit_peak_guess(self, guess):
981985

982986
# Unpacks the embedded lists into flat tuples
983987
# This is what the fit function requires as input
984-
gaus_param_bounds = (tuple([item for sublist in lo_bound for item in sublist]),
985-
tuple([item for sublist in hi_bound for item in sublist]))
988+
gaus_param_bounds = (tuple(item for sublist in lo_bound for item in sublist),
989+
tuple(item for sublist in hi_bound for item in sublist))
986990

987991
# Flatten guess, for use with curve fit
988992
guess = np.ndarray.flatten(guess)
@@ -991,13 +995,15 @@ def _fit_peak_guess(self, guess):
991995
try:
992996
gaussian_params, _ = curve_fit(gaussian_function, self.freqs, self._spectrum_flat,
993997
p0=guess, maxfev=self._maxfev, bounds=gaus_param_bounds)
994-
except RuntimeError:
995-
raise FitError("Model fitting failed due to not finding "
996-
"parameters in the peak component fit.")
997-
except LinAlgError:
998-
raise FitError("Model fitting failed due to a LinAlgError during peak fitting. "
999-
"This can happen with settings that are too liberal, leading, "
1000-
"to a large number of guess peaks that cannot be fit together.")
998+
except RuntimeError as excp:
999+
error_msg = ("Model fitting failed due to not finding "
1000+
"parameters in the peak component fit.")
1001+
raise FitError(error_msg) from excp
1002+
except LinAlgError as excp:
1003+
error_msg = ("Model fitting failed due to a LinAlgError during peak fitting. "
1004+
"This can happen with settings that are too liberal, leading, "
1005+
"to a large number of guess peaks that cannot be fit together.")
1006+
raise FitError(error_msg) from excp
10011007

10021008
# Re-organize params into 2d matrix
10031009
gaussian_params = np.array(group_three(gaussian_params))
@@ -1164,8 +1170,8 @@ def _calc_error(self, metric=None):
11641170
self.error_ = np.sqrt(((self.power_spectrum - self.fooofed_spectrum_) ** 2).mean())
11651171

11661172
else:
1167-
msg = "Error metric '{}' not understood or not implemented.".format(metric)
1168-
raise ValueError(msg)
1173+
error_msg = "Error metric '{}' not understood or not implemented.".format(metric)
1174+
raise ValueError(error_msg)
11691175

11701176

11711177
def _prepare_data(self, freqs, power_spectrum, freq_range, spectra_dim=1):
@@ -1258,10 +1264,11 @@ def _prepare_data(self, freqs, power_spectrum, freq_range, spectra_dim=1):
12581264
if self._check_data:
12591265
# Check if there are any infs / nans, and raise an error if so
12601266
if np.any(np.isinf(power_spectrum)) or np.any(np.isnan(power_spectrum)):
1261-
raise DataError("The input power spectra data, after logging, contains NaNs or Infs. "
1262-
"This will cause the fitting to fail. "
1263-
"One reason this can happen is if inputs are already logged. "
1264-
"Inputs data should be in linear spacing, not log.")
1267+
error_msg = ("The input power spectra data, after logging, contains NaNs or Infs. "
1268+
"This will cause the fitting to fail. "
1269+
"One reason this can happen is if inputs are already logged. "
1270+
"Inputs data should be in linear spacing, not log.")
1271+
raise DataError(error_msg)
12651272

12661273
return freqs, power_spectrum, freq_range, freq_res
12671274

fooof/objs/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def combine_fooofs(fooofs):
179179
fg.power_spectra = temp_power_spectra
180180

181181
# Set the check data mode, as True if any of the inputs have it on, False otherwise
182-
fg.set_check_data_mode(any([getattr(f_obj, '_check_data') for f_obj in fooofs]))
182+
fg.set_check_data_mode(any(getattr(f_obj, '_check_data') for f_obj in fooofs))
183183

184184
# Add data information information
185185
fg.add_meta_data(fooofs[0].get_meta_data())

0 commit comments

Comments
 (0)