mne-tools / mne-tools/mne-python
Issue with updating raw from interactive plots when integrated into PyQt5
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 3.5k
- Forks
- 1.6k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 100
Description
Describe the bug
This issue is related to #8142 - I'm currently implementing MNE 0.24dev0 (having the same issues with 0.23 too) into a pyqt5 application and am trying to mark channels as bad from the plot, however, the bads field in raw.info is not updating (i suspect it may be related to me having show set to False in order to put the figure in a canvas). The tutorial for handling bad channels states block should = True for the bads field to update automatically when the plot is interacted with.
Steps to reproduce
from PyQt5 import QtWidgets, QtGui
import matplotlib
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.qt_compat import QtCore
import sys
from mne.datasets import sample
import mne
import os
class My_Main_window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(My_Main_window, self).__init__(parent)
sample_data_folder = sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
'sample_audvis_filt-0-40_raw.fif')
self.raw = mne.io.read_raw_fif(sample_data_raw_file)
self.fig = self.raw.plot(show=False, block=True)
self.canvas = FigureCanvas(self.fig)
self.canvas.draw()
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.canvas)
self.button = QtWidgets.QPushButton('Print Bad', self)
self.button.clicked.connect(self.BadButton)
layout.addWidget(self.button)
self.stack = QtWidgets.QTabWidget()
self.stack.setLayout(layout)
self.setCentralWidget(self.stack)
# Give the keyboard focus to the figure instead of the manager:
# StrongFocus accepts both tab and click to focus and will enable the
# canvas to process event without clicking.
# https://doc.qt.io/qt-5/qt.html#FocusPolicy-enum
self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus | QtCore.Qt.WheelFocus)
self.canvas.setFocus()
def BadButton(self):
print(self.raw.info['bads'])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName("Plot")
main_win = My_Main_window()
main_win.show()
app.exec()
Expected results
After pressing the "Print Bad" button i expect any selected channels from the plot to print to the terminal.
Actual results
Only the preset bad channels (['MEG 2443', 'EEG 053']) are printed from the sample_audvis_filt-0-40_raw.fif sample dataset.
Additional problem which may be related
Another issue i'm having relates to the psd topomaps when integrated in to pyqt, namely all the previously hidden plot figures appear when selecting a frequency band to plot.
To replicate this you can change the line self.fig = self.raw.plot(show=False, block=True) to self.fig = self.raw.plot_psd(show=False) in the above example, then click and drag over a frequency band on the psd plot to bring up the topomap, the previous figures popup in addition to the topomap popup figure.
Any comments or proposed workarounds would be greatly appreciated.
Kind regards,
Liam
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided PyQt5 reproduction script and the calls to raw.plot(show=False, block=True) and raw.plot_psd(show=False). Inspect how embedded canvases handle channel-selection updates and PSD topomap windows. Done means selected channels update raw.info['bads'] and hidden figures do not appear when a frequency band is selected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- matplotlib, python
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100