Note
Go to the end to download the full example code.
Extracting and visualizing subject head movement#
Continuous head movement can be encoded during MEG recordings by use of HPI coils that continuously emit sinusoidal signals. These signals can then be extracted from the recording and used to estimate head position as a function of time. Here we show an example of how to do this, and how to visualize the result.
HPI frequencies#
First let’s load a short bit of raw data where the subject intentionally moved their head during the recording. Its power spectral density shows five peaks (most clearly visible in the gradiometers) corresponding to the HPI coil frequencies, plus other peaks related to power line interference (60 Hz and harmonics).
# Authors: Eric Larson <larson.eric.d@gmail.com>
# Richard Höchenberger <richard.hoechenberger@gmail.com>
# Daniel McCloy <dan@mccloy.info>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
from os import path as op
import mne
data_path = op.join(mne.datasets.testing.data_path(verbose=True), "SSS")
fname_raw = op.join(data_path, "test_move_anon_raw.fif")
raw = mne.io.read_raw_fif(fname_raw, allow_maxshield="yes").load_data()
raw.compute_psd().plot(picks="data", exclude="bads", amplitude=False)
Opening raw data file /home/circleci/mne_data/MNE-testing-data/SSS/test_move_anon_raw.fif...
Read a total of 12 projection items:
mag.fif : PCA-v1 (1 x 306) idle
mag.fif : PCA-v2 (1 x 306) idle
mag.fif : PCA-v3 (1 x 306) idle
mag.fif : PCA-v4 (1 x 306) idle
mag.fif : PCA-v5 (1 x 306) idle
mag.fif : PCA-v6 (1 x 306) idle
mag.fif : PCA-v7 (1 x 306) idle
grad.fif : PCA-v1 (1 x 306) idle
grad.fif : PCA-v2 (1 x 306) idle
grad.fif : PCA-v3 (1 x 306) idle
grad.fif : PCA-v4 (1 x 306) idle
Average EEG reference (1 x 60) idle
Range : 10800 ... 31199 = 9.000 ... 25.999 secs
Ready.
Reading 0 ... 20399 = 0.000 ... 16.999 secs...
Effective window size : 1.707 (s)
Plotting power spectral density (dB=True).
We can use mne.chpi.get_chpi_info
to retrieve the coil frequencies,
the index of the channel indicating when which coil was switched on, and the
respective “event codes” associated with each coil’s activity.
chpi_freqs, ch_idx, chpi_codes = mne.chpi.get_chpi_info(info=raw.info)
print(f"cHPI coil frequencies extracted from raw: {chpi_freqs} Hz")
Using 5 HPI coils: 83 143 203 263 323 Hz
cHPI coil frequencies extracted from raw: [ 83. 143. 203. 263. 323.] Hz
Estimating continuous head position#
First, let’s extract the HPI coil amplitudes as a function of time:
Using 5 HPI coils: 83 143 203 263 323 Hz
Line interference frequencies: 60.0 120.0 180.0 240.0 300.0 360.0 Hz
Using time window: 83.3 ms
Fitting 5 HPI coil locations at up to 1696 time points (17.0 s duration)
0%| | cHPI amplitudes : 0/1696 [00:00<?, ?it/s]
0%| | cHPI amplitudes : 1/1696 [00:05<2:22:56, 5.06s/it]
1%| | cHPI amplitudes : 13/1696 [00:05<10:26, 2.68it/s]
2%|▏ | cHPI amplitudes : 26/1696 [00:05<05:03, 5.50it/s]
2%|▏ | cHPI amplitudes : 39/1696 [00:05<03:16, 8.44it/s]
3%|▎ | cHPI amplitudes : 53/1696 [00:05<02:19, 11.75it/s]
4%|▍ | cHPI amplitudes : 66/1696 [00:05<01:48, 14.96it/s]
5%|▍ | cHPI amplitudes : 78/1696 [00:05<01:29, 18.05it/s]
5%|▌ | cHPI amplitudes : 87/1696 [00:05<01:18, 20.44it/s]
6%|▌ | cHPI amplitudes : 101/1696 [00:05<01:05, 24.38it/s]
7%|▋ | cHPI amplitudes : 115/1696 [00:05<00:55, 28.48it/s]
8%|▊ | cHPI amplitudes : 129/1696 [00:05<00:47, 32.75it/s]
8%|▊ | cHPI amplitudes : 142/1696 [00:05<00:42, 36.87it/s]
9%|▉ | cHPI amplitudes : 155/1696 [00:05<00:37, 41.15it/s]
10%|▉ | cHPI amplitudes : 168/1696 [00:05<00:33, 45.60it/s]
11%|█ | cHPI amplitudes : 180/1696 [00:05<00:30, 49.87it/s]
11%|█▏ | cHPI amplitudes : 193/1696 [00:05<00:27, 54.68it/s]
12%|█▏ | cHPI amplitudes : 206/1696 [00:05<00:24, 59.70it/s]
13%|█▎ | cHPI amplitudes : 220/1696 [00:05<00:22, 65.30it/s]
14%|█▎ | cHPI amplitudes : 233/1696 [00:05<00:20, 70.67it/s]
14%|█▍ | cHPI amplitudes : 244/1696 [00:05<00:19, 75.30it/s]
15%|█▌ | cHPI amplitudes : 257/1696 [00:05<00:17, 81.07it/s]
16%|█▌ | cHPI amplitudes : 270/1696 [00:05<00:16, 87.05it/s]
17%|█▋ | cHPI amplitudes : 284/1696 [00:05<00:15, 93.76it/s]
18%|█▊ | cHPI amplitudes : 298/1696 [00:05<00:13, 100.73it/s]
18%|█▊ | cHPI amplitudes : 312/1696 [00:05<00:12, 107.92it/s]
19%|█▉ | cHPI amplitudes : 325/1696 [00:05<00:11, 114.67it/s]
20%|█▉ | cHPI amplitudes : 338/1696 [00:05<00:11, 121.65it/s]
21%|██ | cHPI amplitudes : 350/1696 [00:05<00:10, 128.27it/s]
21%|██▏ | cHPI amplitudes : 363/1696 [00:05<00:09, 135.65it/s]
22%|██▏ | cHPI amplitudes : 375/1696 [00:05<00:09, 142.65it/s]
23%|██▎ | cHPI amplitudes : 388/1696 [00:05<00:08, 150.54it/s]
24%|██▍ | cHPI amplitudes : 403/1696 [00:05<00:08, 159.90it/s]
24%|██▍ | cHPI amplitudes : 415/1696 [00:05<00:07, 167.42it/s]
25%|██▌ | cHPI amplitudes : 425/1696 [00:05<00:07, 173.26it/s]
26%|██▌ | cHPI amplitudes : 439/1696 [00:05<00:06, 182.77it/s]
27%|██▋ | cHPI amplitudes : 453/1696 [00:05<00:06, 192.53it/s]
28%|██▊ | cHPI amplitudes : 467/1696 [00:05<00:06, 202.52it/s]
28%|██▊ | cHPI amplitudes : 479/1696 [00:05<00:05, 210.90it/s]
29%|██▉ | cHPI amplitudes : 492/1696 [00:05<00:05, 220.18it/s]
30%|██▉ | cHPI amplitudes : 505/1696 [00:05<00:05, 229.65it/s]
30%|███ | cHPI amplitudes : 517/1696 [00:05<00:04, 238.38it/s]
31%|███▏ | cHPI amplitudes : 530/1696 [00:05<00:04, 248.14it/s]
32%|███▏ | cHPI amplitudes : 543/1696 [00:05<00:04, 258.24it/s]
33%|███▎ | cHPI amplitudes : 557/1696 [00:05<00:04, 269.40it/s]
34%|███▎ | cHPI amplitudes : 569/1696 [00:05<00:04, 278.58it/s]
34%|███▍ | cHPI amplitudes : 578/1696 [00:05<00:03, 284.21it/s]
35%|███▍ | cHPI amplitudes : 591/1696 [00:05<00:03, 294.75it/s]
36%|███▌ | cHPI amplitudes : 605/1696 [00:05<00:03, 306.56it/s]
36%|███▋ | cHPI amplitudes : 618/1696 [00:05<00:03, 317.10it/s]
37%|███▋ | cHPI amplitudes : 632/1696 [00:05<00:03, 329.23it/s]
38%|███▊ | cHPI amplitudes : 646/1696 [00:05<00:03, 341.45it/s]
39%|███▉ | cHPI amplitudes : 659/1696 [00:05<00:02, 351.97it/s]
40%|███▉ | cHPI amplitudes : 672/1696 [00:05<00:02, 362.40it/s]
40%|████ | cHPI amplitudes : 685/1696 [00:05<00:02, 372.90it/s]
41%|████ | cHPI amplitudes : 697/1696 [00:05<00:02, 382.12it/s]
42%|████▏ | cHPI amplitudes : 710/1696 [00:05<00:02, 392.60it/s]
43%|████▎ | cHPI amplitudes : 723/1696 [00:06<00:02, 403.21it/s]
43%|████▎ | cHPI amplitudes : 737/1696 [00:06<00:02, 415.83it/s]
44%|████▍ | cHPI amplitudes : 749/1696 [00:06<00:02, 424.14it/s]
45%|████▍ | cHPI amplitudes : 759/1696 [00:06<00:02, 429.57it/s]
45%|████▌ | cHPI amplitudes : 771/1696 [00:06<00:02, 438.57it/s]
46%|████▋ | cHPI amplitudes : 785/1696 [00:06<00:02, 450.93it/s]
47%|████▋ | cHPI amplitudes : 799/1696 [00:06<00:01, 463.24it/s]
48%|████▊ | cHPI amplitudes : 812/1696 [00:06<00:01, 473.83it/s]
49%|████▊ | cHPI amplitudes : 824/1696 [00:06<00:01, 482.45it/s]
49%|████▉ | cHPI amplitudes : 837/1696 [00:06<00:01, 491.90it/s]
50%|█████ | cHPI amplitudes : 849/1696 [00:06<00:01, 500.27it/s]
51%|█████ | cHPI amplitudes : 861/1696 [00:06<00:01, 508.47it/s]
52%|█████▏ | cHPI amplitudes : 874/1696 [00:06<00:01, 517.54it/s]
52%|█████▏ | cHPI amplitudes : 888/1696 [00:06<00:01, 529.14it/s]
53%|█████▎ | cHPI amplitudes : 900/1696 [00:06<00:01, 536.46it/s]
54%|█████▎ | cHPI amplitudes : 911/1696 [00:06<00:01, 541.63it/s]
54%|█████▍ | cHPI amplitudes : 922/1696 [00:06<00:01, 545.63it/s]
55%|█████▌ | cHPI amplitudes : 936/1696 [00:06<00:01, 556.23it/s]
56%|█████▌ | cHPI amplitudes : 948/1696 [00:06<00:01, 563.22it/s]
57%|█████▋ | cHPI amplitudes : 962/1696 [00:06<00:01, 573.83it/s]
58%|█████▊ | cHPI amplitudes : 976/1696 [00:06<00:01, 584.34it/s]
58%|█████▊ | cHPI amplitudes : 990/1696 [00:06<00:01, 593.56it/s]
59%|█████▉ | cHPI amplitudes : 1003/1696 [00:06<00:01, 600.45it/s]
60%|█████▉ | cHPI amplitudes : 1016/1696 [00:06<00:01, 606.89it/s]
61%|██████ | cHPI amplitudes : 1029/1696 [00:06<00:01, 612.89it/s]
61%|██████▏ | cHPI amplitudes : 1042/1696 [00:06<00:01, 618.49it/s]
62%|██████▏ | cHPI amplitudes : 1055/1696 [00:06<00:01, 624.33it/s]
63%|██████▎ | cHPI amplitudes : 1069/1696 [00:06<00:00, 633.74it/s]
64%|██████▎ | cHPI amplitudes : 1081/1696 [00:06<00:00, 637.19it/s]
64%|██████▍ | cHPI amplitudes : 1092/1696 [00:06<00:00, 638.40it/s]
65%|██████▌ | cHPI amplitudes : 1103/1696 [00:06<00:00, 638.76it/s]
66%|██████▌ | cHPI amplitudes : 1117/1696 [00:06<00:00, 647.09it/s]
67%|██████▋ | cHPI amplitudes : 1131/1696 [00:06<00:00, 655.52it/s]
67%|██████▋ | cHPI amplitudes : 1144/1696 [00:06<00:00, 661.88it/s]
68%|██████▊ | cHPI amplitudes : 1156/1696 [00:06<00:00, 665.29it/s]
69%|██████▉ | cHPI amplitudes : 1169/1696 [00:06<00:00, 669.38it/s]
70%|██████▉ | cHPI amplitudes : 1182/1696 [00:06<00:00, 673.56it/s]
70%|███████ | cHPI amplitudes : 1195/1696 [00:06<00:00, 677.90it/s]
71%|███████ | cHPI amplitudes : 1207/1696 [00:06<00:00, 680.75it/s]
72%|███████▏ | cHPI amplitudes : 1221/1696 [00:06<00:00, 687.63it/s]
73%|███████▎ | cHPI amplitudes : 1233/1696 [00:06<00:00, 689.56it/s]
73%|███████▎ | cHPI amplitudes : 1245/1696 [00:06<00:00, 689.91it/s]
74%|███████▍ | cHPI amplitudes : 1256/1696 [00:06<00:00, 688.99it/s]
75%|███████▍ | cHPI amplitudes : 1270/1696 [00:06<00:00, 696.43it/s]
76%|███████▌ | cHPI amplitudes : 1282/1696 [00:06<00:00, 698.49it/s]
76%|███████▋ | cHPI amplitudes : 1296/1696 [00:06<00:00, 704.81it/s]
77%|███████▋ | cHPI amplitudes : 1310/1696 [00:06<00:00, 711.05it/s]
78%|███████▊ | cHPI amplitudes : 1324/1696 [00:06<00:00, 716.44it/s]
79%|███████▉ | cHPI amplitudes : 1337/1696 [00:06<00:00, 718.64it/s]
80%|███████▉ | cHPI amplitudes : 1350/1696 [00:06<00:00, 721.04it/s]
80%|████████ | cHPI amplitudes : 1363/1696 [00:06<00:00, 722.97it/s]
81%|████████ | cHPI amplitudes : 1375/1696 [00:06<00:00, 723.58it/s]
82%|████████▏ | cHPI amplitudes : 1388/1696 [00:06<00:00, 725.14it/s]
83%|████████▎ | cHPI amplitudes : 1402/1696 [00:06<00:00, 729.84it/s]
83%|████████▎ | cHPI amplitudes : 1414/1696 [00:06<00:00, 730.48it/s]
84%|████████▍ | cHPI amplitudes : 1426/1696 [00:06<00:00, 729.97it/s]
85%|████████▍ | cHPI amplitudes : 1435/1696 [00:06<00:00, 721.33it/s]
85%|████████▌ | cHPI amplitudes : 1449/1696 [00:06<00:00, 726.34it/s]
86%|████████▋ | cHPI amplitudes : 1463/1696 [00:06<00:00, 731.70it/s]
87%|████████▋ | cHPI amplitudes : 1477/1696 [00:06<00:00, 736.24it/s]
88%|████████▊ | cHPI amplitudes : 1489/1696 [00:07<00:00, 736.27it/s]
89%|████████▊ | cHPI amplitudes : 1502/1696 [00:07<00:00, 737.03it/s]
89%|████████▉ | cHPI amplitudes : 1515/1696 [00:07<00:00, 738.07it/s]
90%|█████████ | cHPI amplitudes : 1528/1696 [00:07<00:00, 739.60it/s]
91%|█████████ | cHPI amplitudes : 1541/1696 [00:07<00:00, 740.55it/s]
92%|█████████▏| cHPI amplitudes : 1554/1696 [00:07<00:00, 743.38it/s]
92%|█████████▏| cHPI amplitudes : 1567/1696 [00:07<00:00, 743.31it/s]
93%|█████████▎| cHPI amplitudes : 1579/1696 [00:07<00:00, 743.07it/s]
94%|█████████▎| cHPI amplitudes : 1588/1696 [00:07<00:00, 734.45it/s]
94%|█████████▍| cHPI amplitudes : 1602/1696 [00:07<00:00, 738.92it/s]
95%|█████████▌| cHPI amplitudes : 1616/1696 [00:07<00:00, 743.71it/s]
96%|█████████▌| cHPI amplitudes : 1630/1696 [00:07<00:00, 748.13it/s]
97%|█████████▋| cHPI amplitudes : 1643/1696 [00:07<00:00, 748.85it/s]
98%|█████████▊| cHPI amplitudes : 1655/1696 [00:07<00:00, 748.74it/s]
98%|█████████▊| cHPI amplitudes : 1668/1696 [00:07<00:00, 748.99it/s]
99%|█████████▉| cHPI amplitudes : 1680/1696 [00:07<00:00, 748.89it/s]
100%|█████████▉| cHPI amplitudes : 1692/1696 [00:07<00:00, 748.42it/s]
100%|██████████| cHPI amplitudes : 1696/1696 [00:07<00:00, 233.05it/s]
Second, let’s compute time-varying HPI coil locations from these:
Computing 4385 HPI location guesses (1 cm grid in a 10.7 cm sphere)
HPIFIT: 5 coils digitized in order 5 1 4 3 2
HPI consistency of isotrak and hpifit is OK.
0%| | cHPI locations : 0/1696 [00:00<?, ?it/s]
0%| | cHPI locations : 1/1696 [00:01<37:05, 1.31s/it]
2%|▏ | cHPI locations : 36/1696 [00:01<00:58, 28.44it/s]
4%|▍ | cHPI locations : 68/1696 [00:01<00:29, 54.37it/s]
6%|▌ | cHPI locations : 101/1696 [00:01<00:20, 76.16it/s]
8%|▊ | cHPI locations : 133/1696 [00:01<00:15, 101.53it/s]
10%|▉ | cHPI locations : 168/1696 [00:01<00:11, 130.12it/s]
12%|█▏ | cHPI locations : 201/1696 [00:01<00:10, 147.51it/s]
14%|█▎ | cHPI locations : 232/1696 [00:01<00:08, 172.17it/s]
16%|█▌ | cHPI locations : 264/1696 [00:01<00:07, 198.29it/s]
18%|█▊ | cHPI locations : 299/1696 [00:01<00:06, 227.78it/s]
18%|█▊ | cHPI locations : 301/1696 [00:01<00:06, 215.05it/s]
20%|█▉ | cHPI locations : 333/1696 [00:01<00:05, 242.08it/s]
22%|██▏ | cHPI locations : 365/1696 [00:01<00:04, 269.60it/s]
23%|██▎ | cHPI locations : 398/1696 [00:01<00:04, 298.69it/s]
24%|██▎ | cHPI locations : 401/1696 [00:01<00:04, 278.72it/s]
26%|██▌ | cHPI locations : 433/1696 [00:01<00:04, 306.73it/s]
27%|██▋ | cHPI locations : 465/1696 [00:01<00:03, 335.28it/s]
29%|██▉ | cHPI locations : 499/1696 [00:01<00:03, 366.41it/s]
30%|██▉ | cHPI locations : 501/1696 [00:01<00:03, 337.79it/s]
32%|███▏ | cHPI locations : 535/1696 [00:01<00:03, 368.95it/s]
33%|███▎ | cHPI locations : 559/1696 [00:02<00:03, 349.67it/s]
33%|███▎ | cHPI locations : 568/1696 [00:02<00:03, 321.82it/s]
34%|███▍ | cHPI locations : 573/1696 [00:02<00:03, 294.26it/s]
34%|███▍ | cHPI locations : 577/1696 [00:02<00:04, 271.97it/s]
34%|███▍ | cHPI locations : 581/1696 [00:02<00:04, 250.57it/s]
35%|███▍ | cHPI locations : 587/1696 [00:02<00:04, 234.06it/s]
35%|███▌ | cHPI locations : 600/1696 [00:02<00:04, 224.43it/s]
37%|███▋ | cHPI locations : 630/1696 [00:02<00:04, 245.59it/s]
39%|███▉ | cHPI locations : 660/1696 [00:02<00:03, 267.24it/s]
41%|████ | cHPI locations : 690/1696 [00:02<00:03, 289.42it/s]
41%|████▏ | cHPI locations : 700/1696 [00:02<00:03, 270.98it/s]
43%|████▎ | cHPI locations : 730/1696 [00:02<00:03, 292.86it/s]
45%|████▍ | cHPI locations : 760/1696 [00:02<00:02, 315.30it/s]
47%|████▋ | cHPI locations : 789/1696 [00:03<00:02, 309.84it/s]
47%|████▋ | cHPI locations : 794/1696 [00:03<00:03, 288.62it/s]
47%|████▋ | cHPI locations : 800/1696 [00:03<00:03, 271.01it/s]
47%|████▋ | cHPI locations : 805/1696 [00:03<00:03, 252.27it/s]
48%|████▊ | cHPI locations : 810/1696 [00:03<00:03, 236.35it/s]
48%|████▊ | cHPI locations : 815/1696 [00:03<00:04, 219.85it/s]
48%|████▊ | cHPI locations : 819/1696 [00:03<00:04, 207.26it/s]
49%|████▊ | cHPI locations : 823/1696 [00:03<00:04, 192.86it/s]
49%|████▉ | cHPI locations : 827/1696 [00:03<00:04, 181.56it/s]
49%|████▉ | cHPI locations : 833/1696 [00:03<00:04, 173.16it/s]
50%|█████ | cHPI locations : 848/1696 [00:04<00:05, 169.24it/s]
52%|█████▏ | cHPI locations : 888/1696 [00:04<00:04, 193.74it/s]
55%|█████▍ | cHPI locations : 929/1696 [00:04<00:03, 219.65it/s]
56%|█████▌ | cHPI locations : 948/1696 [00:04<00:03, 214.30it/s]
58%|█████▊ | cHPI locations : 987/1696 [00:04<00:02, 238.74it/s]
61%|██████ | cHPI locations : 1027/1696 [00:04<00:02, 264.59it/s]
61%|██████▏ | cHPI locations : 1040/1696 [00:04<00:02, 254.19it/s]
62%|██████▏ | cHPI locations : 1051/1696 [00:04<00:02, 245.49it/s]
63%|██████▎ | cHPI locations : 1062/1696 [00:04<00:02, 233.04it/s]
63%|██████▎ | cHPI locations : 1072/1696 [00:04<00:02, 224.57it/s]
64%|██████▍ | cHPI locations : 1086/1696 [00:04<00:02, 217.27it/s]
66%|██████▌ | cHPI locations : 1123/1696 [00:04<00:02, 239.57it/s]
68%|██████▊ | cHPI locations : 1161/1696 [00:04<00:02, 263.22it/s]
70%|██████▉ | cHPI locations : 1186/1696 [00:04<00:01, 261.53it/s]
72%|███████▏ | cHPI locations : 1222/1696 [00:04<00:01, 283.99it/s]
74%|███████▍ | cHPI locations : 1256/1696 [00:05<00:01, 305.60it/s]
76%|███████▌ | cHPI locations : 1286/1696 [00:05<00:01, 305.48it/s]
76%|███████▋ | cHPI locations : 1296/1696 [00:05<00:01, 286.72it/s]
77%|███████▋ | cHPI locations : 1301/1696 [00:05<00:01, 271.65it/s]
77%|███████▋ | cHPI locations : 1306/1696 [00:05<00:01, 254.74it/s]
77%|███████▋ | cHPI locations : 1311/1696 [00:05<00:01, 240.08it/s]
78%|███████▊ | cHPI locations : 1315/1696 [00:05<00:01, 226.77it/s]
78%|███████▊ | cHPI locations : 1319/1696 [00:05<00:01, 213.42it/s]
78%|███████▊ | cHPI locations : 1325/1696 [00:05<00:01, 204.11it/s]
78%|███████▊ | cHPI locations : 1331/1696 [00:05<00:01, 194.98it/s]
79%|███████▉ | cHPI locations : 1341/1696 [00:06<00:01, 188.26it/s]
81%|████████ | cHPI locations : 1377/1696 [00:06<00:01, 208.90it/s]
83%|████████▎ | cHPI locations : 1413/1696 [00:06<00:01, 230.17it/s]
85%|████████▍ | cHPI locations : 1441/1696 [00:06<00:01, 232.80it/s]
87%|████████▋ | cHPI locations : 1477/1696 [00:06<00:00, 254.31it/s]
89%|████████▉ | cHPI locations : 1514/1696 [00:06<00:00, 277.13it/s]
91%|█████████ | cHPI locations : 1541/1696 [00:06<00:00, 274.53it/s]
92%|█████████▏| cHPI locations : 1565/1696 [00:06<00:00, 266.08it/s]
93%|█████████▎| cHPI locations : 1573/1696 [00:06<00:00, 253.71it/s]
93%|█████████▎| cHPI locations : 1580/1696 [00:06<00:00, 240.27it/s]
94%|█████████▎| cHPI locations : 1586/1696 [00:06<00:00, 229.34it/s]
94%|█████████▍| cHPI locations : 1590/1696 [00:06<00:00, 217.18it/s]
94%|█████████▍| cHPI locations : 1594/1696 [00:07<00:00, 205.41it/s]
94%|█████████▍| cHPI locations : 1599/1696 [00:07<00:00, 194.57it/s]
95%|█████████▍| cHPI locations : 1609/1696 [00:07<00:00, 187.92it/s]
97%|█████████▋| cHPI locations : 1639/1696 [00:07<00:00, 204.18it/s]
97%|█████████▋| cHPI locations : 1652/1696 [00:07<00:00, 198.02it/s]
100%|█████████▉| cHPI locations : 1692/1696 [00:07<00:00, 220.76it/s]
100%|██████████| cHPI locations : 1696/1696 [00:07<00:00, 229.77it/s]
Lastly, compute head positions from the coil locations:
head_pos = mne.chpi.compute_head_pos(raw.info, chpi_locs, verbose=True)
t=9.000: 5/5 good HPI fits, movements [mm/s] = 0.1 / 0.2 / 0.5 / 0.2 / 0.3
t=10.000: 5/5 good HPI fits, movements [mm/s] = 0.1 / 0.2 / 0.0 / 0.2 / 0.1
t=11.000: 5/5 good HPI fits, movements [mm/s] = 0.0 / 0.1 / 0.1 / 0.0 / 0.0
t=12.000: 5/5 good HPI fits, movements [mm/s] = 0.0 / 0.0 / 0.1 / 0.0 / 0.0
t=13.000: 5/5 good HPI fits, movements [mm/s] = 0.0 / 0.1 / 0.0 / 0.0 / 0.0
t=14.000: 5/5 good HPI fits, movements [mm/s] = 0.0 / 0.1 / 0.1 / 0.1 / 0.1
t=14.580: 5/5 good HPI fits, movements [mm/s] = 5.0 / 4.8 / 6.1 / 5.5 / 4.9
t=14.670: 5/5 good HPI fits, movements [mm/s] = 33.3 / 33.8 / 39.9 / 37.9 / 30.2
t=14.720: 5/5 good HPI fits, movements [mm/s] = 49.3 / 47.7 / 60.2 / 59.1 / 44.3
t=14.760: 5/5 good HPI fits, movements [mm/s] = 64.9 / 63.6 / 73.0 / 71.3 / 56.6
t=14.800: 5/5 good HPI fits, movements [mm/s] = 67.4 / 59.1 / 83.1 / 78.2 / 60.9
t=14.860: 5/5 good HPI fits, movements [mm/s] = 54.4 / 52.2 / 57.6 / 62.0 / 47.2
t=14.990: 5/5 good HPI fits, movements [mm/s] = 23.3 / 22.6 / 24.2 / 27.0 / 19.8
t=15.990: 5/5 good HPI fits, movements [mm/s] = 0.7 / 0.9 / 0.8 / 0.8 / 0.9
t=16.880: 5/5 good HPI fits, movements [mm/s] = 2.5 / 3.4 / 3.0 / 3.8 / 3.3
t=16.930: 5/5 good HPI fits, movements [mm/s] = 52.2 / 55.2 / 58.3 / 67.0 / 59.7
t=16.990: 5/5 good HPI fits, movements [mm/s] = 57.1 / 53.8 / 62.0 / 65.1 / 57.7
t=17.040: 5/5 good HPI fits, movements [mm/s] = 53.0 / 50.7 / 60.0 / 61.9 / 49.3
t=17.090: 5/5 good HPI fits, movements [mm/s] = 57.9 / 49.2 / 66.6 / 64.8 / 51.6
t=17.140: 5/5 good HPI fits, movements [mm/s] = 60.4 / 54.7 / 72.6 / 67.3 / 57.7
t=17.180: 5/5 good HPI fits, movements [mm/s] = 63.6 / 58.2 / 74.5 / 72.2 / 61.7
t=17.220: 5/5 good HPI fits, movements [mm/s] = 61.0 / 60.0 / 75.2 / 71.3 / 63.3
t=17.260: 5/5 good HPI fits, movements [mm/s] = 67.9 / 66.3 / 73.4 / 68.9 / 68.9
t=17.320: 5/5 good HPI fits, movements [mm/s] = 49.7 / 50.9 / 54.8 / 55.0 / 52.6
t=17.470: 5/5 good HPI fits, movements [mm/s] = 18.4 / 21.5 / 19.3 / 20.5 / 17.3
t=18.470: 5/5 good HPI fits, movements [mm/s] = 1.9 / 2.7 / 2.0 / 2.4 / 1.9
t=19.390: 5/5 good HPI fits, movements [mm/s] = 2.4 / 2.7 / 1.9 / 2.9 / 2.5
t=19.500: 5/5 good HPI fits, movements [mm/s] = 23.0 / 25.3 / 19.4 / 28.9 / 27.7
t=19.610: 5/5 good HPI fits, movements [mm/s] = 21.2 / 25.7 / 21.8 / 31.6 / 31.3
t=19.710: 5/5 good HPI fits, movements [mm/s] = 22.0 / 20.7 / 23.0 / 29.7 / 27.8
t=19.850: 5/5 good HPI fits, movements [mm/s] = 16.5 / 14.7 / 18.5 / 21.9 / 20.1
t=20.850: 5/5 good HPI fits, movements [mm/s] = 2.3 / 1.9 / 2.0 / 2.5 / 2.2
t=21.850: 5/5 good HPI fits, movements [mm/s] = 0.6 / 1.7 / 1.7 / 2.0 / 1.9
t=21.950: 5/5 good HPI fits, movements [mm/s] = 25.8 / 27.1 / 24.1 / 32.2 / 26.4
t=22.000: 5/5 good HPI fits, movements [mm/s] = 66.0 / 58.9 / 46.4 / 64.8 / 51.9
t=22.050: 5/5 good HPI fits, movements [mm/s] = 59.7 / 62.9 / 51.0 / 69.1 / 57.9
t=22.100: 5/5 good HPI fits, movements [mm/s] = 61.5 / 65.4 / 58.5 / 75.0 / 64.8
t=22.140: 5/5 good HPI fits, movements [mm/s] = 74.4 / 73.6 / 62.3 / 79.9 / 68.2
t=22.180: 5/5 good HPI fits, movements [mm/s] = 63.9 / 59.6 / 52.9 / 67.2 / 53.2
t=22.240: 5/5 good HPI fits, movements [mm/s] = 38.3 / 39.0 / 36.5 / 48.3 / 42.2
t=22.300: 5/5 good HPI fits, movements [mm/s] = 42.0 / 40.4 / 33.7 / 47.1 / 38.9
t=22.400: 5/5 good HPI fits, movements [mm/s] = 31.7 / 28.5 / 22.1 / 30.8 / 25.0
t=23.400: 5/5 good HPI fits, movements [mm/s] = 1.8 / 1.8 / 1.1 / 1.6 / 1.2
t=24.400: 5/5 good HPI fits, movements [mm/s] = 1.2 / 0.8 / 0.7 / 0.8 / 0.7
t=24.640: 5/5 good HPI fits, movements [mm/s] = 12.6 / 11.6 / 11.5 / 13.0 / 11.0
t=24.720: 5/5 good HPI fits, movements [mm/s] = 30.3 / 33.0 / 31.4 / 39.0 / 35.0
t=24.790: 5/5 good HPI fits, movements [mm/s] = 31.3 / 35.7 / 33.0 / 43.0 / 40.8
t=24.850: 5/5 good HPI fits, movements [mm/s] = 37.2 / 41.4 / 35.6 / 44.6 / 42.4
t=24.890: 5/5 good HPI fits, movements [mm/s] = 50.1 / 51.9 / 49.7 / 60.1 / 52.9
t=24.930: 5/5 good HPI fits, movements [mm/s] = 56.4 / 55.3 / 59.1 / 68.3 / 61.9
t=24.980: 5/5 good HPI fits, movements [mm/s] = 47.2 / 47.1 / 51.0 / 58.7 / 53.7
t=25.080: 5/5 good HPI fits, movements [mm/s] = 23.8 / 19.4 / 23.3 / 28.2 / 26.3
t=25.510: 5/5 good HPI fits, movements [mm/s] = 2.4 / 6.6 / 7.4 / 6.5 / 7.8
Note that these can then be written to disk or read from disk with
mne.chpi.write_head_pos()
and mne.chpi.read_head_pos()
,
respectively.
Visualizing continuous head position#
We can plot as traces, which is especially useful for long recordings:
mne.viz.plot_head_positions(head_pos, mode="traces")
Or we can visualize them as a continuous field (with the vectors pointing in the head-upward direction):
mne.viz.plot_head_positions(head_pos, mode="field")
These head positions can then be used with
mne.preprocessing.maxwell_filter()
to compensate for movement,
or with mne.preprocessing.annotate_movement()
to mark segments as
bad that deviate too much from the average head position.
Computing SNR of the HPI signal#
It is also possible to compute the SNR of the continuous HPI measurements.
This can be a useful proxy for head position along the vertical dimension,
i.e., it can indicate the distance between the HPI coils and the MEG sensors.
Using compute_chpi_snr
, the HPI power and SNR are computed
separately for each MEG sensor type and each HPI coil (frequency), along with
the residual power for each sensor type. The results can then be visualized
with plot_chpi_snr
. Here we’ll just show a few seconds, for speed:
raw.crop(tmin=5, tmax=10)
snr_dict = mne.chpi.compute_chpi_snr(raw)
fig = mne.viz.plot_chpi_snr(snr_dict)
Using 5 HPI coils: 83 143 203 263 323 Hz
Line interference frequencies: 60.0 120.0 180.0 240.0 300.0 360.0 Hz
Using time window: 83.3 ms
Fitting 5 HPI coil locations at up to 496 time points (5.0 s duration)
0%| | cHPI SNRs : 0/496 [00:00<?, ?it/s]
0%| | cHPI SNRs : 1/496 [00:06<53:19, 6.46s/it]
1%| | cHPI SNRs : 4/496 [00:06<12:47, 1.56s/it]
1%|▏ | cHPI SNRs : 7/496 [00:06<07:05, 1.15it/s]
2%|▏ | cHPI SNRs : 10/496 [00:06<04:48, 1.68it/s]
3%|▎ | cHPI SNRs : 13/496 [00:06<03:35, 2.24it/s]
3%|▎ | cHPI SNRs : 16/496 [00:06<02:50, 2.82it/s]
4%|▍ | cHPI SNRs : 19/496 [00:06<02:19, 3.43it/s]
4%|▍ | cHPI SNRs : 22/496 [00:06<01:56, 4.07it/s]
5%|▌ | cHPI SNRs : 25/496 [00:06<01:39, 4.73it/s]
6%|▌ | cHPI SNRs : 28/496 [00:06<01:26, 5.42it/s]
6%|▋ | cHPI SNRs : 31/496 [00:06<01:15, 6.15it/s]
7%|▋ | cHPI SNRs : 34/496 [00:06<01:06, 6.90it/s]
7%|▋ | cHPI SNRs : 37/496 [00:06<00:59, 7.68it/s]
8%|▊ | cHPI SNRs : 40/496 [00:06<00:53, 8.50it/s]
9%|▊ | cHPI SNRs : 43/496 [00:06<00:48, 9.36it/s]
9%|▉ | cHPI SNRs : 46/496 [00:06<00:43, 10.24it/s]
10%|▉ | cHPI SNRs : 49/496 [00:06<00:40, 11.17it/s]
10%|█ | cHPI SNRs : 52/496 [00:06<00:36, 12.13it/s]
11%|█ | cHPI SNRs : 55/496 [00:06<00:33, 13.13it/s]
12%|█▏ | cHPI SNRs : 58/496 [00:06<00:30, 14.17it/s]
12%|█▏ | cHPI SNRs : 61/496 [00:06<00:28, 15.25it/s]
13%|█▎ | cHPI SNRs : 64/496 [00:06<00:26, 16.36it/s]
14%|█▎ | cHPI SNRs : 67/496 [00:06<00:24, 17.52it/s]
14%|█▍ | cHPI SNRs : 70/496 [00:06<00:22, 18.72it/s]
15%|█▍ | cHPI SNRs : 73/496 [00:06<00:21, 19.97it/s]
15%|█▌ | cHPI SNRs : 76/496 [00:06<00:19, 21.26it/s]
16%|█▌ | cHPI SNRs : 79/496 [00:06<00:18, 22.58it/s]
17%|█▋ | cHPI SNRs : 82/496 [00:06<00:17, 23.96it/s]
17%|█▋ | cHPI SNRs : 85/496 [00:06<00:16, 25.37it/s]
18%|█▊ | cHPI SNRs : 88/496 [00:06<00:15, 26.83it/s]
18%|█▊ | cHPI SNRs : 91/496 [00:07<00:14, 28.34it/s]
19%|█▉ | cHPI SNRs : 94/496 [00:07<00:13, 29.90it/s]
20%|█▉ | cHPI SNRs : 97/496 [00:07<00:12, 31.50it/s]
20%|██ | cHPI SNRs : 100/496 [00:07<00:11, 33.15it/s]
21%|██ | cHPI SNRs : 103/496 [00:07<00:11, 34.84it/s]
21%|██▏ | cHPI SNRs : 106/496 [00:07<00:10, 36.58it/s]
22%|██▏ | cHPI SNRs : 109/496 [00:07<00:10, 38.36it/s]
23%|██▎ | cHPI SNRs : 112/496 [00:07<00:09, 40.19it/s]
23%|██▎ | cHPI SNRs : 115/496 [00:07<00:09, 42.07it/s]
24%|██▍ | cHPI SNRs : 118/496 [00:07<00:08, 43.96it/s]
24%|██▍ | cHPI SNRs : 121/496 [00:07<00:08, 45.91it/s]
25%|██▌ | cHPI SNRs : 124/496 [00:07<00:07, 47.90it/s]
26%|██▌ | cHPI SNRs : 127/496 [00:07<00:07, 49.93it/s]
26%|██▌ | cHPI SNRs : 130/496 [00:07<00:07, 52.00it/s]
27%|██▋ | cHPI SNRs : 133/496 [00:07<00:06, 54.10it/s]
27%|██▋ | cHPI SNRs : 136/496 [00:07<00:06, 56.20it/s]
28%|██▊ | cHPI SNRs : 139/496 [00:07<00:06, 58.32it/s]
29%|██▊ | cHPI SNRs : 142/496 [00:07<00:05, 60.51it/s]
29%|██▉ | cHPI SNRs : 145/496 [00:07<00:05, 62.68it/s]
30%|██▉ | cHPI SNRs : 148/496 [00:07<00:05, 64.44it/s]
30%|███ | cHPI SNRs : 151/496 [00:07<00:05, 66.47it/s]
31%|███ | cHPI SNRs : 154/496 [00:07<00:04, 68.60it/s]
32%|███▏ | cHPI SNRs : 157/496 [00:07<00:04, 70.82it/s]
32%|███▏ | cHPI SNRs : 160/496 [00:07<00:04, 73.07it/s]
33%|███▎ | cHPI SNRs : 163/496 [00:07<00:04, 75.39it/s]
34%|███▎ | cHPI SNRs : 167/496 [00:07<00:04, 78.72it/s]
34%|███▍ | cHPI SNRs : 171/496 [00:07<00:03, 81.98it/s]
35%|███▌ | cHPI SNRs : 175/496 [00:07<00:03, 85.26it/s]
36%|███▌ | cHPI SNRs : 179/496 [00:07<00:03, 88.47it/s]
37%|███▋ | cHPI SNRs : 183/496 [00:07<00:03, 91.73it/s]
38%|███▊ | cHPI SNRs : 187/496 [00:07<00:03, 94.88it/s]
39%|███▊ | cHPI SNRs : 191/496 [00:07<00:03, 97.99it/s]
39%|███▉ | cHPI SNRs : 194/496 [00:07<00:03, 100.04it/s]
40%|███▉ | cHPI SNRs : 197/496 [00:07<00:02, 102.13it/s]
40%|████ | cHPI SNRs : 200/496 [00:07<00:02, 104.06it/s]
41%|████ | cHPI SNRs : 203/496 [00:07<00:02, 105.93it/s]
42%|████▏ | cHPI SNRs : 206/496 [00:07<00:02, 107.75it/s]
42%|████▏ | cHPI SNRs : 209/496 [00:07<00:02, 109.57it/s]
43%|████▎ | cHPI SNRs : 212/496 [00:07<00:02, 111.39it/s]
43%|████▎ | cHPI SNRs : 215/496 [00:07<00:02, 113.16it/s]
44%|████▍ | cHPI SNRs : 218/496 [00:07<00:02, 114.84it/s]
45%|████▍ | cHPI SNRs : 221/496 [00:07<00:02, 116.49it/s]
45%|████▌ | cHPI SNRs : 224/496 [00:07<00:02, 118.23it/s]
46%|████▌ | cHPI SNRs : 227/496 [00:07<00:02, 119.91it/s]
46%|████▋ | cHPI SNRs : 230/496 [00:07<00:02, 121.49it/s]
47%|████▋ | cHPI SNRs : 233/496 [00:07<00:02, 123.02it/s]
48%|████▊ | cHPI SNRs : 236/496 [00:07<00:02, 124.53it/s]
48%|████▊ | cHPI SNRs : 239/496 [00:07<00:02, 125.93it/s]
49%|████▉ | cHPI SNRs : 242/496 [00:07<00:01, 127.40it/s]
49%|████▉ | cHPI SNRs : 245/496 [00:07<00:01, 128.82it/s]
50%|█████ | cHPI SNRs : 248/496 [00:07<00:01, 130.18it/s]
51%|█████ | cHPI SNRs : 251/496 [00:07<00:01, 131.58it/s]
51%|█████ | cHPI SNRs : 254/496 [00:07<00:01, 132.95it/s]
52%|█████▏ | cHPI SNRs : 259/496 [00:07<00:01, 139.27it/s]
53%|█████▎ | cHPI SNRs : 265/496 [00:07<00:01, 147.66it/s]
55%|█████▍ | cHPI SNRs : 271/496 [00:08<00:01, 155.91it/s]
56%|█████▌ | cHPI SNRs : 278/496 [00:08<00:01, 165.31it/s]
57%|█████▋ | cHPI SNRs : 285/496 [00:08<00:01, 174.42it/s]
59%|█████▉ | cHPI SNRs : 292/496 [00:08<00:01, 183.11it/s]
60%|██████ | cHPI SNRs : 299/496 [00:08<00:01, 191.60it/s]
62%|██████▏ | cHPI SNRs : 306/496 [00:08<00:00, 199.77it/s]
63%|██████▎ | cHPI SNRs : 313/496 [00:08<00:00, 207.68it/s]
64%|██████▍ | cHPI SNRs : 319/496 [00:08<00:00, 213.87it/s]
65%|██████▌ | cHPI SNRs : 323/496 [00:08<00:00, 214.66it/s]
66%|██████▌ | cHPI SNRs : 327/496 [00:08<00:00, 213.78it/s]
67%|██████▋ | cHPI SNRs : 330/496 [00:08<00:00, 211.86it/s]
67%|██████▋ | cHPI SNRs : 333/496 [00:08<00:00, 210.64it/s]
68%|██████▊ | cHPI SNRs : 336/496 [00:08<00:00, 209.06it/s]
68%|██████▊ | cHPI SNRs : 339/496 [00:08<00:00, 207.60it/s]
69%|██████▉ | cHPI SNRs : 342/496 [00:08<00:00, 205.81it/s]
70%|██████▉ | cHPI SNRs : 345/496 [00:08<00:00, 204.41it/s]
70%|███████ | cHPI SNRs : 348/496 [00:08<00:00, 202.69it/s]
71%|███████ | cHPI SNRs : 351/496 [00:08<00:00, 201.18it/s]
72%|███████▏ | cHPI SNRs : 355/496 [00:08<00:00, 200.71it/s]
72%|███████▏ | cHPI SNRs : 359/496 [00:08<00:00, 200.07it/s]
73%|███████▎ | cHPI SNRs : 362/496 [00:08<00:00, 198.95it/s]
74%|███████▎ | cHPI SNRs : 365/496 [00:08<00:00, 197.58it/s]
74%|███████▍ | cHPI SNRs : 368/496 [00:08<00:00, 196.34it/s]
75%|███████▍ | cHPI SNRs : 371/496 [00:08<00:00, 195.91it/s]
75%|███████▌ | cHPI SNRs : 374/496 [00:08<00:00, 195.36it/s]
76%|███████▌ | cHPI SNRs : 377/496 [00:08<00:00, 194.86it/s]
77%|███████▋ | cHPI SNRs : 381/496 [00:08<00:00, 194.58it/s]
78%|███████▊ | cHPI SNRs : 385/496 [00:08<00:00, 194.43it/s]
78%|███████▊ | cHPI SNRs : 389/496 [00:08<00:00, 194.15it/s]
79%|███████▉ | cHPI SNRs : 393/496 [00:08<00:00, 194.03it/s]
80%|████████ | cHPI SNRs : 397/496 [00:08<00:00, 193.73it/s]
81%|████████ | cHPI SNRs : 401/496 [00:08<00:00, 193.66it/s]
82%|████████▏ | cHPI SNRs : 405/496 [00:08<00:00, 193.62it/s]
82%|████████▏ | cHPI SNRs : 409/496 [00:08<00:00, 193.57it/s]
83%|████████▎ | cHPI SNRs : 413/496 [00:08<00:00, 193.56it/s]
84%|████████▍ | cHPI SNRs : 417/496 [00:08<00:00, 193.69it/s]
85%|████████▍ | cHPI SNRs : 421/496 [00:08<00:00, 193.47it/s]
86%|████████▌ | cHPI SNRs : 425/496 [00:08<00:00, 193.35it/s]
86%|████████▋ | cHPI SNRs : 428/496 [00:08<00:00, 192.81it/s]
87%|████████▋ | cHPI SNRs : 431/496 [00:08<00:00, 191.79it/s]
88%|████████▊ | cHPI SNRs : 434/496 [00:08<00:00, 190.78it/s]
88%|████████▊ | cHPI SNRs : 437/496 [00:08<00:00, 189.72it/s]
89%|████████▊ | cHPI SNRs : 440/496 [00:08<00:00, 189.28it/s]
89%|████████▉ | cHPI SNRs : 443/496 [00:08<00:00, 188.40it/s]
90%|████████▉ | cHPI SNRs : 446/496 [00:08<00:00, 187.57it/s]
91%|█████████ | cHPI SNRs : 449/496 [00:08<00:00, 186.67it/s]
91%|█████████ | cHPI SNRs : 452/496 [00:08<00:00, 185.95it/s]
92%|█████████▏| cHPI SNRs : 455/496 [00:08<00:00, 185.01it/s]
92%|█████████▏| cHPI SNRs : 458/496 [00:08<00:00, 184.45it/s]
93%|█████████▎| cHPI SNRs : 461/496 [00:08<00:00, 183.71it/s]
94%|█████████▎| cHPI SNRs : 464/496 [00:08<00:00, 183.16it/s]
94%|█████████▍| cHPI SNRs : 467/496 [00:08<00:00, 182.39it/s]
95%|█████████▍| cHPI SNRs : 470/496 [00:08<00:00, 181.73it/s]
95%|█████████▌| cHPI SNRs : 473/496 [00:08<00:00, 180.86it/s]
96%|█████████▌| cHPI SNRs : 476/496 [00:09<00:00, 180.18it/s]
97%|█████████▋| cHPI SNRs : 479/496 [00:09<00:00, 179.64it/s]
97%|█████████▋| cHPI SNRs : 482/496 [00:09<00:00, 179.24it/s]
98%|█████████▊| cHPI SNRs : 485/496 [00:09<00:00, 178.78it/s]
98%|█████████▊| cHPI SNRs : 488/496 [00:09<00:00, 178.49it/s]
99%|█████████▉| cHPI SNRs : 491/496 [00:09<00:00, 177.90it/s]
100%|█████████▉| cHPI SNRs : 494/496 [00:09<00:00, 177.27it/s]
100%|██████████| cHPI SNRs : 496/496 [00:09<00:00, 54.32it/s]
Total running time of the script: (0 minutes 45.499 seconds)