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:04<2:05:37, 4.45s/it]
1%| | cHPI amplitudes : 15/1696 [00:04<07:56, 3.52it/s]
2%|▏ | cHPI amplitudes : 29/1696 [00:04<03:59, 6.97it/s]
3%|▎ | cHPI amplitudes : 43/1696 [00:04<02:36, 10.57it/s]
3%|▎ | cHPI amplitudes : 57/1696 [00:04<01:54, 14.32it/s]
4%|▍ | cHPI amplitudes : 71/1696 [00:04<01:29, 18.23it/s]
5%|▌ | cHPI amplitudes : 85/1696 [00:04<01:12, 22.31it/s]
6%|▌ | cHPI amplitudes : 99/1696 [00:04<01:00, 26.56it/s]
7%|▋ | cHPI amplitudes : 113/1696 [00:04<00:51, 30.99it/s]
7%|▋ | cHPI amplitudes : 127/1696 [00:04<00:44, 35.60it/s]
8%|▊ | cHPI amplitudes : 141/1696 [00:04<00:38, 40.39it/s]
9%|▉ | cHPI amplitudes : 155/1696 [00:04<00:33, 45.38it/s]
10%|▉ | cHPI amplitudes : 169/1696 [00:04<00:30, 50.57it/s]
11%|█ | cHPI amplitudes : 183/1696 [00:04<00:27, 55.96it/s]
12%|█▏ | cHPI amplitudes : 197/1696 [00:04<00:24, 61.55it/s]
12%|█▏ | cHPI amplitudes : 211/1696 [00:04<00:22, 67.35it/s]
13%|█▎ | cHPI amplitudes : 225/1696 [00:04<00:20, 73.35it/s]
14%|█▍ | cHPI amplitudes : 239/1696 [00:04<00:18, 79.59it/s]
15%|█▍ | cHPI amplitudes : 253/1696 [00:04<00:16, 86.04it/s]
16%|█▌ | cHPI amplitudes : 267/1696 [00:04<00:15, 92.73it/s]
17%|█▋ | cHPI amplitudes : 281/1696 [00:04<00:14, 99.63it/s]
17%|█▋ | cHPI amplitudes : 295/1696 [00:04<00:13, 106.77it/s]
18%|█▊ | cHPI amplitudes : 309/1696 [00:04<00:12, 114.10it/s]
19%|█▉ | cHPI amplitudes : 323/1696 [00:04<00:11, 121.69it/s]
20%|█▉ | cHPI amplitudes : 337/1696 [00:04<00:10, 129.53it/s]
21%|██ | cHPI amplitudes : 351/1696 [00:04<00:09, 137.57it/s]
22%|██▏ | cHPI amplitudes : 365/1696 [00:04<00:09, 145.81it/s]
22%|██▏ | cHPI amplitudes : 379/1696 [00:04<00:08, 154.30it/s]
23%|██▎ | cHPI amplitudes : 393/1696 [00:04<00:07, 162.97it/s]
24%|██▍ | cHPI amplitudes : 407/1696 [00:04<00:07, 171.95it/s]
25%|██▍ | cHPI amplitudes : 421/1696 [00:04<00:07, 181.14it/s]
26%|██▌ | cHPI amplitudes : 435/1696 [00:04<00:06, 190.56it/s]
26%|██▋ | cHPI amplitudes : 449/1696 [00:04<00:06, 200.14it/s]
27%|██▋ | cHPI amplitudes : 463/1696 [00:04<00:05, 209.96it/s]
28%|██▊ | cHPI amplitudes : 477/1696 [00:05<00:05, 219.87it/s]
29%|██▉ | cHPI amplitudes : 491/1696 [00:05<00:05, 230.07it/s]
30%|██▉ | cHPI amplitudes : 505/1696 [00:05<00:04, 240.50it/s]
31%|███ | cHPI amplitudes : 519/1696 [00:05<00:04, 251.13it/s]
31%|███▏ | cHPI amplitudes : 533/1696 [00:05<00:04, 261.92it/s]
32%|███▏ | cHPI amplitudes : 547/1696 [00:05<00:04, 272.85it/s]
33%|███▎ | cHPI amplitudes : 561/1696 [00:05<00:03, 283.81it/s]
34%|███▍ | cHPI amplitudes : 575/1696 [00:05<00:03, 295.08it/s]
35%|███▍ | cHPI amplitudes : 589/1696 [00:05<00:03, 306.42it/s]
36%|███▌ | cHPI amplitudes : 603/1696 [00:05<00:03, 317.90it/s]
36%|███▋ | cHPI amplitudes : 617/1696 [00:05<00:03, 329.48it/s]
37%|███▋ | cHPI amplitudes : 631/1696 [00:05<00:03, 341.15it/s]
38%|███▊ | cHPI amplitudes : 645/1696 [00:05<00:02, 352.64it/s]
39%|███▉ | cHPI amplitudes : 659/1696 [00:05<00:02, 364.29it/s]
40%|███▉ | cHPI amplitudes : 673/1696 [00:05<00:02, 376.14it/s]
41%|████ | cHPI amplitudes : 687/1696 [00:05<00:02, 387.87it/s]
41%|████▏ | cHPI amplitudes : 701/1696 [00:05<00:02, 399.61it/s]
42%|████▏ | cHPI amplitudes : 715/1696 [00:05<00:02, 411.39it/s]
43%|████▎ | cHPI amplitudes : 729/1696 [00:05<00:02, 422.81it/s]
44%|████▍ | cHPI amplitudes : 743/1696 [00:05<00:02, 434.45it/s]
45%|████▍ | cHPI amplitudes : 757/1696 [00:05<00:02, 446.01it/s]
45%|████▌ | cHPI amplitudes : 771/1696 [00:05<00:02, 457.16it/s]
46%|████▋ | cHPI amplitudes : 785/1696 [00:05<00:01, 468.73it/s]
47%|████▋ | cHPI amplitudes : 799/1696 [00:05<00:01, 480.24it/s]
48%|████▊ | cHPI amplitudes : 813/1696 [00:05<00:01, 491.26it/s]
49%|████▉ | cHPI amplitudes : 827/1696 [00:05<00:01, 502.46it/s]
50%|████▉ | cHPI amplitudes : 841/1696 [00:05<00:01, 513.57it/s]
50%|█████ | cHPI amplitudes : 855/1696 [00:05<00:01, 524.62it/s]
51%|█████ | cHPI amplitudes : 869/1696 [00:05<00:01, 535.23it/s]
52%|█████▏ | cHPI amplitudes : 883/1696 [00:05<00:01, 545.74it/s]
53%|█████▎ | cHPI amplitudes : 897/1696 [00:05<00:01, 556.45it/s]
54%|█████▎ | cHPI amplitudes : 910/1696 [00:05<00:01, 564.52it/s]
54%|█████▍ | cHPI amplitudes : 924/1696 [00:05<00:01, 574.54it/s]
55%|█████▌ | cHPI amplitudes : 938/1696 [00:05<00:01, 584.58it/s]
56%|█████▌ | cHPI amplitudes : 952/1696 [00:05<00:01, 594.32it/s]
57%|█████▋ | cHPI amplitudes : 966/1696 [00:05<00:01, 603.74it/s]
58%|█████▊ | cHPI amplitudes : 980/1696 [00:05<00:01, 613.25it/s]
59%|█████▊ | cHPI amplitudes : 994/1696 [00:05<00:01, 621.62it/s]
59%|█████▉ | cHPI amplitudes : 1008/1696 [00:05<00:01, 630.46it/s]
60%|██████ | cHPI amplitudes : 1022/1696 [00:05<00:01, 639.05it/s]
61%|██████ | cHPI amplitudes : 1036/1696 [00:05<00:01, 647.55it/s]
62%|██████▏ | cHPI amplitudes : 1050/1696 [00:05<00:00, 655.84it/s]
63%|██████▎ | cHPI amplitudes : 1064/1696 [00:05<00:00, 663.76it/s]
64%|██████▎ | cHPI amplitudes : 1078/1696 [00:05<00:00, 670.77it/s]
64%|██████▍ | cHPI amplitudes : 1092/1696 [00:05<00:00, 678.20it/s]
65%|██████▌ | cHPI amplitudes : 1106/1696 [00:05<00:00, 685.48it/s]
66%|██████▌ | cHPI amplitudes : 1120/1696 [00:05<00:00, 692.64it/s]
67%|██████▋ | cHPI amplitudes : 1134/1696 [00:05<00:00, 699.27it/s]
68%|██████▊ | cHPI amplitudes : 1148/1696 [00:05<00:00, 705.92it/s]
69%|██████▊ | cHPI amplitudes : 1162/1696 [00:05<00:00, 711.80it/s]
69%|██████▉ | cHPI amplitudes : 1176/1696 [00:05<00:00, 718.11it/s]
70%|███████ | cHPI amplitudes : 1190/1696 [00:05<00:00, 723.95it/s]
71%|███████ | cHPI amplitudes : 1204/1696 [00:05<00:00, 729.74it/s]
72%|███████▏ | cHPI amplitudes : 1218/1696 [00:05<00:00, 735.28it/s]
73%|███████▎ | cHPI amplitudes : 1232/1696 [00:05<00:00, 740.50it/s]
73%|███████▎ | cHPI amplitudes : 1246/1696 [00:05<00:00, 745.38it/s]
74%|███████▍ | cHPI amplitudes : 1260/1696 [00:05<00:00, 750.29it/s]
75%|███████▌ | cHPI amplitudes : 1274/1696 [00:05<00:00, 754.60it/s]
76%|███████▌ | cHPI amplitudes : 1288/1696 [00:05<00:00, 758.93it/s]
77%|███████▋ | cHPI amplitudes : 1302/1696 [00:05<00:00, 763.45it/s]
78%|███████▊ | cHPI amplitudes : 1316/1696 [00:05<00:00, 767.44it/s]
78%|███████▊ | cHPI amplitudes : 1330/1696 [00:06<00:00, 770.71it/s]
79%|███████▉ | cHPI amplitudes : 1344/1696 [00:06<00:00, 774.91it/s]
80%|████████ | cHPI amplitudes : 1358/1696 [00:06<00:00, 778.47it/s]
81%|████████ | cHPI amplitudes : 1372/1696 [00:06<00:00, 781.78it/s]
82%|████████▏ | cHPI amplitudes : 1386/1696 [00:06<00:00, 785.14it/s]
83%|████████▎ | cHPI amplitudes : 1400/1696 [00:06<00:00, 788.72it/s]
83%|████████▎ | cHPI amplitudes : 1414/1696 [00:06<00:00, 791.24it/s]
84%|████████▍ | cHPI amplitudes : 1428/1696 [00:06<00:00, 794.51it/s]
85%|████████▌ | cHPI amplitudes : 1442/1696 [00:06<00:00, 797.44it/s]
86%|████████▌ | cHPI amplitudes : 1457/1696 [00:06<00:00, 801.49it/s]
87%|████████▋ | cHPI amplitudes : 1471/1696 [00:06<00:00, 804.39it/s]
88%|████████▊ | cHPI amplitudes : 1485/1696 [00:06<00:00, 806.90it/s]
88%|████████▊ | cHPI amplitudes : 1499/1696 [00:06<00:00, 809.42it/s]
89%|████████▉ | cHPI amplitudes : 1513/1696 [00:06<00:00, 811.27it/s]
90%|█████████ | cHPI amplitudes : 1527/1696 [00:06<00:00, 813.07it/s]
91%|█████████ | cHPI amplitudes : 1541/1696 [00:06<00:00, 815.35it/s]
92%|█████████▏| cHPI amplitudes : 1555/1696 [00:06<00:00, 817.40it/s]
93%|█████████▎| cHPI amplitudes : 1569/1696 [00:06<00:00, 819.45it/s]
93%|█████████▎| cHPI amplitudes : 1583/1696 [00:06<00:00, 821.22it/s]
94%|█████████▍| cHPI amplitudes : 1597/1696 [00:06<00:00, 822.29it/s]
95%|█████████▍| cHPI amplitudes : 1611/1696 [00:06<00:00, 824.08it/s]
96%|█████████▌| cHPI amplitudes : 1625/1696 [00:06<00:00, 825.71it/s]
97%|█████████▋| cHPI amplitudes : 1639/1696 [00:06<00:00, 827.07it/s]
97%|█████████▋| cHPI amplitudes : 1653/1696 [00:06<00:00, 827.01it/s]
98%|█████████▊| cHPI amplitudes : 1667/1696 [00:06<00:00, 826.86it/s]
99%|█████████▉| cHPI amplitudes : 1681/1696 [00:06<00:00, 828.78it/s]
100%|█████████▉| cHPI amplitudes : 1695/1696 [00:06<00:00, 828.26it/s]
100%|██████████| cHPI amplitudes : 1696/1696 [00:06<00:00, 263.16it/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<40:16, 1.43s/it]
2%|▏ | cHPI locations : 32/1696 [00:01<01:11, 23.31it/s]
4%|▎ | cHPI locations : 62/1696 [00:01<00:35, 45.77it/s]
5%|▌ | cHPI locations : 92/1696 [00:01<00:23, 68.81it/s]
6%|▌ | cHPI locations : 101/1696 [00:01<00:22, 71.02it/s]
8%|▊ | cHPI locations : 130/1696 [00:01<00:16, 93.37it/s]
9%|▉ | cHPI locations : 160/1696 [00:01<00:13, 117.08it/s]
11%|█ | cHPI locations : 190/1696 [00:01<00:10, 141.36it/s]
12%|█▏ | cHPI locations : 201/1696 [00:01<00:10, 141.19it/s]
14%|█▎ | cHPI locations : 231/1696 [00:01<00:08, 165.56it/s]
16%|█▌ | cHPI locations : 263/1696 [00:01<00:07, 192.31it/s]
17%|█▋ | cHPI locations : 294/1696 [00:01<00:06, 218.66it/s]
18%|█▊ | cHPI locations : 301/1696 [00:01<00:06, 210.77it/s]
20%|██ | cHPI locations : 343/1696 [00:01<00:05, 248.01it/s]
23%|██▎ | cHPI locations : 386/1696 [00:01<00:04, 286.89it/s]
24%|██▎ | cHPI locations : 401/1696 [00:01<00:04, 280.34it/s]
26%|██▌ | cHPI locations : 443/1696 [00:01<00:03, 318.11it/s]
29%|██▊ | cHPI locations : 486/1696 [00:01<00:03, 357.52it/s]
30%|██▉ | cHPI locations : 501/1696 [00:02<00:03, 343.94it/s]
32%|███▏ | cHPI locations : 543/1696 [00:02<00:03, 382.02it/s]
33%|███▎ | cHPI locations : 559/1696 [00:02<00:03, 357.46it/s]
33%|███▎ | cHPI locations : 568/1696 [00:02<00:03, 330.45it/s]
34%|███▍ | cHPI locations : 573/1696 [00:02<00:03, 303.01it/s]
34%|███▍ | cHPI locations : 577/1696 [00:02<00:04, 279.58it/s]
34%|███▍ | cHPI locations : 581/1696 [00:02<00:04, 255.99it/s]
35%|███▍ | cHPI locations : 587/1696 [00:02<00:04, 238.73it/s]
35%|███▌ | cHPI locations : 600/1696 [00:02<00:04, 228.80it/s]
37%|███▋ | cHPI locations : 632/1696 [00:02<00:04, 250.89it/s]
39%|███▉ | cHPI locations : 665/1696 [00:02<00:03, 274.44it/s]
41%|████ | cHPI locations : 697/1696 [00:02<00:03, 297.63it/s]
41%|████▏ | cHPI locations : 700/1696 [00:02<00:03, 274.52it/s]
43%|████▎ | cHPI locations : 732/1696 [00:02<00:03, 297.73it/s]
45%|████▌ | cHPI locations : 765/1696 [00:03<00:02, 322.32it/s]
47%|████▋ | cHPI locations : 789/1696 [00:03<00:02, 312.68it/s]
47%|████▋ | cHPI locations : 794/1696 [00:03<00:03, 290.93it/s]
47%|████▋ | cHPI locations : 800/1696 [00:03<00:03, 272.63it/s]
47%|████▋ | cHPI locations : 805/1696 [00:03<00:03, 255.09it/s]
48%|████▊ | cHPI locations : 810/1696 [00:03<00:03, 239.61it/s]
48%|████▊ | cHPI locations : 815/1696 [00:03<00:03, 225.95it/s]
48%|████▊ | cHPI locations : 819/1696 [00:03<00:04, 211.96it/s]
49%|████▊ | cHPI locations : 823/1696 [00:03<00:04, 199.05it/s]
49%|████▉ | cHPI locations : 827/1696 [00:03<00:04, 187.48it/s]
49%|████▉ | cHPI locations : 833/1696 [00:04<00:04, 177.70it/s]
50%|█████ | cHPI locations : 848/1696 [00:04<00:04, 174.95it/s]
52%|█████▏ | cHPI locations : 877/1696 [00:04<00:04, 192.42it/s]
53%|█████▎ | cHPI locations : 906/1696 [00:04<00:03, 210.34it/s]
55%|█████▌ | cHPI locations : 937/1696 [00:04<00:03, 230.22it/s]
56%|█████▌ | cHPI locations : 948/1696 [00:04<00:03, 220.26it/s]
58%|█████▊ | cHPI locations : 980/1696 [00:04<00:02, 240.99it/s]
60%|█████▉ | cHPI locations : 1012/1696 [00:04<00:02, 262.28it/s]
61%|██████▏ | cHPI locations : 1040/1696 [00:04<00:02, 261.40it/s]
62%|██████▏ | cHPI locations : 1051/1696 [00:04<00:02, 249.62it/s]
63%|██████▎ | cHPI locations : 1062/1696 [00:04<00:02, 240.16it/s]
63%|██████▎ | cHPI locations : 1072/1696 [00:04<00:02, 227.76it/s]
64%|██████▍ | cHPI locations : 1086/1696 [00:04<00:02, 221.45it/s]
66%|██████▌ | cHPI locations : 1117/1696 [00:04<00:02, 240.36it/s]
68%|██████▊ | cHPI locations : 1152/1696 [00:04<00:02, 262.73it/s]
70%|██████▉ | cHPI locations : 1186/1696 [00:05<00:01, 266.08it/s]
72%|███████▏ | cHPI locations : 1216/1696 [00:05<00:01, 284.72it/s]
74%|███████▎ | cHPI locations : 1249/1696 [00:05<00:01, 306.14it/s]
76%|███████▌ | cHPI locations : 1281/1696 [00:05<00:01, 327.35it/s]
76%|███████▌ | cHPI locations : 1286/1696 [00:05<00:01, 307.55it/s]
76%|███████▋ | cHPI locations : 1296/1696 [00:05<00:01, 290.89it/s]
77%|███████▋ | cHPI locations : 1301/1696 [00:05<00:01, 273.51it/s]
77%|███████▋ | cHPI locations : 1306/1696 [00:05<00:01, 256.74it/s]
77%|███████▋ | cHPI locations : 1311/1696 [00:05<00:01, 240.57it/s]
78%|███████▊ | cHPI locations : 1315/1696 [00:05<00:01, 226.05it/s]
78%|███████▊ | cHPI locations : 1319/1696 [00:05<00:01, 213.93it/s]
78%|███████▊ | cHPI locations : 1325/1696 [00:05<00:01, 203.67it/s]
78%|███████▊ | cHPI locations : 1331/1696 [00:05<00:01, 194.89it/s]
79%|███████▉ | cHPI locations : 1341/1696 [00:06<00:01, 187.19it/s]
81%|████████ | cHPI locations : 1373/1696 [00:06<00:01, 206.10it/s]
83%|████████▎ | cHPI locations : 1404/1696 [00:06<00:01, 224.91it/s]
85%|████████▍ | cHPI locations : 1434/1696 [00:06<00:01, 243.57it/s]
85%|████████▍ | cHPI locations : 1441/1696 [00:06<00:01, 231.43it/s]
87%|████████▋ | cHPI locations : 1472/1696 [00:06<00:00, 251.09it/s]
89%|████████▊ | cHPI locations : 1505/1696 [00:06<00:00, 272.75it/s]
91%|█████████ | cHPI locations : 1535/1696 [00:06<00:00, 292.65it/s]
91%|█████████ | cHPI locations : 1541/1696 [00:06<00:00, 274.59it/s]
92%|█████████▏| cHPI locations : 1565/1696 [00:06<00:00, 269.45it/s]
93%|█████████▎| cHPI locations : 1573/1696 [00:06<00:00, 261.69it/s]
93%|█████████▎| cHPI locations : 1580/1696 [00:06<00:00, 254.26it/s]
94%|█████████▎| cHPI locations : 1586/1696 [00:06<00:00, 246.44it/s]
94%|█████████▍| cHPI locations : 1590/1696 [00:06<00:00, 236.48it/s]
94%|█████████▍| cHPI locations : 1594/1696 [00:06<00:00, 226.80it/s]
94%|█████████▍| cHPI locations : 1599/1696 [00:06<00:00, 218.44it/s]
95%|█████████▍| cHPI locations : 1609/1696 [00:07<00:00, 214.76it/s]
97%|█████████▋| cHPI locations : 1652/1696 [00:07<00:00, 233.24it/s]
100%|██████████| cHPI locations : 1696/1696 [00:07<00:00, 238.73it/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", totals=True)
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<49:44, 6.03s/it]
1%| | cHPI SNRs : 4/496 [00:06<11:55, 1.45s/it]
1%|▏ | cHPI SNRs : 7/496 [00:06<06:36, 1.23it/s]
2%|▏ | cHPI SNRs : 10/496 [00:06<04:29, 1.81it/s]
3%|▎ | cHPI SNRs : 13/496 [00:06<03:21, 2.40it/s]
3%|▎ | cHPI SNRs : 16/496 [00:06<02:38, 3.03it/s]
4%|▍ | cHPI SNRs : 19/496 [00:06<02:09, 3.68it/s]
4%|▍ | cHPI SNRs : 22/496 [00:06<01:48, 4.36it/s]
5%|▌ | cHPI SNRs : 25/496 [00:06<01:32, 5.07it/s]
6%|▌ | cHPI SNRs : 28/496 [00:06<01:20, 5.82it/s]
7%|▋ | cHPI SNRs : 33/496 [00:06<01:04, 7.13it/s]
8%|▊ | cHPI SNRs : 39/496 [00:06<00:52, 8.78it/s]
9%|▉ | cHPI SNRs : 45/496 [00:06<00:42, 10.50it/s]
10%|█ | cHPI SNRs : 52/496 [00:06<00:35, 12.59it/s]
12%|█▏ | cHPI SNRs : 59/496 [00:06<00:29, 14.76it/s]
13%|█▎ | cHPI SNRs : 65/496 [00:06<00:25, 16.70it/s]
14%|█▍ | cHPI SNRs : 71/496 [00:06<00:22, 18.72it/s]
16%|█▌ | cHPI SNRs : 77/496 [00:06<00:20, 20.82it/s]
17%|█▋ | cHPI SNRs : 84/496 [00:06<00:17, 23.36it/s]
18%|█▊ | cHPI SNRs : 91/496 [00:06<00:15, 26.01it/s]
20%|█▉ | cHPI SNRs : 98/496 [00:06<00:13, 28.75it/s]
21%|██ | cHPI SNRs : 105/496 [00:06<00:12, 31.59it/s]
23%|██▎ | cHPI SNRs : 112/496 [00:06<00:11, 34.53it/s]
24%|██▍ | cHPI SNRs : 118/496 [00:06<00:10, 37.13it/s]
25%|██▌ | cHPI SNRs : 125/496 [00:06<00:09, 40.28it/s]
26%|██▋ | cHPI SNRs : 131/496 [00:06<00:08, 43.07it/s]
28%|██▊ | cHPI SNRs : 138/496 [00:06<00:07, 46.44it/s]
29%|██▉ | cHPI SNRs : 144/496 [00:06<00:07, 49.41it/s]
30%|███ | cHPI SNRs : 150/496 [00:06<00:06, 52.48it/s]
32%|███▏ | cHPI SNRs : 157/496 [00:06<00:06, 56.18it/s]
33%|███▎ | cHPI SNRs : 164/496 [00:06<00:05, 59.99it/s]
34%|███▍ | cHPI SNRs : 171/496 [00:06<00:05, 63.90it/s]
36%|███▌ | cHPI SNRs : 178/496 [00:06<00:04, 67.92it/s]
37%|███▋ | cHPI SNRs : 185/496 [00:06<00:04, 72.04it/s]
39%|███▊ | cHPI SNRs : 192/496 [00:06<00:03, 76.26it/s]
40%|████ | cHPI SNRs : 199/496 [00:06<00:03, 80.57it/s]
42%|████▏ | cHPI SNRs : 206/496 [00:06<00:03, 84.99it/s]
43%|████▎ | cHPI SNRs : 213/496 [00:06<00:03, 89.49it/s]
44%|████▍ | cHPI SNRs : 220/496 [00:06<00:02, 94.08it/s]
46%|████▌ | cHPI SNRs : 226/496 [00:06<00:02, 98.06it/s]
47%|████▋ | cHPI SNRs : 233/496 [00:06<00:02, 102.84it/s]
48%|████▊ | cHPI SNRs : 240/496 [00:06<00:02, 107.72it/s]
50%|████▉ | cHPI SNRs : 246/496 [00:06<00:02, 111.93it/s]
51%|█████ | cHPI SNRs : 253/496 [00:06<00:02, 116.97it/s]
52%|█████▏ | cHPI SNRs : 259/496 [00:06<00:01, 121.30it/s]
54%|█████▎ | cHPI SNRs : 266/496 [00:06<00:01, 126.48it/s]
55%|█████▌ | cHPI SNRs : 273/496 [00:06<00:01, 131.71it/s]
56%|█████▋ | cHPI SNRs : 280/496 [00:06<00:01, 137.00it/s]
58%|█████▊ | cHPI SNRs : 287/496 [00:06<00:01, 142.33it/s]
59%|█████▉ | cHPI SNRs : 294/496 [00:06<00:01, 147.71it/s]
60%|██████ | cHPI SNRs : 300/496 [00:06<00:01, 152.30it/s]
62%|██████▏ | cHPI SNRs : 307/496 [00:06<00:01, 157.73it/s]
63%|██████▎ | cHPI SNRs : 314/496 [00:06<00:01, 163.20it/s]
65%|██████▍ | cHPI SNRs : 321/496 [00:06<00:01, 168.67it/s]
66%|██████▌ | cHPI SNRs : 328/496 [00:06<00:00, 174.13it/s]
67%|██████▋ | cHPI SNRs : 334/496 [00:06<00:00, 178.68it/s]
69%|██████▉ | cHPI SNRs : 341/496 [00:07<00:00, 184.17it/s]
70%|███████ | cHPI SNRs : 348/496 [00:07<00:00, 189.60it/s]
71%|███████▏ | cHPI SNRs : 354/496 [00:07<00:00, 194.15it/s]
73%|███████▎ | cHPI SNRs : 360/496 [00:07<00:00, 198.75it/s]
74%|███████▍ | cHPI SNRs : 367/496 [00:07<00:00, 204.20it/s]
75%|███████▌ | cHPI SNRs : 373/496 [00:07<00:00, 208.62it/s]
77%|███████▋ | cHPI SNRs : 380/496 [00:07<00:00, 213.93it/s]
78%|███████▊ | cHPI SNRs : 387/496 [00:07<00:00, 219.21it/s]
79%|███████▉ | cHPI SNRs : 394/496 [00:07<00:00, 224.42it/s]
81%|████████ | cHPI SNRs : 401/496 [00:07<00:00, 229.56it/s]
82%|████████▏ | cHPI SNRs : 407/496 [00:07<00:00, 233.70it/s]
83%|████████▎ | cHPI SNRs : 412/496 [00:07<00:00, 235.10it/s]
84%|████████▎ | cHPI SNRs : 415/496 [00:07<00:00, 232.01it/s]
84%|████████▍ | cHPI SNRs : 419/496 [00:07<00:00, 230.89it/s]
85%|████████▌ | cHPI SNRs : 423/496 [00:07<00:00, 230.70it/s]
86%|████████▌ | cHPI SNRs : 427/496 [00:07<00:00, 230.43it/s]
87%|████████▋ | cHPI SNRs : 431/496 [00:07<00:00, 230.13it/s]
88%|████████▊ | cHPI SNRs : 434/496 [00:07<00:00, 228.47it/s]
88%|████████▊ | cHPI SNRs : 437/496 [00:07<00:00, 225.64it/s]
89%|████████▊ | cHPI SNRs : 440/496 [00:07<00:00, 220.52it/s]
89%|████████▉ | cHPI SNRs : 443/496 [00:07<00:00, 218.96it/s]
90%|█████████ | cHPI SNRs : 447/496 [00:07<00:00, 217.96it/s]
91%|█████████ | cHPI SNRs : 451/496 [00:07<00:00, 216.81it/s]
92%|█████████▏| cHPI SNRs : 455/496 [00:07<00:00, 215.82it/s]
93%|█████████▎| cHPI SNRs : 459/496 [00:07<00:00, 214.90it/s]
93%|█████████▎| cHPI SNRs : 463/496 [00:07<00:00, 213.99it/s]
94%|█████████▍| cHPI SNRs : 467/496 [00:07<00:00, 213.13it/s]
95%|█████████▍| cHPI SNRs : 471/496 [00:07<00:00, 212.24it/s]
96%|█████████▌| cHPI SNRs : 475/496 [00:07<00:00, 211.29it/s]
96%|█████████▋| cHPI SNRs : 478/496 [00:07<00:00, 210.37it/s]
97%|█████████▋| cHPI SNRs : 482/496 [00:07<00:00, 209.51it/s]
98%|█████████▊| cHPI SNRs : 486/496 [00:07<00:00, 208.55it/s]
99%|█████████▉| cHPI SNRs : 490/496 [00:07<00:00, 207.49it/s]
99%|█████████▉| cHPI SNRs : 493/496 [00:07<00:00, 206.50it/s]
100%|██████████| cHPI SNRs : 496/496 [00:07<00:00, 205.69it/s]
100%|██████████| cHPI SNRs : 496/496 [00:07<00:00, 64.82it/s]
Total running time of the script: (0 minutes 44.183 seconds)