How to end plt plotting in loop by specific key press?

43 Views Asked by At

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?

0

There are 0 best solutions below