K_FOLD = 10
subjects = range(4, 6)
subjects_set = set(subjects)
accuracies = {
    "GNB": np.zeros((len(subjects), K_FOLD)),
    "SVM": np.zeros((len(subjects), K_FOLD)),
    "LDA": np.zeros((len(subjects), K_FOLD))
}

for subject in subjects:
    print("========= Subject: ", subject)
    # Load data
    print("Loading data ...")
    left_data_file = f"{DATA_FOLDER}/{subject}-left.csv"
    right_data_file = f"{DATA_FOLDER}/{subject}-right.csv"
    data = read_eeg_file(left_data_file, right_data_file, TIME_LENGTH,
                         TIME_WINDOW, EPOCH_SIZE)

    # Pre-processing
    print("Pre-processing ...")
    print("Applying 5º order Butterworth bandpass filter (7-30 Hz)")
    b, a = signal.butter(5, [7, 30], btype="bandpass", fs=FS)

    data.left_data = signal.filtfilt(b, a, data.left_data, axis=1)
    data.right_data = signal.filtfilt(b, a, data.right_data, axis=1)

    print("Spatial-filtering ...")
    data.X = np.concatenate((data.left_data, data.right_data))

    csp = CSP(average_trial_covariance=True, n_components=CSP_COMPONENTS)
    csp.fit(data.left_data, data.right_data)
    data.Z = np.array([csp.project(x) for x in data.X])
Exemple #2
0
print("D: ", D)

subjects = range(1, 6)
accuracies = {"SVM": np.zeros(len(subjects)), "LDA": np.zeros(len(subjects))}

# Create the Filter Bank
filter_bank = FilterBank(4, 40, 4)

for subject in subjects:
    print("========= Subject: ", subject)

    # Load training data
    print("Loading training data ...")
    left_data_file = f"{DATA_FOLDER}/left-hand-training-subject-{subject}.csv"
    right_data_file = f"{DATA_FOLDER}/right-hand-training-subject-{subject}.csv"
    training_data = read_eeg_file(left_data_file, right_data_file, TIME_LENGTH,
                                  TIME_WINDOW, EPOCH_SIZE)

    # Load test data
    print("Loading test data ...")
    left_data_file = f"{DATA_FOLDER}/left-hand-test-subject-{subject}.csv"
    right_data_file = f"{DATA_FOLDER}/right-hand-test-subject-{subject}.csv"
    test_data = read_eeg_file(left_data_file, right_data_file, TIME_LENGTH,
                              TIME_WINDOW, EPOCH_SIZE, False)

    # Apply the filter bank to the training data
    training_data.left_data = filter_bank.apply(training_data.left_data)
    training_data.right_data = filter_bank.apply(training_data.right_data)

    # Compute the spatial and spectral covariances
    spatio_spectral_covariances = SpatioSpectralCovariancesEstimation(
        training_data)