本文整理汇总了Python中mne.io.Raw.filter方法的典型用法代码示例。如果您正苦于以下问题:Python Raw.filter方法的具体用法?Python Raw.filter怎么用?Python Raw.filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mne.io.Raw
的用法示例。
在下文中一共展示了Raw.filter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter_data
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def filter_data(subject, l_freq=l_freq, h_freq=h_freq, n_freq=n_freq,
save=True, n_jobs=1):
"""Filter the data.
params:
subject : str
the subject id to be loaded
l_freq : int
the low frequency to filter
h_freq : int
the high frequency to filter
n_freq : int
the notch filter frequency
save : bool
save the filtered data
n_jobs : int
The number of CPUs to use in parallel.
"""
raw = Raw(maxfiltered_folder + "%s_data_mc_raw_tsss.fif" % subject,
preload=True)
if n_freq is not None:
raw.notch_filter(n_freq, n_jobs=n_jobs)
raw.filter(l_freq, h_freq, n_jobs=n_jobs)
if save is True:
raw.save(save_folder + "%s_filtered_data_mc_raw_tsss.fif" % subject,
overwrite=True)
示例2: test_memmap
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def test_memmap():
fname='ec_rest_before_tsss_mc_rsl.fif'
raw = Raw(fname, preload=False)
raw.preload_data() # data becomes numpy.float64
data_shape = raw._data.shape
tmpdir = mkdtemp(dir='/Users/cjb/tmp')
mmap_fname = opj(tmpdir, 'raw_data.dat')
fp = np.memmap(mmap_fname, dtype='float64', mode='w+',
shape=data_shape)
print('Contents of raw._data:')
print(raw._data[0][:10])
print('Contents of memmap:')
print(fp[0][:10])
fp[:] = raw._data[:]
print('Contents of memmap after assignment:')
print(fp[0][:10])
# delete numpy array and the memmap writer
del raw._data
del fp
raw._data = np.memmap(mmap_fname, dtype='float64', mode='r+',
shape=data_shape)
print('Contents of raw._data after loading from memmap:')
print(raw._data[0][:10])
raw.filter(None,40)
print('Contents of raw._data after filtering:')
print(raw._data[0][:10])
示例3: filter_raw
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def filter_raw():
fname='ec_rest_before_tsss_mc_rsl.fif'
raw = Raw(fname, preload=False)
raw.preload_data() # data becomes numpy.float64
raw.filter(None, 40, n_jobs=4)
del raw
fname='ec_rest_after_tsss_mc_rsl.fif'
raw2 = Raw(fname, preload=False)
raw2.preload_data() # data becomes numpy.float64
raw2.filter(None, 40, n_jobs=4)
del raw2
示例4: generate_data_for_comparing_against_eeglab_infomax
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def generate_data_for_comparing_against_eeglab_infomax(ch_type, random_state):
data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample')
raw_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif')
raw = Raw(raw_fname, preload=True)
if ch_type == 'eeg':
picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads')
else:
picks = pick_types(raw.info, meg=ch_type,
eeg=False, exclude='bads')
# select a small number of channels for the test
number_of_channels_to_use = 5
idx_perm = random_permutation(picks.shape[0], random_state)
picks = picks[idx_perm[:number_of_channels_to_use]]
with warnings.catch_warnings(record=True): # deprecated params
raw.filter(1, 45, picks=picks)
# Eventually we will need to add these, but for now having none of
# them is a nice deprecation sanity check.
# filter_length='10s',
# l_trans_bandwidth=0.5, h_trans_bandwidth=0.5,
# phase='zero-double', fir_window='hann') # use the old way
X = raw[picks, :][0][:, ::20]
# Subtract the mean
mean_X = X.mean(axis=1)
X -= mean_X[:, None]
# pre_whitening: z-score
X /= np.std(X)
T = X.shape[1]
cov_X = np.dot(X, X.T) / T
# Let's whiten the data
U, D, _ = svd(cov_X)
W = np.dot(U, U.T / np.sqrt(D)[:, None])
Y = np.dot(W, X)
return Y
示例5: test_preload_memmap
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def test_preload_memmap():
tmpdir = mkdtemp(dir="/Users/cjb/tmp")
mmap_fname = opj(tmpdir, "raw_data.dat")
# fname='ec_rest_before_tsss_mc_rsl.fif'
data_path = sample.data_path(download=False)
fname = data_path + "/MEG/sample/sample_audvis_raw.fif"
raw = Raw(fname, preload=False)
# """This function actually preloads the data"""
# data_buffer = mmap_fname
# raw._data = raw._read_segment(data_buffer=data_buffer)[0]
# assert len(raw._data) == raw.info['nchan']
# raw.preload = True
# raw.close()
raw.preload_data(data_buffer=mmap_fname)
data_shape = raw._data.shape
print("Contents of raw._data after reading from fif:")
print(type(raw._data))
print(raw._data[100][:5])
del raw._data
raw._data = np.memmap(mmap_fname, dtype="float64", mode="c", shape=data_shape)
print("Contents of raw._data after RE-loading:")
print(type(raw._data))
print(raw._data[100][:5])
raw.filter(None, 40)
print("Contents of raw._data after filtering:")
print(type(raw._data))
print(raw._data[100][:5])
# PROBLEM: Now the filtered data are IN MEMORY, but as a memmap
# What if I'd like to continue from here using it as an ndarray?
del raw._data
rmtree(tmpdir, ignore_errors=True)
示例6: generate_data_for_comparing_against_eeglab_infomax
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def generate_data_for_comparing_against_eeglab_infomax(ch_type, random_state):
data_dir = op.join(testing.data_path(download=False), 'MEG', 'sample')
raw_fname = op.join(data_dir, 'sample_audvis_trunc_raw.fif')
raw = Raw(raw_fname, preload=True)
if ch_type == 'eeg':
picks = pick_types(raw.info, meg=False, eeg=True, exclude='bads')
else:
picks = pick_types(raw.info, meg=ch_type,
eeg=False, exclude='bads')
# select a small number of channels for the test
number_of_channels_to_use = 5
idx_perm = random_permutation(picks.shape[0], random_state)
picks = picks[idx_perm[:number_of_channels_to_use]]
raw.filter(1, 45, n_jobs=2)
X = raw[picks, :][0][:, ::20]
# Substract the mean
mean_X = X.mean(axis=1)
X -= mean_X[:, None]
# pre_whitening: z-score
X /= np.std(X)
T = X.shape[1]
cov_X = np.dot(X, X.T) / T
# Let's whiten the data
U, D, _ = svd(cov_X)
W = np.dot(U, U.T / np.sqrt(D)[:, None])
Y = np.dot(W, X)
return Y
示例7: preprocess_set_ICA_comp_fif_to_ts
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def preprocess_set_ICA_comp_fif_to_ts(fif_file, n_comp_exclude, l_freq, h_freq,
down_sfreq, is_sensor_space):
import os
import numpy as np
import sys
import mne
from mne.io import Raw
from mne.preprocessing import read_ica
from mne.report import Report
from nipype.utils.filemanip import split_filename as split_f
report = Report()
subj_path, basename, ext = split_f(fif_file)
(data_path, sbj_name) = os.path.split(subj_path)
print '*** SBJ %s' % sbj_name + '***'
# n_session = int(filter(str.isdigit, basename))
# print '*** n session = %d' % n_session + '***'
# Read raw
raw = Raw(fif_file, preload=True)
# select sensors
select_sensors = mne.pick_types(raw.info, meg=True, ref_meg=False,
exclude='bads')
picks_meeg = mne.pick_types(raw.info, meg=True, eeg=True,
exclude='bads')
# save electrode locations
sens_loc = [raw.info['chs'][i]['loc'][:3] for i in select_sensors]
sens_loc = np.array(sens_loc)
channel_coords_file = os.path.abspath("correct_channel_coords.txt")
np.savetxt(channel_coords_file, sens_loc, fmt='%s')
# save electrode names
sens_names = np.array([raw.ch_names[pos] for pos in select_sensors],
dtype="str")
channel_names_file = os.path.abspath("correct_channel_names.txt")
np.savetxt(channel_names_file, sens_names, fmt='%s')
# filtering + downsampling
# TODO n_jobs=8
raw.filter(l_freq=l_freq, h_freq=h_freq, picks=picks_meeg,
method='iir',n_jobs=8)
# raw.resample(sfreq=down_sfreq, npad=0)
# load ICA
is_show = False # visualization
ica_filename = os.path.join(subj_path, basename + '-ica.fif')
if os.path.exists(ica_filename) is False:
print "$$$ Warning, no %s found" % ica_filename
sys.exit()
else:
ica = read_ica(ica_filename)
# AP 210316
'''
print '*** ica.exclude before set components= ', ica.exclude
if n_comp_exclude.has_key(sbj_name):
print '*** ICA to be excluded for sbj %s ' % sbj_name + ' ' + str(n_comp_exclude[sbj_name]) + '***'
matrix_c_ICA = n_comp_exclude[sbj_name]
if not matrix_c_ICA[n_session-1]:
print 'no ICA'
else:
print '*** ICA to be excluded for session %d ' %n_session + ' ' + str(matrix_c_ICA[n_session-1]) + '***'
ica.exclude = matrix_c_ICA[n_session-1]
'''
# AP new dict
print '*** ica.exclude before set components= ', ica.exclude
if n_comp_exclude.has_key(sbj_name):
print '*** ICA to be excluded for sbj %s ' % sbj_name + ' ' + str(n_comp_exclude[sbj_name]) + '***'
session_dict = n_comp_exclude[sbj_name]
session_names = session_dict.keys()
componentes = []
for s in session_names:
if basename.find(s) > -1:
componentes = session_dict[s]
break
if len(componentes) == 0:
print '\n no ICA to be excluded \n'
else:
print '\n *** ICA to be excluded for session %s ' % s + \
' ' + str(componentes) + ' *** \n'
ica.exclude = componentes
print '\n *** ica.exclude after set components = ', ica.exclude
fig1 = ica.plot_overlay(raw, show=is_show)
report.add_figs_to_section(fig1, captions=['Signal'],
section='Signal quality')
#.........这里部分代码省略.........
示例8: test_filter
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
def test_filter():
"""Test filtering (FIR and IIR) and Raw.apply_function interface
"""
raw = Raw(fif_fname).crop(0, 7, False)
raw.load_data()
sig_dec = 11
sig_dec_notch = 12
sig_dec_notch_fit = 12
picks_meg = pick_types(raw.info, meg=True, exclude='bads')
picks = picks_meg[:4]
filter_params = dict(picks=picks, n_jobs=2, copy=True)
raw_lp = raw.filter(0., 4.0 - 0.25, **filter_params)
raw_hp = raw.filter(8.0 + 0.25, None, **filter_params)
raw_bp = raw.filter(4.0 + 0.25, 8.0 - 0.25, **filter_params)
raw_bs = raw.filter(8.0 + 0.25, 4.0 - 0.25, **filter_params)
data, _ = raw[picks, :]
lp_data, _ = raw_lp[picks, :]
hp_data, _ = raw_hp[picks, :]
bp_data, _ = raw_bp[picks, :]
bs_data, _ = raw_bs[picks, :]
assert_array_almost_equal(data, lp_data + bp_data + hp_data, sig_dec)
assert_array_almost_equal(data, bp_data + bs_data, sig_dec)
filter_params_iir = dict(picks=picks, n_jobs=2, copy=True, method='iir')
raw_lp_iir = raw.filter(0., 4.0, **filter_params_iir)
raw_hp_iir = raw.filter(8.0, None, **filter_params_iir)
raw_bp_iir = raw.filter(4.0, 8.0, **filter_params_iir)
lp_data_iir, _ = raw_lp_iir[picks, :]
hp_data_iir, _ = raw_hp_iir[picks, :]
bp_data_iir, _ = raw_bp_iir[picks, :]
summation = lp_data_iir + hp_data_iir + bp_data_iir
assert_array_almost_equal(data[:, 100:-100], summation[:, 100:-100],
sig_dec)
# make sure we didn't touch other channels
data, _ = raw[picks_meg[4:], :]
bp_data, _ = raw_bp[picks_meg[4:], :]
assert_array_equal(data, bp_data)
bp_data_iir, _ = raw_bp_iir[picks_meg[4:], :]
assert_array_equal(data, bp_data_iir)
# ... and that inplace changes are inplace
raw_copy = raw.copy()
raw_copy.filter(None, 20., picks=picks, n_jobs=2, copy=False)
assert_true(raw._data[0, 0] != raw_copy._data[0, 0])
assert_equal(raw.filter(None, 20., **filter_params)._data,
raw_copy._data)
# do a very simple check on line filtering
with warnings.catch_warnings(record=True):
warnings.simplefilter('always')
raw_bs = raw.filter(60.0 + 0.5, 60.0 - 0.5, **filter_params)
data_bs, _ = raw_bs[picks, :]
raw_notch = raw.notch_filter(60.0, picks=picks, n_jobs=2,
method='fft', copy=True)
data_notch, _ = raw_notch[picks, :]
assert_array_almost_equal(data_bs, data_notch, sig_dec_notch)
# now use the sinusoidal fitting
raw_notch = raw.notch_filter(None, picks=picks, n_jobs=2,
method='spectrum_fit', copy=True)
data_notch, _ = raw_notch[picks, :]
data, _ = raw[picks, :]
assert_array_almost_equal(data, data_notch, sig_dec_notch_fit)
示例9: Raw
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
import numpy as np
import mne
from mne.io import Raw
from mne.preprocessing import ICA
from mne.preprocessing import create_ecg_epochs, create_eog_epochs
from mne.datasets import sample
###############################################################################
# Setup paths and prepare raw data
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
raw = Raw(raw_fname, preload=True)
raw.filter(1, 45, n_jobs=1)
###############################################################################
# 1) Fit ICA model using the FastICA algorithm
# Other available choices are `infomax` or `extended-infomax`
# We pass a float value between 0 and 1 to select n_components based on the
# percentage of variance explained by the PCA components.
ica = ICA(n_components=0.95, method='fastica')
picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False,
stim=False, exclude='bads')
ica.fit(raw, picks=picks, decim=3, reject=dict(mag=4e-12, grad=4000e-13))
示例10: dict
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
raw_fnormal = scratch_path + "tone_task-tsss-mc-autobad.fif"
raw_fhyp = scratch_path + "hyp_tone_task-tsss-mc-autobad.fif"
reject = dict(
grad=4000e-13, # T / m (gradiometers)
mag=4e-12, # T (magnetometers)
# eog=250e-6 # uV (EOG channels)
)
conditions = ["normal", "hyp"]
for condition in conditions:
if condition == "normal":
raw = Raw(raw_fnormal, preload=True)
raw.filter(1, 90, n_jobs=3)
raw.notch_filter(50, n_jobs=3)
elif condition == "hyp":
raw = Raw(raw_fhyp, preload=True)
raw.filter(1, 90, n_jobs=3)
raw.notch_filter(50, n_jobs=3)
#
ica = ICA(n_components=0.95, method="fastica")
picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude="bads")
ica.fit(raw, picks=picks, decim=3, reject=reject)
# maximum number of components to reject
n_max_ecg, n_max_eog = 3, 1
示例11: fix_triggers
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
events_meg_[:, 1] = run # to keep the run from which the event was found
events_meg.append(events_meg_)
events_meg = np.vstack(events_meg) # concatenate all meg events
# Compare MEG and Behavioral triggers
event_types = ['Target']
for event_type in event_types:
events_behavior_type = fix_triggers(events_meg, events_behavior,
event_type='trigg' + event_type)
# Epoch raw data
epochs_list = list()
for run in range(1, n_runs):
fname_raw = op.join(path_data, subject, 'run%02i.fif' % run)
raw = Raw(fname_raw, preload=True)
raw.filter(.75, h_freq=30.0)
sel = events_behavior_type['meg_file'] == run
time_sample = events_behavior_type['meg_event_tsample'][sel]
trigger_value = events_behavior_type['meg_event_value'][sel]
events_meg = np.vstack((time_sample.astype(int),
np.zeros_like(time_sample, int),
trigger_value.astype(int))).T
event_id = {'ttl_%i' % ii: ii for ii in np.unique(events_meg[:, 2])}
epochs = Epochs(raw, events_meg, event_id=event_id,
tmin=-1.0, tmax=.500, preload=True)
# epochs.resample(128) # XXX BUG MNE when concatenate afterwards
epochs_list.append(epochs)
epochs = concatenate_epochs(epochs_list)
epochs.resample(128)
# Save data
示例12: BSD
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
#
# License: BSD (3-clause)
import mne
from mne.io import Raw
from mne.preprocessing import ICA, create_ecg_epochs
from mne.datasets import sample
print(__doc__)
###############################################################################
# Fit ICA model using the FastICA algorithm, detect and inspect components
data_path = sample.data_path()
raw_fname = data_path + "/MEG/sample/sample_audvis_filt-0-40_raw.fif"
raw = Raw(raw_fname, preload=True)
raw.filter(1, 30, method="iir")
raw.pick_types(meg=True, eeg=False, exclude="bads", stim=True)
# longer + more epochs for more artifact exposure
events = mne.find_events(raw, stim_channel="STI 014")
epochs = mne.Epochs(raw, events, event_id=None, tmin=-0.2, tmax=0.5)
ica = ICA(n_components=0.95, method="fastica").fit(epochs)
ecg_epochs = create_ecg_epochs(raw, tmin=-0.5, tmax=0.5)
ecg_inds, scores = ica.find_bads_ecg(ecg_epochs)
ica.plot_components(ecg_inds)
示例13: Raw
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
import numpy as np
import mne
from mne.io import Raw
from mne.preprocessing import ICA
from mne.preprocessing import create_eog_epochs
from mne.datasets import sample
###############################################################################
# Setup paths and prepare raw data
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
raw = Raw(raw_fname, preload=True)
raw.filter(1, 45, n_jobs=2)
###############################################################################
# Setup ICA seed decompose data, then access and plot sources.
# We pass a float value between 0 and 1 to select n_components based on the
# percentage of variance explained by the PCA components.
ica = ICA(n_components=0.90, max_pca_components=None)
###############################################################################
# 1) Fit ICA model and identify bad sources
picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False,
stim=False, exclude='bads')
示例14: Raw
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
# coh_fname = data_path + 'coh/' + subj + '_' + freq + '_subj_connectivityMatrix.txt'
# plv_fname = data_path + 'coh/' + subj+ '_' + freq + '_subj_plv_ConnectivityMatrix.txt'
# pli_fname = data_path + 'coh/' + subj+ '_' + freq + '_subj_pli_ConnectivityMatrix.txt'
cov_fname = data_path + "cov/emptyroom-cov.fif"
raw_file = data_path + run + "_raw.fif"
cohLog_file = data_path + "logs/" + subj + "_" + freq + "_" + run + "_coherence.log"
event_file = data_path + "eve/" + run + ".eve"
mne.set_log_file(fname=cohLog_file, overwrite=True)
stc_fname = data_path + "ave_projon/stc_py/" + subj + "_" + run
powenv_fname = data_path + "coh/" + subj + "_" + freq + "_" + run + "-noise_powEnv_LabelsMatrix.txt"
print fname_fwd
print
##Load Data
raw = Raw(raw_file, preload=True)
raw.filter(fmin, fmax, picks=None)
print raw.info
print "Raw data filtered to the desired freq band: " + freq
events = mne.read_events(event_file)
# inverse_operator = read_inverse_operator(fname_inv)
# label_lh = mne.read_label(fname_label_lh)
# Read epochs
epochs = mne.Epochs(
raw,
events,
event_id,
tmin,
tmax,
baseline=(None, 0),
proj=True,
示例15: T
# 需要导入模块: from mne.io import Raw [as 别名]
# 或者: from mne.io.Raw import filter [as 别名]
mag=4e-12 # T (magnetometers)
)
# SETTINGS
#raw = Raw(save_folder + "%s_%s_filtered_mc_tsss-raw.fif" % (subject,
# condition),
# preload=True)
raw = Raw(maxfiltered_folder + "%s_%s_mc_tsss-raw.fif" % (subject,
condition),
preload=True)
raw.drop_channels(raw.info["bads"])
raw.notch_filter(n_freq, n_jobs=n_jobs)
raw.filter(l_freq, h_freq, n_jobs=n_jobs)
raw.save(save_folder + "%s_%s_filtered_mc_tsss-raw.fif" % (subject,
condition),
overwrite=True)
# ICA Part
ica = ICA(n_components=0.99, method='fastica', max_iter=256)
picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, emg=False,
bio=False, stim=False, exclude='bads')
ica.fit(raw, picks=picks, decim=decim, reject=reject_params)