Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MRG, FIX, VIZ: refactor plot_evoked_topomap #7706

Merged
merged 14 commits into from
May 2, 2020

Conversation

drammock
Copy link
Member

@drammock drammock commented Apr 30, 2020

closes #7595

Setup for testing:

import numpy as np
import matplotlib.pyplot as plt
from mne.datasets import sample
from mne import read_evokeds

path = sample.data_path()
fname = path + '/MEG/sample/sample_audvis-ave.fif'
condition = 'Left Auditory'
evoked = read_evokeds(fname, condition=condition, baseline=(None, 0))
times = np.linspace(0, 0.5, 7)

interactive usage

evoked.plot_topomap(times='interactive')

current master

fig1

this PR

Screenshot_2020-04-30_14-57-24

interactive without colorbar

evoked.plot_topomap(times='interactive', colorbar=False)

current master

IndexError: index 2 is out of bounds for GridSpec with size 2

this PR

fig2

standard non-interactive usage

evoked.plot_topomap(times=times)

current master

fig3

this PR

fig3

standard usage no colorbar

evoked.plot_topomap(times=times, colorbar=False)

current master

fig4

this PR

fig4

specifying rows/columns

evoked.plot_topomap(times=times, ncols=3, nrows='auto')

current master

fig5

this PR

fig5

rows/columns, no colorbar

evoked.plot_topomap(times=times, ncols=3, nrows='auto', colorbar=False)

current master

fig6

this PR

fig6

user passes axes

fig, axs = plt.subplots(2, 2)
evoked.plot_topomap(times=times[:2], axes=axs[0], colorbar=False)
evoked.plot_topomap(times=times[2], axes=axs[1], colorbar=True)

current master

fig7

Also returns RuntimeError: Axes and times must be equal in sizes. after closing the incomplete figure.

this PR

fig7

@drammock drammock requested a review from hoechenberger April 30, 2020 19:14
@drammock
Copy link
Member Author

cc @fraimondo you may be interested to test these changes, to make sure your nrows / ncols usage still works as expected.

@agramfort
Copy link
Member

@hoechenberger you will be interested in this too

@drammock drammock marked this pull request as ready for review April 30, 2020 22:00
@drammock
Copy link
Member Author

Ready for review. Note that @hoechenberger's use case that motivated this fix is still somewhat problematic, but I don't see a clear solution to either of these problems:

  1. If a user plots to axes of an existing figure in multiple steps (as shown above), the colorbar will only be accurate for the subplots that were drawn at the same time as the colorbar. So, in the example above, the colorbar scale only is accurate for the bottom-left plot, not for the two top-row plots. I think this is unavoidable. May be worth a Notes section warning in the docstring.

  2. In the same example, the colorbar fills the whole subplot space. Although this looks bad in the example above, I think it's actually what we should do when the user passes in their own axes; we should assume that they can use gridspec kwargs if they want to.

@codecov
Copy link

codecov bot commented May 1, 2020

Codecov Report

Merging #7706 into master will increase coverage by 4.31%.
The diff coverage is 89.47%.

@@            Coverage Diff             @@
##           master    #7706      +/-   ##
==========================================
+ Coverage   85.74%   90.05%   +4.31%     
==========================================
  Files         455      455              
  Lines       84571    84621      +50     
  Branches    13400    13399       -1     
==========================================
+ Hits        72512    76203    +3691     
+ Misses       9323     5549    -3774     
- Partials     2736     2869     +133     

Copy link
Member

@hoechenberger hoechenberger left a comment

Choose a reason for hiding this comment

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

  • If a user plots to axes of an existing figure in multiple steps (as shown above), the colorbar will only be accurate for the subplots that were drawn at the same time as the colorbar. So, in the example above, the colorbar scale only is accurate for the bottom-left plot, not for the two top-row plots. I think this is unavoidable. May be worth a Notes section warning in the docstring.

I would almost think that common sense would tell you that, but yes, maybe a few words in the Notes section are justified anyway. :)

  1. In the same example, the colorbar fills the whole subplot space. Although this looks bad in the example above, I think it's actually what we should do when the user passes in their own axes

Yes to me this is expected behavior and should be exactly this way.

Just wondering, we had also discussed whether having a separate cbar_axes kwarg could make sense. But I guess this solution here (last axes used for colorbar) is sufficiently flexible already. Considering a case where maybe I'd like to print 5 topographies with their own colorbar each into the same figure, I could simply create a 5-by-2 grid and plot the topomaps one after another, right?

Aside from my few comments, this looks very good to me and is definitely a great improvement to what we had before.

Please also add a changelog entry.

mne/viz/topomap.py Show resolved Hide resolved
Comment on lines +1658 to +1659
raise RuntimeError(f'You must provide {want_axes} axes (one for '
f'each time{cbar_err}), got {len(axes)}.')
Copy link
Member

Choose a reason for hiding this comment

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

❤️ The future is now ❤️

Comment on lines +1754 to +1755
# axes were given by the user, so don't resize the colorbar
cax = axes[-1]
Copy link
Member

Choose a reason for hiding this comment

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

I think this is the right thing to do. 👍

mne/viz/topomap.py Outdated Show resolved Hide resolved
@drammock
Copy link
Member Author

drammock commented May 1, 2020

@larsoner the Azure failure here was a one-hour timeout, but it looks like it managed to run all the tests successfully, and just barely ran out of time during the "finalize job" step. So I'm marking this as MRG, but let's talk (offline?) about pruning execution time some more (your favorite subject, I know).

@drammock drammock changed the title FIX, VIZ: refactor plot_evoked_topomap MRG, FIX, VIZ: refactor plot_evoked_topomap May 1, 2020
@larsoner
Copy link
Member

larsoner commented May 1, 2020

We already have a PR for the PyVista tests being slow, which accounts for a lot of the slowest ones:

https://dev.azure.com/mne-tools/mne-python/_build/results?buildId=6946&view=logs&jobId=a846d25a-e32c-5640-1b53-e815fab94407&j=a846d25a-e32c-5640-1b53-e815fab94407&t=f8799db5-b972-5f58-516d-bc88b8357f99

But feel free to open an issue about the other ones. I'm happy to take a look soon

Copy link
Member

@hoechenberger hoechenberger left a comment

Choose a reason for hiding this comment

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

Aside from the few tiny nitpicks, LGTM!

mne/viz/topomap.py Outdated Show resolved Hide resolved
doc/changes/latest.inc Outdated Show resolved Hide resolved
doc/changes/latest.inc Outdated Show resolved Hide resolved
Co-authored-by: Richard Höchenberger <richard.hoechenberger@gmail.com>
@larsoner larsoner merged commit 9ad12c9 into mne-tools:master May 2, 2020
@larsoner
Copy link
Member

larsoner commented May 2, 2020

Code LGTM, thanks @drammock !

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.

Evoked.plot_topomap() behaves in strange ways when axes is passed
4 participants