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)
EEG, Gradiometers, Magnetometers
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:03<1:37:55,    3.47s/it]
  1%|          | cHPI amplitudes : 14/1696 [00:03<06:39,    4.21it/s]
  2%|▏         | cHPI amplitudes : 29/1696 [00:03<03:06,    8.94it/s]
  3%|▎         | cHPI amplitudes : 43/1696 [00:03<02:02,   13.51it/s]
  3%|▎         | cHPI amplitudes : 57/1696 [00:03<01:29,   18.27it/s]
  4%|▍         | cHPI amplitudes : 67/1696 [00:03<01:14,   21.78it/s]
  5%|▍         | cHPI amplitudes : 81/1696 [00:03<00:59,   26.95it/s]
  6%|▌         | cHPI amplitudes : 96/1696 [00:03<00:48,   32.70it/s]
  7%|▋         | cHPI amplitudes : 114/1696 [00:03<00:39,   39.94it/s]
  8%|▊         | cHPI amplitudes : 129/1696 [00:03<00:33,   46.14it/s]
  8%|▊         | cHPI amplitudes : 144/1696 [00:03<00:29,   52.56it/s]
  9%|▉         | cHPI amplitudes : 158/1696 [00:03<00:26,   58.75it/s]
 10%|█         | cHPI amplitudes : 172/1696 [00:03<00:23,   65.19it/s]
 11%|█         | cHPI amplitudes : 188/1696 [00:03<00:20,   72.85it/s]
 12%|█▏        | cHPI amplitudes : 205/1696 [00:03<00:18,   81.32it/s]
 13%|█▎        | cHPI amplitudes : 220/1696 [00:03<00:16,   88.96it/s]
 14%|█▍        | cHPI amplitudes : 235/1696 [00:03<00:15,   96.83it/s]
 15%|█▍        | cHPI amplitudes : 249/1696 [00:03<00:13,  104.40it/s]
 16%|█▌        | cHPI amplitudes : 266/1696 [00:03<00:12,  114.09it/s]
 17%|█▋        | cHPI amplitudes : 282/1696 [00:03<00:11,  123.43it/s]
 18%|█▊        | cHPI amplitudes : 299/1696 [00:03<00:10,  133.67it/s]
 19%|█▊        | cHPI amplitudes : 314/1696 [00:03<00:09,  142.82it/s]
 20%|█▉        | cHPI amplitudes : 332/1696 [00:03<00:08,  154.42it/s]
 20%|██        | cHPI amplitudes : 347/1696 [00:03<00:08,  163.99it/s]
 21%|██▏       | cHPI amplitudes : 362/1696 [00:03<00:07,  173.80it/s]
 22%|██▏       | cHPI amplitudes : 377/1696 [00:03<00:07,  183.86it/s]
 23%|██▎       | cHPI amplitudes : 393/1696 [00:03<00:06,  195.00it/s]
 24%|██▍       | cHPI amplitudes : 408/1696 [00:03<00:06,  205.46it/s]
 25%|██▍       | cHPI amplitudes : 423/1696 [00:03<00:05,  216.17it/s]
 26%|██▌       | cHPI amplitudes : 438/1696 [00:03<00:05,  227.02it/s]
 27%|██▋       | cHPI amplitudes : 453/1696 [00:03<00:05,  238.13it/s]
 28%|██▊       | cHPI amplitudes : 470/1696 [00:03<00:04,  251.61it/s]
 29%|██▊       | cHPI amplitudes : 487/1696 [00:04<00:04,  265.13it/s]
 30%|██▉       | cHPI amplitudes : 502/1696 [00:04<00:04,  276.65it/s]
 31%|███       | cHPI amplitudes : 520/1696 [00:04<00:04,  291.94it/s]
 32%|███▏      | cHPI amplitudes : 536/1696 [00:04<00:03,  305.14it/s]
 32%|███▏      | cHPI amplitudes : 551/1696 [00:04<00:03,  317.30it/s]
 33%|███▎      | cHPI amplitudes : 565/1696 [00:04<00:03,  328.31it/s]
 34%|███▍      | cHPI amplitudes : 583/1696 [00:04<00:03,  344.23it/s]
 35%|███▌      | cHPI amplitudes : 598/1696 [00:04<00:03,  356.38it/s]
 36%|███▌      | cHPI amplitudes : 613/1696 [00:04<00:02,  368.48it/s]
 37%|███▋      | cHPI amplitudes : 621/1696 [00:04<00:02,  370.11it/s]
 37%|███▋      | cHPI amplitudes : 630/1696 [00:04<00:02,  374.22it/s]
 38%|███▊      | cHPI amplitudes : 646/1696 [00:04<00:02,  388.65it/s]
 39%|███▉      | cHPI amplitudes : 662/1696 [00:04<00:02,  403.36it/s]
 40%|███▉      | cHPI amplitudes : 674/1696 [00:04<00:02,  411.86it/s]
 41%|████      | cHPI amplitudes : 689/1696 [00:04<00:02,  424.61it/s]
 41%|████▏     | cHPI amplitudes : 703/1696 [00:04<00:02,  435.91it/s]
 42%|████▏     | cHPI amplitudes : 717/1696 [00:04<00:02,  447.20it/s]
 43%|████▎     | cHPI amplitudes : 732/1696 [00:04<00:02,  460.27it/s]
 44%|████▍     | cHPI amplitudes : 747/1696 [00:04<00:02,  473.44it/s]
 45%|████▍     | cHPI amplitudes : 762/1696 [00:04<00:01,  486.23it/s]
 46%|████▌     | cHPI amplitudes : 777/1696 [00:04<00:01,  499.17it/s]
 47%|████▋     | cHPI amplitudes : 792/1696 [00:04<00:01,  511.78it/s]
 48%|████▊     | cHPI amplitudes : 807/1696 [00:04<00:01,  522.15it/s]
 48%|████▊     | cHPI amplitudes : 822/1696 [00:04<00:01,  534.78it/s]
 49%|████▉     | cHPI amplitudes : 834/1696 [00:04<00:01,  536.25it/s]
 50%|████▉     | cHPI amplitudes : 842/1696 [00:04<00:01,  534.35it/s]
 51%|█████     | cHPI amplitudes : 857/1696 [00:04<00:01,  547.05it/s]
 51%|█████▏    | cHPI amplitudes : 870/1696 [00:04<00:01,  550.68it/s]
 52%|█████▏    | cHPI amplitudes : 885/1696 [00:04<00:01,  562.69it/s]
 53%|█████▎    | cHPI amplitudes : 900/1696 [00:04<00:01,  575.15it/s]
 54%|█████▍    | cHPI amplitudes : 915/1696 [00:04<00:01,  587.15it/s]
 55%|█████▍    | cHPI amplitudes : 930/1696 [00:04<00:01,  598.67it/s]
 56%|█████▌    | cHPI amplitudes : 945/1696 [00:04<00:01,  609.65it/s]
 57%|█████▋    | cHPI amplitudes : 960/1696 [00:04<00:01,  620.95it/s]
 57%|█████▋    | cHPI amplitudes : 975/1696 [00:04<00:01,  631.66it/s]
 58%|█████▊    | cHPI amplitudes : 990/1696 [00:04<00:01,  642.32it/s]
 59%|█████▉    | cHPI amplitudes : 1005/1696 [00:04<00:01,  652.89it/s]
 60%|██████    | cHPI amplitudes : 1020/1696 [00:04<00:01,  663.12it/s]
 61%|██████    | cHPI amplitudes : 1035/1696 [00:04<00:00,  673.10it/s]
 62%|██████▏   | cHPI amplitudes : 1050/1696 [00:04<00:00,  682.26it/s]
 63%|██████▎   | cHPI amplitudes : 1065/1696 [00:04<00:00,  691.41it/s]
 64%|██████▎   | cHPI amplitudes : 1080/1696 [00:04<00:00,  700.94it/s]
 65%|██████▍   | cHPI amplitudes : 1095/1696 [00:04<00:00,  710.16it/s]
 65%|██████▌   | cHPI amplitudes : 1110/1696 [00:04<00:00,  719.13it/s]
 66%|██████▋   | cHPI amplitudes : 1126/1696 [00:04<00:00,  728.52it/s]
 67%|██████▋   | cHPI amplitudes : 1142/1696 [00:04<00:00,  737.70it/s]
 68%|██████▊   | cHPI amplitudes : 1157/1696 [00:04<00:00,  745.40it/s]
 69%|██████▉   | cHPI amplitudes : 1172/1696 [00:04<00:00,  753.05it/s]
 70%|███████   | cHPI amplitudes : 1188/1696 [00:04<00:00,  761.58it/s]
 71%|███████   | cHPI amplitudes : 1203/1696 [00:04<00:00,  768.90it/s]
 72%|███████▏  | cHPI amplitudes : 1218/1696 [00:04<00:00,  775.92it/s]
 73%|███████▎  | cHPI amplitudes : 1233/1696 [00:04<00:00,  782.41it/s]
 74%|███████▎  | cHPI amplitudes : 1248/1696 [00:04<00:00,  788.16it/s]
 74%|███████▍  | cHPI amplitudes : 1263/1696 [00:04<00:00,  794.52it/s]
 75%|███████▌  | cHPI amplitudes : 1278/1696 [00:04<00:00,  800.52it/s]
 76%|███████▌  | cHPI amplitudes : 1293/1696 [00:04<00:00,  806.04it/s]
 77%|███████▋  | cHPI amplitudes : 1308/1696 [00:04<00:00,  811.41it/s]
 78%|███████▊  | cHPI amplitudes : 1323/1696 [00:04<00:00,  816.88it/s]
 79%|███████▉  | cHPI amplitudes : 1338/1696 [00:04<00:00,  821.68it/s]
 80%|███████▉  | cHPI amplitudes : 1353/1696 [00:04<00:00,  826.52it/s]
 81%|████████  | cHPI amplitudes : 1368/1696 [00:05<00:00,  829.88it/s]
 81%|████████▏ | cHPI amplitudes : 1382/1696 [00:05<00:00,  831.04it/s]
 82%|████████▏ | cHPI amplitudes : 1396/1696 [00:05<00:00,  831.57it/s]
 83%|████████▎ | cHPI amplitudes : 1410/1696 [00:05<00:00,  831.73it/s]
 84%|████████▍ | cHPI amplitudes : 1424/1696 [00:05<00:00,  832.30it/s]
 85%|████████▍ | cHPI amplitudes : 1438/1696 [00:05<00:00,  833.73it/s]
 86%|████████▌ | cHPI amplitudes : 1453/1696 [00:05<00:00,  838.19it/s]
 87%|████████▋ | cHPI amplitudes : 1468/1696 [00:05<00:00,  840.85it/s]
 87%|████████▋ | cHPI amplitudes : 1483/1696 [00:05<00:00,  844.17it/s]
 88%|████████▊ | cHPI amplitudes : 1498/1696 [00:05<00:00,  846.94it/s]
 89%|████████▉ | cHPI amplitudes : 1509/1696 [00:05<00:00,  838.57it/s]
 90%|████████▉ | cHPI amplitudes : 1524/1696 [00:05<00:00,  842.29it/s]
 91%|█████████ | cHPI amplitudes : 1539/1696 [00:05<00:00,  845.97it/s]
 91%|█████████▏| cHPI amplitudes : 1550/1696 [00:05<00:00,  838.07it/s]
 92%|█████████▏| cHPI amplitudes : 1558/1696 [00:05<00:00,  781.21it/s]
 93%|█████████▎| cHPI amplitudes : 1572/1696 [00:05<00:00,  782.93it/s]
 93%|█████████▎| cHPI amplitudes : 1581/1696 [00:05<00:00,  765.54it/s]
 94%|█████████▍| cHPI amplitudes : 1591/1696 [00:05<00:00,  757.10it/s]
 95%|█████████▍| cHPI amplitudes : 1606/1696 [00:05<00:00,  764.32it/s]
 96%|█████████▌| cHPI amplitudes : 1621/1696 [00:05<00:00,  770.16it/s]
 96%|█████████▋| cHPI amplitudes : 1635/1696 [00:05<00:00,  774.45it/s]
 97%|█████████▋| cHPI amplitudes : 1650/1696 [00:05<00:00,  780.87it/s]
 98%|█████████▊| cHPI amplitudes : 1670/1696 [00:05<00:00,  801.01it/s]
 99%|█████████▉| cHPI amplitudes : 1685/1696 [00:05<00:00,  804.91it/s]
100%|██████████| cHPI amplitudes : 1696/1696 [00:05<00:00,  313.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<34:24,    1.22s/it]
  3%|▎         | cHPI locations  : 47/1696 [00:01<00:41,   40.00it/s]
  5%|▌         | cHPI locations  : 85/1696 [00:01<00:22,   73.03it/s]
  6%|▌         | cHPI locations  : 101/1696 [00:01<00:20,   79.24it/s]
  7%|▋         | cHPI locations  : 127/1696 [00:01<00:15,  100.74it/s]
  9%|▉         | cHPI locations  : 156/1696 [00:01<00:12,  125.41it/s]
 11%|█         | cHPI locations  : 184/1696 [00:01<00:10,  149.66it/s]
 12%|█▏        | cHPI locations  : 201/1696 [00:01<00:09,  150.31it/s]
 13%|█▎        | cHPI locations  : 227/1696 [00:01<00:08,  172.01it/s]
 15%|█▍        | cHPI locations  : 253/1696 [00:01<00:07,  194.20it/s]
 17%|█▋        | cHPI locations  : 287/1696 [00:01<00:06,  224.74it/s]
 18%|█▊        | cHPI locations  : 301/1696 [00:01<00:06,  222.76it/s]
 20%|█▉        | cHPI locations  : 336/1696 [00:01<00:05,  254.22it/s]
 22%|██▏       | cHPI locations  : 367/1696 [00:01<00:04,  282.05it/s]
 23%|██▎       | cHPI locations  : 397/1696 [00:01<00:04,  309.37it/s]
 24%|██▎       | cHPI locations  : 401/1696 [00:01<00:04,  286.89it/s]
 26%|██▌       | cHPI locations  : 434/1696 [00:01<00:03,  317.14it/s]
 28%|██▊       | cHPI locations  : 471/1696 [00:01<00:03,  352.39it/s]
 30%|██▉       | cHPI locations  : 501/1696 [00:01<00:03,  346.04it/s]
 31%|███▏      | cHPI locations  : 532/1696 [00:01<00:03,  373.32it/s]
 33%|███▎      | cHPI locations  : 559/1696 [00:02<00:03,  359.01it/s]
 33%|███▎      | cHPI locations  : 568/1696 [00:02<00:03,  327.27it/s]
 34%|███▍      | cHPI locations  : 573/1696 [00:02<00:03,  296.35it/s]
 34%|███▍      | cHPI locations  : 577/1696 [00:02<00:04,  271.16it/s]
 34%|███▍      | cHPI locations  : 581/1696 [00:02<00:04,  250.11it/s]
 35%|███▍      | cHPI locations  : 587/1696 [00:02<00:04,  229.10it/s]
 35%|███▌      | cHPI locations  : 600/1696 [00:02<00:05,  218.27it/s]
 38%|███▊      | cHPI locations  : 638/1696 [00:02<00:04,  244.41it/s]
 40%|███▉      | cHPI locations  : 674/1696 [00:02<00:03,  269.62it/s]
 41%|████▏     | cHPI locations  : 700/1696 [00:02<00:03,  262.37it/s]
 43%|████▎     | cHPI locations  : 736/1696 [00:02<00:03,  287.02it/s]
 46%|████▌     | cHPI locations  : 774/1696 [00:02<00:02,  313.88it/s]
 47%|████▋     | cHPI locations  : 789/1696 [00:03<00:03,  294.70it/s]
 47%|████▋     | cHPI locations  : 794/1696 [00:03<00:03,  268.90it/s]
 47%|████▋     | cHPI locations  : 800/1696 [00:03<00:03,  248.85it/s]
 47%|████▋     | cHPI locations  : 805/1696 [00:03<00:03,  232.81it/s]
 48%|████▊     | cHPI locations  : 810/1696 [00:03<00:04,  216.53it/s]
 48%|████▊     | cHPI locations  : 815/1696 [00:03<00:04,  202.93it/s]
 48%|████▊     | cHPI locations  : 819/1696 [00:03<00:04,  190.75it/s]
 49%|████▊     | cHPI locations  : 823/1696 [00:03<00:04,  178.03it/s]
 49%|████▉     | cHPI locations  : 827/1696 [00:04<00:05,  167.56it/s]
 49%|████▉     | cHPI locations  : 833/1696 [00:04<00:05,  158.98it/s]
 50%|█████     | cHPI locations  : 848/1696 [00:04<00:05,  156.77it/s]
 53%|█████▎    | cHPI locations  : 893/1696 [00:04<00:04,  181.60it/s]
 55%|█████▍    | cHPI locations  : 930/1696 [00:04<00:03,  202.37it/s]
 56%|█████▌    | cHPI locations  : 948/1696 [00:04<00:03,  199.67it/s]
 58%|█████▊    | cHPI locations  : 978/1696 [00:04<00:03,  216.33it/s]
 60%|█████▉    | cHPI locations  : 1013/1696 [00:04<00:02,  236.79it/s]
 61%|██████▏   | cHPI locations  : 1040/1696 [00:04<00:02,  237.15it/s]
 62%|██████▏   | cHPI locations  : 1051/1696 [00:04<00:02,  226.77it/s]
 63%|██████▎   | cHPI locations  : 1062/1696 [00:04<00:02,  217.10it/s]
 63%|██████▎   | cHPI locations  : 1072/1696 [00:04<00:03,  207.02it/s]
 64%|██████▍   | cHPI locations  : 1086/1696 [00:05<00:03,  200.61it/s]
 66%|██████▌   | cHPI locations  : 1117/1696 [00:05<00:02,  217.35it/s]
 68%|██████▊   | cHPI locations  : 1152/1696 [00:05<00:02,  237.12it/s]
 70%|██████▉   | cHPI locations  : 1186/1696 [00:05<00:02,  239.75it/s]
 72%|███████▏  | cHPI locations  : 1216/1696 [00:05<00:01,  256.34it/s]
 74%|███████▍  | cHPI locations  : 1251/1696 [00:05<00:01,  276.75it/s]
 76%|███████▌  | cHPI locations  : 1286/1696 [00:05<00:01,  278.98it/s]
 76%|███████▋  | cHPI locations  : 1296/1696 [00:05<00:01,  264.39it/s]
 77%|███████▋  | cHPI locations  : 1301/1696 [00:05<00:01,  249.94it/s]
 77%|███████▋  | cHPI locations  : 1306/1696 [00:05<00:01,  235.52it/s]
 77%|███████▋  | cHPI locations  : 1311/1696 [00:05<00:01,  220.58it/s]
 78%|███████▊  | cHPI locations  : 1315/1696 [00:06<00:01,  206.59it/s]
 78%|███████▊  | cHPI locations  : 1319/1696 [00:06<00:01,  194.35it/s]
 78%|███████▊  | cHPI locations  : 1325/1696 [00:06<00:02,  183.66it/s]
 78%|███████▊  | cHPI locations  : 1331/1696 [00:06<00:02,  175.70it/s]
 79%|███████▉  | cHPI locations  : 1341/1696 [00:06<00:02,  171.39it/s]
 82%|████████▏ | cHPI locations  : 1384/1696 [00:06<00:01,  194.03it/s]
 84%|████████▍ | cHPI locations  : 1428/1696 [00:06<00:01,  218.01it/s]
 85%|████████▍ | cHPI locations  : 1441/1696 [00:06<00:01,  214.07it/s]
 87%|████████▋ | cHPI locations  : 1477/1696 [00:06<00:00,  233.89it/s]
 89%|████████▉ | cHPI locations  : 1510/1696 [00:06<00:00,  252.45it/s]
 91%|█████████ | cHPI locations  : 1541/1696 [00:06<00:00,  249.87it/s]
 92%|█████████▏| cHPI locations  : 1565/1696 [00:06<00:00,  241.82it/s]
 93%|█████████▎| cHPI locations  : 1573/1696 [00:07<00:00,  228.79it/s]
 93%|█████████▎| cHPI locations  : 1580/1696 [00:07<00:00,  219.98it/s]
 94%|█████████▎| cHPI locations  : 1586/1696 [00:07<00:00,  210.72it/s]
 94%|█████████▍| cHPI locations  : 1590/1696 [00:07<00:00,  198.94it/s]
 94%|█████████▍| cHPI locations  : 1594/1696 [00:07<00:00,  189.72it/s]
 94%|█████████▍| cHPI locations  : 1599/1696 [00:07<00:00,  180.04it/s]
 95%|█████████▍| cHPI locations  : 1609/1696 [00:07<00:00,  172.95it/s]
 97%|█████████▋| cHPI locations  : 1638/1696 [00:07<00:00,  187.47it/s]
 97%|█████████▋| cHPI locations  : 1652/1696 [00:07<00:00,  181.39it/s]
100%|█████████▉| cHPI locations  : 1689/1696 [00:07<00:00,  200.56it/s]
100%|██████████| cHPI locations  : 1696/1696 [00:07<00:00,  214.58it/s]

Lastly, compute head positions from the coil locations:

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.1
t=12.000:    5/5 good HPI fits, movements [mm/s] =      0.0 /      0.0 /      0.0 /      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.4 /     34.6 /     39.9 /     37.8 /     30.2
t=14.720:    5/5 good HPI fits, movements [mm/s] =     48.8 /     47.1 /     60.5 /     59.5 /     44.5
t=14.760:    5/5 good HPI fits, movements [mm/s] =     65.6 /     62.8 /     72.6 /     70.8 /     56.3
t=14.800:    5/5 good HPI fits, movements [mm/s] =     66.7 /     59.4 /     83.1 /     78.2 /     60.9
t=14.860:    5/5 good HPI fits, movements [mm/s] =     54.9 /     52.3 /     57.8 /     62.1 /     47.2
t=14.990:    5/5 good HPI fits, movements [mm/s] =     23.0 /     22.6 /     24.0 /     27.0 /     19.9
t=15.990:    5/5 good HPI fits, movements [mm/s] =      0.7 /      0.9 /      0.8 /      0.9 /      0.9
t=16.880:    5/5 good HPI fits, movements [mm/s] =      2.5 /      3.4 /      2.9 /      3.8 /      3.3
t=16.930:    5/5 good HPI fits, movements [mm/s] =     51.7 /     55.4 /     58.5 /     67.0 /     59.2
t=16.990:    5/5 good HPI fits, movements [mm/s] =     56.8 /     53.8 /     62.0 /     65.1 /     57.7
t=17.040:    5/5 good HPI fits, movements [mm/s] =     53.9 /     50.1 /     60.0 /     61.6 /     49.4
t=17.090:    5/5 good HPI fits, movements [mm/s] =     57.7 /     50.2 /     66.4 /     65.2 /     51.6
t=17.140:    5/5 good HPI fits, movements [mm/s] =     60.4 /     54.4 /     73.0 /     67.2 /     57.6
t=17.180:    5/5 good HPI fits, movements [mm/s] =     63.7 /     58.0 /     74.3 /     71.8 /     61.7
t=17.220:    5/5 good HPI fits, movements [mm/s] =     60.9 /     61.0 /     75.3 /     71.5 /     63.3
t=17.260:    5/5 good HPI fits, movements [mm/s] =     67.8 /     65.5 /     73.2 /     68.7 /     69.1
t=17.320:    5/5 good HPI fits, movements [mm/s] =     49.6 /     51.0 /     55.0 /     55.1 /     52.6
t=17.470:    5/5 good HPI fits, movements [mm/s] =     18.5 /     21.1 /     19.3 /     20.5 /     17.4
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.5 /      2.7 /      1.9 /      2.9 /      2.5
t=19.500:    5/5 good HPI fits, movements [mm/s] =     23.0 /     25.0 /     19.4 /     28.8 /     27.6
t=19.610:    5/5 good HPI fits, movements [mm/s] =     21.3 /     25.7 /     21.8 /     31.7 /     31.4
t=19.710:    5/5 good HPI fits, movements [mm/s] =     21.6 /     20.6 /     23.0 /     29.8 /     27.7
t=19.850:    5/5 good HPI fits, movements [mm/s] =     16.5 /     14.7 /     18.7 /     21.8 /     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.1 /     26.4
t=22.000:    5/5 good HPI fits, movements [mm/s] =     66.1 /     59.2 /     46.5 /     64.7 /     51.9
t=22.050:    5/5 good HPI fits, movements [mm/s] =     60.0 /     62.4 /     51.0 /     69.3 /     57.9
t=22.100:    5/5 good HPI fits, movements [mm/s] =     61.0 /     65.8 /     58.5 /     74.9 /     64.8
t=22.140:    5/5 good HPI fits, movements [mm/s] =     74.7 /     73.5 /     62.3 /     79.8 /     68.1
t=22.180:    5/5 good HPI fits, movements [mm/s] =     63.6 /     59.2 /     53.0 /     67.5 /     53.8
t=22.240:    5/5 good HPI fits, movements [mm/s] =     38.5 /     39.4 /     36.9 /     48.0 /     42.0
t=22.300:    5/5 good HPI fits, movements [mm/s] =     41.9 /     40.0 /     33.4 /     47.4 /     39.0
t=22.400:    5/5 good HPI fits, movements [mm/s] =     31.9 /     28.6 /     22.1 /     30.7 /     25.2
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.3 /      0.8 /      0.7 /      0.8 /      0.7
t=24.640:    5/5 good HPI fits, movements [mm/s] =     12.6 /     11.5 /     11.5 /     13.1 /     10.9
t=24.720:    5/5 good HPI fits, movements [mm/s] =     30.2 /     33.2 /     31.7 /     38.9 /     35.2
t=24.790:    5/5 good HPI fits, movements [mm/s] =     31.2 /     35.9 /     33.0 /     43.1 /     41.0
t=24.850:    5/5 good HPI fits, movements [mm/s] =     37.5 /     40.8 /     35.6 /     44.9 /     42.1
t=24.890:    5/5 good HPI fits, movements [mm/s] =     50.5 /     51.9 /     49.9 /     59.7 /     53.3
t=24.930:    5/5 good HPI fits, movements [mm/s] =     55.8 /     55.0 /     59.2 /     68.7 /     61.7
t=24.980:    5/5 good HPI fits, movements [mm/s] =     47.3 /     47.2 /     50.9 /     58.5 /     53.7
t=25.080:    5/5 good HPI fits, movements [mm/s] =     23.7 /     19.3 /     23.1 /     28.1 /     26.3
t=25.510:    5/5 good HPI fits, movements [mm/s] =      2.4 /      6.6 /      7.3 /      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:

Position (mm), Rotation (quat)

Or we can visualize them as a continuous field (with the vectors pointing in the head-upward direction):

59 head positions

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:

SNR, magnetometers, cHPI power, magnetometers, Residual variance, magnetometers, SNR, gradiometers, cHPI power, gradiometers, Residual variance, gradiometers
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:05<41:38,    5.05s/it]
  1%|          | cHPI SNRs : 6/496 [00:05<06:36,    1.24it/s]
  2%|▏         | cHPI SNRs : 11/496 [00:05<03:28,    2.32it/s]
  3%|▎         | cHPI SNRs : 16/496 [00:05<02:18,    3.45it/s]
  4%|▍         | cHPI SNRs : 21/496 [00:05<01:42,    4.64it/s]
  5%|▌         | cHPI SNRs : 26/496 [00:05<01:20,    5.87it/s]
  6%|▋         | cHPI SNRs : 31/496 [00:05<01:04,    7.16it/s]
  7%|▋         | cHPI SNRs : 36/496 [00:05<00:54,    8.51it/s]
  8%|▊         | cHPI SNRs : 41/496 [00:05<00:45,    9.91it/s]
  9%|▉         | cHPI SNRs : 46/496 [00:05<00:39,   11.37it/s]
 10%|█         | cHPI SNRs : 51/496 [00:05<00:34,   12.89it/s]
 11%|█▏        | cHPI SNRs : 56/496 [00:05<00:30,   14.47it/s]
 12%|█▏        | cHPI SNRs : 61/496 [00:05<00:26,   16.12it/s]
 13%|█▎        | cHPI SNRs : 66/496 [00:05<00:24,   17.83it/s]
 14%|█▍        | cHPI SNRs : 71/496 [00:05<00:21,   19.62it/s]
 15%|█▌        | cHPI SNRs : 76/496 [00:05<00:19,   21.47it/s]
 16%|█▋        | cHPI SNRs : 81/496 [00:05<00:17,   23.38it/s]
 17%|█▋        | cHPI SNRs : 86/496 [00:05<00:16,   25.37it/s]
 18%|█▊        | cHPI SNRs : 91/496 [00:05<00:14,   27.44it/s]
 19%|█▉        | cHPI SNRs : 96/496 [00:05<00:13,   29.58it/s]
 20%|██        | cHPI SNRs : 101/496 [00:05<00:12,   31.81it/s]
 21%|██▏       | cHPI SNRs : 106/496 [00:05<00:11,   34.11it/s]
 22%|██▏       | cHPI SNRs : 111/496 [00:05<00:10,   36.49it/s]
 23%|██▎       | cHPI SNRs : 116/496 [00:05<00:09,   38.95it/s]
 24%|██▍       | cHPI SNRs : 121/496 [00:05<00:09,   41.49it/s]
 25%|██▌       | cHPI SNRs : 126/496 [00:05<00:08,   44.10it/s]
 26%|██▋       | cHPI SNRs : 131/496 [00:05<00:07,   46.79it/s]
 27%|██▋       | cHPI SNRs : 136/496 [00:05<00:07,   49.56it/s]
 28%|██▊       | cHPI SNRs : 141/496 [00:05<00:06,   52.42it/s]
 29%|██▉       | cHPI SNRs : 146/496 [00:05<00:06,   55.35it/s]
 30%|███       | cHPI SNRs : 151/496 [00:05<00:05,   58.36it/s]
 31%|███▏      | cHPI SNRs : 156/496 [00:05<00:05,   61.44it/s]
 32%|███▏      | cHPI SNRs : 161/496 [00:05<00:05,   64.61it/s]
 33%|███▎      | cHPI SNRs : 165/496 [00:05<00:04,   67.05it/s]
 34%|███▍      | cHPI SNRs : 170/496 [00:05<00:04,   70.34it/s]
 35%|███▌      | cHPI SNRs : 175/496 [00:05<00:04,   73.59it/s]
 36%|███▌      | cHPI SNRs : 179/496 [00:05<00:04,   76.18it/s]
 37%|███▋      | cHPI SNRs : 184/496 [00:05<00:03,   79.63it/s]
 38%|███▊      | cHPI SNRs : 189/496 [00:05<00:03,   83.19it/s]
 39%|███▉      | cHPI SNRs : 194/496 [00:05<00:03,   86.81it/s]
 40%|████      | cHPI SNRs : 199/496 [00:05<00:03,   90.48it/s]
 41%|████      | cHPI SNRs : 204/496 [00:05<00:03,   94.00it/s]
 42%|████▏     | cHPI SNRs : 208/496 [00:05<00:02,   96.71it/s]
 43%|████▎     | cHPI SNRs : 213/496 [00:05<00:02,  100.30it/s]
 44%|████▍     | cHPI SNRs : 217/496 [00:05<00:02,  103.07it/s]
 45%|████▍     | cHPI SNRs : 221/496 [00:05<00:02,  105.67it/s]
 45%|████▌     | cHPI SNRs : 225/496 [00:05<00:02,  108.47it/s]
 46%|████▋     | cHPI SNRs : 230/496 [00:05<00:02,  112.30it/s]
 47%|████▋     | cHPI SNRs : 234/496 [00:05<00:02,  115.14it/s]
 48%|████▊     | cHPI SNRs : 238/496 [00:05<00:02,  117.90it/s]
 49%|████▉     | cHPI SNRs : 243/496 [00:05<00:02,  121.92it/s]
 50%|█████     | cHPI SNRs : 248/496 [00:05<00:01,  126.17it/s]
 51%|█████     | cHPI SNRs : 253/496 [00:05<00:01,  130.49it/s]
 52%|█████▏    | cHPI SNRs : 258/496 [00:05<00:01,  134.84it/s]
 53%|█████▎    | cHPI SNRs : 263/496 [00:05<00:01,  139.15it/s]
 54%|█████▍    | cHPI SNRs : 268/496 [00:05<00:01,  143.45it/s]
 55%|█████▌    | cHPI SNRs : 273/496 [00:06<00:01,  147.76it/s]
 56%|█████▌    | cHPI SNRs : 278/496 [00:06<00:01,  152.07it/s]
 57%|█████▋    | cHPI SNRs : 283/496 [00:06<00:01,  156.42it/s]
 58%|█████▊    | cHPI SNRs : 288/496 [00:06<00:01,  160.70it/s]
 59%|█████▉    | cHPI SNRs : 293/496 [00:06<00:01,  164.89it/s]
 60%|██████    | cHPI SNRs : 298/496 [00:06<00:01,  169.00it/s]
 61%|██████    | cHPI SNRs : 303/496 [00:06<00:01,  173.07it/s]
 62%|██████▏   | cHPI SNRs : 308/496 [00:06<00:01,  177.11it/s]
 63%|██████▎   | cHPI SNRs : 313/496 [00:06<00:01,  181.16it/s]
 64%|██████▍   | cHPI SNRs : 318/496 [00:06<00:00,  185.11it/s]
 65%|██████▌   | cHPI SNRs : 323/496 [00:06<00:00,  189.00it/s]
 66%|██████▌   | cHPI SNRs : 328/496 [00:06<00:00,  192.75it/s]
 67%|██████▋   | cHPI SNRs : 333/496 [00:06<00:00,  196.65it/s]
 68%|██████▊   | cHPI SNRs : 338/496 [00:06<00:00,  200.46it/s]
 69%|██████▉   | cHPI SNRs : 343/496 [00:06<00:00,  204.26it/s]
 70%|███████   | cHPI SNRs : 348/496 [00:06<00:00,  207.87it/s]
 71%|███████   | cHPI SNRs : 353/496 [00:06<00:00,  211.34it/s]
 72%|███████▏  | cHPI SNRs : 358/496 [00:06<00:00,  214.83it/s]
 73%|███████▎  | cHPI SNRs : 363/496 [00:06<00:00,  218.22it/s]
 74%|███████▍  | cHPI SNRs : 368/496 [00:06<00:00,  221.51it/s]
 75%|███████▌  | cHPI SNRs : 373/496 [00:06<00:00,  224.58it/s]
 76%|███████▌  | cHPI SNRs : 378/496 [00:06<00:00,  227.32it/s]
 77%|███████▋  | cHPI SNRs : 383/496 [00:06<00:00,  229.99it/s]
 78%|███████▊  | cHPI SNRs : 388/496 [00:06<00:00,  232.64it/s]
 79%|███████▉  | cHPI SNRs : 393/496 [00:06<00:00,  235.22it/s]
 80%|████████  | cHPI SNRs : 398/496 [00:06<00:00,  237.91it/s]
 81%|████████▏ | cHPI SNRs : 403/496 [00:06<00:00,  240.52it/s]
 82%|████████▏ | cHPI SNRs : 408/496 [00:06<00:00,  243.06it/s]
 83%|████████▎ | cHPI SNRs : 413/496 [00:06<00:00,  245.40it/s]
 84%|████████▍ | cHPI SNRs : 418/496 [00:06<00:00,  247.62it/s]
 85%|████████▌ | cHPI SNRs : 423/496 [00:06<00:00,  249.95it/s]
 86%|████████▋ | cHPI SNRs : 428/496 [00:06<00:00,  252.30it/s]
 87%|████████▋ | cHPI SNRs : 433/496 [00:06<00:00,  254.49it/s]
 88%|████████▊ | cHPI SNRs : 438/496 [00:06<00:00,  256.68it/s]
 89%|████████▉ | cHPI SNRs : 443/496 [00:06<00:00,  258.61it/s]
 90%|█████████ | cHPI SNRs : 448/496 [00:06<00:00,  260.55it/s]
 91%|█████████▏| cHPI SNRs : 453/496 [00:06<00:00,  262.34it/s]
 92%|█████████▏| cHPI SNRs : 458/496 [00:06<00:00,  264.13it/s]
 93%|█████████▎| cHPI SNRs : 463/496 [00:06<00:00,  266.02it/s]
 94%|█████████▍| cHPI SNRs : 468/496 [00:06<00:00,  267.82it/s]
 95%|█████████▌| cHPI SNRs : 473/496 [00:06<00:00,  269.27it/s]
 96%|█████████▋| cHPI SNRs : 478/496 [00:06<00:00,  270.62it/s]
 97%|█████████▋| cHPI SNRs : 483/496 [00:06<00:00,  271.66it/s]
 98%|█████████▊| cHPI SNRs : 488/496 [00:06<00:00,  272.96it/s]
 99%|█████████▉| cHPI SNRs : 493/496 [00:06<00:00,  273.37it/s]
100%|██████████| cHPI SNRs : 496/496 [00:06<00:00,   73.49it/s]

Total running time of the script: (0 minutes 48.813 seconds)

Estimated memory usage: 17 MB

Gallery generated by Sphinx-Gallery