Epoching filtered EEG data outputting 'NoneType Object'

  • MNE version: 1.0.0
  • Operating system: Windows 10/11
  • Python@ 3.9.7

Hello everyone,

Forewarning: I am fairly new to python so could very much be making a silly mistake.

Basically, I have EEG data for 110 individuals, each with 64 channels. I have the data as .set/.fdt files from EEGLAB, which I import (no problem there). I wanted to create a loop, that for each individual would filter all the data into select frequency bands (iter_freqs below), and then segment them into 2s epochs.

The epoching was working before I started bandpass filtering, but now the epoching outputs a ‘NoneType’ object, and I am quite stuck.

Here is the code:


# Define frequency bands
iter_freqs = [
    ('Delta', 0.25, 4),
    ('Theta', 4, 8),
    ('Alpha', 8, 12)
] 

frequency_map = []
epochs = []
filt = []
band_dict = {}
eeg_dict = {}


for file in all_files: 
    for band, fmin, fmax in iter_freqs: 
    ## Load .set file ##
        raw = mne.io.read_raw_eeglab(file,preload=True)
    ## Resample to 100 Hz ##
        raw = raw.resample(100)
    ## Assemble in dict
        eeg_dict[file] = {
            "Subject ID": file[31:39],
            "Data": raw._data}
    ## Bandpass filter ##
        filt = raw.filter(fmin, fmax,n_jobs=1,
                   l_trans_bandwidth = 0.25,
                   h_trans_bandwidth = 0.25)
        frequency_map.append(((band, fmin, fmax), filt))
    ## Epoch in 2s segments ##
        epochs = mne.make_fixed_length_epochs(raw=filt, duration = 2) # <-- THIS IS NOT WORKING, RETURNS DATA = NONE TYPE
        #epochs.subtract_evoked()
        band_dict[file] = {
            "Subject ID": file[31:39],
            "Data": epochs._data}
    ## Assemble power avgs in variable ##
        del epochs
        del raw

Would anyone be able to help?

The next thing I want to do is calculate an average power in each band in each epoch, so ideally I would have the data saved in a format that I can do this in.

My final query is that I process these in MNE and then save them to a dataset, to later feed into a machine learning algorithm. Is there a more straightforward way to do this (see code below), or is this fine?

eeg_data = pd.DataFrame.from_dict(eeg_dict,orient='index')
eeg_data = eeg_data.set_index('Subject ID')

eeg_data_rank = eeg_data.join(lang_class)

# Save EEG data output
eeg_data_rank.to_pickle("eeg_data.pkl.xz", compression="infer")

# Read eeg_data_rank
# eeg_data_rank = pd.read_pickle("eeg_data.pkl.xz", compression="infer")

# Save frequency map 
file_name = "freq_map.pkl"
open_file = open(file_name, "wb")
pickle.dump(frequency_map, open_file)
open_file.close()

Thanks in advance for any help,

Maria

1 Like

Hello @mariaalfar0 and welcome to the forum!

Could you please share the entire error message including the traceback (the full stack of lines Python prints when you get the error)? Because it’s difficult to know where exactly things are going wrong just by looking at your code alone.

Thank you!!

Also please don’t use pickle to store data to disk unless you know exactly what you’re doing. Pickle is not meant for long-term storage and it’s not portable either. Avoid it at all costs if you can!

Hello Richard, thank you for your reply.

I don’t get an error message per se, it looks as if it runs fine but then outputs the following:

epochs = mne.make_fixed_length_epochs(raw, duration = 2)
Not setting metadata
547 matching events found
No baseline correction applied
0 projection items activated

And if I then open up ‘epochs’, where in ‘raw’ I got a 64x109520 NumPy array, I instead get a NoneType object with size 1.

How the data should look:
pre epoch

How it looks after attempting the epoching:
post epoch

And finally, thank you for your heads up about pickle! Running this script over all 110 individuals takes a long time and I was hoping to save the output so I can easily access it without having to run it all again. What, if anything, should I be using to do this?

Thanks so much for your help!

Maria

Hello!

This is exactly the error message I was talking about :slight_smile: Could you please share it including all messages leading up to it?

I cannot see anything on the screenshots you shared, the resolution is so low, all I see is dark blobs:

I suppose I’d use built-in MNE machinery to write the EEG data to disk as a .fif file, and the DataFrame I would simply store as CSV or TSV file, but instead of adding the raw EEG data, I’d add a column that contains the filename to the respective .fif file.

Best wishes,
Richard

Hi Richard,

Thanks again, and I’m sorry about the quality of the screenshots.

raw = mne.io.read_raw_eeglab(file,preload=True)
## Resample to 100 Hz ##
raw = raw.resample(100)
## Assemble in dict
eeg_dict[file] = {
    "Subject ID": file[31:39],
    "Data": raw._data}
Reading F:\CNN project\Data\5mo Cleaned\AA08gs64_nameEvtsLong.fdt
Reading 0 ... 1095204  =      0.000 ...  1095.204 secs...
C:\Users\malfa\AppData\Local\Temp/ipykernel_4488/13170541.py:1: RuntimeWarning: The data contains 'boundary' events, indicating data discontinuities. Be cautious of filtering and epoching around these events.
  raw = mne.io.read_raw_eeglab(file,preload=True)

Up to here it is fine, and my data is (64, 109520), but then when I try to epoch it, it doesn’t output any errors but clearly doesn’t work either?

raw = mne.make_fixed_length_epochs(raw, duration = 2)
Not setting metadata
547 matching events found
No baseline correction applied
0 projection items activated

I’m sorry to not be able to answer your questions more clearly!

All the best,
Maria

You’re overwriting raw here, instead of creating a new variable epochs. Change raw = ... to epochs = ...