Shortcuts

FolderDataset

class torcheeg.datasets.FolderDataset(root_path: str = './folder', structure: str = 'subject_in_label', read_fn: None | ~typing.Callable = <function default_read_fn>, online_transform: None | ~typing.Callable = None, offline_transform: None | ~typing.Callable = None, label_transform: None | ~typing.Callable = None, io_path: None | str = None, io_size: int = 1048576, io_mode: str = 'lmdb', num_worker: int = 0, verbose: bool = True, **kwargs)[source][source]

Read EEG samples and their corresponding labels from a fixed folder structure. This class allows two kinds of common file structures, subject_in_label and label_in_subject. Here, subject_in_label corresponds to the following file structure:

tree
# outputs
label01
|- sub01.edf
|- sub02.edf
label02
|- sub01.edf
|- sub02.edf

And label_in_subject corresponds to the following file structure:

tree
# outputs
sub01
|- label01.edf
|- label02.edf
sub02
|- label01.edf
|- label02.edf

An example dataset for GNN-based methods:

from torcheeg.datasets import FolderDataset
from torcheeg import transforms

sfreq = 128  # Sampling rate
n_channels = 14  # Number of channels
duration = 5  # Data collected for 5 seconds
for i in range(num_files):
    n_samples = sfreq * duration
    data = np.random.randn(n_channels, n_samples)

    ch_names = [f'ch_{i+1:03}' for i in range(n_channels)]
    ch_types = ['eeg'] * n_channels
    info = mne.create_info(ch_names, sfreq, ch_types)
    raw = mne.io.RawArray(data, info)

    file_name = f'sub{i+1}.fif'
    file_path = os.path.join('./root_folder/', file_name)
    raw.save(file_path)

label_map = {'folder1': 0, 'folder2': 1}
dataset = FolderDataset(root_path='./root_folder',
                        structure='subject_in_label',
                        num_channel=14,
                        online_transform=transforms.ToTensor(),
                        label_transform=transforms.Compose([
                            transforms.Select('label'),
                            transforms.Lambda(lambda x: label_map[x])
                        ]),
                        num_worker=4)
Parameters:
  • root_path (str) – The path to the root folder. (default: './folder')

  • structure (str) – Folder structure, which affects how labels and subjects are mapped to EEG signal samples. Please refer to the above description of the structure of the two folders to select the correct parameters. (default: 'subject_in_label')

  • read_fn (Callable) – Method for reading files in a folder. By default, this class provides methods for reading files using mne.io.read_raw. At the same time, we allow users to pass in custom file reading methods. The first input parameter of whose is file_path, and other parameters are additional parameters passed in when the class is initialized. For example, you can pass chunk_size=32 to FolderDataset, then chunk_size will be received here.

  • online_transform (Callable, optional) – The transformation of the EEG signals and baseline EEG signals. The input is a np.ndarray, and the ouput is used as the first and second value of each element in the dataset. (default: None)

  • offline_transform (Callable, optional) – The usage is the same as online_transform, but executed before generating IO intermediate results. (default: None)

  • label_transform (Callable, optional) – The transformation of the label. The input is an information dictionary, and the ouput is used as the third value of each element in the dataset. (default: None)

  • io_path (str) – The path to generated unified data IO, cached as an intermediate result. If set to None, a random path will be generated. (default: None)

  • io_size (int) – Maximum size database may grow to; used to size the memory mapping. If database grows larger than map_size, an exception will be raised and the user must close and reopen. (default: 1048576)

  • io_mode (str) – Storage mode of EEG signal. When io_mode is set to lmdb, TorchEEG provides an efficient database (LMDB) for storing EEG signals. LMDB may not perform well on limited operating systems, where a file system based EEG signal storage is also provided. When io_mode is set to pickle, pickle-based persistence files are used. When io_mode is set to memory, memory are used. (default: lmdb)

  • num_worker (int) – Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process. (default: 0)

  • verbose (bool) – Whether to display logs during processing, such as progress bars, etc. (default: True)

Read the Docs v: latest
Versions
latest
stable
v1.1.1
v1.1.0
v1.0.11
v1.0.10
v1.0.9
v1.0.8.post1
v1.0.8
v1.0.7
v1.0.6
v1.0.4
v1.0.3
v1.0.2
v1.0.1
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources