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.plot_psd()
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)
Effective window size : 1.707 (s)
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 sec duration)

  0%|          | cHPI amplitudes : 0/1696 [00:00<?,       ?it/s]
  0%|          | cHPI amplitudes : 1/1696 [00:02<1:11:32,    2.53s/it]
  1%|1         | cHPI amplitudes : 20/1696 [00:02<03:23,    8.24it/s]
  2%|2         | cHPI amplitudes : 40/1696 [00:02<01:38,   16.81it/s]
  4%|3         | cHPI amplitudes : 60/1696 [00:02<01:03,   25.70it/s]
  5%|4         | cHPI amplitudes : 80/1696 [00:02<00:46,   34.92it/s]
  6%|5         | cHPI amplitudes : 99/1696 [00:02<00:36,   43.99it/s]
  7%|7         | cHPI amplitudes : 119/1696 [00:02<00:29,   53.89it/s]
  8%|8         | cHPI amplitudes : 139/1696 [00:02<00:24,   64.13it/s]
  9%|9         | cHPI amplitudes : 159/1696 [00:02<00:20,   74.72it/s]
 11%|#         | cHPI amplitudes : 179/1696 [00:02<00:17,   85.66it/s]
 12%|#1        | cHPI amplitudes : 199/1696 [00:02<00:15,   96.95it/s]
 13%|#2        | cHPI amplitudes : 219/1696 [00:02<00:13,  108.58it/s]
 14%|#4        | cHPI amplitudes : 239/1696 [00:02<00:12,  120.57it/s]
 15%|#5        | cHPI amplitudes : 259/1696 [00:02<00:10,  132.91it/s]
 16%|#6        | cHPI amplitudes : 279/1696 [00:02<00:09,  145.58it/s]
 18%|#7        | cHPI amplitudes : 299/1696 [00:02<00:08,  158.62it/s]
 19%|#8        | cHPI amplitudes : 319/1696 [00:02<00:08,  172.05it/s]
 20%|#9        | cHPI amplitudes : 339/1696 [00:02<00:07,  185.83it/s]
 21%|##1       | cHPI amplitudes : 359/1696 [00:02<00:06,  199.90it/s]
 22%|##2       | cHPI amplitudes : 379/1696 [00:02<00:06,  214.29it/s]
 24%|##3       | cHPI amplitudes : 399/1696 [00:02<00:05,  229.03it/s]
 25%|##4       | cHPI amplitudes : 419/1696 [00:02<00:05,  244.12it/s]
 26%|##5       | cHPI amplitudes : 439/1696 [00:02<00:04,  259.50it/s]
 27%|##7       | cHPI amplitudes : 459/1696 [00:02<00:04,  275.20it/s]
 28%|##8       | cHPI amplitudes : 479/1696 [00:02<00:04,  291.17it/s]
 29%|##9       | cHPI amplitudes : 499/1696 [00:02<00:03,  307.41it/s]
 31%|###       | cHPI amplitudes : 519/1696 [00:02<00:03,  323.83it/s]
 32%|###1      | cHPI amplitudes : 538/1696 [00:02<00:03,  339.51it/s]
 33%|###2      | cHPI amplitudes : 555/1696 [00:02<00:03,  353.04it/s]
 34%|###3      | cHPI amplitudes : 575/1696 [00:03<00:03,  370.22it/s]
 35%|###5      | cHPI amplitudes : 595/1696 [00:03<00:02,  387.54it/s]
 36%|###6      | cHPI amplitudes : 615/1696 [00:03<00:02,  405.14it/s]
 37%|###7      | cHPI amplitudes : 635/1696 [00:03<00:02,  422.97it/s]
 39%|###8      | cHPI amplitudes : 655/1696 [00:03<00:02,  440.93it/s]
 40%|###9      | cHPI amplitudes : 675/1696 [00:03<00:02,  458.98it/s]
 41%|####      | cHPI amplitudes : 695/1696 [00:03<00:02,  476.88it/s]
 42%|####2     | cHPI amplitudes : 715/1696 [00:03<00:01,  494.83it/s]
 43%|####3     | cHPI amplitudes : 735/1696 [00:03<00:01,  512.98it/s]
 45%|####4     | cHPI amplitudes : 755/1696 [00:03<00:01,  530.94it/s]
 46%|####5     | cHPI amplitudes : 775/1696 [00:03<00:01,  549.08it/s]
 47%|####6     | cHPI amplitudes : 795/1696 [00:03<00:01,  567.11it/s]
 48%|####8     | cHPI amplitudes : 815/1696 [00:03<00:01,  585.09it/s]
 49%|####9     | cHPI amplitudes : 835/1696 [00:03<00:01,  602.71it/s]
 50%|#####     | cHPI amplitudes : 855/1696 [00:03<00:01,  620.30it/s]
 52%|#####1    | cHPI amplitudes : 874/1696 [00:03<00:01,  635.99it/s]
 53%|#####2    | cHPI amplitudes : 894/1696 [00:03<00:01,  653.49it/s]
 54%|#####3    | cHPI amplitudes : 913/1696 [00:03<00:01,  669.30it/s]
 55%|#####5    | cHPI amplitudes : 933/1696 [00:03<00:01,  686.01it/s]
 56%|#####6    | cHPI amplitudes : 953/1696 [00:03<00:01,  702.76it/s]
 57%|#####7    | cHPI amplitudes : 972/1696 [00:03<00:01,  718.00it/s]
 58%|#####8    | cHPI amplitudes : 991/1696 [00:03<00:00,  732.93it/s]
 60%|#####9    | cHPI amplitudes : 1011/1696 [00:03<00:00,  749.13it/s]
 61%|######    | cHPI amplitudes : 1031/1696 [00:03<00:00,  765.07it/s]
 62%|######1   | cHPI amplitudes : 1051/1696 [00:03<00:00,  781.14it/s]
 63%|######3   | cHPI amplitudes : 1069/1696 [00:03<00:00,  791.81it/s]
 64%|######4   | cHPI amplitudes : 1089/1696 [00:03<00:00,  807.51it/s]
 65%|######5   | cHPI amplitudes : 1109/1696 [00:03<00:00,  821.98it/s]
 67%|######6   | cHPI amplitudes : 1129/1696 [00:03<00:00,  837.17it/s]
 68%|######7   | cHPI amplitudes : 1149/1696 [00:03<00:00,  851.61it/s]
 69%|######8   | cHPI amplitudes : 1169/1696 [00:03<00:00,  865.84it/s]
 70%|#######   | cHPI amplitudes : 1189/1696 [00:03<00:00,  879.58it/s]
 71%|#######1  | cHPI amplitudes : 1208/1696 [00:03<00:00,  890.77it/s]
 72%|#######2  | cHPI amplitudes : 1228/1696 [00:03<00:00,  903.24it/s]
 74%|#######3  | cHPI amplitudes : 1248/1696 [00:03<00:00,  915.83it/s]
 75%|#######4  | cHPI amplitudes : 1268/1696 [00:03<00:00,  928.05it/s]
 76%|#######5  | cHPI amplitudes : 1286/1696 [00:03<00:00,  935.19it/s]
 77%|#######7  | cHPI amplitudes : 1306/1696 [00:03<00:00,  945.78it/s]
 78%|#######8  | cHPI amplitudes : 1326/1696 [00:03<00:00,  957.27it/s]
 79%|#######9  | cHPI amplitudes : 1346/1696 [00:03<00:00,  968.38it/s]
 81%|########  | cHPI amplitudes : 1366/1696 [00:03<00:00,  978.36it/s]
 82%|########1 | cHPI amplitudes : 1386/1696 [00:03<00:00,  988.44it/s]
 83%|########2 | cHPI amplitudes : 1405/1696 [00:03<00:00,  994.67it/s]
 84%|########4 | cHPI amplitudes : 1425/1696 [00:03<00:00, 1004.51it/s]
 85%|########5 | cHPI amplitudes : 1445/1696 [00:03<00:00, 1014.05it/s]
 86%|########6 | cHPI amplitudes : 1464/1696 [00:03<00:00, 1021.34it/s]
 88%|########7 | cHPI amplitudes : 1484/1696 [00:03<00:00, 1029.51it/s]
 89%|########8 | cHPI amplitudes : 1504/1696 [00:03<00:00, 1037.89it/s]
 90%|########9 | cHPI amplitudes : 1522/1696 [00:03<00:00, 1039.93it/s]
 91%|######### | cHPI amplitudes : 1542/1696 [00:03<00:00, 1047.72it/s]
 92%|#########2| cHPI amplitudes : 1561/1696 [00:03<00:00, 1053.56it/s]
 93%|#########3| cHPI amplitudes : 1581/1696 [00:03<00:00, 1060.83it/s]
 94%|#########4| cHPI amplitudes : 1600/1696 [00:03<00:00, 1066.13it/s]
 96%|#########5| cHPI amplitudes : 1620/1696 [00:03<00:00, 1073.57it/s]
 97%|#########6| cHPI amplitudes : 1639/1696 [00:03<00:00, 1077.69it/s]
 98%|#########7| cHPI amplitudes : 1659/1696 [00:03<00:00, 1083.91it/s]
 99%|#########8| cHPI amplitudes : 1679/1696 [00:03<00:00, 1089.32it/s]
100%|#########9| cHPI amplitudes : 1695/1696 [00:03<00:00, 1082.51it/s]
100%|##########| cHPI amplitudes : 1696/1696 [00:03<00:00,  430.07it/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<19:24,    1.46it/s]
  2%|2         | cHPI locations  : 36/1696 [00:00<00:30,   53.78it/s]
  4%|4         | cHPI locations  : 74/1696 [00:00<00:14,  110.79it/s]
  5%|5         | cHPI locations  : 87/1696 [00:00<00:13,  122.48it/s]
  6%|5         | cHPI locations  : 101/1696 [00:00<00:13,  121.51it/s]
  8%|8         | cHPI locations  : 138/1696 [00:00<00:09,  169.39it/s]
 10%|9         | cHPI locations  : 166/1696 [00:00<00:07,  204.95it/s]
 10%|#         | cHPI locations  : 173/1696 [00:00<00:07,  201.19it/s]
 11%|#1        | cHPI locations  : 195/1696 [00:00<00:06,  227.76it/s]
 12%|#1        | cHPI locations  : 201/1696 [00:01<00:07,  198.60it/s]
 13%|#2        | cHPI locations  : 220/1696 [00:01<00:07,  208.48it/s]
 14%|#4        | cHPI locations  : 242/1696 [00:01<00:06,  232.11it/s]
 16%|#5        | cHPI locations  : 264/1696 [00:01<00:05,  255.92it/s]
 18%|#7        | cHPI locations  : 301/1696 [00:01<00:05,  270.27it/s]
 20%|#9        | cHPI locations  : 339/1696 [00:01<00:04,  312.26it/s]
 21%|##        | cHPI locations  : 355/1696 [00:01<00:04,  311.87it/s]
 23%|##2       | cHPI locations  : 389/1696 [00:01<00:03,  348.54it/s]
 24%|##3       | cHPI locations  : 401/1696 [00:01<00:04,  320.06it/s]
 26%|##5       | cHPI locations  : 438/1696 [00:01<00:03,  358.65it/s]
 28%|##8       | cHPI locations  : 476/1696 [00:01<00:03,  398.92it/s]
 28%|##8       | cHPI locations  : 481/1696 [00:01<00:03,  381.49it/s]
 30%|##9       | cHPI locations  : 501/1696 [00:01<00:03,  351.18it/s]
 31%|###       | cHPI locations  : 523/1696 [00:01<00:03,  369.93it/s]
 32%|###2      | cHPI locations  : 543/1696 [00:01<00:03,  367.49it/s]
 33%|###2      | cHPI locations  : 559/1696 [00:01<00:03,  303.83it/s]
 33%|###3      | cHPI locations  : 568/1696 [00:02<00:04,  246.27it/s]
 34%|###3      | cHPI locations  : 573/1696 [00:02<00:05,  212.58it/s]
 34%|###4      | cHPI locations  : 577/1696 [00:02<00:05,  196.61it/s]
 34%|###4      | cHPI locations  : 581/1696 [00:02<00:06,  177.06it/s]
 35%|###4      | cHPI locations  : 587/1696 [00:02<00:06,  162.41it/s]
 35%|###5      | cHPI locations  : 600/1696 [00:03<00:06,  157.09it/s]
 38%|###7      | cHPI locations  : 637/1696 [00:03<00:05,  177.67it/s]
 38%|###8      | cHPI locations  : 645/1696 [00:03<00:05,  177.67it/s]
 40%|####      | cHPI locations  : 682/1696 [00:03<00:05,  199.41it/s]
 41%|####1     | cHPI locations  : 700/1696 [00:03<00:05,  194.52it/s]
 42%|####2     | cHPI locations  : 719/1696 [00:03<00:04,  199.87it/s]
 45%|####4     | cHPI locations  : 757/1696 [00:03<00:04,  222.55it/s]
 47%|####6     | cHPI locations  : 789/1696 [00:03<00:04,  216.84it/s]
 47%|####6     | cHPI locations  : 794/1696 [00:03<00:04,  202.81it/s]
 47%|####7     | cHPI locations  : 800/1696 [00:03<00:04,  186.34it/s]
 47%|####7     | cHPI locations  : 805/1696 [00:04<00:05,  175.74it/s]
 48%|####7     | cHPI locations  : 810/1696 [00:04<00:05,  162.02it/s]
 48%|####8     | cHPI locations  : 815/1696 [00:04<00:05,  153.51it/s]
 48%|####8     | cHPI locations  : 819/1696 [00:04<00:06,  142.25it/s]
 49%|####8     | cHPI locations  : 823/1696 [00:04<00:06,  135.51it/s]
 49%|####8     | cHPI locations  : 827/1696 [00:04<00:06,  126.40it/s]
 49%|####9     | cHPI locations  : 833/1696 [00:04<00:07,  121.37it/s]
 50%|####9     | cHPI locations  : 842/1696 [00:04<00:06,  123.04it/s]
 50%|#####     | cHPI locations  : 848/1696 [00:05<00:07,  118.25it/s]
 52%|#####2    | cHPI locations  : 886/1696 [00:05<00:05,  136.11it/s]
 54%|#####3    | cHPI locations  : 913/1696 [00:05<00:05,  146.16it/s]
 56%|#####5    | cHPI locations  : 948/1696 [00:05<00:04,  153.29it/s]
 58%|#####7    | cHPI locations  : 979/1696 [00:05<00:04,  164.46it/s]
 60%|#####9    | cHPI locations  : 1017/1696 [00:05<00:03,  183.16it/s]
 61%|######1   | cHPI locations  : 1040/1696 [00:05<00:03,  183.17it/s]
 62%|######1   | cHPI locations  : 1051/1696 [00:05<00:03,  172.56it/s]
 63%|######2   | cHPI locations  : 1062/1696 [00:05<00:03,  167.19it/s]
 63%|######3   | cHPI locations  : 1072/1696 [00:05<00:03,  158.06it/s]
 64%|######4   | cHPI locations  : 1086/1696 [00:06<00:03,  155.01it/s]
 66%|######5   | cHPI locations  : 1111/1696 [00:06<00:03,  163.17it/s]
 68%|######7   | cHPI locations  : 1150/1696 [00:06<00:03,  181.48it/s]
 70%|######9   | cHPI locations  : 1186/1696 [00:06<00:02,  187.31it/s]
 70%|#######   | cHPI locations  : 1195/1696 [00:06<00:02,  187.17it/s]
 72%|#######2  | cHPI locations  : 1229/1696 [00:06<00:02,  203.58it/s]
 75%|#######4  | cHPI locations  : 1267/1696 [00:06<00:01,  222.79it/s]
 76%|#######5  | cHPI locations  : 1286/1696 [00:06<00:01,  218.60it/s]
 76%|#######6  | cHPI locations  : 1291/1696 [00:06<00:01,  215.53it/s]
 76%|#######6  | cHPI locations  : 1296/1696 [00:06<00:01,  203.37it/s]
 77%|#######6  | cHPI locations  : 1301/1696 [00:06<00:02,  192.44it/s]
 77%|#######7  | cHPI locations  : 1306/1696 [00:06<00:02,  177.93it/s]
 77%|#######7  | cHPI locations  : 1311/1696 [00:07<00:02,  164.30it/s]
 78%|#######7  | cHPI locations  : 1315/1696 [00:07<00:02,  155.96it/s]
 78%|#######7  | cHPI locations  : 1319/1696 [00:07<00:02,  144.88it/s]
 78%|#######8  | cHPI locations  : 1325/1696 [00:07<00:02,  139.23it/s]
 78%|#######8  | cHPI locations  : 1331/1696 [00:07<00:02,  131.29it/s]
 79%|#######9  | cHPI locations  : 1341/1696 [00:07<00:02,  127.86it/s]
 81%|########1 | cHPI locations  : 1379/1696 [00:07<00:02,  144.66it/s]
 81%|########1 | cHPI locations  : 1380/1696 [00:07<00:02,  142.21it/s]
 84%|########3 | cHPI locations  : 1418/1696 [00:07<00:01,  160.17it/s]
 85%|########4 | cHPI locations  : 1441/1696 [00:08<00:01,  161.63it/s]
 87%|########6 | cHPI locations  : 1471/1696 [00:08<00:01,  172.07it/s]
 89%|########8 | cHPI locations  : 1509/1696 [00:08<00:00,  190.72it/s]
 91%|######### | cHPI locations  : 1541/1696 [00:08<00:00,  194.20it/s]
 92%|#########2| cHPI locations  : 1564/1696 [00:08<00:00,  201.12it/s]
 92%|#########2| cHPI locations  : 1565/1696 [00:08<00:00,  186.98it/s]
 93%|#########2| cHPI locations  : 1573/1696 [00:08<00:00,  174.60it/s]
 93%|#########3| cHPI locations  : 1580/1696 [00:08<00:00,  162.49it/s]
 94%|#########3| cHPI locations  : 1586/1696 [00:08<00:00,  155.36it/s]
 94%|#########3| cHPI locations  : 1590/1696 [00:09<00:00,  143.99it/s]
 94%|#########3| cHPI locations  : 1594/1696 [00:09<00:00,  132.20it/s]
 94%|#########4| cHPI locations  : 1599/1696 [00:09<00:00,  119.92it/s]
 95%|#########4| cHPI locations  : 1609/1696 [00:09<00:00,  117.83it/s]
 97%|#########7| cHPI locations  : 1646/1696 [00:09<00:00,  132.74it/s]
 97%|#########7| cHPI locations  : 1652/1696 [00:09<00:00,  125.06it/s]
100%|#########9| cHPI locations  : 1688/1696 [00:10<00:00,  139.74it/s]
100%|##########| cHPI locations  : 1696/1696 [00:10<00:00,  169.36it/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.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.8
t=14.670:    5/5 good HPI fits, movements [mm/s] =     33.5 /     34.5 /     39.8 /     37.8 /     30.4
t=14.720:    5/5 good HPI fits, movements [mm/s] =     49.3 /     46.6 /     60.4 /     59.5 /     44.6
t=14.760:    5/5 good HPI fits, movements [mm/s] =     64.8 /     63.5 /     72.8 /     70.7 /     56.1
t=14.800:    5/5 good HPI fits, movements [mm/s] =     67.2 /     58.8 /     83.1 /     78.2 /     61.3
t=14.860:    5/5 good HPI fits, movements [mm/s] =     54.1 /     52.3 /     57.6 /     62.0 /     47.0
t=14.990:    5/5 good HPI fits, movements [mm/s] =     23.7 /     22.5 /     24.0 /     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.6 /      3.4 /      3.0 /      3.8 /      3.3
t=16.930:    5/5 good HPI fits, movements [mm/s] =     52.1 /     55.3 /     58.1 /     67.2 /     59.4
t=16.990:    5/5 good HPI fits, movements [mm/s] =     57.2 /     53.7 /     62.0 /     64.8 /     57.3
t=17.040:    5/5 good HPI fits, movements [mm/s] =     53.2 /     50.3 /     60.0 /     62.0 /     49.3
t=17.090:    5/5 good HPI fits, movements [mm/s] =     57.5 /     50.0 /     66.4 /     65.3 /     52.0
t=17.140:    5/5 good HPI fits, movements [mm/s] =     60.5 /     54.7 /     73.1 /     67.2 /     57.2
t=17.180:    5/5 good HPI fits, movements [mm/s] =     63.8 /     57.8 /     74.0 /     71.8 /     62.2
t=17.220:    5/5 good HPI fits, movements [mm/s] =     60.7 /     59.9 /     75.3 /     71.6 /     63.1
t=17.260:    5/5 good HPI fits, movements [mm/s] =     68.1 /     67.0 /     73.6 /     68.7 /     69.2
t=17.320:    5/5 good HPI fits, movements [mm/s] =     49.6 /     50.4 /     54.6 /     55.1 /     52.4
t=17.470:    5/5 good HPI fits, movements [mm/s] =     18.3 /     21.2 /     19.4 /     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.2 /     19.5 /     28.9 /     27.5
t=19.610:    5/5 good HPI fits, movements [mm/s] =     21.3 /     25.7 /     21.7 /     31.7 /     31.5
t=19.710:    5/5 good HPI fits, movements [mm/s] =     21.6 /     20.6 /     23.1 /     29.6 /     27.6
t=19.850:    5/5 good HPI fits, movements [mm/s] =     16.7 /     14.8 /     18.5 /     21.8 /     20.2
t=20.850:    5/5 good HPI fits, movements [mm/s] =      2.3 /      1.9 /      2.0 /      2.6 /      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.9 /     24.1 /     32.0 /     26.4
t=22.000:    5/5 good HPI fits, movements [mm/s] =     66.0 /     59.4 /     46.4 /     64.7 /     52.2
t=22.050:    5/5 good HPI fits, movements [mm/s] =     60.1 /     62.9 /     51.0 /     69.3 /     57.8
t=22.100:    5/5 good HPI fits, movements [mm/s] =     61.3 /     65.7 /     58.5 /     74.8 /     64.4
t=22.140:    5/5 good HPI fits, movements [mm/s] =     74.4 /     73.5 /     62.3 /     80.3 /     68.2
t=22.180:    5/5 good HPI fits, movements [mm/s] =     63.6 /     59.1 /     53.4 /     67.1 /     53.8
t=22.240:    5/5 good HPI fits, movements [mm/s] =     38.7 /     39.4 /     36.5 /     48.0 /     42.1
t=22.300:    5/5 good HPI fits, movements [mm/s] =     42.1 /     40.1 /     33.6 /     47.4 /     39.1
t=22.400:    5/5 good HPI fits, movements [mm/s] =     31.8 /     28.4 /     22.1 /     30.8 /     24.9
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.2 /     33.2 /     31.5 /     38.9 /     35.4
t=24.790:    5/5 good HPI fits, movements [mm/s] =     31.2 /     35.8 /     33.3 /     42.9 /     41.0
t=24.850:    5/5 good HPI fits, movements [mm/s] =     37.4 /     41.2 /     35.5 /     45.1 /     41.9
t=24.890:    5/5 good HPI fits, movements [mm/s] =     50.0 /     51.8 /     49.9 /     59.9 /     53.6
t=24.930:    5/5 good HPI fits, movements [mm/s] =     56.3 /     55.5 /     59.2 /     68.2 /     61.6
t=24.980:    5/5 good HPI fits, movements [mm/s] =     47.3 /     47.1 /     51.0 /     58.7 /     53.7
t=25.080:    5/5 good HPI fits, movements [mm/s] =     23.8 /     19.1 /     23.1 /     28.2 /     26.3
t=25.510:    5/5 good HPI fits, movements [mm/s] =      2.5 /      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 sec duration)

  0%|          | cHPI SNRs : 0/496 [00:00<?,       ?it/s]
  0%|          | cHPI SNRs : 1/496 [00:04<37:35,    4.56s/it]
  3%|2         | cHPI SNRs : 14/496 [00:04<02:30,    3.21it/s]
  5%|5         | cHPI SNRs : 27/496 [00:04<01:14,    6.34it/s]
  8%|8         | cHPI SNRs : 40/496 [00:04<00:47,    9.60it/s]
 11%|#         | cHPI SNRs : 53/496 [00:04<00:34,   13.00it/s]
 13%|#3        | cHPI SNRs : 66/496 [00:04<00:25,   16.56it/s]
 16%|#5        | cHPI SNRs : 79/496 [00:04<00:20,   20.26it/s]
 19%|#8        | cHPI SNRs : 92/496 [00:04<00:16,   24.12it/s]
 21%|##1       | cHPI SNRs : 105/496 [00:04<00:13,   28.15it/s]
 24%|##3       | cHPI SNRs : 117/496 [00:04<00:11,   32.01it/s]
 26%|##6       | cHPI SNRs : 130/496 [00:04<00:10,   36.36it/s]
 29%|##8       | cHPI SNRs : 143/496 [00:04<00:08,   40.90it/s]
 31%|###1      | cHPI SNRs : 156/496 [00:04<00:07,   45.63it/s]
 34%|###4      | cHPI SNRs : 169/496 [00:04<00:06,   50.53it/s]
 37%|###6      | cHPI SNRs : 182/496 [00:04<00:05,   55.61it/s]
 39%|###9      | cHPI SNRs : 195/496 [00:04<00:04,   60.91it/s]
 42%|####1     | cHPI SNRs : 208/496 [00:04<00:04,   66.40it/s]
 45%|####4     | cHPI SNRs : 221/496 [00:04<00:03,   72.09it/s]
 47%|####7     | cHPI SNRs : 234/496 [00:04<00:03,   78.00it/s]
 50%|####9     | cHPI SNRs : 247/496 [00:04<00:02,   84.10it/s]
 52%|#####2    | cHPI SNRs : 260/496 [00:04<00:02,   90.42it/s]
 55%|#####5    | cHPI SNRs : 273/496 [00:04<00:02,   96.94it/s]
 58%|#####7    | cHPI SNRs : 286/496 [00:04<00:02,  103.69it/s]
 60%|######    | cHPI SNRs : 299/496 [00:04<00:01,  110.65it/s]
 63%|######2   | cHPI SNRs : 312/496 [00:04<00:01,  117.83it/s]
 66%|######5   | cHPI SNRs : 325/496 [00:04<00:01,  125.22it/s]
 68%|######8   | cHPI SNRs : 338/496 [00:04<00:01,  132.82it/s]
 71%|#######   | cHPI SNRs : 351/496 [00:04<00:01,  140.65it/s]
 73%|#######3  | cHPI SNRs : 364/496 [00:05<00:00,  148.69it/s]
 76%|#######6  | cHPI SNRs : 377/496 [00:05<00:00,  156.95it/s]
 79%|#######8  | cHPI SNRs : 390/496 [00:05<00:00,  165.42it/s]
 81%|########  | cHPI SNRs : 400/496 [00:05<00:00,  171.57it/s]
 83%|########3 | cHPI SNRs : 413/496 [00:05<00:00,  180.49it/s]
 86%|########5 | cHPI SNRs : 426/496 [00:05<00:00,  189.63it/s]
 89%|########8 | cHPI SNRs : 439/496 [00:05<00:00,  198.98it/s]
 91%|#########1| cHPI SNRs : 452/496 [00:05<00:00,  208.50it/s]
 94%|#########3| cHPI SNRs : 465/496 [00:05<00:00,  218.24it/s]
 96%|#########6| cHPI SNRs : 478/496 [00:05<00:00,  227.91it/s]
 99%|#########8| cHPI SNRs : 491/496 [00:05<00:00,  237.98it/s]
100%|##########| cHPI SNRs : 496/496 [00:05<00:00,   95.70it/s]

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

Estimated memory usage: 109 MB

Gallery generated by Sphinx-Gallery