# Passing head position to maxwell filter

**URL:** <https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374>\
**Category:** Support & Discussions\
**Created:** [August 4, 2022, 9:04am UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374 "2022-08-04T09:04:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raphaelle](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@Raphaelle](https://mne.discourse.group/u/Raphaelle)\
**Post date:** [August 4, 2022, 9:04am UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/1 "2022-08-04T09:04:58Z")

</div>

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 :

```python

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 !

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [August 4, 2022, 1:50pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/2 "2022-08-04T13:50:25Z")

</div>

Hello @Raphaelle, and welcome to the forum!

> [@Raphaelle](#):
>
> ```python
> head_position=input_raw.info['dev_head_t']
> 
> ```

`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()`](https://mne.tools/stable/generated/mne.chpi.read_head_pos.html) function.

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

[https://mne.tools/stable/auto\_tutorials/preprocessing/59\_head\_positions.html](https://mne.tools/stable/auto_tutorials/preprocessing/59_head_positions.html)

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![Raphaelle](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@Raphaelle](https://mne.discourse.group/u/Raphaelle)\
**Post date:** [August 5, 2022, 3:19pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/3 "2022-08-05T15:19:34Z")

</div>

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 :

```python
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):

```python
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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [August 5, 2022, 4:15pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/4 "2022-08-05T16:15:09Z")

</div>

> [@Raphaelle](#):
>
> it stays at ‘0%’ step for several minutes, then it stops

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 🤔

---

<div class="post-metadata">

**Author:** ![Raphaelle](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@Raphaelle](https://mne.discourse.group/u/Raphaelle)\
**Post date:** [August 5, 2022, 5:00pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/5 "2022-08-05T17:00:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [August 14, 2022, 2:04pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/6 "2022-08-14T14:04:59Z")

</div>

Can you share the data you’re having this issue with?

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/pascal/32/2054_2.png) [@pascal](https://mne.discourse.group/u/pascal)\
**Post date:** [November 3, 2022, 1:56pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/7 "2022-11-03T13:56:33Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [November 3, 2022, 3:31pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/8 "2022-11-03T15:31:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/pascal/32/2054_2.png) [@pascal](https://mne.discourse.group/u/pascal)\
**Post date:** [November 3, 2022, 5:57pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/9 "2022-11-03T17:57:24Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [November 8, 2022, 4:29pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/10 "2022-11-08T16:29:11Z")

</div>

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](https://github.com/mne-tools/mne-python/pull/11308/)

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?

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/pascal/32/2054_2.png) [@pascal](https://mne.discourse.group/u/pascal)\
**Post date:** [November 9, 2022, 9:56am UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/11 "2022-11-09T09:56:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [November 9, 2022, 3:43pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/12 "2022-11-09T15:43:45Z")

</div>

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

 ![Screenshot_2022-11-09_09-36-27](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/e/e5da8519a9922f3c9d161eea6461caab5decc5b2.png)

and here’s one from a notebook running in JupyterLab:

 ![Screenshot 2022-11-09 at 09-40-03 Untitled.ipynb - JupyterLab](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/a/a601140e1828f1301202500a26b0213762cbf49e.png)

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

```python
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)

---

<div class="post-metadata">

**Author:** ![pascal](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/pascal/32/2054_2.png) [@pascal](https://mne.discourse.group/u/pascal)\
**Post date:** [November 10, 2022, 8:55am UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/13 "2022-11-10T08:55:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [November 10, 2022, 2:52pm UTC](https://mne.discourse.group/t/passing-head-position-to-maxwell-filter/5374/14 "2022-11-10T14:52:04Z")

</div>

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.
