Intracranial EEG

Initialize NeuroAnalyzer
using NeuroAnalyzer
using CairoMakie
CairoMakie.activate!(type = "png")
# Quarto sets a default figure size (672 × 480 px), which overrides the sizes of
# NeuroAnalyzer figures: remove it
delete!(CairoMakie.Makie.CURRENT_DEFAULT_THEME, :resolution)
using MAT
using Statistics
NeuroAnalyzer.progress_bar = false

# Makie figure and axis settings matching NeuroAnalyzer plots
fig_size = (900, 450)
ax_style = (titlesize = 18,
            xlabelsize = 18,
            ylabelsize = 18,
            xticklabelsize = 12,
            yticklabelsize = 12,
            xticks = LinearTicks(10),
            xminorticksvisible = true,
            xminorticks = IntervalsBetween(10),
            xautolimitmargin = (0, 0),
            yautolimitmargin = (0.1, 0.1))

Intracranial EEG (iEEG) is recorded with electrodes placed directly on or in the brain, usually in patients with drug-resistant epilepsy during the evaluation before epilepsy surgery:

  • electrocorticography (ECoG): grids or strips of electrodes on the surface of the cortex (subdural),
  • stereo-EEG (sEEG): depth electrodes with several contacts, inserted into the brain.

Compared with the scalp EEG, iEEG has:

  • a much higher spatial resolution (millimetres) and signal-to-noise ratio, since the signal is not attenuated and blurred by the skull and scalp,
  • a larger amplitude (hundreds of μV) and a broader frequency range, including high-gamma activity (about 70–150 Hz), which reflects local neuronal population activity and is a robust marker of cortical activation,
  • coverage limited to the regions implanted for clinical reasons.

Loading ECoG Data

The example dataset is a MATLAB file with an ECoG recording (47 channels, 1000 Hz) and a stimulus channel coding a task with 3-second blocks: rest (0) and two task conditions (11 and 12). Data stored in a non-standard format are loaded manually and placed in a NEURO object:

# read data from a MATLAB file
dataset = matread("files/eeg/ecog.mat")

# in NeuroAnalyzer channels are in rows: (channels, samples, epochs)
data = reshape(permutedims(Float64.(dataset["data"])), 47, :, 1)

# create NeuroAnalyzer.NEURO object
ecog = create_object(; data_type = "ecog")

# the dataset sampling rate is 1000 Hz
create_data!(ecog;
             data = data,
             fs = 1000,
             type = "ecog")

# the recording was made in a 60 Hz power grid
ecog.header.recording[:line_frequency] = 60

The task blocks are added as markers: a marker at each change of the stimulus channel, with the stimulus code as its value:

stim = vec(dataset["stim"])
onsets = findall(diff([0.0; stim]) .!= 0)
for idx in onsets
    add_marker!(ecog;
                id = "block",
                start = (idx - 1) / sr(ecog),
                len = 3.0,
                value = string(Int(stim[idx])),
                ch = 0)
end
first(ecog.markers, 5)
5×5 DataFrame
Row id start length value channel
String Float64 Float64 String Int64
1 block 10.16 3.0 12 0
2 block 13.16 3.0 0 0
3 block 16.16 3.0 12 0
4 block 19.16 3.0 0 0
5 block 22.16 3.0 12 0
info(ecog)

Ten seconds of the recording (all channels):

NeuroAnalyzer.plot(ecog;
                   ch = "all",
                   seg = (10, 20),
                   gui = false)

Power Spectrum

plot_psd(ecog;
         ch = "all",
         seg = (10, 40),
         flim = (0, 200))

The power decreases with frequency over the whole range (the 1/f spectrum is visible up to 200 Hz, as the signal is recorded directly from the cortex), and the line noise is visible at 60 Hz and its harmonics (120 and 180 Hz). It is removed using notch filters:

for f in [60, 120, 180]
    NeuroAnalyzer.filter!(ecog;
                          ch = "all",
                          fprototype = :iirnotch,
                          cutoff = f,
                          bw = 2)
end

High-Gamma Activity

The power of the high-gamma band (70–150 Hz) is compared between the task blocks and the rest blocks. The recording is divided into 3-second epochs for each condition:

e0 = epoch(ecog; marker = "0", offset = 0, ep_len = 3)
e11 = epoch(ecog; marker = "11", offset = 0, ep_len = 3)
e12 = epoch(ecog; marker = "12", offset = 0, ep_len = 3)
println("Epochs: rest ", nepochs(e0),
        ", condition 11 ", nepochs(e11),
        ", condition 12 ", nepochs(e12))
Epochs: rest 60, condition 11 30, condition 12 30

The mean high-gamma power of each channel, relative to rest (log10 of the ratio; 0.3 is a two-fold increase):

hg(obj) = vec(mean(band_power(obj; ch = "all", flim = (70, 150)); dims = 2))
hg0 = hg(e0)
r11 = log10.(hg(e11) ./ hg0)
r12 = log10.(hg(e12) ./ hg0)
ch_n = nchannels(ecog)
fig = Figure(size = fig_size)
ax = Axis(fig[1, 1];
          ax_style...,
          xticks = 1:2:ch_n,
          xlabel = "Channel",
          ylabel = "log10(task / rest)",
          title = "High-gamma (70-150 Hz) power relative to rest")
barplot!(ax, (1:ch_n) .- 0.2, r11; width = 0.4, label = "condition 11")
barplot!(ax, (1:ch_n) .+ 0.2, r12; width = 0.4, label = "condition 12")
hlines!(ax, 0; color = :black)
axislegend(ax)
fig

The two conditions activate different sets of channels: condition 12 increases the high-gamma power by up to 2.3 times (log ratio 0.37) in channels 6–7, 13–15 and 21–22, where condition 11 slightly decreases it, while condition 11 activates mainly channels 29, 36 and 44. With the electrode locations (not included in this dataset), such maps show the cortical areas involved in each task, e.g. for functional mapping before epilepsy surgery.