Maxfilter error: ill conditioned matrix

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

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

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

Hi Burkhard!

This behavior is documented in the docstring of mne.io.read_raw_fif:

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

Clemens

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

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(
    [[ 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. ]])
trans = mne.read_trans(data_path + '/MEG/sample/sample_audvis_raw-trans.fif')
mne.viz.plot_alignment(raw.info, trans=trans, subject='sample',
                       subjects_dir=data_path + '/subjects',
                       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
-------------- 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

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>

        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(
    [[ 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. ]])
trans = mne.read_trans(data_path + '/MEG/sample/sample_audvis_raw-trans.fif')
mne.viz.plot_alignment(raw.info, trans=trans, subject='sample',
                       subjects_dir=data_path + '/subjects',
                       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

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 . 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
-------------- 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
-------------- 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

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 :slight_smile:

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