Skip to content

MRG: Refactor mlab object out of examples - #6464

Merged
massich merged 36 commits into
mne-tools:masterfrom
GuillaumeFavelier:mlab_examples
Jun 22, 2019
Merged

massich merged 36 commits into
mne-tools:masterfrom
GuillaumeFavelier:mlab_examples

Conversation

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor

Reference issue

Follow-up of #5822

What does this implement/fix?

I noticed the example scripts still use heavily the object mlab from Mayavi so this PR isolates those calls behind the 3d backend abstraction layer without exposing the private class _Renderer.

@GuillaumeFavelier GuillaumeFavelier changed the title Refactor mlab object out of examples WIP: Refactor mlab object out of examples Jun 17, 2019
@GuillaumeFavelier GuillaumeFavelier self-assigned this Jun 17, 2019
Comment thread mne/viz/backends/_pyvista.py Outdated


def _close_all():
# XXX This is not implemented yet

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.

raise NotImplementedError

Comment thread mne/viz/backends/_pyvista.py Outdated
self.plotter.close()

def title(self, text):
# XXX This is not implemented yet

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.

NotImplementedError

Comment thread mne/viz/_3d.py Outdated
renderer.title(title)
elif title is not None:
raise TypeError('title must be an instance of `str` or `None`: '
'{} was given'.format(type(title)))

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.

this should be put in a separate private function. If you decide to make the function public

mne.viz.set_camera(renderer or mlab.figure or scene, view)

you don't need to add any parameter for view to the public API.

thoughts someone?

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.

maybe a kwargs as extra parameters?

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 no kwargs in public functions if we can

Comment thread mne/viz/_3d.py Outdated
elevation=view.get('elevation', 90),
focalpoint=view.get('focalpoint',
(0., 0., 0.)),
distance=view.get('distance', 0.6))

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 like the view.get('key', or_default). But If you do this, I would do something like:

_check_value(view, (dict, None))
view = dict() if view is None else view
render.set_cammera(azimuth=view.get('azimuth', 90),
                            elevation=view.get('elevation', 90),
                            focalpoint=view.get('focalpoint',
                                                (0., 0., 0.)),
                            distance=view.get('distance', 0.6)))

and I even think that if you put the defaults in set_cammera and allow a *kwargs something like this should work:

_check_value(view, (dict, None))
view = dict() if view is None else view
render.set_cammera(**view)

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.

yes it works!!

def set_camera(azimuth=90, elevation=90, focalpoint=(0., 0., 0.),
               distance=0.6, **dump_parameters):
    print('azimuth :', azimuth,
          ', elevation :', elevation,
          ', focalpoint :', focalpoint,
          ', distance :', distance)


set_camera(**dict())
set_camera(**dict(azimuth='az', focalpoint='focal'))
set_camera(**dict(azimuth='az', not_here='not_here'))

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 like it, this is very compact and it's straightforward to pass the parameters that way.

@massich massich Jun 17, 2019 •

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.

its a way of saying: I have this dict of paramameters use what you can. Like this the defaults are carried by whoever has knowledge of the defaults (set_camera in this case) otherwise you have to keep passing the defaults.

Notice that you need **dump_parameters otherwise the 3rd call would give you an error. I would log/warn/error if dump_parameters is not empty dict

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.

or maybe is a good idea to not have dump_parameters like this if you pass something that it's not supposed to be passed it would crash. Up to you

Comment thread mne/viz/_3d.py Outdated
view = dict()
if not isinstance(view, dict):
raise TypeError('view must be an instance of `dict` or None: '
'{} was given'.format(type(view)))

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 use the check

@codecov

codecov Bot commented Jun 17, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #6464 into master will increase coverage by <.01%.
The diff coverage is 88.79%.

@@            Coverage Diff             @@
##           master    #6464      +/-   ##
==========================================
+ Coverage   89.23%   89.24%   +<.01%     
==========================================
  Files         413      413              
  Lines       74286    74443     +157     
  Branches    12277    12292      +15     
==========================================
+ Hits        66291    66438     +147     
- Misses       5139     5148       +9     
- Partials     2856     2857       +1

@lgtm-com

lgtm-com Bot commented Jun 18, 2019

Copy link
Copy Markdown

This pull request introduces 2 alerts when merging 83a1944 into 5d2f027 - view on LGTM.com

new alerts:

  • 2 for Unused exception object

Comment thread mne/io/egi/egi.py
ValueError('Watchout. This does not seem to be a simple '
'binary EGI file.')
raise ValueError('Watchout. This does not seem to be a simple '
'binary EGI file.')

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.

nice catch but this should be done is a different PR

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 wasn't sure, I'll open one right now

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.

By any chance, do you know why it has been reported by LGTM only now?

@agramfort

agramfort commented Jun 19, 2019 via email

Copy link
Copy Markdown
Member

Comment thread mne/viz/_3d.py Outdated
Parameters
----------
raw : instance of Raw
A Raw object containing FIF data.

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.

This should be info

Comment thread mne/viz/_3d.py Outdated
picks: array of int
Indices of selected channels.
ch_names: list of str
The names of the channels.

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.

No need for this, just use info['ch_names']

Comment thread mne/viz/_3d.py Outdated
raw : instance of Raw
A Raw object containing FIF data.
picks: array of int
Indices of selected channels.

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.

Move this after con and make the default None with %(picks_data_good)s or so

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.

When I tried to replace the docstring of picks by %(picks_good_data)s I got the following error:

E           AssertionError: 
E           mne.viz._3d.plot_sensors_connectivity %(picks_good_data)s != picks

How does it work?

@lgtm-com

lgtm-com Bot commented Jun 19, 2019

Copy link
Copy Markdown

This pull request introduces 9 alerts when merging 6d72a18 into 9dee0a7 - view on LGTM.com

new alerts:

  • 9 for Unused local variable

@larsoner

Copy link
Copy Markdown
Member

I'm not sure there is much value in plot_decimate_head_surface.py, maybe we should just get rid of it.

plot_read_inverse if it plots a source space can just use plot_alignment.

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, I updated the TODO list accordingly.

@agramfort agramfort changed the title WIP: Refactor mlab object out of examples MRG: Refactor mlab object out of examples Jun 21, 2019
@GuillaumeFavelier

Copy link
Copy Markdown
Contributor Author

With your latest changes, @agramfort , we should be free of any mlab call in examples right?

@larsoner

Copy link
Copy Markdown
Member

Nothing in examples but tutorials is another story:

larsoner@bunk:~/python/mne-python$ git grep ".*mayavi.*mlab"
...
tutorials/misc/plot_ecog.py:from mayavi import mlab
tutorials/misc/plot_sensor_locations.py:from mayavi import mlab
tutorials/sample-datasets/plot_brainstorm_phantom_elekta.py:from mayavi import mlab
tutorials/sample-datasets/plot_phantom_4DBTi.py:from mayavi import mlab
tutorials/simulation/plot_dics.py:from mayavi import mlab
tutorials/simulation/plot_point_spread.py:from mayavi import mlab
tutorials/source-modeling/plot_dipole_orientations.py:from mayavi import mlab
tutorials/source-modeling/plot_forward.py:from mayavi import mlab  # noqa
tutorials/source-modeling/plot_source_alignment.py:from mayavi import mlab

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor Author

I would prefer if we move step by step, the focus of this PR is mainly on examples. What do you say if we do a follow-up PR just for this?

@larsoner

Copy link
Copy Markdown
Member

Fine by me

@GuillaumeFavelier

GuillaumeFavelier commented Jun 21, 2019 •

Copy link
Copy Markdown
Contributor Author

I think I found a good compromise for set_3d_title (new size parameter) so with this the TODO list is done and I think the PR is ready for reviews/merge if the CIs come back happy.

I shall add that with the modifications of the _Renderer API here, some new features are not available yet in the PyVista backend. A follow-up PR will address this.

@larsoner

Copy link
Copy Markdown
Member

I shall add that with the modifications of the _Renderer API here, some new features are not available yet in the PyVista backend. A follow-up PR might address this.

Can you update the table so we can keep track?

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor Author

I already did for the new plot_sensors_connectivity() function but actually we have no table for the _Renderer "contract", what do you think is a good place for this?

@larsoner

Copy link
Copy Markdown
Member

Ahh right it's not public. Just make sure it's documented somewhere so we don't forget, like a GitHub issue or code comment.

@GuillaumeFavelier

Copy link
Copy Markdown
Contributor Author

To be more precise, the following functions of mne.viz.backends._pyvista._Renderer are not implemented yet:

  • tube()
  • text3d()
  • scalarbar()
  • _set_3d_view()
  • _set_3d_title()

# text.property.justification = 'center'
# text.actor.text_scale_mode = 'none'
# text.property.bold = True
# text.property.font_size = 40

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 this needs to be cleaned up before merging

@agramfort

Copy link
Copy Markdown
Member

@larsoner merge if happy

@massich

massich commented Jun 22, 2019 •

Copy link
Copy Markdown
Contributor

MRG +1

@agramfort

Copy link
Copy Markdown
Member

@massich feel free to merge if diff lgty

@massich
massich merged commit 2638d92 into mne-tools:master Jun 22, 2019
@GuillaumeFavelier
GuillaumeFavelier deleted the mlab_examples branch June 24, 2019 16:17
@GuillaumeFavelier GuillaumeFavelier mentioned this pull request Jul 1, 2019
5 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants