[Mne_analysis] blocking script while selecting artefacts

Bjørn Erik Juel b.e.juel at medisin.uio.no
Wed Feb 5 10:02:52 EST 2020
Search archives:

        External Email - Use Caution        

Hey all.

I am trying to do something very straightforward with MNE, but I am running into issues that I cannot find a simple solution to. I am hoping someone here will be able to help me out!

I have around 30 datasets with 64 channel spontaneous EEG recordings which I want to preprocess using MNE. Due to the analysis I am doing, I needed to epoch the data, and I would to mark (and reject) bad channels and epochs before further analysis. I would also need to save the number of bad channels and epochs for stats later on. 
That's it! All I need is to do is some basic filtering and re-referencing (visualization), mark bad channels and epochs, save the number of bad channels and epochs, and then move on to the next data set.

Here is my current script (it is within a loop that iterates over all raw datasets):

# load data
data = raw.load_data()

# resample 
new_sampling = 1000
data.load_data()
data.resample(new_sampling, npad='auto')

# filter (first high- then low-pass; notch-filter?)
l_cut, h_cut = 1, 80
data.filter(l_freq=l_cut, h_freq=h_cut)

# plotting data to select bad channels
data.plot(duration=20,n_channels=60,scalings=dict(eeg=100e-6),block=False) # blocking doesnt work here

# to avoid this part running before the bad channels are selected
#input("Press Enter when you are done selecting bad channels...")
#time.sleep(5)
bad_channels.append(data.info['bads']) 
n_bad_channels.append(len(data.info['bads'])) 

# interpolate channels (spherical spline method recommended)
data = data.interpolate_bads(reset_bads=True)  # for presentation of bad channels change to False

# re-reference to average
data.set_eeg_reference('average', projection=False)  # you might want to go with True

# 6. remove bad channels (or do not remove but track them)
data = epoch_data(data, 5.) # this is a function for making events and epoching (it works)

# plot data to pick bad epochs
n_epochs_before = data.get_data().shape[0]
data.plot(n_epochs=3,n_channels=60,scalings=dict(eeg=150e-6),block=True)

n_bad_epochs.append(n_epochs_before - data.get_data().shape[0])


This does not work, however, because the "block=True" input to the plot function crashes my Python (running on Anaconda, Spyder 3.7, in Windows 10). In other words, the script just keeps running while my plot is open, meaning that anything I do while the plot is open (rejecting channels or trials) will be rendered useless. E.g. the rereferencing to average reference happens with bad channels still in the data, the n_bad_channels will always be zero (because it gets stored before I can mark bad channels), and so on. 

Does anyone know away to make the "block" parameter work with python3/anaconda/spyder/windows10? Or perhaps you have a clever workaround for making the script wait until the figure closes (time.sleep, and waiting for buttonpress does not work)


Hope this made sense, and that someone has a solution to offer.
Best,
Bjørn




More information about the Mne_analysis mailing list