Interactive data browsing with annotations

Hello, I’m currently plotting an interactive plot of an EEG with annotations.

I want to know when the user selects a time region with an annotation in the time bar of the interactive plot. Is this possible?

    raw.set_annotations(annot)
    raw.plot()

For example, I have an annotation in the 8234 second and I want to know when the user selects this region:

Hello Eva,

welcome to the MNE-Forum.

I am not sure if I understand your question right:
Do you want to be notified when a user selects an annotation inside the shown time range?
Or do you want get the time boundaries of a selected annotation(-region)?

The raw-browser offers interaction for adding, editing and removing annotations, but does not support returning a selection of annotations.

If you have the newest stable version of mne-python installed (0.24.1), you could try the new browser-backend and see if that offers you what you need, e.g. there you would see the time-boundaries of the current selected annotation inside the annotation-toolbar (which is activated by pressing “a”):

mne.viz.set_browser_backend('pyqtgraph')
raw.plot()

Best wishes
Martin

Hello Martin,

What I would like to do, is to be able to print the current selected time region in the plot. So for example, if the user is selecting the time region from second 0 to second 10, I would like to be able to print that in the console or store it in a variable.

Thanks for your response!

Hello Eva,

this is not supported by the official API. But for the pyqtgraph-backend there is in fact a reference to the last created or selected annotation-region stored internally.
If you run

browser=raw.plot(block=False)

in a python-terminal (or in IPython, but then you also need to have interactive support activated with %gui qt5 before) you can access this reference with:

current_region = browser.mne.selected_region
if current_region is not None:  # It is None when no region is selected
    start, end = current_region.getRegion()
    print(f'Bounds of selected region are {start} and {end}')
1 Like

Hello again Martin,

Thank you so much! This seems to work but the code never enters the if condition because current_region is always None. I used the same code you provided so I’m not sure what the problem could be.

Thanks again,

Best regards,

Eva

Hello Eva,

what terminal are you using (Python, IPython, etc.)?
Maybe you don’t run the complete code everytime and just the if-statement?
You would need to rerun

current_region = browser.mne.selected_region

everytime, because the namespace current_region is only referencing the value of browser.mne.selected_region which is None at the beginning.
This might be better:

if browser.mne.selected_region is not None:  # It is None when no region is selected
    start, end = browser.mne.selected_region.getRegion()
    print(f'Bounds of selected region "{browser.mne.selected_region.description}" are {start} and {end}')

Does it work?

Hello Martin,

I’m using Python terminal.

That still returns None for me. However, I found a parameter that returns exactly what I was looking for:

browser.mne.viewbox.viewRange()

It returns the selected range in both the x and y axis like this:
[[4983.87890625, 4993.87890625], [0.0, 20.0]]

Thank you so much for your help!

Best regards,

Eva

2 Likes