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:04<2:18:41,    4.91s/it]
  1%|          | cHPI amplitudes : 15/1696 [00:04<08:46,    3.19it/s]
  2%|▏         | cHPI amplitudes : 29/1696 [00:04<04:23,    6.32it/s]
  3%|▎         | cHPI amplitudes : 43/1696 [00:04<02:52,    9.58it/s]
  3%|▎         | cHPI amplitudes : 57/1696 [00:04<02:06,   12.99it/s]
  4%|▍         | cHPI amplitudes : 71/1696 [00:04<01:38,   16.55it/s]
  5%|▍         | cHPI amplitudes : 84/1696 [00:05<01:20,   19.99it/s]
  6%|▌         | cHPI amplitudes : 98/1696 [00:05<01:06,   23.86it/s]
  7%|▋         | cHPI amplitudes : 112/1696 [00:05<00:56,   27.90it/s]
  7%|▋         | cHPI amplitudes : 126/1696 [00:05<00:48,   32.10it/s]
  8%|▊         | cHPI amplitudes : 139/1696 [00:05<00:43,   36.16it/s]
  9%|▉         | cHPI amplitudes : 153/1696 [00:05<00:37,   40.73it/s]
 10%|▉         | cHPI amplitudes : 167/1696 [00:05<00:33,   45.48it/s]
 11%|█         | cHPI amplitudes : 180/1696 [00:05<00:30,   50.05it/s]
 11%|█▏        | cHPI amplitudes : 194/1696 [00:05<00:27,   55.19it/s]
 12%|█▏        | cHPI amplitudes : 208/1696 [00:05<00:24,   60.52it/s]
 13%|█▎        | cHPI amplitudes : 222/1696 [00:05<00:22,   66.06it/s]
 14%|█▍        | cHPI amplitudes : 236/1696 [00:05<00:20,   71.81it/s]
 15%|█▍        | cHPI amplitudes : 250/1696 [00:05<00:18,   77.78it/s]
 16%|█▌        | cHPI amplitudes : 264/1696 [00:05<00:17,   83.95it/s]
 16%|█▋        | cHPI amplitudes : 278/1696 [00:05<00:15,   90.36it/s]
 17%|█▋        | cHPI amplitudes : 292/1696 [00:05<00:14,   96.98it/s]
 18%|█▊        | cHPI amplitudes : 306/1696 [00:05<00:13,  103.82it/s]
 19%|█▉        | cHPI amplitudes : 320/1696 [00:05<00:12,  110.91it/s]
 20%|█▉        | cHPI amplitudes : 334/1696 [00:05<00:11,  118.18it/s]
 21%|██        | cHPI amplitudes : 348/1696 [00:05<00:10,  125.67it/s]
 21%|██▏       | cHPI amplitudes : 362/1696 [00:05<00:09,  133.44it/s]
 22%|██▏       | cHPI amplitudes : 376/1696 [00:05<00:09,  141.44it/s]
 23%|██▎       | cHPI amplitudes : 390/1696 [00:05<00:08,  149.62it/s]
 24%|██▍       | cHPI amplitudes : 404/1696 [00:05<00:08,  158.09it/s]
 25%|██▍       | cHPI amplitudes : 418/1696 [00:05<00:07,  166.81it/s]
 25%|██▌       | cHPI amplitudes : 432/1696 [00:05<00:07,  175.75it/s]
 26%|██▋       | cHPI amplitudes : 446/1696 [00:05<00:06,  184.94it/s]
 27%|██▋       | cHPI amplitudes : 460/1696 [00:05<00:06,  194.35it/s]
 28%|██▊       | cHPI amplitudes : 474/1696 [00:05<00:05,  203.84it/s]
 29%|██▉       | cHPI amplitudes : 488/1696 [00:05<00:05,  213.62it/s]
 30%|██▉       | cHPI amplitudes : 502/1696 [00:05<00:05,  223.67it/s]
 30%|███       | cHPI amplitudes : 516/1696 [00:05<00:05,  233.87it/s]
 31%|███▏      | cHPI amplitudes : 530/1696 [00:05<00:04,  244.09it/s]
 32%|███▏      | cHPI amplitudes : 545/1696 [00:05<00:04,  255.50it/s]
 33%|███▎      | cHPI amplitudes : 560/1696 [00:05<00:04,  267.10it/s]
 34%|███▍      | cHPI amplitudes : 574/1696 [00:05<00:04,  277.77it/s]
 35%|███▍      | cHPI amplitudes : 589/1696 [00:05<00:03,  289.70it/s]
 36%|███▌      | cHPI amplitudes : 604/1696 [00:05<00:03,  301.85it/s]
 36%|███▋      | cHPI amplitudes : 619/1696 [00:05<00:03,  314.11it/s]
 37%|███▋      | cHPI amplitudes : 634/1696 [00:05<00:03,  326.41it/s]
 38%|███▊      | cHPI amplitudes : 649/1696 [00:05<00:03,  338.74it/s]
 39%|███▉      | cHPI amplitudes : 664/1696 [00:05<00:02,  351.28it/s]
 40%|████      | cHPI amplitudes : 679/1696 [00:05<00:02,  363.81it/s]
 41%|████      | cHPI amplitudes : 693/1696 [00:05<00:02,  375.20it/s]
 42%|████▏     | cHPI amplitudes : 708/1696 [00:05<00:02,  387.91it/s]
 43%|████▎     | cHPI amplitudes : 723/1696 [00:05<00:02,  400.45it/s]
 44%|████▎     | cHPI amplitudes : 738/1696 [00:05<00:02,  412.95it/s]
 44%|████▍     | cHPI amplitudes : 752/1696 [00:05<00:02,  424.07it/s]
 45%|████▌     | cHPI amplitudes : 767/1696 [00:05<00:02,  436.58it/s]
 46%|████▌     | cHPI amplitudes : 781/1696 [00:05<00:02,  447.65it/s]
 47%|████▋     | cHPI amplitudes : 795/1696 [00:05<00:01,  458.89it/s]
 48%|████▊     | cHPI amplitudes : 809/1696 [00:05<00:01,  470.31it/s]
 49%|████▊     | cHPI amplitudes : 824/1696 [00:05<00:01,  482.74it/s]
 49%|████▉     | cHPI amplitudes : 839/1696 [00:05<00:01,  495.04it/s]
 50%|█████     | cHPI amplitudes : 853/1696 [00:05<00:01,  505.89it/s]
 51%|█████     | cHPI amplitudes : 867/1696 [00:05<00:01,  516.50it/s]
 52%|█████▏    | cHPI amplitudes : 881/1696 [00:05<00:01,  526.40it/s]
 53%|█████▎    | cHPI amplitudes : 896/1696 [00:05<00:01,  538.29it/s]
 54%|█████▎    | cHPI amplitudes : 911/1696 [00:05<00:01,  550.22it/s]
 55%|█████▍    | cHPI amplitudes : 926/1696 [00:05<00:01,  561.78it/s]
 55%|█████▌    | cHPI amplitudes : 940/1696 [00:06<00:01,  571.86it/s]
 56%|█████▋    | cHPI amplitudes : 954/1696 [00:06<00:01,  581.93it/s]
 57%|█████▋    | cHPI amplitudes : 968/1696 [00:06<00:01,  590.71it/s]
 58%|█████▊    | cHPI amplitudes : 982/1696 [00:06<00:01,  600.13it/s]
 59%|█████▊    | cHPI amplitudes : 996/1696 [00:06<00:01,  609.52it/s]
 60%|█████▉    | cHPI amplitudes : 1011/1696 [00:06<00:01,  619.85it/s]
 60%|██████    | cHPI amplitudes : 1026/1696 [00:06<00:01,  629.92it/s]
 61%|██████▏   | cHPI amplitudes : 1039/1696 [00:06<00:01,  635.08it/s]
 62%|██████▏   | cHPI amplitudes : 1054/1696 [00:06<00:00,  644.75it/s]
 63%|██████▎   | cHPI amplitudes : 1068/1696 [00:06<00:00,  653.11it/s]
 64%|██████▍   | cHPI amplitudes : 1083/1696 [00:06<00:00,  662.14it/s]
 65%|██████▍   | cHPI amplitudes : 1098/1696 [00:06<00:00,  671.03it/s]
 66%|██████▌   | cHPI amplitudes : 1113/1696 [00:06<00:00,  679.67it/s]
 67%|██████▋   | cHPI amplitudes : 1128/1696 [00:06<00:00,  688.00it/s]
 67%|██████▋   | cHPI amplitudes : 1143/1696 [00:06<00:00,  696.36it/s]
 68%|██████▊   | cHPI amplitudes : 1158/1696 [00:06<00:00,  704.75it/s]
 69%|██████▉   | cHPI amplitudes : 1172/1696 [00:06<00:00,  711.44it/s]
 70%|██████▉   | cHPI amplitudes : 1186/1696 [00:06<00:00,  717.13it/s]
 71%|███████   | cHPI amplitudes : 1200/1696 [00:06<00:00,  723.41it/s]
 72%|███████▏  | cHPI amplitudes : 1215/1696 [00:06<00:00,  730.92it/s]
 73%|███████▎  | cHPI amplitudes : 1230/1696 [00:06<00:00,  737.94it/s]
 73%|███████▎  | cHPI amplitudes : 1244/1696 [00:06<00:00,  742.70it/s]
 74%|███████▍  | cHPI amplitudes : 1259/1696 [00:06<00:00,  749.24it/s]
 75%|███████▌  | cHPI amplitudes : 1274/1696 [00:06<00:00,  755.60it/s]
 76%|███████▌  | cHPI amplitudes : 1288/1696 [00:06<00:00,  759.69it/s]
 77%|███████▋  | cHPI amplitudes : 1303/1696 [00:06<00:00,  765.21it/s]
 78%|███████▊  | cHPI amplitudes : 1317/1696 [00:06<00:00,  769.60it/s]
 78%|███████▊  | cHPI amplitudes : 1331/1696 [00:06<00:00,  772.68it/s]
 79%|███████▉  | cHPI amplitudes : 1346/1696 [00:06<00:00,  777.71it/s]
 80%|████████  | cHPI amplitudes : 1361/1696 [00:06<00:00,  782.44it/s]
 81%|████████  | cHPI amplitudes : 1375/1696 [00:06<00:00,  785.71it/s]
 82%|████████▏ | cHPI amplitudes : 1390/1696 [00:06<00:00,  790.12it/s]
 83%|████████▎ | cHPI amplitudes : 1405/1696 [00:06<00:00,  795.20it/s]
 84%|████████▎ | cHPI amplitudes : 1420/1696 [00:06<00:00,  799.59it/s]
 85%|████████▍ | cHPI amplitudes : 1435/1696 [00:06<00:00,  803.46it/s]
 85%|████████▌ | cHPI amplitudes : 1450/1696 [00:06<00:00,  807.50it/s]
 86%|████████▋ | cHPI amplitudes : 1465/1696 [00:06<00:00,  811.88it/s]
 87%|████████▋ | cHPI amplitudes : 1480/1696 [00:06<00:00,  815.24it/s]
 88%|████████▊ | cHPI amplitudes : 1494/1696 [00:06<00:00,  816.21it/s]
 89%|████████▉ | cHPI amplitudes : 1509/1696 [00:06<00:00,  819.85it/s]
 90%|████████▉ | cHPI amplitudes : 1523/1696 [00:06<00:00,  822.00it/s]
 91%|█████████ | cHPI amplitudes : 1538/1696 [00:06<00:00,  825.46it/s]
 92%|█████████▏| cHPI amplitudes : 1552/1696 [00:06<00:00,  827.44it/s]
 92%|█████████▏| cHPI amplitudes : 1566/1696 [00:06<00:00,  829.32it/s]
 93%|█████████▎| cHPI amplitudes : 1581/1696 [00:06<00:00,  831.85it/s]
 94%|█████████▍| cHPI amplitudes : 1595/1696 [00:06<00:00,  831.60it/s]
 95%|█████████▍| cHPI amplitudes : 1610/1696 [00:06<00:00,  834.05it/s]
 96%|█████████▌| cHPI amplitudes : 1625/1696 [00:06<00:00,  836.52it/s]
 97%|█████████▋| cHPI amplitudes : 1639/1696 [00:06<00:00,  835.85it/s]
 97%|█████████▋| cHPI amplitudes : 1653/1696 [00:06<00:00,  837.58it/s]
 98%|█████████▊| cHPI amplitudes : 1667/1696 [00:06<00:00,  838.50it/s]
 99%|█████████▉| cHPI amplitudes : 1681/1696 [00:06<00:00,  837.51it/s]
100%|█████████▉| cHPI amplitudes : 1694/1696 [00:06<00:00,  834.42it/s]
100%|██████████| cHPI amplitudes : 1696/1696 [00:06<00:00,  246.60it/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:00<27:40,    1.02it/s]
  3%|▎         | cHPI locations  : 56/1696 [00:00<00:27,   59.09it/s]
  6%|▌         | cHPI locations  : 101/1696 [00:01<00:15,  101.22it/s]
  9%|▉         | cHPI locations  : 156/1696 [00:01<00:09,  158.17it/s]
 12%|█▏        | cHPI locations  : 201/1696 [00:01<00:07,  194.36it/s]
 15%|█▌        | cHPI locations  : 256/1696 [00:01<00:05,  250.48it/s]
 18%|█▊        | cHPI locations  : 301/1696 [00:01<00:04,  281.92it/s]
 21%|██        | cHPI locations  : 356/1696 [00:01<00:03,  337.41it/s]
 24%|██▎       | cHPI locations  : 401/1696 [00:01<00:03,  362.86it/s]
 27%|██▋       | cHPI locations  : 456/1696 [00:01<00:02,  417.66it/s]
 30%|██▉       | cHPI locations  : 501/1696 [00:01<00:02,  436.31it/s]
 33%|███▎      | cHPI locations  : 555/1696 [00:01<00:02,  489.09it/s]
 33%|███▎      | cHPI locations  : 559/1696 [00:01<00:02,  438.90it/s]
 33%|███▎      | cHPI locations  : 568/1696 [00:01<00:02,  394.97it/s]
 34%|███▍      | cHPI locations  : 573/1696 [00:01<00:03,  357.38it/s]
 34%|███▍      | cHPI locations  : 577/1696 [00:01<00:03,  326.84it/s]
 34%|███▍      | cHPI locations  : 581/1696 [00:01<00:03,  297.31it/s]
 35%|███▍      | cHPI locations  : 587/1696 [00:02<00:04,  274.69it/s]
 35%|███▌      | cHPI locations  : 600/1696 [00:02<00:04,  259.50it/s]
 37%|███▋      | cHPI locations  : 631/1696 [00:02<00:03,  280.57it/s]
 39%|███▉      | cHPI locations  : 664/1696 [00:02<00:03,  303.81it/s]
 41%|████      | cHPI locations  : 697/1696 [00:02<00:03,  327.62it/s]
 41%|████▏     | cHPI locations  : 700/1696 [00:02<00:03,  299.80it/s]
 43%|████▎     | cHPI locations  : 736/1696 [00:02<00:02,  325.90it/s]
 46%|████▌     | cHPI locations  : 772/1696 [00:02<00:02,  352.67it/s]
 47%|████▋     | cHPI locations  : 789/1696 [00:02<00:02,  334.57it/s]
 47%|████▋     | cHPI locations  : 794/1696 [00:02<00:02,  311.58it/s]
 47%|████▋     | cHPI locations  : 800/1696 [00:02<00:03,  290.03it/s]
 47%|████▋     | cHPI locations  : 805/1696 [00:02<00:03,  269.06it/s]
 48%|████▊     | cHPI locations  : 810/1696 [00:02<00:03,  250.95it/s]
 48%|████▊     | cHPI locations  : 815/1696 [00:03<00:03,  237.08it/s]
 48%|████▊     | cHPI locations  : 819/1696 [00:03<00:03,  221.31it/s]
 49%|████▊     | cHPI locations  : 823/1696 [00:03<00:04,  206.86it/s]
 49%|████▉     | cHPI locations  : 827/1696 [00:03<00:04,  194.51it/s]
 49%|████▉     | cHPI locations  : 833/1696 [00:03<00:04,  183.56it/s]
 50%|█████     | cHPI locations  : 848/1696 [00:03<00:04,  179.81it/s]
 52%|█████▏    | cHPI locations  : 879/1696 [00:03<00:04,  197.95it/s]
 54%|█████▎    | cHPI locations  : 909/1696 [00:03<00:03,  216.02it/s]
 55%|█████▌    | cHPI locations  : 940/1696 [00:03<00:03,  235.26it/s]
 56%|█████▌    | cHPI locations  : 948/1696 [00:03<00:03,  222.82it/s]
 58%|█████▊    | cHPI locations  : 979/1696 [00:03<00:02,  242.22it/s]
 60%|█████▉    | cHPI locations  : 1010/1696 [00:03<00:02,  262.15it/s]
 61%|██████▏   | cHPI locations  : 1040/1696 [00:03<00:02,  260.96it/s]
 62%|██████▏   | cHPI locations  : 1051/1696 [00:04<00:02,  249.27it/s]
 63%|██████▎   | cHPI locations  : 1062/1696 [00:04<00:02,  238.36it/s]
 63%|██████▎   | cHPI locations  : 1072/1696 [00:04<00:02,  227.76it/s]
 64%|██████▍   | cHPI locations  : 1086/1696 [00:04<00:02,  220.02it/s]
 66%|██████▌   | cHPI locations  : 1116/1696 [00:04<00:02,  237.68it/s]
 68%|██████▊   | cHPI locations  : 1147/1696 [00:04<00:02,  256.55it/s]
 69%|██████▉   | cHPI locations  : 1177/1696 [00:04<00:01,  275.29it/s]
 70%|██████▉   | cHPI locations  : 1186/1696 [00:04<00:01,  260.76it/s]
 72%|███████▏  | cHPI locations  : 1221/1696 [00:04<00:01,  283.34it/s]
 74%|███████▍  | cHPI locations  : 1257/1696 [00:04<00:01,  307.24it/s]
 76%|███████▌  | cHPI locations  : 1286/1696 [00:04<00:01,  303.80it/s]
 76%|███████▋  | cHPI locations  : 1296/1696 [00:04<00:01,  286.56it/s]
 77%|███████▋  | cHPI locations  : 1301/1696 [00:04<00:01,  268.85it/s]
 77%|███████▋  | cHPI locations  : 1306/1696 [00:05<00:01,  252.21it/s]
 77%|███████▋  | cHPI locations  : 1311/1696 [00:05<00:01,  235.41it/s]
 78%|███████▊  | cHPI locations  : 1315/1696 [00:05<00:01,  220.62it/s]
 78%|███████▊  | cHPI locations  : 1319/1696 [00:05<00:01,  208.55it/s]
 78%|███████▊  | cHPI locations  : 1325/1696 [00:05<00:01,  197.58it/s]
 78%|███████▊  | cHPI locations  : 1331/1696 [00:05<00:01,  188.79it/s]
 79%|███████▉  | cHPI locations  : 1341/1696 [00:05<00:01,  180.62it/s]
 81%|████████  | cHPI locations  : 1369/1696 [00:05<00:01,  196.10it/s]
 82%|████████▏ | cHPI locations  : 1399/1696 [00:05<00:01,  213.35it/s]
 85%|████████▍ | cHPI locations  : 1434/1696 [00:05<00:01,  234.42it/s]
 85%|████████▍ | cHPI locations  : 1441/1696 [00:05<00:01,  223.40it/s]
 87%|████████▋ | cHPI locations  : 1475/1696 [00:05<00:00,  244.20it/s]
 89%|████████▉ | cHPI locations  : 1510/1696 [00:05<00:00,  266.26it/s]
 91%|█████████ | cHPI locations  : 1541/1696 [00:05<00:00,  265.71it/s]
 92%|█████████▏| cHPI locations  : 1565/1696 [00:06<00:00,  258.67it/s]
 93%|█████████▎| cHPI locations  : 1573/1696 [00:06<00:00,  245.01it/s]
 93%|█████████▎| cHPI locations  : 1580/1696 [00:06<00:00,  231.56it/s]
 94%|█████████▎| cHPI locations  : 1586/1696 [00:06<00:00,  216.66it/s]
 94%|█████████▍| cHPI locations  : 1590/1696 [00:06<00:00,  203.54it/s]
 94%|█████████▍| cHPI locations  : 1594/1696 [00:06<00:00,  191.51it/s]
 94%|█████████▍| cHPI locations  : 1599/1696 [00:06<00:00,  181.02it/s]
 95%|█████████▍| cHPI locations  : 1609/1696 [00:06<00:00,  175.17it/s]
 97%|█████████▋| cHPI locations  : 1649/1696 [00:06<00:00,  196.97it/s]
 97%|█████████▋| cHPI locations  : 1652/1696 [00:07<00:00,  186.46it/s]
100%|█████████▉| cHPI locations  : 1692/1696 [00:07<00:00,  208.81it/s]
100%|██████████| cHPI locations  : 1696/1696 [00:07<00:00,  238.77it/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.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)
Position, Rotation

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:06<50:34,    6.13s/it]
  1%|          | cHPI SNRs : 5/496 [00:06<09:39,    1.18s/it]
  2%|▏         | cHPI SNRs : 9/496 [00:06<05:11,    1.56it/s]
  3%|▎         | cHPI SNRs : 13/496 [00:06<03:29,    2.31it/s]
  3%|▎         | cHPI SNRs : 17/496 [00:06<02:34,    3.09it/s]
  4%|▍         | cHPI SNRs : 21/496 [00:06<02:01,    3.91it/s]
  5%|▍         | cHPI SNRs : 24/496 [00:06<01:43,    4.54it/s]
  5%|▌         | cHPI SNRs : 27/496 [00:06<01:30,    5.20it/s]
  6%|▌         | cHPI SNRs : 29/496 [00:06<01:22,    5.65it/s]
  6%|▋         | cHPI SNRs : 31/496 [00:06<01:15,    6.13it/s]
  7%|▋         | cHPI SNRs : 34/496 [00:06<01:07,    6.88it/s]
  8%|▊         | cHPI SNRs : 38/496 [00:06<00:57,    7.94it/s]
  8%|▊         | cHPI SNRs : 42/496 [00:06<00:50,    9.04it/s]
  9%|▉         | cHPI SNRs : 46/496 [00:06<00:44,   10.19it/s]
 10%|█         | cHPI SNRs : 50/496 [00:06<00:39,   11.37it/s]
 11%|█         | cHPI SNRs : 53/496 [00:06<00:36,   12.28it/s]
 11%|█▏        | cHPI SNRs : 56/496 [00:06<00:33,   13.21it/s]
 12%|█▏        | cHPI SNRs : 58/496 [00:06<00:31,   13.84it/s]
 12%|█▎        | cHPI SNRs : 62/496 [00:06<00:28,   15.24it/s]
 13%|█▎        | cHPI SNRs : 66/496 [00:06<00:25,   16.67it/s]
 14%|█▍        | cHPI SNRs : 69/496 [00:06<00:24,   17.79it/s]
 15%|█▍        | cHPI SNRs : 72/496 [00:06<00:22,   18.93it/s]
 15%|█▌        | cHPI SNRs : 75/496 [00:06<00:20,   20.14it/s]
 16%|█▌        | cHPI SNRs : 79/496 [00:06<00:19,   21.78it/s]
 17%|█▋        | cHPI SNRs : 82/496 [00:06<00:17,   23.04it/s]
 17%|█▋        | cHPI SNRs : 86/496 [00:06<00:16,   24.84it/s]
 18%|█▊        | cHPI SNRs : 90/496 [00:06<00:15,   26.68it/s]
 19%|█▉        | cHPI SNRs : 94/496 [00:06<00:14,   28.60it/s]
 20%|█▉        | cHPI SNRs : 98/496 [00:06<00:13,   30.55it/s]
 20%|██        | cHPI SNRs : 101/496 [00:06<00:12,   31.95it/s]
 21%|██        | cHPI SNRs : 104/496 [00:06<00:11,   33.34it/s]
 21%|██▏       | cHPI SNRs : 106/496 [00:06<00:11,   34.23it/s]
 22%|██▏       | cHPI SNRs : 108/496 [00:06<00:11,   35.19it/s]
 23%|██▎       | cHPI SNRs : 112/496 [00:06<00:10,   37.50it/s]
 23%|██▎       | cHPI SNRs : 115/496 [00:06<00:09,   39.23it/s]
 24%|██▍       | cHPI SNRs : 119/496 [00:06<00:09,   41.63it/s]
 25%|██▍       | cHPI SNRs : 123/496 [00:06<00:08,   44.13it/s]
 25%|██▌       | cHPI SNRs : 126/496 [00:06<00:08,   45.95it/s]
 26%|██▌       | cHPI SNRs : 129/496 [00:06<00:07,   47.62it/s]
 27%|██▋       | cHPI SNRs : 132/496 [00:06<00:07,   49.16it/s]
 27%|██▋       | cHPI SNRs : 135/496 [00:06<00:07,   51.02it/s]
 28%|██▊       | cHPI SNRs : 139/496 [00:06<00:06,   53.77it/s]
 29%|██▊       | cHPI SNRs : 142/496 [00:06<00:06,   55.71it/s]
 29%|██▉       | cHPI SNRs : 145/496 [00:06<00:06,   57.69it/s]
 30%|██▉       | cHPI SNRs : 148/496 [00:06<00:05,   59.66it/s]
 30%|███       | cHPI SNRs : 151/496 [00:07<00:05,   61.69it/s]
 31%|███       | cHPI SNRs : 154/496 [00:07<00:05,   63.72it/s]
 32%|███▏      | cHPI SNRs : 157/496 [00:07<00:05,   65.85it/s]
 32%|███▏      | cHPI SNRs : 160/496 [00:07<00:04,   68.00it/s]
 33%|███▎      | cHPI SNRs : 163/496 [00:07<00:04,   70.26it/s]
 33%|███▎      | cHPI SNRs : 166/496 [00:07<00:04,   72.07it/s]
 34%|███▍      | cHPI SNRs : 170/496 [00:07<00:04,   75.35it/s]
 35%|███▌      | cHPI SNRs : 174/496 [00:07<00:04,   78.65it/s]
 36%|███▌      | cHPI SNRs : 178/496 [00:07<00:03,   81.90it/s]
 37%|███▋      | cHPI SNRs : 182/496 [00:07<00:03,   85.25it/s]
 37%|███▋      | cHPI SNRs : 185/496 [00:07<00:03,   87.21it/s]
 38%|███▊      | cHPI SNRs : 188/496 [00:07<00:03,   88.95it/s]
 38%|███▊      | cHPI SNRs : 190/496 [00:07<00:03,   89.59it/s]
 39%|███▊      | cHPI SNRs : 192/496 [00:07<00:03,   90.20it/s]
 39%|███▉      | cHPI SNRs : 195/496 [00:07<00:03,   92.13it/s]
 40%|████      | cHPI SNRs : 199/496 [00:07<00:03,   95.66it/s]
 41%|████      | cHPI SNRs : 203/496 [00:07<00:02,   99.06it/s]
 42%|████▏     | cHPI SNRs : 207/496 [00:07<00:02,  102.47it/s]
 43%|████▎     | cHPI SNRs : 211/496 [00:07<00:02,  106.04it/s]
 43%|████▎     | cHPI SNRs : 214/496 [00:07<00:02,  107.41it/s]
 44%|████▍     | cHPI SNRs : 217/496 [00:07<00:02,  108.60it/s]
 44%|████▍     | cHPI SNRs : 219/496 [00:07<00:02,  108.49it/s]
 45%|████▍     | cHPI SNRs : 223/496 [00:07<00:02,  112.00it/s]
 46%|████▌     | cHPI SNRs : 226/496 [00:07<00:02,  114.07it/s]
 46%|████▌     | cHPI SNRs : 229/496 [00:07<00:02,  116.18it/s]
 47%|████▋     | cHPI SNRs : 232/496 [00:07<00:02,  118.23it/s]
 47%|████▋     | cHPI SNRs : 235/496 [00:07<00:02,  120.35it/s]
 48%|████▊     | cHPI SNRs : 239/496 [00:07<00:02,  123.66it/s]
 49%|████▉     | cHPI SNRs : 242/496 [00:07<00:02,  124.24it/s]
 50%|████▉     | cHPI SNRs : 246/496 [00:07<00:01,  127.46it/s]
 50%|█████     | cHPI SNRs : 250/496 [00:07<00:01,  129.92it/s]
 51%|█████     | cHPI SNRs : 253/496 [00:07<00:01,  131.48it/s]
 52%|█████▏    | cHPI SNRs : 256/496 [00:07<00:01,  133.10it/s]
 52%|█████▏    | cHPI SNRs : 259/496 [00:07<00:01,  134.89it/s]
 53%|█████▎    | cHPI SNRs : 262/496 [00:07<00:01,  136.11it/s]
 53%|█████▎    | cHPI SNRs : 265/496 [00:07<00:01,  136.97it/s]
 54%|█████▍    | cHPI SNRs : 268/496 [00:07<00:01,  138.41it/s]
 55%|█████▍    | cHPI SNRs : 271/496 [00:07<00:01,  139.99it/s]
 55%|█████▌    | cHPI SNRs : 274/496 [00:07<00:01,  141.49it/s]
 56%|█████▌    | cHPI SNRs : 277/496 [00:07<00:01,  142.85it/s]
 56%|█████▋    | cHPI SNRs : 280/496 [00:07<00:01,  144.23it/s]
 57%|█████▋    | cHPI SNRs : 283/496 [00:07<00:01,  145.59it/s]
 58%|█████▊    | cHPI SNRs : 286/496 [00:07<00:01,  146.67it/s]
 58%|█████▊    | cHPI SNRs : 289/496 [00:07<00:01,  147.54it/s]
 59%|█████▉    | cHPI SNRs : 292/496 [00:07<00:01,  148.64it/s]
 60%|█████▉    | cHPI SNRs : 296/496 [00:07<00:01,  151.69it/s]
 61%|██████    | cHPI SNRs : 302/496 [00:07<00:01,  159.48it/s]
 62%|██████▏   | cHPI SNRs : 306/496 [00:07<00:01,  161.46it/s]
 62%|██████▏   | cHPI SNRs : 309/496 [00:07<00:01,  162.15it/s]
 63%|██████▎   | cHPI SNRs : 312/496 [00:07<00:01,  161.36it/s]
 64%|██████▎   | cHPI SNRs : 315/496 [00:07<00:01,  161.28it/s]
 64%|██████▍   | cHPI SNRs : 318/496 [00:07<00:01,  161.58it/s]
 65%|██████▍   | cHPI SNRs : 321/496 [00:07<00:01,  161.77it/s]
 65%|██████▌   | cHPI SNRs : 324/496 [00:07<00:01,  162.09it/s]
 66%|██████▌   | cHPI SNRs : 327/496 [00:07<00:01,  162.09it/s]
 67%|██████▋   | cHPI SNRs : 330/496 [00:08<00:01,  162.12it/s]
 67%|██████▋   | cHPI SNRs : 333/496 [00:08<00:01,  162.62it/s]
 68%|██████▊   | cHPI SNRs : 336/496 [00:08<00:00,  163.49it/s]
 68%|██████▊   | cHPI SNRs : 339/496 [00:08<00:00,  164.04it/s]
 69%|██████▉   | cHPI SNRs : 342/496 [00:08<00:00,  163.95it/s]
 70%|██████▉   | cHPI SNRs : 345/496 [00:08<00:00,  164.05it/s]
 70%|███████   | cHPI SNRs : 348/496 [00:08<00:00,  164.16it/s]
 71%|███████   | cHPI SNRs : 351/496 [00:08<00:00,  164.34it/s]
 71%|███████▏  | cHPI SNRs : 354/496 [00:08<00:00,  165.16it/s]
 72%|███████▏  | cHPI SNRs : 357/496 [00:08<00:00,  165.80it/s]
 73%|███████▎  | cHPI SNRs : 361/496 [00:08<00:00,  166.90it/s]
 73%|███████▎  | cHPI SNRs : 364/496 [00:08<00:00,  167.25it/s]
 74%|███████▍  | cHPI SNRs : 368/496 [00:08<00:00,  168.50it/s]
 75%|███████▍  | cHPI SNRs : 371/496 [00:08<00:00,  169.30it/s]
 76%|███████▌  | cHPI SNRs : 375/496 [00:08<00:00,  170.31it/s]
 76%|███████▌  | cHPI SNRs : 378/496 [00:08<00:00,  170.24it/s]
 77%|███████▋  | cHPI SNRs : 381/496 [00:08<00:00,  170.97it/s]
 78%|███████▊  | cHPI SNRs : 385/496 [00:08<00:00,  171.92it/s]
 78%|███████▊  | cHPI SNRs : 389/496 [00:08<00:00,  172.84it/s]
 79%|███████▉  | cHPI SNRs : 392/496 [00:08<00:00,  172.36it/s]
 80%|███████▉  | cHPI SNRs : 396/496 [00:08<00:00,  173.34it/s]
 80%|████████  | cHPI SNRs : 399/496 [00:08<00:00,  173.56it/s]
 81%|████████  | cHPI SNRs : 402/496 [00:08<00:00,  173.83it/s]
 81%|████████▏ | cHPI SNRs : 404/496 [00:08<00:00,  167.21it/s]
 82%|████████▏ | cHPI SNRs : 405/496 [00:08<00:00,  156.13it/s]
 82%|████████▏ | cHPI SNRs : 407/496 [00:08<00:00,  152.36it/s]
 82%|████████▏ | cHPI SNRs : 409/496 [00:08<00:00,  149.10it/s]
 83%|████████▎ | cHPI SNRs : 411/496 [00:08<00:00,  147.64it/s]
 83%|████████▎ | cHPI SNRs : 414/496 [00:08<00:00,  149.16it/s]
 84%|████████▍ | cHPI SNRs : 416/496 [00:08<00:00,  147.50it/s]
 84%|████████▍ | cHPI SNRs : 419/496 [00:08<00:00,  147.96it/s]
 85%|████████▌ | cHPI SNRs : 422/496 [00:08<00:00,  148.70it/s]
 86%|████████▌ | cHPI SNRs : 425/496 [00:08<00:00,  149.73it/s]
 86%|████████▋ | cHPI SNRs : 428/496 [00:08<00:00,  150.50it/s]
 87%|████████▋ | cHPI SNRs : 431/496 [00:08<00:00,  151.30it/s]
 88%|████████▊ | cHPI SNRs : 434/496 [00:08<00:00,  151.97it/s]
 88%|████████▊ | cHPI SNRs : 437/496 [00:08<00:00,  152.62it/s]
 89%|████████▊ | cHPI SNRs : 440/496 [00:08<00:00,  153.09it/s]
 89%|████████▉ | cHPI SNRs : 443/496 [00:08<00:00,  153.65it/s]
 90%|████████▉ | cHPI SNRs : 446/496 [00:08<00:00,  154.40it/s]
 91%|█████████ | cHPI SNRs : 449/496 [00:08<00:00,  154.42it/s]
 91%|█████████ | cHPI SNRs : 452/496 [00:08<00:00,  154.70it/s]
 92%|█████████▏| cHPI SNRs : 455/496 [00:08<00:00,  154.93it/s]
 92%|█████████▏| cHPI SNRs : 458/496 [00:08<00:00,  155.21it/s]
 93%|█████████▎| cHPI SNRs : 461/496 [00:08<00:00,  155.52it/s]
 94%|█████████▎| cHPI SNRs : 464/496 [00:08<00:00,  156.08it/s]
 94%|█████████▍| cHPI SNRs : 467/496 [00:08<00:00,  156.62it/s]
 95%|█████████▍| cHPI SNRs : 470/496 [00:08<00:00,  157.14it/s]
 95%|█████████▌| cHPI SNRs : 473/496 [00:08<00:00,  157.28it/s]
 96%|█████████▌| cHPI SNRs : 476/496 [00:08<00:00,  157.42it/s]
 97%|█████████▋| cHPI SNRs : 479/496 [00:08<00:00,  157.78it/s]
 97%|█████████▋| cHPI SNRs : 482/496 [00:08<00:00,  158.24it/s]
 98%|█████████▊| cHPI SNRs : 485/496 [00:08<00:00,  158.56it/s]
 98%|█████████▊| cHPI SNRs : 488/496 [00:09<00:00,  158.55it/s]
 99%|█████████▉| cHPI SNRs : 491/496 [00:09<00:00,  158.59it/s]
100%|█████████▉| cHPI SNRs : 494/496 [00:09<00:00,  158.40it/s]
100%|██████████| cHPI SNRs : 496/496 [00:09<00:00,   54.79it/s]

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

Gallery generated by Sphinx-Gallery