def test_gamma_map(): """Test Gamma MAP inverse""" forward = read_forward_solution(fname_fwd, force_fixed=False, surf_ori=True) forward = pick_types_forward(forward, meg=False, eeg=True) evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0)) evoked.resample(50) evoked.crop(tmin=0, tmax=0.3) cov = read_cov(fname_cov) cov = regularize(cov, evoked.info) alpha = 0.2 stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=True, update_mode=1, verbose=False) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertices)[idx] == 96397) stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=1, verbose=False) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertices)[idx] == 82010) # force fixed orientation stc, res = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=2, loose=None, return_residual=True, verbose=False) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) # assert_true(np.concatenate(stc.vertices)[idx] == 83398) # XXX FIX assert_array_almost_equal(evoked.times, res.times)
def test_gamma_map(): """Test Gamma MAP inverse""" forward = read_forward_solution(fname_fwd, force_fixed=False, surf_ori=True) forward = pick_types_forward(forward, meg=False, eeg=True) evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0), proj=False) evoked.resample(50, npad=100) evoked.crop(tmin=0.1, tmax=0.16) # crop to nice window near samp border cov = read_cov(fname_cov) cov = regularize(cov, evoked.info) alpha = 0.5 stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=True, update_mode=1) _check_stc(stc, evoked, 68477) stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=1) _check_stc(stc, evoked, 82010) # force fixed orientation stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=2, loose=None, return_residual=False) _check_stc(stc, evoked, 85739, 20)
def test_gamma_map(): """Test Gamma MAP inverse""" forward = read_forward_solution(fname_fwd) forward = convert_forward_solution(forward, surf_ori=True) forward = pick_types_forward(forward, meg=False, eeg=True) evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0), proj=False) evoked.resample(50, npad=100) evoked.crop(tmin=0.1, tmax=0.16) # crop to window around peak cov = read_cov(fname_cov) cov = regularize(cov, evoked.info) alpha = 0.5 stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=True, update_mode=1) _check_stc(stc, evoked, 68477) stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=1) _check_stc(stc, evoked, 82010) dips = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=1, return_as_dipoles=True) assert_true(isinstance(dips[0], Dipole)) stc_dip = make_stc_from_dipoles(dips, forward['src']) _check_stcs(stc, stc_dip) # force fixed orientation stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=2, loose=0, return_residual=False) _check_stc(stc, evoked, 85739, 20)
def test_gamma_map(): """Test Gamma MAP inverse.""" forward = read_forward_solution(fname_fwd) forward = convert_forward_solution(forward, surf_ori=True) forward = pick_types_forward(forward, meg=False, eeg=True) evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0), proj=False) evoked.resample(50, npad=100) evoked.crop(tmin=0.1, tmax=0.14) # crop to window around peak cov = read_cov(fname_cov) cov = regularize(cov, evoked.info, rank=None) alpha = 0.5 stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=True, update_mode=1) _check_stc(stc, evoked, 68477, 'lh', fwd=forward) stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=1) _check_stc(stc, evoked, 82010, 'lh', fwd=forward) dips = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=1, return_as_dipoles=True) assert (isinstance(dips[0], Dipole)) stc_dip = make_stc_from_dipoles(dips, forward['src']) _check_stcs(stc, stc_dip) # force fixed orientation stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=2, loose=0, return_residual=False) _check_stc(stc, evoked, 85739, 'lh', fwd=forward, ratio=20.)
def test_gamma_map(): """Test Gamma MAP inverse""" forward = read_forward_solution(fname_fwd, force_fixed=False, surf_ori=True) evoked = fiff.Evoked(fname_evoked, setno=0, baseline=(None, 0)) evoked.crop(tmin=0, tmax=0.3) cov = read_cov(fname_cov) cov = mne.cov.regularize(cov, evoked.info) alpha = 0.2 stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=True, update_mode=1) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertno)[idx] == 96397) stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=1) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertno)[idx] == 82010) # force fixed orientation stc, res = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=2, loose=None, return_residual=True) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertno)[idx] == 83398) assert_array_almost_equal(evoked.times, res.times)
def test_gamma_map(): """Test Gamma MAP inverse""" forward = read_forward_solution(fname_fwd, force_fixed=False, surf_ori=True) forward = pick_types_forward(forward, meg=False, eeg=True) evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0)) evoked.resample(50) evoked.crop(tmin=0, tmax=0.3) cov = read_cov(fname_cov) cov = regularize(cov, evoked.info) alpha = 0.2 stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=True, update_mode=1, verbose=False) assert_array_almost_equal(stc.times, evoked.times, 5) idx = np.argmax(np.sum(stc.data**2, axis=1)) assert_true(np.concatenate(stc.vertices)[idx] == 96397) stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=1, verbose=False) assert_array_almost_equal(stc.times, evoked.times, 5) idx = np.argmax(np.sum(stc.data**2, axis=1)) assert_true(np.concatenate(stc.vertices)[idx] == 82010) # force fixed orientation stc, res = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=2, loose=None, return_residual=True, verbose=False) assert_array_almost_equal(stc.times, evoked.times, 5) idx = np.argmax(np.sum(stc.data**2, axis=1)) # assert_true(np.concatenate(stc.vertices)[idx] == 83398) # XXX FIX assert_array_almost_equal(evoked.times, res.times)
def test_gamma_map_vol_sphere(): """Gamma MAP with a sphere forward and volumic source space.""" evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0), proj=False) evoked.resample(50, npad=100) evoked.crop(tmin=0.1, tmax=0.16) # crop to window around peak cov = read_cov(fname_cov) cov = regularize(cov, evoked.info, rank=None) info = evoked.info sphere = mne.make_sphere_model(r0=(0., 0., 0.), head_radius=0.080) src = mne.setup_volume_source_space(subject=None, pos=30., mri=None, sphere=(0.0, 0.0, 0.0, 80.0), bem=None, mindist=5.0, exclude=2.0) fwd = mne.make_forward_solution(info, trans=None, src=src, bem=sphere, eeg=False, meg=True) alpha = 0.5 pytest.raises(ValueError, gamma_map, evoked, fwd, cov, alpha, loose=0, return_residual=False) pytest.raises(ValueError, gamma_map, evoked, fwd, cov, alpha, loose=0.2, return_residual=False) stc = gamma_map(evoked, fwd, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=2, return_residual=False) assert_array_almost_equal(stc.times, evoked.times, 5) # Compare orientation obtained using fit_dipole and gamma_map # for a simulated evoked containing a single dipole stc = mne.VolSourceEstimate(50e-9 * np.random.RandomState(42).randn(1, 4), vertices=stc.vertices[:1], tmin=stc.tmin, tstep=stc.tstep) evoked_dip = mne.simulation.simulate_evoked(fwd, stc, info, cov, nave=1e9, use_cps=True) dip_gmap = gamma_map(evoked_dip, fwd, cov, 0.1, return_as_dipoles=True) amp_max = [np.max(d.amplitude) for d in dip_gmap] dip_gmap = dip_gmap[np.argmax(amp_max)] assert (dip_gmap[0].pos[0] in src[0]['rr'][stc.vertices]) dip_fit = mne.fit_dipole(evoked_dip, cov, sphere)[0] assert (np.abs(np.dot(dip_fit.ori[0], dip_gmap.ori[0])) > 0.99)
def test_gamma_map_vol_sphere(): """Gamma MAP with a sphere forward and volumic source space""" evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0), proj=False) evoked.resample(50, npad=100) evoked.crop(tmin=0.1, tmax=0.16) # crop to window around peak cov = read_cov(fname_cov) cov = regularize(cov, evoked.info) info = evoked.info sphere = mne.make_sphere_model(r0=(0., 0., 0.), head_radius=0.080) src = mne.setup_volume_source_space(subject=None, pos=15., mri=None, sphere=(0.0, 0.0, 0.0, 80.0), bem=None, mindist=5.0, exclude=2.0) fwd = mne.make_forward_solution(info, trans=None, src=src, bem=sphere, eeg=False, meg=True) alpha = 0.5 assert_raises(ValueError, gamma_map, evoked, fwd, cov, alpha, loose=0, return_residual=False) assert_raises(ValueError, gamma_map, evoked, fwd, cov, alpha, loose=0.2, return_residual=False) stc = gamma_map(evoked, fwd, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=2, return_residual=False) assert_array_almost_equal(stc.times, evoked.times, 5)
def test_gamma_map_vol_sphere(): """Gamma MAP with a sphere forward and volumic source space""" evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0), proj=False) evoked.resample(50, npad=100) evoked.crop(tmin=0.1, tmax=0.16) # crop to window around peak cov = read_cov(fname_cov) cov = regularize(cov, evoked.info) info = evoked.info sphere = mne.make_sphere_model(r0=(0., 0., 0.), head_radius=0.080) src = mne.setup_volume_source_space(subject=None, pos=15., mri=None, sphere=(0.0, 0.0, 0.0, 80.0), bem=None, mindist=5.0, exclude=2.0) fwd = mne.make_forward_solution(info, trans=None, src=src, bem=sphere, eeg=False, meg=True) alpha = 0.5 assert_raises(ValueError, gamma_map, evoked, fwd, cov, alpha, loose=None, return_residual=False) assert_raises(ValueError, gamma_map, evoked, fwd, cov, alpha, loose=0.2, return_residual=False) stc = gamma_map(evoked, fwd, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=2, return_residual=False) assert_array_almost_equal(stc.times, evoked.times, 5)
def test_gamma_map(): """Test Gamma MAP inverse""" alpha = 0.2 stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=True, update_mode=1) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertno)[idx] == 96397) stc = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=1) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertno)[idx] == 82010) # force fixed orientation stc, res = gamma_map(evoked, forward, cov, alpha, tol=1e-5, xyz_same_gamma=False, update_mode=2, loose=None, return_residual=True) idx = np.argmax(np.sum(stc.data ** 2, axis=1)) assert_true(np.concatenate(stc.vertno)[idx] == 83398) assert_array_almost_equal(evoked.times, res.times)
def mne_gamma_map(evoked, fwd, noiseCovariance, return_idx=50, alpha=1e-3): numberOfDipoles = int(fwd['sol']['data'].shape[1] / 3) numberOfTimepoints = evoked._data.shape[1] evoked.set_eeg_reference('average', projection=True) stc = gamma_map(evoked, fwd, noiseCovariance, alpha, xyz_same_gamma=False, loose=0, return_residual=False, return_as_dipoles=False, verbose=0) y_est = dipoles_to_stc(stc, numberOfDipoles, numberOfTimepoints) return y_est[:, return_idx]
condition = 'Left visual' evoked = mne.read_evokeds(evoked_fname, condition=condition, baseline=(None, 0)) evoked.crop(tmin=-50e-3, tmax=300e-3) # Read the forward solution forward = mne.read_forward_solution(fwd_fname) # Read noise noise covariance matrix and regularize it cov = mne.read_cov(cov_fname) cov = mne.cov.regularize(cov, evoked.info, rank=None) # Run the Gamma-MAP method with dipole output alpha = 0.5 dipoles, residual = gamma_map( evoked, forward, cov, alpha, xyz_same_gamma=True, return_residual=True, return_as_dipoles=True) ############################################################################### # Plot dipole activations plot_dipole_amplitudes(dipoles) # Plot dipole location of the strongest dipole with MRI slices idx = np.argmax([np.max(np.abs(dip.amplitude)) for dip in dipoles]) plot_dipole_locations(dipoles[idx], forward['mri_head_t'], 'sample', subjects_dir=subjects_dir, mode='orthoview', idx='amplitude') # # Plot dipole locations of all dipoles with MRI slices # for dip in dipoles: # plot_dipole_locations(dip, forward['mri_head_t'], 'sample',
condition = 'Left visual' evoked = mne.read_evokeds(evoked_fname, condition=condition, baseline=(None, 0)) evoked.crop(tmin=-50e-3, tmax=300e-3) # Read the forward solution forward = mne.read_forward_solution(fwd_fname, surf_ori=True, force_fixed=False) # Read noise covariance matrix and regularize it cov = mne.read_cov(cov_fname) cov = mne.cov.regularize(cov, evoked.info) # Run the Gamma-MAP method alpha = 0.5 stc, residual = gamma_map(evoked, forward, cov, alpha, xyz_same_gamma=True, return_residual=True) # View in 2D and 3D ("glass" brain like 3D plot) # Show the sources as spheres scaled by their strength scale_factors = np.max(np.abs(stc.data), axis=1) scale_factors = 0.5 * (1 + scale_factors / np.max(scale_factors)) plot_sparse_source_estimates( forward['src'], stc, bgcolor=(1, 1, 1), modes=['sphere'], opacity=0.1, scale_factors=(scale_factors, None), fig_name="Gamma-MAP") # Show the evoked response and the residual for gradiometers ylim = dict(grad=[-120, 120]) evoked.pick_types(meg='grad', exclude='bads')
def test_gamma_map_standard(): """Test Gamma MAP inverse.""" forward = read_forward_solution(fname_fwd) forward = convert_forward_solution(forward, surf_ori=True) forward = pick_types_forward(forward, meg=False, eeg=True) evoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0), proj=False) evoked.resample(50, npad=100) evoked.crop(tmin=0.1, tmax=0.14) # crop to window around peak cov = read_cov(fname_cov) cov = regularize(cov, evoked.info, rank=None) alpha = 0.5 with catch_logging() as log: stc = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=True, update_mode=1, verbose=True) _check_stc(stc, evoked, 68477, 'lh', fwd=forward) assert_var_exp_log(log.getvalue(), 20, 22) with catch_logging() as log: stc_vec, res = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=True, update_mode=1, pick_ori='vector', return_residual=True, verbose=True) assert_var_exp_log(log.getvalue(), 20, 22) assert_stcs_equal(stc_vec.magnitude(), stc) _check_stc(stc_vec, evoked, 68477, 'lh', fwd=forward, res=res) stc, res = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=1, pick_ori='vector', return_residual=True) _check_stc(stc, evoked, 82010, 'lh', fwd=forward, dist_limit=6., ratio=2., res=res) with catch_logging() as log: dips = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=1, return_as_dipoles=True, verbose=True) exp_var = assert_var_exp_log(log.getvalue(), 58, 60) dip_exp_var = np.mean(sum(dip.gof for dip in dips)) assert_allclose(exp_var, dip_exp_var, atol=10) # not really equiv, close assert (isinstance(dips[0], Dipole)) stc_dip = make_stc_from_dipoles(dips, forward['src']) assert_stcs_equal(stc.magnitude(), stc_dip) # force fixed orientation stc, res = gamma_map(evoked, forward, cov, alpha, tol=1e-4, xyz_same_gamma=False, update_mode=2, loose=0, return_residual=True) _check_stc(stc, evoked, 85739, 'lh', fwd=forward, ratio=20., res=res)