本文整理汇总了Python中mne.preprocessing.read_ica函数的典型用法代码示例。如果您正苦于以下问题:Python read_ica函数的具体用法?Python read_ica怎么用?Python read_ica使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_ica函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_n_components_and_max_pca_components_none
def test_n_components_and_max_pca_components_none(method):
"""Test n_components and max_pca_components=None."""
_skip_check_picard(method)
raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data()
events = read_events(event_name)
picks = pick_types(raw.info, eeg=True, meg=False)
epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), preload=True)
max_pca_components = None
n_components = None
random_state = 12345
tempdir = _TempDir()
output_fname = op.join(tempdir, 'test_ica-ica.fif')
ica = ICA(max_pca_components=max_pca_components, method=method,
n_components=n_components, random_state=random_state)
with pytest.warns(None): # convergence
ica.fit(epochs)
ica.save(output_fname)
ica = read_ica(output_fname)
# ICA.fit() replaced max_pca_components, which was previously None,
# with the appropriate integer value.
assert_equal(ica.max_pca_components, epochs.info['nchan'])
assert ica.n_components is None
示例2: test_n_components_none
def test_n_components_none():
"""Test n_components=None."""
raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data()
events = read_events(event_name)
picks = pick_types(raw.info, eeg=True, meg=False)
epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), preload=True)
max_pca_components = 10
n_components = None
random_state = 12345
tempdir = _TempDir()
output_fname = op.join(tempdir, 'test_ica-ica.fif')
ica = ICA(max_pca_components=max_pca_components,
n_components=n_components, random_state=random_state)
with warnings.catch_warnings(record=True): # convergence
ica.fit(epochs)
ica.save(output_fname)
ica = read_ica(output_fname)
# ICA.fit() replaced max_pca_components, which was previously None,
# with the appropriate integer value.
assert_equal(ica.max_pca_components, 10)
assert_is_none(ica.n_components)
示例3: load_ica
def load_ica(subject, description, ica_data_root=None):
if ica_data_root is None:
# use default data root
import deepthought
data_root = os.path.join(deepthought.DATA_PATH, 'OpenMIIR')
ica_data_root = os.path.join(data_root, 'eeg', 'preprocessing', 'ica')
ica_filepath = os.path.join(ica_data_root,
'{}-{}-ica.fif'.format(subject, description))
return read_ica(ica_filepath)
示例4: cli
def cli(subjdirs):
"""
Show the variance explained for mne ica solution
EXAMPLES:
Show explained variances for ica solutions for each subject in FIF_DATASET and write them to file vars.txt:
$ ica_var FIF_DATASET/*/*-ica.fif >> vars.txt
"""
for fname in subjdirs:
with nostdout():
ica = read_ica(fname)
n_comp = ica.n_components_
tot_var = ica.pca_explained_variance_.sum()
n_comp_var = ica.pca_explained_variance_[:n_comp].sum()
PVE = n_comp_var / tot_var
click.echo(PVE)
示例5: Raw
#noise_cov_er.save(empty_room_fname[:-4]+'-cov.fif')
###############################################################################
# 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.
raw = Raw(raw_fname, preload=True)
picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, ecg=False,
stim=False, exclude='bads')
# maximum number of components to reject
n_max_ecg, n_max_eog = 2, 3 # here we expect horizontal EOG components
try:
ica = read_ica(raw_data_folder + '/ica_pre.fif')
except:
ica = ICA(n_components=0.95, max_pca_components = 64, method='fastica')
#noise_cov = noise_cov_er)
#ica.fit(raw, picks=picks, decim=3, reject=dict(mag=4e-12, grad=4000e-13))
ica.fit(raw, picks=picks, decim = 5, reject=dict(mag=4e-11, grad=4000e-12))
# To save an ICA solution you can say:
###############################################################################
# 2) identify bad components by analyzing latent sources.
title = 'Sources related to %s artifacts (red)'
# generate ECG epochs use detection via phase statistics
示例6: test_ica_additional
def test_ica_additional(method):
"""Test additional ICA functionality."""
_skip_check_picard(method)
tempdir = _TempDir()
stop2 = 500
raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data()
raw.del_proj() # avoid warnings
raw.set_annotations(Annotations([0.5], [0.5], ['BAD']))
# XXX This breaks the tests :(
# raw.info['bads'] = [raw.ch_names[1]]
test_cov = read_cov(test_cov_name)
events = read_events(event_name)
picks = pick_types(raw.info, meg=True, stim=False, ecg=False,
eog=False, exclude='bads')[1::2]
epochs = Epochs(raw, events, None, tmin, tmax, picks=picks,
baseline=(None, 0), preload=True, proj=False)
epochs.decimate(3, verbose='error')
assert len(epochs) == 4
# test if n_components=None works
ica = ICA(n_components=None, max_pca_components=None,
n_pca_components=None, random_state=0, method=method, max_iter=1)
with pytest.warns(UserWarning, match='did not converge'):
ica.fit(epochs)
# for testing eog functionality
picks2 = np.concatenate([picks, pick_types(raw.info, False, eog=True)])
epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2,
baseline=(None, 0), preload=True)
del picks2
test_cov2 = test_cov.copy()
ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4,
n_pca_components=4, method=method)
assert (ica.info is None)
with pytest.warns(RuntimeWarning, match='normalize_proj'):
ica.fit(raw, picks[:5])
assert (isinstance(ica.info, Info))
assert (ica.n_components_ < 5)
ica = ICA(n_components=3, max_pca_components=4, method=method,
n_pca_components=4, random_state=0)
pytest.raises(RuntimeError, ica.save, '')
ica.fit(raw, picks=[1, 2, 3, 4, 5], start=start, stop=stop2)
# check passing a ch_name to find_bads_ecg
with pytest.warns(RuntimeWarning, match='longer'):
_, scores_1 = ica.find_bads_ecg(raw)
_, scores_2 = ica.find_bads_ecg(raw, raw.ch_names[1])
assert scores_1[0] != scores_2[0]
# test corrmap
ica2 = ica.copy()
ica3 = ica.copy()
corrmap([ica, ica2], (0, 0), threshold='auto', label='blinks', plot=True,
ch_type="mag")
corrmap([ica, ica2], (0, 0), threshold=2, plot=False, show=False)
assert (ica.labels_["blinks"] == ica2.labels_["blinks"])
assert (0 in ica.labels_["blinks"])
# test retrieval of component maps as arrays
components = ica.get_components()
template = components[:, 0]
EvokedArray(components, ica.info, tmin=0.).plot_topomap([0], time_unit='s')
corrmap([ica, ica3], template, threshold='auto', label='blinks', plot=True,
ch_type="mag")
assert (ica2.labels_["blinks"] == ica3.labels_["blinks"])
plt.close('all')
ica_different_channels = ICA(n_components=2, random_state=0).fit(
raw, picks=[2, 3, 4, 5])
pytest.raises(ValueError, corrmap, [ica_different_channels, ica], (0, 0))
# test warnings on bad filenames
ica_badname = op.join(op.dirname(tempdir), 'test-bad-name.fif.gz')
with pytest.warns(RuntimeWarning, match='-ica.fif'):
ica.save(ica_badname)
with pytest.warns(RuntimeWarning, match='-ica.fif'):
read_ica(ica_badname)
# test decim
ica = ICA(n_components=3, max_pca_components=4,
n_pca_components=4, method=method, max_iter=1)
raw_ = raw.copy()
for _ in range(3):
raw_.append(raw_)
n_samples = raw_._data.shape[1]
with pytest.warns(UserWarning, match='did not converge'):
ica.fit(raw, picks=picks[:5], decim=3)
assert raw_._data.shape[1] == n_samples
# test expl var
ica = ICA(n_components=1.0, max_pca_components=4,
n_pca_components=4, method=method, max_iter=1)
with pytest.warns(UserWarning, match='did not converge'):
ica.fit(raw, picks=None, decim=3)
assert (ica.n_components_ == 4)
ica_var = _ica_explained_variance(ica, raw, normalize=True)
#.........这里部分代码省略.........
示例7: dict
return eve_dict, id_dict
# Set epoch parameters
tmin, tmax = -0.4, 0.6 # no need to take more than this, wide enough to see eyemov though
rej_tmin, rej_tmax = -0.2, 0.2 # reject trial only if blinks in the 400 ms middle portion!
baseline = (-0.2, 0.)
reject = dict(eog=150e-6, mag=4e-12, grad=4000e-13) # compare to standard rejection
raw_path = ad._scratch_folder + '/tsss_initial/007_SGF'
eve_path = ad._scratch_folder + '/events.fif/007_SGF/raw'
fname = raw_path + '/VS_1a_1_tsss_mc.fif'
raw = Raw(fname, preload=True)
ica = read_ica(raw_path + '/ica_pre.fif')
print 'Excluding', ica.exclude
raw_ica = ica.apply(raw, copy=True)
events = mne.read_events(eve_path + '/VS_1a_1-eve.fif')
picks = pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True, misc=True)
eve_dict, id_dict = split_events_by_trialtype(events)
for trial_type in ['VS']:
epochs = mne.Epochs(raw, eve_dict[trial_type], id_dict,
tmin, tmax, picks=picks, verbose=False,
baseline=baseline, reject=reject, preload=True,
reject_tmin=rej_tmin, reject_tmax=rej_tmax) # Check rejection settings
epochs_ica = mne.Epochs(raw_ica, eve_dict[trial_type], id_dict,
tmin, tmax, picks=picks, verbose=False,
baseline=baseline, reject=None, preload=True)
示例8: preprocess_ICA_fif_to_ts
def preprocess_ICA_fif_to_ts(fif_file, ECG_ch_name, EoG_ch_name, l_freq, h_freq, down_sfreq, variance, is_sensor_space, data_type):
import os
import numpy as np
import mne
from mne.io import Raw
from mne.preprocessing import ICA, read_ica
from mne.preprocessing import create_ecg_epochs, create_eog_epochs
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 data_path
# Read raw
# If None the compensation in the data is not modified.
# If set to n, e.g. 3, apply gradient compensation of grade n as for
# CTF systems (compensation=3)
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")
print '*** ' + channel_coords_file + '***'
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")
# AP 21032016
# channel_names_file = os.path.join(data_path, "correct_channel_names.txt")
channel_names_file = os.path.abspath("correct_channel_names.txt")
np.savetxt(channel_names_file,sens_names , fmt = '%s')
### filtering + downsampling
raw.filter(l_freq=l_freq, h_freq=h_freq, picks=picks_meeg,
method='iir', n_jobs=8)
# raw.filter(l_freq = l_freq, h_freq = h_freq, picks = picks_meeg,
# method='iir')
# raw.resample(sfreq=down_sfreq, npad=0)
### 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_title = 'Sources related to %s artifacts (red)'
is_show = False # visualization
reject = dict(mag=4e-12, grad=4000e-13)
# check if we have an ICA, if yes, we load it
ica_filename = os.path.join(subj_path,basename + "-ica.fif")
if os.path.exists(ica_filename) is False:
ica = ICA(n_components=variance, method='fastica', max_iter=500) # , max_iter=500
ica.fit(raw, picks=select_sensors, reject=reject) # decim = 3,
has_ICA = False
else:
has_ICA = True
print ica_filename + ' exists!!!'
ica = read_ica(ica_filename)
ica.exclude = []
# 2) identify bad components by analyzing latent sources.
# generate ECG epochs use detection via phase statistics
# if we just have exclude channels we jump these steps
# if len(ica.exclude)==0:
n_max_ecg = 3
n_max_eog = 2
# check if ECG_ch_name is in the raw channels
if ECG_ch_name in raw.info['ch_names']:
ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5,
picks=select_sensors,
ch_name=ECG_ch_name)
# if not a synthetic ECG channel is created from cross channel average
else:
ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5,
picks=select_sensors)
# ICA for ECG artifact
# threshold=0.25 come default
ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps')
print scores
print '\n len ecg_inds *** ' + str(len(ecg_inds)) + '***\n'
if len(ecg_inds) > 0:
ecg_evoked = ecg_epochs.average()
fig1 = ica.plot_scores(scores, exclude=ecg_inds,
#.........这里部分代码省略.........
示例9: preprocess_set_ICA_comp_fif_to_ts
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')
#.........这里部分代码省略.........
示例10: Raw
cond_names = ad.analysis_dict[subj][input_files].keys()
# sort names so that VS comes before FFA!
cond_names.sort(reverse=True)
for cond in cond_names:
if 'empty' not in cond:
raw_path = ad._scratch_folder + '/' + input_files + '/' + subj
in_fnames = ad.analysis_dict[subj][input_files][cond]['files']
for fname in in_fnames:
img_prefix = img_folder + '/' + cond
print 'In: ', fname
raw = Raw(fname, preload=True) # for finding events from raw, must be preloaded
ica = read_ica(ica_folder + '/' + cond + '-ica.fif')
# 2) identify bad components by analyzing latent sources.
title = 'Sources related to %s artifacts (red)'
# generate ECG epochs use detection via phase statistics
picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=True, ecg=True, stim=False, exclude='bads')
# create_ecg_epochs is strange: it strips the channels of anything non M/EEG
# UNLESS picks=None
#picks=None
# This will work with the above, but uses MASSIVE RAM
# Not sure the ECG quality is good enough for the QRS-detector
ecg_inds, scores = ica.find_bads_ecg(raw, method='ctps', ch_name='ECG002', threshold=0.25)
if len(ecg_inds) < 1:
# destroy the ECG channel by changing it to an EMG!
示例11: read_ica
raw = mne.io.read_raw_fif(run_fname, preload=True, add_eeg_ref=False)
###############################################################################
# We change the channel type for ECG and EOG.
raw.set_channel_types({'EEG061': 'eog', 'EEG062': 'eog', 'EEG063': 'ecg',
'EEG064': 'misc'}) # EEG064 free floating el.
raw.rename_channels({'EEG061': 'EOG061', 'EEG062': 'EOG062',
'EEG063': 'ECG063'})
###############################################################################
# Bad sensors are repaired.
raw.info['bads'] = bads
raw.interpolate_bads()
raw.set_eeg_reference()
###############################################################################
# Now let's get to some serious ICA preprocessing
ica_name = op.join(meg_dir, subject, 'run_%02d-ica.fif' % run)
ica = read_ica(ica_name)
n_max_ecg = 3 # use max 3 components
ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5)
ecg_inds, scores_ecg = ica.find_bads_ecg(ecg_epochs, method='ctps',
threshold=0.8)
ica.plot_sources(raw, exclude=ecg_inds)
ica.plot_scores(scores_ecg, exclude=ecg_inds)
ica.plot_properties(raw, ecg_inds)
ica.exclude += ecg_inds[:n_max_ecg]
示例12: test_ica_additional
def test_ica_additional():
"""Test additional functionality
"""
stop2 = 500
test_cov2 = deepcopy(test_cov)
ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4,
n_pca_components=4)
ica.decompose_raw(raw, picks[:5])
assert_true(ica.n_components_ < 5)
ica = ICA(n_components=3, max_pca_components=4,
n_pca_components=4)
assert_raises(RuntimeError, ica.save, '')
ica.decompose_raw(raw, picks=None, start=start, stop=stop2)
# epochs extraction from raw fit
assert_raises(RuntimeError, ica.get_sources_epochs, epochs)
# test reading and writing
test_ica_fname = op.join(op.dirname(tempdir), 'ica_test.fif')
for cov in (None, test_cov):
ica = ICA(noise_cov=cov, n_components=3, max_pca_components=4,
n_pca_components=4)
ica.decompose_raw(raw, picks=picks, start=start, stop=stop2)
sources = ica.get_sources_epochs(epochs)
assert_true(sources.shape[1] == ica.n_components_)
for exclude in [[], [0]]:
ica.exclude = [0]
ica.save(test_ica_fname)
ica_read = read_ica(test_ica_fname)
assert_true(ica.exclude == ica_read.exclude)
# test pick merge -- add components
ica.pick_sources_raw(raw, exclude=[1])
assert_true(ica.exclude == [0, 1])
# -- only as arg
ica.exclude = []
ica.pick_sources_raw(raw, exclude=[0, 1])
assert_true(ica.exclude == [0, 1])
# -- remove duplicates
ica.exclude += [1]
ica.pick_sources_raw(raw, exclude=[0, 1])
assert_true(ica.exclude == [0, 1])
ica_raw = ica.sources_as_raw(raw)
assert_true(ica.exclude == [ica.ch_names.index(e) for e in
ica_raw.info['bads']])
ica.n_pca_components = 2
ica.save(test_ica_fname)
ica_read = read_ica(test_ica_fname)
assert_true(ica.n_pca_components ==
ica_read.n_pca_components)
ica.n_pca_components = 4
ica_read.n_pca_components = 4
ica.exclude = []
ica.save(test_ica_fname)
ica_read = read_ica(test_ica_fname)
assert_true(ica.ch_names == ica_read.ch_names)
assert_true(np.allclose(ica.mixing_matrix_, ica_read.mixing_matrix_,
rtol=1e-16, atol=1e-32))
assert_array_equal(ica.pca_components_,
ica_read.pca_components_)
assert_array_equal(ica.pca_mean_, ica_read.pca_mean_)
assert_array_equal(ica.pca_explained_variance_,
ica_read.pca_explained_variance_)
assert_array_equal(ica._pre_whitener, ica_read._pre_whitener)
# assert_raises(RuntimeError, ica_read.decompose_raw, raw)
sources = ica.get_sources_raw(raw)
sources2 = ica_read.get_sources_raw(raw)
assert_array_almost_equal(sources, sources2)
_raw1 = ica.pick_sources_raw(raw, exclude=[1])
_raw2 = ica_read.pick_sources_raw(raw, exclude=[1])
assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0])
os.remove(test_ica_fname)
# score funcs raw, with catch since "ties preclude exact" warning
# XXX this should be fixed by a future PR...
with warnings.catch_warnings(True) as w:
sfunc_test = [ica.find_sources_raw(raw, target='EOG 061',
score_func=n, start=0, stop=10)
for n, f in score_funcs.items()]
# score funcs raw
# check lenght of scores
[assert_true(ica.n_components_ == len(scores)) for scores in sfunc_test]
# check univariate stats
scores = ica.find_sources_raw(raw, score_func=stats.skew)
# check exception handling
assert_raises(ValueError, ica.find_sources_raw, raw,
target=np.arange(1))
## score funcs epochs ##
#.........这里部分代码省略.........
示例13: test_ica_additional
def test_ica_additional():
"""Test additional functionality
"""
stop2 = 500
test_cov2 = deepcopy(test_cov)
ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4,
n_pca_components=4)
assert_true(ica.info is None)
ica.decompose_raw(raw, picks[:5])
assert_true(isinstance(ica.info, Info))
assert_true(ica.n_components_ < 5)
ica = ICA(n_components=3, max_pca_components=4,
n_pca_components=4)
assert_raises(RuntimeError, ica.save, '')
ica.decompose_raw(raw, picks=None, start=start, stop=stop2)
# epochs extraction from raw fit
assert_raises(RuntimeError, ica.get_sources_epochs, epochs)
# test reading and writing
test_ica_fname = op.join(op.dirname(tempdir), 'ica_test.fif')
for cov in (None, test_cov):
ica = ICA(noise_cov=cov, n_components=3, max_pca_components=4,
n_pca_components=4)
ica.decompose_raw(raw, picks=picks, start=start, stop=stop2)
sources = ica.get_sources_epochs(epochs)
assert_true(sources.shape[1] == ica.n_components_)
for exclude in [[], [0]]:
ica.exclude = [0]
ica.save(test_ica_fname)
ica_read = read_ica(test_ica_fname)
assert_true(ica.exclude == ica_read.exclude)
# test pick merge -- add components
ica.pick_sources_raw(raw, exclude=[1])
assert_true(ica.exclude == [0, 1])
# -- only as arg
ica.exclude = []
ica.pick_sources_raw(raw, exclude=[0, 1])
assert_true(ica.exclude == [0, 1])
# -- remove duplicates
ica.exclude += [1]
ica.pick_sources_raw(raw, exclude=[0, 1])
assert_true(ica.exclude == [0, 1])
ica_raw = ica.sources_as_raw(raw)
assert_true(ica.exclude == [ica_raw.ch_names.index(e) for e in
ica_raw.info['bads']])
ica.n_pca_components = 2
ica.save(test_ica_fname)
ica_read = read_ica(test_ica_fname)
assert_true(ica.n_pca_components ==
ica_read.n_pca_components)
ica.n_pca_components = 4
ica_read.n_pca_components = 4
ica.exclude = []
ica.save(test_ica_fname)
ica_read = read_ica(test_ica_fname)
assert_true(ica.ch_names == ica_read.ch_names)
assert_true(isinstance(ica_read.info, Info)) # XXX improve later
assert_true(np.allclose(ica.mixing_matrix_, ica_read.mixing_matrix_,
rtol=1e-16, atol=1e-32))
assert_array_equal(ica.pca_components_,
ica_read.pca_components_)
assert_array_equal(ica.pca_mean_, ica_read.pca_mean_)
assert_array_equal(ica.pca_explained_variance_,
ica_read.pca_explained_variance_)
assert_array_equal(ica._pre_whitener, ica_read._pre_whitener)
# assert_raises(RuntimeError, ica_read.decompose_raw, raw)
sources = ica.get_sources_raw(raw)
sources2 = ica_read.get_sources_raw(raw)
assert_array_almost_equal(sources, sources2)
_raw1 = ica.pick_sources_raw(raw, exclude=[1])
_raw2 = ica_read.pick_sources_raw(raw, exclude=[1])
assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0])
os.remove(test_ica_fname)
# check scrore funcs
for name, func in score_funcs.items():
if name in score_funcs_unsuited:
continue
scores = ica.find_sources_raw(raw, target='EOG 061', score_func=func,
start=0, stop=10)
assert_true(ica.n_components_ == len(scores))
# check univariate stats
scores = ica.find_sources_raw(raw, score_func=stats.skew)
# check exception handling
assert_raises(ValueError, ica.find_sources_raw, raw,
target=np.arange(1))
params = []
params += [(None, -1, slice(2), [0, 1])] # varicance, kurtosis idx params
#.........这里部分代码省略.........
示例14: dict
session_no = ''
ica_check_eves = dict(FFA=['A','B'])
ica_cond = 'FFA'
ica_excludes = load_excludes(ica_excludes_path, subj, ica_cond)
print 30*'*'
print 'ICA excludes:', ica_excludes
print 30*'*'
raw_path = opj(ad._scratch_folder, input_files, subj)
in_fnames = ad.analysis_dict[subj][input_files][cond]['files']
events = mne.read_events(opj(eve_folder, cond + '-eve.fif'))
eve_dict, id_dict = \
split_events_by_trialtype(events, condition=cond)
for fname in in_fnames:
ica = read_ica(opj(ica_folder, cond + '-ica.fif'))
print 'In: ', fname
raw = Raw(fname, preload=performBandpassFilter)
rep_section_name = ''
if performBandpassFilter:
raw.filter(filter_params['highpass'],
filter_params['lowpass'],
method='iir', n_jobs=1
)
picks = mne.pick_types(raw.info, meg=True, eog=True)
for trial_type in trial_types:
epochs = mne.Epochs(raw, eve_dict[trial_type],
id_dict[trial_type],
示例15: test_ica_additional
def test_ica_additional():
"""Test additional ICA functionality
"""
stop2 = 500
raw = io.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5)
picks = pick_types(raw.info, meg=True, stim=False, ecg=False,
eog=False, exclude='bads')
test_cov = read_cov(test_cov_name)
events = read_events(event_name)
picks = pick_types(raw.info, meg=True, stim=False, ecg=False,
eog=False, exclude='bads')
epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks,
baseline=(None, 0), preload=True)
# for testing eog functionality
picks2 = pick_types(raw.info, meg=True, stim=False, ecg=False,
eog=True, exclude='bads')
epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2,
baseline=(None, 0), preload=True)
test_cov2 = deepcopy(test_cov)
ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4,
n_pca_components=4)
assert_true(ica.info is None)
ica.decompose_raw(raw, picks[:5])
assert_true(isinstance(ica.info, Info))
assert_true(ica.n_components_ < 5)
ica = ICA(n_components=3, max_pca_components=4,
n_pca_components=4)
assert_raises(RuntimeError, ica.save, '')
ica.decompose_raw(raw, picks=None, start=start, stop=stop2)
# test warnings on bad filenames
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
ica_badname = op.join(op.dirname(tempdir), 'test-bad-name.fif.gz')
ica.save(ica_badname)
read_ica(ica_badname)
assert_true(len(w) == 2)
# test decim
ica = ICA(n_components=3, max_pca_components=4,
n_pca_components=4)
raw_ = raw.copy()
for _ in range(3):
raw_.append(raw_)
n_samples = raw_._data.shape[1]
ica.decompose_raw(raw, picks=None, decim=3)
assert_true(raw_._data.shape[1], n_samples)
# test expl var
ica = ICA(n_components=1.0, max_pca_components=4,
n_pca_components=4)
ica.decompose_raw(raw, picks=None, decim=3)
assert_true(ica.n_components_ == 4)
# epochs extraction from raw fit
assert_raises(RuntimeError, ica.get_sources_epochs, epochs)
# test reading and writing
test_ica_fname = op.join(op.dirname(tempdir), 'test-ica.fif')
for cov in (None, test_cov):
ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4,
n_pca_components=4)
with warnings.catch_warnings(record=True): # ICA does not converge
ica.decompose_raw(raw, picks=picks, start=start, stop=stop2)
sources = ica.get_sources_epochs(epochs)
assert_true(ica.mixing_matrix_.shape == (2, 2))
assert_true(ica.unmixing_matrix_.shape == (2, 2))
assert_true(ica.pca_components_.shape == (4, len(picks)))
assert_true(sources.shape[1] == ica.n_components_)
for exclude in [[], [0]]:
ica.exclude = [0]
ica.save(test_ica_fname)
ica_read = read_ica(test_ica_fname)
assert_true(ica.exclude == ica_read.exclude)
# test pick merge -- add components
ica.pick_sources_raw(raw, exclude=[1])
assert_true(ica.exclude == [0, 1])
# -- only as arg
ica.exclude = []
ica.pick_sources_raw(raw, exclude=[0, 1])
assert_true(ica.exclude == [0, 1])
# -- remove duplicates
ica.exclude += [1]
ica.pick_sources_raw(raw, exclude=[0, 1])
assert_true(ica.exclude == [0, 1])
# test basic include
ica.exclude = []
ica.pick_sources_raw(raw, include=[1])
ica_raw = ica.sources_as_raw(raw)
assert_true(ica.exclude == [ica_raw.ch_names.index(e) for e in
ica_raw.info['bads']])
# test filtering
d1 = ica_raw._data[0].copy()
with warnings.catch_warnings(record=True): # dB warning
ica_raw.filter(4, 20)
#.........这里部分代码省略.........