def test_inverse_operator_noise_cov_rank(evoked, noise_cov): """Test MNE inverse operator with a specified noise cov rank.""" fwd_op = read_forward_solution_meg(fname_fwd, surf_ori=True) inv = make_inverse_operator( evoked.info, fwd_op, noise_cov, rank=dict(meg=64)) assert (compute_rank_inverse(inv) == 64) inv = make_inverse_operator( evoked.info, fwd_op, noise_cov, rank=dict(meg=64)) assert (compute_rank_inverse(inv) == 64) fwd_op = read_forward_solution_eeg(fname_fwd, surf_ori=True) inv = make_inverse_operator( evoked.info, fwd_op, noise_cov, rank=dict(eeg=20)) assert (compute_rank_inverse(inv) == 20)
def test_make_inverse_operator_diag(evoked, noise_cov): """Test MNE inverse computation with diagonal noise cov.""" noise_cov = noise_cov.as_diag() fwd_op = convert_forward_solution(read_forward_solution(fname_fwd), surf_ori=True) inv_op = make_inverse_operator(evoked.info, fwd_op, noise_cov, loose=0.2, depth=0.8) _compare_io(inv_op) inverse_operator_diag = read_inverse_operator(fname_inv_meeg_diag) # This one is pretty bad _compare_inverses_approx(inverse_operator_diag, inv_op, evoked, rtol=1e-1, atol=1e-1, ctol=0.99, check_K=False) # Inverse has 366 channels - 6 proj = 360 assert (compute_rank_inverse(inverse_operator_diag) == 360)
def test_make_inverse_operator_fixed(evoked, noise_cov): """Test MNE inverse computation (fixed orientation).""" fwd = read_forward_solution_meg(fname_fwd) # can't make fixed inv with depth weighting without free ori fwd fwd_fixed = convert_forward_solution(fwd, force_fixed=True, use_cps=True) pytest.raises(ValueError, make_inverse_operator, evoked.info, fwd_fixed, noise_cov, depth=0.8, fixed=True) # now compare to C solution # note that the forward solution must not be surface-oriented # to get equivalence (surf_ori=True changes the normals) with catch_logging() as log: inv_op = make_inverse_operator( # test depth=0. alias for depth=None evoked.info, fwd, noise_cov, depth=0., fixed=True, use_cps=False, verbose=True) log = log.getvalue() assert 'MEG: rank 302 computed from 305' in log assert 'EEG channels: 0' in repr(inv_op) assert 'MEG channels: 305' in repr(inv_op) del fwd_fixed inverse_operator_nodepth = read_inverse_operator(fname_inv_fixed_nodepth) # XXX We should have this but we don't (MNE-C doesn't restrict info): # assert 'EEG channels: 0' in repr(inverse_operator_nodepth) assert 'MEG channels: 305' in repr(inverse_operator_nodepth) _compare_inverses_approx(inverse_operator_nodepth, inv_op, evoked, rtol=1e-5, atol=1e-4) # Inverse has 306 channels - 6 proj = 302 assert (compute_rank_inverse(inverse_operator_nodepth) == 302) # Now with depth fwd_surf = convert_forward_solution(fwd, surf_ori=True) # not fixed for kwargs, use_fwd in zip([dict(fixed=True), dict(loose=0.)], [fwd, fwd_surf]): # Should be equiv. inv_op_depth = make_inverse_operator( evoked.info, use_fwd, noise_cov, depth=0.8, use_cps=True, **kwargs) inverse_operator_depth = read_inverse_operator(fname_inv_fixed_depth) # Normals should be the adjusted ones assert_allclose(inverse_operator_depth['source_nn'], fwd_surf['source_nn'][2::3], atol=1e-5) _compare_inverses_approx(inverse_operator_depth, inv_op_depth, evoked, rtol=1e-3, atol=1e-4)
def test_apply_inverse_operator(evoked, inv, min_, max_): """Test MNE inverse application.""" # use fname_inv as it will be faster than fname_full (fewer verts and chs) inverse_operator = read_inverse_operator(inv) # Inverse has 306 channels - 4 proj = 302 assert (compute_rank_inverse(inverse_operator) == 302) # Inverse has 306 channels - 4 proj = 302 assert (compute_rank_inverse(inverse_operator) == 302) stc = apply_inverse(evoked, inverse_operator, lambda2, "MNE") assert stc.subject == 'sample' assert stc.data.min() > min_ assert stc.data.max() < max_ assert abs(stc).data.mean() > 1e-11 # test if using prepared and not prepared inverse operator give the same # result inv_op = prepare_inverse_operator(inverse_operator, nave=evoked.nave, lambda2=lambda2, method="MNE") stc2 = apply_inverse(evoked, inv_op, lambda2, "MNE") assert_array_almost_equal(stc.data, stc2.data) assert_array_almost_equal(stc.times, stc2.times) # This is little more than a smoke test... stc = apply_inverse(evoked, inverse_operator, lambda2, "sLORETA") assert stc.subject == 'sample' assert abs(stc).data.min() > 0 assert 2 < stc.data.max() < 7 assert abs(stc).data.mean() > 0.1 stc = apply_inverse(evoked, inverse_operator, lambda2, "eLORETA") assert stc.subject == 'sample' assert abs(stc).data.min() > min_ assert stc.data.max() < max_ * 2 assert abs(stc).data.mean() > 1e-11 stc = apply_inverse(evoked, inverse_operator, lambda2, "dSPM") assert stc.subject == 'sample' assert abs(stc).data.min() > 0 assert 7.5 < stc.data.max() < 15 assert abs(stc).data.mean() > 0.1 # test without using a label (so delayed computation is used) label = read_label(fname_label % 'Aud-lh') for method in INVERSE_METHODS: stc = apply_inverse(evoked, inv_op, lambda2, method) stc_label = apply_inverse(evoked, inv_op, lambda2, method, label=label) assert_equal(stc_label.subject, 'sample') label_stc = stc.in_label(label) assert label_stc.subject == 'sample' assert_allclose(stc_label.data, label_stc.data) # Test that no errors are raised with loose inverse ops and picking normals noise_cov = read_cov(fname_cov) fwd = read_forward_solution_meg(fname_fwd) inv_op_meg = make_inverse_operator( evoked.info, fwd, noise_cov, loose=1, fixed='auto', depth=None) apply_inverse(evoked, inv_op_meg, 1 / 9., method='MNE', pick_ori='normal') # Test we get errors when using custom ref or no average proj is present evoked.info['custom_ref_applied'] = True pytest.raises(ValueError, apply_inverse, evoked, inv_op, lambda2, "MNE") evoked.info['custom_ref_applied'] = False evoked.info['projs'] = [] # remove EEG proj pytest.raises(ValueError, apply_inverse, evoked, inv_op, lambda2, "MNE") # But test that we do not get EEG-related errors on MEG-only inv (gh-4650) apply_inverse(evoked, inv_op_meg, 1. / 9.)