Passing head position to maxwell filter

Hi everyone,

I am having trouble passing head position to maxwell filter function.
I need it to concatenate data from each runs of my experience.
Here is the code I used :


noisy_chs, flat_chs = mne.preprocessing.find_bad_channels_maxwell(raw)
raw.info['bads'] = noisy_chs + flat_chs 
raw.info['bads'] += bad_channels  # bad channels from observation
head_position=input_raw.info['dev_head_t']
raw_pretreated = mne.preprocessing.maxwell_filter(raw, origin=(0., 0., 0.04), calibration=cal_fname, cross_talk=ct_fname, head_pos = head_position )

Error message : TypeError: head_pos must be an instance of ndarray or None, got <class 'mne.transforms.Transform'> instead.

The problem seems to be that raw.info[ā€˜dev_head_tā€™] is not an array, but I couldnā€™t find an appropriate method to convert it. Or maybe I am not using the maxwell filter function correctly?

Thank you in advance for your help !

Hello @Raphaelle, and welcome to the forum!

info['dev_head_t'] is the device-to-head coordinate system transformation matrix, not the head positions.

Typically, head positions are loaded e.g. via the read_head_pos() function.

But they can also be computed, please see this tutorial:

https://mne.tools/stable/auto_tutorials/preprocessing/59_head_positions.html

Best wishes,
Richard

Hi Richard, thank you for your answer.

I tried both methods, but couldnā€™t do the first one (read_head_pos() function) because I donā€™t have any separated file ending with .pos.
I tried to compute with the other method :

chpi_amplitudes = mne.chpi.compute_chpi_amplitudes(raw)
chpi_locs = mne.chpi.compute_chpi_locs(raw.info, chpi_amplitudes)
head_position = mne.chpi.compute_head_pos(raw.info, chpi_locs, verbose=True)

but the first function doesnā€™t seem to work (it stays at ā€˜0%ā€™ step for several minutes, then it stops):

Using 4 HPI coils: 293 307 314 321 Hz
Line interference frequencies: 50 100 150 200 250 300 Hz
Using time window: 142.9 ms
Fitting 4 HPI coil locations at up to 75293 time points (753.0 sec duration)
  0%|          | cHPI amplitudes : 0/75293 [00:00<?,       ?it/s]

At the end of the script, the variable ā€œhead_positionā€ is an empty array.

Do you have an idea of what is going wrong ?
(I precise we did use HPI during the acquisition)

Thanks in advance,

Bests,

Raphaƫlle

Which version of MNE is that? I think I read about a similar issue not too long ago and @larsoner fixed it in MNE 1.1, but I might be totally mistaken :thinking:

I have the last version (MNE 1.1), and I work with a mac.

Can you share the data youā€™re having this issue with?

Has this issue been solved?
I am encoutering a similar issue as described by Raphaelle, chpi_amplitudes does not seem to produce a result for me. Is there any suggestion on how to handle this?

Linux platform, MNE version 1.2.0

The console stays at the following input until I reset the kernel:

Using 5 HPI coils: 293 307 314 321 328 Hz
Line interference frequencies: 50 100 150 200 250 300 Hz
Using time window: 142.9 ms
Fitting 5 HPI coil locations at up to 30693 time points (307.0 sec duration)
0%| | cHPI amplitudes : 0/30693 [00:00<?, ?it/s]

In order to fix it, we need to be able to reproduce it, and to do that we need a (preferably small) data file and a minimal working code sample that reproduces the problem. @Raphaelle never responded to @richard 's request for the data file, so no progress has been made AFAIK. @pascal can you provide a file / MWE for which this problem arises?

I spotted my mistake - the data had not been loaded with raw.load_data().
When doing so, the commands work well and I get to the visualization of the head movement. I might not be the only one forgetting to do so, hope it will be of help.
Thank you for your help anyways @drammock !

1 Like

update: @larsoner added this to our test suite. MAINT: Add test for preload during cHPI by larsoner Ā· Pull Request #11308 Ā· mne-tools/mne-python Ā· GitHub

It seems like itā€™s not actually a problem with preloading; it might (?) be a problem with the progress bar itself, and if you waited long enough it would actually work.@pascal how long did you wait before you reset the kernel? How long does it take when you do preload the data and it works?

I waited a good 10 minutes I would say. The data is 307 seconds long. I also cropped it to 10 or 15 seconds (donā€™t remember exactly) and waited again for around 10 minutes without a result.
When preloading (the original 307 seconds data set), it takes around 30 seconds to compute the chpi amplitudes.
However, the progress bar does not work in the meantime. The only console message I get is the one I posted before, and after 30 seconds, the computation is completed, without any notification.

hmm, I cannot reproduce. Hereā€™s a screenshot from an IPython terminal inside VSCode:

and hereā€™s one from a notebook running in JupyterLab:

In both cases the progress bar is working normally. The code run prior to the call to compute_chpi_amplitudes was this:

import mne
raw_file = '/path/to/my/raw.fif'
raw = mne.io.read_raw_fif(raw_file, verbose=False, preload=False, allow_maxshield=True)
raw.crop(tmax=300)

(you can see the preload=False in there)

Looks good. One thing I thought about: Could it be an issue that I am using Spyder as IDE?

quite possibly. We seem to often get reports of things working fine in terminal or vscode that donā€™t work properly in Spyder or PyCharm.