Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion fooof/objs/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ def __init__(self, peak_width_limits=(0.5, 12.0), max_n_peaks=np.inf, min_peak_h
## RUN MODES
# Set default debug mode - controls if an error is raised if model fitting is unsuccessful
self._debug = False
# Set default check data mode - controls if an error is raised if NaN / Inf data are added
# Set default data checking modes - controls which checks get run on input data
# check_freqs: check the frequency values, and raises an error for uneven spacing
self._check_freqs = True
# check_data: checks the power values and raises an error for any NaN / Inf values
self._check_data = True

# Set internal settings, based on inputs, and initialize data & results attributes
Expand Down Expand Up @@ -1221,6 +1224,14 @@ def _prepare_data(self, freqs, power_spectrum, freq_range, spectra_dim=1):
# Log power values
power_spectrum = np.log10(power_spectrum)

## Data checks - run checks on inputs based on check modes

if self._check_freqs:
# Check if the frequency data is unevenly spaced, and raise an error if so
freq_diffs = np.diff(freqs)
if not np.all(np.isclose(freq_diffs, freq_res)):
raise DataError("The input frequency values are not evenly spaced. "
"The model expects equidistant frequency values in linear space.")
if self._check_data:
# Check if there are any infs / nans, and raise an error if so
if np.any(np.isinf(power_spectrum)) or np.any(np.isnan(power_spectrum)):
Expand Down
6 changes: 5 additions & 1 deletion fooof/tests/objs/test_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ def test_fooof_checks():
tfm.fit(xs, ys)
assert tfm.freqs[0] != 0

# Check error if there is a post-logging inf or nan
# Check error for `check_freqs` - for if there is non-even frequency values
with raises(DataError):
tfm.fit(np.array([1, 2, 4]), np.array([1, 2, 3]))

# Check error for `check_data` - for if there is a post-logging inf or nan
with raises(DataError): # Double log (1) -> -inf
tfm.fit(np.array([1, 2, 3]), np.log10(np.array([1, 2, 3])))
with raises(DataError): # Log (-1) -> NaN
Expand Down