|
|
import json |
|
|
import numpy as np |
|
|
from datasets import Dataset, DatasetDict |
|
|
|
|
|
def load_bci_dataset(data_path="."): |
|
|
"""Load BCI Grid Movement Intent Dataset""" |
|
|
|
|
|
|
|
|
train_data = [] |
|
|
with open(f"{data_path}/data/train-00000-of-00001.jsonl", "r") as f: |
|
|
for line in f: |
|
|
train_data.append(json.loads(line.strip())) |
|
|
|
|
|
|
|
|
test_data = [] |
|
|
with open(f"{data_path}/data/test-00000-of-00001.jsonl", "r") as f: |
|
|
for line in f: |
|
|
test_data.append(json.loads(line.strip())) |
|
|
|
|
|
|
|
|
dataset = DatasetDict({ |
|
|
"train": Dataset.from_list(train_data), |
|
|
"test": Dataset.from_list(test_data) |
|
|
}) |
|
|
|
|
|
print(f"Dataset loaded: {len(dataset['train'])} train, {len(dataset['test'])} test samples") |
|
|
return dataset |
|
|
|
|
|
def prepare_for_ml(dataset, target_intent="W"): |
|
|
"""Prepare data for machine learning""" |
|
|
|
|
|
|
|
|
X = np.array([sample["neural_channels"] for sample in dataset]) |
|
|
|
|
|
|
|
|
if target_intent == "W": |
|
|
y = np.array([sample["movement_intent"][0] for sample in dataset]) |
|
|
elif target_intent == "A": |
|
|
y = np.array([sample["movement_intent"][1] for sample in dataset]) |
|
|
elif target_intent == "S": |
|
|
y = np.array([sample["movement_intent"][2] for sample in dataset]) |
|
|
elif target_intent == "D": |
|
|
y = np.array([sample["movement_intent"][3] for sample in dataset]) |
|
|
else: |
|
|
|
|
|
y = np.array([sample["movement_intent"] for sample in dataset]) |
|
|
|
|
|
return X, y |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
dataset = load_bci_dataset() |
|
|
|
|
|
|
|
|
X_train, y_train = prepare_for_ml(dataset["train"], "W") |
|
|
X_test, y_test = prepare_for_ml(dataset["test"], "W") |
|
|
|
|
|
print(f"Training set: {X_train.shape}, {y_train.shape}") |
|
|
print(f"Test set: {X_test.shape}, {y_test.shape}") |
|
|
|
|
|
|
|
|
print(f"Class distribution (W intent):") |
|
|
print(f" True: {np.sum(y_train == 1)} ({np.mean(y_train == 1)*100:.1f}%)") |
|
|
print(f" False: {np.sum(y_train == 0)} ({np.mean(y_train == 0)*100:.1f}%)") |