Skip to content

Commit

Permalink
MRG: Fix report BEM (mne-tools#5659)
Browse files Browse the repository at this point in the history
* FIX: Fix report BEM

* FIX: Add test for mpl nesting
  • Loading branch information
larsoner authored and agramfort committed Oct 25, 2018
1 parent bf7eba6 commit e6ce2be
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ docstring:
check-manifest:
check-manifest --ignore .circleci*,doc,logo,mne/io/*/tests/data*,mne/io/tests/data,mne/preprocessing/tests/data,.DS_Store

nesting:
@echo "Running import nesting tests"
@$(PYTESTS) mne/tests/test_import_nesting.py

pep:
@$(MAKE) -k flake pydocstyle docstring codespell-error check-manifest
@$(MAKE) -k flake pydocstyle docstring codespell-error check-manifest nesting

manpages:
@echo "I: generating manpages"
Expand Down
2 changes: 1 addition & 1 deletion mne/realtime/tests/test_fieldtrip_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from mne.realtime import FieldTripClient, RtEpochs
from mne.externals.six.moves import queue

from .test_mockclient import _call_base_epochs_public_api
from mne.realtime.tests.test_mockclient import _call_base_epochs_public_api

# Set our plotters to test mode
import matplotlib
Expand Down
7 changes: 5 additions & 2 deletions mne/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
from .viz.raw import _data_types

from .externals.tempita import HTMLTemplate, Template
from .externals.six import BytesIO
from .externals.six import moves
from .externals.six import BytesIO, moves, string_types
from .externals.h5io import read_hdf5, write_hdf5

VALID_EXTENSIONS = ['raw.fif', 'raw.fif.gz', 'sss.fif', 'sss.fif.gz',
Expand Down Expand Up @@ -1021,6 +1020,7 @@ def _add_or_replace(self, fname, sectionlabel, html, replace=False):
Existing figures are only replaced if this is set to ``True``.
Defaults to ``False``.
"""
assert isinstance(html, string_types) # otherwise later will break
if replace and fname in self.fnames:
# Find last occurrence of the figure
ind = max([i for i, existing in enumerate(self.fnames)
Expand Down Expand Up @@ -1220,6 +1220,9 @@ def add_bem_to_section(self, subject, caption='BEM', section='bem',
caption=caption)
html, caption, _ = self._validate_input(html, caption, section)
sectionvar = self._sectionvars[section]
# convert list->str
assert isinstance(html, list)
html = u''.join(html)
self._add_or_replace('%s-#-%s-#-custom' % (caption[0], sectionvar),
sectionvar, html)

Expand Down
40 changes: 38 additions & 2 deletions mne/tests/test_import_nesting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path as op
import sys
from subprocess import Popen, PIPE

Expand Down Expand Up @@ -50,8 +51,43 @@ def test_module_nesting():
proc = Popen([sys.executable, '-c', run_script], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
stdout = stdout.decode('utf-8')
if proc.returncode:
raise AssertionError(stdout)
stderr = stderr.decode('utf-8')
assert not proc.returncode, stdout + stderr


mpl_script = """
import os
import os.path as op
import re
import sys
import mne
reg = re.compile('test_.*.py')
for dirpath, _, filenames in os.walk('{0}'):
if dirpath.endswith('tests'):
test_dir = op.join('{0}', dirpath)
sys.path.insert(0, test_dir)
for filename in filenames:
if reg.match(filename) is not None:
__import__(op.splitext(filename)[0])
for x in sys.modules.keys():
if x.startswith('matplotlib.pyplot'):
print('\\nFound un-nested pyplot import: ' +
op.join(test_dir, filename))
exit(1)
sys.path.pop(0)
"""


def test_mpl_nesting():
"""Test that matplotlib imports are properly nested in tests."""
mne_path = op.abspath(op.join(op.dirname(__file__), '..'))
proc = Popen([sys.executable, '-c', mpl_script.format(mne_path)],
stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
stdout = stdout.decode('utf-8')
stderr = stderr.decode('utf-8')
assert not proc.returncode, stdout + stderr


run_tests_if_main()
20 changes: 15 additions & 5 deletions mne/tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from mne.viz import plot_alignment

import matplotlib
from matplotlib import pyplot as plt
matplotlib.use('Agg') # for testing don't use X server

data_dir = testing.data_path(download=False)
Expand All @@ -43,9 +42,13 @@
'data'))
evoked_fname = op.join(base_dir, 'test-ave.fif')

# Two example figures
fig1 = plt.plot([1, 2], [1, 2])[0].figure
fig2 = plt.plot([3, 4], [3, 4])[0].figure

def _get_example_figures():
"""Create two example figures."""
from matplotlib import pyplot as plt
fig1 = plt.plot([1, 2], [1, 2])[0].figure
fig2 = plt.plot([3, 4], [3, 4])[0].figure
return [fig1, fig2]


@pytest.mark.slowtest
Expand Down Expand Up @@ -233,6 +236,10 @@ def test_render_mri():
report.parse_folder(data_path=tempdir, mri_decim=30, pattern='*')
report.save(op.join(tempdir, 'report.html'), open_browser=False)
assert repr(report)
report.add_bem_to_section('sample', caption='extra', section='foo',
subjects_dir=subjects_dir, decim=30)
report.save(op.join(tempdir, 'report.html'), open_browser=False,
overwrite=True)


@testing.requires_testing_data
Expand Down Expand Up @@ -271,7 +278,7 @@ def test_add_slider_to_section():
report = Report(info_fname=raw_fname,
subject='sample', subjects_dir=subjects_dir)
section = 'slider_section'
figs = [fig1, fig2]
figs = _get_example_figures()
report.add_slider_to_section(figs, section=section)
report.save(op.join(tempdir, 'report.html'), open_browser=False)

Expand Down Expand Up @@ -314,6 +321,7 @@ def test_open_report():
hdf5 = op.join(tempdir, 'report.h5')

# Test creating a new report through the open_report function
fig1 = _get_example_figures()[0]
with open_report(hdf5, subjects_dir=subjects_dir) as report:
assert report.subjects_dir == subjects_dir
assert report._fname == hdf5
Expand All @@ -338,6 +346,7 @@ def test_open_report():
def test_remove():
"""Test removing figures from a report."""
r = Report()
fig1, fig2 = _get_example_figures()
r.add_figs_to_section(fig1, 'figure1', 'mysection')
r.add_figs_to_section(fig1, 'figure1', 'othersection')
r.add_figs_to_section(fig2, 'figure1', 'mysection')
Expand Down Expand Up @@ -371,6 +380,7 @@ def test_remove():
def test_add_or_replace():
"""Test replacing existing figures in a report."""
r = Report()
fig1, fig2 = _get_example_figures()
r.add_figs_to_section(fig1, 'duplicate', 'mysection')
r.add_figs_to_section(fig1, 'duplicate', 'mysection')
r.add_figs_to_section(fig1, 'duplicate', 'othersection')
Expand Down

0 comments on commit e6ce2be

Please sign in to comment.