# RuntimeError: Matrix is badly conditioned

**URL:** <https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177>\
**Category:** Support & Discussions\
**Tags:** preprocessing\
**Created:** [August 7, 2024, 7:54pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177 "2024-08-07T19:54:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschendel](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@mschendel](https://mne.discourse.group/u/mschendel)\
**Post date:** [August 7, 2024, 7:54pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/1 "2024-08-07T19:54:34Z")

</div>

Hello,  
I am able to run the prepare\_emptyroom, but when I try the maxfilter command on the prepared data, I get this error. I tried a set of 7 subject data files and 7 empty room files, and each created the error.

- MNE version: 1.7.0
- operating system: Ubuntu 22.04.4 LTS

Here is some of the data I’m working with:  
empty\_ready is the empty room raw file that went through

```python
In [9]: empty_ready = mne.preprocessing.maxwell_filter_prepare_emptyroom(empty_raw, raw =raw, bads ='union' , verbose=True)

In [10]: empty_ready.info
Out[10]:
<Info | 22 non-empty values
 acq_pars: ACQch001 110113 ACQch002 110112 ACQch003 110111 ACQch004 110122 ...
 acq_stim: 1 10.000000 1000.000000
 bads: 2 items (MEG0443, MEG1622)
 ch_names: MEG0113, MEG0112, MEG0111, MEG0122, MEG0123, MEG0121, MEG0132, ...
 chs: 204 Gradiometers, 102 Magnetometers, 4 Stimulus
 custom_ref_applied: False
 description: Vectorview system
 dev_head_t: MEG device -> head transform
 dig: 135 items (3 Cardinal, 4 HPI, 128 Extra)
 events: 1 item (list)
 experimenter: MEG (meg)
 highpass: 0.1 Hz
 hpi_subsystem: 3 items (dict)
 line_freq: 60.0
 lowpass: 330.0 Hz
 meas_date: 2024-01-10 19:25:58 UTC
 meas_id: 4 items (dict)
 nchan: 310
 proj_id: 1 item (ndarray)
 proj_name: test
 projs: mag_ssp_upright.fif : PCA-v1: off, mag_ssp_upright.fif : PCA-v2: ...
 sfreq: 1000.0 Hz
 subject_info: 6 items (dict)
>
In [11]: empty_proc = maxwell_filter(empty_ready,origin = (0,0,40), int_order=8, ext_order=3, calibration=cal_data, cross_talk= ct_data, st_duration =6, st_correlation=0.
   ...: 95 , coord_frame = 'head', head_pos=head_pos)
   ...:
Maxwell filtering raw data
102 of 102 magnetometer types replaced with T3.
    Bad MEG channels being reconstructed: ['MEG0443', 'MEG1622']
    Processing 204 gradiometers and 102 magnetometers
    Using fine calibration sss_cal_3046_2009-04-29.dat
        Adjusting non-orthogonal EX and EY
        Adjusted coil positions by (μ ± σ): 0.5° ± 0.4° (max: 2.4°)
    Using origin 0.0, 0.0, 40000.0 mm in the head frame
    Processing data using tSSS with st_duration=6.0
/export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:2349: RuntimeWarning: invalid value encountered in scalar divide
  f"bits/sample ({100 * I_tots[lim_idx] / max_info:0.1f}% of peak "
        Using 95/95 harmonic components for 0.000 (80/80 in, 15/15 out)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[9], line 1
----> 1 empty_proc = maxwell_filter(empty_ready,origin = (0,0,40), int_order=8, ext_order=3, calibration=cal_data, cross_talk= ct_data, st_duration =6, st_correlation=0.95 , coord_frame = 'head', head_pos=head_pos)

File <decorator-gen-358>:12, in maxwell_filter(raw, origin, int_order, ext_order, calibration, cross_talk, st_duration, st_correlation, coord_frame, destination, regularize, ignore_ref, bad_condition, head_pos, st_fixed, st_only, mag_scale, skip_by_annotation, extended_proj, verbose)

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:406, in maxwell_filter(raw, origin, int_order, ext_order, calibration, cross_talk, st_duration, st_correlation, coord_frame, destination, regularize, ignore_ref, bad_condition, head_pos, st_fixed, st_only, mag_scale, skip_by_annotation, extended_proj, verbose)
    384 logger.info("Maxwell filtering raw data")
    385 params = _prep_maxwell_filter(
    386 raw=raw,
    387 origin=origin,
   (...)
    404 extended_proj=extended_proj,
    405 )
--> 406 raw_sss = _run_maxwell_filter(raw, **params)
    407 # Update info
    408 _update_sss_info(raw_sss, **params["update_kwargs"])

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:688, in _run_maxwell_filter(raw, skip_by_annotation, st_duration, st_correlation, st_only, st_when, ctc, coil_scale, this_pos_quat, meg_picks, good_mask, grad_picks, head_pos, info, _get_this_decomp_trans, S_recon, update_kwargs, ignore_ref, reconstruct, copy)
    660 def _run_maxwell_filter(
    661 raw,
    662 skip_by_annotation,
   (...)
    686 # The time it takes to recompute S and pS themselves is roughly on par
    687 # with the np.dot with the data, so not a huge gain to be made there.
--> 688 S_decomp, S_decomp_full, pS_decomp, reg_moments, n_use_in = _get_this_decomp_trans(
    689 info["dev_head_t"], t=0.0
    690 )
    691 update_kwargs.update(reg_moments=reg_moments.copy())
    692 if ctc is not None:

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:1259, in _get_decomp(trans, all_coils, cal, regularize, exp, ignore_ref, coil_scale, grad_picks, mag_picks, good_mask, mag_or_fine, bad_condition, t, mag_scale, mult)
   1257 msg = "Matrix is badly conditioned: %0.0f >= 1000" % cond
   1258 if bad_condition == "error":
-> 1259 raise RuntimeError(msg)
   1260 elif bad_condition == "warning":
   1261 warn(msg)

RuntimeError: Matrix is badly conditioned: 18132836229591732 >= 1000

```

I found the [previous thread](https://mne.discourse.group/t/maxfilter-error-ill-conditioned-matrix/1491) about this error, which seemed to indicate issues with head position measurement.  
I checked the head position from the participant data:  
 ![M87104027_head_pos](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/3/333327fa967d9a1da280cef804f910fd3835c084.jpeg)

Is there anything else I can check to troubleshoot this? Any other info I can provide? Does this head position include too large a change?

Thanks for your help,  
Megan

---

<div class="post-metadata">

**Author:** ![mschendel](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@mschendel](https://mne.discourse.group/u/mschendel)\
**Post date:** [August 23, 2024, 4:14pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/2 "2024-08-23T16:14:37Z")

</div>

Hello,  
Just wanted to try to see if anyone can help troubleshoot this issue. I will try to attend the office hours next week.  
Here is another example, where the head position looks pretty fine to me from the original run:

```python
mne.viz.plot_head_positions(head_pos, mode="traces")

```

![M87104027_visit2_run1_headpos](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/a/a054874947fdadb48e9aaa62d5bf4fe46d327d5b.jpeg)

```python

In [16]: empty_proc = maxwell_filter(empty_ready,origin = (0,0,40), int_order=8, ext_order=3, calibration=cal
    ...: _data, cross_talk= ct_data, st_duration =6, st_correlation=0.95 , coord_frame = 'head', head_pos=head
    ...: _pos)
    ...:
Maxwell filtering raw data
102 of 102 magnetometer types replaced with T3.
    Bad MEG channels being reconstructed: ['MEG0443']
    Processing 204 gradiometers and 102 magnetometers
    Using fine calibration sss_cal_3046_2009-04-29.dat
        Adjusting non-orthogonal EX and EY
        Adjusted coil positions by (μ ± σ): 0.5° ± 0.4° (max: 2.4°)
    Using origin 0.0, 0.0, 40000.0 mm in the head frame
    Processing data using tSSS with st_duration=6.0
/export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:2349: RuntimeWarning: invalid value encountered in scalar divide
  f"bits/sample ({100 * I_tots[lim_idx] / max_info:0.1f}% of peak "
        Using 95/95 harmonic components for 0.000 (80/80 in, 15/15 out)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[16], line 1
----> 1 empty_proc = maxwell_filter(empty_ready,origin = (0,0,40), int_order=8, ext_order=3, calibration=cal_data, cross_talk= ct_data, st_duration =6, st_correlation=0.95 , coord_frame = 'head', head_pos=head_pos)

File <decorator-gen-358>:12, in maxwell_filter(raw, origin, int_order, ext_order, calibration, cross_talk, st_duration, st_correlation, coord_frame, destination, regularize, ignore_ref, bad_condition, head_pos, st_fixed, st_only, mag_scale, skip_by_annotation, extended_proj, verbose)

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:406, in maxwell_filter(raw, origin, int_order, ext_order, calibration, cross_talk, st_duration, st_correlation, coord_frame, destination, regularize, ignore_ref, bad_condition, head_pos, st_fixed, st_only, mag_scale, skip_by_annotation, extended_proj, verbose)
    384 logger.info("Maxwell filtering raw data")
    385 params = _prep_maxwell_filter(
    386 raw=raw,
    387 origin=origin,
   (...)
    404 extended_proj=extended_proj,
    405 )
--> 406 raw_sss = _run_maxwell_filter(raw, **params)
    407 # Update info
    408 _update_sss_info(raw_sss, **params["update_kwargs"])

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:688, in _run_maxwell_filter(raw, skip_by_annotation, st_duration, st_correlation, st_only, st_when, ctc, coil_scale, this_pos_quat, meg_picks, good_mask, grad_picks, head_pos, info, _get_this_decomp_trans, S_recon, update_kwargs, ignore_ref, reconstruct, copy)
    660 def _run_maxwell_filter(
    661 raw,
    662 skip_by_annotation,
   (...)
    686 # The time it takes to recompute S and pS themselves is roughly on par
    687 # with the np.dot with the data, so not a huge gain to be made there.
--> 688 S_decomp, S_decomp_full, pS_decomp, reg_moments, n_use_in = _get_this_decomp_trans(
    689 info["dev_head_t"], t=0.0
    690 )
    691 update_kwargs.update(reg_moments=reg_moments.copy())
    692 if ctc is not None:

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:1259, in _get_decomp(trans, all_coils, cal, regularize, exp, ignore_ref, coil_scale, grad_picks, mag_picks, good_mask, mag_or_fine, bad_condition, t, mag_scale, mult)
   1257 msg = "Matrix is badly conditioned: %0.0f >= 1000" % cond
   1258 if bad_condition == "error":
-> 1259 raise RuntimeError(msg)
   1260 elif bad_condition == "warning":
   1261 warn(msg)

RuntimeError: Matrix is badly conditioned: 17743322965512802 >= 1000

```

Please let me know if I can provide any additional info.  
Thanks,  
Megan

---

<div class="post-metadata">

**Author:** ![mschendel](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@mschendel](https://mne.discourse.group/u/mschendel)\
**Post date:** [August 23, 2024, 6:19pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/3 "2024-08-23T18:19:15Z")

</div>

Some additional info: if I try to replicate my participant processing as I did using MEGIN’s command line maxfilter, I get the same error. So I’m just not using maxwell\_filter correctly, it seems.

My original maxfilter command:

```python
/neuro/bin/util/maxfilter-2.2 -v -force -autobad 60 -badlimit 20 -movecomp inter -st 6 -corr 0.95 -f M87104027+Study20240405+avmmn_visit2_run1_raw.fif -o /M87104027_avmmn_visit2_run1_tsss_mc.fif -origin 0 0 40 -frame head -format short -hp M87104027_avmmn_visit2_run1_tsss_mc_headpos.asc 

```

What I tried in mne (I used the asc file created from my command line maxfilter-2.2 run for the head\_pos):

```python
In [12]: raw_proc = maxwell_filter(raw,origin = (0,0,40), int_order=8, ext_order=3, calibration=cal_data, cros
    ...: s_talk= ct_data, st_duration =6, st_correlation=0.95 , coord_frame = 'head', head_pos=head_pos)
Maxwell filtering raw data
102 of 102 magnetometer types replaced with T3.
    No bad MEG channels
    Processing 204 gradiometers and 102 magnetometers
    Using fine calibration sss_cal_3046_2009-04-29.dat
        Adjusting non-orthogonal EX and EY
        Adjusted coil positions by (μ ± σ): 0.5° ± 0.4° (max: 2.4°)
    Using origin 0.0, 0.0, 40000.0 mm in the head frame
    Processing data using tSSS with st_duration=6.0
/export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:2349: RuntimeWarning: invalid value encountered in scalar divide
  f"bits/sample ({100 * I_tots[lim_idx] / max_info:0.1f}% of peak "
        Using 95/95 harmonic components for 0.000 (80/80 in, 15/15 out)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[12], line 1
----> 1 raw_proc = maxwell_filter(raw,origin = (0,0,40), int_order=8, ext_order=3, calibration=cal_data, cross_talk= ct_data, st_duration =6, st_correlation=0.95 , coord_frame = 'head', head_pos=head_pos)

File <decorator-gen-358>:12, in maxwell_filter(raw, origin, int_order, ext_order, calibration, cross_talk, st_duration, st_correlation, coord_frame, destination, regularize, ignore_ref, bad_condition, head_pos, st_fixed, st_only, mag_scale, skip_by_annotation, extended_proj, verbose)

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:406, in maxwell_filter(raw, origin, int_order, ext_order, calibration, cross_talk, st_duration, st_correlation, coord_frame, destination, regularize, ignore_ref, bad_condition, head_pos, st_fixed, st_only, mag_scale, skip_by_annotation, extended_proj, verbose)
    384 logger.info("Maxwell filtering raw data")
    385 params = _prep_maxwell_filter(
    386 raw=raw,
    387 origin=origin,
   (...)
    404 extended_proj=extended_proj,
    405 )
--> 406 raw_sss = _run_maxwell_filter(raw, **params)
    407 # Update info
    408 _update_sss_info(raw_sss, **params["update_kwargs"])

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:688, in _run_maxwell_filter(raw, skip_by_annotation, st_duration, st_correlation, st_only, st_when, ctc, coil_scale, this_pos_quat, meg_picks, good_mask, grad_picks, head_pos, info, _get_this_decomp_trans, S_recon, update_kwargs, ignore_ref, reconstruct, copy)
    660 def _run_maxwell_filter(
    661 raw,
    662 skip_by_annotation,
   (...)
    686 # The time it takes to recompute S and pS themselves is roughly on par
    687 # with the np.dot with the data, so not a huge gain to be made there.
--> 688 S_decomp, S_decomp_full, pS_decomp, reg_moments, n_use_in = _get_this_decomp_trans(
    689 info["dev_head_t"], t=0.0
    690 )
    691 update_kwargs.update(reg_moments=reg_moments.copy())
    692 if ctc is not None:

File /export/research/analysis/human/jstephen/shared/programs/python/anaconda_072020/new/envs/mne1.7/lib/python3.12/site-packages/mne/preprocessing/maxwell.py:1259, in _get_decomp(trans, all_coils, cal, regularize, exp, ignore_ref, coil_scale, grad_picks, mag_picks, good_mask, mag_or_fine, bad_condition, t, mag_scale, mult)
   1257 msg = "Matrix is badly conditioned: %0.0f >= 1000" % cond
   1258 if bad_condition == "error":
-> 1259 raise RuntimeError(msg)
   1260 elif bad_condition == "warning":
   1261 warn(msg)

RuntimeError: Matrix is badly conditioned: 15210861592565222 >= 1000

```

Thanks for any eyes on this,  
Megan

---

<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 23, 2024, 9:40pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/4 "2024-08-23T21:40:41Z")

</div>

@larsoner Would you have any idea what’s going on here?

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [August 26, 2024, 4:18am UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/5 "2024-08-26T04:18:50Z")

</div>

Typically this is triggered by implausible (or very far out of helmet) head positions. But your position estimates look reasonable.

I think it’s `origin = (0,0,40)`. Keep in mind the units are SI meters in MNE, not millimeters like in `maxfilter`. Can you see if `origin=(0., 0., 0.04)` fixes the problem?

---

<div class="post-metadata">

**Author:** ![mschendel](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@mschendel](https://mne.discourse.group/u/mschendel)\
**Post date:** [August 26, 2024, 3:27pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/6 "2024-08-26T15:27:12Z")

</div>

Hi all,  
YES. Thank you for noticing that.  
I read [preprocessing doc](https://mne.tools/stable/generated/mne.preprocessing.maxwell_filter.html) , but I didn’t see anything about meters, there. It would be great to add it to info about the origin parameter. I also don’t see anything in the [tutorial article.](https://mne.tools/stable/auto_tutorials/preprocessing/60_maxwell_filtering_sss.html#sphx-glr-auto-tutorials-preprocessing-60-maxwell-filtering-sss-py)

If I look closely in the output, using origin = (0,0,0.04), I see

```python
    Using origin 0.0, 0.0, 40.0 mm in the head frame

```

and if I use origin (0,0,40), I see:

```python
Using origin 0.0, 0.0, 40000.0 mm in the head frame

```

…too bad I didn’t notice that sooner.

Anyway. That’s sorted. Thanks for your help!  
Megan

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 27, 2024, 8:19am UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/7 "2024-08-27T08:19:18Z")

</div>

The documentation you link states for the `origin` argument:

> origin : array\_like, shape (3,) | str  
> Origin of internal and external multipolar moment space in meters. […]

There is nothing to change here.

Mathieu

---

<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 27, 2024, 11:50am UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/8 "2024-08-27T11:50:27Z")

</div>

Maybe there should be a warning if the values are unusually large

---

<div class="post-metadata">

**Author:** ![mschendel](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@mschendel](https://mne.discourse.group/u/mschendel)\
**Post date:** [August 28, 2024, 4:00pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/9 "2024-08-28T16:00:47Z")

</div>

Hi all,  
You’re right. I read it, but I didn’t notice.

It would be nice to include a hint in the error output to check head position and head origin.

Thanks again for your help!  
Megan

---

<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:** [September 4, 2024, 4:01pm UTC](https://mne.discourse.group/t/runtimeerror-matrix-is-badly-conditioned/9177/10 "2024-09-04T16:01:17Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
