File size: 3,452 Bytes
ed155b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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