0

I am plotting EEG-data using MNE and I want to exit a plot and continue the loop every time a button is pressed. Below is some example code of my program.

os.chdir('M:\\')
#Create a dictionary containing all files by participant, including cleaned files
root_folder = 'M:\\'

for folder in os.listdir(root_folder):
    for filename in os.listdir(folder):
        # Create the file path by joining the directory path and the filename
        file_path = os.path.join(folder, filename)
        
# Load the pickled file
        with open(file_path,'rb') as g:
            raw = pickle.load(g)
        
        #Plot EEG data and select bads
        raw.plot(duration=10, start=0, n_channels= 64, scalings = 'auto', block=False)
        
        #Interpolate bad channels 
        raw_interp = raw.interpolate_bads(reset_bads=False)
        if plt.waitforbuttonpress(100000) == 'q':
            plt.close()
        
        #Pickle data file
        nufilename = filename.split("_HighPassed",maxsplit=1)[0]
        nufilename = nufilename + "_Interp_Chans.pkl"
        
        Dir1 = 'M:\\'
        savepath = os.path.join(Dir1,folder)
        FullSavePath = os.path.join(savepath,nufilename)

        with open(FullSavePath, "wb") as f:
            pickle.dump(raw_interp, f)
        f.close()

I want to employ the button press function after the raw has been plotted and continue the loop of plotting eeg-data. I have looked at the plt waitforbuttonpress function, but haven't gotten it to work yet. Any suggestions?

  • The [pyplot.waitforbuttonpress](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.waitforbuttonpress.html) will return if you press *any* button, and the return value will be then `True`. You'll need another function which can recognize keyboard key press events. See, for example [keypress_demo](https://matplotlib.org/stable/gallery/event_handling/keypress_demo.html) – Niko Föhr Feb 15 '23 at 07:27
  • Yes, I know. The problem with the keypress function is that it uses canvas to draw a figure and uses this as the argument. My program keeps crashing when I try it. – Andreas Massey Feb 15 '23 at 08:00

0 Answers0