Skip to content

Commit 04a9a3c

Browse files
committed
add check_freqs to check for uneven frequency samples
1 parent 208fc09 commit 04a9a3c

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
@@ -197,7 +197,10 @@ def __init__(self, peak_width_limits=(0.5, 12.0), max_n_peaks=np.inf, min_peak_h
197197
## RUN MODES
198198
# Set default debug mode - controls if an error is raised if model fitting is unsuccessful
199199
self._debug = False
200-
# Set default check data mode - controls if an error is raised if NaN / Inf data are added
200+
# Set default data checking modes - controls which checks get run on input data
201+
# check_freqs: check the frequency values, and raises an error for uneven spacing
202+
self._check_freqs = True
203+
# check_data: checks the power values and raises an error for any NaN / Inf values
201204
self._check_data = True
202205

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

1227+
## Data checks - run checks on inputs based on check modes
1228+
1229+
if self._check_freqs:
1230+
# Check if the frequency data is unevenly spaced, and raise an error if so
1231+
freq_diffs = np.diff(freqs)
1232+
if not np.all(np.isclose(freq_diffs, freq_res)):
1233+
raise DataError("The input frequency values are not evenly spaced. "
1234+
"The model expects equidistant frequency values in linear space.")
12241235
if self._check_data:
12251236
# Check if there are any infs / nans, and raise an error if so
12261237
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
@@ -161,7 +161,11 @@ def test_fooof_checks():
161161
tfm.fit(xs, ys)
162162
assert tfm.freqs[0] != 0
163163

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

0 commit comments

Comments
 (0)