Forward Solutions

Initialize NeuroAnalyzer
using NeuroAnalyzer
using LinearAlgebra
import CairoMakie
# 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)

The forward solution (lead field, gain matrix) gives the signal at each channel produced by a dipole of unit moment (1 A·m) at each source. It is computed from the BEM model, the source space and the sensor positions coregistered with the MRI.

The examples use the sample subject of MNE-Python: the BEM solution and the cortical source space are described in the Head Models tutorial, the head → MRI transform in the Coregistration tutorial:

bem_dir = "files/bem/sample"
bem_sol = import_bem(joinpath(bem_dir, "sample-320-320-320-bem-sol.fif"))
src = import_src(joinpath(bem_dir, "sample-oct-5-src.fif"))

MNE-Python forward solutions

MNE-Python forward solutions (make_forward_solution(), *-fwd.fif) are loaded with import_fwd(); the sources are moved to the head frame of the sensors:

fwd = import_fwd(joinpath(bem_dir, "sample-eeg-oct-5-fwd.fif"))
NeuroAnalyzer FORWARD
  channels: 59 (eeg 59)
  sources: 1884 (lh, rh), free orientation
  gain: 59 × 5652
  frame: head
  file: files/bem/sample/sample-eeg-oct-5-fwd.fif

Sources closer than 5 mm to the inner skull were excluded by MNE-Python (1884 of 2052 sources). With free orientation, each source has three columns (dipoles along x, y and z of the head frame), so the gain matrix has 59 rows (EEG channels) and 3 × 1884 columns. Units are V per A·m for EEG (T per A·m for magnetometers, T/m per A·m for gradiometers):

size(fwd.gain), unique(fwd.header[:units])
((59, 5652), ["V/Am"])

The sensitivity of the EEG to each source, the norm of its lead field (all channels, all three orientations), shows which sources produce the largest signals: the crowns of the gyri close to the skull; deep sources and sources in the sulci produce weaker signals:

n = size(fwd.src.pos, 1)
# μV per nAm: V/(A·m) × 10⁶ μV/V × 10⁻⁹ A·m/nAm
sens = [sqrt(sum(abs2, fwd.gain[:, (3i - 2):3i])) for i in 1:n] .* 1e-3
lh = fwd.src.space .== 1
ctx = NeuroAnalyzer.SURFACE(fwd.src.pos[lh, :], fwd.src.triangles[1];
                            values = Dict(:sens => sens[lh]))
plot_surface(ctx; values = :sens, cb_title = "μV / nAm", cam = (10, 180),
             gui = false)

Computing forward solutions

forward() computes the MEG and EEG forward solution of a FIFF measurement (channel positions, MEG coil types, device → head transform) with a BEM model, as make_forward_solution() of MNE-Python (forward_eeg(): EEG electrodes given as positions). The MEG field is computed at the integration points of each coil (coil definitions of MNE-Python), the EEG potentials against the reference electrode of the recording. Here with the 3-layer BEM model with 320 triangles per surface (for speed; use 5120 triangles for real data):

# head → MRI transform of the coregistration (see the Coregistration tutorial)
head_mri_mne = import_trans(joinpath(bem_dir, "sample_audvis_raw-trans.fif");
                            from = :head, to = :tkr)
fwd_c = forward(joinpath(bem_dir, "sample-info.fif"),
                src,
                bem_sol,
                head_mri_mne)
NeuroAnalyzer FORWARD
  channels: 366 (grad 204, mag 102, eeg 60)
  sources: 2018 (lh, rh), free orientation
  gain: 366 × 6054
  frame: head
  file: files/bem/sample/sample-info.fif

MEG and EEG see different sources. For a dipole oriented along the normal of the cortex (the orientation of the pyramidal cells), the sensitivity of MEG decreases with the radial component of the orientation: radial sources (on the crowns of the gyri) produce almost no magnetic field outside the head, which is produced by tangential sources (in the walls of the sulci). The sensitivity of EEG hardly depends on the orientation:

# sensitivity to a dipole along the normal: gain projected on the normal
function sensitivity(fwd, type)
    ch = fwd.channel_type .== type
    n = size(fwd.src.pos, 1)
    [norm(fwd.gain[ch, (3i - 2):3i] * fwd.src.normals[i, :]) for i in 1:n]
end
# radial component: |normal · direction from the center of the sources|
c = vec(sum(fwd_c.src.pos; dims = 1)) ./ size(fwd_c.src.pos, 1)
rad = [abs(dot(fwd_c.src.normals[i, :],
               normalize(fwd_c.src.pos[i, :] .- c)))
       for i in axes(fwd_c.src.pos, 1)]
fig = CairoMakie.Figure(; size = (900, 400))
for (k, (t, ty)) in enumerate((("MEG (magnetometers)", "mag"), ("EEG", "eeg")))
    s = sensitivity(fwd_c, ty)
    ax = CairoMakie.Axis(fig[1, k];
                         title = t,
                         xlabel = "radial component of the orientation",
                         ylabel = "relative sensitivity")
    CairoMakie.scatter!(ax, rad, s ./ maximum(s);
                        markersize = 4, color = (:black, 0.4))
end
fig

The forward solution is the input of source localization: dipole fitting, minimum norm estimates and beamformers.

Dipole fitting

The forward model is used to fit equivalent current dipoles to EEG and MEG data, see the Dipole Fitting tutorial.