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
andlabel_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 passchunk_size=32
toFolderDataset
, thenchunk_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 topickle
, pickle-based persistence files are used. When io_mode is set tomemory
, 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
)