def test_render_add_sections(): """Test adding figures/images to section. """ tempdir = _TempDir() import matplotlib.pyplot as plt report = Report(subjects_dir=subjects_dir) # Check add_figs_to_section functionality fig = plt.plot([1, 2], [1, 2])[0].figure report.add_figs_to_section(figs=fig, # test non-list input captions=['evoked response'], scale=1.2, image_format='svg') assert_raises(ValueError, report.add_figs_to_section, figs=[fig, fig], captions='H') assert_raises(ValueError, report.add_figs_to_section, figs=fig, captions=['foo'], scale=0, image_format='svg') assert_raises(ValueError, report.add_figs_to_section, figs=fig, captions=['foo'], scale=1e-10, image_format='svg') # need to recreate because calls above change size fig = plt.plot([1, 2], [1, 2])[0].figure # Check add_images_to_section img_fname = op.join(tempdir, 'testimage.png') fig.savefig(img_fname) report.add_images_to_section(fnames=[img_fname], captions=['evoked response']) assert_raises(ValueError, report.add_images_to_section, fnames=[img_fname, img_fname], captions='H') evoked = read_evokeds(evoked_fname, condition='Left Auditory', baseline=(-0.2, 0.0)) fig = plot_trans(evoked.info, trans_fname, subject='sample', subjects_dir=subjects_dir) report.add_figs_to_section(figs=fig, # test non-list input captions='random image', scale=1.2)
def test_render_add_sections(): """Test adding figures/images to section. """ tempdir = _TempDir() import matplotlib.pyplot as plt report = Report(subjects_dir=subjects_dir) # Check add_figs_to_section functionality fig = plt.plot([1, 2], [1, 2])[0].figure report.add_figs_to_section(figs=fig, # test non-list input captions=['evoked response'], scale=1.2, image_format='svg') assert_raises(ValueError, report.add_figs_to_section, figs=[fig, fig], captions='H') # Check add_images_to_section img_fname = op.join(tempdir, 'testimage.png') fig.savefig(img_fname) report.add_images_to_section(fnames=[img_fname], captions=['evoked response']) assert_raises(ValueError, report.add_images_to_section, fnames=[img_fname, img_fname], captions='H') # Check deprecation of add_section with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') report.add_section(figs=fig, captions=['evoked response']) assert_true(w[0].category == DeprecationWarning)
def test_render_add_sections(): """Test adding figures/images to section. """ tempdir = _TempDir() import matplotlib.pyplot as plt report = Report(subjects_dir=subjects_dir) # Check add_figs_to_section functionality fig = plt.plot([1, 2], [1, 2])[0].figure report.add_figs_to_section( figs=fig, # test non-list input captions=['evoked response']) assert_raises(ValueError, report.add_figs_to_section, figs=[fig, fig], captions='H') # Check add_images_to_section img_fname = op.join(tempdir, 'testimage.png') fig.savefig(img_fname) report.add_images_to_section(fnames=[img_fname], captions=['evoked response']) assert_raises(ValueError, report.add_images_to_section, fnames=[img_fname, img_fname], captions='H') # Check deprecation of add_section with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') report.add_section(figs=fig, captions=['evoked response']) assert_true(w[0].category == DeprecationWarning)
def test_deprecated_methods(tmpdir): """Test methods that are scheduled for removal after 0.24.""" r = Report() r.add_projs(info=raw_fname, title='SSP Projectors', tags=('mytag',)) fig = plt.figure() # Empty figure img_fname = op.join(tmpdir, 'testimage.png') fig.savefig(img_fname) with pytest.warns(DeprecationWarning, match='Report.fnames'): assert len(r.fnames) == 1 with pytest.warns(DeprecationWarning, match='Report.sections'): assert len(r.sections) == 1 with pytest.warns(DeprecationWarning, match='use "title" instead'): r.remove(caption='SSP Projectors') with pytest.warns(DeprecationWarning, match='use .* instead'): r.remove(caption='SSP Projectors', tags=('mytag',)) with pytest.warns(DeprecationWarning, match='Use.*Report.add_figure'): with pytest.raises(TypeError, match='It seems you passed a path'): r.add_figs_to_section(['foo'], 'caption', 'section') with pytest.raises( ValueError, match='Number of "captions" and report items must be equal' ): with pytest.warns(DeprecationWarning, match='Use.*Report.add_figure'): r.add_figs_to_section(figs=[fig, fig], captions='H') # Passing lists should work with pytest.warns(DeprecationWarning, match='Use.*Report.add_image'): r.add_images_to_section(fnames=[img_fname], captions=['evoked response']) with pytest.raises( ValueError, match='Number of "captions" and report items must be equal' ): with pytest.warns(DeprecationWarning, match='Use.*Report.add_image'): r.add_images_to_section(fnames=[img_fname, img_fname], captions='H') with pytest.warns(DeprecationWarning, match='Use.*Report.add_bem'): r.add_bem_to_section( subject='sample', subjects_dir=subjects_dir, decim=100 )
def test_render_add_sections(renderer, tmpdir): """Test adding figures/images to section.""" tempdir = str(tmpdir) report = Report(subjects_dir=subjects_dir) # Check add_figs_to_section functionality fig = plt.plot([1, 2], [1, 2])[0].figure report.add_figs_to_section(figs=fig, # test non-list input captions=['evoked response'], scale=1.2, image_format='svg') pytest.raises(ValueError, report.add_figs_to_section, figs=[fig, fig], captions='H') pytest.raises(ValueError, report.add_figs_to_section, figs=fig, captions=['foo'], scale=0, image_format='svg') pytest.raises(ValueError, report.add_figs_to_section, figs=fig, captions=['foo'], scale=1e-10, image_format='svg') # need to recreate because calls above change size fig = plt.plot([1, 2], [1, 2])[0].figure # Check add_images_to_section with png img_fname = op.join(tempdir, 'testimage.png') fig.savefig(img_fname) report.add_images_to_section(fnames=[img_fname], captions=['evoked response']) report.add_images_to_section(fnames=[img_fname], captions=['evoked response']) pytest.raises(ValueError, report.add_images_to_section, fnames=[img_fname, img_fname], captions='H') pytest.raises(ValueError, report.add_images_to_section, fnames=['foobar.xxx'], captions='H') evoked = read_evokeds(evoked_fname, condition='Left Auditory', baseline=(-0.2, 0.0)) fig = plot_alignment(evoked.info, trans_fname, subject='sample', subjects_dir=subjects_dir) report.add_figs_to_section(figs=fig, # test non-list input captions='random image', scale=1.2) assert (repr(report)) fname = op.join(str(tmpdir), 'test.html') report.save(fname, open_browser=False) with open(fname, 'r') as fid: html = fid.read() assert html.count('<li class="report_custom"') == 8 # several
def test_render_add_sections(): """Test adding figures/images to section. """ tempdir = _TempDir() import matplotlib.pyplot as plt report = Report(subjects_dir=subjects_dir) # Check add_figs_to_section functionality fig = plt.plot([1, 2], [1, 2])[0].figure report.add_figs_to_section( figs=fig, # test non-list input captions=['evoked response'], scale=1.2, image_format='svg') assert_raises(ValueError, report.add_figs_to_section, figs=[fig, fig], captions='H') # Check add_images_to_section img_fname = op.join(tempdir, 'testimage.png') fig.savefig(img_fname) report.add_images_to_section(fnames=[img_fname], captions=['evoked response']) assert_raises(ValueError, report.add_images_to_section, fnames=[img_fname, img_fname], captions='H') # Check deprecation of add_section with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') report.add_section(figs=fig, captions=['evoked response']) assert_true(w[0].category == DeprecationWarning) evoked = read_evokeds(evoked_fname, condition='Left Auditory', baseline=(-0.2, 0.0)) fig = plot_trans(evoked.info, trans_fname=trans_fname, subject='sample', subjects_dir=subjects_dir) report.add_figs_to_section( figs=fig, # test non-list input captions='random image', scale=1.2)
def test_render_add_sections(): """Test adding figures/images to section. """ from PIL import Image tempdir = _TempDir() import matplotlib.pyplot as plt report = Report(subjects_dir=subjects_dir) # Check add_figs_to_section functionality fig = plt.plot([1, 2], [1, 2])[0].figure report.add_figs_to_section( figs=fig, captions=["evoked response"], scale=1.2, image_format="svg" # test non-list input ) assert_raises(ValueError, report.add_figs_to_section, figs=[fig, fig], captions="H") assert_raises(ValueError, report.add_figs_to_section, figs=fig, captions=["foo"], scale=0, image_format="svg") assert_raises(ValueError, report.add_figs_to_section, figs=fig, captions=["foo"], scale=1e-10, image_format="svg") # need to recreate because calls above change size fig = plt.plot([1, 2], [1, 2])[0].figure # Check add_images_to_section with png and then gif img_fname = op.join(tempdir, "testimage.png") fig.savefig(img_fname) report.add_images_to_section(fnames=[img_fname], captions=["evoked response"]) im = Image.open(img_fname) op.join(tempdir, "testimage.gif") im.save(img_fname) # matplotlib does not support gif report.add_images_to_section(fnames=[img_fname], captions=["evoked response"]) assert_raises(ValueError, report.add_images_to_section, fnames=[img_fname, img_fname], captions="H") assert_raises(ValueError, report.add_images_to_section, fnames=["foobar.xxx"], captions="H") evoked = read_evokeds(evoked_fname, condition="Left Auditory", baseline=(-0.2, 0.0)) fig = plot_trans(evoked.info, trans_fname, subject="sample", subjects_dir=subjects_dir) report.add_figs_to_section(figs=fig, captions="random image", scale=1.2) # test non-list input
def test_render_report(): """Test rendering -*.fif files for mne report. """ report = Report(info_fname=raw_fname) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') report.parse_folder(data_path=base_dir) assert_true(len(w) == 1) # Check correct paths and filenames assert_true(raw_fname in report.fnames) assert_true(event_name in report.fnames) assert_true(report.data_path == base_dir) # Check if all files were rendered in the report fnames = glob.glob(op.join(base_dir, '*.fif')) bad_name = 'test_ctf_comp_raw-eve.fif' decrement = any(fname.endswith(bad_name) for fname in fnames) fnames = [fname for fname in fnames if fname.endswith(('-eve.fif', '-ave.fif', '-cov.fif', '-sol.fif', '-fwd.fif', '-inv.fif', '-src.fif', '-trans.fif', 'raw.fif', 'sss.fif', '-epo.fif')) and not fname.endswith(bad_name)] # last file above gets created by another test, and it shouldn't be there for fname in fnames: assert_true(''.join(report.html).find(op.basename(fname)) != -1) assert_equal(len(report.fnames), len(fnames)) assert_equal(len(report.html), len(report.fnames)) evoked1 = read_evokeds(evoked1_fname) evoked2 = read_evokeds(evoked2_fname) assert_equal(len(report.fnames) + len(evoked1) + len(evoked2) - 2, report.initial_id - decrement) # Check saving functionality report.data_path = tempdir report.save(fname=op.join(tempdir, 'report.html'), open_browser=False) assert_true(op.isfile(op.join(tempdir, 'report.html'))) # Check add_figs_to_section functionality fig = evoked1[0].plot(show=False) report.add_figs_to_section(figs=fig, # test non-list input captions=['evoked response']) assert_equal(len(report.html), len(fnames) + 1) assert_equal(len(report.html), len(report.fnames)) assert_raises(ValueError, report.add_figs_to_section, figs=[fig, fig], captions='H') # Check add_images_to_section img_fname = op.join(tempdir, 'testimage.png') fig.savefig(img_fname) report.add_images_to_section(fnames=[img_fname], captions=['evoked response']) assert_raises(ValueError, report.add_images_to_section, fnames=[img_fname, img_fname], captions='H') # Check deprecation of add_section with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') report.add_section(figs=fig, captions=['evoked response']) assert_true(w[0].category == DeprecationWarning) # Check saving same report to new filename report.save(fname=op.join(tempdir, 'report2.html'), open_browser=False) assert_true(op.isfile(op.join(tempdir, 'report2.html'))) # Check overwriting file report.save(fname=op.join(tempdir, 'report.html'), open_browser=False, overwrite=True) assert_true(op.isfile(op.join(tempdir, 'report.html')))