# Maxfilter error: ill conditioned matrix

**URL:** <https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [April 18, 2018, 1:32pm UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491 "2018-04-18T13:32:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 18, 2018, 1:32pm UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/1 "2018-04-18T13:32:09Z")

</div>

Dear all,  
I am using maxfilter from within mne\_python, and I am getting the following  
error for several participants:  
Matrix is badly conditioned: 22221 \>= 1000  
Head position change is over 25mm

At first I thought it was because the HPI wasn't fit properly (I had one  
participant for whom 2/4 coils had a too large distance), but I am also  
getting the matrix-badly-condition error with other participants for whom  
the HPI was fine.

I know that I can set the function to give a warning instead of an error,  
but I still would like to understand what happens.  
Do you have any recommendation on how to deal with this?

Thank you,  
Sophie  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180418/dae5c7be/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180418/dae5c7be/attachment.html)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 18, 2018, 4:44pm UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/2 "2018-04-18T16:44:00Z")

</div>

Hey Sophie,

What is the head position change/distance (`destination` and the  
`info['dev\_head\_t']['trans'][:3, 3])?

Are you using regularization? Time-varying head positions (head\_pos is not  
None)?

The "bad condition" suggests that something might be amiss, and if nothing  
is, that the resulting data reconstruction could amplify noise. Usually  
using regularization prevents this problem, though.

Eric

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 19, 2018, 7:26am UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/3 "2018-04-19T07:26:08Z")

</div>

Hallo, dear MNE-Python team,

recently, I realized that the preload option in mne.io.read\_raw\_fif offers more than I expected. Below is the line of code, which provides the expected bahaviour, i.e. it opens the rawdata fif file and preloads the data into RAM:  
rawdata = mne.io.read\_raw\_fif(name\_of\_rawdata\_fif,preload=True)

Changing the type of the option value from boolean into string does not throw an error message, but instead it creates a file called True within the current working directory. This is obsiously no preloading into RAM, isn't it? Or does this file work as a buffer? Is this behavior intended or a bug?  
rawdata = mne.io.read\_raw\_fif(name\_of\_rawdata\_fif,preload='True')

Interestingly, the loglevel DEBUG does not tell about creating the funny file True.

cheers,  
Burkhard

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 19, 2018, 7:50am UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/4 "2018-04-19T07:50:06Z")

</div>

Hi Burkhard!

This behavior is documented in the docstring of mne.io.read\_raw\_fif:

preload : bool or str (default False)  
&nbsp;&nbsp;&nbsp;&nbsp;Preload data into memory for data manipulation and faster indexing.  
&nbsp;&nbsp;&nbsp;&nbsp;If True, the data will be preloaded into memory (fast, requires  
&nbsp;&nbsp;&nbsp;&nbsp;large amount of memory). If preload is a string, preload is the  
&nbsp;&nbsp;&nbsp;&nbsp;file name of a memory-mapped file which is used to store the data  
&nbsp;&nbsp;&nbsp;&nbsp;on the hard drive (slower, requires less memory).

Clemens

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 20, 2018, 7:54am UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/5 "2018-04-20T07:54:42Z")

</div>

External Email - Use Caution

Dear Eric,  
it seems like we have a problem with one HPI coil-set.  
The weird thing is that during the recording, when I measured the HPI  
before each run, it claims that 3/4 coils are fine.

For maxfilter, head\_pos is None, but I am using another run as reference:  
destination =  
\<Transform | MEG device-\>head\>  
[[0.71960711 0.63715118 -0.27605024 -0.07788484]  
[-0.67182261 0.73935592 -0.04479922 -0.01268383]  
[0.17555553 0.21769466 0.96009851 0.06360817]  
[0. 0. 0. 1.]]

Here is my call to maxfilter:  
sss = maxwell\_filter(imp,verbose=False, destination=destination,  
bad\_condition='error',st\_duration = 30,  
calibration=cal\_path, cross\_talk=ct\_path)

info['dev\_head\_t']['trans'][:3, 3] for imp (my raw data) is:  
array([-0.07870771, -0.01272191, 0.06554133]).

I realized that the outcome of info['dev\_head\_t'] differs strongly between  
a run for which maxfilter runs fine, and one for which I get the error, all  
from the same participant and recording.

Run 1 with the error:  
info['dev\_head\_t']:  
\<Transform | MEG device-\>head\>  
[[0.71646905 0.64524961 -0.26518956 -0.07870771]  
[-0.67314613 0.73924029 -0.0199627 -0.01272191]  
[0.18315783 0.19281393 0.96398985 0.06554133]  
[0. 0. 0. 1.]]

Run 3 with no error:  
Out[76]:  
\<Transform | MEG device-\>head\>  
[[0.17830184 -0.8871333 0.42567968 0.04394912]  
[0.86059254 0.35034028 0.36965176 -0.01058153]  
[-0.47706306 0.30042711 0.82592654 0.06920396]  
[0. 0. 0. 1.]]

Do you have any idea where the problem could come from?  
Thank you!  
Sophie

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 20, 2018, 4:30pm UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/6 "2018-04-20T16:30:05Z")

</div>

External Email - Use Caution

> destination =  
> \<Transform | MEG device-\>head\>  
> [[0.71960711 0.63715118 -0.27605024 -0.07788484]  
> [-0.67182261 0.73935592 -0.04479922 -0.01268383]  
> [0.17555553 0.21769466 0.96009851 0.06360817]  
> [0. 0. 0. 1.]]

This appears to have a large rotation and translation. To confirm, we can  
take a look at what this transformation would be like for the "sample"  
subject:

import mne  
data\_path = mne.datasets.sample.data\_path()  
raw = mne.io.read\_raw\_fif(data\_path +  
'/MEG/sample/sample\_audvis\_raw.fif')raw.info['dev\_head\_t']['trans'] =  
np.array(  
&nbsp;&nbsp;&nbsp;&nbsp;[[0.71960711, 0.63715118, -0.27605024, -0.07788484],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-0.67182261, 0.73935592, -0.04479922, -0.01268383],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0.17555553, 0.21769466, 0.96009851, 0.06360817],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0., 0., 0., 1.]])  
trans = mne.read\_trans(data\_path + '/MEG/sample/sample\_audvis\_raw-trans.fif')  
mne.viz.plot\_alignment(raw.info, trans=trans, subject='sample',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subjects\_dir=data\_path + '/subjects',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coord\_frame='meg', dig=True)

?  
This view from the front of the MEG helmet looks like:

I recommend adapting this script to use your own data, as it will show the  
correct digitization (and correct head if you have an MRI, if not, omit the  
subject\* and trans parameters, and set surfaces= in the plot\_alignment  
call). Was this roughly the orientation and position of the subject during  
acquisition? If not, your dev\_head\_t is probably incorrect.

The actual origin used by Maxfilter will also depend on what you pass as  
the "origin" parameter. But in any case, it appears that it will be quite  
far from the device origin, and quite close to one side of the helmet (or  
maybe outside it). This is probably causing the conditioning problems.  
Assuming the dev\_head\_t is indeed correct, you should inspect the resulting  
transform to see if it has amplified some of the noise, which (I think) is  
a potential risk when the condition number gets too large.

Eric  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180420/51cce404/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180420/51cce404/attachment-0001.html)  
-------------- next part --------------  
A non-text attachment was scrubbed...  
Name: snapshot.png  
Type: image/png  
Size: 98236 bytes  
Desc: not available  
Url : [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180420/51cce404/attachment-0001.png](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180420/51cce404/attachment-0001.png)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 23, 2018, 9:02am UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/7 "2018-04-23T09:02:28Z")

</div>

External Email - Use Caution

Dear Eric,  
thank you for the code snippet! I adapted it to my data and indeed, 3  
participants (for whom I get the maxfilter error) come out of the recording  
with a completely distorted head position for all or some runs (see pic). I  
know I positioned them correctly and the HPI that I measure at the  
beginning of a run was ok (3/4 coils fine).  
So I am afraid, the dev\_head\_t is off.  
Do you have any idea of how that could have happend? Is there anything I  
could do to fix this?  
I already tried hpifit from the elekta tools, but re-ordering the coils  
does not solve it.  
Thank you,  
Sophie  
[image: Folie1.png]

> From: Eric Larson \<larson.eric.d at gmail.com\>  
> Date: Fr., 20. Apr. 2018 um 18:30 Uhr  
> Subject: Re: [Mne\_analysis] Maxfilter error: ill conditioned matrix  
> To: Discussion and support forum for the users of MNE Software \<  
> mne\_analysis at nmr.mgh.harvard.edu\>
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;External Email - Use Caution
> 
> destination =
> 
> > \<Transform | MEG device-\>head\>  
> > [[0.71960711 0.63715118 -0.27605024 -0.07788484]  
> > [-0.67182261 0.73935592 -0.04479922 -0.01268383]  
> > [0.17555553 0.21769466 0.96009851 0.06360817]  
> > [0. 0. 0. 1.]]
> 
> This appears to have a large rotation and translation. To confirm, we can  
> take a look at what this transformation would be like for the "sample"  
> subject:
> 
> import mne  
> data\_path = mne.datasets.sample.data\_path()  
> raw = mne.io.read\_raw\_fif(data\_path + '/MEG/sample/sample\_audvis\_raw.fif')raw.info['dev\_head\_t']['trans'] = np.array(  
> &nbsp;&nbsp;&nbsp;&nbsp;[[0.71960711, 0.63715118, -0.27605024, -0.07788484],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-0.67182261, 0.73935592, -0.04479922, -0.01268383],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0.17555553, 0.21769466, 0.96009851, 0.06360817],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0., 0., 0., 1.]])  
> trans = mne.read\_trans(data\_path + '/MEG/sample/sample\_audvis\_raw-trans.fif')  
> mne.viz.plot\_alignment(raw.info, trans=trans, subject='sample',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subjects\_dir=data\_path + '/subjects',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coord\_frame='meg', dig=True)
> 
> ?  
> This view from the front of the MEG helmet looks like:
> 
> [image: snapshot.png]
> 
> I recommend adapting this script to use your own data, as it will show the  
> correct digitization (and correct head if you have an MRI, if not, omit the  
> subject\* and trans parameters, and set surfaces= in the plot\_alignment  
> call). Was this roughly the orientation and position of the subject during  
> acquisition? If not, your dev\_head\_t is probably incorrect.
> 
> The actual origin used by Maxfilter will also depend on what you pass as  
> the "origin" parameter. But in any case, it appears that it will be quite  
> far from the device origin, and quite close to one side of the helmet (or  
> maybe outside it). This is probably causing the conditioning problems.  
> Assuming the dev\_head\_t is indeed correct, you should inspect the resulting  
> transform to see if it has amplified some of the noise, which (I think) is  
> a potential risk when the condition number gets too large.
> 
> Eric
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mne\_analysis mailing list  
> Mne\_analysis at nmr.mgh.harvard.edu  
> [Mne\_analysis Info Page](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)
> 
> The information in this e-mail is intended only for the person to whom it  
> is  
> addressed. If you believe this e-mail was sent to you in error and the  
> e-mail  
> contains patient information, please contact the Partners Compliance  
> HelpLine at  
> [MyComplianceReport.com: Compliance and Ethics Reporting](http://www.partners.org/complianceline) . If the e-mail was sent to you in  
> error  
> but does not contain patient information, please contact the sender and  
> properly  
> dispose of the e-mail.

-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180423/0976c9d5/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180423/0976c9d5/attachment-0001.html)  
-------------- next part --------------  
A non-text attachment was scrubbed...  
Name: snapshot.png  
Type: image/png  
Size: 98236 bytes  
Desc: not available  
Url : [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180423/0976c9d5/attachment-0002.png](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180423/0976c9d5/attachment-0002.png)  
-------------- next part --------------  
A non-text attachment was scrubbed...  
Name: Folie1.png  
Type: image/png  
Size: 106556 bytes  
Desc: not available  
Url : [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180423/0976c9d5/attachment-0003.png](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180423/0976c9d5/attachment-0003.png)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [April 23, 2018, 2:22pm UTC](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491/8 "2018-04-23T14:22:27Z")

</div>

External Email - Use Caution

> 3 participants (for whom I get the maxfilter error) come out of the  
> recording with a completely distorted head position for all or some runs  
> (see pic).

The picture did not come through for me but I can imagine well enough a bad  
head position 🙂

Do you have any idea of how that could have happend? Is there anything I

> could do to fix this?

Every once in a while I see a head position like this that doesn't make any  
sense. Did you record continuous head position? If so, you could try  
estimating the head position from the first few seconds, and swap this in  
for the dev\_head\_t.

If you didn't have cHPI on, then there might be some way to do it (some  
information is stored in the measurement info), but I haven't had to do it  
/ thought about how. You could try contacting Elekta about it to see if  
they have any other ideas/tips.

Eric  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180423/cb5be467/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180423/cb5be467/attachment.html)
