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”):
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.
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}')
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.
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}')