EtienneCmb / visbrain

A multi-purpose GPU-accelerated open-source suite for brain data visualization
http://visbrain.org
Other
241 stars 64 forks source link

screenshot() in IPython #76

Open SyamGadde opened 4 years ago

SyamGadde commented 4 years ago

I recently discovered, through a morning of trial and error, and then by stepping through in a debugger, that Brain.screenshot() doesn't write anything to the given file if running in IPython. I believe the culprit is this short-circuit:

https://github.com/EtienneCmb/visbrain/blob/b599038e095919dc193b12d5e502d127de7d03c9/visbrain/io/write_image.py#L404-L405

because CONFIG['MPL_RENDER'] is set to True in config.py:

https://github.com/EtienneCmb/visbrain/blob/b599038e095919dc193b12d5e502d127de7d03c9/visbrain/config.py#L42-L45

whenever you are running in IPython. I would have expected it to write out a file whether I was using python or ipython. Anyway, I know the work-around now, but might be a stumbling block for those who don't realize this. Thanks for developing this useful tool!

EtienneCmb commented 4 years ago

Hi @SyamGadde ,

Thank you for your feedback. Indeed, Visbrain is currently not supported in iPython / Jupyter. For example, I would to have interactive figures in Jupyter however, I believe that for the moment Vispy is not completely ready for it. So I found a "patch", in an iPython context, the screenshot is rendered offline and displayed with Matplotlib (MPL_RENDER). But as you noted, this is clearly not the ideal solution !

MroutsideturnAF commented 3 years ago

Hi @EtienneCmb,

Thank you for mentioning your workaround. It was very helpful! If anyone else wants to go that way, here is how I used mpl_render combined with a Visbrain SceneObject.


from mpl_render import RenderingImShow 
import numpy as np 
import mpl_render
import matplotlib.pyplot as plt

# sc8 is a Visbrain SceneObj.
# The render function from Visbrain turns the scene object into an array
ar = sc8.render()
# unfortunately the image gets flipped in this process and needs to get flipped back
ar = np.flip(ar, axis = 0)

fig, ax = plt.subplots(1, 1) 
p = RenderingImShow( ax, size = (400, 300), extent = (-5, 0, 0, 5), 
                    render_callback = (lambda size, extent: ar))

# here I delete axis and colorbar, which somehow get added while applying mpl_render
plt.axis('off') 
plt.delaxes(fig.axes[1])

# save the figure in high resolution now possible
fig.savefig("High resoltion.png", dpi=1200)