本文整理汇总了Python中mne.read_dipole函数的典型用法代码示例。如果您正苦于以下问题:Python read_dipole函数的具体用法?Python read_dipole怎么用?Python read_dipole使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_dipole函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_io_dipoles
def test_io_dipoles(tmpdir):
"""Test IO for .dip files."""
dipole = read_dipole(fname_dip)
assert 'Dipole ' in repr(dipole) # test repr
out_fname = op.join(str(tmpdir), 'temp.dip')
dipole.save(out_fname)
dipole_new = read_dipole(out_fname)
_compare_dipoles(dipole, dipole_new)
示例2: test_io_dipoles
def test_io_dipoles():
"""Test IO for .dip files."""
tempdir = _TempDir()
dipole = read_dipole(fname_dip)
print(dipole) # test repr
out_fname = op.join(tempdir, 'temp.dip')
dipole.save(out_fname)
dipole_new = read_dipole(out_fname)
_compare_dipoles(dipole, dipole_new)
示例3: test_dipole_fixed
def test_dipole_fixed():
"""Test reading a fixed-position dipole (from Xfit)."""
dip = read_dipole(fname_xfit_dip)
_check_roundtrip_fixed(dip)
with warnings.catch_warnings(record=True) as w: # unused fields
dip_txt = read_dipole(fname_xfit_dip_txt)
assert_true(any('extra fields' in str(ww.message) for ww in w))
assert_allclose(dip.info['chs'][0]['loc'][:3], dip_txt.pos[0])
assert_allclose(dip_txt.amplitude[0], 12.1e-9)
with warnings.catch_warnings(record=True): # unused fields
dip_txt_seq = read_dipole(fname_xfit_seq_txt)
assert_allclose(dip_txt_seq.gof, [27.3, 46.4, 43.7, 41., 37.3, 32.5])
示例4: test_dipole_fixed
def test_dipole_fixed():
"""Test reading a fixed-position dipole (from Xfit)."""
dip = read_dipole(fname_xfit_dip)
# print the representation of the object DipoleFixed
print(dip)
_check_roundtrip_fixed(dip)
with pytest.warns(RuntimeWarning, match='extra fields'):
dip_txt = read_dipole(fname_xfit_dip_txt)
assert_allclose(dip.info['chs'][0]['loc'][:3], dip_txt.pos[0])
assert_allclose(dip_txt.amplitude[0], 12.1e-9)
with pytest.warns(RuntimeWarning, match='extra fields'):
dip_txt_seq = read_dipole(fname_xfit_seq_txt)
assert_allclose(dip_txt_seq.gof, [27.3, 46.4, 43.7, 41., 37.3, 32.5])
示例5: test_dipole_fixed
def test_dipole_fixed():
"""Test reading a fixed-position dipole (from Xfit)"""
tempdir = _TempDir()
dip = read_dipole(fname_xfit_dip)
dip.save(op.join(tempdir, 'test-dip.fif.gz'))
dip_read = read_dipole(op.join(tempdir, 'test-dip.fif.gz'))
assert_allclose(dip_read.data, dip_read.data)
assert_allclose(dip_read.times, dip.times)
assert_equal(dip_read.info['xplotter_layout'], dip.info['xplotter_layout'])
assert_equal(dip_read.ch_names, dip.ch_names)
for ch_1, ch_2 in zip(dip_read.info['chs'], dip.info['chs']):
assert_equal(ch_1['ch_name'], ch_2['ch_name'])
for key in ('loc', 'kind', 'unit_mul', 'range', 'coord_frame', 'unit',
'cal', 'coil_type', 'scanno', 'logno'):
assert_allclose(ch_1[key], ch_2[key], err_msg=key)
示例6: test_plot_dipole_locations
def test_plot_dipole_locations():
"""Test plotting dipole locations."""
dipoles = read_dipole(dip_fname)
trans = read_trans(trans_fname)
dipoles.plot_locations(trans, 'sample', subjects_dir, fig_name='foo')
assert_raises(ValueError, dipoles.plot_locations, trans, 'sample',
subjects_dir, mode='foo')
示例7: test_confidence
def test_confidence(tmpdir):
"""Test confidence limits."""
evoked = read_evokeds(fname_evo_full, 'Left Auditory', baseline=(None, 0))
evoked.crop(0.08, 0.08).pick_types() # MEG-only
cov = make_ad_hoc_cov(evoked.info)
sphere = make_sphere_model((0., 0., 0.04), 0.08)
dip_py = fit_dipole(evoked, cov, sphere)[0]
fname_test = op.join(str(tmpdir), 'temp-dip.txt')
dip_py.save(fname_test)
dip_read = read_dipole(fname_test)
with pytest.warns(RuntimeWarning, match="'noise/ft/cm', 'prob'"):
dip_xfit = read_dipole(fname_dip_xfit)
for dip_check in (dip_py, dip_read):
assert_allclose(dip_check.pos, dip_xfit.pos, atol=5e-4) # < 0.5 mm
assert_allclose(dip_check.gof, dip_xfit.gof, atol=5e-1) # < 0.5%
assert_array_equal(dip_check.nfree, dip_xfit.nfree) # exact match
assert_allclose(dip_check.khi2, dip_xfit.khi2, rtol=2e-2) # 2% miss
assert set(dip_check.conf.keys()) == set(dip_xfit.conf.keys())
for key in sorted(dip_check.conf.keys()):
assert_allclose(dip_check.conf[key], dip_xfit.conf[key],
rtol=1.5e-1, err_msg=key)
示例8: test_len_index_dipoles
def test_len_index_dipoles():
"""Test len and indexing of Dipole objects."""
dipole = read_dipole(fname_dip)
d0 = dipole[0]
d1 = dipole[:1]
_check_dipole(d0, 1)
_check_dipole(d1, 1)
_compare_dipoles(d0, d1)
mask = dipole.gof > 15
idx = np.where(mask)[0]
d_mask = dipole[mask]
_check_dipole(d_mask, 4)
_compare_dipoles(d_mask, dipole[idx])
示例9: _check_roundtrip_fixed
def _check_roundtrip_fixed(dip):
"""Helper to test roundtrip IO for fixed dipoles."""
tempdir = _TempDir()
dip.save(op.join(tempdir, 'test-dip.fif.gz'))
dip_read = read_dipole(op.join(tempdir, 'test-dip.fif.gz'))
assert_allclose(dip_read.data, dip_read.data)
assert_allclose(dip_read.times, dip.times)
assert_equal(dip_read.info['xplotter_layout'], dip.info['xplotter_layout'])
assert_equal(dip_read.ch_names, dip.ch_names)
for ch_1, ch_2 in zip(dip_read.info['chs'], dip.info['chs']):
assert_equal(ch_1['ch_name'], ch_2['ch_name'])
for key in ('loc', 'kind', 'unit_mul', 'range', 'coord_frame', 'unit',
'cal', 'coil_type', 'scanno', 'logno'):
assert_allclose(ch_1[key], ch_2[key], err_msg=key)
示例10: test_confidence
def test_confidence():
"""Test confidence limits."""
tempdir = _TempDir()
evoked = read_evokeds(fname_evo_full, 'Left Auditory', baseline=(None, 0))
evoked.crop(0.08, 0.08).pick_types() # MEG-only
cov = make_ad_hoc_cov(evoked.info)
sphere = make_sphere_model((0., 0., 0.04), 0.08)
dip_py = fit_dipole(evoked, cov, sphere)[0]
fname_test = op.join(tempdir, 'temp-dip.txt')
dip_py.save(fname_test)
dip_read = read_dipole(fname_test)
with warnings.catch_warnings(record=True) as w:
dip_xfit = read_dipole(fname_dip_xfit)
assert_equal(len(w), 1)
assert_true("['noise/ft/cm', 'prob']" in str(w[0].message))
for dip_check in (dip_py, dip_read):
assert_allclose(dip_check.pos, dip_xfit.pos, atol=5e-4) # < 0.5 mm
assert_allclose(dip_check.gof, dip_xfit.gof, atol=5e-1) # < 0.5%
assert_array_equal(dip_check.nfree, dip_xfit.nfree) # exact match
assert_allclose(dip_check.khi2, dip_xfit.khi2, rtol=2e-2) # 2% miss
assert_equal(set(dip_check.conf.keys()), set(dip_xfit.conf.keys()))
for key in sorted(dip_check.conf.keys()):
assert_allclose(dip_check.conf[key], dip_xfit.conf[key],
rtol=1.5e-1, err_msg=key)
示例11: test_plot_dipole_mri_orthoview
def test_plot_dipole_mri_orthoview():
"""Test mpl dipole plotting."""
dipoles = read_dipole(dip_fname)
trans = read_trans(trans_fname)
for coord_frame, idx, show_all in zip(['head', 'mri'],
['gof', 'amplitude'], [True, False]):
fig = dipoles.plot_locations(trans, 'sample', subjects_dir,
coord_frame=coord_frame, idx=idx,
show_all=show_all, mode='orthoview')
fig.canvas.scroll_event(0.5, 0.5, 1) # scroll up
fig.canvas.scroll_event(0.5, 0.5, -1) # scroll down
fig.canvas.key_press_event('up')
fig.canvas.key_press_event('down')
fig.canvas.key_press_event('a') # some other key
ax = plt.subplot(111)
pytest.raises(TypeError, dipoles.plot_locations, trans, 'sample',
subjects_dir, ax=ax)
plt.close('all')
示例12: test_io_dipoles
def test_io_dipoles():
"""Test IO for .dip files
"""
tempdir = _TempDir()
out_fname = op.join(tempdir, 'temp.dip')
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
times, pos, amplitude, ori, gof = read_dip(fname_dip)
assert_true(len(w) >= 1)
assert_true(pos.shape[1] == 3)
assert_true(ori.shape[1] == 3)
assert_true(len(times) == len(pos))
assert_true(len(times) == gof.size)
assert_true(len(times) == amplitude.size)
dipole = Dipole(times, pos, amplitude, ori, gof, 'ALL')
print(dipole) # test repr
dipole.save(out_fname)
dipole_new = read_dipole(out_fname)
_compare_dipoles(dipole, dipole_new)
示例13: test_dipole_fixed
def test_dipole_fixed():
"""Test reading a fixed-position dipole (from Xfit)"""
dip = read_dipole(fname_xfit_dip)
_check_roundtrip_fixed(dip)
示例14: test_dipole_fitting
def test_dipole_fitting():
"""Test dipole fitting."""
amp = 100e-9
tempdir = _TempDir()
rng = np.random.RandomState(0)
fname_dtemp = op.join(tempdir, 'test.dip')
fname_sim = op.join(tempdir, 'test-ave.fif')
fwd = convert_forward_solution(read_forward_solution(fname_fwd),
surf_ori=False, force_fixed=True,
use_cps=True)
evoked = read_evokeds(fname_evo)[0]
cov = read_cov(fname_cov)
n_per_hemi = 5
vertices = [np.sort(rng.permutation(s['vertno'])[:n_per_hemi])
for s in fwd['src']]
nv = sum(len(v) for v in vertices)
stc = SourceEstimate(amp * np.eye(nv), vertices, 0, 0.001)
evoked = simulate_evoked(fwd, stc, evoked.info, cov, nave=evoked.nave,
random_state=rng)
# For speed, let's use a subset of channels (strange but works)
picks = np.sort(np.concatenate([
pick_types(evoked.info, meg=True, eeg=False)[::2],
pick_types(evoked.info, meg=False, eeg=True)[::2]]))
evoked.pick_channels([evoked.ch_names[p] for p in picks])
evoked.add_proj(make_eeg_average_ref_proj(evoked.info))
write_evokeds(fname_sim, evoked)
# Run MNE-C version
run_subprocess([
'mne_dipole_fit', '--meas', fname_sim, '--meg', '--eeg',
'--noise', fname_cov, '--dip', fname_dtemp,
'--mri', fname_fwd, '--reg', '0', '--tmin', '0',
])
dip_c = read_dipole(fname_dtemp)
# Run mne-python version
sphere = make_sphere_model(head_radius=0.1)
with pytest.warns(RuntimeWarning, match='projection'):
dip, residual = fit_dipole(evoked, cov, sphere, fname_fwd)
assert isinstance(residual, Evoked)
# Sanity check: do our residuals have less power than orig data?
data_rms = np.sqrt(np.sum(evoked.data ** 2, axis=0))
resi_rms = np.sqrt(np.sum(residual.data ** 2, axis=0))
assert (data_rms > resi_rms * 0.95).all(), \
'%s (factor: %s)' % ((data_rms / resi_rms).min(), 0.95)
# Compare to original points
transform_surface_to(fwd['src'][0], 'head', fwd['mri_head_t'])
transform_surface_to(fwd['src'][1], 'head', fwd['mri_head_t'])
assert_equal(fwd['src'][0]['coord_frame'], FIFF.FIFFV_COORD_HEAD)
src_rr = np.concatenate([s['rr'][v] for s, v in zip(fwd['src'], vertices)],
axis=0)
src_nn = np.concatenate([s['nn'][v] for s, v in zip(fwd['src'], vertices)],
axis=0)
# MNE-C skips the last "time" point :(
out = dip.crop(dip_c.times[0], dip_c.times[-1])
assert (dip is out)
src_rr, src_nn = src_rr[:-1], src_nn[:-1]
# check that we did about as well
corrs, dists, gc_dists, amp_errs, gofs = [], [], [], [], []
for d in (dip_c, dip):
new = d.pos
diffs = new - src_rr
corrs += [np.corrcoef(src_rr.ravel(), new.ravel())[0, 1]]
dists += [np.sqrt(np.mean(np.sum(diffs * diffs, axis=1)))]
gc_dists += [180 / np.pi * np.mean(np.arccos(np.sum(src_nn * d.ori,
axis=1)))]
amp_errs += [np.sqrt(np.mean((amp - d.amplitude) ** 2))]
gofs += [np.mean(d.gof)]
# XXX possibly some OpenBLAS numerical differences make
# things slightly worse for us
factor = 0.7
assert dists[0] / factor >= dists[1], 'dists: %s' % dists
assert corrs[0] * factor <= corrs[1], 'corrs: %s' % corrs
assert gc_dists[0] / factor >= gc_dists[1] * 0.8, \
'gc-dists (ori): %s' % gc_dists
assert amp_errs[0] / factor >= amp_errs[1],\
'amplitude errors: %s' % amp_errs
# This one is weird because our cov/sim/picking is weird
assert gofs[0] * factor <= gofs[1] * 2, 'gof: %s' % gofs
示例15: test_plot_dipole_amplitudes
def test_plot_dipole_amplitudes():
"""Test plotting dipole amplitudes."""
import matplotlib.pyplot as plt
dipoles = read_dipole(dip_fname)
dipoles.plot_amplitudes(show=False)
plt.close('all')