def test_detect_artifacts_replacement_of_run_ica(method, idx, ch_name): """Test replacement workflow for run_ica() function.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() ica = ICA(n_components=2, method=method) ica.fit(raw) ica.detect_artifacts(raw, start_find=0, stop_find=5, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx)
def test_ica_additional(): """Test additional ICA functionality""" tempdir = _TempDir() stop2 = 500 raw = Raw(raw_fname).crop(1.5, stop, False) raw.load_data() 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) # test if n_components=None works with warnings.catch_warnings(record=True): ica = ICA(n_components=None, max_pca_components=None, n_pca_components=None, random_state=0) ica.fit(epochs, picks=picks, decim=3) # 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 = test_cov.copy() ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4, n_pca_components=4) assert_true(ica.info is None) with warnings.catch_warnings(record=True): ica.fit(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, '') with warnings.catch_warnings(record=True): ica.fit(raw, picks=[1, 2, 3, 4, 5], start=start, stop=stop2) # test corrmap ica2 = 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_true(ica.labels_["blinks"] == ica2.labels_["blinks"]) assert_true(0 in ica.labels_["blinks"]) plt.close('all') # 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_naming(w, 'test_ica.py', 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] with warnings.catch_warnings(record=True): ica.fit(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) with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(ica.n_components_ == 4) # epochs extraction from raw fit assert_raises(RuntimeError, ica.get_sources, 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.fit(raw, picks=picks, start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() 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.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.exclude == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.exclude = [] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == [0, 1]) ica_raw = ica.get_sources(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) assert_true((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.notch_filter([10]) assert_true((d1 != ica_raw._data[0]).any()) 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) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ _pre_whitener') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) 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) for attr in ['mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', '_pre_whitener']: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert_true(ica.ch_names == ica_read.ch_names) assert_true(isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check scrore funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # varicance, kurtosis idx params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) with warnings.catch_warnings(record=True): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) assert_raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') assert_raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 idx, scores = ica.find_bads_eog(raw) assert_true(isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) assert_true(len(ica_raw._filenames) == 0) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = Raw(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs._raw is None) assert_true(ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert_true(ncomps_ == expected)
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) assert_true((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.notch_filter([10]) assert_true((d1 != ica_raw._data[0]).any()) 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) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ _pre_whitener') f = lambda x, y: getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) 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) for attr in ['mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', '_pre_whitener']: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert_true(ica.ch_names == ica_read.ch_names) assert_true(isinstance(ica_read.info, Info)) 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 params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) ## score funcs epochs ## # check score funcs for name, func in score_funcs.items(): if name in score_funcs_unsuited: continue scores = ica.find_sources_epochs(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.find_sources_epochs(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.find_sources_raw(raw, target='MEG 1531', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.find_sources_raw(raw, target='EOG 061', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.sources_as_raw(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) assert_true(len(ica_raw._filenames) == 0) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = io.Raw(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.sources_as_epochs(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) sources_epochs = ica.get_sources_epochs(epochs) assert_array_equal(ica_epochs.get_data(), sources_epochs) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs.raw is None) assert_true(ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = _check_n_pca_components(ica, ncomps) assert_true(ncomps_ == expected)
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') # make sure a single threshold in a list works corrmap([ica, ica3], template, threshold=[0.5], label='blinks', plot=True, ch_type="mag") 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) assert (np.all(ica_var[:-1] >= ica_var[1:])) # test ica sorting ica.exclude = [0] ica.labels_ = dict(blink=[0], think=[1]) ica_sorted = _sort_components(ica, [3, 2, 1, 0], copy=True) assert_equal(ica_sorted.exclude, [3]) assert_equal(ica_sorted.labels_, dict(blink=[3], think=[2])) # epochs extraction from raw fit pytest.raises(RuntimeError, ica.get_sources, 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, method=method, max_iter=1) with pytest.warns(None): # ICA does not converge ica.fit(raw, picks=picks[:10], start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() assert (ica.mixing_matrix_.shape == (2, 2)) assert (ica.unmixing_matrix_.shape == (2, 2)) assert (ica.pca_components_.shape == (4, 10)) assert (sources.shape[1] == ica.n_components_) for exclude in [[], [0], np.array([1, 2, 3])]: ica.exclude = exclude ica.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (list(ica.exclude) == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.apply(raw) ica.exclude = [] ica.apply(raw, exclude=[1]) assert (ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert (ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert (ica.exclude == [ ica_raw.ch_names.index(e) for e in ica_raw.info['bads'] ]) # test filtering d1 = ica_raw._data[0].copy() ica_raw.filter(4, 20, fir_design='firwin2') assert_equal(ica_raw.info['lowpass'], 20.) assert_equal(ica_raw.info['highpass'], 4.) assert ((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() ica_raw.notch_filter([10], trans_bandwidth=10, fir_design='firwin') assert ((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.method = 'fake' ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (ica.n_pca_components == ica_read.n_pca_components) assert_equal(ica.method, ica_read.method) assert_equal(ica.labels_, ica_read.labels_) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ pre_whitener_') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) 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) for attr in [ 'mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', 'pre_whitener_' ]: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert (ica.ch_names == ica_read.ch_names) assert (isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, start=0, stop=50, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # variance, kurtosis params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) # Make sure detect_artifacts marks the right components. # For int criterion, the doc says "E.g. range(2) would return the two # sources with the highest score". Assert that's what it does. # Only test for skew, since it's always the same code. ica.exclude = [] ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=None, eog_ch=None, skew_criterion=0, var_criterion=None, kurt_criterion=None) assert np.abs(scores[ica.exclude]) == np.max(np.abs(scores)) evoked = epochs.average() evoked_data = evoked.data.copy() raw_data = raw[:][0].copy() epochs_data = epochs.get_data().copy() with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) pytest.raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') pytest.raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert (isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) idx, scores = ica.find_bads_eog(evoked, ch_name='MEG 1441') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(evoked, method='correlation') assert_equal(len(scores), ica.n_components_) assert_array_equal(raw_data, raw[:][0]) assert_array_equal(epochs_data, epochs.get_data()) assert_array_equal(evoked_data, evoked.data) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert (ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert (eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert (ica_raw.last_samp - ica_raw.first_samp == 100) assert_equal(len(ica_raw._filenames), 1) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = read_raw_fif(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert (ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) assert (ica.n_components_ == ica_epochs.get_data().shape[1]) assert (ica_epochs._raw is None) assert (ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert (ncomps_ == expected) ica = ICA(method=method) with pytest.warns(None): # sometimes does not converge ica.fit(raw, picks=picks[:5]) with pytest.warns(RuntimeWarning, match='longer'): ica.find_bads_ecg(raw) ica.find_bads_eog(epochs, ch_name='MEG 0121') assert_array_equal(raw_data, raw[:][0]) raw.drop_channels(['MEG 0122']) pytest.raises(RuntimeError, ica.find_bads_eog, raw) with pytest.warns(RuntimeWarning, match='longer'): pytest.raises(RuntimeError, ica.find_bads_ecg, raw)
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) assert (np.all(ica_var[:-1] >= ica_var[1:])) # test ica sorting ica.exclude = [0] ica.labels_ = dict(blink=[0], think=[1]) ica_sorted = _sort_components(ica, [3, 2, 1, 0], copy=True) assert_equal(ica_sorted.exclude, [3]) assert_equal(ica_sorted.labels_, dict(blink=[3], think=[2])) # epochs extraction from raw fit pytest.raises(RuntimeError, ica.get_sources, 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, method=method, max_iter=1) with pytest.warns(None): # ICA does not converge ica.fit(raw, picks=picks[:10], start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() assert (ica.mixing_matrix_.shape == (2, 2)) assert (ica.unmixing_matrix_.shape == (2, 2)) assert (ica.pca_components_.shape == (4, 10)) assert (sources.shape[1] == ica.n_components_) for exclude in [[], [0], np.array([1, 2, 3])]: ica.exclude = exclude ica.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (list(ica.exclude) == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.apply(raw) ica.exclude = [] ica.apply(raw, exclude=[1]) assert (ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert (ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert (ica.exclude == [ica_raw.ch_names.index(e) for e in ica_raw.info['bads']]) # test filtering d1 = ica_raw._data[0].copy() ica_raw.filter(4, 20, fir_design='firwin2') assert_equal(ica_raw.info['lowpass'], 20.) assert_equal(ica_raw.info['highpass'], 4.) assert ((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() ica_raw.notch_filter([10], trans_bandwidth=10, fir_design='firwin') assert ((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.method = 'fake' ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (ica.n_pca_components == ica_read.n_pca_components) assert_equal(ica.method, ica_read.method) assert_equal(ica.labels_, ica_read.labels_) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ pre_whitener_') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) 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) for attr in ['mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', 'pre_whitener_']: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert (ica.ch_names == ica_read.ch_names) assert (isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, start=0, stop=50, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # variance, kurtosis params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) # Make sure detect_artifacts marks the right components. # For int criterion, the doc says "E.g. range(2) would return the two # sources with the highest score". Assert that's what it does. # Only test for skew, since it's always the same code. ica.exclude = [] ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=None, eog_ch=None, skew_criterion=0, var_criterion=None, kurt_criterion=None) assert np.abs(scores[ica.exclude]) == np.max(np.abs(scores)) evoked = epochs.average() evoked_data = evoked.data.copy() raw_data = raw[:][0].copy() epochs_data = epochs.get_data().copy() with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) pytest.raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') pytest.raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert (isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) idx, scores = ica.find_bads_eog(evoked, ch_name='MEG 1441') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(evoked, method='correlation') assert_equal(len(scores), ica.n_components_) assert_array_equal(raw_data, raw[:][0]) assert_array_equal(epochs_data, epochs.get_data()) assert_array_equal(evoked_data, evoked.data) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): ecg_events = ica_find_ecg_events( raw, sources[np.abs(ecg_scores).argmax()]) assert (ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): eog_events = ica_find_eog_events( raw, sources[np.abs(eog_scores).argmax()]) assert (eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert (ica_raw.last_samp - ica_raw.first_samp == 100) assert_equal(len(ica_raw._filenames), 1) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = read_raw_fif(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert (ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) assert (ica.n_components_ == ica_epochs.get_data().shape[1]) assert (ica_epochs._raw is None) assert (ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert (ncomps_ == expected) ica = ICA(method=method) with pytest.warns(None): # sometimes does not converge ica.fit(raw, picks=picks[:5]) with pytest.warns(RuntimeWarning, match='longer'): ica.find_bads_ecg(raw) ica.find_bads_eog(epochs, ch_name='MEG 0121') assert_array_equal(raw_data, raw[:][0]) raw.drop_channels(['MEG 0122']) pytest.raises(RuntimeError, ica.find_bads_eog, raw) with pytest.warns(RuntimeWarning, match='longer'): pytest.raises(RuntimeError, ica.find_bads_ecg, raw)
def __run_ica_rejection(self, epochs, n_components=None, random_state=42, method='fastica', fit_params=None, max_iter=200, make_plots=False): """ Simple function for running ICA artifact rejection, based on hardcoded options. Parameters ---------- epochs : Epoch object (MNE) MNE ready epochs to process. See 'build_epochs' for more details. n_components : int, optional Number of principal components passed to the ICA during fitting. See the mne.preprocessing.ICA docstring for more details. The default is None. random_state : int, optional The value to be used as the 'seed' for `numpy.random.RandomState`. See the mne.preprocessing.ICA docstring for more details. The default is 42. method : TYPE, optional ICA method to be used. Can be 'fastica', 'infomax', or 'picard'. See the mne.preprocessing.ICA docstring for more details. The default is 'fastica'. fit_params : dict, optional Additional parameters pased to the ICA estimator as specified by 'method'. See the mne.preprocessing.ICA docstring for more details. The default is None. max_iter : int, optional Maximum number of iterations during ICA fit. The default is 200. make_plots : bool, optional Plot the individual principal components found for the ICA. The default is False. Returns ------- cleaned_epochs : Epoch object Returns an artifact cleaned MNE ready epoch object. See Also -------- mne.preprocessing.ICA """ if method.casefold() == 'infomax': fit_params = dict(extended=True) # For extended infomax # We will use artificat rejection for each epoch through ICA. ica = ICA(n_components=n_components, random_state=random_state, method=method, fit_params=fit_params, max_iter=max_iter) # Fit the ICA ica.fit(epochs) # Detect artifacts automatically ica.detect_artifacts(epochs) # Exclude based on detected artifacts ica.exclude print("The rejected ICA components were: " + str(ica.exclude)) # This is just making basic plots right now. Will update later to be more thorough. if make_plots is True and n_components is None: nchans = ica.info['nchan'] ica.plot_components() [ica.plot_properties(epochs, picks=[x]) for x in range(0, nchans)] elif make_plots is True: ncomps = n_components ica.plot_components() [ica.plot_properties(epochs, picks=[x]) for x in range(0, ncomps)] # Save to new data - not needed here, but kept just in case cleaned_epochs = epochs.copy() # Apply ICA rejections ica.apply(cleaned_epochs) return cleaned_epochs
def test_ica_additional(): """Test additional ICA functionality """ stop2 = 500 raw = fiff.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) picks = fiff.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 = fiff.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 = fiff.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 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), 'ica_test.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) assert_true((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.notch_filter([10]) assert_true((d1 != ica_raw._data[0]).any()) 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) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ _pre_whitener') f = lambda x, y: getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) 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) for attr in [ 'mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', '_pre_whitener' ]: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert_true(ica.ch_names == ica_read.ch_names) assert_true(isinstance(ica_read.info, Info)) 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 params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) ## score funcs epochs ## # check score funcs for name, func in score_funcs.items(): if name in score_funcs_unsuited: continue scores = ica.find_sources_epochs(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.find_sources_epochs(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.find_sources_raw(raw, target='MEG 1531', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.find_sources_raw(raw, target='EOG 061', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.sources_as_raw(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) assert_true(len(ica_raw._filenames) == 0) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test_ica.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = fiff.Raw(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.sources_as_epochs(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) sources_epochs = ica.get_sources_epochs(epochs) assert_array_equal(ica_epochs.get_data(), sources_epochs) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs.raw is None) assert_true(ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = _check_n_pca_components(ica, ncomps) assert_true(ncomps_ == expected)
class MNEprepro(): """ Class to preproces CTF data Usage: raw_prepro = MNEprepro(subject, experiment, paths_dic) paths_dic = { "root": "~/Desktop/projects/MNE/data", "subj_anat": 'anatomy' "out": "~/Desktop/projects/MNE/data_prep" } subject = '18011014C' experiment = 'Movie' root = folder where subjects name folders are subj_anat = name anatomy folder in subject folder out = path to output files FOLDER EXAMPLE: /path2/MEG_data: /subject_name /experiment1.ds /experiment2.ds /anatomy /t1.nii.gz /cortical_surf_4k.gii /polhemus.pos STEPS to do 1- load the data 2- Load previous prepro info -> bad channels, mov, etc if any 3- plot and find bad channels 4- movement rejection 5- muscle rejection 6- ICA or SSP 7- epoch (save or no save) 7a- epoch based on photodiode 8- head model 8- do sensor level: Pow ERF Conn 9- source level: ... out directory: /bad_chn_annot /ICA """ def __init__(self, subject, experiment, paths_dic): self.subject = subject self.experiment = experiment self.pth_root = op.expanduser(paths_dic["root"]) self.pth_out = op.expanduser(paths_dic["out"]) self.pth_FS = op.expanduser(paths_dic["FS"]) mne.set_config('SUBJECTS_DIR', self.pth_FS) self.check_outdir() self.pth_subject = op.join(self.pth_root, subject) if self.check_MXfilter_sbjs() is True: print('Using MaxFilter preprocessed data') self.raw = mne.io.read_raw_fif(self.pth_raw, preload=False) else: self.pth_raw = glob.glob(op.join(self.pth_subject, subject) + '_' + experiment + '*')[-1] self.raw = mne.io.read_raw_ctf(self.pth_raw, preload=False) if self.raw.compensation_grade != 3: self.raw.apply_gradient_compensation(3) def check_outdir(self): from os import makedirs out_dir = self.pth_out self.out_bd_ch = op.join(out_dir, 'bad_chans') self.out_annot = op.join(out_dir, 'annots') self.out_ICAs = op.join(out_dir, 'ICAs') self.out_srcData = op.join(out_dir, 'data2src') makedirs(self.out_bd_ch, exist_ok=True) # I want to loop it makedirs(self.out_annot, exist_ok=True) makedirs(self.out_ICAs, exist_ok=True) makedirs(self.out_srcData, exist_ok=True) def check_MXfilter_sbjs(self): pth_mx_sbj = self.pth_out + '/MX_filter_subj/' + self.subject if op.exists(pth_mx_sbj): self.pth_raw = glob.glob(op.join(pth_mx_sbj, self.subject + '_' + self.experiment + '*'))[-1] is_MX = True else: is_MX = False return is_MX def detect_bad_channels(self, zscore_v=4, overwrite=False, method='both', neigh_max_distance=.035): """ zscore_v = zscore threshold, save_csv: path_tosaveCSV""" fname = self.subject + '_' + self.experiment + '_bads.csv' out_csv_f = op.join(self.out_bd_ch, fname) if op.exists(out_csv_f) and not overwrite: bad_chns = csv_read(out_csv_f) print('Reading from file, bad chans are:', bad_chns) else: from itertools import compress print('Looking for bad channels') # set recording length Fs = self.raw.info['sfreq'] t1x = 30 t2x = 220 t2 = min(self.raw.last_samp/Fs, t2x) t1 = max(0, t1x + t2-t2x) # Start earlier if recording is shorter # Get data raw_copy = self.raw.copy().crop(t1, t2).load_data() raw_copy = raw_copy.pick_types(meg=True, ref_meg=False)\ .filter(1, 45).resample(150, npad='auto') data_chans = raw_copy.get_data() # Get channel distances matrix chns_locs = np.asarray([x['loc'][:3] for x in raw_copy.info['chs']]) chns_dist = np.linalg.norm(chns_locs - chns_locs[:, None], axis=-1) chns_dist[chns_dist > neigh_max_distance] = 0 # Get avg channel uncorrelation between neighbours chns_corr = np.abs(np.corrcoef(data_chans)) weig = np.array(chns_dist, dtype=bool) chn_nei_corr = np.average(chns_corr, axis=1, weights=weig) chn_nei_uncorr_z = zscore(1-chn_nei_corr) # l ower corr higer Z # Get channel magnitudes max_Pow = np.sqrt(np.sum(data_chans ** 2, axis=1)) max_Z = zscore(max_Pow) if method == 'corr': # Based on local uncorrelation feat_vec = chn_nei_uncorr_z max_th = feat_vec > zscore_v elif method == 'norm': # Based on magnitude feat_vec = max_Z max_th = feat_vec > zscore_v elif method == 'both': # Combine uncorrelation with magnitude feat_vec = (chn_nei_uncorr_z+max_Z)/2 max_th = (feat_vec) > zscore_v bad_chns = list(compress(raw_copy.info['ch_names'], max_th)) raw_copy.info['bads'] = bad_chns if bad_chns: print(['Plotting data,bad chans are:'] + bad_chns) pfig(), pplot(feat_vec), plt.axhline(zscore_v) plt.title(['Plotting data,bad chans are:'] + bad_chns) raw_copy.plot(n_channels=100, block=True, bad_color='r') bad_chns = raw_copy.info['bads'] print('Bad chans are:', bad_chns) else: print('No bad chans found') csv_save(bad_chns, out_csv_f) self.ch_max_Z = max_Z self.raw.info['bads'] = bad_chns def detect_movement(self, thr_mov=.01, plot=True, overwrite=False, save=True): from mne.transforms import read_trans fname = self.subject + '_' + self.experiment + '_mov.txt' out_csv_f = op.join(self.out_annot, fname) fname_t = self.subject + '_' + self.experiment + '_dev2head-trans.fif' out_csv_f_t = op.join(self.out_annot, fname_t) if op.exists(out_csv_f) and not overwrite: mov_annot = read_annotations(out_csv_f) print('Reading from file, mov segments are:', mov_annot) print('Reading from file, dev to head transformation') dev_head_t = read_trans(out_csv_f_t) else: print('Calculating head pos') pos = mne.chpi._calculate_head_pos_ctf(self.raw, gof_limit=-1) mov_annot, hpi_disp, dev_head_t = annotate_motion(self.raw, pos, thr=thr_mov) if plot is True: plt.figure() plt.plot(hpi_disp) plt.axhline(y=thr_mov, color='r') plt.show(block=True) if save is True: mov_annot.save(out_csv_f) dev_head_t.save(out_csv_f_t) #fig.savefig(out_csv_f[:-4]+'.png') old_annot = self.raw.annotations # if orig_time cant + with none time self.raw.set_annotations(mov_annot) self.raw.set_annotations(self.raw.annotations + old_annot) self.raw.info['dev_head_t_old'] = self.raw.info['dev_head_t'] self.raw.info['dev_head_t'] = dev_head_t self.annot_movement = mov_annot def detect_muscle(self, thr=1.5, t_min=.5, plot=True, overwrite=False): """Find and annotate mucsle artifacts - by Luke Bloy""" fname = self.subject + '_' + self.experiment + '_mus.txt' out_csv_f = op.join(self.out_annot, fname) if op.exists(out_csv_f) and not overwrite: mus_annot = read_annotations(out_csv_f) print('Reading from file, muscle segments are:', mus_annot) else: print('Calculating muscle artifacts') raw = self.raw.copy().load_data() raw.pick_types(meg=True, ref_meg=False) raw.notch_filter(np.arange(60, 241, 60), fir_design='firwin') raw.filter(110, 140, fir_design='firwin') raw.apply_hilbert(envelope=True) sfreq = raw.info['sfreq'] art_scores = stats.zscore(raw._data, axis=1) # band pass filter the data art_scores_filt = mne.filter.filter_data(art_scores.mean(axis=0), sfreq, None, 4) art_mask = art_scores_filt > thr # remove artifact free periods shorter than t_min idx_min = t_min * sfreq comps, num_comps = label(art_mask == 0) for l in range(1, num_comps+1): l_idx = np.nonzero(comps == l)[0] if len(l_idx) < idx_min: art_mask[l_idx] = True mus_annot = _annotations_from_mask(raw.times, art_mask, 'Bad-muscle') if plot: del raw print('Plotting data, mark or delete art, by pressing a \n' 'Marked or demarked channels will be saved') old_bd_chns = self.raw.info['bads'] raw = self.raw.copy().load_data().pick_types(meg=True, ref_meg=False) raw.notch_filter(np.arange(60, 181, 60), fir_design='firwin') raw.filter(1, 140) old_annot = raw.annotations # if orig_time cant + none raw.set_annotations(mus_annot) raw.set_annotations(raw.annotations + old_annot) raw.plot(n_channels=140, block=True, bad_color='r') mus_annot = raw.annotations if not (old_bd_chns == raw.info['bads']): bad_chns = raw.info['bads'] print('Saving new bad channels list \n ') print('Bad chans are:', bad_chns) fname = self.subject + '_' + self.experiment + '_bads.csv' csv_save(bad_chns, op.join(self.out_bd_ch, fname)) mus_annot.save(out_csv_f) old_annot = self.raw.annotations # if orig_time cant + with none time self.raw.set_annotations(mus_annot) self.raw.set_annotations(self.raw.annotations + old_annot) self.annot_muscle = mus_annot def run_ICA(self, overwrite=False): fname = self.subject + '_' + self.experiment + '-ica.fif.gz' out_fname = op.join(self.out_ICAs, fname) if op.exists(out_fname) and not overwrite: self.ica = mne.preprocessing.read_ica(out_fname) else: from mne.preprocessing import ICA raw_copy = self.raw.copy().load_data().filter(1, 45) self.ica = ICA(method='fastica', random_state=42, n_components=0.99, max_iter=1000) picks = mne.pick_types(raw_copy.info, meg=True, ref_meg=False, stim=False, exclude='bads') reject = dict(grad=4000e-13, mag=6e-12) # what rejec intervals? self.ica.fit(raw_copy, picks=picks, reject=reject, decim=3) self.ica.detect_artifacts(raw_copy) self.ica.done = False self.ica.save(out_fname) def plot_ICA(self, check_if_done=True, overwrite=False): fname = self.subject + '_' + self.experiment + '-ica.fif.gz' out_fname = op.join(self.out_ICAs, fname) # Load previous ICA instance if op.exists(out_fname) and not overwrite: self.ica = mne.preprocessing.read_ica(out_fname) else: # self.run_ICA(self) return # Check if ICA comps were inspected data_not_clean = True if check_if_done is True: if self.ica.info['description'] == 'done': data_not_clean = False # Plot interactively to select bad comps if data_not_clean is True: raw_copy = self.raw.copy().load_data().filter(1, 45) while data_not_clean is True: # ICA comp plotting self.ica.plot_components(inst=raw_copy) self.ica.plot_sources(raw_copy, block=True) # Clean and raw sensor plotting raw_plot = raw_copy.copy().pick_types(meg=True, ref_meg=False) raw_plot.plot(n_channels=80, title='NO ICA') raw_ica = raw_copy.copy().pick_types(meg=True, ref_meg=False) self.ica.apply(raw_ica) raw_ica.plot(n_channels=80, title='ICA cleaned', block=True) data_not_clean = bool(int(input("Select other ICA components? " "[0-no, 1-yes]: "))) if data_not_clean is False: self.ica.info['description'] = 'done' self.ica.save(out_fname) #self.ica.apply(self.raw.load_data()) def get_events(self, plot=False, movie_annot=None): # general description of the data raw_copy = self.raw.copy() task = self.experiment fs = raw_copy.info['sfreq'] time = raw_copy.buffer_size_sec N_samples = raw_copy.n_times ID = self.subject print('Data for subject ' + ID + ' is composed from') print(str(N_samples) + ' samples with ' + str(fs) + ' Hz sampl rate') print('Total time = ' + str(time) + 's') # get photodiode events from CTF data PD_ts, Ind_PD_ON, Ind_PD_OFF, T_PD = get_photodiode_events(raw_copy, fs) # pick Trigger channel time series from CTF data Trig = mne.io.pick.pick_channels_regexp(raw_copy.info['ch_names'], 'UPPT001') Trig_ts = raw_copy.get_data(picks=Trig) # get events from trigger channel events_trig = mne.find_events(raw_copy, stim_channel='UPPT001', shortest_event=1) print(str(len(Ind_PD_ON)) + ' PD ONSETS FOUND') if task == 'CarTask': event_id = {'Transp/H2L': 10, 'Transp/L2H': 20, 'NotTransp/H2L': 30, 'NotTransp/L2H': 40} # get trigger names for PD ON states events = get_triger_names_PD(event_id, Ind_PD_ON, events_trig) all_trl_info, col_info = get_all_trl_info(event_id, Ind_PD_ON, Ind_PD_OFF, events_trig) self.all_trl_info = all_trl_info self.all_trl_info_col_names = col_info # get different event length for each condition event_len = {'Transp/H2L': [-.86, 2.42], 'Transp/L2H': [-3.17, 2.3], 'NotTransp/H2L': [-.9, 2.37], 'NotTransp/L2H': [-3.17, 2.1]} self.event_len = event_len elif task == 'Movie': if movie_annot is not None: if fs != 600: raise ValueError('Sampling must be 600Hz') event_id, events = get_pd_annotations(Ind_PD_ON, events_trig, movie_annot) else: event_id = {'SceneOnset': 1} events = np.zeros((len(Ind_PD_ON), 3)) events[:, 0] = Ind_PD_ON events[:, 2] = 1 all_trl_info = None elif task == 'Flanker': event_id = {'Incongruent/Left': 3, 'Incongruent/Right': 5, 'Congruent/Left': 4, 'Congruent/Right': 6, 'Reward/Face': 7, 'Reward/Coin': 8, 'Reward/Neut_FaceTRL': 9, 'Reward/Neut_CoinTRL': 10} # get trigger names for PD ON states events = get_triger_names_PD(event_id, Ind_PD_ON, events_trig) all_trl_info = None events = events.astype(np.int64) # plot trig and PD if plot: plt.figure() plot_events(PD_ts, Ind_PD_ON, T_PD, Ind_PD_OFF, Trig_ts, events, task, ID, all_trl_info=all_trl_info) self.event_id = event_id self.events = events def epoching(self, tmin=-0.5, tmax=0.5, plot=False, f_min=1, f_max=45, overwrite=False, apply_ica=True, cond_name=None, movie_annot=None, save=True): if cond_name is not None: fname = "%s_%s_%s-epo.fif" % (self.subject, self.experiment, cond_name.replace('/', '')) else: fname = "%s_%s-epo.fif" % (self.subject, self.experiment) out_fname = self.out_srcData + '/' + fname if op.exists(out_fname) and not overwrite: print('Reading epoched data from file') self.epochs = mne.read_epochs(out_fname) else: self.get_events(plot, movie_annot) raw_copy = self.raw.copy().load_data() info = raw_copy.info picks = mne.pick_types(info, meg=True, ref_meg=False) stim_ch = ['UADC015-3007', 'UPPT001'] pick_stim = mne.pick_channels(info['ch_names'], stim_ch) pick_epo = np.append(picks, pick_stim) raw_copy.pick(pick_epo) pick_fil = mne.pick_types(raw_copy.info, meg=True, ref_meg=False) raw_copy.filter(f_min, f_max, picks=pick_fil) if cond_name is not None: # Different conds diff lengths event = self.events ids = self.event_id[cond_name] tmin, tmax = self.event_len[cond_name] events = event[event[:, 2] == ids] epochs = mne.Epochs(raw_copy, events=events, tmin=tmin, tmax=tmax, event_id={cond_name: ids}, baseline=(tmin, 0.0)).load_data() else: epochs = mne.Epochs(raw_copy, events=self.events, tmin=tmin, tmax=tmax, event_id=self.event_id, baseline=(tmin, 0.0)).load_data() self.epochs = epochs if apply_ica is True: if hasattr(self, 'ica'): # Do ICA only on meg chns piks = mne.pick_types(epochs.info, meg=True) epochs_data = epochs.copy().pick(piks) self.ica.apply(epochs_data) self.epochs._data[:, piks, :] = epochs_data._data else: return self.epochs.save(out_fname, overwrite=overwrite) def src_modelling(self, spacing=['oct5'], overwrite=False): from mne import (read_forward_solution, make_forward_solution, write_forward_solution, setup_source_space) subject = self.subject task = self.experiment mne.set_config('SUBJECTS_DIR', self.pth_FS) FS_subj = op.join(self.pth_FS, subject) fname_trans = op.join(FS_subj, subject + '-trans.fif') fname_bem = op.join(FS_subj, '%s-bem_sol.fif' % subject) if not op.exists(fname_bem) or overwrite: # make_watershed_bem already run in the sh script # mne.bem.make_watershed_bem(subject, overwrite=True, # volume='T1', atlas=True, gcaatlas=False, # preflood=None) model = mne.make_bem_model(subject, ico=4, conductivity=[0.3]) bem = mne.make_bem_solution(model) mne.write_bem_solution(fname_bem, bem) else: bem = mne.read_bem_solution(fname_bem) for space in spacing: fname_src = op.join(FS_subj, 'bem', '%s-src.fif' % space) bname_fwd = '%s_%s_%s-fwd.fif' % (subject, task, space) fname_fwd = op.join(self.out_srcData, bname_fwd) if not op.exists(fname_src) or overwrite: src = setup_source_space(subject, space, subjects_dir=self.pth_FS) src.save(fname_src, overwrite=overwrite) if op.exists(fname_fwd) and not overwrite: self.fwd = read_forward_solution(fname_fwd) else: self.fwd = make_forward_solution(self.raw.info, fname_trans, fname_src, fname_bem) write_forward_solution(fname_fwd, self.fwd, overwrite) def mne_cov(self, overwrite=False): from mne import compute_covariance, read_cov fname = self.subject + '_' + self.experiment + '_ncov-cov.fif.gz' out_fname = op.join(self.out_srcData, fname) if op.exists(out_fname) and not overwrite: print('Reading noise covariance from file') self.ncov = read_cov(out_fname) else: self.ncov = compute_covariance(self.epochs, tmax=0, method='shrunk') self.ncov.save(out_fname) def mne_inv_operator(self, overwrite=False): from mne.minimum_norm import (read_inverse_operator, make_inverse_operator, write_inverse_operator) fname = self.subject + '_' + self.experiment + '_mne-inv.fif.gz' out_fname = op.join(self.out_srcData, fname) if op.exists(out_fname) and not overwrite: print('Reading inverse operator from file') self.inv = read_inverse_operator(out_fname) else: self.inv = make_inverse_operator(self.epochs.info, self.fwd, self.ncov, loose=0.2, depth=0.8) write_inverse_operator(out_fname, self.inv)
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 params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) ## score funcs epochs ## # check score funcs for name, func in score_funcs.items(): if name in score_funcs_unsuited: continue scores = ica.find_sources_epochs(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.find_sources_epochs(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.find_sources_raw(raw, target='MEG 1531', score_func='pearsonr') ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.find_sources_raw(raw, target='EOG 061', score_func='pearsonr') eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.sources_as_raw(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test_ica.fif') ica_raw.save(test_ica_fname) ica_raw2 = fiff.Raw(test_ica_fname, preload=True) assert_array_almost_equal(ica_raw._data, ica_raw2._data) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.sources_as_epochs(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) sources_epochs = ica.get_sources_epochs(epochs) assert_array_equal(ica_epochs.get_data(), sources_epochs) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs.raw is None) assert_true(ica_epochs.preload == True) # regression test for plot method assert_raises(ValueError, ica.plot_sources_raw, raw, order=np.arange(50)) assert_raises(ValueError, ica.plot_sources_epochs, epochs, order=np.arange(50))
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_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( 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 params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) ## score funcs epochs ## # check score funcs for name, func in score_funcs.items(): if name in score_funcs_unsuited: continue scores = ica.find_sources_epochs(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.find_sources_epochs(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.find_sources_raw(raw, target='MEG 1531', score_func='pearsonr') ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.find_sources_raw(raw, target='EOG 061', score_func='pearsonr') eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.sources_as_raw(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test_ica.fif') ica_raw.save(test_ica_fname) ica_raw2 = fiff.Raw(test_ica_fname, preload=True) assert_array_almost_equal(ica_raw._data, ica_raw2._data) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.sources_as_epochs(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) sources_epochs = ica.get_sources_epochs(epochs) assert_array_equal(ica_epochs.get_data(), sources_epochs) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs.raw is None) assert_true(ica_epochs.preload == True) # regression test for plot method assert_raises(ValueError, ica.plot_sources_raw, raw, order=np.arange(50)) assert_raises(ValueError, ica.plot_sources_epochs, epochs, order=np.arange(50))