本文整理汇总了Python中mne.compute_covariance函数的典型用法代码示例。如果您正苦于以下问题:Python compute_covariance函数的具体用法?Python compute_covariance怎么用?Python compute_covariance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了compute_covariance函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _get_data
def _get_data():
"""Read in data used in tests."""
# read forward model
forward = mne.read_forward_solution(fname_fwd)
# read data
raw = mne.io.read_raw_fif(fname_raw, preload=True)
events = mne.read_events(fname_event)
event_id, tmin, tmax = 1, -0.1, 0.15
# decimate for speed
left_temporal_channels = mne.read_selection('Left-temporal')
picks = mne.pick_types(raw.info, selection=left_temporal_channels)
picks = picks[::2]
raw.pick_channels([raw.ch_names[ii] for ii in picks])
del picks
raw.info.normalize_proj() # avoid projection warnings
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
baseline=(None, 0.), preload=True, reject=reject)
noise_cov = mne.compute_covariance(epochs, tmin=None, tmax=0.)
data_cov = mne.compute_covariance(epochs, tmin=0.01, tmax=0.15)
return epochs, data_cov, noise_cov, forward
开发者ID:Eric89GXL,项目名称:mne-python,代码行数:26,代码来源:test_check.py
示例2: test_cov_ctf
def test_cov_ctf():
"""Test basic cov computation on ctf data with/without compensation."""
raw = read_raw_ctf(ctf_fname).crop(0., 2.).load_data()
events = make_fixed_length_events(raw, 99999)
assert len(events) == 2
ch_names = [raw.info['ch_names'][pick]
for pick in pick_types(raw.info, meg=True, eeg=False,
ref_meg=False)]
for comp in [0, 1]:
raw.apply_gradient_compensation(comp)
epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True)
with pytest.warns(RuntimeWarning, match='Too few samples'):
noise_cov = compute_covariance(epochs, tmax=0.,
method=['empirical'])
prepare_noise_cov(noise_cov, raw.info, ch_names)
raw.apply_gradient_compensation(0)
epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True)
with pytest.warns(RuntimeWarning, match='Too few samples'):
noise_cov = compute_covariance(epochs, tmax=0., method=['empirical'])
raw.apply_gradient_compensation(1)
# TODO This next call in principle should fail.
prepare_noise_cov(noise_cov, raw.info, ch_names)
# make sure comps matrices was not removed from raw
assert raw.info['comps'], 'Comps matrices removed'
开发者ID:jhouck,项目名称:mne-python,代码行数:28,代码来源:test_cov.py
示例3: test_compute_covariance_auto_reg
def test_compute_covariance_auto_reg():
"""Test automated regularization"""
raw = Raw(raw_fname, preload=False)
events = find_events(raw, stim_channel='STI 014')
event_ids = [1, 2, 3, 4]
reject = dict(mag=4e-12)
# cov with merged events and keep_sample_mean=True
events_merged = merge_events(events, event_ids, 1234)
picks = pick_types(raw.info, meg='mag', eeg=False)
epochs = Epochs(raw, events_merged, 1234, tmin=-0.2, tmax=0,
picks=picks[:5], baseline=(-0.2, -0.1), proj=True,
reject=reject, preload=True)
epochs.crop(None, 0)[:10]
method_params = dict(factor_analysis=dict(iter_n_components=[30]),
pca=dict(iter_n_components=[30]))
with warnings.catch_warnings(record=True) as w:
covs = compute_covariance(epochs, method='auto',
method_params=method_params,
projs=True,
return_estimators=True)
warnings.simplefilter('always')
assert_equal(len(w), 1)
logliks = [c['loglik'] for c in covs]
assert_true(np.diff(logliks).max() <= 0) # descending order
methods = ['empirical',
'factor_analysis',
'ledoit_wolf',
# 'pca', XXX FAILS
]
with warnings.catch_warnings(record=True) as w:
cov3 = compute_covariance(epochs, method=methods,
method_params=method_params, projs=False,
return_estimators=True)
warnings.simplefilter('always')
assert_equal(len(w), 1)
assert_equal(set([c['method'] for c in cov3]),
set(methods))
# projs not allowed with FA or PCA
assert_raises(ValueError, compute_covariance, epochs, method='pca',
projs=True)
# invalid prespecified method
assert_raises(ValueError, compute_covariance, epochs, method='pizza')
# invalid scalings
assert_raises(ValueError, compute_covariance, epochs, method='shrunk',
scalings=dict(misc=123))
开发者ID:Lem97,项目名称:mne-python,代码行数:55,代码来源:test_cov.py
示例4: test_cov_mismatch
def test_cov_mismatch():
"""Test estimation with MEG<->Head mismatch."""
raw = read_raw_fif(raw_fname).crop(0, 5).load_data()
events = find_events(raw, stim_channel='STI 014')
raw.pick_channels(raw.ch_names[:5])
raw.add_proj([], remove_existing=True)
epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0., preload=True)
for kind in ('shift', 'None'):
epochs_2 = epochs.copy()
# This should be fine
with warnings.catch_warnings(record=True) as w:
compute_covariance([epochs, epochs_2])
assert_equal(len(w), 0)
if kind == 'shift':
epochs_2.info['dev_head_t']['trans'][:3, 3] += 0.001
else: # None
epochs_2.info['dev_head_t'] = None
assert_raises(ValueError, compute_covariance, [epochs, epochs_2])
assert_equal(len(w), 0)
compute_covariance([epochs, epochs_2], on_mismatch='ignore')
assert_equal(len(w), 0)
compute_covariance([epochs, epochs_2], on_mismatch='warn')
assert_raises(ValueError, compute_covariance, epochs,
on_mismatch='x')
assert_true(any('transform mismatch' in str(ww.message) for ww in w))
# This should work
epochs.info['dev_head_t'] = None
epochs_2.info['dev_head_t'] = None
compute_covariance([epochs, epochs_2], method=None)
开发者ID:jdammers,项目名称:mne-python,代码行数:29,代码来源:test_cov.py
示例5: test_cov_mismatch
def test_cov_mismatch():
"""Test estimation with MEG<->Head mismatch."""
raw = read_raw_fif(raw_fname, add_eeg_ref=False).crop(0, 5).load_data()
events = find_events(raw, stim_channel="STI 014")
raw.pick_channels(raw.ch_names[:5])
raw.add_proj([], remove_existing=True)
epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0.0, preload=True, add_eeg_ref=False)
for kind in ("shift", "None"):
epochs_2 = epochs.copy()
# This should be fine
with warnings.catch_warnings(record=True) as w:
compute_covariance([epochs, epochs_2])
assert_equal(len(w), 0)
if kind == "shift":
epochs_2.info["dev_head_t"]["trans"][:3, 3] += 0.001
else: # None
epochs_2.info["dev_head_t"] = None
assert_raises(ValueError, compute_covariance, [epochs, epochs_2])
assert_equal(len(w), 0)
compute_covariance([epochs, epochs_2], on_mismatch="ignore")
assert_equal(len(w), 0)
compute_covariance([epochs, epochs_2], on_mismatch="warn")
assert_raises(ValueError, compute_covariance, epochs, on_mismatch="x")
assert_true(any("transform mismatch" in str(ww.message) for ww in w))
# This should work
epochs.info["dev_head_t"] = None
epochs_2.info["dev_head_t"] = None
compute_covariance([epochs, epochs_2], method=None)
开发者ID:joewalter,项目名称:mne-python,代码行数:28,代码来源:test_cov.py
示例6: test_cov_mismatch
def test_cov_mismatch():
"""Test estimation with MEG<->Head mismatch."""
raw = read_raw_fif(raw_fname).crop(0, 5).load_data()
events = find_events(raw, stim_channel='STI 014')
raw.pick_channels(raw.ch_names[:5])
raw.add_proj([], remove_existing=True)
epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0., preload=True)
for kind in ('shift', 'None'):
epochs_2 = epochs.copy()
# This should be fine
compute_covariance([epochs, epochs_2])
if kind == 'shift':
epochs_2.info['dev_head_t']['trans'][:3, 3] += 0.001
else: # None
epochs_2.info['dev_head_t'] = None
pytest.raises(ValueError, compute_covariance, [epochs, epochs_2])
compute_covariance([epochs, epochs_2], on_mismatch='ignore')
with pytest.raises(RuntimeWarning, match='transform mismatch'):
compute_covariance([epochs, epochs_2], on_mismatch='warn')
pytest.raises(ValueError, compute_covariance, epochs,
on_mismatch='x')
# This should work
epochs.info['dev_head_t'] = None
epochs_2.info['dev_head_t'] = None
compute_covariance([epochs, epochs_2], method=None)
开发者ID:jhouck,项目名称:mne-python,代码行数:25,代码来源:test_cov.py
示例7: test_cov_estimation_with_triggers
def test_cov_estimation_with_triggers():
"""Estimate raw with triggers
"""
raw = Raw(raw_fname)
events = find_events(raw)
event_ids = [1, 2, 3, 4]
reject = dict(grad=10000e-13, mag=4e-12, eeg=80e-6, eog=150e-6)
# cov with merged events and keep_sample_mean=True
events_merged = merge_events(events, event_ids, 1234)
epochs = Epochs(raw, events_merged, 1234, tmin=-0.2, tmax=0,
baseline=(-0.2, -0.1), proj=True,
reject=reject, preload=True)
cov = compute_covariance(epochs, keep_sample_mean=True)
cov_mne = read_cov(cov_km_fname)
assert_true(cov_mne.ch_names == cov.ch_names)
assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro')
/ linalg.norm(cov.data, ord='fro')) < 0.005)
# Test with tmin and tmax (different but not too much)
cov_tmin_tmax = compute_covariance(epochs, tmin=-0.19, tmax=-0.01)
assert_true(np.all(cov.data != cov_tmin_tmax.data))
assert_true((linalg.norm(cov.data - cov_tmin_tmax.data, ord='fro')
/ linalg.norm(cov_tmin_tmax.data, ord='fro')) < 0.05)
# cov using a list of epochs and keep_sample_mean=True
epochs = [Epochs(raw, events, ev_id, tmin=-0.2, tmax=0,
baseline=(-0.2, -0.1), proj=True, reject=reject)
for ev_id in event_ids]
cov2 = compute_covariance(epochs, keep_sample_mean=True)
assert_array_almost_equal(cov.data, cov2.data)
assert_true(cov.ch_names == cov2.ch_names)
# cov with keep_sample_mean=False using a list of epochs
cov = compute_covariance(epochs, keep_sample_mean=False)
cov_mne = read_cov(cov_fname)
assert_true(cov_mne.ch_names == cov.ch_names)
assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro')
/ linalg.norm(cov.data, ord='fro')) < 0.005)
# test IO when computation done in Python
cov.save('test-cov.fif') # test saving
cov_read = read_cov('test-cov.fif')
assert_true(cov_read.ch_names == cov.ch_names)
assert_true(cov_read.nfree == cov.nfree)
assert_true((linalg.norm(cov.data - cov_read.data, ord='fro')
/ linalg.norm(cov.data, ord='fro')) < 1e-5)
开发者ID:sudo-nim,项目名称:mne-python,代码行数:49,代码来源:test_cov.py
示例8: test_compute_covariance_auto_reg
def test_compute_covariance_auto_reg():
"""Test automated regularization"""
raw = read_raw_fif(raw_fname, preload=True)
raw.resample(100, npad='auto') # much faster estimation
events = find_events(raw, stim_channel='STI 014')
event_ids = [1, 2, 3, 4]
reject = dict(mag=4e-12)
# cov with merged events and keep_sample_mean=True
events_merged = merge_events(events, event_ids, 1234)
# we need a few channels for numerical reasons in PCA/FA
picks = pick_types(raw.info, meg='mag', eeg=False)[:10]
raw.pick_channels([raw.ch_names[pick] for pick in picks])
raw.info.normalize_proj()
epochs = Epochs(
raw, events_merged, 1234, tmin=-0.2, tmax=0,
baseline=(-0.2, -0.1), proj=True, reject=reject, preload=True)
epochs = epochs.crop(None, 0)[:10]
method_params = dict(factor_analysis=dict(iter_n_components=[3]),
pca=dict(iter_n_components=[3]))
covs = compute_covariance(epochs, method='auto',
method_params=method_params,
projs=True,
return_estimators=True)
logliks = [c['loglik'] for c in covs]
assert_true(np.diff(logliks).max() <= 0) # descending order
methods = ['empirical',
'factor_analysis',
'ledoit_wolf',
'pca']
cov3 = compute_covariance(epochs, method=methods,
method_params=method_params, projs=None,
return_estimators=True)
assert_equal(set([c['method'] for c in cov3]),
set(methods))
# invalid prespecified method
assert_raises(ValueError, compute_covariance, epochs, method='pizza')
# invalid scalings
assert_raises(ValueError, compute_covariance, epochs, method='shrunk',
scalings=dict(misc=123))
开发者ID:EmanuelaLiaci,项目名称:mne-python,代码行数:48,代码来源:test_cov.py
示例9: run_evoked
def run_evoked(subject_id):
subject = "sub%03d" % subject_id
print("processing subject: %s" % subject)
data_path = op.join(meg_dir, subject)
epochs = mne.read_epochs(op.join(data_path, '%s-epo.fif' % subject),
preload=False)
evoked_famous = epochs['face/famous'].average()
evoked_scrambled = epochs['scrambled'].average()
evoked_unfamiliar = epochs['face/unfamiliar'].average()
# Simplify comment
evoked_famous.comment = 'famous'
evoked_scrambled.comment = 'scrambled'
evoked_unfamiliar.comment = 'unfamiliar'
contrast = mne.combine_evoked([evoked_famous, evoked_unfamiliar,
evoked_scrambled], weights=[0.5, 0.5, -1.])
contrast.comment = 'contrast'
faces = mne.combine_evoked([evoked_famous, evoked_unfamiliar], 'nave')
faces.comment = 'faces'
mne.evoked.write_evokeds(op.join(data_path, '%s-ave.fif' % subject),
[evoked_famous, evoked_scrambled,
evoked_unfamiliar, contrast, faces])
# take care of noise cov
cov = mne.compute_covariance(epochs, tmax=0, method='shrunk')
cov.save(op.join(data_path, '%s-cov.fif' % subject))
开发者ID:mne-tools,项目名称:mne-biomag-group-demo,代码行数:30,代码来源:06-make_evoked.py
示例10: test_low_rank_methods
def test_low_rank_methods(rank, raw_epochs_events):
"""Test low-rank covariance matrix estimation."""
epochs = raw_epochs_events[1]
sss_proj_rank = 139 # 80 MEG + 60 EEG - 1 proj
n_ch = 366
methods = ('empirical', 'diagonal_fixed', 'oas')
bounds = {
'None': dict(empirical=(-15000, -5000),
diagonal_fixed=(-1500, -500),
oas=(-700, -600)),
'full': dict(empirical=(-18000, -8000),
diagonal_fixed=(-2000, -1600),
oas=(-1600, -1000)),
'info': dict(empirical=(-15000, -5000),
diagonal_fixed=(-700, -600),
oas=(-700, -600)),
}
with pytest.warns(RuntimeWarning, match='Too few samples'):
covs = compute_covariance(
epochs, method=methods, return_estimators=True, rank=rank,
verbose=True)
for cov in covs:
method = cov['method']
these_bounds = bounds[str(rank)][method]
this_rank = _cov_rank(cov, epochs.info, proj=(rank != 'full'))
if rank == 'full' and method != 'empirical':
assert this_rank == n_ch
else:
assert this_rank == sss_proj_rank
assert these_bounds[0] < cov['loglik'] < these_bounds[1], \
(rank, method)
开发者ID:Eric89GXL,项目名称:mne-python,代码行数:31,代码来源:test_cov.py
示例11: test_lcmv_reg_proj
def test_lcmv_reg_proj(proj):
"""Test LCMV with and without proj."""
raw = mne.io.read_raw_fif(fname_raw, preload=True)
events = mne.find_events(raw)
raw.pick_types()
assert len(raw.ch_names) == 305
epochs = mne.Epochs(raw, events, None, preload=True, proj=proj)
with pytest.warns(RuntimeWarning, match='Too few samples'):
noise_cov = mne.compute_covariance(epochs, tmax=0)
data_cov = mne.compute_covariance(epochs, tmin=0.04, tmax=0.15)
forward = mne.read_forward_solution(fname_fwd)
filters = make_lcmv(epochs.info, forward, data_cov, reg=0.05,
noise_cov=noise_cov, pick_ori='max-power',
weight_norm='nai', rank=None, verbose=True)
want_rank = 302 # 305 good channels - 3 MEG projs
assert filters['rank'] == want_rank
开发者ID:pmolfese,项目名称:mne-python,代码行数:16,代码来源:test_lcmv.py
示例12: test_lcmv
def test_lcmv():
"""Test LCMV
"""
event_id, tmin, tmax = 1, -0.2, 0.2
# Setup for reading the raw data
raw.info['bads'] = ['MEG 2443', 'EEG 053'] # 2 bads channels
# Set up pick list: EEG + MEG - bad channels (modify to your needs)
left_temporal_channels = mne.read_selection('Left-temporal')
picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True,
exclude=raw.info['bads'], selection=left_temporal_channels)
# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks, baseline=(None, 0), preload=True,
reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))
evoked = epochs.average()
noise_cov = mne.read_cov(fname_cov)
noise_cov = mne.cov.regularize(noise_cov, evoked.info,
mag=0.05, grad=0.05, eeg=0.1, proj=True)
data_cov = mne.compute_covariance(epochs, tmin=0.04, tmax=0.15)
stc = lcmv(evoked, forward, noise_cov, data_cov, reg=0.01)
stc_pow = np.sum(stc.data, axis=1)
idx = np.argmax(stc_pow)
max_stc = stc.data[idx]
tmax = stc.times[np.argmax(max_stc)]
assert_true(0.09 < tmax < 0.1)
assert_true(2. < np.max(max_stc) < 3.)
开发者ID:baca790,项目名称:mne-python,代码行数:33,代码来源:test_lcmv.py
示例13: _get_bf_data
def _get_bf_data(save_fieldtrip=False):
raw, epochs, evoked, data_cov, _, _, _, _, _, fwd = _get_data(proj=False)
if save_fieldtrip is True:
# raw needs to be saved with all channels and picked in FieldTrip
raw.save(op.join(ft_data_path, 'raw.fif'), overwrite=True)
# src (tris are not available in fwd['src'] once imported into MATLAB)
src = fwd['src'].copy()
mne.write_source_spaces(op.join(ft_data_path, 'src.fif'), src)
# pick gradiometers only:
epochs.pick_types(meg='grad')
evoked.pick_types(meg='grad')
# compute covariance matrix
data_cov = mne.compute_covariance(epochs, tmin=0.04, tmax=0.145,
method='empirical')
if save_fieldtrip is True:
# if the covariance matrix and epochs need resaving:
# data covariance:
cov_savepath = op.join(ft_data_path, 'sample_cov')
sample_cov = {'sample_cov': data_cov['data']}
savemat(cov_savepath, sample_cov)
# evoked data:
ev_savepath = op.join(ft_data_path, 'sample_evoked')
data_ev = {'sample_evoked': evoked.data}
savemat(ev_savepath, data_ev)
return evoked, data_cov, fwd
开发者ID:Eric89GXL,项目名称:mne-python,代码行数:31,代码来源:test_external.py
示例14: _get_data
def _get_data(tmin=-0.1, tmax=0.15, all_forward=True, epochs=True,
epochs_preload=True, data_cov=True):
"""Read in data used in tests."""
label = mne.read_label(fname_label)
events = mne.read_events(fname_event)
raw = mne.io.read_raw_fif(fname_raw, preload=True)
forward = mne.read_forward_solution(fname_fwd)
if all_forward:
forward_surf_ori = _read_forward_solution_meg(
fname_fwd, surf_ori=True)
forward_fixed = _read_forward_solution_meg(
fname_fwd, force_fixed=True, surf_ori=True, use_cps=False)
forward_vol = _read_forward_solution_meg(fname_fwd_vol)
else:
forward_surf_ori = None
forward_fixed = None
forward_vol = None
event_id, tmin, tmax = 1, tmin, tmax
# Setup for reading the raw data
raw.info['bads'] = ['MEG 2443', 'EEG 053'] # 2 bad channels
# Set up pick list: MEG - bad channels
left_temporal_channels = mne.read_selection('Left-temporal')
picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=True,
eog=True, ref_meg=False, exclude='bads',
selection=left_temporal_channels)
raw.pick_channels([raw.ch_names[ii] for ii in picks])
raw.info.normalize_proj() # avoid projection warnings
if epochs:
# Read epochs
epochs = mne.Epochs(
raw, events, event_id, tmin, tmax, proj=True,
baseline=(None, 0), preload=epochs_preload,
reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))
if epochs_preload:
epochs.resample(200, npad=0, n_jobs=2)
epochs.crop(0, None)
evoked = epochs.average()
info = evoked.info
else:
epochs = None
evoked = None
info = raw.info
noise_cov = mne.read_cov(fname_cov)
noise_cov['projs'] = [] # avoid warning
with warnings.catch_warnings(record=True): # bad proj
noise_cov = mne.cov.regularize(noise_cov, info, mag=0.05, grad=0.05,
eeg=0.1, proj=True)
if data_cov:
with warnings.catch_warnings(record=True): # too few samples
data_cov = mne.compute_covariance(epochs, tmin=0.04, tmax=0.145)
else:
data_cov = None
return raw, epochs, evoked, data_cov, noise_cov, label, forward,\
forward_surf_ori, forward_fixed, forward_vol
开发者ID:HSMin,项目名称:mne-python,代码行数:59,代码来源:test_lcmv.py
示例15: calc_inverse_operator
def calc_inverse_operator(events_id, epochs_fn, fwd_sub_fn, inv_fn, min_crop_t=None, max_crop_t=0):
for cond in events_id.keys():
epochs = mne.read_epochs(epochs_fn.format(cond=cond))
noise_cov = mne.compute_covariance(epochs.crop(min_crop_t, max_crop_t, copy=True))
forward_sub = mne.read_forward_solution(fwd_sub_fn.format(cond=cond))
inverse_operator_sub = make_inverse_operator(epochs.info, forward_sub, noise_cov,
loose=None, depth=None)
write_inverse_operator(inv_fn.format(cond=cond), inverse_operator_sub)
开发者ID:ofek-schechner,项目名称:mmvt,代码行数:8,代码来源:subcortical_meg_reconstruction.py
示例16: compute_epochs_cov_evokeds
def compute_epochs_cov_evokeds(subject):
"""Epoch, compute noise covariance and average.
params:
subject : str
the subject id to be loaded
"""
raw = Raw(save_folder + "%s_filtered_ica_mc_raw_tsss.fif" % subject,
preload=True)
# Select events to extract epochs from.
event_id = {'ent_left': 1,
'ent_right': 2,
'ctl_left': 4,
'ctl_right': 8}
# Setup for reading the raw data
events = mne.find_events(raw, min_duration=0.01)
picks = mne.pick_types(raw.info, meg=True, eeg=True, stim=False, eog=False,
include=include, exclude='bads')
# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), reject=reject,
preload=True)
epochs.save(epochs_folder + "%s_filtered_ica_mc_tsss-epo.fif" % subject)
# Plot epochs.
# epochs.plot(trellis=False)
# Look at channels that caused dropped events, showing that the subject's
# blinks were likely to blame for most epochs being dropped
epochs.drop_bad_epochs()
fig = epochs.plot_drop_log(subject=subject, show=False)
fig.savefig(epochs_folder + "pics/%s_drop_log.png" % subject)
# Make noise cov
cov = compute_covariance(epochs, tmin=None, tmax=0, method="auto")
mne.write_cov(epochs_folder + "%s-cov.fif" % subject, cov)
# Average epochs and get evoked data corresponding to the left stimulation
###########################################################################
# Save evoked responses for different conditions to disk
# average epochs and get Evoked datasets
evokeds = [epochs[cond].average() for cond in ['ent_left', 'ent_right',
'ctl_left', 'ctl_right']]
evokeds = [epochs[cond].average() for cond in epochs.event_id.keys()]
# save evoked data to disk
mne.write_evokeds(epochs_folder +
'%s_filtered_ica_mc_raw_tsss-ave.fif' % subject, evokeds)
plt.close("all")
开发者ID:MadsJensen,项目名称:malthe_alpha_project,代码行数:55,代码来源:epochs_cov_evoked.py
示例17: _get_data
def _get_data(tmin=-0.1, tmax=0.15, all_forward=True, epochs=True,
epochs_preload=True, data_cov=True):
"""Read in data used in tests
"""
label = mne.read_label(fname_label)
events = mne.read_events(fname_event)
raw = mne.fiff.Raw(fname_raw, preload=True)
forward = mne.read_forward_solution(fname_fwd)
if all_forward:
forward_surf_ori = mne.read_forward_solution(fname_fwd, surf_ori=True)
forward_fixed = mne.read_forward_solution(fname_fwd, force_fixed=True,
surf_ori=True)
forward_vol = mne.read_forward_solution(fname_fwd_vol, surf_ori=True)
else:
forward_surf_ori = None
forward_fixed = None
forward_vol = None
event_id, tmin, tmax = 1, tmin, tmax
# Setup for reading the raw data
raw.info['bads'] = ['MEG 2443', 'EEG 053'] # 2 bads channels
if epochs:
# Set up pick list: MEG - bad channels
left_temporal_channels = mne.read_selection('Left-temporal')
picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False,
stim=True, eog=True, ref_meg=False,
exclude='bads',
selection=left_temporal_channels)
# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks, baseline=(None, 0),
preload=epochs_preload,
reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))
if epochs_preload:
epochs.resample(200, npad=0, n_jobs=2)
evoked = epochs.average()
info = evoked.info
else:
epochs = None
evoked = None
info = raw.info
noise_cov = mne.read_cov(fname_cov)
noise_cov = mne.cov.regularize(noise_cov, info,
mag=0.05, grad=0.05, eeg=0.1, proj=True)
if data_cov:
data_cov = mne.compute_covariance(epochs, tmin=0.04, tmax=0.15)
else:
data_cov = None
return raw, epochs, evoked, data_cov, noise_cov, label, forward,\
forward_surf_ori, forward_fixed, forward_vol
开发者ID:Anevar,项目名称:mne-python,代码行数:55,代码来源:test_lcmv.py
示例18: test_lcmv
def test_lcmv():
"""Test LCMV with evoked data and single trials
"""
event_id, tmin, tmax = 1, -0.1, 0.15
# Setup for reading the raw data
raw.info['bads'] = ['MEG 2443', 'EEG 053'] # 2 bads channels
# Set up pick list: EEG + MEG - bad channels (modify to your needs)
left_temporal_channels = mne.read_selection('Left-temporal')
picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False,
stim=True, eog=True, exclude='bads',
selection=left_temporal_channels)
# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks, baseline=(None, 0), preload=True,
reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))
epochs.resample(200, npad=0, n_jobs=2)
evoked = epochs.average()
noise_cov = mne.read_cov(fname_cov)
noise_cov = mne.cov.regularize(noise_cov, evoked.info,
mag=0.05, grad=0.05, eeg=0.1, proj=True)
data_cov = mne.compute_covariance(epochs, tmin=0.04, tmax=0.15)
stc = lcmv(evoked, forward, noise_cov, data_cov, reg=0.01)
stc_pow = np.sum(stc.data, axis=1)
idx = np.argmax(stc_pow)
max_stc = stc.data[idx]
tmax = stc.times[np.argmax(max_stc)]
assert_true(0.09 < tmax < 0.1)
assert_true(2. < np.max(max_stc) < 3.)
# Now test single trial using fixed orientation forward solution
# so we can compare it to the evoked solution
forward_fixed = mne.read_forward_solution(fname_fwd, force_fixed=True,
surf_ori=True)
stcs = lcmv_epochs(epochs, forward_fixed, noise_cov, data_cov, reg=0.01)
epochs.drop_bad_epochs()
assert_true(len(epochs.events) == len(stcs))
# average the single trial estimates
stc_avg = np.zeros_like(stc.data)
for this_stc in stcs:
stc_avg += this_stc.data
stc_avg /= len(stcs)
# compare it to the solution using evoked with fixed orientation
stc_fixed = lcmv(evoked, forward_fixed, noise_cov, data_cov, reg=0.01)
assert_array_almost_equal(stc_avg, stc_fixed.data)
开发者ID:cdamon,项目名称:mne-python,代码行数:54,代码来源:test_lcmv.py
示例19: test_cov_estimation_with_triggers
def test_cov_estimation_with_triggers():
"""Test estimation from raw with triggers
"""
events = find_events(raw)
event_ids = [1, 2, 3, 4]
reject = dict(grad=10000e-13, mag=4e-12, eeg=80e-6, eog=150e-6)
# cov with merged events and keep_sample_mean=True
events_merged = merge_events(events, event_ids, 1234)
epochs = Epochs(raw, events_merged, 1234, tmin=-0.2, tmax=0,
baseline=(-0.2, -0.1), proj=True,
reject=reject, preload=True)
cov = compute_covariance(epochs, keep_sample_mean=True)
cov_mne = read_cov(cov_km_fname)
assert_true(cov_mne.ch_names == cov.ch_names)
assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro')
/ linalg.norm(cov.data, ord='fro')) < 0.005)
# Test with tmin and tmax (different but not too much)
cov_tmin_tmax = compute_covariance(epochs, tmin=-0.19, tmax=-0.01)
assert_true(np.all(cov.data != cov_tmin_tmax.data))
assert_true((linalg.norm(cov.data - cov_tmin_tmax.data, ord='fro')
/ linalg.norm(cov_tmin_tmax.data, ord='fro')) < 0.05)
# cov using a list of epochs and keep_sample_mean=True
epochs = [Epochs(raw, events, ev_id, tmin=-0.2, tmax=0,
baseline=(-0.2, -0.1), proj=True, reject=reject)
for ev_id in event_ids]
cov2 = compute_covariance(epochs, keep_sample_mean=True)
assert_array_almost_equal(cov.data, cov2.data)
assert_true(cov.ch_names == cov2.ch_names)
# cov with keep_sample_mean=False using a list of epochs
cov = compute_covariance(epochs, keep_sample_mean=False)
cov_mne = read_cov(cov_fname)
assert_true(cov_mne.ch_names == cov.ch_names)
assert_true((linalg.norm(cov.data - cov_mne.data, ord='fro')
/ linalg.norm(cov.data, ord='fro')) < 0.005)
# test IO when computation done in Python
cov.save('test-cov.fif') # test saving
cov_read = read_cov('test-cov.fif')
assert_true(cov_read.ch_names == cov.ch_names)
assert_true(cov_read.nfree == cov.nfree)
assert_true((linalg.norm(cov.data - cov_read.data, ord='fro')
/ linalg.norm(cov.data, ord='fro')) < 1e-5)
# cov with list of epochs with different projectors
epochs = [Epochs(raw, events[:4], event_ids[0], tmin=-0.2, tmax=0,
baseline=(-0.2, -0.1), proj=True, reject=reject),
Epochs(raw, events[:4], event_ids[0], tmin=-0.2, tmax=0,
baseline=(-0.2, -0.1), proj=False, reject=reject)]
# these should fail
assert_raises(ValueError, compute_covariance, epochs)
assert_raises(ValueError, compute_covariance, epochs, projs=None)
# these should work, but won't be equal to above
cov = compute_covariance(epochs, projs=epochs[0].info['projs'])
cov = compute_covariance(epochs, projs=[])
开发者ID:starzynski,项目名称:mne-python,代码行数:60,代码来源:test_cov.py
示例20: localize_epochs
def localize_epochs(epochs, fwd, reg=0):
''' Returns a list of Sourceestimates, one per Epoch '''
cov = mne.compute_covariance(epochs)
weights = calculate_weights(fwd, cov, reg=reg)
stcs = []
print 'Multiplying data by beamformer weights...'
for epoch in epochs:
sol = np.dot(weights, epoch)
src = mne.SourceEstimate(sol, [fwd['src'][0]['vertno'], fwd['src'][1]['vertno']], epochs.tmin, epochs.times[1] - epochs.times[0])
stcs.append(src)
return stcs
开发者ID:gsudre,项目名称:research_code,代码行数:13,代码来源:virtual_electrode.py
注:本文中的mne.compute_covariance函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论