Skip to content

Commit bc415a9

Browse files
authored
Merge pull request #269 from fooof-tools/checkf
[MNT] - Add `check_freqs` to check for uneven frequency samples
2 parents 9fd6893 + 04a9a3c commit bc415a9

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

fooof/objs/fit.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ def __init__(self, peak_width_limits=(0.5, 12.0), max_n_peaks=np.inf, min_peak_h
198198
## RUN MODES
199199
# Set default debug mode - controls if an error is raised if model fitting is unsuccessful
200200
self._debug = False
201-
# Set default check data mode - controls if an error is raised if NaN / Inf data are added
201+
# Set default data checking modes - controls which checks get run on input data
202+
# check_freqs: check the frequency values, and raises an error for uneven spacing
203+
self._check_freqs = True
204+
# check_data: checks the power values and raises an error for any NaN / Inf values
202205
self._check_data = True
203206

204207
# Set internal settings, based on inputs, and initialize data & results attributes
@@ -1241,6 +1244,14 @@ def _prepare_data(self, freqs, power_spectrum, freq_range, spectra_dim=1):
12411244
# Log power values
12421245
power_spectrum = np.log10(power_spectrum)
12431246

1247+
## Data checks - run checks on inputs based on check modes
1248+
1249+
if self._check_freqs:
1250+
# Check if the frequency data is unevenly spaced, and raise an error if so
1251+
freq_diffs = np.diff(freqs)
1252+
if not np.all(np.isclose(freq_diffs, freq_res)):
1253+
raise DataError("The input frequency values are not evenly spaced. "
1254+
"The model expects equidistant frequency values in linear space.")
12441255
if self._check_data:
12451256
# Check if there are any infs / nans, and raise an error if so
12461257
if np.any(np.isinf(power_spectrum)) or np.any(np.isnan(power_spectrum)):

fooof/tests/objs/test_fit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ def test_fooof_checks():
164164
tfm.fit(xs, ys)
165165
assert tfm.freqs[0] != 0
166166

167-
# Check error if there is a post-logging inf or nan
167+
# Check error for `check_freqs` - for if there is non-even frequency values
168+
with raises(DataError):
169+
tfm.fit(np.array([1, 2, 4]), np.array([1, 2, 3]))
170+
171+
# Check error for `check_data` - for if there is a post-logging inf or nan
168172
with raises(DataError): # Double log (1) -> -inf
169173
tfm.fit(np.array([1, 2, 3]), np.log10(np.array([1, 2, 3])))
170174
with raises(DataError): # Log (-1) -> NaN

0 commit comments

Comments
 (0)