ica.apply() doesn't seem to change the data at all

It seems like zeroing out ICA components has no effect on the data. I plot the sources and components and click on the sources I want to zero out before exiting the window, I then use .apply(raw) which tells me it is zeroing out the number of components that I selected, but then when I plot both raw and raw_corrected they are identical.

Before applying ica I also use

ica.plot_overlay(raw, exclude=[9,10], picks="eeg")

to see what the effect will be and there is definitely a difference shown there. The difference in the “average across EEG channels” plot is very noticeable.

MNE version: e.g. 0.24.0
operating system: macOS 12

raw = mne.io.read_raw(before_ICA_path, preload = True)
raw.interpolate_bads()

ica_chans = raw.info['ch_names'].copy()

# create ica instance with defined parameters
ica = ICA(.95,           
          max_iter= 1000, # max iterations allowed for the algorithm
          random_state= 42, 
          method = 'infomax', 
          fit_params=dict(extended = True) 
         )                              

# fit ica with the parameters above to the data
ica.fit(raw,
        picks = ica_chans,          # channels to use
        reject = dict(eeg = 300e-5) # threshold to ignore parts of the signal
       )

# plot topology and select components in sources plot.
ica.plot_sources(raw)
ica.plot_components()

raw_corrected = ica.apply(raw)

raw.plot(start = 14, scalings="auto")
raw_corrected.plot(start = 14, scalings="auto")

Is there any reason why this would be happening? There was a github issue opened on this topic, where I think the people responding basically were saying that the difference will be small enough to not see a change between plots but they didn’t go into any more detail. That just doesn’t really make sense to me either, especially since there is such a noticeable difference when I use plot_overlay.

For anyone having this same issue, the problem was that I did not know that ica.apply() modifies the data in place so I needed to copy the raw data when using it in .apply so I changed this:

raw_corrected = ica.apply(raw)

raw.plot(start = 14, scalings="auto")
raw_corrected.plot(start = 14, scalings="auto")

to this:

raw_corrected = ica.apply(raw.copy())

raw.plot(start = 14, scalings="auto")
raw_corrected.plot(start = 14, scalings="auto")

Or else it literally was plotting the exact same thing.

2 Likes

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