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

**URL:** <https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759>\
**Category:** Support & Discussions\
**Tags:** eeg\
**Created:** [February 27, 2021, 2:33pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759 "2021-02-27T14:33:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulF](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PaulF](https://mne.discourse.group/u/PaulF)\
**Post date:** [February 27, 2021, 2:33pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/1 "2021-02-27T14:33:32Z")

</div>

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

```python
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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [February 27, 2021, 4:08pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/2 "2021-02-27T16:08:54Z")

</div>

> [@PaulF](#):
>
> 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.

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:

> <https://github.com/mne-tools/mne-bids/blob/45f3f39c54472b3765692ef1fdfd285f1b6172f2/mne_bids/inspect.py#L139-L140>

So what you can do in your concrete example is:

```python
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.

---

<div class="post-metadata">

**Author:** ![PaulF](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PaulF](https://mne.discourse.group/u/PaulF)\
**Post date:** [February 27, 2021, 4:14pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/3 "2021-02-27T16:14:35Z")

</div>

Yes–by command line I meant Python code. Thanks for pointing out raw\_fig–it’s exactly what I need. 😀

---

<div class="post-metadata">

**Author:** ![PaulF](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PaulF](https://mne.discourse.group/u/PaulF)\
**Post date:** [February 27, 2021, 10:21pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/4 "2021-02-27T22:21:11Z")

</div>

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

```python
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:

```python
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'

```

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [February 27, 2021, 10:48pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/5 "2021-02-27T22:48:33Z")

</div>

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 👍

---

<div class="post-metadata">

**Author:** ![PaulF](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PaulF](https://mne.discourse.group/u/PaulF)\
**Post date:** [February 28, 2021, 12:28am UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/6 "2021-02-28T00:28:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PaulF](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PaulF](https://mne.discourse.group/u/PaulF)\
**Post date:** [March 5, 2021, 7:05pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/7 "2021-03-05T19:05:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [March 5, 2021, 8:04pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/8 "2021-03-05T20:04:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PaulF](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@PaulF](https://mne.discourse.group/u/PaulF)\
**Post date:** [March 5, 2021, 9:14pm UTC](https://mne.discourse.group/t/obtaining-bad-channels-and-annotations-from-command-line-without-closing-plot-window/2759/9 "2021-03-05T21:14:55Z")

</div>

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.
