Create dataset loader
Browse files
eeg_processed_train_dataset.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datasets
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
import glob
|
| 5 |
+
from tqdm import tqdm # Optional for progress
|
| 6 |
+
|
| 7 |
+
_DESCRIPTION = "EEG dataset with X and y numpy arrays."
|
| 8 |
+
_CITATION = "" # Add citation if available
|
| 9 |
+
_HOMEPAGE = "https://huggingface.co/datasets/prastab/eeg-processed-train-dataset" # Or your project page
|
| 10 |
+
_LICENSE = "" # Specify license, e.g., 'apache-2.0'
|
| 11 |
+
|
| 12 |
+
class EEGProcessedDataset(datasets.GeneratorBasedBuilder):
|
| 13 |
+
"""EEG Dataset Builder."""
|
| 14 |
+
|
| 15 |
+
VERSION = datasets.Version("1.0.0")
|
| 16 |
+
|
| 17 |
+
def _info(self):
|
| 18 |
+
# Define the features of your dataset
|
| 19 |
+
# IMPORTANT: Specify correct shapes and dtypes!
|
| 20 |
+
features = datasets.Features(
|
| 21 |
+
{
|
| 22 |
+
"X": datasets.Array3D(shape=(256, 62), dtype="float32"), # Adjust shape! (seq_len, channels)
|
| 23 |
+
"y": datasets.Array3D(shape=(64, 62), dtype="float32"), # Adjust shape! (out_seq_len, channels)
|
| 24 |
+
}
|
| 25 |
+
)
|
| 26 |
+
return datasets.DatasetInfo(
|
| 27 |
+
description=_DESCRIPTION,
|
| 28 |
+
features=features,
|
| 29 |
+
homepage=_HOMEPAGE,
|
| 30 |
+
license=_LICENSE,
|
| 31 |
+
citation=_CITATION,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
def _split_generators(self, dl_manager):
|
| 35 |
+
# Data is already in the repo, so we just need paths relative to the repo root
|
| 36 |
+
repo_path = os.path.dirname(__file__) # Gets the directory of this script within the repo
|
| 37 |
+
|
| 38 |
+
return [
|
| 39 |
+
datasets.SplitGenerator(
|
| 40 |
+
name=datasets.Split.TRAIN,
|
| 41 |
+
gen_kwargs={"split_dir": os.path.join(repo_path, "train")},
|
| 42 |
+
),
|
| 43 |
+
datasets.SplitGenerator(
|
| 44 |
+
name=datasets.Split.VALIDATION, # or 'val' if you prefer
|
| 45 |
+
gen_kwargs={"split_dir": os.path.join(repo_path, "val")},
|
| 46 |
+
),
|
| 47 |
+
datasets.SplitGenerator(
|
| 48 |
+
name=datasets.Split.TEST,
|
| 49 |
+
gen_kwargs={"split_dir": os.path.join(repo_path, "test")},
|
| 50 |
+
),
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
def _generate_examples(self, split_dir):
|
| 54 |
+
"""Yields examples."""
|
| 55 |
+
x_files = sorted(glob.glob(os.path.join(split_dir, "*_X_*.npy")))
|
| 56 |
+
y_files = sorted(glob.glob(os.path.join(split_dir, "*_y_*.npy")))
|
| 57 |
+
|
| 58 |
+
if len(x_files) != len(y_files):
|
| 59 |
+
raise ValueError(f"Mismatch in X/Y file count for {split_dir}")
|
| 60 |
+
if not x_files:
|
| 61 |
+
raise FileNotFoundError(f"No data files found in {split_dir}")
|
| 62 |
+
|
| 63 |
+
global_idx = 0
|
| 64 |
+
for xf, yf in tqdm(zip(x_files, y_files), total=len(x_files), desc=f"Processing {os.path.basename(split_dir)}"):
|
| 65 |
+
# Basic check for pairing
|
| 66 |
+
x_chunk_id = os.path.basename(xf).split('_X_')[-1]
|
| 67 |
+
y_chunk_id = os.path.basename(yf).split('_y_')[-1]
|
| 68 |
+
if x_chunk_id != y_chunk_id:
|
| 69 |
+
print(f"Warning: Potential file mismatch? {xf} vs {yf}") # Add better checking if needed
|
| 70 |
+
|
| 71 |
+
x_data = np.load(xf) # Loads (chunk_size, seq_len, channels)
|
| 72 |
+
y_data = np.load(yf) # Loads (chunk_size, out_seq_len, channels)
|
| 73 |
+
|
| 74 |
+
if x_data.shape[0] != y_data.shape[0]:
|
| 75 |
+
raise ValueError(f"Sample count mismatch in chunk {xf}/{yf}")
|
| 76 |
+
|
| 77 |
+
# Iterate through samples within the loaded chunk
|
| 78 |
+
for i in range(x_data.shape[0]):
|
| 79 |
+
yield global_idx, {
|
| 80 |
+
"X": x_data[i].astype(np.float32), # Ensure correct dtype
|
| 81 |
+
"y": y_data[i].astype(np.float32), # Ensure correct dtype
|
| 82 |
+
}
|
| 83 |
+
global_idx += 1
|