return model.fit( train['x'], train['y'], batch_size=32, epochs=epochs, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=([checkpointer] if checkpointer is not None else [])) ### script start # _compX, _compY = separateXY(readall_comp_epochs('data/competition/epoched/ica')) _compX, _compY = epoch_comp(prep_comp(load_comp(True), comp_channel_map3, GOODS, l_freq=LO_FREQ, h_freq=HI_FREQ), CLASSES, resample=RESAMPLE, trange=T_RANGE) # _pilotX, _pilotY = epoch_pilot(loadall_pilot(True), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) _pilotX, _pilotY = epoch_pilot( load_pilot('data/rivet/raw/pilot2/BCI_imaginedmoves_3class_7-4-21.vhdr'), CLASSES, GOODS, resample=RESAMPLE, trange=T_RANGE, l_freq=LO_FREQ, h_freq=HI_FREQ) _compX, _compY = stratify(_compX, _compY, FOLDS)
CLASSES = 2 FOLDS = 9 GOODS = ['Fz','FC5','FC1','FC2','FC6','T7','C3','C1','Cz','C2','C4','T8','CP5','CP1','CPz','CP2','CP6','P3','Pz','P4','POz'] KERNELS = 1 WEIGHT_PATH = f"weights/competition/subject-separated/{CLASSES}class/{FOLDS}fold/channel_map2" ## local functions def train(model, train, validation, weight_file): checkpointer = ModelCheckpoint(filepath=weight_file, verbose=1, save_best_only=True) return model.fit(train['x'], train['y'], batch_size=16, epochs=50, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=[checkpointer]) ### script start _compX, _compY = epoch_comp(prep_comp(comp_channel_map2, GOODS), CLASSES) _pilotX, _pilotY = epoch_pilot(prep_pilot('data/rivet/VIPA_BCIpilot_imaginedmovement.vhdr', GOODS), CLASSES) chans, samples = _pilotX.shape[1], _pilotX.shape[2] Path(WEIGHT_PATH).mkdir(parents=True, exist_ok=True) pilotX = add_kernel_dim((_pilotX,), kernels=KERNELS) pilotY = onehot((_pilotY,)) comp_avg = {'acc': 0, 'bal': 0, 'kap': 0} pilot_avg = {'acc': 0, 'bal': 0, 'kap': 0} for i in range(0, FOLDS): trainX, valX, compX = add_kernel_dim(get_fold(_compX, FOLDS, i, test_val_rest_split), kernels=KERNELS) trainY, valY, compY = onehot(get_fold(_compY, FOLDS, i, test_val_rest_split))
from mne.decoding import CSP from sklearn.svm import SVC from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split, StratifiedKFold from sklearn.ensemble import RandomForestClassifier # constants CLASSES = 2 FOLDS = 10 GOODS = [ 'Fz', 'FC3', 'FC1', 'FC2', 'FC4', 'C5', 'C3', 'C1', 'Cz', 'C2', 'C4', 'C6', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'P1', 'Pz', 'P2', 'POz' ] # script start _compX, _compY = epoch_comp(prep_comp(comp_channel_map3, GOODS, h_freq=30.), CLASSES) _pilotX, _pilotY = epoch_pilot(prepall_pilot(GOODS, h_freq=30.), CLASSES) csp = CSP(n_components=4) csp.fit(_compX, _compY) csp_compX = csp.transform(_compX) csp_pilotX = csp.transform(_pilotX) print(csp_compX) print(csp_compX.shape) # clf = SVC(kernel='linear', C=0.05, probability=True) # clf = MLPClassifier([10, 3], batch_size=16) clf = RandomForestClassifier() skf = StratifiedKFold(FOLDS, shuffle=True, random_state=1)
def train(model, train, validation, weight_file=None, epochs=300): checkpointer = ModelCheckpoint( filepath=weight_file, verbose=1, save_best_only=True) if weight_file is not None else None return model.fit( train['x'], train['y'], batch_size=32, epochs=epochs, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=([checkpointer] if checkpointer is not None else [])) ### script start subject_epochs, subject_labels = epoch_comp(load_comp_array(True), CLASSES, comp_channel_map3, GOODS, RESAMPLE, T_RANGE, LO_FREQ, HI_FREQ) print(subject_epochs) print(subject_labels) for i in range(0, len(subject_epochs)): rolled_epochs = np.roll(subject_epochs, i, 0) rolled_labels = np.roll(subject_labels, i, 0) test_epochs, test_labels = rolled_epochs[0], rolled_labels[0] trai_epochs, trai_labels = rolled_epochs[1:], rolled_labels[1:] print(test_labels) print(len(trai_labels))
def train(model, train, validation, weight_file): checkpointer = ModelCheckpoint(filepath=weight_file, verbose=1, save_best_only=True) return model.fit(train['x'], train['y'], batch_size=16, epochs=50, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=[checkpointer]) ### script start compX, compY = epoch_comp(prep_comp({}), CLASSES) chans, samples = compX.shape[1], compX.shape[2] Path(WEIGHT_PATH).mkdir(parents=True, exist_ok=True) comp_avg = {'acc': 0, 'bal': 0, 'kap': 0} for i in range(0, FOLDS): trainX, valX, testX = add_kernel_dim(get_fold(compX, FOLDS, i, test_val_rest_split), kernels=KERNELS) trainY, valY, testY = onehot(get_fold(compY, FOLDS, i, test_val_rest_split)) # weight file path weight_file = f"{WEIGHT_PATH}/{i+1}.h5"
save_best_only=True) if weight_file is not None else None return model.fit( train['x'], train['y'], batch_size=32, epochs=epochs, verbose=0, validation_data=(validation['x'], validation['y']), callbacks=([checkpointer] if checkpointer is not None else [])) ### script start _compX, _compY = epoch_comp(prep_comp(comp_channel_map3, GOODS, h_freq=LOW_PASS), CLASSES, resample=RESAMPLE, trange=T_RANGE) _pilotX, _pilotY = epoch_pilot(prepall_pilot(GOODS, h_freq=LOW_PASS), CLASSES, resample=RESAMPLE, trange=T_RANGE) # _pilotX, _pilotY = epoch_pilot(prep_pilot('data/rivet/VIPA_BCIpilot_realmovement.vhdr', GOODS, l_freq=0.5, h_freq=30.), CLASSES) chans, samples = _pilotX.shape[1], _pilotX.shape[2] Path(WEIGHT_PATH).mkdir(parents=True, exist_ok=True) # stratify and split pilot data for transfer learning skf = StratifiedKFold(TRANSFER_FOLDS, shuffle=True) comp_avg = {'acc': 0, 'bal': 0, 'kap': 0}