Repository navigation
[MRG+1] Renderer class as visualization backend abstraction - #5822
Conversation
| if _BACKEND is None: | ||
| _BACKEND = os.environ.get('MNE_BACKEND', default_backend) | ||
| except NameError: | ||
| _BACKEND = os.environ.get('MNE_BACKEND', default_backend) |
There was a problem hiding this comment.
you want me to rename the variable _BACKEND too?
| def set_backend(backend_name): | ||
| """Sets the backend for MNE | ||
| The backend will be set as specified and operations will used | ||
| that backend |
Codecov Report
@@ Coverage Diff @@
## master #5822 +/- ##
========================================
+ Coverage 88.7% 89.1% +0.4%
========================================
Files 396 399 +3
Lines 71995 75737 +3742
Branches 12087 13742 +1655
========================================
+ Hits 63860 67487 +3627
- Misses 5206 5315 +109
- Partials 2929 2935 +6 |
|
|
||
| __version__ = '0.18.dev0' | ||
|
|
||
| # Set the default backend |
There was a problem hiding this comment.
I would move all this into some existing utils. ie .utils .viz.utils and import it.
If does not make sense in any of the existing utils, I would create something new. If you want to keep it separate it while developing, I would put in a new file somewhere else while we figure out where shall it go.
There was a problem hiding this comment.
Thanks for the feedback! I like the idea. I'm gonna work on it.
There was a problem hiding this comment.
I moved the functions and the main backend selector variable to mne/viz/utils.py for now
| opacity=opacity, figure=renderer.fig) | ||
|
|
||
|
|
||
| def add_3d_arrows(x, y, z, u, v, w, color, scale, height, resolution, |
There was a problem hiding this comment.
why did we rename it to arrows? I think quiver is quite a standard name.
If so, I would keep it as add_quiver_3d or similar.
There was a problem hiding this comment.
first try to be honest, I can change it.
There was a problem hiding this comment.
and what would you say about add_arrows(), maybe just add_quiver() is enough?
| global _BACKEND | ||
| try: | ||
| if _BACKEND is None: | ||
| _BACKEND = os.environ.get('MNE_3D_BACKEND', default_backend) |
There was a problem hiding this comment.
if the env variable is called MNE_3D_BACKEND i would keep it.
MNE_3D_BACKEND = os.environ.get(..)There was a problem hiding this comment.
yeah I was not sure about the suggestion of @agramfort but I agree, it makes more sense to me indeed
| _BACKEND = os.environ.get('MNE_3D_BACKEND', default_backend) | ||
|
|
||
|
|
||
| def set_backend(backend_name): |
There was a problem hiding this comment.
all these function names are a bit confusing to me (as a user).
I guess that set_backend, get_backend etc are exposed in viz. Then the user code:
from mne.viz import set_backend
set_backend(my_awesome_backend)with this code I don't really understand which kind of backend. Either you add some info in the function name, or you put them in a directory.
from mne.viz.3D import set_backend
set_backend(my_awesome_backend) # I know I'm talking aobut 3D 'cos I've been the one importing it.from mne.viz import set_3D_backend
set_3D_backend(my_awesome_backend) # I know I'm talking aobut 3D 'cos the name says so.If you ask me I like the first one better.
There was a problem hiding this comment.
Good idea, thanks! I thought convenient to have it all in mne.viz to be honest so I can implement your second idea and wait for more feeback.
There was a problem hiding this comment.
Don't rush it.
How is the structure of mne.viz?
There was a problem hiding this comment.
hm... to start, there is already a mne.viz._3d.py module. I could move the 3D backend setup code in there I suppose.
There was a problem hiding this comment.
viz/utils.py is probably not the right place for this stuff
I would make a mne/viz/backends/ directory and put all your code there. I would have one py file per backend that contained the abstracted functions/methods for it, starting with mne/viz/backends/_pysurfer_mayavi.py or so
|
I would make a mne/viz/backends/ directory and put all your code there. I would have one py file per backend that contained the abstracted functions/methods for it, starting with mne/viz/backends/_pysurfer_mayavi.py or so
I agree with this
|
|
Following @larsoner advice, I moved the backend implementation in As of now, changing the backend is done as follows : from mne.viz.backends.renderer import set_3D_backend
set_3D_backend(my_awesome_backend) |
| It corresponds to Freesurfer environment variable SUBJECTS_DIR. | ||
| surfaces : str | list | ||
| Surfaces to plot. Supported values: | ||
|
|
There was a problem hiding this comment.
@GuillaumeFavelier can you avoid such cosmetic changes? it makes diff longer to read for little gain. thx
There was a problem hiding this comment.
Yes of course. my bad, I'll be more careful.
| Returns | ||
| ------- | ||
| fig : instance of mlab.Figure | ||
| The mayavi figure. |
There was a problem hiding this comment.
if you don't return anything anymore it's an API break. Careful with this.
There was a problem hiding this comment.
what do you suggest to return then? mlab.Figure is backend specific.
There was a problem hiding this comment.
What do you think of returning the instance of the Renderer itself? That would be really convenient considering the current architecture.
| fig.scene.interactor.interactor_style = \ | ||
| tvtk.InteractorStyleTerrain() | ||
| _toggle_mlab_render(fig, False) | ||
| renderer.init((800, 800), (0.5, 0.5, 0.5)) |
There was a problem hiding this comment.
please keep keywords names explicit.
There was a problem hiding this comment.
I tried to find a generic name for a function making basic pre-configuration of the visualization.
| if key != 'helmet': | ||
| surface.actor.property.backface_culling = True | ||
| if key != 'helmet': | ||
| renderer.add_surface(surf, colors[key], alphas[key], True) |
There was a problem hiding this comment.
try to keep explicit parameter names. For example here True it's not clear what it means. If you name it it becomes clear.
There was a problem hiding this comment.
I'm working on it.
| if len(eegp_loc) > 0: | ||
| with warnings.catch_warnings(record=True): # traits | ||
| quiv = mlab.quiver3d( | ||
| renderer.add_3d_arrows( |
There was a problem hiding this comment.
if 3d arrows is a vispy naming convention why not but if not I would suggest to keep names that are standard for vtk/mayavi. So quiver3d
There was a problem hiding this comment.
ok, I'll modify it accordingly
| ---------- | ||
| backend_name : {'mlab'}, default is 'mlab' | ||
| """ | ||
| global MNE_3D_BACKEND |
There was a problem hiding this comment.
tensorly also uses a global?
thinking out loud maybe a context manage at some point would be nice to have:
with mne.3d_backend('mayavi'):
....
There was a problem hiding this comment.
tensorly uses global in set_backend() and get_backend() as well.
There was a problem hiding this comment.
Can you add a smoke test that you can do set_3d_backend('mayavi') followed by set_3d_backend('mayavi') without it breaking things (do some simple plot afterward)?
And I think that set_3d_backend('foo') should be a ValueError (rather than warn), and set_3d_backend(1) should be a TypeError (checked with mne.utils._validate_type) and that these degenerate conditions should be checked with with pytest.raises(..., match='...'): in a new mne/viz/backends/tests/test_renderer.py file.
massich
left a comment
There was a problem hiding this comment.
I give it a thought, and I disagree. I would keep add_xx if you actually add the structures and use only xx if you reset everytime.
i.e what does this do?
data_A = data[:10]
data_B = data[10:]
render.sphere(center=data_A)
render.sphere(center=data_B)|
If you reset you oblige me to collect all my data and supply it at once. Rather than add this and add that and add.. in the scene. |
|
keep the pace @GuillaumeFavelier ! no strong feeling about the add_ or not. For me it's a detail. Let's make it work first and then we make it nice. |
|
Yes. Just let it as an open discussion and we can remove the commit or do a
find replace.
The idea is just to keep bouncing ideas and sharing points of view. By the
end we'll call plot.
…On Tue, Jan 15, 2019, 20:30 Alexandre Gramfort ***@***.***> wrote:
keep the pace @GuillaumeFavelier <https://github.com/GuillaumeFavelier> !
no strong feeling about the add_ or not. For me it's a detail. Let's make
it work first and then we make it nice.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#5822 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGt-47O9623_n4iGXAqhx4tzOxPu2fnWks5vDixygaJpZM4Z1Ysv>
.
|
|
sounds like a good idea
|
| meg=None, eeg='original', | ||
| meg=None, eeg='original', ren=None, | ||
| dig=False, ecog=True, src=None, mri_fiducials=False, | ||
| bem=None, seeg=True, show_axes=False, fig=None, |
There was a problem hiding this comment.
@GuillaumeFavelier be careful with API breakage. We have a strong policy of deprecation warnings for one release cycle. Any code that works with 0.17 should work with 0.18 out of the box but with warnings in case of deprecation.
There was a problem hiding this comment.
I can rollback _3d.py and think about a slower strategy of integration.
| cont.module_manager.scalar_lut_manager.lut.table = colormap | ||
|
|
||
|
|
||
| def surface(surface, color=(0.7, 0.7, 0.7), opacity=1.0, |
There was a problem hiding this comment.
what is the conceptual difference between surface and mesh?
There was a problem hiding this comment.
I would say surface has a more generic definition, you can define a surface by an equation for example while a mesh will be described by its combinatorial elements : vertice, edge and triangle.
|
We need both in public api?
|
|
I don't think so. |
|
there is still hope to merge this before tomorrow morning :)
… |
| The last Y component of the quiver. | ||
| w: array, shape (n_quivers,) | ||
| The last Z component of the quiver. | ||
| color: (red, green, blue) |
There was a problem hiding this comment.
I would write:
color: tuple
The color of the quiver: (red, green, blue).
| The resolution of the arrow. | ||
| glyph_height: float | ||
| The height of the glyph used with the quiver. | ||
| glyph_center: |
| The zenith angle of the camera. | ||
| distance: float | ||
| The distance to the focal point. | ||
| focalpoint: (x, y, z) |
There was a problem hiding this comment.
same as above. Say it's a tuple
| return self.mlab.screenshot(self.fig) | ||
|
|
||
| def project(self, xyz, ch_names): | ||
| """Convert 3d points to a 2d perspective.""" |
|
besides LGTM @GuillaumeFavelier you'll need to rebase on master once the CIs are fixed. Hopefully really soon. |
|
@GuillaumeFavelier you can rebase now that #5915 is in and things should work |
|
FYI @GuillaumeFavelier you will also need to use the two updated hash values from this PR: I updated the datasets on |
|
This pull request introduces 1 alert when merging 00d978e into 63a8ce2 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
This pull request introduces 1 alert when merging d7bb6bf into 63a8ce2 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
This pull request introduces 1 alert when merging 4658f05 into 6fdfb41 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
Thanks @GuillaumeFavelier ! |
What does this implement/fix?
This goal of this PR is to isolate actual implementation of visualization features as backend file stored in the new
backenddirectory so that it's easier to maintain this part of the code and manage potentially new backends.Additional information
I started the code refactoring of the function
plot_alignment(), it's still a WIP. I'm currently working on themayavi.mlab.quiver3d()function.