def load_train_test_hgd(subject_id): hgd_names = [ 'Fp2', 'Fp1', 'F4', 'F3', 'C4', 'C3', 'P4', 'P3', 'O2', 'O1', 'F8', 'F7', 'T8', 'T7', 'P8', 'P7', 'M2', 'M1', 'Fz', 'Cz', 'Pz' ] log.info("Loading dataset..") # using the moabb dataset to load our data dataset = MOABBDataset(dataset_name="Schirrmeister2017", subject_ids=[subject_id]) sfreq = 32 train_whole_set = dataset.split('run')['train'] log.info("Preprocessing dataset..") # Define preprocessing steps preprocessors = [ # convert from volt to microvolt, directly modifying the numpy array MNEPreproc( fn='set_eeg_reference', ref_channels='average', ), MNEPreproc(fn='pick_channels', ch_names=hgd_names, ordered=True), NumpyPreproc(fn=lambda x: x * 1e6), NumpyPreproc(fn=lambda x: np.clip(x, -800, 800)), NumpyPreproc(fn=lambda x: x / 10), MNEPreproc(fn='resample', sfreq=sfreq), NumpyPreproc(fn=lambda x: np.clip(x, -80, 80)), NumpyPreproc(fn=lambda x: x / 3), NumpyPreproc(fn=exponential_moving_demean, init_block_size=int(sfreq * 10), factor_new=1 / (sfreq * 5)), # keep only EEG sensors # NumpyPreproc(fn=exponential_moving_demean, init_block_size=sfreq*10, factor_new=1/(sfreq*5)), ] # Preprocess the data preprocess(train_whole_set, preprocessors) # Next, extract the 4-second trials from the dataset. # Create windows using braindecode function for this. It needs parameters to define how # trials should be used. class_names = ['Right Hand', 'Rest'] # for later plotting class_mapping = {'right_hand': 0, 'rest': 1} windows_dataset = create_windows_from_events( train_whole_set, trial_start_offset_samples=0, trial_stop_offset_samples=0, preload=True, mapping=class_mapping, ) from torch.utils.data import Subset n_split = int(np.round(0.75 * len(windows_dataset))) valid_set = Subset(windows_dataset, range(n_split, len(windows_dataset))) train_set = Subset(windows_dataset, range(0, n_split)) return train_set, valid_set
def test_predict_trials(): ds = MOABBDataset('BNCI2014001', subject_ids=1) ds1 = ds.split([0])['0'] # determine original trial size windows_ds1 = create_windows_from_events( ds1, ) trial_size = windows_ds1[0][0].shape[1] # create two windows per trial, where windows maximally overlap window_size_samples = trial_size - 1 window_stride_samples = 5 windows_ds1 = create_windows_from_events( ds1, window_size_samples=window_size_samples, window_stride_samples=window_stride_samples, drop_last_window=False, ) in_chans = windows_ds1[0][0].shape[0] n_classes = len(windows_ds1.get_metadata()['target'].unique()) model = ShallowFBCSPNet( in_chans=in_chans, n_classes=n_classes, ) to_dense_prediction_model(model) output_shape = get_output_shape(model, in_chans, window_size_samples) # the number of samples required to get 1 output receptive_field_size = window_size_samples - output_shape[-1] + 1 preds, targets = predict_trials(model, windows_ds1) # some model, cropped data assert preds.shape[-1] + receptive_field_size - 1 == trial_size assert preds.shape[1] == n_classes assert preds.shape[0] == targets.shape[0] metadata = windows_ds1.get_metadata() expected_targets = metadata[metadata['i_window_in_trial'] == 0][ 'target'].values np.testing.assert_array_equal(expected_targets, targets) # some model, trialwise data windows_ds2 = create_windows_from_events(ds1) with pytest.warns(UserWarning, match='This function was designed to predict' ' trials from cropped datasets.'): predict_trials(model, windows_ds2) # cropped EEGClassifier, cropped data clf = EEGClassifier( model, criterion=torch.nn.NLLLoss, optimizer=optim.AdamW, train_split=None, optimizer__lr=0.0625 * 0.01, optimizer__weight_decay=0, batch_size=64, ) clf.initialize() clf.predict_trials(windows_ds1, return_targets=True) # cropped EEGClassifier, trialwise data with pytest.warns(UserWarning, match="This method was designed to predict " "trials in cropped mode. Calling it " "when cropped is False will give the " "same result as '.predict'."): clf.predict_trials(windows_ds2)