Initialize NeuroAnalyzer
using NeuroAnalyzerusing NeuroAnalyzerMagnetoencephalography (MEG) is a non-invasive method that measures the magnetic fields produced by the electrical activity of the brain. The same neural currents that generate the EEG (the postsynaptic currents of synchronously active cortical pyramidal neurons) also produce magnetic fields outside the head. They are extremely weak (10–1000 fT, i.e. \(10^{-14}\)–\(10^{-12}\) T, about \(10^{-9}\)–\(10^{-8}\) of the Earth’s magnetic field) and are measured with very sensitive sensors inside a magnetically shielded room.
MEG has the millisecond temporal resolution of EEG, and because magnetic fields pass the skull and scalp almost undistorted, a better spatial resolution for superficial sources.
| Feature | Description |
|---|---|
| Signal source | intracellular (primary) currents of synchronously active pyramidal neurons, mainly postsynaptic dendritic currents |
| Temporal resolution | milliseconds (as EEG) |
| Spatial resolution | a few mm for superficial sources at high signal-to-noise ratio; worse for deep sources (depends on depth, SNR and the inverse method) |
| Invasiveness | non-invasive, no electrodes on the head |
| Sensors | SQUIDs (cooled with liquid helium) or optically pumped magnetometers |
| Environment | magnetically shielded room |
Sources
Maxwell’s equations
At the frequencies of brain activity, the electromagnetic fields can be treated as quasi-static (the time derivatives in Maxwell’s equations are negligible), so the magnetic field is determined by the currents through the Biot-Savart law. The magnetic field \(\mathbf{B}\) at the point \(\mathbf{r}\) produced by a current dipole \(\mathbf{Q}\) (primary current only, in an infinite homogeneous medium) is:
\[ \mathbf{B}(\mathbf{r}) = \frac{\mu_0}{4\pi} \frac{\mathbf{Q} \times \mathbf{R}}{|\mathbf{R}|^3} \]
where \(\mu_0\) is the permeability of free space, \(\mathbf{Q}\) the current dipole moment and \(\mathbf{R}\) the vector from the dipole to the measurement point. In a real head, the volume (return) currents also contribute; for a spherically symmetric conductor the field outside the head is given by the Sarvas formula, in which radial dipoles produce no field and the conductivities of the tissues do not matter.
SQUIDs (superconducting quantum interference devices) are the most sensitive magnetic field detectors. They operate at the temperature of liquid helium (about 4 K, −269 °C), so the sensors are placed in a dewar (a vacuum-insulated vessel), 2–4 cm from the scalp.
System components:
| Feature | MEG | EEG |
|---|---|---|
| Measures | magnetic fields | electric potential differences |
| Generators | synchronous postsynaptic currents of pyramidal neurons | the same |
| Sensitive to | tangential, superficial sources | radial and tangential sources, also deeper ones |
| Effect of the skull | small (fields pass almost undistorted) | strong (low skull conductivity smears the potentials) |
| Reference | not needed (magnetometers measure the field directly) | every channel depends on the reference |
| Temporal resolution | milliseconds | milliseconds |
| Spatial resolution | better for superficial tangential sources | lower |
| Sensor placement | fixed helmet, 2–4 cm from the scalp (OPMs: on the scalp); the head position must be monitored | electrodes on the scalp, move with the head |
| Subjects | must stay still; fixed helmet size (a problem for children) | flexible, portable, long recordings possible |
| Artifacts | environmental magnetic noise, magnetic objects (implants, dental work), heart, eyes, muscles | electrode and skin artifacts, eyes, muscles, heart |
| Cost | high (helium, shielded room) | low |
MEG and EEG are complementary: they measure different aspects of the same neural currents, and their combination improves source localization.
Comparison with other methods:
| Method | Temporal resolution | Spatial resolution | Invasiveness | Cost | Measures |
|---|---|---|---|---|---|
| MEG | milliseconds | 2–5 mm (superficial sources) | non-invasive | high | neural currents (magnetic fields) |
| EEG | milliseconds | 5–10 mm or worse | non-invasive | low | neural currents (potentials) |
| fMRI | seconds | 1–3 mm | non-invasive | high | blood oxygenation (BOLD) |
| PET | minutes | 4–6 mm | radioactive tracer | high | metabolism, receptors |
| DTI | (structural) | 1–2 mm | non-invasive | high | white matter tracts |
| Application | Description |
|---|---|
| Epilepsy | localizing interictal epileptic activity for surgical planning |
| Presurgical functional mapping | localizing eloquent cortex (somatosensory, motor, auditory, visual, language areas) |
| Cognitive neuroscience | perception, attention, memory, language, decision-making |
| Oscillations and connectivity | brain rhythms and networks in source space |
| Clinical research | autism, ADHD, schizophrenia, dementia, Parkinson’s disease, traumatic brain injury |
| Brain-computer interfaces | real-time decoding (e.g. motor imagery) |
| Pharmacology | effects of drugs on brain activity |
Limitations: high cost and limited availability, sensitivity to magnetic noise and magnetic objects, the requirement to keep the head still (or to track and compensate its movements), low sensitivity to radial and deep sources, and the inverse problem of source localization.
Developments: on-scalp MEG with OPMs (wearable, closer to the brain), high-Tc SQUIDs cooled with liquid nitrogen, helium recycling, and integration with other modalities (EEG, MRI, brain stimulation).
Preprocessing:
Source localization: estimating the location, orientation and strength of the neural sources:
Analysis: evoked responses (event-related fields, ERFs, see ERPs/ERFs), power spectra and event-related synchronization/desynchronization (ERS/ERD), functional connectivity (e.g. coherence, imaginary coherence, phase-locking value) and effective connectivity (e.g. Granger causality), preferably in source space.
import_fiff() reads MEGIN (Elekta Neuromag) FIFF raw files (Vectorview, TRIUX, MaxFilter-processed and MNE-Python-saved files, including split files *_raw.fif, *_raw-1.fif, …). Signals are calibrated and converted to fT (magnetometers), fT/cm (gradiometers) and μV (EEG/EOG/EMG).
A 32-second TRIUX recording:
meg = import_fiff("files/meg/meg.fif")
info(meg) Data type: MEG
File format: FIFF
Source file: files/meg/meg.fif
File size [MB]: 40.0
Memory size [MB]: 80.41
Subject: 2: Subject Subject
Recording: TRIUX system
Recording notes:
Recording date: 26-9-2016
Recording time: 15:25:51
Sampling rate (Hz): 1000
Signal length [samples]: 32000
Signal length [seconds]: 32.0
Number of channels: 327
Epochs ID:
Number of epochs: 1
Epoch length [samples]: 32000
Epoch length [seconds]: 32.0
Line frequency: 50.0 Hz
Markers: yes
Channel locations: yes
Number of MAG channels: 102
Number of GRAD channels: 204
Number of EEG channels: 0
Channels:
ch label type unit bad
------ --------------- ----------- ------- -------
1 IASX+ OTHER V false
2 IASX- OTHER V false
3 IASY+ OTHER V false
4 IASY- OTHER V false
5 IASZ+ OTHER V false
6 IASZ- OTHER V false
7 IAS_DX OTHER V false
8 IAS_DY OTHER V false
9 IAS_X OTHER V false
10 IAS_Y OTHER V false
11 IAS_Z OTHER V false
12 MEG 0111 MAG fT false
13 MEG 0112 GRAD fT/cm false
14 MEG 0113 GRAD fT/cm false
15 MEG 0121 MAG fT false
16 MEG 0122 GRAD fT/cm false
17 MEG 0123 GRAD fT/cm false
18 MEG 0131 MAG fT false
19 MEG 0132 GRAD fT/cm false
20 MEG 0133 GRAD fT/cm false
21 MEG 0141 MAG fT false
22 MEG 0142 GRAD fT/cm false
23 MEG 0143 GRAD fT/cm false
24 MEG 0211 MAG fT false
25 MEG 0212 GRAD fT/cm false
26 MEG 0213 GRAD fT/cm false
27 MEG 0221 MAG fT false
28 MEG 0222 GRAD fT/cm false
29 MEG 0223 GRAD fT/cm false
30 MEG 0231 MAG fT false
31 MEG 0232 GRAD fT/cm false
32 MEG 0233 GRAD fT/cm false
33 MEG 0241 MAG fT false
34 MEG 0242 GRAD fT/cm false
35 MEG 0243 GRAD fT/cm false
36 MEG 0311 MAG fT false
37 MEG 0312 GRAD fT/cm false
38 MEG 0313 GRAD fT/cm false
39 MEG 0321 MAG fT false
40 MEG 0322 GRAD fT/cm false
41 MEG 0323 GRAD fT/cm false
42 MEG 0331 MAG fT false
43 MEG 0332 GRAD fT/cm false
44 MEG 0333 GRAD fT/cm false
45 MEG 0341 MAG fT false
46 MEG 0342 GRAD fT/cm false
47 MEG 0343 GRAD fT/cm false
48 MEG 0411 MAG fT false
49 MEG 0412 GRAD fT/cm false
50 MEG 0413 GRAD fT/cm false
51 MEG 0421 MAG fT false
52 MEG 0422 GRAD fT/cm false
53 MEG 0423 GRAD fT/cm false
54 MEG 0431 MAG fT false
55 MEG 0432 GRAD fT/cm false
56 MEG 0433 GRAD fT/cm false
57 MEG 0441 MAG fT false
58 MEG 0442 GRAD fT/cm false
59 MEG 0443 GRAD fT/cm false
60 MEG 0511 MAG fT false
61 MEG 0512 GRAD fT/cm false
62 MEG 0513 GRAD fT/cm false
63 MEG 0521 MAG fT false
64 MEG 0522 GRAD fT/cm false
65 MEG 0523 GRAD fT/cm false
66 MEG 0531 MAG fT false
67 MEG 0532 GRAD fT/cm false
68 MEG 0533 GRAD fT/cm false
69 MEG 0541 MAG fT false
70 MEG 0542 GRAD fT/cm false
71 MEG 0543 GRAD fT/cm false
72 MEG 0611 MAG fT false
73 MEG 0612 GRAD fT/cm false
74 MEG 0613 GRAD fT/cm false
75 MEG 0621 MAG fT false
76 MEG 0622 GRAD fT/cm false
77 MEG 0623 GRAD fT/cm false
78 MEG 0631 MAG fT false
79 MEG 0632 GRAD fT/cm false
80 MEG 0633 GRAD fT/cm false
81 MEG 0641 MAG fT false
82 MEG 0642 GRAD fT/cm false
83 MEG 0643 GRAD fT/cm false
84 MEG 0711 MAG fT false
85 MEG 0712 GRAD fT/cm false
86 MEG 0713 GRAD fT/cm false
87 MEG 0721 MAG fT false
88 MEG 0722 GRAD fT/cm false
89 MEG 0723 GRAD fT/cm false
90 MEG 0731 MAG fT false
91 MEG 0732 GRAD fT/cm false
92 MEG 0733 GRAD fT/cm false
93 MEG 0741 MAG fT false
94 MEG 0742 GRAD fT/cm false
95 MEG 0743 GRAD fT/cm false
96 MEG 0811 MAG fT false
97 MEG 0812 GRAD fT/cm false
98 MEG 0813 GRAD fT/cm false
99 MEG 0821 MAG fT false
100 MEG 0822 GRAD fT/cm false
101 MEG 0823 GRAD fT/cm false
102 MEG 0911 MAG fT false
103 MEG 0912 GRAD fT/cm false
104 MEG 0913 GRAD fT/cm false
105 MEG 0921 MAG fT false
106 MEG 0922 GRAD fT/cm false
107 MEG 0923 GRAD fT/cm false
108 MEG 0931 MAG fT false
109 MEG 0932 GRAD fT/cm false
110 MEG 0933 GRAD fT/cm false
111 MEG 0941 MAG fT false
112 MEG 0942 GRAD fT/cm false
113 MEG 0943 GRAD fT/cm false
114 MEG 1011 MAG fT false
115 MEG 1012 GRAD fT/cm false
116 MEG 1013 GRAD fT/cm false
117 MEG 1021 MAG fT false
118 MEG 1022 GRAD fT/cm false
119 MEG 1023 GRAD fT/cm false
120 MEG 1031 MAG fT false
121 MEG 1032 GRAD fT/cm false
122 MEG 1033 GRAD fT/cm false
123 MEG 1041 MAG fT false
124 MEG 1042 GRAD fT/cm false
125 MEG 1043 GRAD fT/cm false
126 MEG 1111 MAG fT false
127 MEG 1112 GRAD fT/cm false
128 MEG 1113 GRAD fT/cm false
129 MEG 1121 MAG fT false
130 MEG 1122 GRAD fT/cm false
131 MEG 1123 GRAD fT/cm false
132 MEG 1131 MAG fT false
133 MEG 1132 GRAD fT/cm false
134 MEG 1133 GRAD fT/cm false
135 MEG 1141 MAG fT false
136 MEG 1142 GRAD fT/cm false
137 MEG 1143 GRAD fT/cm false
138 MEG 1211 MAG fT false
139 MEG 1212 GRAD fT/cm false
140 MEG 1213 GRAD fT/cm false
141 MEG 1221 MAG fT false
142 MEG 1222 GRAD fT/cm false
143 MEG 1223 GRAD fT/cm false
144 MEG 1231 MAG fT false
145 MEG 1232 GRAD fT/cm false
146 MEG 1233 GRAD fT/cm false
147 MEG 1241 MAG fT false
148 MEG 1242 GRAD fT/cm false
149 MEG 1243 GRAD fT/cm false
150 MEG 1311 MAG fT false
151 MEG 1312 GRAD fT/cm false
152 MEG 1313 GRAD fT/cm false
153 MEG 1321 MAG fT false
154 MEG 1322 GRAD fT/cm false
155 MEG 1323 GRAD fT/cm false
156 MEG 1331 MAG fT false
157 MEG 1332 GRAD fT/cm false
158 MEG 1333 GRAD fT/cm false
159 MEG 1341 MAG fT false
160 MEG 1342 GRAD fT/cm false
161 MEG 1343 GRAD fT/cm false
162 MEG 1411 MAG fT false
163 MEG 1412 GRAD fT/cm false
164 MEG 1413 GRAD fT/cm false
165 MEG 1421 MAG fT false
166 MEG 1422 GRAD fT/cm false
167 MEG 1423 GRAD fT/cm false
168 MEG 1431 MAG fT false
169 MEG 1432 GRAD fT/cm false
170 MEG 1433 GRAD fT/cm false
171 MEG 1441 MAG fT false
172 MEG 1442 GRAD fT/cm false
173 MEG 1443 GRAD fT/cm false
174 MEG 1511 MAG fT false
175 MEG 1512 GRAD fT/cm false
176 MEG 1513 GRAD fT/cm false
177 MEG 1521 MAG fT false
178 MEG 1522 GRAD fT/cm false
179 MEG 1523 GRAD fT/cm false
180 MEG 1531 MAG fT false
181 MEG 1532 GRAD fT/cm false
182 MEG 1533 GRAD fT/cm false
183 MEG 1541 MAG fT false
184 MEG 1542 GRAD fT/cm false
185 MEG 1543 GRAD fT/cm false
186 MEG 1611 MAG fT false
187 MEG 1612 GRAD fT/cm false
188 MEG 1613 GRAD fT/cm false
189 MEG 1621 MAG fT false
190 MEG 1622 GRAD fT/cm false
191 MEG 1623 GRAD fT/cm false
192 MEG 1631 MAG fT false
193 MEG 1632 GRAD fT/cm false
194 MEG 1633 GRAD fT/cm false
195 MEG 1641 MAG fT false
196 MEG 1642 GRAD fT/cm false
197 MEG 1643 GRAD fT/cm false
198 MEG 1711 MAG fT false
199 MEG 1712 GRAD fT/cm false
200 MEG 1713 GRAD fT/cm false
201 MEG 1721 MAG fT false
202 MEG 1722 GRAD fT/cm false
203 MEG 1723 GRAD fT/cm false
204 MEG 1731 MAG fT false
205 MEG 1732 GRAD fT/cm false
206 MEG 1733 GRAD fT/cm false
207 MEG 1741 MAG fT false
208 MEG 1742 GRAD fT/cm false
209 MEG 1743 GRAD fT/cm false
210 MEG 1811 MAG fT false
211 MEG 1812 GRAD fT/cm false
212 MEG 1813 GRAD fT/cm false
213 MEG 1821 MAG fT false
214 MEG 1822 GRAD fT/cm false
215 MEG 1823 GRAD fT/cm false
216 MEG 1831 MAG fT false
217 MEG 1832 GRAD fT/cm false
218 MEG 1833 GRAD fT/cm false
219 MEG 1841 MAG fT false
220 MEG 1842 GRAD fT/cm false
221 MEG 1843 GRAD fT/cm false
222 MEG 1911 MAG fT false
223 MEG 1912 GRAD fT/cm false
224 MEG 1913 GRAD fT/cm false
225 MEG 1921 MAG fT false
226 MEG 1922 GRAD fT/cm false
227 MEG 1923 GRAD fT/cm false
228 MEG 1931 MAG fT false
229 MEG 1932 GRAD fT/cm false
230 MEG 1933 GRAD fT/cm false
231 MEG 1941 MAG fT false
232 MEG 1942 GRAD fT/cm false
233 MEG 1943 GRAD fT/cm false
234 MEG 2011 MAG fT false
235 MEG 2012 GRAD fT/cm false
236 MEG 2013 GRAD fT/cm false
237 MEG 2021 MAG fT false
238 MEG 2022 GRAD fT/cm false
239 MEG 2023 GRAD fT/cm false
240 MEG 2031 MAG fT false
241 MEG 2032 GRAD fT/cm false
242 MEG 2033 GRAD fT/cm false
243 MEG 2041 MAG fT false
244 MEG 2042 GRAD fT/cm false
245 MEG 2043 GRAD fT/cm false
246 MEG 2111 MAG fT false
247 MEG 2112 GRAD fT/cm false
248 MEG 2113 GRAD fT/cm false
249 MEG 2121 MAG fT false
250 MEG 2122 GRAD fT/cm false
251 MEG 2123 GRAD fT/cm false
252 MEG 2131 MAG fT false
253 MEG 2132 GRAD fT/cm false
254 MEG 2133 GRAD fT/cm false
255 MEG 2141 MAG fT false
256 MEG 2142 GRAD fT/cm false
257 MEG 2143 GRAD fT/cm false
258 MEG 2211 MAG fT false
259 MEG 2212 GRAD fT/cm false
260 MEG 2213 GRAD fT/cm false
261 MEG 2221 MAG fT false
262 MEG 2222 GRAD fT/cm false
263 MEG 2223 GRAD fT/cm false
264 MEG 2231 MAG fT false
265 MEG 2232 GRAD fT/cm false
266 MEG 2233 GRAD fT/cm false
267 MEG 2241 MAG fT false
268 MEG 2242 GRAD fT/cm false
269 MEG 2243 GRAD fT/cm false
270 MEG 2311 MAG fT false
271 MEG 2312 GRAD fT/cm false
272 MEG 2313 GRAD fT/cm false
273 MEG 2321 MAG fT false
274 MEG 2322 GRAD fT/cm false
275 MEG 2323 GRAD fT/cm false
276 MEG 2331 MAG fT false
277 MEG 2332 GRAD fT/cm false
278 MEG 2333 GRAD fT/cm false
279 MEG 2341 MAG fT false
280 MEG 2342 GRAD fT/cm false
281 MEG 2343 GRAD fT/cm false
282 MEG 2411 MAG fT false
283 MEG 2412 GRAD fT/cm false
284 MEG 2413 GRAD fT/cm false
285 MEG 2421 MAG fT false
286 MEG 2422 GRAD fT/cm false
287 MEG 2423 GRAD fT/cm false
288 MEG 2431 MAG fT false
289 MEG 2432 GRAD fT/cm false
290 MEG 2433 GRAD fT/cm false
291 MEG 2441 MAG fT false
292 MEG 2442 GRAD fT/cm false
293 MEG 2443 GRAD fT/cm false
294 MEG 2511 MAG fT false
295 MEG 2512 GRAD fT/cm false
296 MEG 2513 GRAD fT/cm false
297 MEG 2521 MAG fT false
298 MEG 2522 GRAD fT/cm false
299 MEG 2523 GRAD fT/cm false
300 MEG 2531 MAG fT false
301 MEG 2532 GRAD fT/cm false
302 MEG 2533 GRAD fT/cm false
303 MEG 2541 MAG fT false
304 MEG 2542 GRAD fT/cm false
305 MEG 2543 GRAD fT/cm false
306 MEG 2611 MAG fT false
307 MEG 2612 GRAD fT/cm false
308 MEG 2613 GRAD fT/cm false
309 MEG 2621 MAG fT false
310 MEG 2622 GRAD fT/cm false
311 MEG 2623 GRAD fT/cm false
312 MEG 2631 MAG fT false
313 MEG 2632 GRAD fT/cm false
314 MEG 2633 GRAD fT/cm false
315 MEG 2641 MAG fT false
316 MEG 2642 GRAD fT/cm false
317 MEG 2643 GRAD fT/cm false
318 STI 001 MRK V false
319 STI 002 MRK V false
320 STI 003 MRK V false
321 STI 004 MRK V false
322 STI 005 MRK V false
323 STI 006 MRK V false
324 STI 007 MRK V false
325 STI 008 MRK V false
326 STI 101 MRK V false
327 SYS 201 OTHER false
Options:
allow_maxshield = true: import raw data recorded with internal active shielding (see below; refused by default).channel_locs = :fiff (default): sensor/electrode positions from the file; :layout: bundled flattened 306-channel layout; :none: no locations.Magnetometers and gradiometers channel indices:
first(meg.header.recording[:magnetometers], 5)5-element Vector{Int64}:
12
15
18
21
24
first(meg.header.recording[:gradiometers], 5)5-element Vector{Int64}:
13
14
16
17
19
Bad channels stored in the file (MNE-Python convention) are marked on import (none in this recording):
labels(meg)[meg.header.recording[:bad_channel]]String[]
MEG is usually recorded at 1–5 kHz (1000 Hz in this recording); the data are often downsampled after filtering.
The channels STI001, STI002, …, STI016 are binary channels: each records one TTL line of the trigger interface, switching between 0 and 1.
The channels STI101 and STI102 are combined channels: they record the 16 binary lines of their trigger interface at once, as a decimal number. For example, if lines 1, 3 and 4 are high, the binary number is 1101 (from left to right: line 4 high, line 3 high, line 2 low, line 1 high), and the combined channel records the value 13. import_fiff() reads the events from the combined channel into meg.markers.
Before each recording, the positions of the HPI coils (head position indicator; usually 3–5 coils attached to the head and digitized together with the head shape) are measured: a current with a known frequency is sent to each coil and its position is estimated from the magnetic field measured by the MEG sensors. This gives the position of the head relative to the sensors (the device → head transform). With continuous HPI (cHPI), the coils are active during the whole recording, so the head position can be tracked over time and head movements compensated (see Movement compensation).
The MEG header keeps the full sensor geometry needed for SSS, movement compensation and source modelling:
| Field | Content |
|---|---|
:coil_loc |
one row per channel: coil center r0 (m) and coil frame ex, ey, ez (ez = coil normal); MEG in the device frame, EEG in the head frame |
:coil_id |
FIFF coil type code (selects coil integration points) |
:dev_head_t |
4 × 4 device → head transform (empty for empty-room data) |
:head_origin |
center of the sphere fitted to the head-shape digitization (head frame, m) |
:hpi |
cHPI coil frequencies (:freqs) and digitized coil positions (:dig) |
r0, ex, ey, ez of the first channel:
meg.header.recording[:coil_loc][1, :]12-element Vector{Float64}:
0.0
0.0
0.0
1.0
0.0
0.0
0.0
1.0
0.0
0.0
0.0
1.0
Head transform:
meg.header.recording[:dev_head_t]0×0 Matrix{Float64}
Channel locations (meg.locs) are taken from the file. MEG sensors are transformed to the head frame (RAS: x → right, y → anterior, z → superior). With at least 10 head-shape points, coordinates are centered on the fitted head sphere and divided by its radius, so EEG electrodes lie at radius ≈ 1 and MEG sensors at their true distance from the head. Otherwise the outermost MEG sensor is placed at radius 1.2 and the outermost EEG electrode at 1.0.
first(meg.locs, 5)| Row | label | loc_radius | loc_theta | loc_x | loc_y | loc_z | loc_radius_sph | loc_theta_sph | loc_phi_sph |
|---|---|---|---|---|---|---|---|---|---|
| String | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
| 1 | MEG 0111 | 0.982558 | 163.548 | -0.942328 | 0.278278 | -0.434999 | 1.07454 | 163.548 | -23.88 |
| 2 | MEG 0112 | 0.982558 | 163.548 | -0.942328 | 0.278278 | -0.434999 | 1.07454 | 163.548 | -23.88 |
| 3 | MEG 0113 | 0.982558 | 163.548 | -0.942328 | 0.278278 | -0.434999 | 1.07454 | 163.548 | -23.88 |
| 4 | MEG 0121 | 0.997199 | 154.714 | -0.901655 | 0.42594 | -0.127296 | 1.00529 | 154.714 | -7.27467 |
| 5 | MEG 0122 | 0.997199 | 154.714 | -0.901655 | 0.42594 | -0.127296 | 1.00529 | 154.714 | -7.27467 |
Locations can also be read without loading the data, from any FIFF file with measurement info, e.g. from an evoked (averaged) file:
locs = import_locs_fiff("files/meg/SSS/sample_audvis-ave.fif")
first(locs, 5)| Row | label | loc_radius | loc_theta | loc_x | loc_y | loc_z | loc_radius_sph | loc_theta_sph | loc_phi_sph |
|---|---|---|---|---|---|---|---|---|---|
| String | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
| 1 | MEG 0113 | 1.13475 | 171.68 | -1.1228 | 0.164209 | -0.74014 | 1.35479 | 171.68 | -33.1144 |
| 2 | MEG 0112 | 1.13475 | 171.68 | -1.1228 | 0.164209 | -0.74014 | 1.35479 | 171.68 | -33.1144 |
| 3 | MEG 0111 | 1.13475 | 171.68 | -1.1228 | 0.164209 | -0.74014 | 1.35479 | 171.68 | -33.1144 |
| 4 | MEG 0122 | 1.20002 | 159.992 | -1.12759 | 0.410583 | -0.393983 | 1.26304 | 159.992 | -18.1758 |
| 5 | MEG 0123 | 1.20002 | 159.992 | -1.12759 | 0.410583 | -0.393983 | 1.26304 | 159.992 | -18.1758 |
In a Vectorview/TRIUX helmet each magnetometer and its two planar gradiometers share one position, so three rows of locs have identical coordinates.
Plotting signal:
NeuroAnalyzer.plot(meg; ch = "all", gui = false)Plotting PSD of gradiometers:
plot_psd(meg; ch = "grad", gui = false)Plotting PSD topomap of magnetometers:
plot_psd(meg; ch = "mag", type = :topo, gui = false)delete_channel(), keep_channel(), add_channel(), rename_channel() and related functions keep all per-channel MEG header fields consistent: magnetometer/gradiometer lists, SSP channel mask and projection vectors, sensor geometry (:coil_loc, :coil_id) and locations.
mag = labels(meg)[meg.header.recording[:magnetometers]]
meg_mag = keep_channel(meg; ch = mag)When a deleted channel is part of an SSP projector, its weights are removed from the projection vectors (a warning is shown).
Brain signals measured with MEG are tiny (tens to hundreds of fT), while the environment produces fields that are many orders of magnitude larger: the Earth’s field, building vibrations, elevators, traffic, power lines. A magnetically shielded room removes most of it, but what remains is still often larger than the brain signal. In addition, there are interference sources close to the sensors: dental work, implants, eye and heart activity, and the system’s own internal active shielding.
Three related techniques remove such interference:
| Method | Main idea | Uses | Requires |
|---|---|---|---|
| SSP: signal-space projection | remove known noise spatial patterns | empty-room noise, ECG/EOG artifacts | noise patterns (projection vectors) |
| SSS: signal-space separation | separate fields from sources inside vs. outside the sensor helmet using physics | environmental interference, bad channel reconstruction, head movement | accurate sensor geometry and calibration |
| tSSS: temporal (spatiotemporal) SSS | SSS + remove signals that appear in both the inside and the outside parts | interference near the head (implants, dental work, stimulators) | as SSS |
SSS and tSSS are also called Maxwell filtering (they follow from Maxwell’s equations).
Status in NeuroAnalyzer:
generate_ssp_projectors(), apply_ssp_projectors()).maxwell_filter(), chpi_head_positions()) are available for MEGIN Vectorview/TRIUX data.import_fiff().Signal-space projection (SSP) estimates a projection matrix that removes noise from a recording, using the spatial patterns of the noise measured without the signal of interest.
At any time point, the MEG measurement is a vector b with one value per channel. A source with a fixed position and orientation always produces the same spatial pattern across channels; only its amplitude changes over time. If we know the patterns of the noise sources, we can remove everything that lies along those patterns.
Let the noise patterns be the columns of a matrix. After orthonormalization (by SVD) they form a basis U of the noise subspace. The SSP operator is
\[ P = I - U U^T \]
and the cleaned data are \(b_{clean} = P b\). \(P\) is an orthogonal projector: it removes the components of the signal lying in the noise subspace and leaves everything orthogonal to it untouched.
Where do the noise vectors come from?
PCA-v1).Limitations:
import_fiff() reads the projection vectors stored in a FIFF file into the recording header. The TRIUX recording above has no projectors; this 24-second Vectorview recording (MGH) has three empty-room PCA projectors:
raw = import_fiff("files/meg/test_raw.fif")raw.header.recording[:ssp_labels]3-element Vector{String}:
"PCA-v1"
"PCA-v2"
"PCA-v3"
Number of channels covered by projectors:
count(raw.header.recording[:ssp_channels])102
Size of the projection vectors (projections × channels):
size(raw.header.recording[:ssp_data])(3, 102)
The projector and the number of independent projection vectors (the rank removed from the data):
p = generate_ssp_projectors(raw)
size(p.U, 2)3
Applying the projector (all projections or selected ones):
raw_ssp = apply_ssp_projectors(raw)
raw_ssp12 = apply_ssp_projectors(raw; pidx = [1, 2])apply_ssp_projectors!(raw) modifies the object in place.
The implementation follows MNE-Python:
Mark bad channels before generating projectors. SSP is refused for MaxShield data and should not be applied after SSS (Maxwell filtering removes the projectors stored in the file).
The MEG sensors sit in a thin shell around the head, between the head surface and the helmet’s outer surface. In that shell there are no currents. At the frequencies of interest (quasi-static approximation), Maxwell’s equations then say that the magnetic field can be written as the gradient of a scalar potential that satisfies Laplace’s equation. The solutions of Laplace’s equation in spherical coordinates are spherical harmonics, and they split naturally into two families:
1/r^(l+1) away from the expansion origin. They describe fields from sources inside the inner sphere, i.e. the brain.r^l. They describe fields from sources outside the outer sphere, i.e. the environment.Any field in the sensor shell is the sum of an internal and an external part:
\[ b = S_{in} x_{in} + S_{out} x_{out} \]
S_in and S_out are computed from the sensor geometry alone (positions, orientations, coil shapes). The coefficients x_in, x_out are estimated from the data by least squares. The cleaned signal is reconstructed from the internal part only:
\[ b_{SSS} = S_{in} x_{in} \]
Unlike SSP, this needs no noise recording and makes no assumption about the noise pattern: anything consistent with an external source is removed. Mathematically it is an oblique projection, not an orthogonal one.
Expansion order
The expansions are truncated:
\(80 + 15 = 95\) components are fitted to the ~306 sensor values of a Vectorview/TRIUX system. In practice regularization removes some weak internal components, so the data after SSS have a rank of roughly 70 instead of ~306. Rank-dependent analyses (ICA, beamformers, noise covariance estimation) must take this into account.
What SSS needs
sss_cal.dat) with corrected sensor orientations and gains, and a cross-talk file (ct_sparse.fif) describing coupling between the sensors of each triplet. Without them, suppression is clearly weaker.Extra benefits
x_in describes the sources independently of the sensors, the internal field can be recomputed for a fixed reference head position. This corrects for head movement during the recording.Limitation: nearby interference
SSS separates inside from outside the two spheres. A source that sits between the inner sphere and the sensors, or very close to the head (dental work, implants, a magnetised object on the skin), does not fit cleanly into either expansion. Part of its field leaks into the internal component and survives SSS. This is the problem tSSS addresses.
An artifact source close to the sensors leaks into both the internal and the external reconstructions, and the leaked parts share the same time course. Genuine brain activity appears only in the internal part.
Temporal (spatiotemporal) SSS (tSSS) therefore:
Parameters
st_duration). Longer windows estimate the correlation more reliably and act as a high-pass filter at about 1 / st_duration Hz. 10 s is common (MaxFilter’s default); 4–10 s are used in practice. The buffer must be shorter than the recording, and ideally the recording length should be a multiple of the buffer.st_correlation). Lower values remove more; 0.98 is the usual default, 0.9 for strong artifacts.When to use it
Use tSSS when there is interference near the head: magnetic implants, dental fillings or braces, deep brain stimulators, vagus nerve stimulators, or strong muscle artifacts. For clean data, SSS alone is often enough, and tSSS adds only a slight risk of removing brain activity that happens to correlate with external signals.
Many MEGIN systems can run with internal active shielding (IAS, MaxShield): compensation coils in the walls, floor and ceiling of the shielded room are driven by a feedback loop to cancel low-frequency environmental fields in real time. The coils can compensate the homogeneous fields \(B_x\), \(B_y\), \(B_z\) and the diagonal gradients \(\partial B_x / \partial x\), \(\partial B_y / \partial y\), \(\partial B_z / \partial z\); the feedback drives selected magnetometers of the helmet (on its right, left, front, rear and top) towards zero field.
The IAS channels can be recorded:
IASX-, IASX+, IASY-, IASY+, IASZ-, IASZ+IAS_X, IAS_Y, IAS_Z, IAS_DX, IAS_DYThe compensation coils produce fields of their own close to the helmet, so data recorded with IAS on (stored in maxshield_raw_data blocks) are distorted and not suitable for analysis without SSS. SSS removes the compensation fields together with the rest of the external interference.
NeuroAnalyzer refuses such files by default:
obj = import_fiff("ias_on_raw.fif")Gives error: ... contains raw MaxShield data (internal active shielding was on) ...
To import anyway, e.g. for inspection:
obj = import_fiff("ias_on_raw.fif"; allow_maxshield = true)
obj.header.recording[:maxshield] # trueObjects flagged with :maxshield => true are rejected by generate_ssp_projectors() and apply_ssp_projectors(), because SSP cannot correct IAS distortion.
A typical Neuromag/MEGIN pipeline:
Rules of thumb:
maxwell_filter() and chpi_head_positions() support MEGIN Vectorview and TRIUX data imported with import_fiff() (they need the sensor geometry, head origin and cHPI information stored at import). They are new; compare results with MaxFilter or MNE-Python before relying on them.
Calibration files
The fine-calibration (sss_cal.dat) and cross-talk (ct_sparse.fif) files are specific to each MEG system: they are provided with the system (in a MaxFilter installation, usually /neuro/databases/sss/sss_cal.dat and /neuro/databases/ctc/ct_sparse.fif) and must be used only with data recorded on that system. The recording test_raw.fif comes from the Vectorview system at MGH, whose files are:
sss_cal_file = "files/meg/SSS/sss_cal_mgh.dat"
cross_talk_file = "files/meg/SSS/ct_sparse_mgh.fif"1. Mark bad channels
Bad channels are excluded from the expansion and reconstructed by it, so they must be marked first (noisy, flat or jumping channels found by inspection; automatic detection is not yet available). For example:
bad = get_channel(raw; ch = ["MEG 2443"])
raw.header.recording[:bad_channel][bad] .= true2. SSS
raw_sss = maxwell_filter(raw;
int_order = 8, # internal order
ext_order = 3, # external order
origin = :auto, # fitted head sphere
regularize = :in, # :none to disable
calibration = sss_cal_file, # fine calibration
cross_talk = cross_talk_file)If the head origin cannot be estimated (fewer than 10 digitization points), set it explicitly in meters, e.g. origin = [0.0, 0.0, 0.04].
Gradiometers before (blue) and after (black) SSS:
NeuroAnalyzer.plot(raw, raw_sss; ch = "grad", seg = (5, 10), gui = false)3. tSSS
Same function, with a temporal buffer:
raw_tsss = maxwell_filter(raw;
st_duration = 10.0, # buffer (s)
st_correlation = 0.98, # correlation limit
calibration = sss_cal_file,
cross_talk = cross_talk_file)Result
After Maxwell filtering:
:sss => true (and :tsss => true for tSSS) are set in the recording header;:sss_info holds the processing parameters, including the number of internal components used, which is the rank of the MEG data;:maxshield flag is cleared;:dev_head_t is set to the destination head position.The rank of the data after SSS (needed e.g. for ICA and covariance estimation):
raw_sss.header.recording[:sss_info][:rank]73
Maxwell filtering can be applied only once; data must be continuous (one epoch). Epoch the data after filtering. maxwell_filter!() modifies the object in place.
If cHPI coils were active during the recording, the header contains their frequencies and digitized positions. A 17-second Vectorview recording with five cHPI coils:
chpi = import_fiff("files/meg/SSS/chpi5_raw.fif")
chpi.header.recording[:hpi][:freqs]5-element Vector{Float64}:
261.3330078125
278.0
294.6669921875
311.3330078125
328.0
Estimate the head position over time (one row per window: time, rotation quaternion q1–q3, translation x, y, z in m of the device → head transform, goodness of fit, coil position error in m, velocity in m/s):
head_pos = chpi_head_positions(chpi;
t_window = 0.2, # window length (s)
t_step = 0.1, # step (s)
gof_limit = 0.98, # minimum goodness of fit
dist_limit = 0.005) # maximum coil error (m)Total head displacement relative to the start of the recording:
xyz = head_pos[:, 5:7]
d = 1000 .* sqrt.(sum((xyz .- xyz[1:1, :]) .^ 2; dims = 2))
println("maximum displacement: ", round(maximum(d); digits = 1), " mm")maximum displacement: 7.5 mm
Apply SSS/tSSS with movement compensation; the data are transformed to the initial head position (or to destination, a 4 × 4 device → head transform or a translation vector in m). The fine-calibration and cross-talk files of this system are not available, so they are omitted:
chpi_mc = maxwell_filter(chpi; head_pos = head_pos)An empty-room recording (without a subject) is usually measured before or after the experiment; it characterizes the environmental and sensor noise (e.g. for SSP projectors or for the noise covariance in source localization). An empty-room recording from the MGH system:
er = import_fiff("files/meg/empty_room_raw.fif")Gradiometers:
plot_psd(er; ch = "grad", flim = (0, 300), gui = false)Magnetometers:
plot_psd(er; ch = "mag", flim = (0, 300), gui = false)Without a head, the expansion is done in the device frame (coord_frame = :meg):
er_sss = maxwell_filter(er;
coord_frame = :meg,
calibration = sss_cal_file,
cross_talk = cross_talk_file)