usingNeuroAnalyzerusingLinearAlgebraimportCairoMakie# Quarto sets a default figure size (672 × 480 px), which overrides the sizes of# NeuroAnalyzer figures: remove itdelete!(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:
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:
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:
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)
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 normalfunctionsensitivity(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 in1: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 inaxes(fwd_c.src.pos, 1)]fig = CairoMakie.Figure(; size = (900, 400))for (k, (t, ty)) inenumerate((("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))endfig
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.