Can't display ica.plot_properties() in a mne.Report

Hi,

I am having a hard time adding a figure from ica.plot_properties() into a mne.report, here is the error :

AttributeError                            Traceback (most recent call last)
<ipython-input-20-90570c3b403e> in <module>
     54 with mne.open_report(f_report) as rep:
     55     rep = mne.Report(image_format = 'svg', title = subject)
---> 56     rep.add_figs_to_section(figs = ica_figs, captions = ica_captions, section = 'ICA plotting')
     57     rep.save(f_report, open_browser = True, overwrite = True)
     58     rep.save(f_report[:-2]+'html',open_browser = True, overwrite = True)

~/.virtualenvs/py3.6/lib/python3.6/site-packages/mne/report.py in add_figs_to_section(self, figs, captions, section, scale, image_format, comments, replace)
   1110             img_klass = self._sectionvars[section]
   1111 
-> 1112             img = _fig_to_img(fig, image_format, scale)
   1113             html = image_template.substitute(img=img, id=global_id,
   1114                                              div_klass=div_klass,

~/.virtualenvs/py3.6/lib/python3.6/site-packages/mne/report.py in _fig_to_img(fig, image_format, scale, **kwargs)
     72     elif not isinstance(fig, Figure):
     73         from .viz.backends.renderer import backend, MNE_3D_BACKEND_TESTING
---> 74         backend._check_3d_figure(figure=fig)
     75         if not MNE_3D_BACKEND_TESTING:
     76             img = backend._take_3d_screenshot(figure=fig)

AttributeError: 'NoneType' object has no attribute '_check_3d_figure'

Here is the code to reproduce the error as well as the raw data used (>100Mo), you can directly see the outputs and the error in the Jupyter Notebook version : reportError_code.zip - Google Drive

It seems to me that the report function can’t display a figure with multiple axes like ica.plot_properties(), if I just add an ica.plot_scores or plot_components figure with only 1 axe in the report it works fine (example also included in the provided code). Also displaying the figure directly outside of the report works fine.

I can’t figure it out why it displays an error related to a 3D figure but trying to solve this with mne.viz.set_3d_backend(“notebook”), and replacing notebook by pyvista or mayavi but none worked…
Setting matplotlib to qt instead of inline as recommended for Linux users won’t work…
I also tried updating to the last mne, matplotlib and Ipython version, didn’t work neither…

I am running out of ideas and it seems nobody ever encountered the same problem so if you have any clue I would appreciate !

Cheers,

Arnaud

Config :
Platform: Linux-4.15.0-141-generic-x86_64-with-Ubuntu-18.04-bionic
Python: 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0]
Executable: /home/arnaud/.virtualenvs/py3.6/bin/python
CPU: x86_64: 4 cores
Memory: Unavailable (requires “psutil” package)
mne: 0.22.1
numpy: 1.16.2 {blas=openblas, lapack=openblas}
scipy: 1.5.2
matplotlib: 3.3.4 {backend=Qt5Agg}

sklearn: 0.23.2
numba: Not found
nibabel: Not found
nilearn: Not found
dipy: Not found
cupy: Not found
pandas: 0.24.2
mayavi: 4.7.2
pyvista: 0.29.1 {OpenGL 4.6 (Core Profile) Mesa 20.0.8 via Mesa DRI Intel(R) HD Graphics 620 (KBL GT2)}
vtk: 9.0.1
PyQt5: 5.12.1

Hello @apoublan, in principle, adding ICA property plots to the report should work; in fact, we’re doing that in the MNE BIDS Pipeline, producing reports like this:

http://mne.tools/mne-bids-pipeline/examples/ERP_CORE/sub-015_ses-ERN_task-ERN_proc-ica_report.html

However, we don’t use SVG and we only add one property plot at a time.

I can have a look at your code later.

Best wishes,

Richard

Hello @richard,
Thanks for the report example !
I tried in PNG too, same problem and in the example I sent you there is only one EOG component identified so one property plot at a time.

Thanks a lot, let me know.

Best wishes

Arnaud

ICA.plot_properties() returns a list of figures. The following MWE based on you code & data works for me:

# %%
import mne


fname = './S01-raw.fif'
raw = mne.io.read_raw_fif(fname, preload=True)

n_components = 0.98
raw_ica = raw.copy().pick_types(eeg=True).filter(l_freq=1, h_freq=40)

ica = mne.preprocessing.ICA(n_components=n_components, method='picard',
                            random_state=11)
ica.fit(raw_ica)

ica_captions = []
ica_figs = []
ica_figs.extend(ica.plot_properties(raw, picks=[0, 1]))
ica_captions.extend(['property 1', 'property 2'])

report = mne.Report(image_format='svg')
report.add_figs_to_section(section='ICA', figs=ica_figs, captions=ica_captions)
report.save('/tmp/foo.html', overwrite=True)

Hi Richard,

I could have a look at your fix, indeed the use of the extend method instead of append to add the list of figures returned by ica.plot_properties worked out, thank you !

Best,

Arnaud

1 Like