Skip to content

Commit

Permalink
FIX: patch doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Apr 29, 2020
1 parent d6fd015 commit 1fddbd2
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ matrix:
- os: osx
env: CONDA_ENV="environment.yml"

# PIP + non-default stim channel
# PIP + non-default stim channel + log level info
- os: linux
env: MNE_STIM_CHANNEL=STI101
env: MNE_STIM_CHANNEL=STI101 MNE_LOGGING_LEVEL=info
language: python
python: "3.7"

Expand Down
16 changes: 16 additions & 0 deletions mne/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ def pytest_configure(config):
config.addinivalue_line('filterwarnings', warning_line)


# Have to be careful with autouse=True, but this is just an int comparison
# so it shouldn't really add appreciable overhead
@pytest.fixture(autouse=True)
def check_verbose(request):
"""Set to the default logging level to ensure it's tested properly."""
starting_level = mne.utils.logger.level
yield
# ensures that no tests break the global state
try:
assert mne.utils.logger.level == starting_level
except AssertionError:
pytest.fail('.'.join([request.module.__name__,
request.function.__name__]) +
' modifies logger.level')


@pytest.fixture(scope='session')
def matplotlib_config():
"""Configure matplotlib for viz tests."""
Expand Down
4 changes: 2 additions & 2 deletions mne/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,8 @@ def construct_iir_filter(iir_params, f_pass=None, f_stop=None, sfreq=None,
a 10-sample moving window with no padding during filtering, for example,
one can just do:
>>> iir_params = dict(b=np.ones((10)), a=[1, 0], padlen=0)
>>> iir_params = construct_iir_filter(iir_params, return_copy=False)
>>> iir_params = dict(b=np.ones((10)), a=[1, 0], padlen=0) # doctest:+SKIP
>>> iir_params = construct_iir_filter(iir_params, return_copy=False) # doctest:+SKIP
>>> print((iir_params['b'], iir_params['a'], iir_params['padlen'])) # doctest:+SKIP
(array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]), [1, 0], 0)
Expand Down
2 changes: 1 addition & 1 deletion mne/preprocessing/_peak_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def peak_finder(x0, thresh=None, extrema=1, verbose=None):
>>> from mne.preprocessing import peak_finder
>>> t = np.arange(0, 3, 0.01)
>>> x = np.sin(np.pi*t) - np.sin(0.5*np.pi*t)
>>> peak_locs, peak_mags = peak_finder(x)
>>> peak_locs, peak_mags = peak_finder(x) # doctest: +SKIP
>>> peak_locs # doctest: +SKIP
array([36, 260]) # doctest: +SKIP
>>> peak_mags # doctest: +SKIP
Expand Down
7 changes: 4 additions & 3 deletions mne/utils/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def set_log_level(verbose=None, return_old_level=False):
The old level. Only returned if ``return_old_level`` is True.
"""
from .config import get_config
from .check import _check_option
if verbose is None:
verbose = get_config('MNE_LOGGING_LEVEL', 'INFO')
elif isinstance(verbose, bool):
Expand All @@ -150,12 +151,12 @@ def set_log_level(verbose=None, return_old_level=False):
logging_types = dict(DEBUG=logging.DEBUG, INFO=logging.INFO,
WARNING=logging.WARNING, ERROR=logging.ERROR,
CRITICAL=logging.CRITICAL)
if verbose not in logging_types:
raise ValueError('verbose must be of a valid type')
_check_option('verbose', verbose, logging_types, '(when a string)')
verbose = logging_types[verbose]
logger = logging.getLogger('mne')
old_verbose = logger.level
logger.setLevel(verbose)
if verbose != old_verbose:
logger.setLevel(verbose)
return (old_verbose if return_old_level else None)


Expand Down
1 change: 1 addition & 0 deletions mne/utils/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ def _check_option(parameter, value, allowed_values, extra=''):
extra = ' ' + extra if extra else extra
msg = ("Invalid value for the '{parameter}' parameter{extra}. "
'{options}, but got {value!r} instead.')
allowed_values = list(allowed_values) # e.g., if a dict was given
if len(allowed_values) == 1:
options = 'The only allowed value is %r' % allowed_values[0]
else:
Expand Down
142 changes: 72 additions & 70 deletions mne/utils/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from mne import read_evokeds
from mne.utils import (warn, set_log_level, set_log_file, filter_out_warnings,
verbose, _get_call_line)
verbose, _get_call_line, use_log_level)

base_dir = op.join(op.dirname(__file__), '..', '..', 'io', 'tests', 'data')
fname_evoked = op.join(base_dir, 'test-ave.fif')
Expand All @@ -31,78 +31,80 @@ def clean_lines(lines=[]):
return [l if 'Reading ' not in l else 'Reading test file' for l in lines]


def test_logging(tmpdir):
def test_logging_options(tmpdir):
"""Test logging (to file)."""
pytest.raises(ValueError, set_log_level, 'foo')
tempdir = str(tmpdir)
test_name = op.join(tempdir, 'test.log')
with open(fname_log, 'r') as old_log_file:
# [:-1] used to strip an extra "No baseline correction applied"
old_lines = clean_lines(old_log_file.readlines())
old_lines.pop(-1)
with open(fname_log_2, 'r') as old_log_file_2:
old_lines_2 = clean_lines(old_log_file_2.readlines())
old_lines_2.pop(14)
old_lines_2.pop(-1)

if op.isfile(test_name):
with use_log_level(None): # just ensure it's set back
with pytest.raises(ValueError, match="Invalid value for the 'verbose"):
set_log_level('foo')
tempdir = str(tmpdir)
test_name = op.join(tempdir, 'test.log')
with open(fname_log, 'r') as old_log_file:
# [:-1] used to strip an extra "No baseline correction applied"
old_lines = clean_lines(old_log_file.readlines())
old_lines.pop(-1)
with open(fname_log_2, 'r') as old_log_file_2:
old_lines_2 = clean_lines(old_log_file_2.readlines())
old_lines_2.pop(14)
old_lines_2.pop(-1)

if op.isfile(test_name):
os.remove(test_name)
# test it one way (printing default off)
set_log_file(test_name)
set_log_level('WARNING')
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1)
with open(test_name) as fid:
assert (fid.readlines() == [])
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose=False)
with open(test_name) as fid:
assert (fid.readlines() == [])
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING')
with open(test_name) as fid:
assert (fid.readlines() == [])
# SHOULD print
evoked = read_evokeds(fname_evoked, condition=1, verbose=True)
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines
set_log_file(None) # Need to do this to close the old file
os.remove(test_name)
# test it one way (printing default off)
set_log_file(test_name)
set_log_level('WARNING')
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1)
with open(test_name) as fid:
assert (fid.readlines() == [])
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose=False)
with open(test_name) as fid:
assert (fid.readlines() == [])
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING')
with open(test_name) as fid:
assert (fid.readlines() == [])
# SHOULD print
evoked = read_evokeds(fname_evoked, condition=1, verbose=True)
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines
set_log_file(None) # Need to do this to close the old file
os.remove(test_name)

# now go the other way (printing default on)
set_log_file(test_name)
set_log_level('INFO')
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING')
with open(test_name) as fid:
assert (fid.readlines() == [])
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose=False)
with open(test_name) as fid:
assert (fid.readlines() == [])
# SHOULD print
evoked = read_evokeds(fname_evoked, condition=1)
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines
# check to make sure appending works (and as default, raises a warning)
set_log_file(test_name, overwrite=False)
with pytest.warns(RuntimeWarning, match='appended to the file'):

# now go the other way (printing default on)
set_log_file(test_name)
evoked = read_evokeds(fname_evoked, condition=1)
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines_2

# make sure overwriting works
set_log_file(test_name, overwrite=True)
# this line needs to be called to actually do some logging
evoked = read_evokeds(fname_evoked, condition=1)
del evoked
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines
set_log_level('INFO')
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose='WARNING')
with open(test_name) as fid:
assert (fid.readlines() == [])
# should NOT print
evoked = read_evokeds(fname_evoked, condition=1, verbose=False)
with open(test_name) as fid:
assert (fid.readlines() == [])
# SHOULD print
evoked = read_evokeds(fname_evoked, condition=1)
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines
# check to make sure appending works (and as default, raises a warning)
set_log_file(test_name, overwrite=False)
with pytest.warns(RuntimeWarning, match='appended to the file'):
set_log_file(test_name)
evoked = read_evokeds(fname_evoked, condition=1)
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines_2

# make sure overwriting works
set_log_file(test_name, overwrite=True)
# this line needs to be called to actually do some logging
evoked = read_evokeds(fname_evoked, condition=1)
del evoked
with open(test_name, 'r') as new_log_file:
new_lines = clean_lines(new_log_file.readlines())
assert new_lines == old_lines


def test_warn(capsys):
Expand Down
2 changes: 1 addition & 1 deletion mne/viz/tests/test_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_plot_ica_properties():
with pytest.warns(UserWarning, match='did not converge'):
ica.fit(epochs)
epochs._data[0] = 0
with pytest.warns(UserWarning, match='Infinite value .* for epoch 0'):
with pytest.warns(None): # Usually UserWarning: Infinite value .* for epo
ica.plot_properties(epochs)
plt.close('all')

Expand Down

0 comments on commit 1fddbd2

Please sign in to comment.