Skip to content

Commit d3747b5

Browse files
larsoneragramfort
authored andcommitted
MRG: Better type check for report (#6132)
* FIX: Better type check for report * FIX: Reword
1 parent aa74f02 commit d3747b5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

mne/report.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ def _fig_to_img(fig, image_format='png', scale=None, **kwargs):
6969
try:
7070
mlab = _import_mlab()
7171
# on some systems importing Mayavi raises SystemExit (!)
72-
except Exception as e:
73-
warn('Could not import mayavi (%r). Trying to render'
74-
'`mayavi.core.api.Scene` figure instances'
75-
' will throw an error.' % (e,))
72+
except Exception:
73+
is_mayavi = False
74+
else:
75+
import mayavi
76+
is_mayavi = isinstance(fig, mayavi.core.scene.Scene)
77+
if not is_mayavi:
78+
raise TypeError('Each fig must be a matplotlib Figure, mayavi '
79+
'Scene, or NumPy ndarray, got %s (type %s)'
80+
% (fig, type(fig)))
7681
if fig.scene is not None:
7782
img = mlab.screenshot(figure=fig)
7883
else: # Testing mode

mne/tests/test_report.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ def test_render_report():
142142
# ndarray support smoke test
143143
report.add_figs_to_section(np.zeros((2, 3, 3)), 'caption', 'section')
144144

145+
with pytest.raises(TypeError, match='Each fig must be a'):
146+
report.add_figs_to_section('foo', 'caption', 'section')
147+
with pytest.raises(TypeError, match='Each fig must be a'):
148+
report.add_figs_to_section(['foo'], 'caption', 'section')
149+
145150

146151
@testing.requires_testing_data
147152
def test_report_raw_psd_and_date():

0 commit comments

Comments
 (0)