Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vizualization framework for channel specific annotations #202

Merged
merged 15 commits into from
Sep 30, 2023

Conversation

scott-huberty
Copy link
Contributor

@scott-huberty scott-huberty commented Sep 26, 2023

Closes #200
Closes #196

(EDIT) starts to close mne-tools/mne-python#8946 . I think we will need to implement the same funcitionality for the matplotlib backend before closing that issue.

@scott-huberty scott-huberty marked this pull request as draft September 26, 2023 18:43
@scott-huberty scott-huberty changed the title Ch spec annot viz Vizualization framework for channel specific annotations Sep 26, 2023
should handle a key error that will be thrown when trying to access "ch_names" in an Annotation without that key. This case occurs in the mne base tests.
@scott-huberty
Copy link
Contributor Author

scott-huberty commented Sep 26, 2023

This almost works:

Of course, the sample audvis dataset always seems to present the most complex scenario (floating point sfreq, etc) 🙂

If I run this:

import mne
from mne.datasets import sample
mne.viz.set_browser_backend("qt")

data_path = sample.data_path()
meg_path = data_path / "MEG" / "sample"
raw_fname = meg_path / "sample_audvis_raw.fif"
raw = mne.io.read_raw_fif(raw_fname, preload=True)
raw.pick("meg").filter(1, 30)

ch_names = ["MEG 0133", "MEG 0142", "MEG 0143", "MEG 0121"]
onsets = [1, 4.25, 7.25]
durs = [2, 1.75, 1.25]
descs = ["Specific_channels_test", 'BAD_test', 'Good_test']
chs = [ch_names, [], []]
annots = mne.Annotations(onset=onsets, duration=durs, description=descs, ch_names=chs)

raw.set_annotations(annots)
raw.plot(theme="light")

I get this plot

Screen Shot 2023-09-26 at 3 19 47 PM

"MEG 0143" is in ch_names, but it is NOT highlighted in the plot. ["MEG 0212", "MEG 0222"] are NOT in the ch_names but they ARE highlighted.

@drammock I think this has to do with our "dirty hack", and that these MEG channels are not ordered in the plot by their number (i.e. "MEG 0133", "MEG 0143", "MEG 0142").

@drammock
Copy link
Member

ah, right you are. Our dirty hack will not work. Maybe check if the channel order is separately specified / kept track of somewhere in fig.mne)?

@larsoner
Copy link
Member

Aesthetically pleasing despite the indexing bug!

For the indexing I would look at how we figure out when you click on a channel name Y label which channel is actually bad. Indexing correspondence between channel names and what is currently shown should be used there I think

I could not figure out how to find the y coordinates of the
FillBetweenItems. So this only tests that the FillBetweenItems, i.e. the shaded rectangles around each channel in annotation["ch_names"], match the number of channel names in the annot["ch_names"] key
@scott-huberty scott-huberty marked this pull request as ready for review September 27, 2023 20:42
@larsoner
Copy link
Member

@scott-huberty did you test interactivity? Using this code:

import mne
raw = mne.io.read_raw_fif(mne.datasets.sample.data_path() / "MEG" / "sample" / "sample_audvis_raw.fif")
raw.annotations.append(raw.first_time + 1, 2, "CH_SPEC", [["MEG 0113"]])
raw.plot()

pressing "a" on the keyboard to enter annot mode and shrinking the annot I see the ch-specific one does not shrink:

Screenshot 2023-09-29 at 1 52 56 PM

Can you replicate?

@scott-huberty
Copy link
Contributor Author

I did not test that, and can replicate 😅

Looking into it

@larsoner
Copy link
Member

Code looks good other than the interactivity issue!

@larsoner
Copy link
Member

FYI @scott-huberty I changed your wording in the top-comment from "supersedes" to "closes" because the latter does GitHub closing magic

@scott-huberty
Copy link
Contributor Author

scott-huberty commented Sep 29, 2023

Going to stop for the day soon, but FYI the fix that we thought we had for this (updating the xData of the FillBetween items) is not actually working (the FillBetween item does not redraw on the plot to reflect its new xData). to be continued...

@larsoner
Copy link
Member

xData was just a list, right? There is probably a .update or something that you need to call. But simpler would probably be too call https://pyqtgraph.readthedocs.io/en/latest/api_reference/graphicsItems/plotcurveitem.html#pyqtgraph.PlotCurveItem.setData on the two curves that define the fill between. If that doesn't fix it, calling that then https://pyqtgraph.readthedocs.io/en/latest/api_reference/graphicsItems/fillbetweenitem.html#pyqtgraph.FillBetweenItem.setCurves should do it. It's likely that these set* functions update some attributes under the hood that are not updated when you just assign a new value in the xData list.

@scott-huberty
Copy link
Contributor Author

Exactly! The setData is what we'll need.I just need to put it in the right place in the codebase. (I tried putting it in the _stop_changed method, but stop_changed isn't being triggered when I adjust an annotation)

Let's make sure this works.. Lot of conflicts due to black formatting PR
…fic annot

- This fix only works if you change the annot region start/stop using the spinboxes
- TODO: Make FillBetweenItems stay synced with annot region if you drag the annot region in the plotting area
@scott-huberty
Copy link
Contributor Author

the FillBetweenItems (i.e. shaded rectangles) of channels specific annotations will now stay in sync with the annotation region IF you use the start/stop spinboxes to change the annotation region start/stop time. For some reason, dragging or extending an annotation directly in the plotting area triggers a different code path.

I had to rebase with main after #203, and force push my changes to this remote branch. Before doing that I made a local copy of the state of this remote branch, in case I realize that i've done something terribly wrong..

@larsoner
Copy link
Member

in case I realize that i've done something terribly wrong..

At the end of the day almost all that matters for our squash-and-merge is the diff on GitHub, so just check to make sure that looks okay and you should be good

@larsoner
Copy link
Member

For some reason, dragging or extending an annotation directly in the plotting area triggers a different code path.

This I think is where the logger.debug can/should help us. I'd add logger.debugs in all the click event handlers (now if you want!) and it should become clear which one is triggered by dragging the edge of an annot

@scott-huberty
Copy link
Contributor Author

Alright that should do it... I'll leave this in your court @larsoner @drammock - enjoy the dev retreat!

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll commit these and merge, thanks @scott-huberty !

mne_qt_browser/_pg_figure.py Outdated Show resolved Hide resolved
mne_qt_browser/_pg_figure.py Outdated Show resolved Hide resolved
mne_qt_browser/_pg_figure.py Outdated Show resolved Hide resolved
Comment on lines 90 to 92
from mne import Annotations
from pyqtgraph.graphicsItems.FillBetweenItem import FillBetweenItem

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't nest in tests

@larsoner larsoner enabled auto-merge (squash) September 30, 2023 22:43
@larsoner larsoner merged commit a5749af into mne-tools:main Sep 30, 2023
21 checks passed
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.

ENH: Proper viz of channel-specific annotations
3 participants