Skip to content

[MRG+1] Renderer class as visualization backend abstraction - #5822

Merged
agramfort merged 66 commits into
mne-tools:masterfrom
GuillaumeFavelier:renderer_backend
Feb 12, 2019
Merged

agramfort merged 66 commits into
mne-tools:masterfrom
GuillaumeFavelier:renderer_backend

Conversation

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor

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 backend directory 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 the mayavi.mlab.quiver3d() function.

Comment thread mne/__init__.py Outdated
if _BACKEND is None:
_BACKEND = os.environ.get('MNE_BACKEND', default_backend)
except NameError:
_BACKEND = os.environ.get('MNE_BACKEND', default_backend)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call it MNE_3D_BACKEND

@GuillaumeFavelier GuillaumeFavelier Jan 11, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you want me to rename the variable _BACKEND too?

Comment thread mne/__init__.py Outdated
def set_backend(backend_name):
"""Sets the backend for MNE
The backend will be set as specified and operations will used
that backend

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check pep257

@codecov

codecov Bot commented Jan 8, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #5822 into master will increase coverage by 0.4%.
The diff coverage is 80.31%.

@@           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

Comment thread mne/__init__.py Outdated

__version__ = '0.18.dev0'

# Set the default backend

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I like the idea. I'm gonna work on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the functions and the main backend selector variable to mne/viz/utils.py for now

Comment thread mne/viz/mlab_backend.py Outdated
opacity=opacity, figure=renderer.fig)


def add_3d_arrows(x, y, z, u, v, w, color, scale, height, resolution,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first try to be honest, I can change it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what would you say about add_arrows(), maybe just add_quiver() is enough?

Comment thread mne/viz/utils.py Outdated
global _BACKEND
try:
if _BACKEND is None:
_BACKEND = os.environ.get('MNE_3D_BACKEND', default_backend)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the env variable is called MNE_3D_BACKEND i would keep it.

MNE_3D_BACKEND = os.environ.get(..)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I was not sure about the suggestion of @agramfort but I agree, it makes more sense to me indeed

Comment thread mne/viz/utils.py Outdated
_BACKEND = os.environ.get('MNE_3D_BACKEND', default_backend)


def set_backend(backend_name):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't rush it.

How is the structure of mne.viz?

@GuillaumeFavelier GuillaumeFavelier Jan 11, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... to start, there is already a mne.viz._3d.py module. I could move the 3D backend setup code in there I suppose.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@agramfort

agramfort commented Jan 11, 2019 via email

Copy link
Copy Markdown
Member

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor Author

Following @larsoner advice, I moved the backend implementation in mne/viz/backends directory.

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)

Comment thread mne/viz/_3d.py
It corresponds to Freesurfer environment variable SUBJECTS_DIR.
surfaces : str | list
Surfaces to plot. Supported values:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GuillaumeFavelier can you avoid such cosmetic changes? it makes diff longer to read for little gain. thx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes of course. my bad, I'll be more careful.

Comment thread mne/viz/_3d.py
Returns
-------
fig : instance of mlab.Figure
The mayavi figure.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't return anything anymore it's an API break. Careful with this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you suggest to return then? mlab.Figure is backend specific.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of returning the instance of the Renderer itself? That would be really convenient considering the current architecture.

Comment thread mne/viz/_3d.py Outdated
fig.scene.interactor.interactor_style = \
tvtk.InteractorStyleTerrain()
_toggle_mlab_render(fig, False)
renderer.init((800, 800), (0.5, 0.5, 0.5))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep keywords names explicit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to find a generic name for a function making basic pre-configuration of the visualization.

Comment thread mne/viz/_3d.py Outdated
if key != 'helmet':
surface.actor.property.backface_culling = True
if key != 'helmet':
renderer.add_surface(surf, colors[key], alphas[key], True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to keep explicit parameter names. For example here True it's not clear what it means. If you name it it becomes clear.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on it.

Comment thread mne/viz/_3d.py Outdated
if len(eegp_loc) > 0:
with warnings.catch_warnings(record=True): # traits
quiv = mlab.quiver3d(
renderer.add_3d_arrows(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll modify it accordingly

----------
backend_name : {'mlab'}, default is 'mlab'
"""
global MNE_3D_BACKEND

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'):
    ....

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tensorly uses global in set_backend() and get_backend() as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 massich left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@massich

massich commented Jan 15, 2019

Copy link
Copy Markdown
Contributor

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.

@agramfort

Copy link
Copy Markdown
Member

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.

@massich

massich commented Jan 15, 2019 via email

Copy link
Copy Markdown
Contributor

@agramfort

agramfort commented Jan 16, 2019 via email

Copy link
Copy Markdown
Member

Comment thread mne/viz/_3d.py
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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can rollback _3d.py and think about a slower strategy of integration.

Comment thread mne/viz/backends/_pysurfer_mayavi.py Outdated
cont.module_manager.scalar_lut_manager.lut.table = colormap


def surface(surface, color=(0.7, 0.7, 0.7), opacity=1.0,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the conceptual difference between surface and mesh?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@agramfort

agramfort commented Jan 16, 2019 via email

Copy link
Copy Markdown
Member

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor Author

I don't think so.

@agramfort

agramfort commented Feb 10, 2019 via email

Copy link
Copy Markdown
Member

Comment thread mne/viz/backends/_pysurfer_mayavi.py Outdated
The last Y component of the quiver.
w: array, shape (n_quivers,)
The last Z component of the quiver.
color: (red, green, blue)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write:

color: tuple
    The color of the quiver: (red, green, blue).

Comment thread mne/viz/backends/_pysurfer_mayavi.py Outdated
The resolution of the arrow.
glyph_height: float
The height of the glyph used with the quiver.
glyph_center:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing

Comment thread mne/viz/backends/_pysurfer_mayavi.py Outdated
The zenith angle of the camera.
distance: float
The distance to the focal point.
focalpoint: (x, y, z)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above. Say it's a tuple

Comment thread mne/viz/backends/_pysurfer_mayavi.py Outdated
return self.mlab.screenshot(self.fig)

def project(self, xyz, ch_names):
"""Convert 3d points to a 2d perspective."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring missing

@agramfort

Copy link
Copy Markdown
Member

besides LGTM

@GuillaumeFavelier you'll need to rebase on master once the CIs are fixed. Hopefully really soon.

@larsoner

Copy link
Copy Markdown
Member

@GuillaumeFavelier you can rebase now that #5915 is in and things should work

@larsoner

Copy link
Copy Markdown
Member

FYI @GuillaumeFavelier you will also need to use the two updated hash values from this PR:

https://github.com/mne-tools/mne-python/pull/5904/files#diff-00583e36a75a694e071f9fc054a71c08

I updated the datasets on osf.io a couple of days ago. (This is also why master CircleCI build currently fails.)

@larsoner

Copy link
Copy Markdown
Member

This pull request introduces 1 alert when merging 00d978e into 63a8ce2 - view on LGTM.com

new alerts:

  • 1 for Unused import

Comment posted by LGTM.com

@larsoner

Copy link
Copy Markdown
Member

This pull request introduces 1 alert when merging d7bb6bf into 63a8ce2 - view on LGTM.com

new alerts:

  • 1 for Unused import

Comment posted by LGTM.com

@larsoner

Copy link
Copy Markdown
Member

This pull request introduces 1 alert when merging 4658f05 into 6fdfb41 - view on LGTM.com

new alerts:

  • 1 for Unused import

Comment posted by LGTM.com

@agramfort
agramfort merged commit 7856133 into mne-tools:master Feb 12, 2019
@agramfort

Copy link
Copy Markdown
Member

Thanks @GuillaumeFavelier !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants