ica.apply(epochs) requires preload=True despite epochs having just been defined

  • MNE version: 1.7.1
  • operating system: Windows 11

Dear users,

I am having trouble applying a pre-existing ica file to epoched data. I am using…

# Define filenames
raw_fif_fname = os.path.join('[path_to_raw].fif')
events_fname = os.path.join('[path_to_events_file]-eve.txt')
ica_fname = os.path.join('path_to_ica_file]-ica.fif')

# Read in the events file for this subject    
events = mne.read_events(events_fname)

# Read raw data
raw = mne.io.read_raw_fif(raw_fif_fname, preload=True).pick(['MEG0211', 'MEG1311'])
                                                                                               
# Apply filtering
raw_filt = raw.copy().filter(0, 40)

# Epoch the raw data
epochs = mne.Epochs(raw, events, tmin=-7, tmax=7) 

# Use the existing ica file to remove artefacts
ica = mne.preprocessing.read_ica(ica_fname)
epochs_icad = ica.apply(epochs)

This causes the following error:

RuntimeError: By default, MNE does not load data into main memory to conserve resources. ica.apply requires epochs data to be loaded. Use preload=True (or string) in the constructor or epochs.load_data()."
}

I don’t see why this error is occurring, since the epochs are not being loaded in, they are being defined in the line above. I’ve also set preload=True when reading in the raw data, which does not resolve the problem

Found the solution. Silly me, I didn’t read the error message properly. I just had to use:

ica.apply(epochs.load_data())

You can also simply pass preload=True here.

Best wishes,
Richard

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.