Saving a coregistration visualization in a file

  • MNE version: 0.24.1
  • operating system: Ubuntu 20.04

Hi,

I have been trying to save visualizations of MRI-MEG coregistration in an image file. This has proven particularly difficult. The documentation of mne.viz.plot_alignment states that the function output is a mayavi.mlab.Figure object.

My code looks something like this:

fig = mne.viz.plot_alignment(info=info, trans=trans, subject=subject, 
                             dig=True, meg=['helmet', 'sensors'], 
                             subjects_dir=subjects_dir, surfaces='brain', 
                             mri_fiducials=fiducials, bem=bemmodel, 
                             verbose=True)

mlab.savefig(fig_fname, figure=fig)

Here, mlab.savefig() is the mayavi method for saving figures.

This code results in an error, _Figure object has no attribute 'scene'.
Notably, the _Figure object appears to be an mne.viz.backends._pyvista._Figure object instead of mayavi.mlab.Figure.

This is where I’m stuck, so how would I continue from here, to save a screenshot of the coregistration in a script?

Thanks for your help everyone!

Best,
Santeri

Hi, I’m not familiar with Mayavi, I suggest you do

pip install pyvista
pip install pyvistaqt

For Pyvista is a much pythonic package to use.
After you install Pyvista and its Qt-based pyvistaqt.
Then you could run

fig = mne.viz.plot_alignment(info=info, trans=trans, subject=subject, 
                             dig=True, meg=['helmet', 'sensors'], 
                             subjects_dir=subjects_dir, surfaces='brain', 
                             mri_fiducials=fiducials, bem=bemmodel, 
                             verbose=True)

Remember, adjust the figure as you want, don’t close it.
Then run

fig.plotter.screenshot('my_fig.png')

Hi,

This resolved the issue. Thank you! Sorry that I forgot to reply earlier.