Skip to content

Commit b151e17

Browse files
committed
add tests for check_data mode & associated fixes
1 parent f3d1ccb commit b151e17

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

fooof/objs/fit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def fit(self, freqs=None, power_spectrum=None, freq_range=None):
445445
# This serves as a catch all for curve_fits which will fail given NaN or Inf
446446
# Because FitError's are by default caught, this allows fitting to continue
447447
if not self._check_data:
448-
if not np.any(np.isinf(power_spectrum)) or np.any(np.isnan(power_spectrum)):
448+
if np.any(np.isinf(self.power_spectrum)) or np.any(np.isnan(self.power_spectrum)):
449449
raise FitError("There are NaN or Inf values in the data, "
450450
"which preclude model fitting.")
451451

@@ -703,7 +703,7 @@ def set_debug_mode(self, debug):
703703
self._debug = debug
704704

705705

706-
def set_check_data(self, check_data):
706+
def set_check_data_mode(self, check_data):
707707
"""Set check data mode, which controls if an error is raised if NaN or Inf data are added.
708708
709709
Parameters

fooof/tests/objs/test_fit.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from fooof.core.items import OBJ_DESC
1313
from fooof.core.errors import FitError
1414
from fooof.core.utils import group_three
15-
from fooof.sim import gen_power_spectrum
15+
from fooof.sim import gen_freqs, gen_power_spectrum
1616
from fooof.data import FOOOFSettings, FOOOFMetaData, FOOOFResults
1717
from fooof.core.errors import DataError, NoDataError, InconsistentDataError
1818

@@ -364,7 +364,7 @@ def raise_runtime_error(*args, **kwargs):
364364
assert np.all(np.isnan(getattr(tfm, result)))
365365

366366
def test_fooof_debug():
367-
"""Test FOOOF fit failure in debug mode."""
367+
"""Test FOOOF in debug mode, including with fit failures."""
368368

369369
tfm = FOOOF(verbose=False)
370370
tfm._maxfev = 5
@@ -374,3 +374,22 @@ def test_fooof_debug():
374374

375375
with raises(FitError):
376376
tfm.fit(*gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2, 20, 0.3, 4]))
377+
378+
def test_fooof_check_data():
379+
"""Test FOOOF in with check data mode turned off, including with NaN data."""
380+
381+
tfm = FOOOF(verbose=False)
382+
383+
tfm.set_check_data_mode(False)
384+
assert tfm._check_data is False
385+
386+
# Add data, with check data turned off
387+
# In check data mode, adding data with NaN should run
388+
freqs = gen_freqs([3, 50], 0.5)
389+
powers = np.ones_like(freqs) * np.nan
390+
tfm.add_data(freqs, powers)
391+
assert tfm.has_data
392+
393+
# Model fitting should execute, but return a null model fit, given the NaNs, without failing
394+
tfm.fit()
395+
assert not fm.has_model

0 commit comments

Comments
 (0)