本文整理汇总了Python中mne.time_frequency.tfr.AverageTFR类的典型用法代码示例。如果您正苦于以下问题:Python AverageTFR类的具体用法?Python AverageTFR怎么用?Python AverageTFR使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AverageTFR类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_channels
def test_add_channels():
"""Test tfr splitting / re-appending channel types
"""
data = np.zeros((6, 2, 3))
times = np.array([0.1, 0.2, 0.3])
freqs = np.array([0.10, 0.20])
info = mne.create_info(
["MEG 001", "MEG 002", "MEG 003", "EEG 001", "EEG 002", "STIM 001"],
1000.0,
["mag", "mag", "mag", "eeg", "eeg", "stim"],
)
tfr = AverageTFR(info, data=data, times=times, freqs=freqs, nave=20, comment="test", method="crazy-tfr")
tfr_eeg = tfr.pick_types(meg=False, eeg=True, copy=True)
tfr_meg = tfr.pick_types(meg=True, copy=True)
tfr_stim = tfr.pick_types(meg=False, stim=True, copy=True)
tfr_eeg_meg = tfr.pick_types(meg=True, eeg=True, copy=True)
tfr_new = tfr_meg.add_channels([tfr_eeg, tfr_stim], copy=True)
assert_true(all(ch in tfr_new.ch_names for ch in tfr_stim.ch_names + tfr_meg.ch_names))
tfr_new = tfr_meg.add_channels([tfr_eeg], copy=True)
assert_true(ch in tfr_new.ch_names for ch in tfr.ch_names)
assert_array_equal(tfr_new.data, tfr_eeg_meg.data)
assert_true(all(ch not in tfr_new.ch_names for ch in tfr_stim.ch_names))
# Now test errors
tfr_badsf = tfr_eeg.copy()
tfr_badsf.info["sfreq"] = 3.1415927
tfr_eeg = tfr_eeg.crop(-0.1, 0.1)
assert_raises(RuntimeError, tfr_meg.add_channels, [tfr_badsf])
assert_raises(AssertionError, tfr_meg.add_channels, [tfr_eeg])
assert_raises(ValueError, tfr_meg.add_channels, [tfr_meg])
assert_raises(AssertionError, tfr_meg.add_channels, tfr_badsf)
示例2: test_add_channels
def test_add_channels():
"""Test tfr splitting / re-appending channel types."""
data = np.zeros((6, 2, 3))
times = np.array([.1, .2, .3])
freqs = np.array([.10, .20])
info = mne.create_info(
['MEG 001', 'MEG 002', 'MEG 003', 'EEG 001', 'EEG 002', 'STIM 001'],
1000., ['mag', 'mag', 'mag', 'eeg', 'eeg', 'stim'])
tfr = AverageTFR(info, data=data, times=times, freqs=freqs,
nave=20, comment='test', method='crazy-tfr')
tfr_eeg = tfr.copy().pick_types(meg=False, eeg=True)
tfr_meg = tfr.copy().pick_types(meg=True)
tfr_stim = tfr.copy().pick_types(meg=False, stim=True)
tfr_eeg_meg = tfr.copy().pick_types(meg=True, eeg=True)
tfr_new = tfr_meg.copy().add_channels([tfr_eeg, tfr_stim])
assert all(ch in tfr_new.ch_names
for ch in tfr_stim.ch_names + tfr_meg.ch_names)
tfr_new = tfr_meg.copy().add_channels([tfr_eeg])
assert all(ch in tfr_new.ch_names
for ch in tfr.ch_names if ch != 'STIM 001')
assert_array_equal(tfr_new.data, tfr_eeg_meg.data)
assert all(ch not in tfr_new.ch_names for ch in tfr_stim.ch_names)
# Now test errors
tfr_badsf = tfr_eeg.copy()
tfr_badsf.info['sfreq'] = 3.1415927
tfr_eeg = tfr_eeg.crop(-.1, .1)
pytest.raises(RuntimeError, tfr_meg.add_channels, [tfr_badsf])
pytest.raises(AssertionError, tfr_meg.add_channels, [tfr_eeg])
pytest.raises(ValueError, tfr_meg.add_channels, [tfr_meg])
pytest.raises(TypeError, tfr_meg.add_channels, tfr_badsf)
示例3: test_plot_tfr_topomap
def test_plot_tfr_topomap():
"""Test plotting of TFR data
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
raw = _get_raw()
times = np.linspace(-0.1, 0.1, 200)
n_freqs = 3
nave = 1
rng = np.random.RandomState(42)
data = rng.randn(len(raw.ch_names), n_freqs, len(times))
tfr = AverageTFR(raw.info, data, times, np.arange(n_freqs), nave)
tfr.plot_topomap(ch_type="mag", tmin=0.05, tmax=0.150, fmin=0, fmax=10, res=16)
eclick = mpl.backend_bases.MouseEvent("button_press_event", plt.gcf().canvas, 0, 0, 1)
eclick.xdata = 0.1
eclick.ydata = 0.1
eclick.inaxes = plt.gca()
erelease = mpl.backend_bases.MouseEvent("button_release_event", plt.gcf().canvas, 0.9, 0.9, 1)
erelease.xdata = 0.3
erelease.ydata = 0.2
pos = [[0.11, 0.11], [0.25, 0.5], [0.0, 0.2], [0.2, 0.39]]
_onselect(eclick, erelease, tfr, pos, "mag", 1, 3, 1, 3, "RdBu_r", list())
tfr._onselect(eclick, erelease, None, "mean", None)
plt.close("all")
示例4: test_crop
def test_crop():
"""Test TFR cropping"""
data = np.zeros((3, 2, 3))
times = np.array([0.1, 0.2, 0.3])
freqs = np.array([0.10, 0.20])
info = mne.create_info(["MEG 001", "MEG 002", "MEG 003"], 1000.0, ["mag", "mag", "mag"])
tfr = AverageTFR(info, data=data, times=times, freqs=freqs, nave=20, comment="test", method="crazy-tfr")
tfr.crop(0.2, 0.3)
assert_array_equal(tfr.times, [0.2, 0.3])
assert_equal(tfr.data.shape[-1], 2)
示例5: test_plot_tfr_topo
def test_plot_tfr_topo():
"""Test plotting of TFR data
"""
epochs = _get_epochs()
n_freqs = 3
nave = 1
data = np.random.randn(len(epochs.ch_names), n_freqs, len(epochs.times))
tfr = AverageTFR(epochs.info, data, epochs.times, np.arange(n_freqs), nave)
tfr.plot_topo(baseline=(None, 0), mode="ratio", title="Average power", vmin=0.0, vmax=14.0)
tfr.plot([4], baseline=(None, 0), mode="ratio")
示例6: test_plot_tfr_topo
def test_plot_tfr_topo():
"""Test plotting of TFR data."""
epochs = _get_epochs()
n_freqs = 3
nave = 1
data = np.random.RandomState(0).randn(len(epochs.ch_names),
n_freqs, len(epochs.times))
tfr = AverageTFR(epochs.info, data, epochs.times, np.arange(n_freqs), nave)
tfr.plot_topo(baseline=(None, 0), mode='ratio', title='Average power',
vmin=0., vmax=14., show=False)
tfr.plot([4], baseline=(None, 0), mode='ratio', show=False, title='foo')
示例7: test_plot_tfr_topomap
def test_plot_tfr_topomap():
"""Test plotting of TFR data
"""
raw = _get_raw()
times = np.linspace(-0.1, 0.1, 200)
n_freqs = 3
nave = 1
rng = np.random.RandomState(42)
data = rng.randn(len(raw.ch_names), n_freqs, len(times))
tfr = AverageTFR(raw.info, data, times, np.arange(n_freqs), nave)
tfr.plot_topomap(ch_type="mag", tmin=0.05, tmax=0.150, fmin=0, fmax=10, res=16)
示例8: test_crop
def test_crop():
"""Test TFR cropping."""
data = np.zeros((3, 2, 3))
times = np.array([.1, .2, .3])
freqs = np.array([.10, .20])
info = mne.create_info(['MEG 001', 'MEG 002', 'MEG 003'], 1000.,
['mag', 'mag', 'mag'])
tfr = AverageTFR(info, data=data, times=times, freqs=freqs,
nave=20, comment='test', method='crazy-tfr')
tfr.crop(0.2, 0.3)
assert_array_equal(tfr.times, [0.2, 0.3])
assert_equal(tfr.data.shape[-1], 2)
示例9: test_plot_tfr_topo
def test_plot_tfr_topo():
"""Test plotting of TFR data."""
import matplotlib.pyplot as plt
epochs = _get_epochs()
n_freqs = 3
nave = 1
data = np.random.RandomState(0).randn(len(epochs.ch_names),
n_freqs, len(epochs.times))
tfr = AverageTFR(epochs.info, data, epochs.times, np.arange(n_freqs), nave)
plt.close('all')
fig = tfr.plot_topo(baseline=(None, 0), mode='ratio',
title='Average power', vmin=0., vmax=14.)
# test opening tfr by clicking
num_figures_before = len(plt.get_fignums())
# could use np.reshape(fig.axes[-1].images[0].get_extent(), (2, 2)).mean(1)
with pytest.warns(None): # on old mpl there is a warning
_fake_click(fig, fig.axes[0], (0.08, 0.65))
assert num_figures_before + 1 == len(plt.get_fignums())
plt.close('all')
tfr.plot([4], baseline=(None, 0), mode='ratio', show=False, title='foo')
pytest.raises(ValueError, tfr.plot, [4], yscale='lin', show=False)
# nonuniform freqs
freqs = np.logspace(*np.log10([3, 10]), num=3)
tfr = AverageTFR(epochs.info, data, epochs.times, freqs, nave)
fig = tfr.plot([4], baseline=(None, 0), mode='mean', vmax=14., show=False)
assert fig.axes[0].get_yaxis().get_scale() == 'log'
# one timesample
tfr = AverageTFR(epochs.info, data[:, :, [0]], epochs.times[[1]],
freqs, nave)
with pytest.warns(None): # matplotlib equal left/right
tfr.plot([4], baseline=None, vmax=14., show=False, yscale='linear')
# one frequency bin, log scale required: as it doesn't make sense
# to plot log scale for one value, we test whether yscale is set to linear
vmin, vmax = 0., 2.
fig, ax = plt.subplots()
tmin, tmax = epochs.times[0], epochs.times[-1]
with pytest.warns(RuntimeWarning, match='not masking'):
_imshow_tfr(ax, 3, tmin, tmax, vmin, vmax, None, tfr=data[:, [0], :],
freq=freqs[[-1]], x_label=None, y_label=None,
colorbar=False, cmap=('RdBu_r', True), yscale='log')
fig = plt.gcf()
assert fig.axes[0].get_yaxis().get_scale() == 'linear'
# ValueError when freq[0] == 0 and yscale == 'log'
these_freqs = freqs[:3].copy()
these_freqs[0] = 0
with pytest.warns(RuntimeWarning, match='not masking'):
pytest.raises(ValueError, _imshow_tfr, ax, 3, tmin, tmax, vmin, vmax,
None, tfr=data[:, :3, :], freq=these_freqs, x_label=None,
y_label=None, colorbar=False, cmap=('RdBu_r', True),
yscale='log')
示例10: test_io
def test_io():
"""Test TFR IO capacities."""
tempdir = _TempDir()
fname = op.join(tempdir, 'test-tfr.h5')
data = np.zeros((3, 2, 3))
times = np.array([.1, .2, .3])
freqs = np.array([.10, .20])
info = mne.create_info(['MEG 001', 'MEG 002', 'MEG 003'], 1000.,
['mag', 'mag', 'mag'])
tfr = AverageTFR(info, data=data, times=times, freqs=freqs,
nave=20, comment='test', method='crazy-tfr')
tfr.save(fname)
tfr2 = read_tfrs(fname, condition='test')
assert_array_equal(tfr.data, tfr2.data)
assert_array_equal(tfr.times, tfr2.times)
assert_array_equal(tfr.freqs, tfr2.freqs)
assert_equal(tfr.comment, tfr2.comment)
assert_equal(tfr.nave, tfr2.nave)
assert_raises(IOError, tfr.save, fname)
tfr.comment = None
tfr.save(fname, overwrite=True)
assert_equal(read_tfrs(fname, condition=0).comment, tfr.comment)
tfr.comment = 'test-A'
tfr2.comment = 'test-B'
fname = op.join(tempdir, 'test2-tfr.h5')
write_tfrs(fname, [tfr, tfr2])
tfr3 = read_tfrs(fname, condition='test-A')
assert_equal(tfr.comment, tfr3.comment)
assert_true(isinstance(tfr.info, mne.Info))
tfrs = read_tfrs(fname, condition=None)
assert_equal(len(tfrs), 2)
tfr4 = tfrs[1]
assert_equal(tfr2.comment, tfr4.comment)
assert_raises(ValueError, read_tfrs, fname, condition='nonono')
# Test save of EpochsTFR.
data = np.zeros((5, 3, 2, 3))
tfr = EpochsTFR(info, data=data, times=times, freqs=freqs,
comment='test', method='crazy-tfr')
tfr.save(fname, True)
read_tfr = read_tfrs(fname)[0]
assert_array_equal(tfr.data, read_tfr.data)
示例11: test_crop
def test_crop():
"""Test TFR cropping."""
data = np.zeros((3, 4, 5))
times = np.array([.1, .2, .3, .4, .5])
freqs = np.array([.10, .20, .30, .40])
info = mne.create_info(['MEG 001', 'MEG 002', 'MEG 003'], 1000.,
['mag', 'mag', 'mag'])
tfr = AverageTFR(info, data=data, times=times, freqs=freqs,
nave=20, comment='test', method='crazy-tfr')
tfr.crop(tmin=0.2)
assert_array_equal(tfr.times, [0.2, 0.3, 0.4, 0.5])
assert tfr.data.ndim == 3
assert tfr.data.shape[-1] == 4
tfr.crop(fmax=0.3)
assert_array_equal(tfr.freqs, [0.1, 0.2, 0.3])
assert tfr.data.ndim == 3
assert tfr.data.shape[-2] == 3
tfr.crop(tmin=0.3, tmax=0.4, fmin=0.1, fmax=0.2)
assert_array_equal(tfr.times, [0.3, 0.4])
assert tfr.data.ndim == 3
assert tfr.data.shape[-1] == 2
assert_array_equal(tfr.freqs, [0.1, 0.2])
assert tfr.data.shape[-2] == 2
示例12: test_plot
def test_plot():
"""Test TFR plotting."""
import matplotlib.pyplot as plt
data = np.zeros((3, 2, 3))
times = np.array([.1, .2, .3])
freqs = np.array([.10, .20])
info = mne.create_info(['MEG 001', 'MEG 002', 'MEG 003'], 1000.,
['mag', 'mag', 'mag'])
tfr = AverageTFR(info, data=data, times=times, freqs=freqs,
nave=20, comment='test', method='crazy-tfr')
tfr.plot([1, 2], title='title', colorbar=False,
mask=np.ones(tfr.data.shape[1:], bool))
plt.close('all')
ax = plt.subplot2grid((2, 2), (0, 0))
ax2 = plt.subplot2grid((2, 2), (1, 1))
ax3 = plt.subplot2grid((2, 2), (0, 1))
tfr.plot(picks=[0, 1, 2], axes=[ax, ax2, ax3])
plt.close('all')
tfr.plot([1, 2], title='title', colorbar=False, exclude='bads')
plt.close('all')
tfr.plot_topo(picks=[1, 2])
plt.close('all')
fig = tfr.plot(picks=[1], cmap='RdBu_r') # interactive mode on by default
fig.canvas.key_press_event('up')
fig.canvas.key_press_event(' ')
fig.canvas.key_press_event('down')
cbar = fig.get_axes()[0].CB # Fake dragging with mouse.
ax = cbar.cbar.ax
_fake_click(fig, ax, (0.1, 0.1))
_fake_click(fig, ax, (0.1, 0.2), kind='motion')
_fake_click(fig, ax, (0.1, 0.3), kind='release')
_fake_click(fig, ax, (0.1, 0.1), button=3)
_fake_click(fig, ax, (0.1, 0.2), button=3, kind='motion')
_fake_click(fig, ax, (0.1, 0.3), kind='release')
fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down
fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up
plt.close('all')
示例13: test_plot_tfr_topomap
def test_plot_tfr_topomap():
"""Test plotting of TFR data."""
import matplotlib as mpl
import matplotlib.pyplot as plt
raw = read_raw_fif(raw_fname)
times = np.linspace(-0.1, 0.1, 200)
res = 8
n_freqs = 3
nave = 1
rng = np.random.RandomState(42)
picks = [93, 94, 96, 97, 21, 22, 24, 25, 129, 130, 315, 316, 2, 5, 8, 11]
info = pick_info(raw.info, picks)
data = rng.randn(len(picks), n_freqs, len(times))
tfr = AverageTFR(info, data, times, np.arange(n_freqs), nave)
tfr.plot_topomap(ch_type='mag', tmin=0.05, tmax=0.150, fmin=0, fmax=10,
res=res, contours=0)
eclick = mpl.backend_bases.MouseEvent('button_press_event',
plt.gcf().canvas, 0, 0, 1)
eclick.xdata = eclick.ydata = 0.1
eclick.inaxes = plt.gca()
erelease = mpl.backend_bases.MouseEvent('button_release_event',
plt.gcf().canvas, 0.9, 0.9, 1)
erelease.xdata = 0.3
erelease.ydata = 0.2
pos = [[0.11, 0.11], [0.25, 0.5], [0.0, 0.2], [0.2, 0.39]]
_onselect(eclick, erelease, tfr, pos, 'grad', 1, 3, 1, 3, 'RdBu_r', list())
_onselect(eclick, erelease, tfr, pos, 'mag', 1, 3, 1, 3, 'RdBu_r', list())
eclick.xdata = eclick.ydata = 0.
erelease.xdata = erelease.ydata = 0.9
tfr._onselect(eclick, erelease, None, 'mean', None)
plt.close('all')
# test plot_psds_topomap
info = raw.info.copy()
chan_inds = channel_indices_by_type(info)
info = pick_info(info, chan_inds['grad'][:4])
fig, axes = plt.subplots()
freqs = np.arange(3., 9.5)
bands = [(4, 8, 'Theta')]
psd = np.random.rand(len(info['ch_names']), freqs.shape[0])
plot_psds_topomap(psd, freqs, info, bands=bands, axes=[axes])
示例14: test_plot
def test_plot():
"""Test TFR plotting."""
import matplotlib.pyplot as plt
data = np.zeros((3, 2, 3))
times = np.array([0.1, 0.2, 0.3])
freqs = np.array([0.10, 0.20])
info = mne.create_info(["MEG 001", "MEG 002", "MEG 003"], 1000.0, ["mag", "mag", "mag"])
tfr = AverageTFR(info, data=data, times=times, freqs=freqs, nave=20, comment="test", method="crazy-tfr")
tfr.plot([1, 2], title="title")
plt.close("all")
ax = plt.subplot2grid((2, 2), (0, 0))
ax2 = plt.subplot2grid((2, 2), (1, 1))
ax3 = plt.subplot2grid((2, 2), (0, 1))
tfr.plot(picks=[0, 1, 2], axes=[ax, ax2, ax3])
plt.close("all")
tfr.plot_topo(picks=[1, 2])
plt.close("all")
tfr.plot_topo(picks=[1, 2])
plt.close("all")
示例15: test_plot_joint
def test_plot_joint():
"""Test TFR joint plotting."""
import matplotlib.pyplot as plt
raw = read_raw_fif(raw_fname)
times = np.linspace(-0.1, 0.1, 200)
n_freqs = 3
nave = 1
rng = np.random.RandomState(42)
data = rng.randn(len(raw.ch_names), n_freqs, len(times))
tfr = AverageTFR(raw.info, data, times, np.arange(n_freqs), nave)
topomap_args = {'res': 8, 'contours': 0, 'sensors': False}
for combine in ('mean', 'rms', None):
tfr.plot_joint(title='auto', colorbar=True,
combine=combine, topomap_args=topomap_args)
plt.close('all')
# check various timefreqs
for timefreqs in (
{(tfr.times[0], tfr.freqs[1]): (0.1, 0.5),
(tfr.times[-1], tfr.freqs[-1]): (0.2, 0.6)},
[(tfr.times[1], tfr.freqs[1])]):
tfr.plot_joint(timefreqs=timefreqs, topomap_args=topomap_args)
plt.close('all')
# test bad timefreqs
timefreqs = ([(-100, 1)], tfr.times[1], [1],
[(tfr.times[1], tfr.freqs[1], tfr.freqs[1])])
for these_timefreqs in timefreqs:
assert_raises(ValueError, tfr.plot_joint, these_timefreqs)
# test that the object is not internally modified
tfr_orig = tfr.copy()
tfr.plot_joint(baseline=(0, None), exclude=[tfr.ch_names[0]],
topomap_args=topomap_args)
plt.close('all')
assert_array_equal(tfr.data, tfr_orig.data)
assert_true(set(tfr.ch_names) == set(tfr_orig.ch_names))
assert_true(set(tfr.times) == set(tfr_orig.times))