Skip to content

Fix orphaned annot window - #10776

Merged
drammock merged 11 commits into
mne-tools:mainfrom
drammock:fix-orphaned-annot-window
Jun 23, 2022
Merged

drammock merged 11 commits into
mne-tools:mainfrom
drammock:fix-orphaned-annot-window

Conversation

@drammock

@drammock drammock commented Jun 15, 2022 •

Copy link
Copy Markdown
Member

This fixes problems with the matplotlib backend of the raw browser:

MWE for the last item:

import os
import mne

sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
                                    'sample_audvis_filt-0-40_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file).crop(tmax=10).load_data()
annot = mne.Annotations([1], [1.5], 'foo')
raw.set_annotations(annot)
mne.set_log_level('debug')
fig = raw.plot()

yields this output

[ ...omitted Raw loading stuff ]
Using matplotlib as 2D backend.
Opening raw browser...
Closing raw browser <MNEBrowseFigure size 2560x1440 with 5 Axes>
Channels marked as bad:
['MEG 2443', 'EEG 053']

note the line "Closing raw browser...", that shouldn't happen during figure creation

@drammock
drammock force-pushed the fix-orphaned-annot-window branch from 670981a to 029f34c Compare June 16, 2022 14:50
@drammock

Copy link
Copy Markdown
Member Author

OK, I'm totally flummoxed. I've added some extremely verbose debugging lines, and here is the result:

import os
import mne

mne.set_config('MNE_BROWSER_BACKEND', 'matplotlib')
sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
                                    'sample_audvis_filt-0-40_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file).crop(tmax=10).load_data()
mne.set_log_level('debug')
fig = raw.plot()

output:

[ ... omitting the "opening raw data file stuff ... ]
Using matplotlib as 2D backend.
Opening raw browser...
* calling "plt_show(True, block=False, fig=<MNEBrowseFigure size 2560x1440 with 5 Axes>)"
* calling "import matplotlib.pyplot as plt"
* calling "if show and get_backend() != 'agg'" (show=True)
Closing raw browser <MNEBrowseFigure size 2560x1440 with 5 Axes>
Channels marked as bad:
['MEG 2443', 'EEG 053']
* calling "if fig and 'block' in kwargs" (fig=<MNEBrowseFigure size 2560x1440 with 5 Axes>, kwargs='dict(block=False)')
* calling fig.show(block=False)

So for some reason, Closing raw browser ... (which is a debug line in the _close method of BrowserBase class) is showing up on this line:

https://github.com/drammock/mne-python/blob/670981ad9206b49fe6122051919f5b2a823bae07/mne/viz/utils.py#L123

which makes absolutely no sense.

@drammock

drammock commented Jun 16, 2022 •

Copy link
Copy Markdown
Member Author

ok, after commenting out the plt_show call (these lines: https://github.com/drammock/mne-python/blob/670981ad9206b49fe6122051919f5b2a823bae07/mne/viz/utils.py#L154-L157)
and replacing them with pass, then going line-by-line through the plt_show function, I get the following extremely surprising result:

In [1]: import os
   ...: import mne
   ...: 
   ...: mne.set_config('MNE_BROWSER_BACKEND', 'matplotlib')
   ...: sample_data_folder = mne.datasets.sample.data_path()
   ...: sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
   ...:                                     'sample_audvis_filt-0-40_raw.fif')
   ...: raw = mne.io.read_raw_fif(sample_data_raw_file).crop(tmax=10).load_data()
   ...: mne.set_log_level('debug')
   ...: fig = raw.plot()
Opening raw data file /data/mne_data/MNE-sample-data/MEG/sample/sample_audvis_filt-0-40_raw.fif...
    Read a total of 4 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
        Average EEG reference (1 x 60)  idle
    Range : 6450 ... 48149 =     42.956 ...   320.665 secs
Ready.
Reading 0 ... 1502  =      0.000 ...    10.003 secs...
Using matplotlib as 2D backend.
Opening raw browser...
In [2]: from matplotlib import get_backend
In [3]: import matplotlib.pyplot as plt
In [4]: get_backend()                                             # <-- WTF
Closing raw browser <MNEBrowseFigure size 2560x1440 with 5 Axes>  # <-- WTF
Channels marked as bad:                                           # <-- WTF
['MEG 2443', 'EEG 053']                                           # <-- WTF
Out[4]: 'QtAgg'

The get_backend() function does this:

def get_backend():
    """
    Return the name of the current backend.

    See Also
    --------
    matplotlib.use
    """
    return rcParams['backend']

so drilling down further:

In [1]: import os
   ...: import mne
   ...: 
   ...: mne.set_config('MNE_BROWSER_BACKEND', 'matplotlib')
   ...: sample_data_folder = mne.datasets.sample.data_path()
   ...: sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
   ...:                                     'sample_audvis_filt-0-40_raw.fif')
   ...: raw = mne.io.read_raw_fif(sample_data_raw_file).crop(tmax=10).load_data()
   ...: mne.set_log_level('debug')
   ...: fig = raw.plot()
Opening raw data file /data/mne_data/MNE-sample-data/MEG/sample/sample_audvis_filt-0-40_raw.fif...
    Read a total of 4 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
        Average EEG reference (1 x 60)  idle
    Range : 6450 ... 48149 =     42.956 ...   320.665 secs
Ready.
Reading 0 ... 1502  =      0.000 ...    10.003 secs...
Using matplotlib as 2D backend.
Opening raw browser...
In [2]: import matplotlib
In [3]: matplotlib.rcParams['backend']                            # <-- WTF
Closing raw browser <MNEBrowseFigure size 2560x1440 with 5 Axes>  # <-- WTF
Channels marked as bad:                                           # <-- WTF
['MEG 2443', 'EEG 053']                                           # <-- WTF
Out[3]: 'QtAgg'

somehow matplotlib's instantiation of rcParams is triggering a close event on the figure?!
it even happens if I just type matplotlib.rcP<tab>!

Screenshot_2022-06-16_10-04-47

If anyone has any shadow of an idea about what might be going on here, please LMK

@larsoner

Copy link
Copy Markdown
Member

If anyone has any shadow of an idea about what might be going on here, please LMK

Any way you could connect a on_close handler to the figure that will raise RuntimeError, then you do your tab completion? It might spit out a traceback that tells you where it comes from.

If that doesn't work, I'd dig into the matplotlib code, find a place that will handle on_close in the Qt backend (or whatever you're using), and traceback.print_stack()

@drammock
drammock marked this pull request as ready for review June 17, 2022 01:51
@drammock
drammock marked this pull request as draft June 17, 2022 13:48
@drammock
drammock force-pushed the fix-orphaned-annot-window branch from 19cd96b to 432461e Compare June 17, 2022 21:27
@drammock

Copy link
Copy Markdown
Member Author

The code on this is ready for review but I'd still like to add some tests that would have caught these regressions.

@drammock
drammock marked this pull request as ready for review June 18, 2022 17:23
@larsoner

Copy link
Copy Markdown
Member

Looks good so far!

@drammock

Copy link
Copy Markdown
Member Author

OK, I've added a test that fails on main and passes here (locally at least). Ready for review/merge if the CIs are all happy.

@agramfort agramfort left a comment

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.

thx a lot @drammock

I'll let @larsoner or @hoechenberger test / merge when they find the time

@hoechenberger hoechenberger left a comment

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.

@drammock this PR reverts lots of changes that should not be reverted, possibly a rebase went wrong or so

@hoechenberger hoechenberger left a comment

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.

Seems GH failed me, after reloading several,time, those changes I mentioned are not visible anymore … hum.

@drammock

Copy link
Copy Markdown
Member Author

@drammock this PR reverts lots of changes that should not be reverted, possibly a rebase went wrong or so

Argh sorry. I did rebase but git didn't complain about conflicts, I'll have to see what happened.

@hoechenberger

Copy link
Copy Markdown
Member

@drammock this PR reverts lots of changes that should not be reverted, possibly a rebase went wrong or so

Argh sorry. I did rebase but git didn't complain about conflicts, I'll have to see what happened.

I think there was a caching problem somewhere, I cannot see these problems anymore

So all is good to go from my end!

@drammock

Copy link
Copy Markdown
Member Author

the macOS conda job timed out after 6 hours on the install step. Restarted. Has that behavior been seen before?

@drammock drammock mentioned this pull request Jun 23, 2022
Comment thread mne/viz/_figure.py Resolved
@drammock
drammock merged commit 37a9e55 into mne-tools:main Jun 23, 2022
@drammock
drammock deleted the fix-orphaned-annot-window branch June 23, 2022 17:01
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