def test_epochs_copy(): """Test copy epochs """ epochs = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, reject=reject, flat=flat) copied = epochs.copy() assert_array_equal(epochs._data, copied._data) epochs = Epochs(raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=False, reject=reject, flat=flat) copied = epochs.copy() data = epochs.get_data() copied_data = copied.get_data() assert_array_equal(data, copied_data)
def interpolate_bad_channels(epochs: Epochs, bads: list) -> Epochs: """ Interpolates channels in an Epochs instance based on a list of channel names. Parameters ---------- epochs bads Returns ------- """ if not epochs.preload: epochs_interpolated = epochs.copy().load_data() else: epochs_interpolated = epochs.copy() epochs_interpolated.info["bads"] = bads if bads: bads_str = ", ".join(bads) description = f", interpolated: {bads_str}" epochs_interpolated.info.update( description=epochs.info["description"] + description) epochs_interpolated.interpolate_bads(reset_bads=True) epochs_interpolated.info.update(temp=f'{epochs.info["temp"]}_ransac') return epochs_interpolated
def run_autoreject(epochs: Epochs, n_jobs: int = 11, subset: bool = False) -> autoreject.RejectLog: """ Finds bad epochs based on AutoReject. Parameters ---------- epochs: the instance to be cleaned n_jobs: the number of parallel processes to be run subset: whether to train autoreject on a random subset of data (faster) Returns ------- Autoreject instance """ if not epochs.preload: epochs_autoreject = epochs.copy().load_data() else: epochs_autoreject = epochs.copy() ar = autoreject.AutoReject(random_state=42, n_jobs=n_jobs) n_epochs = len(epochs_autoreject) if subset: logger.info(f"Fitting autoreject on random (n={int(n_epochs * 0.25)}) " f"subset of epochs: ") subset = sample(set(np.arange(0, n_epochs, 1)), int(n_epochs * 0.25)) ar.fit(epochs_autoreject[subset]) else: logger.info(f"Fitting autoreject on (n={n_epochs}) epochs: ") ar.fit(epochs_autoreject) reject_log = ar.get_reject_log(epochs_autoreject) # report bad epochs where more than 15% (value updated from config.py) # of channels were marked as noisy within an epoch threshold = settings["autoreject"]["threshold"] num_bad_epochs = np.count_nonzero(reject_log.labels, axis=1) bad_epochs = np.where(num_bad_epochs > threshold * epochs.info["nchan"]) bad_epochs = bad_epochs[0].tolist() reject_log.report = bad_epochs auto_bad_epochs = np.where(reject_log.bad_epochs)[0].tolist() if len(bad_epochs) < len(auto_bad_epochs): reject_log.report = sorted(list(set(bad_epochs + auto_bad_epochs))) logger.info( "\nAUTOREJECT report\n" f"There are {len(epochs_autoreject[reject_log.bad_epochs])} " f"bad epochs found with Autoreject. " f"You can assess these epochs with reject_log.bad_epochs\n" f"\nThere are {len(reject_log.report)} bad epochs where more than " f"{int(threshold * 100)}% of the channels were noisy. " f"You can assess these epochs with reject_log.report") return reject_log
def compute_power(epochs: Epochs, config: dict) -> AverageTFR: """ Computes Time-Frequency Representation (TFR) using complex Morlet wavelets averaged over epochs. Power is written to an HDF5 file. Parameters ---------- epochs config """ fmin = config["morlet"]["fmin"] fmax = config["morlet"]["fmax"] step = config["morlet"]["step"] freqs = np.logspace(*np.log10([fmin, fmax]), num=step) n_cycles = freqs / 2.0 power = tfr_morlet( epochs.average() if config["is_evoked"] else epochs.copy(), freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=False, decim=config["morlet"]["decim"], average=True, n_jobs=-1, ) return power
def _load_custom_data(self, filter, pick): matFile = sio.loadmat(self.filename) data = matFile['epoch'] self.info = self._mock_raw_info() ###########################先滤波 # event_channel = data[-1:,:] # # _data = mne.filter.filter_data(data[:-1,:],self.info['sfreq'],7,30) # _data = self.myfilter(data[:-1,:]) # data = np.concatenate((_data,event_channel),axis=0) ########################### self.raw = mne.io.RawArray(data, self.info) # raw = mne.io.read_raw_cnt(filesToOpen[0],None) if filter: self.raw.filter(7., 30., fir_design='firwin') events = find_events(self.raw, shortest_event=1, stim_channel='STI 014') picks = None if pick: picks = pick_types(self.info, meg=False, eeg=True, stim=False, eog=False, exclude=['eog', 'stim']) epochs = Epochs(self.raw, events, self.event_id, self.tmin, self.tmax, picks=picks, preload=True) labels = epochs.events[:, -1] epochs_train = epochs.copy().crop(tmin=0.5, tmax=3.5).get_data() # return epochs_train, labels
def compute_surface_laplacian(epochs: Epochs, verbose: bool = True) -> Epochs: """ Performs the surface Laplacian transform on the Epochs instance For more information about the transform parameters please refer to Cohen, M. X. (2014). Analyzing neural time series data: theory and practice . MIT press. For more information about this function in particular, visit the MNE documentation at: https://mne.tools/dev/generated/mne.preprocessing.compute_current_source_density.html Parameters ---------- epochs: the epochs to be transformed verbose: whether to visualize the power spectral densities before and after the Laplacian transform Returns ------- Raw instance """ epochs_csd = compute_current_source_density(epochs.copy()) if verbose: fig, axes_subplot = plt.subplots( nrows=2, ncols=1, sharex="all", sharey="all", dpi=220 ) epochs.plot_psd(ax=axes_subplot[0], show=False, fmax=60) epochs_csd.plot_psd(ax=axes_subplot[1], show=False, fmax=60) fig.show() return epochs_csd
def test_cov_mismatch(): """Test estimation with MEG<->Head mismatch.""" raw = read_raw_fif(raw_fname).crop(0, 5).load_data() events = find_events(raw, stim_channel='STI 014') raw.pick_channels(raw.ch_names[:5]) raw.add_proj([], remove_existing=True) epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0., preload=True) for kind in ('shift', 'None'): epochs_2 = epochs.copy() # This should be fine compute_covariance([epochs, epochs_2]) if kind == 'shift': epochs_2.info['dev_head_t']['trans'][:3, 3] += 0.001 else: # None epochs_2.info['dev_head_t'] = None pytest.raises(ValueError, compute_covariance, [epochs, epochs_2]) compute_covariance([epochs, epochs_2], on_mismatch='ignore') with pytest.raises(RuntimeWarning, match='transform mismatch'): compute_covariance([epochs, epochs_2], on_mismatch='warn') with pytest.raises(ValueError, match='Invalid value'): compute_covariance(epochs, on_mismatch='x') # This should work epochs.info['dev_head_t'] = None epochs_2.info['dev_head_t'] = None compute_covariance([epochs, epochs_2], method=None)
def test_cov_mismatch(): """Test estimation with MEG<->Head mismatch.""" raw = read_raw_fif(raw_fname, add_eeg_ref=False).crop(0, 5).load_data() events = find_events(raw, stim_channel="STI 014") raw.pick_channels(raw.ch_names[:5]) raw.add_proj([], remove_existing=True) epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0.0, preload=True, add_eeg_ref=False) for kind in ("shift", "None"): epochs_2 = epochs.copy() # This should be fine with warnings.catch_warnings(record=True) as w: compute_covariance([epochs, epochs_2]) assert_equal(len(w), 0) if kind == "shift": epochs_2.info["dev_head_t"]["trans"][:3, 3] += 0.001 else: # None epochs_2.info["dev_head_t"] = None assert_raises(ValueError, compute_covariance, [epochs, epochs_2]) assert_equal(len(w), 0) compute_covariance([epochs, epochs_2], on_mismatch="ignore") assert_equal(len(w), 0) compute_covariance([epochs, epochs_2], on_mismatch="warn") assert_raises(ValueError, compute_covariance, epochs, on_mismatch="x") assert_true(any("transform mismatch" in str(ww.message) for ww in w)) # This should work epochs.info["dev_head_t"] = None epochs_2.info["dev_head_t"] = None compute_covariance([epochs, epochs_2], method=None)
def test_epochs_copy(): """Test copy epochs """ epochs = Epochs( raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, reject=reject, flat=flat ) copied = epochs.copy() assert_array_equal(epochs._data, copied._data) epochs = Epochs( raw, events[:5], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=False, reject=reject, flat=flat ) copied = epochs.copy() data = epochs.get_data() copied_data = copied.get_data() assert_array_equal(data, copied_data)
def test_cov_mismatch(): """Test estimation with MEG<->Head mismatch.""" raw = read_raw_fif(raw_fname).crop(0, 5).load_data() events = find_events(raw, stim_channel='STI 014') raw.pick_channels(raw.ch_names[:5]) raw.add_proj([], remove_existing=True) epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0., preload=True) for kind in ('shift', 'None'): epochs_2 = epochs.copy() # This should be fine with warnings.catch_warnings(record=True) as w: compute_covariance([epochs, epochs_2]) assert_equal(len(w), 0) if kind == 'shift': epochs_2.info['dev_head_t']['trans'][:3, 3] += 0.001 else: # None epochs_2.info['dev_head_t'] = None assert_raises(ValueError, compute_covariance, [epochs, epochs_2]) assert_equal(len(w), 0) compute_covariance([epochs, epochs_2], on_mismatch='ignore') assert_equal(len(w), 0) compute_covariance([epochs, epochs_2], on_mismatch='warn') assert_raises(ValueError, compute_covariance, epochs, on_mismatch='x') assert_true(any('transform mismatch' in str(ww.message) for ww in w)) # This should work epochs.info['dev_head_t'] = None epochs_2.info['dev_head_t'] = None compute_covariance([epochs, epochs_2], method=None)
def test_cov_mismatch(): """Test estimation with MEG<->Head mismatch.""" raw = read_raw_fif(raw_fname).crop(0, 5).load_data() events = find_events(raw, stim_channel='STI 014') raw.pick_channels(raw.ch_names[:5]) raw.add_proj([], remove_existing=True) epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0., preload=True) for kind in ('shift', 'None'): epochs_2 = epochs.copy() # This should be fine compute_covariance([epochs, epochs_2]) if kind == 'shift': epochs_2.info['dev_head_t']['trans'][:3, 3] += 0.001 else: # None epochs_2.info['dev_head_t'] = None pytest.raises(ValueError, compute_covariance, [epochs, epochs_2]) compute_covariance([epochs, epochs_2], on_mismatch='ignore') with pytest.raises(RuntimeWarning, match='transform mismatch'): compute_covariance([epochs, epochs_2], on_mismatch='warn') pytest.raises(ValueError, compute_covariance, epochs, on_mismatch='x') # This should work epochs.info['dev_head_t'] = None epochs_2.info['dev_head_t'] = None compute_covariance([epochs, epochs_2], method=None)
def test_cov_mismatch(): """Test estimation with MEG<->Head mismatch.""" raw = read_raw_fif(raw_fname).crop(0, 5).load_data() events = find_events(raw, stim_channel='STI 014') raw.pick_channels(raw.ch_names[:5]) raw.add_proj([], remove_existing=True) epochs = Epochs(raw, events, None, tmin=-0.2, tmax=0., preload=True) for kind in ('shift', 'None'): epochs_2 = epochs.copy() # This should be fine with warnings.catch_warnings(record=True) as w: compute_covariance([epochs, epochs_2]) assert_equal(len(w), 0) if kind == 'shift': epochs_2.info['dev_head_t']['trans'][:3, 3] += 0.001 else: # None epochs_2.info['dev_head_t'] = None assert_raises(ValueError, compute_covariance, [epochs, epochs_2]) assert_equal(len(w), 0) compute_covariance([epochs, epochs_2], on_mismatch='ignore') assert_equal(len(w), 0) compute_covariance([epochs, epochs_2], on_mismatch='warn') assert_raises(ValueError, compute_covariance, epochs, on_mismatch='x') assert_true(any('transform mismatch' in str(ww.message) for ww in w)) # This should work epochs.info['dev_head_t'] = None epochs_2.info['dev_head_t'] = None compute_covariance([epochs, epochs_2], method=None)
def test_decim(): """Test evoked decimation.""" rng = np.random.RandomState(0) n_channels, n_times = 10, 20 dec_1, dec_2 = 2, 3 decim = dec_1 * dec_2 sfreq = 10. sfreq_new = sfreq / decim data = rng.randn(n_channels, n_times) info = create_info(n_channels, sfreq, 'eeg') with info._unlock(): info['lowpass'] = sfreq_new / float(decim) evoked = EvokedArray(data, info, tmin=-1) evoked_dec = evoked.copy().decimate(decim) evoked_dec_2 = evoked.copy().decimate(decim, offset=1) evoked_dec_3 = evoked.decimate(dec_1).decimate(dec_2) assert_array_equal(evoked_dec.data, data[:, ::decim]) assert_array_equal(evoked_dec_2.data, data[:, 1::decim]) assert_array_equal(evoked_dec.data, evoked_dec_3.data) # Check proper updating of various fields assert evoked_dec.first == -2 assert evoked_dec.last == 1 assert_array_equal(evoked_dec.times, [-1, -0.4, 0.2, 0.8]) assert evoked_dec_2.first == -2 assert evoked_dec_2.last == 1 assert_array_equal(evoked_dec_2.times, [-0.9, -0.3, 0.3, 0.9]) assert evoked_dec_3.first == -2 assert evoked_dec_3.last == 1 assert_array_equal(evoked_dec_3.times, [-1, -0.4, 0.2, 0.8]) # make sure the time nearest zero is also sample number 0. for ev in (evoked_dec, evoked_dec_2, evoked_dec_3): lowest_index = np.argmin(np.abs(np.arange(ev.first, ev.last))) idxs_of_times_nearest_zero = \ np.where(np.abs(ev.times) == np.min(np.abs(ev.times)))[0] # we use `in` here in case two times are equidistant from 0. assert lowest_index in idxs_of_times_nearest_zero assert len(idxs_of_times_nearest_zero) in (1, 2) # Now let's do it with some real data raw = read_raw_fif(raw_fname) events = read_events(event_name) sfreq_new = raw.info['sfreq'] / decim with raw.info._unlock(): raw.info['lowpass'] = sfreq_new / 4. # suppress aliasing warnings picks = pick_types(raw.info, meg=True, eeg=True, exclude=()) epochs = Epochs(raw, events, 1, -0.2, 0.5, picks=picks, preload=True) for offset in (0, 1): ev_ep_decim = epochs.copy().decimate(decim, offset).average() ev_decim = epochs.average().decimate(decim, offset) expected_times = epochs.times[offset::decim] assert_allclose(ev_decim.times, expected_times) assert_allclose(ev_ep_decim.times, expected_times) expected_data = epochs.get_data()[:, :, offset::decim].mean(axis=0) assert_allclose(ev_decim.data, expected_data) assert_allclose(ev_ep_decim.data, expected_data) assert_equal(ev_decim.info['sfreq'], sfreq_new) assert_array_equal(ev_decim.times, expected_times)
def loadthings(subject): runs = [6, 10, 14] raw_fnames = eegbci.load_data(subject, runs) raw = concatenate_raws([read_raw_edf(f, preload=True) for f in raw_fnames]) raw_fnames = eegbci.load_data(subject, runs) raw = concatenate_raws([read_raw_edf(f, preload=True) for f in raw_fnames]) eegbci.standardize(raw) #create 10-05 system montage = make_standard_montage('standard_1005') raw.set_montage(montage) raw.filter(7, 13) events, _ = events_from_annotations(raw, event_id=dict(T1=0, T2=1)) #0: left, 1:right picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') tmin, tmax = -1, 4 epochs = Epochs(raw, events, None, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) #epochs_train=epochs.copy().crop(0,2) epochs_train = epochs.copy().crop(0, 2) labels = epochs.events[:, -1] # %% epochs_train_data = epochs_train.get_data() labels = epochs_train.events[:, -1] ad = np.array(epochs_train_data) All_Data = ad.reshape( 64, epochs_train_data.shape[2] * epochs_train_data.shape[0]) labels = labels.reshape(45, 1) extended = [] for i in range(321): extended.append(labels) extended = np.array(extended) extended = extended.reshape(45, 321) extended = extended.T Labels = extended #np.savetxt("pythondata/extended.csv", extended) row, column = Labels.shape Labels = Labels.reshape(1, row * column) Labels = Labels.T return All_Data, Labels
def test_ica_full_data_recovery(method): """Test recovery of full data when no source is rejected.""" # Most basic recovery _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(0.5, stop).load_data() events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] with pytest.warns(RuntimeWarning, match='projection'): epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) evoked = epochs.average() n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() data_evoked = evoked.data raw.set_annotations(Annotations([0.5], [0.5], ['BAD'])) methods = [method] for method in methods: stuff = [(2, n_channels, True), (2, n_channels // 2, False)] for n_components, n_pca_components, ok in stuff: ica = ICA(n_components=n_components, random_state=0, max_pca_components=n_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1) with pytest.warns(UserWarning, match=None): # sometimes warns ica.fit(raw, picks=list(range(n_channels))) raw2 = ica.apply(raw.copy(), exclude=[]) if ok: assert_allclose(data[:n_channels], raw2._data[:n_channels], rtol=1e-10, atol=1e-15) else: diff = np.abs(data[:n_channels] - raw2._data[:n_channels]) assert (np.max(diff) > 1e-14) ica = ICA(n_components=n_components, method=method, max_pca_components=n_pca_components, n_pca_components=n_pca_components, random_state=0) with pytest.warns(None): # sometimes warns ica.fit(epochs, picks=list(range(n_channels))) epochs2 = ica.apply(epochs.copy(), exclude=[]) data2 = epochs2.get_data()[:, :n_channels] if ok: assert_allclose(data_epochs[:, :n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(data_epochs[:, :n_channels] - data2) assert (np.max(diff) > 1e-14) evoked2 = ica.apply(evoked.copy(), exclude=[]) data2 = evoked2.data[:n_channels] if ok: assert_allclose(data_evoked[:n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(evoked.data[:n_channels] - data2) assert (np.max(diff) > 1e-14) pytest.raises(ValueError, ICA, method='pizza-decomposision')
def load_data(user): tmin, tmax = -1., 4. event_id = dict(LEFT_HAND=2, RIGHT_HAND=3) raw_edf = [] path = op.join('data_i2r',user) directories = os.listdir(path) for data_folder in directories: file_list = glob.glob('data_i2r' + '/' + user + '/' + data_folder + '/' + 'DataTraining' + '/*.edf') raw_files = [read_raw_edf(raw_fnames, preload=True, stim_channel='auto')for raw_fnames in file_list] raw_edf.extend(raw_files) raw = concatenate_raws(raw_edf) # strip channel names of "." characters raw.rename_channels(lambda x: x.strip('.')) # Apply band-pass filter raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge') events = find_events(raw, shortest_event=0, stim_channel='STI 014') picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') print events[:10] # Read epochs (train will be done only between 0.5 and 2.5s) # Testing will be done with a running classifier epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) epochs_train = epochs.copy().crop(tmin=0.5, tmax=2.5) labels = epochs.events[:, -1] - 2 epochs_data = epochs.get_data() epochs_data_train = epochs_train.get_data() #split data into training and testing set cv = ShuffleSplit(10, test_size=0.2, random_state=42) cv_split = cv.split(epochs_data_train) X_train =[] X_test =[] y_train = [] y_test = [] for train_idx, test_idx in cv_split: X_train, X_test = epochs_data_train[train_idx],epochs_data_train[test_idx] y_train, y_test = labels[train_idx], labels[test_idx] return X_train,y_train,X_test,y_test # if __name__ == '__main__': # data_directory = 'data_i2r'; # user = '******' # (X_train,y_train,X_test,y_test)=load_data(user) # print ("train data size is " + str(X_train.size)) # print ("test data size is "+ str(X_test.size))
def test_ica_full_data_recovery(): """Test recovery of full data when no source is rejected""" # Most basic recovery raw = Raw(raw_fname).crop(0.5, stop, copy=False) raw.load_data() events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] with warnings.catch_warnings(record=True): # bad proj epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) evoked = epochs.average() n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() data_evoked = evoked.data for method in ['fastica']: stuff = [(2, n_channels, True), (2, n_channels // 2, False)] for n_components, n_pca_components, ok in stuff: ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1) with warnings.catch_warnings(record=True): ica.fit(raw, picks=list(range(n_channels))) raw2 = ica.apply(raw.copy(), exclude=[]) if ok: assert_allclose(data[:n_channels], raw2._data[:n_channels], rtol=1e-10, atol=1e-15) else: diff = np.abs(data[:n_channels] - raw2._data[:n_channels]) assert_true(np.max(diff) > 1e-14) ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components) with warnings.catch_warnings(record=True): ica.fit(epochs, picks=list(range(n_channels))) epochs2 = ica.apply(epochs.copy(), exclude=[]) data2 = epochs2.get_data()[:, :n_channels] if ok: assert_allclose(data_epochs[:, :n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(data_epochs[:, :n_channels] - data2) assert_true(np.max(diff) > 1e-14) evoked2 = ica.apply(evoked.copy(), exclude=[]) data2 = evoked2.data[:n_channels] if ok: assert_allclose(data_evoked[:n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(evoked.data[:n_channels] - data2) assert_true(np.max(diff) > 1e-14) assert_raises(ValueError, ICA, method='pizza-decomposision')
def test_ica_full_data_recovery(): """Test recovery of full data when no source is rejected""" # Most basic recovery raw = Raw(raw_fname).crop(0.5, stop, copy=False) raw.load_data() events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] with warnings.catch_warnings(record=True): # bad proj epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) evoked = epochs.average() n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() data_evoked = evoked.data for method in ['fastica']: stuff = [(2, n_channels, True), (2, n_channels // 2, False)] for n_components, n_pca_components, ok in stuff: ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1) with warnings.catch_warnings(record=True): ica.fit(raw, picks=list(range(n_channels))) raw2 = ica.apply(raw.copy(), exclude=[]) if ok: assert_allclose(data[:n_channels], raw2._data[:n_channels], rtol=1e-10, atol=1e-15) else: diff = np.abs(data[:n_channels] - raw2._data[:n_channels]) assert_true(np.max(diff) > 1e-14) ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components) with warnings.catch_warnings(record=True): ica.fit(epochs, picks=list(range(n_channels))) epochs2 = ica.apply(epochs.copy(), exclude=[]) data2 = epochs2.get_data()[:, :n_channels] if ok: assert_allclose(data_epochs[:, :n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(data_epochs[:, :n_channels] - data2) assert_true(np.max(diff) > 1e-14) evoked2 = ica.apply(evoked.copy(), exclude=[]) data2 = evoked2.data[:n_channels] if ok: assert_allclose(data_evoked[:n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(evoked.data[:n_channels] - data2) assert_true(np.max(diff) > 1e-14) assert_raises(ValueError, ICA, method='pizza-decomposision')
def test_epochs_proj_mixin(): """Test SSP proj methods from ProjMixin class """ for proj in [True, False]: epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=proj) assert_true(all(p['active'] == proj for p in epochs.info['projs'])) # test adding / deleting proj if proj: epochs.get_data() assert_true(all(p['active'] == proj for p in epochs.info['projs'])) assert_raises(ValueError, epochs.add_proj, epochs.info['projs'][0], {'remove_existing': True}) assert_raises(ValueError, epochs.add_proj, 'spam') assert_raises(ValueError, epochs.del_proj, 0) else: projs = deepcopy(epochs.info['projs']) n_proj = len(epochs.info['projs']) epochs.del_proj(0) assert_true(len(epochs.info['projs']) == n_proj - 1) epochs.add_proj(projs, remove_existing=False) assert_true(len(epochs.info['projs']) == 2 * n_proj - 1) epochs.add_proj(projs, remove_existing=True) assert_true(len(epochs.info['projs']) == n_proj) for preload in [True, False]: print 'preload is %s' % preload epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=False, preload=preload, add_eeg_ref=True, verbose=True) epochs2 = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=True, preload=preload, add_eeg_ref=True) assert_allclose(epochs.copy().apply_proj().get_data()[0], epochs2.get_data()[0]) # make sure the preprocssing works on copies by calling # get_data twice assert_array_equal(epochs2.get_data(), epochs2.get_data()) data = epochs.get_data().copy() data2 = np.array([e for e in epochs]) assert_array_equal(data, data2) epochs.apply_proj() assert_array_equal(epochs._projector, epochs2._projector) assert_allclose(epochs._data, epochs2.get_data()) epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=None, proj=False, add_eeg_ref=True) data = epochs.get_data().copy() epochs.apply_proj() assert_allclose(np.dot(epochs._projector, data[0]), epochs._data[0])
def get_cross_val_score(subject, runs=[6, 10, 14], event_id=dict(rest=1, hands=2, feet=3), n_components=4): tmin, tmax = -1., 4. raw = get_raw(subject, runs) events = find_events(raw, shortest_event=0, stim_channel='STI 014') picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) epochs_train = epochs.copy().crop(tmin=1., tmax=2.) labels = epochs.events[:, -1] - 1 epochs_data_train = epochs_train.get_data() cv = KFold(n_splits=5) svc = svm.SVC(kernel="linear") csp = mne.decoding.CSP(n_components=n_components, reg=None, log=True, norm_trace=False) scores = [] self_scores = [] for train, test in cv.split(epochs_data_train): # fit x = epochs_data_train[train] y = labels[train] x = csp.fit_transform(x, y) svc.fit(x, y) self_scores.append(svc.score(x, y)) # estimate x_test = epochs_data_train[test] y_test = labels[test] x_test = csp.transform(x_test) score = svc.score(x_test, y_test) scores.append(score) return np.mean(self_scores), np.mean(scores)
def test_equalize_channels(): """Test equalization of channels """ epochs1 = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=False) epochs2 = epochs1.copy() ch_names = epochs1.ch_names[2:] epochs1.drop_channels(epochs1.ch_names[:1]) epochs2.drop_channels(epochs2.ch_names[1:2]) my_comparison = [epochs1, epochs2] equalize_channels(my_comparison) for e in my_comparison: assert_equal(ch_names, e.ch_names)
def test_equalize_channels(): """Test equalization of channels """ epochs1 = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=False) epochs2 = epochs1.copy() ch_names = epochs1.ch_names[2:] epochs1.drop_channels(epochs1.ch_names[:1]) epochs2.drop_channels(epochs2.ch_names[1:2]) my_comparison = [epochs1, epochs2] equalize_channels(my_comparison) for e in my_comparison: assert_equal(ch_names, e.ch_names)
def generate_virtual_epoch(epochs: mne.Epochs, W: np.ndarray, frequency_mean: float = 10, frequency_std: float = 0.2, noise_phase_level: float = 0.005, noise_amplitude_level: float = 0.1) -> mne.Epochs: """ Generate epochs with simulated data using Kuramoto oscillators. Arguments: epoch: mne.Epochs Epochs object to get epoch info structure W: np.ndarray Coupling matrix between the oscillators frequency_mean: float Mean of the normal distribution for oscillators frequency frequency_std: float Standart deviation of the normal distribution for oscillators frequency noise_phase_level: float Amount of noise at the phase level noise_amplitude_level: float Amount of noise at the amplitude level Returns: mne.Epochs new epoch with simulated data """ n_epo, n_chan, n_samp = epochs.get_data().shape sfreq = epochs.info['sfreq'] N = int(n_chan / 2) Nt = n_samp * n_epo tmax = n_samp / sfreq * n_epo # s tv = np.linspace(0., tmax, Nt) freq = frequency_mean + frequency_std * np.random.randn(n_chan) omega = 2. * np.pi * freq def fp(t, p): p = np.atleast_2d(p) coupling = np.squeeze((np.sin(p) * np.matmul(W, np.cos(p).T).T) - (np.cos(p) * np.matmul(W, np.sin(p).T).T)) dotp = omega - coupling + noise_phase_level * np.random.randn(n_chan) / n_samp return dotp p0 = 2 * np.pi * np.block([np.zeros(N) + np.random.rand(N) + 0.5, np.zeros(N) + np.random.rand(N) + 0.5]) # initialization ans = solve_ivp(fun=fp, t_span=(tv[0], tv[-1]), y0=p0, t_eval=tv) phi = ans['y'].T % (2 * np.pi) eeg = np.sin(phi) + noise_amplitude_level * np.random.randn(*phi.shape) simulation = epochs.copy() simulation._data = np.transpose(np.reshape(eeg.T, [n_chan, n_epo, n_samp]), (1, 0, 2)) return simulation
def test_decim(): """Test evoked decimation.""" rng = np.random.RandomState(0) n_channels, n_times = 10, 20 dec_1, dec_2 = 2, 3 decim = dec_1 * dec_2 sfreq = 10. sfreq_new = sfreq / decim data = rng.randn(n_channels, n_times) info = create_info(n_channels, sfreq, 'eeg') info['lowpass'] = sfreq_new / float(decim) evoked = EvokedArray(data, info, tmin=-1) evoked_dec = evoked.copy().decimate(decim) evoked_dec_2 = evoked.copy().decimate(decim, offset=1) evoked_dec_3 = evoked.decimate(dec_1).decimate(dec_2) assert_array_equal(evoked_dec.data, data[:, ::decim]) assert_array_equal(evoked_dec_2.data, data[:, 1::decim]) assert_array_equal(evoked_dec.data, evoked_dec_3.data) # Check proper updating of various fields assert evoked_dec.first == -1 assert evoked_dec.last == 2 assert_array_equal(evoked_dec.times, [-1, -0.4, 0.2, 0.8]) assert evoked_dec_2.first == -1 assert evoked_dec_2.last == 2 assert_array_equal(evoked_dec_2.times, [-0.9, -0.3, 0.3, 0.9]) assert evoked_dec_3.first == -1 assert evoked_dec_3.last == 2 assert_array_equal(evoked_dec_3.times, [-1, -0.4, 0.2, 0.8]) # Now let's do it with some real data raw = read_raw_fif(raw_fname) events = read_events(event_name) sfreq_new = raw.info['sfreq'] / decim raw.info['lowpass'] = sfreq_new / 4. # suppress aliasing warnings picks = pick_types(raw.info, meg=True, eeg=True, exclude=()) epochs = Epochs(raw, events, 1, -0.2, 0.5, picks=picks, preload=True) for offset in (0, 1): ev_ep_decim = epochs.copy().decimate(decim, offset).average() ev_decim = epochs.average().decimate(decim, offset) expected_times = epochs.times[offset::decim] assert_allclose(ev_decim.times, expected_times) assert_allclose(ev_ep_decim.times, expected_times) expected_data = epochs.get_data()[:, :, offset::decim].mean(axis=0) assert_allclose(ev_decim.data, expected_data) assert_allclose(ev_ep_decim.data, expected_data) assert_equal(ev_decim.info['sfreq'], sfreq_new) assert_array_equal(ev_decim.times, expected_times)
def test_apply_reference(): """Test base function for rereferencing.""" raw = read_raw_fif(fif_fname, preload=True, add_eeg_ref=False) # Rereference raw data by creating a copy of original data reref, ref_data = _apply_reference(raw.copy(), ref_from=['EEG 001', 'EEG 002']) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # The CAR reference projection should have been removed by the function assert_true(not _has_eeg_average_ref_proj(reref.info['projs'])) # Test that disabling the reference does not break anything reref, ref_data = _apply_reference(raw, []) assert_array_equal(raw._data, reref._data) # Test that data is modified in place when copy=False reref, ref_data = _apply_reference(raw, ['EEG 001', 'EEG 002']) assert_true(raw is reref) # Test re-referencing Epochs object raw = read_raw_fif(fif_fname, preload=False, add_eeg_ref=False) events = read_events(eve_fname) picks_eeg = pick_types(raw.info, meg=False, eeg=True) epochs = Epochs(raw, events=events, event_id=1, tmin=-0.2, tmax=0.5, picks=picks_eeg, preload=True, add_eeg_ref=False) reref, ref_data = _apply_reference(epochs.copy(), ref_from=['EEG 001', 'EEG 002']) assert_true(reref.info['custom_ref_applied']) _test_reference(epochs, reref, ref_data, ['EEG 001', 'EEG 002']) # Test re-referencing Evoked object evoked = epochs.average() reref, ref_data = _apply_reference(evoked.copy(), ref_from=['EEG 001', 'EEG 002']) assert_true(reref.info['custom_ref_applied']) _test_reference(evoked, reref, ref_data, ['EEG 001', 'EEG 002']) # Test invalid input raw_np = read_raw_fif(fif_fname, preload=False, add_eeg_ref=False) assert_raises(RuntimeError, _apply_reference, raw_np, ['EEG 001'])
def grid_search(subject=1): runs = [6, 10, 14] event_id = dict(rest=1, hands=2, feet=3) tmin, tmax = -1., 4. raw = get_raw(subject, runs) events = find_events(raw, shortest_event=0, stim_channel='STI 014') picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) epochs_data_train = epochs.copy().crop(tmin=1., tmax=2.).get_data() labels = epochs.events[:, -1] csp = mne.decoding.CSP(reg=None, norm_trace=False, transform_into="csp_space") fft = FFT() svm = SVC() pl = Pipeline([("csp", csp), ("fft", fft), ("svm", svm)]) params = { "csp__n_components": np.arange(4, 10), "fft__R": np.arange(40, 110, 20), "svm__C": np.arange(1, 101, 25), "svm__gamma": np.logspace(-2, 2, 4) } clf = GridSearchCV(pl, params, n_jobs=3, cv=5, return_train_score=True) clf.fit(epochs_data_train, labels) df = pd.DataFrame(clf.cv_results_) df.to_excel("data/grid_fft/grid_fft_%s_1.xlsx" % subject, index=False, header=True) print("%s end" % subject)
def grid_search(subject=1): runs = [6, 10, 14] event_id = dict(rest=1, hands=2, feet=3) tmin, tmax = -1., 4. raw = get_raw(subject, runs) events = find_events(raw, shortest_event=0, stim_channel='STI 014') picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) epochs_data_train = epochs.copy().crop(tmin=1., tmax=2.).get_data() labels = epochs.events[:, -1] csp = mne.decoding.CSP(reg=None, log=True, norm_trace=False) svm = SVC() pl = Pipeline([("csp", csp), ("svm", svm)]) params = { "csp__n_components": np.arange(4, 10), "svm__C": np.arange(1000, 10000, 1000), "svm__gamma": np.logspace(-4, 0, 5) } def main1(): clf = GridSearchCV(pl, params, n_jobs=-1, cv=10) clf.fit(epochs_data_train, labels) df = pd.DataFrame(clf.cv_results_) df.to_excel("data/grid/grid_default_%s.xlsx" % subject, index=False, header=True) print("%s end" % subject) print(timeit.timeit(main1, number=1))
def apply_autoreject(epochs: Epochs, reject_log: autoreject.RejectLog) -> Epochs: """ Drop bad epochs based on AutoReject. Parameters ---------- epochs reject_log Returns ------- """ epochs_autoreject = epochs.copy().drop(reject_log.report, reason="AUTOREJECT") epochs_autoreject.info.update(temp=f'{epochs.info["temp"]}_autoreject') return epochs_autoreject
def apply_ica(epochs: Epochs, ica: ICA) -> Epochs: """ Applies ICA on Epochs instance. Parameters ---------- epochs ica Returns ------- """ ica_epochs = epochs.copy().load_data() ica.apply(ica_epochs) ica_epochs.info.update(description=f"n_components: {len(ica.exclude)}") ica_epochs.info.update(temp=f'{epochs.info["temp"]}_ICA') return ica_epochs
def test_epochs_proj_mixin(): """Test SSP proj methods from ProjMixin class """ for proj in [True, False]: epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=proj) assert_true(all(p["active"] == proj for p in epochs.info["projs"])) # test adding / deleting proj if proj: epochs.get_data() assert_true(all(p["active"] == proj for p in epochs.info["projs"])) assert_raises(ValueError, epochs.add_proj, epochs.info["projs"][0], {"remove_existing": True}) assert_raises(ValueError, epochs.add_proj, "spam") assert_raises(ValueError, epochs.del_proj, 0) else: projs = deepcopy(epochs.info["projs"]) n_proj = len(epochs.info["projs"]) epochs.del_proj(0) assert_true(len(epochs.info["projs"]) == n_proj - 1) epochs.add_proj(projs, remove_existing=False) assert_true(len(epochs.info["projs"]) == 2 * n_proj - 1) epochs.add_proj(projs, remove_existing=True) assert_true(len(epochs.info["projs"]) == n_proj) epochs = Epochs( raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=False, add_eeg_ref=True ) epochs2 = Epochs( raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=True, add_eeg_ref=True ) assert_allclose(epochs.copy().apply_proj().get_data()[0], epochs2.get_data()[0]) data = epochs.get_data().copy() data2 = np.array([e for e in epochs]) assert_array_equal(data, data2) epochs.apply_proj() assert_array_equal(epochs._projector, epochs2._projector) assert_allclose(epochs._data, epochs2.get_data()) epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=None, proj=False, add_eeg_ref=True) data = epochs.get_data().copy() epochs.apply_proj() assert_allclose(np.dot(epochs._projector, data[0]), epochs._data[0])
def prepare_epochs_for_ica(epochs: Epochs) -> Epochs: """ Drops epochs that were marked bad based on a global outlier detection. This implementation for the preliminary epoch rejection was based on the Python implementation of the FASTER algorithm from Marijn van Vliet https://gist.github.com/wmvanvliet/d883c3fe1402c7ced6fc Parameters ---------- epochs Returns ------- Epochs instance """ logger.info("Preliminary epoch rejection: ") def _deviation(data: np.ndarray) -> np.ndarray: """ Computes the deviation from mean for each channel. """ channels_mean = np.mean(data, axis=2) return channels_mean - np.mean(channels_mean, axis=0) metrics = { "amplitude": lambda x: np.mean(np.ptp(x, axis=2), axis=1), "deviation": lambda x: np.mean(_deviation(x), axis=1), "variance": lambda x: np.mean(np.var(x, axis=2), axis=1), } epochs_data = epochs.get_data() bad_epochs = [] for metric in metrics: scores = metrics[metric](epochs_data) outliers = bads._find_outliers(scores, threshold=3.0) logger.info(f"Bad epochs by {metric}\n\t{outliers}") bad_epochs.extend(outliers) bad_epochs = list(set(bad_epochs)) epochs_faster = epochs.copy().drop(bad_epochs, reason="FASTER") return epochs_faster
def test_apply_reference(): """Test base function for rereferencing""" raw = Raw(fif_fname, preload=True) # Rereference raw data by creating a copy of original data reref, ref_data = _apply_reference( raw.copy(), ref_from=['EEG 001', 'EEG 002']) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # The CAR reference projection should have been removed by the function assert_true(not _has_eeg_average_ref_proj(reref.info['projs'])) # Test that disabling the reference does not break anything reref, ref_data = _apply_reference(raw, []) assert_array_equal(raw._data, reref._data) # Test that data is modified in place when copy=False reref, ref_data = _apply_reference(raw, ['EEG 001', 'EEG 002']) assert_true(raw is reref) # Test re-referencing Epochs object raw = Raw(fif_fname, preload=False, add_eeg_ref=False) events = read_events(eve_fname) picks_eeg = pick_types(raw.info, meg=False, eeg=True) epochs = Epochs(raw, events=events, event_id=1, tmin=-0.2, tmax=0.5, picks=picks_eeg, preload=True) reref, ref_data = _apply_reference( epochs.copy(), ref_from=['EEG 001', 'EEG 002']) assert_true(reref.info['custom_ref_applied']) _test_reference(epochs, reref, ref_data, ['EEG 001', 'EEG 002']) # Test re-referencing Evoked object evoked = epochs.average() reref, ref_data = _apply_reference( evoked.copy(), ref_from=['EEG 001', 'EEG 002']) assert_true(reref.info['custom_ref_applied']) _test_reference(evoked, reref, ref_data, ['EEG 001', 'EEG 002']) # Test invalid input raw_np = Raw(fif_fname, preload=False) assert_raises(RuntimeError, _apply_reference, raw_np, ['EEG 001'])
def get_cross_val_score(subject, runs=[6, 10, 14], event_id=dict(rest=1, hands=2, feet=3), n_components=4, C=1.): tmin, tmax = -1., 4. raw = get_raw(subject, runs) events = find_events(raw, shortest_event=0, stim_channel='STI 014') picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) epochs_train = epochs.copy().crop(tmin=1., tmax=2.) labels = epochs.events[:, -1] scores = [] epochs_data_train = epochs_train.get_data() cv = KFold(n_splits=5) svc = svm.SVC(kernel='linear', C=C) csp = mne.decoding.CSP(n_components=n_components, reg=None, log=True, norm_trace=False) clf = sklearn.pipeline.Pipeline([('CSP', csp), ('SVM', svc)]) scores = cross_val_score(clf, epochs_data_train, labels, cv=cv, n_jobs=1) return np.mean(scores)
def extract_X_and_y(raw_nparray, raw_info, opts, verbose=False): # need to make a new RawArray, because once we apply filter, we mutate its internal _data raw = RawArray(raw_nparray, raw_info, verbose=verbose) picks = pick_types(raw.info, eeg=True) picks = getChannelSubsetMotorBand() #picks = getChannelSubsetFront() #picks = getChannelSubsetBack() #print picks # Apply band-pass filter raw._data[picks] = lfilter(opts.b, opts.a, raw._data[picks]) consecutive = True train_events = mne.find_events(raw, shortest_event=0, consecutive=consecutive, verbose=verbose) train_epochs = Epochs(raw, train_events, opts.event_labels, tmin=opts.epoch_full_tmin, tmax=opts.epoch_full_tmax, proj=True, picks=picks, baseline=None, preload=True, add_eeg_ref=False, verbose=verbose) epochs_trimmed = train_epochs.copy().crop(tmin=opts.epoch_trim_tmin, tmax=opts.epoch_trim_tmax) if verbose: print "train: epochs", epochs_trimmed X = epochs_trimmed.get_data() y = epochs_trimmed.events[:, -1] - 2 if verbose: print "y", y.shape return [X, y]
def extract_X_and_y(raw_nparray, raw_info, opts, verbose=False): # need to make a new RawArray, because once we apply filter, we mutate its internal _data raw = RawArray(raw_nparray, raw_info, verbose=verbose) picks = pick_types(raw.info, eeg=True) picks = getChannelSubsetMotorBand() # picks = getChannelSubsetFront() # picks = getChannelSubsetBack() # print picks # Apply band-pass filter raw._data[picks] = lfilter(opts.b, opts.a, raw._data[picks]) consecutive = True train_events = mne.find_events(raw, shortest_event=0, consecutive=consecutive, verbose=verbose) train_epochs = Epochs( raw, train_events, opts.event_labels, tmin=opts.epoch_full_tmin, tmax=opts.epoch_full_tmax, proj=True, picks=picks, baseline=None, preload=True, add_eeg_ref=False, verbose=verbose, ) epochs_trimmed = train_epochs.copy().crop(tmin=opts.epoch_trim_tmin, tmax=opts.epoch_trim_tmax) if verbose: print "train: epochs", epochs_trimmed X = epochs_trimmed.get_data() y = epochs_trimmed.events[:, -1] - 2 if verbose: print "y", y.shape return [X, y]
def run_ica(epochs: Epochs, fit_params: dict = None) -> ICA: """ Runs ICA decomposition on Epochs instance. If there are no EOG channels found, it tries to use 'Fp1' and 'Fp2' as EOG channels; if they are not found either, it chooses the first two channels to identify EOG components with mne.preprocessing.ica.find_bads_eog(). Parameters ---------- epochs: the instance to be used for ICA decomposition fit_params: parameters to be passed to ICA fit (e.g. orthogonal picard, extended infomax) Returns ------- ICA instance """ ica = ICA( n_components=settings["ica"]["n_components"], random_state=42, method=settings["ica"]["method"], fit_params=fit_params, ) ica_epochs = epochs.copy() ica.fit(ica_epochs, decim=settings["ica"]["decim"]) if "eog" not in epochs.get_channel_types(): if "Fp1" and "Fp2" in epochs.get_montage().ch_names: eog_channels = ["Fp1", "Fp2"] else: eog_channels = epochs.get_montage().ch_names[:2] logger.info("EOG channels are not found. Attempting to use " f'{",".join(eog_channels)} channels as EOG channels.') ica_epochs.set_channel_types({ch: "eog" for ch in eog_channels}) eog_indices, _ = ica.find_bads_eog(ica_epochs) ica.exclude = eog_indices return ica
def test_apply_reference(): """Test base function for rereferencing.""" raw = read_raw_fif(fif_fname, preload=True) # Rereference raw data by creating a copy of original data reref, ref_data = _apply_reference(raw.copy(), ref_from=['EEG 001', 'EEG 002']) assert (reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # The CAR reference projection should have been removed by the function assert (not _has_eeg_average_ref_proj(reref.info['projs'])) # Test that data is modified in place when copy=False reref, ref_data = _apply_reference(raw, ['EEG 001', 'EEG 002']) assert (raw is reref) # Test that disabling the reference does not change anything reref, ref_data = _apply_reference(raw.copy(), []) assert_array_equal(raw._data, reref._data) # Test re-referencing Epochs object raw = read_raw_fif(fif_fname, preload=False) events = read_events(eve_fname) picks_eeg = pick_types(raw.info, meg=False, eeg=True) epochs = Epochs(raw, events=events, event_id=1, tmin=-0.2, tmax=0.5, picks=picks_eeg, preload=True) reref, ref_data = _apply_reference(epochs.copy(), ref_from=['EEG 001', 'EEG 002']) assert (reref.info['custom_ref_applied']) _test_reference(epochs, reref, ref_data, ['EEG 001', 'EEG 002']) # Test re-referencing Evoked object evoked = epochs.average() reref, ref_data = _apply_reference(evoked.copy(), ref_from=['EEG 001', 'EEG 002']) assert (reref.info['custom_ref_applied']) _test_reference(evoked, reref, ref_data, ['EEG 001', 'EEG 002']) # Referencing needs data to be preloaded raw_np = read_raw_fif(fif_fname, preload=False) pytest.raises(RuntimeError, _apply_reference, raw_np, ['EEG 001']) # Test having inactive SSP projections that deal with channels involved # during re-referencing raw = read_raw_fif(fif_fname, preload=True) raw.add_proj( Projection( active=False, data=dict(col_names=['EEG 001', 'EEG 002'], row_names=None, data=np.array([[1, 1]]), ncol=2, nrow=1), desc='test', kind=1, )) # Projection concerns channels mentioned in projector with pytest.raises(RuntimeError, match='Inactive signal space'): _apply_reference(raw, ['EEG 001']) # Projection does not concern channels mentioned in projector, no error _apply_reference(raw, ['EEG 003'], ['EEG 004']) # CSD cannot be rereferenced raw.info['custom_ref_applied'] = FIFF.FIFFV_MNE_CUSTOM_REF_CSD with pytest.raises(RuntimeError, match="Cannot set.* type 'CSD'"): raw.set_eeg_reference()
def test_ica_core(): """Test ICA on raw and epochs""" 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') # XXX. The None cases helped revealing bugs but are time consuming. 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) noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [2, 1.0] # for future dbg add cases max_pca_components = [3] picks_ = [picks] methods = ['fastica'] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_, methods) # # test init catchers assert_raises(ValueError, ICA, n_components=3, max_pca_components=2) assert_raises(ValueError, ICA, n_components=2.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks, method in iter_ica_params: # Test ICA raw ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method, max_iter=1) assert_raises(ValueError, ica.__contains__, 'mag') print(ica) # to test repr # test fit checker assert_raises(RuntimeError, ica.get_sources, raw) assert_raises(RuntimeError, ica.get_sources, epochs) # test decomposition with warnings.catch_warnings(record=True): ica.fit(raw, picks=pcks, start=start, stop=stop) repr(ica) # to test repr assert_true('mag' in ica) # should now work without error # test re-fit unmixing1 = ica.unmixing_matrix_ with warnings.catch_warnings(record=True): ica.fit(raw, picks=pcks, start=start, stop=stop) assert_array_almost_equal(unmixing1, ica.unmixing_matrix_) sources = ica.get_sources(raw)[:, :][0] assert_true(sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3.preload = False assert_raises(ValueError, ica.apply, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) with warnings.catch_warnings(record=True): ica.fit(epochs, picks=picks) data = epochs.get_data()[:, 0, :] n_samples = np.prod(data.shape) assert_equal(ica.n_samples_, n_samples) print(ica) # to test repr sources = ica.get_sources(epochs).get_data() assert_true(sources.shape[1] == ica.n_components_) assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False assert_raises(ValueError, ica.apply, epochs3, include=[1, 2]) # test for bug with whitener updating _pre_whitener = ica._pre_whitener.copy() epochs._data[:, 0, 10:15] *= 1e12 ica.apply(epochs, copy=True) assert_array_equal(_pre_whitener, ica._pre_whitener) # test expl. var threshold leading to empty sel ica.n_components = 0.1 assert_raises(RuntimeError, ica.fit, epochs) offender = 1, 2, 3, assert_raises(ValueError, ica.get_sources, offender) assert_raises(ValueError, ica.fit, offender) assert_raises(ValueError, ica.apply, offender)
def test_ica_core(): """Test ICA on raw and epochs """ 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') # XXX. The None cases helped revealing bugs but are time consuming. 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) noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [2, 1.0] # for future dbg add cases max_pca_components = [3] picks_ = [picks] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_) # # test init catchers assert_raises(ValueError, ICA, n_components=3, max_pca_components=2) assert_raises(ValueError, ICA, n_components=2.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks in iter_ica_params: # Test ICA raw ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) print(ica) # to test repr # test fit checker assert_raises(RuntimeError, ica.get_sources_raw, raw) assert_raises(RuntimeError, ica.get_sources_epochs, epochs) # test decomposition ica.decompose_raw(raw, picks=pcks, start=start, stop=stop) print(ica) # to test repr # test re-init exception assert_raises(RuntimeError, ica.decompose_raw, raw, picks=picks) sources = ica.get_sources_raw(raw) assert_true(sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3._preloaded = False assert_raises(ValueError, ica.pick_sources_raw, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition # test re-init exception assert_raises(RuntimeError, ica.decompose_epochs, epochs, picks=picks) ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) ica.decompose_epochs(epochs, picks=picks) data = epochs.get_data()[:, 0, :] n_samples = np.prod(data.shape) assert_equal(ica.n_samples_, n_samples) print(ica) # to test repr # test pick block after epochs fit assert_raises(ValueError, ica.pick_sources_raw, raw) sources = ica.get_sources_epochs(epochs) assert_true(sources.shape[1] == ica.n_components_) assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False assert_raises(ValueError, ica.pick_sources_epochs, epochs3, include=[1, 2])
# Apply band-pass filter raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge') events, _ = events_from_annotations(raw, event_id=dict(T1=2, T2=3)) picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') # Read epochs (train will be done only between 1 and 2s) # Testing will be done with a running classifier epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) epochs_train = epochs.copy().crop(tmin=1., tmax=2.) labels = epochs.events[:, -1] - 2 ######## Authors: Martin Billinger <*****@*****.**> ######## # #visualize transformed data # # epoch_number = 1 # # for epoch in epochs.get_data()[:3]: # # #show one signal at a time # # for channel in epoch[:5]: # # plt.plot(channel) # # # # #plt.savefig(os.path.join(os.getcwd(),f'mne_data: epoch{epoch_number}')) # # plt.show() # # epoch_number += 1 # # # # #apply hilbert transform # # new_epochs = epochs.apply_hilbert()
def test_ica_core(method): """Test ICA on raw and epochs.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() # XXX. The None cases helped revealing bugs but are time consuming. 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) noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [2, 1.0] # for future dbg add cases max_pca_components = [3] picks_ = [picks] methods = [method] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_, methods) # # test init catchers pytest.raises(ValueError, ICA, n_components=3, max_pca_components=2) pytest.raises(ValueError, ICA, n_components=2.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks, method in iter_ica_params: # Test ICA raw ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method, max_iter=1) pytest.raises(ValueError, ica.__contains__, 'mag') print(ica) # to test repr # test fit checker pytest.raises(RuntimeError, ica.get_sources, raw) pytest.raises(RuntimeError, ica.get_sources, epochs) # test decomposition with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=pcks, start=start, stop=stop) repr(ica) # to test repr assert ('mag' in ica) # should now work without error # test re-fit unmixing1 = ica.unmixing_matrix_ with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=pcks, start=start, stop=stop) assert_array_almost_equal(unmixing1, ica.unmixing_matrix_) raw_sources = ica.get_sources(raw) # test for #3804 assert_equal(raw_sources._filenames, [None]) print(raw_sources) # test for gh-6271 (scaling of ICA traces) fig = raw_sources.plot() assert len(fig.axes[0].lines) in (4, 5) for line in fig.axes[0].lines[1:-1]: # first and last are markers y = line.get_ydata() assert np.ptp(y) < 10 plt.close('all') sources = raw_sources[:, :][0] assert (sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3.preload = False pytest.raises(RuntimeError, ica.apply, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method) with pytest.warns(None): # sometimes warns ica.fit(epochs, picks=picks) data = epochs.get_data()[:, 0, :] n_samples = np.prod(data.shape) assert_equal(ica.n_samples_, n_samples) print(ica) # to test repr sources = ica.get_sources(epochs).get_data() assert (sources.shape[1] == ica.n_components_) pytest.raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False pytest.raises(RuntimeError, ica.apply, epochs3, include=[1, 2]) # test for bug with whitener updating _pre_whitener = ica.pre_whitener_.copy() epochs._data[:, 0, 10:15] *= 1e12 ica.apply(epochs.copy()) assert_array_equal(_pre_whitener, ica.pre_whitener_) # test expl. var threshold leading to empty sel ica.n_components = 0.1 pytest.raises(RuntimeError, ica.fit, epochs) offender = 1, 2, 3, pytest.raises(ValueError, ica.get_sources, offender) pytest.raises(TypeError, ica.fit, offender) pytest.raises(TypeError, ica.apply, offender)
# Extract epochs from filtered data, padded by window size epochs = Epochs(raw_filter, events, event_id, tmin - w_size, tmax + w_size, proj=False, baseline=None, preload=True) epochs.drop_bad() y = le.fit_transform(epochs.events[:, 2]) # Roll covariance, csp and lda over time for t, w_time in enumerate(centered_w_times): # Center the min and max of the window w_tmin = w_time - w_size / 2. w_tmax = w_time + w_size / 2. # Crop data into time-window of interest X = epochs.copy().crop(w_tmin, w_tmax).get_data() # Save mean scores over folds for each frequency and time window tf_scores[freq, t] = np.mean(cross_val_score(estimator=clf, X=X, y=y, scoring='roc_auc', cv=cv, n_jobs=1), axis=0) ############################################################################### # Plot time-frequency results # Set up time frequency object av_tfr = AverageTFR(create_info(['freq'], sfreq), tf_scores[np.newaxis, :], centered_w_times, freqs[1:], 1) chance = np.mean(y) # set chance level to white in the plot av_tfr.plot([0], vmin=chance, title="Time-Frequency Decoding Scores",
def test_low_rank(): """Test low-rank covariance matrix estimation.""" raw = read_raw_fif(raw_fname).set_eeg_reference(projection=True).crop(0, 3) raw = maxwell_filter(raw, regularize=None) # heavily reduce the rank sss_proj_rank = 139 # 80 MEG + 60 EEG - 1 proj n_ch = 366 proj_rank = 365 # one EEG proj events = make_fixed_length_events(raw) methods = ('empirical', 'diagonal_fixed', 'oas') epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True) bounds = { 'None': dict(empirical=(-6000, -5000), diagonal_fixed=(-1500, -500), oas=(-700, -600)), 'full': dict(empirical=(-9000, -8000), diagonal_fixed=(-2000, -1600), oas=(-1600, -1000)), } for rank in ('full', None): covs = compute_covariance( epochs, method=methods, return_estimators=True, verbose='error', rank=rank) for cov in covs: method = cov['method'] these_bounds = bounds[str(rank)][method] this_rank = _cov_rank(cov, epochs.info) if rank is None or method == 'empirical': assert this_rank == sss_proj_rank else: assert this_rank == proj_rank assert these_bounds[0] < cov['loglik'] < these_bounds[1], \ (rank, method) if method == 'empirical': emp_cov = cov # save for later, rank param does not matter # Test equivalence with mne.cov.regularize subspace with pytest.raises(ValueError, match='are dependent.*must equal'): regularize(emp_cov, epochs.info, rank=None, mag=0.1, grad=0.2) assert _cov_rank(emp_cov, epochs.info) == sss_proj_rank reg_cov = regularize(emp_cov, epochs.info, proj=True, rank='full') assert _cov_rank(reg_cov, epochs.info) == proj_rank del reg_cov with catch_logging() as log: reg_r_cov = regularize(emp_cov, epochs.info, proj=True, rank=None, verbose=True) log = log.getvalue() assert 'jointly' in log assert _cov_rank(reg_r_cov, epochs.info) == sss_proj_rank reg_r_only_cov = regularize(emp_cov, epochs.info, proj=False, rank=None) assert _cov_rank(reg_r_only_cov, epochs.info) == sss_proj_rank assert_allclose(reg_r_only_cov['data'], reg_r_cov['data']) del reg_r_only_cov, reg_r_cov # test that rank=306 is same as rank='full' epochs_meg = epochs.copy().pick_types() assert len(epochs_meg.ch_names) == 306 epochs_meg.info.update(bads=[], projs=[]) cov_full = compute_covariance(epochs_meg, method='oas', rank='full', verbose='error') assert _cov_rank(cov_full, epochs_meg.info) == 306 cov_dict = compute_covariance(epochs_meg, method='oas', rank=306, verbose='error') assert _cov_rank(cov_dict, epochs_meg.info) == 306 assert_allclose(cov_full['data'], cov_dict['data']) # Work with just EEG data to simplify projection / rank reduction raw.pick_types(meg=False, eeg=True) n_proj = 2 raw.add_proj(compute_proj_raw(raw, n_eeg=n_proj)) n_ch = len(raw.ch_names) rank = n_ch - n_proj - 1 # plus avg proj assert len(raw.info['projs']) == 3 epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True) assert len(raw.ch_names) == n_ch emp_cov = compute_covariance(epochs, rank='full', verbose='error') assert _cov_rank(emp_cov, epochs.info) == rank reg_cov = regularize(emp_cov, epochs.info, proj=True, rank='full') assert _cov_rank(reg_cov, epochs.info) == rank reg_r_cov = regularize(emp_cov, epochs.info, proj=False, rank=None) assert _cov_rank(reg_r_cov, epochs.info) == rank dia_cov = compute_covariance(epochs, rank=None, method='diagonal_fixed', verbose='error') assert _cov_rank(dia_cov, epochs.info) == rank assert_allclose(dia_cov['data'], reg_cov['data']) # test our deprecation: can simply remove later epochs.pick_channels(epochs.ch_names[:103]) # degenerate with pytest.raises(ValueError, match='can.*only be used with rank="full"'): compute_covariance(epochs, rank=None, method='pca') with pytest.raises(ValueError, match='can.*only be used with rank="full"'): compute_covariance(epochs, rank=None, method='factor_analysis')
def test_generalization_across_time(): """Test time generalization decoding """ from sklearn.svm import SVC raw = io.Raw(raw_fname, preload=False) events = read_events(event_name) picks = pick_types(raw.info, meg='mag', stim=False, ecg=False, eog=False, exclude='bads') picks = picks[0:2] decim = 30 # Test on time generalization within one condition with warnings.catch_warnings(record=True): epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, decim=decim) # Test default running gat = GeneralizationAcrossTime() assert_equal("<GAT | no fit, no prediction, no score>", "%s" % gat) with warnings.catch_warnings(record=True): gat.fit(epochs) assert_equal("<GAT | fitted, start : -0.200 (s), stop : 0.499 (s), no " "prediction, no score>", '%s' % gat) gat.predict(epochs) assert_equal("<GAT | fitted, start : -0.200 (s), stop : 0.499 (s), " "predict_type : 'predict' on 15 epochs, no score>", "%s" % gat) gat.score(epochs) assert_equal("<GAT | fitted, start : -0.200 (s), stop : 0.499 (s), " "predict_type : 'predict' on 15 epochs,\n scored " "(accuracy_score)>", "%s" % gat) with warnings.catch_warnings(record=True): gat.fit(epochs, y=epochs.events[:, 2]) old_type = gat.predict_type gat.predict_type = 'foo' assert_raises(ValueError, gat.predict, epochs) gat.predict_type = old_type old_mode = gat.predict_mode gat.predict_mode = 'super-foo-mode' assert_raises(ValueError, gat.predict, epochs) gat.predict_mode = old_mode gat.score(epochs, y=epochs.events[:, 2]) assert_true("accuracy_score" in '%s' % gat.scorer_) epochs2 = epochs.copy() # check _DecodingTime class assert_equal("<DecodingTime | start: -0.200 (s), stop: 0.499 (s), step: " "0.047 (s), length: 0.047 (s), n_time_windows: 15>", "%s" % gat.train_times) assert_equal("<DecodingTime | start: -0.200 (s), stop: 0.499 (s), step: " "0.047 (s), length: 0.047 (s), n_time_windows: 15 x 15>", "%s" % gat.test_times_) # the y-check gat.predict_mode = 'mean-prediction' epochs2.events[:, 2] += 10 assert_raises(ValueError, gat.score, epochs2) gat.predict_mode = 'cross-validation' # Test basics # --- number of trials assert_true(gat.y_train_.shape[0] == gat.y_true_.shape[0] == gat.y_pred_.shape[2] == 14) # --- number of folds assert_true(np.shape(gat.estimators_)[1] == gat.cv) # --- length training size assert_true(len(gat.train_times['slices']) == 15 == np.shape(gat.estimators_)[0]) # --- length testing sizes assert_true(len(gat.test_times_['slices']) == 15 == np.shape(gat.scores_)[0]) assert_true(len(gat.test_times_['slices'][0]) == 15 == np.shape(gat.scores_)[1]) # Test longer time window gat = GeneralizationAcrossTime(train_times={'length': .100}) with warnings.catch_warnings(record=True): gat2 = gat.fit(epochs) assert_true(gat is gat2) # return self assert_true(hasattr(gat2, 'cv_')) assert_true(gat2.cv_ != gat.cv) scores = gat.score(epochs) assert_true(isinstance(scores, list)) # type check assert_equal(len(scores[0]), len(scores)) # shape check assert_equal(len(gat.test_times_['slices'][0][0]), 2) # Decim training steps gat = GeneralizationAcrossTime(train_times={'step': .100}) with warnings.catch_warnings(record=True): gat.fit(epochs) gat.score(epochs) assert_equal(len(gat.scores_), 8) # Test start stop training gat = GeneralizationAcrossTime(train_times={'start': 0.090, 'stop': 0.250}) # predict without fit assert_raises(RuntimeError, gat.predict, epochs) with warnings.catch_warnings(record=True): gat.fit(epochs) gat.score(epochs) assert_equal(len(gat.scores_), 4) assert_equal(gat.train_times['times_'][0], epochs.times[6]) assert_equal(gat.train_times['times_'][-1], epochs.times[9]) # Test score without passing epochs gat = GeneralizationAcrossTime() with warnings.catch_warnings(record=True): gat.fit(epochs) assert_raises(RuntimeError, gat.score) gat.predict(epochs, test_times='diagonal') # Test diagonal decoding scores = gat.score() assert_true(scores is gat.scores_) assert_equal(np.shape(gat.scores_), (15, 1)) # Test generalization across conditions gat = GeneralizationAcrossTime(predict_mode='mean-prediction') with warnings.catch_warnings(record=True): gat.fit(epochs[0:6]) gat.predict(epochs[7:]) assert_raises(ValueError, gat.predict, epochs, test_times='hahahaha') assert_raises(RuntimeError, gat.score) gat.score(epochs[7:]) svc = SVC(C=1, kernel='linear', probability=True) gat = GeneralizationAcrossTime(clf=svc, predict_type='predict_proba', predict_mode='mean-prediction') with warnings.catch_warnings(record=True): gat.fit(epochs) # sklearn needs it: c.f. # https://github.com/scikit-learn/scikit-learn/issues/2723 # and http://bit.ly/1u7t8UT assert_raises(ValueError, gat.score, epochs2) gat.score(epochs) scores = sum(scores, []) # flatten assert_true(0.0 <= np.min(scores) <= 1.0) assert_true(0.0 <= np.max(scores) <= 1.0) # test various predict_type gat = GeneralizationAcrossTime(clf=svc, predict_type="predict_proba") with warnings.catch_warnings(record=True): gat.fit(epochs) gat.predict(epochs) # check that 2 class probabilistic estimates are [p, 1-p] assert_true(gat.y_pred_.shape[3] == 2) gat.score(epochs) # check that continuous prediction leads to AUC rather than accuracy assert_true("roc_auc_score" in '%s' % gat.scorer_) gat = GeneralizationAcrossTime(predict_type="decision_function") # XXX Sklearn doesn't like non-binary inputs. We could binarize the data, # or change Sklearn default behavior epochs.events[:, 2][epochs.events[:, 2] == 3] = 0 with warnings.catch_warnings(record=True): gat.fit(epochs) gat.predict(epochs) # check that 2 class non-probabilistic continuous estimates are [distance] assert_true(gat.y_pred_.shape[3] == 1) gat.score(epochs) # check that continuous prediction leads to AUC rather than accuracy assert_true("roc_auc_score" in '%s' % gat.scorer_) # Test that gets error if train on one dataset, test on another, and don't # specify appropriate cv: gat = GeneralizationAcrossTime() with warnings.catch_warnings(record=True): gat.fit(epochs) gat.predict(epochs) assert_raises(ValueError, gat.predict, epochs[:10]) # Test combinations of complex scenarios # 2 or more distinct classes n_classes = [2] # 4 tested # nicely ordered labels or not y = epochs.events[:, 2] y[len(y) // 2:] += 2 ys = (y, y + 1000) # Classifier and regressor svc = SVC(C=1, kernel='linear', probability=True) clfs = [svc] # SVR tested # Continuous, and probabilistic estimate predict_types = ['predict_proba', 'decision_function'] # Test all combinations for clf_n, clf in enumerate(clfs): for y in ys: for n_class in n_classes: for pt in predict_types: y_ = y % n_class with warnings.catch_warnings(record=True): gat = GeneralizationAcrossTime( cv=2, clf=clf, predict_type=pt) gat.fit(epochs, y=y_) gat.score(epochs, y=y_)
def test_epochs_proj_mixin(): """Test SSP proj methods from ProjMixin class """ for proj in [True, False]: epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=proj) assert_true(all(p["active"] == proj for p in epochs.info["projs"])) # test adding / deleting proj if proj: epochs.get_data() assert_true(all(p["active"] == proj for p in epochs.info["projs"])) assert_raises(ValueError, epochs.add_proj, epochs.info["projs"][0], {"remove_existing": True}) assert_raises(ValueError, epochs.add_proj, "spam") assert_raises(ValueError, epochs.del_proj, 0) else: projs = deepcopy(epochs.info["projs"]) n_proj = len(epochs.info["projs"]) epochs.del_proj(0) assert_true(len(epochs.info["projs"]) == n_proj - 1) epochs.add_proj(projs, remove_existing=False) assert_true(len(epochs.info["projs"]) == 2 * n_proj - 1) epochs.add_proj(projs, remove_existing=True) assert_true(len(epochs.info["projs"]) == n_proj) # catch no-gos. # wrong proj argument assert_raises( ValueError, Epochs, raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj="crazy" ) # delayed without reject params assert_raises( RuntimeError, Epochs, raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj="delayed", reject=None, ) for preload in [True, False]: epochs = Epochs( raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj="delayed", preload=preload, add_eeg_ref=True, verbose=True, reject=reject, ) epochs2 = Epochs( raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), proj=True, preload=preload, add_eeg_ref=True, reject=reject, ) assert_allclose(epochs.copy().apply_proj().get_data()[0], epochs2.get_data()[0]) # make sure data output is constant across repeated calls # e.g. drop bads assert_array_equal(epochs.get_data(), epochs.get_data()) assert_array_equal(epochs2.get_data(), epochs2.get_data()) # test epochs.next calls data = epochs.get_data().copy() data2 = np.array([e for e in epochs]) assert_array_equal(data, data2) # cross application from processing stream 1 to 2 epochs.apply_proj() assert_array_equal(epochs._projector, epochs2._projector) assert_allclose(epochs._data, epochs2.get_data()) # test mixin against manual application epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=None, proj=False, add_eeg_ref=True) data = epochs.get_data().copy() epochs.apply_proj() assert_allclose(np.dot(epochs._projector, data[0]), epochs._data[0])
def test_apply_reference(): """Test base function for rereferencing.""" raw = read_raw_fif(fif_fname, preload=True) # Rereference raw data by creating a copy of original data reref, ref_data = _apply_reference( raw.copy(), ref_from=['EEG 001', 'EEG 002']) assert (reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # The CAR reference projection should have been removed by the function assert (not _has_eeg_average_ref_proj(reref.info['projs'])) # Test that data is modified in place when copy=False reref, ref_data = _apply_reference(raw, ['EEG 001', 'EEG 002']) assert (raw is reref) # Test that disabling the reference does not change anything reref, ref_data = _apply_reference(raw.copy(), []) assert_array_equal(raw._data, reref._data) # Test re-referencing Epochs object raw = read_raw_fif(fif_fname, preload=False) events = read_events(eve_fname) picks_eeg = pick_types(raw.info, meg=False, eeg=True) epochs = Epochs(raw, events=events, event_id=1, tmin=-0.2, tmax=0.5, picks=picks_eeg, preload=True) reref, ref_data = _apply_reference( epochs.copy(), ref_from=['EEG 001', 'EEG 002']) assert (reref.info['custom_ref_applied']) _test_reference(epochs, reref, ref_data, ['EEG 001', 'EEG 002']) # Test re-referencing Evoked object evoked = epochs.average() reref, ref_data = _apply_reference( evoked.copy(), ref_from=['EEG 001', 'EEG 002']) assert (reref.info['custom_ref_applied']) _test_reference(evoked, reref, ref_data, ['EEG 001', 'EEG 002']) # Referencing needs data to be preloaded raw_np = read_raw_fif(fif_fname, preload=False) pytest.raises(RuntimeError, _apply_reference, raw_np, ['EEG 001']) # Test having inactive SSP projections that deal with channels involved # during re-referencing raw = read_raw_fif(fif_fname, preload=True) raw.add_proj( Projection( active=False, data=dict( col_names=['EEG 001', 'EEG 002'], row_names=None, data=np.array([[1, 1]]), ncol=2, nrow=1 ), desc='test', kind=1, ) ) # Projection concerns channels mentioned in projector pytest.raises(RuntimeError, _apply_reference, raw, ['EEG 001']) # Projection does not concern channels mentioned in projector, no error _apply_reference(raw, ['EEG 003'], ['EEG 004'])
def test_generalization_across_time(): """Test time generalization decoding """ from sklearn.svm import SVC from sklearn.preprocessing import LabelEncoder from sklearn.metrics import mean_squared_error raw = io.Raw(raw_fname, preload=False) events = read_events(event_name) picks = pick_types(raw.info, meg='mag', stim=False, ecg=False, eog=False, exclude='bads') picks = picks[0:2] decim = 30 # Test on time generalization within one condition with warnings.catch_warnings(record=True): epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, decim=decim) # Test default running gat = GeneralizationAcrossTime() assert_equal("<GAT | no fit, no prediction, no score>", "%s" % gat) assert_raises(ValueError, gat.fit, epochs, picks='foo') with warnings.catch_warnings(record=True): # check classic fit + check manual picks gat.fit(epochs, picks=[0]) # check optional y as array gat.fit(epochs, y=epochs.events[:, 2]) # check optional y as list gat.fit(epochs, y=epochs.events[:, 2].tolist()) assert_equal(len(gat.picks_), len(gat.ch_names), 1) assert_equal("<GAT | fitted, start : -0.200 (s), stop : 0.499 (s), no " "prediction, no score>", '%s' % gat) assert_equal(gat.ch_names, epochs.ch_names) gat.predict(epochs) assert_equal("<GAT | fitted, start : -0.200 (s), stop : 0.499 (s), " "predicted 14 epochs, no score>", "%s" % gat) gat.score(epochs) gat.score(epochs, y=epochs.events[:, 2]) gat.score(epochs, y=epochs.events[:, 2].tolist()) assert_equal("<GAT | fitted, start : -0.200 (s), stop : 0.499 (s), " "predicted 14 epochs,\n scored " "(accuracy_score)>", "%s" % gat) with warnings.catch_warnings(record=True): gat.fit(epochs, y=epochs.events[:, 2]) old_mode = gat.predict_mode gat.predict_mode = 'super-foo-mode' assert_raises(ValueError, gat.predict, epochs) gat.predict_mode = old_mode gat.score(epochs, y=epochs.events[:, 2]) assert_true("accuracy_score" in '%s' % gat.scorer_) epochs2 = epochs.copy() # check _DecodingTime class assert_equal("<DecodingTime | start: -0.200 (s), stop: 0.499 (s), step: " "0.047 (s), length: 0.047 (s), n_time_windows: 15>", "%s" % gat.train_times) assert_equal("<DecodingTime | start: -0.200 (s), stop: 0.499 (s), step: " "0.047 (s), length: 0.047 (s), n_time_windows: 15 x 15>", "%s" % gat.test_times_) # the y-check gat.predict_mode = 'mean-prediction' epochs2.events[:, 2] += 10 gat_ = copy.deepcopy(gat) assert_raises(ValueError, gat_.score, epochs2) gat.predict_mode = 'cross-validation' # Test basics # --- number of trials assert_true(gat.y_train_.shape[0] == gat.y_true_.shape[0] == len(gat.y_pred_[0][0]) == 14) # --- number of folds assert_true(np.shape(gat.estimators_)[1] == gat.cv) # --- length training size assert_true(len(gat.train_times['slices']) == 15 == np.shape(gat.estimators_)[0]) # --- length testing sizes assert_true(len(gat.test_times_['slices']) == 15 == np.shape(gat.scores_)[0]) assert_true(len(gat.test_times_['slices'][0]) == 15 == np.shape(gat.scores_)[1]) # Test longer time window gat = GeneralizationAcrossTime(train_times={'length': .100}) with warnings.catch_warnings(record=True): gat2 = gat.fit(epochs) assert_true(gat is gat2) # return self assert_true(hasattr(gat2, 'cv_')) assert_true(gat2.cv_ != gat.cv) scores = gat.score(epochs) assert_true(isinstance(scores, list)) # type check assert_equal(len(scores[0]), len(scores)) # shape check assert_equal(len(gat.test_times_['slices'][0][0]), 2) # Decim training steps gat = GeneralizationAcrossTime(train_times={'step': .100}) with warnings.catch_warnings(record=True): gat.fit(epochs) gat.score(epochs) assert_true(len(gat.scores_) == len(gat.estimators_) == 8) # training time assert_equal(len(gat.scores_[0]), 15) # testing time # Test start stop training gat = GeneralizationAcrossTime(train_times={'start': 0.090, 'stop': 0.250}) # predict without fit assert_raises(RuntimeError, gat.predict, epochs) with warnings.catch_warnings(record=True): gat.fit(epochs) gat.score(epochs) assert_equal(len(gat.scores_), 4) assert_equal(gat.train_times['times_'][0], epochs.times[6]) assert_equal(gat.train_times['times_'][-1], epochs.times[9]) # Test score without passing epochs gat = GeneralizationAcrossTime() with warnings.catch_warnings(record=True): gat.fit(epochs) assert_raises(RuntimeError, gat.score) gat.predict(epochs, test_times='diagonal') # Test diagonal decoding scores = gat.score() assert_true(scores is gat.scores_) assert_equal(np.shape(gat.scores_), (15, 1)) # Test generalization across conditions gat = GeneralizationAcrossTime(predict_mode='mean-prediction') with warnings.catch_warnings(record=True): gat.fit(epochs[0:6]) gat.predict(epochs[7:]) assert_raises(ValueError, gat.predict, epochs, test_times='hahahaha') assert_raises(RuntimeError, gat.score) gat.score(epochs[7:]) svc = SVC(C=1, kernel='linear', probability=True) gat = GeneralizationAcrossTime(clf=svc, predict_mode='mean-prediction') with warnings.catch_warnings(record=True): gat.fit(epochs) # sklearn needs it: c.f. # https://github.com/scikit-learn/scikit-learn/issues/2723 # and http://bit.ly/1u7t8UT assert_raises(ValueError, gat.score, epochs2) gat.score(epochs) scores = sum(scores, []) # flatten assert_true(0.0 <= np.min(scores) <= 1.0) assert_true(0.0 <= np.max(scores) <= 1.0) # Test that gets error if train on one dataset, test on another, and don't # specify appropriate cv: gat = GeneralizationAcrossTime() with warnings.catch_warnings(record=True): gat.fit(epochs) gat.predict(epochs) assert_raises(ValueError, gat.predict, epochs[:10]) # Test combinations of complex scenarios # 2 or more distinct classes n_classes = [2, 4] # 4 tested # nicely ordered labels or not le = LabelEncoder() y = le.fit_transform(epochs.events[:, 2]) y[len(y) // 2:] += 2 ys = (y, y + 1000) # Univariate and multivariate prediction svc = SVC(C=1, kernel='linear') class SVC_proba(SVC): def predict(self, x): probas = super(SVC_proba, self).predict_proba(x) return probas[:, 0] svcp = SVC_proba(C=1, kernel='linear', probability=True) clfs = [svc, svcp] scorers = [None, mean_squared_error] # Test all combinations for clf, scorer in zip(clfs, scorers): for y in ys: for n_class in n_classes: y_ = y % n_class with warnings.catch_warnings(record=True): gat = GeneralizationAcrossTime(cv=2, clf=clf) gat.fit(epochs, y=y_) gat.score(epochs, y=y_, scorer=scorer)
# strip channel names of "." characters raw.rename_channels(lambda x: x.strip('.')) # Apply band-pass filter raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge') events, _ = events_from_annotations(raw, event_id=dict(T1=2, T2=3)) picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') # Read epochs (train will be done only between 1 and 2s) # Testing will be done with a running classifier epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=None, preload=True) epochs_train = epochs.copy().crop(tmin=1., tmax=2.) labels = epochs.events[:, -1] - 2 ############################################################################### # Classification with linear discrimant analysis # Define a monte-carlo cross-validation generator (reduce variance): scores = [] epochs_data = epochs.get_data() epochs_data_train = epochs_train.get_data() cv = ShuffleSplit(10, test_size=0.2, random_state=42) cv_split = cv.split(epochs_data_train) # Assemble a classifier lda = LinearDiscriminantAnalysis() csp = CSP(n_components=4, reg=None, log=True, norm_trace=False)