Obtaining bad channels and annotations from command line without closing plot window

Iā€™m using MNE 0.20.7 with Python 3.7 on a Mac with OS 10.15.4.

Suppose I plot my raw data:

raw.plot(block=True)

then remove bad channels and annotate in the plot window. I know that when I close the plot window,

raw.info['bads']

stores the list of channels I de-selected and

raw_copy.annotations.onset
raw_copy.annotations.duration

stores the start times and durations of my annotations.

Iā€™m curious to know if thereā€™s a command line tool for obtaining this information from the window in its current state without having to close the window itself. This would be similar to retrieving axis information in matplotlib using a command such as ax.get_xticks()

Thanks

I assume by ā€œcommand-line toolā€ you mean some Python code?

raw.plot() returns a figure object which will have all the information youā€™re looking for. Weā€™re actually making use of this in the MNE-BIDS Inspector:

So what you can do in your concrete example is:

fig = raw.plot()
# ... interact with the figure ā€¦

# retrieve current selection of bad channels and Annotations
# while the figure window is still open
bads = fig.mne.info['bads']
annotations = fig.mne.inst.annotations

This wonā€™t work with block=True, as that will halt code execution until the window is closed.

I hope this helps.

Yesā€“by command line I meant Python code. Thanks for pointing out raw_figā€“itā€™s exactly what I need. :grinning:

Iā€™m trying to get this to work. Will I need to install mne-bids? I entered the syntax you suggested:

import mne
raw=mne.io.read_raw_edf('my_file.edf',preload=True)

fig = raw.plot()
bads = fig.mne.info['bads']
annotations = fig.mne.inst.annotations
print(bads)

When I did this I encountered the following error message:

Traceback (most recent call last):
  File "/Users/fishbacp/Desktop/my_script.py", line 61, in <module>
    bads = fig.mne.info['bads']
AttributeError: 'Figure' object has no attribute 'mne'


I forgot to mention that the code example I posted relies on plotting functionality introduced with MNE 0.22. So once you update to this version, it should work :+1:

Got it! Updated to .22 and it works perfectly. Thanks so much. This forum is awesome!

1 Like

MNE .22 with Python 3.92 on a Mac with OS 10.15.4.

I wasnā€™t sure if I should start a new thread, but Iā€™m also wondering if I can update raw itself based upon annotations I make when the plot window is open. I have a tkinter GUI in which the user plots, selects bad channels, and annotates. When they press a button, I want raw to be updated to reflect the removal of both bad channels and annotations. Your previous advice shows how to find the bad channels and remove them:

fig=raw.plot()
bads=fig.mne.info[ā€˜badsā€™]
raw.drop_channels(bads)

Now raw.info[ā€˜ch_namesā€™] will reflect an updated list of channels

Similarly, annotations I store as

annotations = fig.mne.inst.annotations

What is the simplest way to update raw to remove the annotations without having to access the starts and durations in the annotations?

Hi Paul,

Did you create this tkinter GUI yourself, or do you mean you are using the tkinter backend of matplotlib? By default raw.plot() uses whatever matplotlib GUI backend is currently set, and when you plot a raw object with fig = raw.plot() and then do some bad channel marking, annotation, etc., then when you close the figure window all of your annotations and bad channels will automatically be updated on the raw object in-place. So after the plot is closed, the original raw object should give you access to everything you want (if Iā€™ve understood correctly what you want).

If you want to access the updated bads, annotations, etc while the plot is still open then Richard is correct that fig.mne.info['bads'] or fig.mne.inst.annotations is where those things are stored. It should be perfectly safe to read those values, however, note that the structure of fig.mne.* is fairly new, undocumented, and not yet guaranteed to be stable. In particular, we havenā€™t rigorously tested what happens if users edit the values in fig.mne.* while the plot is still open, so itā€™s possible that it might not behave the way you want it to.

If for your workflow you need fig.mne.* to be documented and stable and user-editable then please open a GitHub issue and tag @drammock on it and we can discuss further.

2 Likes

The GUI is one Iā€™ve created myself. The idea is that the user can have both the GUI and the plot open in adjacent windows. Currently, when they mark bad channels in the plot and hit a button on my GUI, raw is updated to include only the good channels. The plot window stays open though. (This I accomplished with Richardā€™s help a week or so ago.)

I am aware of the ā€œautomatic updatingā€ of raw you refer to when the window is closed, but for workflow purposes Iā€™d like to use a button in my GUI to do it with the window open. So perhaps I should open the GitHub issue as you suggest.

Thanks.