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
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")
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)

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 120 180 240 300 360 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:29:41,    3.17s/it]
  1%|1         | cHPI amplitudes : 20/1696 [00:03<04:14,    6.58it/s]
  2%|2         | cHPI amplitudes : 40/1696 [00:03<02:03,   13.44it/s]
  3%|3         | cHPI amplitudes : 59/1696 [00:03<01:20,   20.23it/s]
  5%|4         | cHPI amplitudes : 78/1696 [00:03<00:59,   27.28it/s]
  6%|5         | cHPI amplitudes : 98/1696 [00:03<00:45,   35.01it/s]
  7%|6         | cHPI amplitudes : 118/1696 [00:03<00:36,   43.04it/s]
  8%|8         | cHPI amplitudes : 138/1696 [00:03<00:30,   51.38it/s]
  9%|9         | cHPI amplitudes : 157/1696 [00:03<00:25,   59.56it/s]
 10%|#         | cHPI amplitudes : 176/1696 [00:03<00:22,   68.06it/s]
 12%|#1        | cHPI amplitudes : 196/1696 [00:03<00:19,   77.34it/s]
 13%|#2        | cHPI amplitudes : 216/1696 [00:03<00:17,   86.97it/s]
 14%|#3        | cHPI amplitudes : 237/1696 [00:03<00:14,   97.44it/s]
 15%|#5        | cHPI amplitudes : 257/1696 [00:03<00:13,  107.74it/s]
 16%|#6        | cHPI amplitudes : 277/1696 [00:03<00:11,  118.35it/s]
 18%|#7        | cHPI amplitudes : 297/1696 [00:03<00:10,  129.32it/s]
 19%|#8        | cHPI amplitudes : 318/1696 [00:03<00:09,  141.22it/s]
 20%|#9        | cHPI amplitudes : 339/1696 [00:03<00:08,  153.50it/s]
 21%|##1       | cHPI amplitudes : 360/1696 [00:03<00:08,  166.13it/s]
 22%|##2       | cHPI amplitudes : 380/1696 [00:03<00:07,  178.49it/s]
 24%|##3       | cHPI amplitudes : 400/1696 [00:03<00:06,  191.14it/s]
 25%|##4       | cHPI amplitudes : 419/1696 [00:03<00:06,  203.34it/s]
 26%|##5       | cHPI amplitudes : 436/1696 [00:03<00:05,  214.23it/s]
 27%|##6       | cHPI amplitudes : 454/1696 [00:03<00:05,  226.27it/s]
 28%|##7       | cHPI amplitudes : 472/1696 [00:03<00:05,  238.53it/s]
 29%|##8       | cHPI amplitudes : 488/1696 [00:03<00:04,  249.36it/s]
 30%|##9       | cHPI amplitudes : 504/1696 [00:03<00:04,  260.35it/s]
 31%|###       | cHPI amplitudes : 520/1696 [00:03<00:04,  271.65it/s]
 32%|###1      | cHPI amplitudes : 536/1696 [00:03<00:04,  283.19it/s]
 33%|###2      | cHPI amplitudes : 553/1696 [00:03<00:03,  295.77it/s]
 34%|###3      | cHPI amplitudes : 570/1696 [00:03<00:03,  308.68it/s]
 35%|###4      | cHPI amplitudes : 591/1696 [00:03<00:03,  326.10it/s]
 36%|###6      | cHPI amplitudes : 611/1696 [00:03<00:03,  342.73it/s]
 37%|###7      | cHPI amplitudes : 630/1696 [00:03<00:02,  358.40it/s]
 38%|###8      | cHPI amplitudes : 650/1696 [00:03<00:02,  375.55it/s]
 40%|###9      | cHPI amplitudes : 670/1696 [00:03<00:02,  392.77it/s]
 41%|####      | cHPI amplitudes : 690/1696 [00:03<00:02,  410.26it/s]
 42%|####1     | cHPI amplitudes : 711/1696 [00:03<00:02,  429.35it/s]
 43%|####3     | cHPI amplitudes : 731/1696 [00:03<00:02,  447.00it/s]
 44%|####4     | cHPI amplitudes : 752/1696 [00:03<00:02,  466.00it/s]
 46%|####5     | cHPI amplitudes : 772/1696 [00:03<00:01,  483.90it/s]
 47%|####6     | cHPI amplitudes : 793/1696 [00:03<00:01,  503.05it/s]
 48%|####7     | cHPI amplitudes : 814/1696 [00:03<00:01,  522.51it/s]
 49%|####9     | cHPI amplitudes : 833/1696 [00:03<00:01,  538.85it/s]
 50%|#####     | cHPI amplitudes : 853/1696 [00:03<00:01,  556.69it/s]
 51%|#####1    | cHPI amplitudes : 873/1696 [00:03<00:01,  574.76it/s]
 53%|#####2    | cHPI amplitudes : 893/1696 [00:03<00:01,  592.15it/s]
 54%|#####3    | cHPI amplitudes : 913/1696 [00:03<00:01,  609.89it/s]
 55%|#####4    | cHPI amplitudes : 932/1696 [00:03<00:01,  625.26it/s]
 56%|#####6    | cHPI amplitudes : 953/1696 [00:03<00:01,  644.06it/s]
 57%|#####7    | cHPI amplitudes : 973/1696 [00:03<00:01,  661.70it/s]
 59%|#####8    | cHPI amplitudes : 994/1696 [00:04<00:01,  680.76it/s]
 60%|#####9    | cHPI amplitudes : 1015/1696 [00:04<00:00,  699.49it/s]
 61%|######1   | cHPI amplitudes : 1036/1696 [00:04<00:00,  717.89it/s]
 62%|######2   | cHPI amplitudes : 1057/1696 [00:04<00:00,  736.08it/s]
 64%|######3   | cHPI amplitudes : 1078/1696 [00:04<00:00,  754.30it/s]
 65%|######4   | cHPI amplitudes : 1099/1696 [00:04<00:00,  772.08it/s]
 66%|######6   | cHPI amplitudes : 1120/1696 [00:04<00:00,  789.37it/s]
 67%|######7   | cHPI amplitudes : 1141/1696 [00:04<00:00,  806.20it/s]
 69%|######8   | cHPI amplitudes : 1162/1696 [00:04<00:00,  823.40it/s]
 70%|######9   | cHPI amplitudes : 1183/1696 [00:04<00:00,  839.94it/s]
 71%|#######   | cHPI amplitudes : 1204/1696 [00:04<00:00,  855.82it/s]
 72%|#######2  | cHPI amplitudes : 1223/1696 [00:04<00:00,  866.34it/s]
 73%|#######2  | cHPI amplitudes : 1237/1696 [00:04<00:00,  866.12it/s]
 74%|#######3  | cHPI amplitudes : 1251/1696 [00:04<00:00,  865.08it/s]
 75%|#######4  | cHPI amplitudes : 1265/1696 [00:04<00:00,  863.73it/s]
 75%|#######5  | cHPI amplitudes : 1279/1696 [00:04<00:00,  862.85it/s]
 76%|#######6  | cHPI amplitudes : 1293/1696 [00:04<00:00,  861.90it/s]
 77%|#######7  | cHPI amplitudes : 1307/1696 [00:04<00:00,  860.54it/s]
 78%|#######7  | cHPI amplitudes : 1321/1696 [00:04<00:00,  859.88it/s]
 79%|#######8  | cHPI amplitudes : 1335/1696 [00:04<00:00,  858.82it/s]
 80%|#######9  | cHPI amplitudes : 1349/1696 [00:04<00:00,  858.66it/s]
 80%|########  | cHPI amplitudes : 1363/1696 [00:04<00:00,  857.36it/s]
 81%|########1 | cHPI amplitudes : 1377/1696 [00:04<00:00,  856.47it/s]
 82%|########2 | cHPI amplitudes : 1391/1696 [00:04<00:00,  855.61it/s]
 83%|########2 | cHPI amplitudes : 1406/1696 [00:04<00:00,  857.69it/s]
 84%|########4 | cHPI amplitudes : 1426/1696 [00:04<00:00,  873.74it/s]
 85%|########5 | cHPI amplitudes : 1446/1696 [00:04<00:00,  888.74it/s]
 86%|########6 | cHPI amplitudes : 1466/1696 [00:04<00:00,  903.71it/s]
 88%|########7 | cHPI amplitudes : 1486/1696 [00:04<00:00,  917.60it/s]
 89%|########8 | cHPI amplitudes : 1507/1696 [00:04<00:00,  932.81it/s]
 90%|########9 | cHPI amplitudes : 1524/1696 [00:04<00:00,  936.46it/s]
 91%|######### | cHPI amplitudes : 1539/1696 [00:04<00:00,  935.44it/s]
 92%|#########1| cHPI amplitudes : 1554/1696 [00:04<00:00,  933.70it/s]
 93%|#########2| cHPI amplitudes : 1569/1696 [00:04<00:00,  932.83it/s]
 93%|#########3| cHPI amplitudes : 1584/1696 [00:04<00:00,  931.16it/s]
 95%|#########4| cHPI amplitudes : 1604/1696 [00:04<00:00,  943.44it/s]
 96%|#########5| cHPI amplitudes : 1625/1696 [00:04<00:00,  957.77it/s]
 97%|#########7| cHPI amplitudes : 1646/1696 [00:04<00:00,  972.88it/s]
 98%|#########8| cHPI amplitudes : 1667/1696 [00:04<00:00,  987.41it/s]
100%|#########9| cHPI amplitudes : 1688/1696 [00:04<00:00, 1001.48it/s]
100%|##########| cHPI amplitudes : 1696/1696 [00:04<00:00,  363.41it/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<23:35,    1.20it/s]
  2%|1         | cHPI locations  : 30/1696 [00:00<00:45,   36.99it/s]
  4%|3         | cHPI locations  : 61/1696 [00:00<00:21,   75.68it/s]
  5%|5         | cHPI locations  : 91/1696 [00:00<00:14,  113.39it/s]
  6%|5         | cHPI locations  : 101/1696 [00:00<00:13,  115.53it/s]
  8%|7         | cHPI locations  : 133/1696 [00:00<00:10,  154.46it/s]
 10%|9         | cHPI locations  : 166/1696 [00:00<00:07,  195.03it/s]
 12%|#1        | cHPI locations  : 197/1696 [00:01<00:06,  233.18it/s]
 12%|#1        | cHPI locations  : 201/1696 [00:01<00:06,  219.03it/s]
 14%|#3        | cHPI locations  : 233/1696 [00:01<00:05,  257.72it/s]
 16%|#5        | cHPI locations  : 265/1696 [00:01<00:04,  296.70it/s]
 18%|#7        | cHPI locations  : 297/1696 [00:01<00:04,  335.89it/s]
 18%|#7        | cHPI locations  : 301/1696 [00:01<00:04,  312.41it/s]
 20%|#9        | cHPI locations  : 333/1696 [00:01<00:03,  350.84it/s]
 22%|##1       | cHPI locations  : 366/1696 [00:01<00:03,  390.67it/s]
 24%|##3       | cHPI locations  : 399/1696 [00:01<00:03,  430.70it/s]
 24%|##3       | cHPI locations  : 401/1696 [00:01<00:03,  397.85it/s]
 25%|##5       | cHPI locations  : 432/1696 [00:01<00:02,  434.50it/s]
 27%|##7       | cHPI locations  : 465/1696 [00:01<00:02,  474.00it/s]
 29%|##9       | cHPI locations  : 498/1696 [00:01<00:02,  513.59it/s]
 30%|##9       | cHPI locations  : 501/1696 [00:01<00:02,  470.24it/s]
 31%|###1      | cHPI locations  : 532/1696 [00:01<00:02,  506.30it/s]
 33%|###2      | cHPI locations  : 559/1696 [00:01<00:02,  477.84it/s]
 33%|###3      | cHPI locations  : 568/1696 [00:01<00:02,  436.06it/s]
 34%|###3      | cHPI locations  : 573/1696 [00:01<00:02,  396.58it/s]
 34%|###4      | cHPI locations  : 577/1696 [00:01<00:03,  364.98it/s]
 34%|###4      | cHPI locations  : 581/1696 [00:01<00:03,  335.20it/s]
 35%|###4      | cHPI locations  : 587/1696 [00:01<00:03,  311.59it/s]
 35%|###5      | cHPI locations  : 600/1696 [00:02<00:03,  297.20it/s]
 37%|###7      | cHPI locations  : 633/1696 [00:02<00:03,  327.32it/s]
 39%|###9      | cHPI locations  : 666/1696 [00:02<00:02,  358.05it/s]
 41%|####1     | cHPI locations  : 699/1696 [00:02<00:02,  389.19it/s]
 41%|####1     | cHPI locations  : 700/1696 [00:02<00:02,  357.12it/s]
 43%|####3     | cHPI locations  : 732/1696 [00:02<00:02,  386.94it/s]
 45%|####5     | cHPI locations  : 765/1696 [00:02<00:02,  418.29it/s]
 47%|####6     | cHPI locations  : 789/1696 [00:02<00:02,  403.52it/s]
 47%|####6     | cHPI locations  : 794/1696 [00:02<00:02,  372.06it/s]
 47%|####7     | cHPI locations  : 800/1696 [00:02<00:02,  347.46it/s]
 47%|####7     | cHPI locations  : 805/1696 [00:02<00:02,  324.10it/s]
 48%|####7     | cHPI locations  : 810/1696 [00:02<00:02,  303.49it/s]
 48%|####8     | cHPI locations  : 815/1696 [00:02<00:03,  284.36it/s]
 48%|####8     | cHPI locations  : 819/1696 [00:02<00:03,  266.90it/s]
 49%|####8     | cHPI locations  : 823/1696 [00:02<00:03,  250.24it/s]
 49%|####8     | cHPI locations  : 827/1696 [00:02<00:03,  236.20it/s]
 49%|####9     | cHPI locations  : 833/1696 [00:03<00:03,  223.86it/s]
 50%|#####     | cHPI locations  : 848/1696 [00:03<00:03,  219.12it/s]
 52%|#####1    | cHPI locations  : 879/1696 [00:03<00:03,  242.62it/s]
 54%|#####3    | cHPI locations  : 910/1696 [00:03<00:02,  266.60it/s]
 56%|#####5    | cHPI locations  : 943/1696 [00:03<00:02,  292.92it/s]
 56%|#####5    | cHPI locations  : 948/1696 [00:03<00:02,  275.26it/s]
 58%|#####7    | cHPI locations  : 978/1696 [00:03<00:02,  298.89it/s]
 59%|#####9    | cHPI locations  : 1009/1696 [00:03<00:02,  323.99it/s]
 61%|######    | cHPI locations  : 1034/1696 [00:03<00:01,  343.71it/s]
 61%|######1   | cHPI locations  : 1040/1696 [00:03<00:02,  319.26it/s]
 62%|######1   | cHPI locations  : 1051/1696 [00:03<00:02,  300.89it/s]
 63%|######2   | cHPI locations  : 1062/1696 [00:03<00:02,  285.36it/s]
 63%|######3   | cHPI locations  : 1072/1696 [00:03<00:02,  272.28it/s]
 64%|######4   | cHPI locations  : 1086/1696 [00:03<00:02,  263.37it/s]
 66%|######5   | cHPI locations  : 1117/1696 [00:03<00:02,  286.51it/s]
 68%|######7   | cHPI locations  : 1147/1696 [00:03<00:01,  309.24it/s]
 69%|######9   | cHPI locations  : 1177/1696 [00:03<00:01,  332.44it/s]
 70%|######9   | cHPI locations  : 1186/1696 [00:03<00:01,  313.92it/s]
 71%|#######1  | cHPI locations  : 1211/1696 [00:03<00:01,  332.58it/s]
 73%|#######2  | cHPI locations  : 1237/1696 [00:03<00:01,  352.56it/s]
 74%|#######4  | cHPI locations  : 1258/1696 [00:03<00:01,  367.72it/s]
 76%|#######5  | cHPI locations  : 1281/1696 [00:03<00:01,  385.39it/s]
 76%|#######5  | cHPI locations  : 1286/1696 [00:04<00:01,  361.06it/s]
 76%|#######6  | cHPI locations  : 1296/1696 [00:04<00:01,  340.73it/s]
 77%|#######6  | cHPI locations  : 1301/1696 [00:04<00:01,  318.92it/s]
 77%|#######7  | cHPI locations  : 1306/1696 [00:04<00:01,  299.18it/s]
 77%|#######7  | cHPI locations  : 1311/1696 [00:04<00:01,  281.67it/s]
 78%|#######7  | cHPI locations  : 1315/1696 [00:04<00:01,  265.53it/s]
 78%|#######7  | cHPI locations  : 1319/1696 [00:04<00:01,  250.66it/s]
 78%|#######8  | cHPI locations  : 1325/1696 [00:04<00:01,  237.68it/s]
 78%|#######8  | cHPI locations  : 1331/1696 [00:04<00:01,  225.97it/s]
 79%|#######9  | cHPI locations  : 1341/1696 [00:04<00:01,  210.77it/s]
 81%|########  | cHPI locations  : 1373/1696 [00:04<00:01,  233.84it/s]
 83%|########2 | cHPI locations  : 1406/1696 [00:04<00:01,  258.36it/s]
 85%|########4 | cHPI locations  : 1439/1696 [00:04<00:00,  283.46it/s]
 85%|########4 | cHPI locations  : 1441/1696 [00:04<00:00,  267.49it/s]
 87%|########6 | cHPI locations  : 1473/1696 [00:04<00:00,  292.14it/s]
 89%|########8 | cHPI locations  : 1506/1696 [00:04<00:00,  318.24it/s]
 91%|######### | cHPI locations  : 1539/1696 [00:04<00:00,  344.94it/s]
 91%|######### | cHPI locations  : 1541/1696 [00:05<00:00,  321.61it/s]
 92%|#########2| cHPI locations  : 1565/1696 [00:05<00:00,  314.22it/s]
 93%|#########2| cHPI locations  : 1573/1696 [00:05<00:00,  296.78it/s]
 93%|#########3| cHPI locations  : 1580/1696 [00:05<00:00,  278.24it/s]
 94%|#########3| cHPI locations  : 1586/1696 [00:05<00:00,  263.54it/s]
 94%|#########3| cHPI locations  : 1590/1696 [00:05<00:00,  245.94it/s]
 94%|#########3| cHPI locations  : 1594/1696 [00:05<00:00,  230.58it/s]
 94%|#########4| cHPI locations  : 1599/1696 [00:05<00:00,  218.02it/s]
 95%|#########4| cHPI locations  : 1609/1696 [00:05<00:00,  210.49it/s]
 97%|#########6| cHPI locations  : 1641/1696 [00:05<00:00,  232.54it/s]
 97%|#########7| cHPI locations  : 1652/1696 [00:05<00:00,  222.82it/s]
 99%|#########9| cHPI locations  : 1683/1696 [00:05<00:00,  244.19it/s]
100%|##########| cHPI locations  : 1696/1696 [00:05<00:00,  284.68it/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.0 /      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.1
t=14.000:    5/5 good HPI fits, movements [mm/s] =      0.1 /      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.5 /     34.6 /     39.8 /     38.2 /     30.3
t=14.720:    5/5 good HPI fits, movements [mm/s] =     49.0 /     47.4 /     60.4 /     59.2 /     44.7
t=14.760:    5/5 good HPI fits, movements [mm/s] =     65.2 /     62.4 /     72.8 /     70.9 /     56.1
t=14.800:    5/5 good HPI fits, movements [mm/s] =     66.9 /     59.5 /     83.1 /     78.1 /     60.9
t=14.860:    5/5 good HPI fits, movements [mm/s] =     54.3 /     52.2 /     57.6 /     62.0 /     47.2
t=14.990:    5/5 good HPI fits, movements [mm/s] =     23.5 /     22.4 /     24.0 /     27.1 /     20.1
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] =     51.0 /     55.2 /     58.1 /     67.2 /     59.4
t=16.990:    5/5 good HPI fits, movements [mm/s] =     57.8 /     54.0 /     62.0 /     65.1 /     57.5
t=17.040:    5/5 good HPI fits, movements [mm/s] =     53.1 /     49.9 /     60.0 /     61.9 /     49.6
t=17.090:    5/5 good HPI fits, movements [mm/s] =     58.0 /     50.1 /     66.6 /     64.8 /     51.2
t=17.140:    5/5 good HPI fits, movements [mm/s] =     60.3 /     54.2 /     72.6 /     67.4 /     57.6
t=17.180:    5/5 good HPI fits, movements [mm/s] =     63.7 /     60.0 /     74.5 /     72.0 /     62.2
t=17.220:    5/5 good HPI fits, movements [mm/s] =     60.9 /     59.5 /     75.3 /     71.4 /     63.1
t=17.260:    5/5 good HPI fits, movements [mm/s] =     68.1 /     65.3 /     73.4 /     68.9 /     69.2
t=17.320:    5/5 good HPI fits, movements [mm/s] =     49.6 /     50.9 /     54.8 /     55.0 /     52.4
t=17.470:    5/5 good HPI fits, movements [mm/s] =     18.4 /     21.1 /     19.4 /     20.4 /     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.5 /     28.9 /     27.5
t=19.610:    5/5 good HPI fits, movements [mm/s] =     21.3 /     25.7 /     21.7 /     31.8 /     31.5
t=19.710:    5/5 good HPI fits, movements [mm/s] =     21.5 /     20.8 /     23.2 /     29.7 /     27.6
t=19.850:    5/5 good HPI fits, movements [mm/s] =     16.6 /     14.7 /     18.5 /     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 /     26.8 /     24.1 /     32.0 /     26.4
t=22.000:    5/5 good HPI fits, movements [mm/s] =     65.7 /     59.5 /     46.4 /     64.8 /     52.1
t=22.050:    5/5 good HPI fits, movements [mm/s] =     59.9 /     62.8 /     51.0 /     69.2 /     57.8
t=22.100:    5/5 good HPI fits, movements [mm/s] =     61.8 /     65.6 /     58.5 /     75.0 /     64.7
t=22.140:    5/5 good HPI fits, movements [mm/s] =     74.2 /     73.4 /     62.3 /     79.6 /     68.0
t=22.180:    5/5 good HPI fits, movements [mm/s] =     64.0 /     59.3 /     53.0 /     67.7 /     53.7
t=22.240:    5/5 good HPI fits, movements [mm/s] =     38.4 /     39.6 /     36.7 /     48.1 /     41.8
t=22.300:    5/5 good HPI fits, movements [mm/s] =     41.8 /     39.9 /     33.7 /     47.4 /     39.3
t=22.400:    5/5 good HPI fits, movements [mm/s] =     31.9 /     28.6 /     22.0 /     30.8 /     25.1
t=23.400:    5/5 good HPI fits, movements [mm/s] =      1.8 /      1.8 /      1.1 /      1.5 /      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.3 /     33.0 /     31.7 /     38.9 /     35.1
t=24.790:    5/5 good HPI fits, movements [mm/s] =     31.0 /     35.9 /     33.0 /     42.8 /     41.1
t=24.850:    5/5 good HPI fits, movements [mm/s] =     37.5 /     41.1 /     35.6 /     45.2 /     42.0
t=24.890:    5/5 good HPI fits, movements [mm/s] =     50.6 /     51.7 /     49.5 /     60.1 /     53.3
t=24.930:    5/5 good HPI fits, movements [mm/s] =     56.0 /     54.3 /     59.3 /     68.4 /     62.0
t=24.980:    5/5 good HPI fits, movements [mm/s] =     47.1 /     48.1 /     51.0 /     58.5 /     53.8
t=25.080:    5/5 good HPI fits, movements [mm/s] =     23.8 /     19.2 /     23.3 /     28.1 /     26.3
t=25.510:    5/5 good HPI fits, movements [mm/s] =      2.4 /      6.5 /      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:

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 120 180 240 300 360 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<42:26,    5.14s/it]
  1%|1         | cHPI SNRs : 6/496 [00:05<06:43,    1.21it/s]
  2%|2         | cHPI SNRs : 11/496 [00:05<03:32,    2.28it/s]
  3%|3         | cHPI SNRs : 16/496 [00:05<02:21,    3.39it/s]
  4%|4         | cHPI SNRs : 21/496 [00:05<01:44,    4.55it/s]
  5%|5         | cHPI SNRs : 27/496 [00:05<01:18,    6.01it/s]
  6%|6         | cHPI SNRs : 32/496 [00:05<01:03,    7.28it/s]
  7%|7         | cHPI SNRs : 37/496 [00:05<00:53,    8.60it/s]
  8%|8         | cHPI SNRs : 42/496 [00:05<00:45,    9.98it/s]
  9%|9         | cHPI SNRs : 47/496 [00:05<00:39,   11.41it/s]
 10%|#         | cHPI SNRs : 52/496 [00:05<00:34,   12.91it/s]
 11%|#1        | cHPI SNRs : 57/496 [00:05<00:30,   14.47it/s]
 12%|#2        | cHPI SNRs : 62/496 [00:05<00:26,   16.09it/s]
 14%|#3        | cHPI SNRs : 67/496 [00:05<00:24,   17.76it/s]
 15%|#4        | cHPI SNRs : 72/496 [00:05<00:21,   19.50it/s]
 16%|#5        | cHPI SNRs : 77/496 [00:05<00:19,   21.31it/s]
 17%|#6        | cHPI SNRs : 82/496 [00:05<00:17,   23.20it/s]
 18%|#7        | cHPI SNRs : 87/496 [00:05<00:16,   25.16it/s]
 19%|#8        | cHPI SNRs : 92/496 [00:05<00:14,   27.19it/s]
 20%|#9        | cHPI SNRs : 97/496 [00:05<00:13,   29.28it/s]
 21%|##        | cHPI SNRs : 102/496 [00:05<00:12,   31.46it/s]
 22%|##1       | cHPI SNRs : 107/496 [00:05<00:11,   33.71it/s]
 23%|##2       | cHPI SNRs : 112/496 [00:05<00:10,   36.04it/s]
 24%|##3       | cHPI SNRs : 117/496 [00:05<00:09,   38.45it/s]
 25%|##4       | cHPI SNRs : 122/496 [00:05<00:09,   40.94it/s]
 26%|##5       | cHPI SNRs : 127/496 [00:05<00:08,   43.52it/s]
 27%|##6       | cHPI SNRs : 132/496 [00:05<00:07,   46.17it/s]
 28%|##7       | cHPI SNRs : 137/496 [00:05<00:07,   48.90it/s]
 29%|##8       | cHPI SNRs : 142/496 [00:05<00:06,   51.71it/s]
 30%|##9       | cHPI SNRs : 147/496 [00:05<00:06,   54.61it/s]
 31%|###       | cHPI SNRs : 152/496 [00:05<00:05,   57.56it/s]
 32%|###1      | cHPI SNRs : 157/496 [00:05<00:05,   60.59it/s]
 33%|###2      | cHPI SNRs : 162/496 [00:05<00:05,   63.70it/s]
 34%|###3      | cHPI SNRs : 167/496 [00:05<00:04,   66.88it/s]
 35%|###4      | cHPI SNRs : 172/496 [00:05<00:04,   70.14it/s]
 36%|###5      | cHPI SNRs : 177/496 [00:05<00:04,   73.48it/s]
 37%|###6      | cHPI SNRs : 182/496 [00:05<00:04,   76.86it/s]
 38%|###7      | cHPI SNRs : 187/496 [00:05<00:03,   80.27it/s]
 39%|###8      | cHPI SNRs : 192/496 [00:05<00:03,   83.73it/s]
 40%|###9      | cHPI SNRs : 197/496 [00:05<00:03,   87.25it/s]
 41%|####      | cHPI SNRs : 202/496 [00:05<00:03,   90.83it/s]
 42%|####1     | cHPI SNRs : 207/496 [00:05<00:03,   94.47it/s]
 43%|####2     | cHPI SNRs : 212/496 [00:05<00:02,   98.16it/s]
 44%|####3     | cHPI SNRs : 217/496 [00:05<00:02,  101.91it/s]
 45%|####4     | cHPI SNRs : 222/496 [00:05<00:02,  105.61it/s]
 46%|####5     | cHPI SNRs : 227/496 [00:05<00:02,  109.41it/s]
 47%|####6     | cHPI SNRs : 232/496 [00:05<00:02,  113.35it/s]
 48%|####7     | cHPI SNRs : 237/496 [00:05<00:02,  117.34it/s]
 49%|####8     | cHPI SNRs : 242/496 [00:05<00:02,  121.27it/s]
 50%|####9     | cHPI SNRs : 247/496 [00:05<00:01,  125.28it/s]
 51%|#####     | cHPI SNRs : 252/496 [00:06<00:01,  129.24it/s]
 52%|#####1    | cHPI SNRs : 257/496 [00:06<00:01,  133.19it/s]
 53%|#####2    | cHPI SNRs : 262/496 [00:06<00:01,  137.14it/s]
 54%|#####3    | cHPI SNRs : 267/496 [00:06<00:01,  141.15it/s]
 55%|#####4    | cHPI SNRs : 272/496 [00:06<00:01,  145.12it/s]
 56%|#####5    | cHPI SNRs : 277/496 [00:06<00:01,  148.91it/s]
 57%|#####6    | cHPI SNRs : 282/496 [00:06<00:01,  152.85it/s]
 58%|#####7    | cHPI SNRs : 287/496 [00:06<00:01,  156.89it/s]
 59%|#####8    | cHPI SNRs : 292/496 [00:06<00:01,  160.83it/s]
 60%|#####9    | cHPI SNRs : 297/496 [00:06<00:01,  164.82it/s]
 61%|######    | cHPI SNRs : 302/496 [00:06<00:01,  168.59it/s]
 62%|######1   | cHPI SNRs : 307/496 [00:06<00:01,  172.42it/s]
 63%|######2   | cHPI SNRs : 312/496 [00:06<00:01,  176.12it/s]
 64%|######3   | cHPI SNRs : 317/496 [00:06<00:00,  179.60it/s]
 65%|######4   | cHPI SNRs : 322/496 [00:06<00:00,  183.23it/s]
 66%|######5   | cHPI SNRs : 327/496 [00:06<00:00,  186.68it/s]
 67%|######6   | cHPI SNRs : 332/496 [00:06<00:00,  190.21it/s]
 68%|######7   | cHPI SNRs : 337/496 [00:06<00:00,  193.75it/s]
 69%|######8   | cHPI SNRs : 342/496 [00:06<00:00,  197.22it/s]
 70%|######9   | cHPI SNRs : 347/496 [00:06<00:00,  200.72it/s]
 71%|#######   | cHPI SNRs : 352/496 [00:06<00:00,  204.13it/s]
 72%|#######1  | cHPI SNRs : 357/496 [00:06<00:00,  207.62it/s]
 73%|#######2  | cHPI SNRs : 362/496 [00:06<00:00,  211.17it/s]
 74%|#######3  | cHPI SNRs : 367/496 [00:06<00:00,  214.40it/s]
 75%|#######5  | cHPI SNRs : 372/496 [00:06<00:00,  217.59it/s]
 76%|#######6  | cHPI SNRs : 377/496 [00:06<00:00,  220.87it/s]
 77%|#######7  | cHPI SNRs : 382/496 [00:06<00:00,  224.03it/s]
 78%|#######8  | cHPI SNRs : 387/496 [00:06<00:00,  226.22it/s]
 79%|#######9  | cHPI SNRs : 392/496 [00:06<00:00,  229.32it/s]
 80%|########  | cHPI SNRs : 397/496 [00:06<00:00,  232.23it/s]
 81%|########1 | cHPI SNRs : 402/496 [00:06<00:00,  234.49it/s]
 82%|########2 | cHPI SNRs : 407/496 [00:06<00:00,  236.95it/s]
 83%|########3 | cHPI SNRs : 412/496 [00:06<00:00,  239.39it/s]
 84%|########4 | cHPI SNRs : 417/496 [00:06<00:00,  241.95it/s]
 85%|########5 | cHPI SNRs : 422/496 [00:06<00:00,  244.25it/s]
 86%|########6 | cHPI SNRs : 427/496 [00:06<00:00,  246.14it/s]
 87%|########7 | cHPI SNRs : 432/496 [00:06<00:00,  248.55it/s]
 88%|########8 | cHPI SNRs : 437/496 [00:06<00:00,  250.92it/s]
 89%|########9 | cHPI SNRs : 442/496 [00:06<00:00,  252.76it/s]
 90%|######### | cHPI SNRs : 447/496 [00:06<00:00,  254.02it/s]
 91%|#########1| cHPI SNRs : 452/496 [00:06<00:00,  255.97it/s]
 92%|#########2| cHPI SNRs : 457/496 [00:06<00:00,  257.45it/s]
 93%|#########3| cHPI SNRs : 462/496 [00:06<00:00,  259.12it/s]
 94%|#########4| cHPI SNRs : 467/496 [00:06<00:00,  260.80it/s]
 95%|#########5| cHPI SNRs : 472/496 [00:06<00:00,  262.54it/s]
 96%|#########6| cHPI SNRs : 477/496 [00:06<00:00,  264.48it/s]
 97%|#########7| cHPI SNRs : 482/496 [00:06<00:00,  266.51it/s]
 98%|#########8| cHPI SNRs : 487/496 [00:06<00:00,  268.43it/s]
 99%|#########9| cHPI SNRs : 492/496 [00:06<00:00,  269.40it/s]
100%|##########| cHPI SNRs : 496/496 [00:06<00:00,   72.58it/s]

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

Estimated memory usage: 18 MB

Gallery generated by Sphinx-Gallery