Skip to content

Commit

Permalink
BUG: Fix fine cal indexing (#8522)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Nov 14, 2020
1 parent c0d098e commit da066d8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/changes/0.21.inc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Bugs

- Fix reading GDF files with excluded channels in :func:`mne.io.read_raw_gdf` (:gh:`8520` by `Clemens Brunner`_)

- Fix bug in :func:`mne.preprocessing.compute_fine_calibration` where the magnetometer calibration coefficients were computed incorrectly (:gh:`8522` by `Eric Larson`_)


.. _changes_0_21_1:

Expand Down
3 changes: 2 additions & 1 deletion mne/preprocessing/_fine_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def _adjust_mag_normals(info, data, origin, ext_order):
mag_scale = 100.
picks_use = pick_types(info, meg='mag', exclude='bads')
picks_meg = pick_types(info, meg=True, exclude=())
picks_mag_orig = pick_types(info, meg='mag', exclude='bads')
info = pick_info(info, picks_use) # copy
data = data[picks_use]
cals = np.ones((len(data), 1))
Expand Down Expand Up @@ -317,7 +318,7 @@ def _adjust_mag_normals(info, data, origin, ext_order):
assert zs.shape == (len(data), 3)
assert cals.shape == (len(data), 1)
imb_cals = np.ones(len(picks_meg))
imb_cals[picks_mag] = cals[:, 0]
imb_cals[picks_mag_orig] = cals[:, 0]
return zs, imb_cals, good


Expand Down
8 changes: 6 additions & 2 deletions mne/preprocessing/tests/test_fine_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def test_compute_fine_cal():
assert counts == 1
assert set(got_cal.keys()) == set(want_cal.keys())
assert got_cal['ch_names'] == want_cal['ch_names']
# in practice these should never be exactly 1.
assert sum([(ic == 1.).any() for ic in want_cal['imb_cals']]) == 0
assert sum([(ic == 1.).any() for ic in got_cal['imb_cals']]) == 0

got_imb = np.array(got_cal['imb_cals'], float)
want_imb = np.array(want_cal['imb_cals'], float)
Expand Down Expand Up @@ -108,7 +111,7 @@ def test_compute_fine_cal():
raw_sss_py = maxwell_filter(raw, calibration=got_cal, **kwargs)
_assert_shielding(raw_sss, raw, 26, 27)
_assert_shielding(raw_sss_mf, raw, 61, 63)
_assert_shielding(raw_sss_py, raw, 59, 60)
_assert_shielding(raw_sss_py, raw, 61, 63)

# redoing with given mag data should yield same result
got_cal_redo, _ = compute_fine_calibration(
Expand All @@ -117,6 +120,7 @@ def test_compute_fine_cal():
assert got_cal['ch_names'] == got_cal_redo['ch_names']
assert_allclose(got_cal['imb_cals'], got_cal_redo['imb_cals'], atol=5e-5)
assert_allclose(got_cal['locs'], got_cal_redo['locs'], atol=1e-6)
assert sum([(ic == 1.).any() for ic in got_cal['imb_cals']]) == 0

# redoing with 3 imlabance parameters should improve the shielding factor
grad_picks = pick_types(raw.info, meg='grad')
Expand All @@ -133,4 +137,4 @@ def test_compute_fine_cal():
corr = np.corrcoef(got_grad_3_imbs[:, 0], got_grad_imbs[:, 0])[0, 1]
assert 0.6 < corr < 0.7
raw_sss_py = maxwell_filter(raw, calibration=got_cal_3, **kwargs)
_assert_shielding(raw_sss_py, raw, 60, 62)
_assert_shielding(raw_sss_py, raw, 68, 70)

0 comments on commit da066d8

Please sign in to comment.