本文整理汇总了Python中mne.minimum_norm.inverse.read_inverse_operator函数的典型用法代码示例。如果您正苦于以下问题:Python read_inverse_operator函数的具体用法?Python read_inverse_operator怎么用?Python read_inverse_operator使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_inverse_operator函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_io_inverse_operator
def test_io_inverse_operator():
"""Test IO of inverse_operator."""
tempdir = _TempDir()
inverse_operator = read_inverse_operator(fname_inv)
x = repr(inverse_operator)
assert (x)
assert (isinstance(inverse_operator['noise_cov'], Covariance))
# just do one example for .gz, as it should generalize
_compare_io(inverse_operator, '.gz')
# test warnings on bad filenames
inv_badname = op.join(tempdir, 'test-bad-name.fif.gz')
with pytest.warns(RuntimeWarning, match='-inv.fif'):
write_inverse_operator(inv_badname, inverse_operator)
with pytest.warns(RuntimeWarning, match='-inv.fif'):
read_inverse_operator(inv_badname)
# make sure we can write and read
inv_fname = op.join(tempdir, 'test-inv.fif')
args = (10, 1. / 9., 'dSPM')
inv_prep = prepare_inverse_operator(inverse_operator, *args)
write_inverse_operator(inv_fname, inv_prep)
inv_read = read_inverse_operator(inv_fname)
_compare(inverse_operator, inv_read)
inv_read_prep = prepare_inverse_operator(inv_read, *args)
_compare(inv_prep, inv_read_prep)
inv_prep_prep = prepare_inverse_operator(inv_prep, *args)
_compare(inv_prep, inv_prep_prep)
示例2: test_make_inverse_operator_fixed
def test_make_inverse_operator_fixed():
"""Test MNE inverse computation (fixed orientation)
"""
fwd_op = read_forward_solution(fname_fwd, surf_ori=True)
fwd_1 = read_forward_solution(fname_fwd, surf_ori=False, force_fixed=False)
fwd_2 = read_forward_solution(fname_fwd, surf_ori=False, force_fixed=True)
evoked = _get_evoked()
noise_cov = read_cov(fname_cov)
# can't make depth-weighted fixed inv without surf ori fwd
assert_raises(ValueError, make_inverse_operator, evoked.info, fwd_1,
noise_cov, depth=0.8, loose=None, fixed=True)
# can't make fixed inv with depth weighting without free ori fwd
assert_raises(ValueError, make_inverse_operator, evoked.info, fwd_2,
noise_cov, depth=0.8, loose=None, fixed=True)
# compare to C solution w/fixed
inv_op = make_inverse_operator(evoked.info, fwd_op, noise_cov, depth=0.8,
loose=None, fixed=True)
_compare_io(inv_op)
inverse_operator_fixed = read_inverse_operator(fname_inv_fixed)
_compare_inverses_approx(inverse_operator_fixed, inv_op, evoked, 2)
# Inverse has 306 channels - 4 proj = 302
assert_true(compute_rank_inverse(inverse_operator_fixed) == 302)
# now compare to C solution
# note that the forward solution must not be surface-oriented
# to get equivalency (surf_ori=True changes the normals)
inv_op = make_inverse_operator(evoked.info, fwd_2, noise_cov, depth=None,
loose=None, fixed=True)
inverse_operator_nodepth = read_inverse_operator(fname_inv_nodepth)
_compare_inverses_approx(inverse_operator_nodepth, inv_op, evoked, 2)
# Inverse has 306 channels - 4 proj = 302
assert_true(compute_rank_inverse(inverse_operator_fixed) == 302)
示例3: test_make_inverse_operator_fixed
def test_make_inverse_operator_fixed(self):
"""Test MNE inverse computation w/ fixed orientation (& no depth
weighting)
"""
# can't make fixed inv without surf ori fwd
assert_raises(ValueError, make_inverse_operator, evoked.info,
self.fwd_1, noise_cov, depth=0.8, loose=None, fixed=True)
# can't make fixed inv with depth weighting without free ori fwd
assert_raises(ValueError, make_inverse_operator, evoked.info,
self.fwd_2, noise_cov, depth=0.8, loose=None, fixed=True)
inv_op = make_inverse_operator(evoked.info, self.fwd_op, noise_cov,
depth=0.8, loose=None, fixed=True)
_compare_io(inv_op)
inverse_operator_fixed = read_inverse_operator(fname_inv_fixed)
_compare_inverses_approx(inverse_operator_fixed, inv_op, evoked, 2)
# Inverse has 306 channels - 4 proj = 302
assert_true(compute_rank_inverse(inverse_operator_fixed) == 302)
# Now without depth weighting, these should be equivalent
inv_op = make_inverse_operator(evoked.info, self.fwd_2, noise_cov,
depth=None, loose=None, fixed=True)
inv_2 = make_inverse_operator(evoked.info, self.fwd_op, noise_cov,
depth=None, loose=None, fixed=True)
_compare_inverses_approx(inv_op, inv_2, evoked, 2)
_compare_io(inv_op)
# now compare to C solution
inverse_operator_nodepth = read_inverse_operator(fname_inv_nodepth)
_compare_inverses_approx(inverse_operator_nodepth, inv_op, evoked, 2)
# Inverse has 306 channels - 4 proj = 302
assert_true(compute_rank_inverse(inverse_operator_fixed) == 302)
示例4: test_io_inverse_operator
def test_io_inverse_operator():
"""Test IO of inverse_operator
"""
tempdir = _TempDir()
inverse_operator = read_inverse_operator(fname_inv)
x = repr(inverse_operator)
assert_true(x)
assert_true(isinstance(inverse_operator['noise_cov'], Covariance))
# just do one example for .gz, as it should generalize
_compare_io(inverse_operator, '.gz')
# test warnings on bad filenames
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
inv_badname = op.join(tempdir, 'test-bad-name.fif.gz')
write_inverse_operator(inv_badname, inverse_operator)
read_inverse_operator(inv_badname)
assert_naming(w, 'test_inverse.py', 2)
# make sure we can write and read
inv_fname = op.join(tempdir, 'test-inv.fif')
args = (10, 1. / 9., 'dSPM')
inv_prep = prepare_inverse_operator(inverse_operator, *args)
write_inverse_operator(inv_fname, inv_prep)
inv_read = read_inverse_operator(inv_fname)
_compare(inverse_operator, inv_read)
inv_read_prep = prepare_inverse_operator(inv_read, *args)
_compare(inv_prep, inv_read_prep)
inv_prep_prep = prepare_inverse_operator(inv_prep, *args)
_compare(inv_prep, inv_prep_prep)
示例5: test_io_inverse_operator
def test_io_inverse_operator():
"""Test IO of inverse_operator with GZip
"""
inverse_operator = read_inverse_operator(fname_inv)
# just do one example for .gz, as it should generalize
_compare_io(inverse_operator, '.gz')
# test warnings on bad filenames
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
inv_badname = op.join(tempdir, 'test-bad-name.fif.gz')
write_inverse_operator(inv_badname, inverse_operator)
read_inverse_operator(inv_badname)
assert_true(len(w) == 2)
示例6: test_make_inverse_operator_loose
def test_make_inverse_operator_loose(evoked):
"""Test MNE inverse computation (precomputed and non-precomputed)."""
# Test old version of inverse computation starting from forward operator
noise_cov = read_cov(fname_cov)
inverse_operator = read_inverse_operator(fname_inv)
fwd_op = convert_forward_solution(read_forward_solution_meg(fname_fwd),
surf_ori=True, copy=False)
with catch_logging() as log:
with pytest.deprecated_call(): # limit_depth_chs
my_inv_op = make_inverse_operator(
evoked.info, fwd_op, noise_cov, loose=0.2, depth=0.8,
limit_depth_chs=False, verbose=True)
log = log.getvalue()
assert 'MEG: rank 302 computed' in log
assert 'limit = 1/%d' % fwd_op['nsource'] in log
_compare_io(my_inv_op)
assert_equal(inverse_operator['units'], 'Am')
_compare_inverses_approx(my_inv_op, inverse_operator, evoked,
rtol=1e-2, atol=1e-5, depth_atol=1e-3)
# Test MNE inverse computation starting from forward operator
with catch_logging() as log:
my_inv_op = make_inverse_operator(evoked.info, fwd_op, noise_cov,
loose='auto', depth=0.8,
fixed=False, verbose=True)
log = log.getvalue()
assert 'MEG: rank 302 computed from 305' in log
_compare_io(my_inv_op)
_compare_inverses_approx(my_inv_op, inverse_operator, evoked,
rtol=1e-3, atol=1e-5)
assert ('dev_head_t' in my_inv_op['info'])
assert ('mri_head_t' in my_inv_op)
示例7: test_apply_mne_inverse_raw
def test_apply_mne_inverse_raw():
"""Test MNE with precomputed inverse operator on Raw."""
start = 3
stop = 10
raw = read_raw_fif(fname_raw)
label_lh = read_label(fname_label % 'Aud-lh')
_, times = raw[0, start:stop]
inverse_operator = read_inverse_operator(fname_full)
inverse_operator = prepare_inverse_operator(inverse_operator, nave=1,
lambda2=lambda2, method="dSPM")
for pick_ori in [None, "normal", "vector"]:
stc = apply_inverse_raw(raw, inverse_operator, lambda2, "dSPM",
label=label_lh, start=start, stop=stop, nave=1,
pick_ori=pick_ori, buffer_size=None,
prepared=True)
stc2 = apply_inverse_raw(raw, inverse_operator, lambda2, "dSPM",
label=label_lh, start=start, stop=stop,
nave=1, pick_ori=pick_ori,
buffer_size=3, prepared=True)
if pick_ori is None:
assert_true(np.all(stc.data > 0))
assert_true(np.all(stc2.data > 0))
assert_true(stc.subject == 'sample')
assert_true(stc2.subject == 'sample')
assert_array_almost_equal(stc.times, times)
assert_array_almost_equal(stc2.times, times)
assert_array_almost_equal(stc.data, stc2.data)
示例8: test_make_inverse_operator
def test_make_inverse_operator():
"""Test MNE inverse computation (precomputed and non-precomputed)."""
# Test old version of inverse computation starting from forward operator
evoked = _get_evoked()
noise_cov = read_cov(fname_cov)
inverse_operator = read_inverse_operator(fname_inv)
fwd_op = convert_forward_solution(read_forward_solution_meg(fname_fwd),
surf_ori=True, copy=False)
with catch_logging() as log:
my_inv_op = make_inverse_operator(evoked.info, fwd_op, noise_cov,
loose=0.2, depth=0.8,
limit_depth_chs=False, verbose=True)
log = log.getvalue()
assert 'rank 302 (3 small eigenvalues omitted)' in log
_compare_io(my_inv_op)
assert_equal(inverse_operator['units'], 'Am')
_compare_inverses_approx(my_inv_op, inverse_operator, evoked,
rtol=1e-2, atol=1e-5, depth_atol=1e-3)
# Test MNE inverse computation starting from forward operator
with catch_logging() as log:
my_inv_op = make_inverse_operator(evoked.info, fwd_op, noise_cov,
loose=0.2, depth=0.8, verbose=True)
log = log.getvalue()
assert 'rank 302 (3 small eigenvalues omitted)' in log
_compare_io(my_inv_op)
_compare_inverses_approx(my_inv_op, inverse_operator, evoked,
rtol=1e-3, atol=1e-5)
assert ('dev_head_t' in my_inv_op['info'])
assert ('mri_head_t' in my_inv_op)
示例9: test_tfr_with_inverse_operator
def test_tfr_with_inverse_operator():
"""Test time freq with MNE inverse computation"""
tmin, tmax, event_id = -0.2, 0.5, 1
# Setup for reading the raw data
raw = io.Raw(fname_data)
events = find_events(raw, stim_channel='STI 014')
inverse_operator = read_inverse_operator(fname_inv)
inv = prepare_inverse_operator(inverse_operator, nave=1,
lambda2=1. / 9., method="dSPM")
raw.info['bads'] += ['MEG 2443', 'EEG 053'] # bads + 2 more
# picks MEG gradiometers
picks = pick_types(raw.info, meg=True, eeg=False, eog=True,
stim=False, exclude='bads')
# Load condition 1
event_id = 1
events3 = events[:3] # take 3 events to keep the computation time low
epochs = Epochs(raw, events3, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6),
preload=True)
# Compute a source estimate per frequency band
bands = dict(alpha=[10, 10])
label = read_label(fname_label)
stcs = source_band_induced_power(epochs, inv, bands,
n_cycles=2, use_fft=False, pca=True,
label=label, prepared=True)
stc = stcs['alpha']
assert_true(len(stcs) == len(list(bands.keys())))
assert_true(np.all(stc.data > 0))
assert_array_almost_equal(stc.times, epochs.times)
stcs_no_pca = source_band_induced_power(epochs, inv, bands,
n_cycles=2, use_fft=False,
pca=False, label=label,
prepared=True)
assert_array_almost_equal(stcs['alpha'].data, stcs_no_pca['alpha'].data)
# Compute a source estimate per frequency band
epochs = Epochs(raw, events[:10], event_id, tmin, tmax, picks=picks,
baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6),
preload=True)
frequencies = np.arange(7, 30, 2) # define frequencies of interest
power, phase_lock = source_induced_power(epochs, inv,
frequencies, label,
baseline=(-0.1, 0),
baseline_mode='percent',
n_cycles=2, n_jobs=1,
prepared=True)
assert_true(np.all(phase_lock > 0))
assert_true(np.all(phase_lock <= 1))
assert_true(np.max(power) > 10)
示例10: test_apply_inverse_sphere
def test_apply_inverse_sphere():
"""Test applying an inverse with a sphere model (rank-deficient)."""
evoked = _get_evoked()
evoked.pick_channels(evoked.ch_names[:306:8])
evoked.info['projs'] = []
cov = make_ad_hoc_cov(evoked.info)
sphere = make_sphere_model('auto', 'auto', evoked.info)
fwd = read_forward_solution(fname_fwd)
vertices = [fwd['src'][0]['vertno'][::5],
fwd['src'][1]['vertno'][::5]]
stc = SourceEstimate(np.zeros((sum(len(v) for v in vertices), 1)),
vertices, 0., 1.)
fwd = restrict_forward_to_stc(fwd, stc)
fwd = make_forward_solution(evoked.info, fwd['mri_head_t'], fwd['src'],
sphere, mindist=5.)
evoked = EvokedArray(fwd['sol']['data'].copy(), evoked.info)
assert fwd['sol']['nrow'] == 39
assert fwd['nsource'] == 101
assert fwd['sol']['ncol'] == 303
tempdir = _TempDir()
temp_fname = op.join(tempdir, 'temp-inv.fif')
inv = make_inverse_operator(evoked.info, fwd, cov, loose=1.)
# This forces everything to be float32
write_inverse_operator(temp_fname, inv)
inv = read_inverse_operator(temp_fname)
stc = apply_inverse(evoked, inv, method='eLORETA',
method_params=dict(eps=1e-2))
# assert zero localization bias
assert_array_equal(np.argmax(stc.data, axis=0),
np.repeat(np.arange(101), 3))
示例11: test_make_inverse_operator_fixed
def test_make_inverse_operator_fixed():
"""Test MNE inverse computation (fixed orientation)."""
fwd = read_forward_solution_meg(fname_fwd)
evoked = _get_evoked()
noise_cov = read_cov(fname_cov)
# 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 equivalency (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 'rank 302 (3 small eigenvalues omitted)' 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)
示例12: test_source_psd_epochs
def test_source_psd_epochs():
"""Test multi-taper source PSD computation in label from epochs"""
raw = fiff.Raw(fname_data)
inverse_operator = read_inverse_operator(fname_inv)
label = read_label(fname_label)
event_id, tmin, tmax = 1, -0.2, 0.5
lambda2, method = 1. / 9., 'dSPM'
bandwidth = 8.
fmin, fmax = 0, 100
picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=True,
ecg=True, eog=True, include=['STI 014'],
exclude='bads')
reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6)
events = find_events(raw)
epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), reject=reject)
# only look at one epoch
epochs.drop_bad_epochs()
one_epochs = epochs[:1]
# return list
stc_psd = compute_source_psd_epochs(one_epochs, inverse_operator,
lambda2=lambda2, method=method,
pick_normal=True, label=label,
bandwidth=bandwidth,
fmin=fmin, fmax=fmax)[0]
# return generator
stcs = compute_source_psd_epochs(one_epochs, inverse_operator,
lambda2=lambda2, method=method,
pick_normal=True, label=label,
bandwidth=bandwidth,
fmin=fmin, fmax=fmax,
return_generator=True)
for stc in stcs:
stc_psd_gen = stc
assert_array_almost_equal(stc_psd.data, stc_psd_gen.data)
# compare with direct computation
stc = apply_inverse_epochs(one_epochs, inverse_operator,
lambda2=lambda2, method=method,
pick_normal=True, label=label)[0]
sfreq = epochs.info['sfreq']
psd, freqs = multitaper_psd(stc.data, sfreq=sfreq, bandwidth=bandwidth,
fmin=fmin, fmax=fmax)
assert_array_almost_equal(psd, stc_psd.data)
assert_array_almost_equal(freqs, stc_psd.times)
示例13: test_io_inverse_operator
def test_io_inverse_operator():
"""Test IO of inverse_operator
"""
for inv in [inverse_operator, inverse_operator_vol]:
inv_init = copy.deepcopy(inv)
write_inverse_operator('test-inv.fif', inv)
this_inv = read_inverse_operator('test-inv.fif')
_compare(inv, inv_init)
_compare(inv, this_inv)
示例14: test_io_inverse_operator
def test_io_inverse_operator():
"""Test IO of inverse_operator
"""
for inv in [inverse_operator, inverse_operator_vol]:
inv_init = copy.deepcopy(inv)
for out_file in ['test-inv.fif', 'test-inv.fif.gz']:
write_inverse_operator(out_file, inv)
this_inv = read_inverse_operator(out_file)
_compare(inv, inv_init)
_compare(inv, this_inv)
示例15: test_inverse_operator_volume
def test_inverse_operator_volume():
"""Test MNE inverse computation on volume source space"""
evoked = fiff.Evoked(fname_data, setno=0, baseline=(None, 0))
inverse_operator_vol = read_inverse_operator(fname_vol_inv)
stc = apply_inverse(evoked, inverse_operator_vol, lambda2, "dSPM")
stc.save('tmp-vl.stc')
stc2 = read_source_estimate('tmp-vl.stc')
assert_true(np.all(stc.data > 0))
assert_true(np.all(stc.data < 35))
assert_array_almost_equal(stc.data, stc2.data)
assert_array_almost_equal(stc.times, stc2.times)