NeuroAnalyzer tutorials: Analyze EEG (4)

using NeuroAnalyzer
using Plots
using Statistics
eeg = load("files/eeg.hdf");
[ Info: Loaded: EEG (24 × 282991 × 1; 1105.43 s)

Calculate coherence and magnitude-squared coherence using multi-taper method:

coh, msc, f = coherence(eeg, eeg, ch1="Fp1", ch2="Fp2", method=:mt)
(coh = [0.08022197964578587 0.5216040031845453 … 0.8954710636980591 0.927123930776235;;;],
 mscoh = [0.006435566018288882 0.2720707361381431 … 0.8018684259205333 0.859558783017977;;;],
 f = [0.0, 0.00048828125, 0.0009765625, 0.00146484375, 0.001953125, 0.00244140625, 0.0029296875, 0.00341796875, 0.00390625, 0.00439453125  …  127.99560546875, 127.99609375, 127.99658203125, 127.9970703125, 127.99755859375, 127.998046875, 127.99853515625, 127.9990234375, 127.99951171875, 128.0],)

Using FFT method:

coh, msc, f = coherence(eeg, eeg, ch1="Fp1", ch2="Fp2", method=:fft)
(coh = [0.0802219796457858 0.5216040031845453 … 0.8954710636980591 0.927123930776235;;;],
 mscoh = [0.006435566018288871 0.2720707361381431 … 0.8018684259205334 0.859558783017977;;;],
 f = [0.0, 0.00048828125, 0.0009765625, 0.00146484375, 0.001953125, 0.00244140625, 0.0029296875, 0.00341796875, 0.00390625, 0.00439453125  …  127.99560546875, 127.99609375, 127.99658203125, 127.9970703125, 127.99755859375, 127.998046875, 127.99853515625, 127.9990234375, 127.99951171875, 128.0],)

Plot coherence between O1 vs O2 in the alpha band:

eeg_alpha = NeuroAnalyzer.filter(eeg, ch="all", fprototype=:butterworth, ftype=:bp, cutoff=band_frq(eeg, band=:alpha), order=8)
coh, msc, f = coherence(eeg_alpha, eeg_alpha, ch1="O1", ch2="O2", ep1=1, ep2=1, frq_lim=band_frq(eeg, band=:alpha))
Plots.plot(f, coh[1, :, 1], label="coherence", xlabel="Frequency [Hz]")
Plots.plot!(f, msc[1, :, 1], label="MSC")
[ Info: Signal should be tapered prior to filtering to reduce edge artifacts

Also, plot_coherence() function is available:

coh, mscoh, f = coherence(eeg, eeg, ch1=get_channel(eeg, type="eeg")[1:4], ch2=get_channel(eeg, type="eeg")[5:8], ep1=1, ep2=1, frq_lim=band_frq(eeg, band=:alpha))
# simplify values
coh, _ = areduce(coh, f, n=0.1)
mscoh, f = areduce(mscoh, f, n=0.1)
# generate label pairs
l = paired_labels(get_channel(eeg, type="eeg")[1:4], get_channel(eeg, type="eeg")[5:8])
# plot coherence values along frequencies
plot_coherence(coh[1, :, 1], f, title="Coherence $(l[1])")
[ Info: Minimum coherence 0.208 at 9.1 Hz
[ Info: Maximum coherence 0.868 at 8.5 Hz

For multi-channel data:

plot_coherence(mscoh[:, :, 1], f, clabels=l, title="Magnitude-squared coherence")
[ Info: Channel pair Fp1-C3 minimum coherence 0.043 at 9.1 Hz
[ Info: Channel pair Fp1-C3 maximum coherence 0.753 at 8.5 Hz
[ Info: Channel pair Fp2-C4 minimum coherence 0.027 at 10.2 Hz
[ Info: Channel pair Fp2-C4 maximum coherence 0.733 at 8.5 Hz
[ Info: Channel pair F3-P3 minimum coherence 0.017 at 8.8 Hz
[ Info: Channel pair F3-P3 maximum coherence 0.855 at 12.9 Hz
[ Info: Channel pair F4-P4 minimum coherence 0.001 at 10.6 Hz
[ Info: Channel pair F4-P4 maximum coherence 0.857 at 8.8 Hz
plot_coherence_avg(mscoh[:, :, 1], f, clabels=l, title="Averaged magnitude-squared coherence")
[ Info: Channel pair Fp1-C3 minimum coherence 0.043 at 9.1 Hz
[ Info: Channel pair Fp1-C3 maximum coherence 0.753 at 8.5 Hz
[ Info: Channel pair Fp2-C4 minimum coherence 0.027 at 10.2 Hz
[ Info: Channel pair Fp2-C4 maximum coherence 0.733 at 8.5 Hz
[ Info: Channel pair F3-P3 minimum coherence 0.017 at 8.8 Hz
[ Info: Channel pair F3-P3 maximum coherence 0.855 at 12.9 Hz
[ Info: Channel pair F4-P4 minimum coherence 0.001 at 10.6 Hz
[ Info: Channel pair F4-P4 maximum coherence 0.857 at 8.8 Hz
plot_coherence_butterfly(mscoh[:, :, 1], f, clabels=l, title="Magnitude-squared coherence")
[ Info: Channel pair Fp1-C3 minimum coherence 0.043 at 9.1 Hz
[ Info: Channel pair Fp1-C3 maximum coherence 0.753 at 8.5 Hz
[ Info: Channel pair Fp2-C4 minimum coherence 0.027 at 10.2 Hz
[ Info: Channel pair Fp2-C4 maximum coherence 0.733 at 8.5 Hz
[ Info: Channel pair F3-P3 minimum coherence 0.017 at 8.8 Hz
[ Info: Channel pair F3-P3 maximum coherence 0.855 at 12.9 Hz
[ Info: Channel pair F4-P4 minimum coherence 0.001 at 10.6 Hz
[ Info: Channel pair F4-P4 maximum coherence 0.857 at 8.8 Hz

Plot averaged coherence values as connections:

c = zeros(19, 19)
coh_avg = round.(mean(coh[:, :, 1], dims=2), digits=2)
c[1, 5] = coh_avg[1]
c[2, 6] = coh_avg[2]
c[3, 7] = coh_avg[3]
c[4, 8] = coh_avg[4]
plot_locs(eeg, ch="eeg", connections=c)

(!) Plotting connections requires locs to be added to the object.

(!) Line thickness represents connectivity value. If mono=true, positive values are represented with gray lines, negative – with dotted gray lines. If mono=false, positive values are represented with red lines, negative – with blue lines.

To presents values numerically, set the option weights to false. Positive values

plot_locs(eeg, ch="eeg", connections=c, weights=false)

(!) If mono=false, positive values are in red and negative values are in blue.