NeuroAnalyzer tutorials: Edit EEG (5)

ICA deconstruct

By default all signal channels are deconstructed and the number of components is the number of signal channel:

ic, ic_mw, ic_var = ica_decompose(eeg)

Output:

[ Info: Attempting to calculate 19 components.
[ Info: Converged at: 1.0e-6
[ Info: Component  1: percent variance accounted for: 29.94
[ Info: Component  2: percent variance accounted for: 20.0
[ Info: Component  3: percent variance accounted for: 11.25
[ Info: Component  4: percent variance accounted for: 7.9
[ Info: Component  5: percent variance accounted for: 6.05
[ Info: Component  6: percent variance accounted for: 6.04
[ Info: Component  7: percent variance accounted for: 5.68
[ Info: Component  8: percent variance accounted for: 3.95
[ Info: Component  9: percent variance accounted for: 2.45
[ Info: Component 10: percent variance accounted for: 1.46
[ Info: Component 11: percent variance accounted for: 1.13
[ Info: Component 12: percent variance accounted for: 0.83
[ Info: Component 13: percent variance accounted for: 0.73
[ Info: Component 14: percent variance accounted for: 0.68
[ Info: Component 15: percent variance accounted for: 0.47
[ Info: Component 16: percent variance accounted for: 0.44
[ Info: Component 17: percent variance accounted for: 0.41
[ Info: Component 18: percent variance accounted for: 0.32
[ Info: Component 19: percent variance accounted for: 0.27

(!) Components are sorted in the order of decreasing variance accounted for (the higher the value, the higher the component accounts for all the data).

To deconstruct into defined number of components:

ic, ic_mw, ic_var = ica_decompose(eeg, n=5)

To deconstruct selected channels:

ic, ic_mw, ic_var = ica_decompose(eeg, ch=1:10, n=5)

ICA components and weights may be included in the NeroAnalyzer object as embedded components:

add_component!(eeg, c=:ic, v=ic)
add_component!(eeg, c=:ic_mw, v=ic_mw)

(!) Components are removed from the object when the signal data is altered in any way. (!) Embedded components are saved with the signal data when using save().

ICA reconstruct

To reconstruct the source signal without a specific component(s):

eeg_new = ica_reconstruct(eeg, ic, ic_mw, ic_idx=[2, 5, 8])
preview(eeg, eeg_new)

(!) by default ic_idx is the list of components that should be removed during reconstruction.

To reconstruct the source signal keeping only the selected component(s), use keep=true option:

eeg_new = ica_reconstruct(eeg, ic, ic_mw, ch=1:19, ic_idx=1:2, keep=true)
preview(eeg, eeg_new)

To use the embedded component for reconstruction:

ica_reconstruct!(eeg, ic_idx=1)

(!) The components must be named :ic and :ic_mw.

To remove the component from the signal:

ica_remove(eeg, ic, ic_mw, ic_idx=1)

(!) ica_remove() reconstructs the source signal using only the specified component(s) and subtract the reconstructed signal from the source.

ICA: plotting

To plot the components:

p1 = NeuroAnalyzer.plot(eeg, ch=1:19, mono=true, seg=(0, 20))
p2 = NeuroAnalyzer.plot(eeg, ic, mono=true, seg=(0, 20))
p = Plots.plot(p1, p2, layout=(2, 1))
plot_save(p, file_name="images/ica_components.png")

To plot the components power spectrum:

p1 = plot_psd(eeg, ch=1, title="original signal", mono=true)
p2 = plot_psd(eeg, ic, c_idx=2, title="ICA #02", mono=true)
eeg_new = ica_reconstruct(eeg, ic, ic_mw, ch=1:19, ic_idx=2, keep=true);
p3 = plot_psd(eeg_new, ch=1, title="signal reconstructed from ICA #02", mono=true)
eeg_new = ica_reconstruct(eeg, ic, ic_mw, ch=1:19, ic_idx=2);
p4 = plot_psd(eeg_new, ch=1, title="signal after removing ICA #02", mono=true)
p = Plots.plot(p1, p2, p3, p4, layout=(2, 2))
plot_save(p, file_name="images/ica_components_psd.png")

To plot the component spectrogram:

p1 = plot_spectrogram(eeg, seg=(0, 10), ch=1, title="original signal")
p2 = plot_spectrogram(eeg, ic, c_idx=2, title="ICA #02")
eeg_new = ica_reconstruct(eeg, ic, ic_mw, ch=1:19, ic_idx=2, keep=true);
p3 = plot_spectrogram(eeg_new, ch=1, title="signal reconstructed from ICA #02")
eeg_new = ica_reconstruct(eeg, ic, ic_mw, ch=1:19, ic_idx=2);
p4 = plot_spectrogram(eeg_new, ch=1, title="signal after removing ICA #02")
p = Plots.plot(p1, p2, p3, p4, layout=(2, 2))
plot_save(p, file_name="images/ica_components_spec.png")

(!) ch is the channel number; ic_idx is the component number.

Plot topographical map of ICA components averaged at time segment [20, 21] seconds:

p = plot_icatopo(eeg, ic, ic_mw, seg=(20, 21))
plot_save(p, file_name="images/ica_topo1.png")

or using embedded components:

p = plot_icatopo(eeg, ic_idx=1:10, seg=(20, 21))
plot_save(p, file_name="images/ica_topo2.png")

Removing ECG artifacts using ICA

EEG has already been filtered at 1 Hz and 40 hz, ECG is in channel 24:

ic, ic_mw, ic_var = ica_decompose(eeg, ch=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 24])
eeg_new = ica_reconstruct(eeg, ic, ic_mw, ic_idx=1)
p1 = NeuroAnalyzer.plot(eeg, ch=[1, 2, 3, 4, 24])
p2 = NeuroAnalyzer.plot(eeg, ic, c_idx=1:4)
p3 = NeuroAnalyzer.plot(eeg_new, ch=1:4)
p = Plots.plot(p1, p2, p3, layout=(3, 1))
plot_save(p, file_name="images/ecg_ica.png")

Output:

[ Info: Converged at: 1.0e-6
[ Info: Component  1: percent variance accounted for: 76.98
[ Info: Component  2: percent variance accounted for: 10.16
[ Info: Component  3: percent variance accounted for: 8.1
[ Info: Component  4: percent variance accounted for: 1.17