eeg-processed-train-dataset / eeg_processed_train_dataset.py
prastab's picture rupak78's picture
Create dataset loader (#1)
ed155b7 verified
Raw History Blame Contribute Delete
3.45 kB
import datasets
import numpy as np
import os
import glob
from tqdm import tqdm # Optional for progress
_DESCRIPTION = "EEG dataset with X and y numpy arrays."
_CITATION = "" # Add citation if available
_HOMEPAGE = "https://huggingface.co/datasets/prastab/eeg-processed-train-dataset" # Or your project page
_LICENSE = "" # Specify license, e.g., 'apache-2.0'
class EEGProcessedDataset(datasets.GeneratorBasedBuilder):
"""EEG Dataset Builder."""
VERSION = datasets.Version("1.0.0")
def _info(self):
# Define the features of your dataset
# IMPORTANT: Specify correct shapes and dtypes!
features = datasets.Features(
{
"X": datasets.Array3D(shape=(256, 62), dtype="float32"), # Adjust shape! (seq_len, channels)
"y": datasets.Array3D(shape=(64, 62), dtype="float32"), # Adjust shape! (out_seq_len, channels)
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
# Data is already in the repo, so we just need paths relative to the repo root
repo_path = os.path.dirname(__file__) # Gets the directory of this script within the repo
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"split_dir": os.path.join(repo_path, "train")},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION, # or 'val' if you prefer
gen_kwargs={"split_dir": os.path.join(repo_path, "val")},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"split_dir": os.path.join(repo_path, "test")},
),
]
def _generate_examples(self, split_dir):
"""Yields examples."""
x_files = sorted(glob.glob(os.path.join(split_dir, "*_X_*.npy")))
y_files = sorted(glob.glob(os.path.join(split_dir, "*_y_*.npy")))
if len(x_files) != len(y_files):
raise ValueError(f"Mismatch in X/Y file count for {split_dir}")
if not x_files:
raise FileNotFoundError(f"No data files found in {split_dir}")
global_idx = 0
for xf, yf in tqdm(zip(x_files, y_files), total=len(x_files), desc=f"Processing {os.path.basename(split_dir)}"):
# Basic check for pairing
x_chunk_id = os.path.basename(xf).split('_X_')[-1]
y_chunk_id = os.path.basename(yf).split('_y_')[-1]
if x_chunk_id != y_chunk_id:
print(f"Warning: Potential file mismatch? {xf} vs {yf}") # Add better checking if needed
x_data = np.load(xf) # Loads (chunk_size, seq_len, channels)
y_data = np.load(yf) # Loads (chunk_size, out_seq_len, channels)
if x_data.shape[0] != y_data.shape[0]:
raise ValueError(f"Sample count mismatch in chunk {xf}/{yf}")
# Iterate through samples within the loaded chunk
for i in range(x_data.shape[0]):
yield global_idx, {
"X": x_data[i].astype(np.float32), # Ensure correct dtype
"y": y_data[i].astype(np.float32), # Ensure correct dtype
}
global_idx += 1