You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"\n# Fixed-Length Windows Extraction\n :depth: 2\n"
8
+
]
9
+
},
10
+
{
11
+
"cell_type": "markdown",
12
+
"metadata": {},
13
+
"source": [
14
+
"## Introduction to Fixed-Length Windows Function\n\nIn many EEG decoding tasks, such as self-supervised pre-training,\nit is useful to split long continuous recordings\ninto **fixed-length, overlapping or non-overlapping windows**\n\nThe function :func:`braindecode.preprocessing.create_fixed_length_windows`\nprovides an easy way to slice a continuous EEG recording into such windows.\n\nThis tutorial explains how to use it, what its parameters mean, and how\nit can be applied to EEG datasets.\n\n\n"
"## Parameters\n\n**concat_ds** : `ConcatDataset`\n - A concat of base datasets each holding raw and description.\n\n**start_offset_samples** : int (default=0)\n - Start offset from beginning of recording in samples.\n\n**stop_offset_samples** : int or None (default=None)\n - Stop offset from beginning of recording in samples. If None, set to be the end of the recording.\n\n**window_size_samples** : int or None\n - Window size in samples. If None, set to be the maximum possible window size, ie length of the recording, once offsets are accounted for.\n\n**window_stride_samples** : int or None\n - Stride between windows in samples. If None, set to be equal to winddow_size_samples, so windows will not overlap.\n\n**drop_last_window** : bool or None\n - Whether or not have a last overlapping window, when windows do not equally divide the continuous signal. Must be set to a bool if window size and stride are not None.\n\n**mapping** : dict(str: int) or None\n - Mapping from event description to target value.\n\n**preload** : bool (default=False)\n - If True, preload the data of the Epochs objects.\n\n**picks** : str | list | slice | None\n - Channels to include. If None, all available channels are used. See mne.Epochs.\n\n**reject** : dict or None\n - Epoch rejection parameters based on peak-to-peak amplitude. If None, no rejection is done based on peak-to-peak amplitude. See mne.Epochs.\n\n**flat** : dict or None\n - Epoch rejection parameters based on flatness of signals. If None, no rejection based on flatness is done. See mne.Epochs.\n\n**targets_from** : str (default='metadata')\n - Choose where to get targets from: either 'metadata' or 'events'\n\n**last_target_only** : bool (default=True)\n - If `True`, only use the last target in the window.\n\n**lazy_metadata** : bool (default=False)\n - If True, metadata is not computed immediately, but only when accessed by using the _LazyDataFrame (experimental).\n\n**on_missing** : str (default='error')\n - What to do if one or several event ids are not found in the recording. Valid keys are \u2018error\u2019 | \u2018warning\u2019 | \u2018ignore\u2019. See mne.Epochs.\n\n**n_jobs** : int (default=1)\n - Number of jobs to use to parallelize the windowing.\n\n**verbose** : bool | str | int | None\n - Control verbosity of the logging output when calling mne.Epochs.\n\n"
29
+
]
30
+
},
31
+
{
32
+
"cell_type": "markdown",
33
+
"metadata": {},
34
+
"source": [
35
+
"## Example 1: Basic 2-Second, 50% Overlapping Windows\n\n\n"
"window_size_samples = int(sfreq * 2) # 2-second windows\nwindow_stride_samples = int(window_size_samples * 0.5) # 50% overlap\n\nwindows_dataset = create_fixed_length_windows(\n concat_ds=dataset,\n start_offset_samples=0,\n stop_offset_samples=None,\n window_size_samples=window_size_samples,\n window_stride_samples=window_stride_samples,\n drop_last_window=True,\n mapping=None,\n preload=True,\n picks=\"eeg\", # Only EEG channels\n reject=dict(eeg=150e-6), # Reject windows where EEG p2p > 150 \u00b5V\n flat=None,\n targets_from=\"metadata\",\n last_target_only=True,\n on_missing=\"warning\",\n n_jobs=1,\n verbose=\"error\",\n)\n\n# Let's inspect the output to better understand what we created.\n\n# Check how many windows were created\nprint(f\"Number of windows: {len(windows_dataset)}\")\n\n# Each window contains EEG data of fixed size\nX, y = windows_dataset[0]\nprint(f\"Window data shape: {X.shape}\")\nprint(f\"Window label: {y}\")"
94
+
]
95
+
},
96
+
{
97
+
"cell_type": "markdown",
98
+
"metadata": {},
99
+
"source": [
100
+
"## Working with Targets\nIn `create_fixed_length_windows`, targets can be derived in two ways:\n\n1. From the recording metadata (e.g., \"session\", \"condition\"). This is the default when `targets_from='metadata'`.\n2. From signal channels themselves when `targets_from='channels'`, useful for cases like sleep staging where\n annotations are stored in auxiliary channels.\n\n\nAdditionally:\n\n- **mapping**: Optionally map target values (e.g. from \"0train\" / \"1test\" to 0 / 1).\n- **last_target_only=True** (default): If multiple targets are present within a window (as in time-varying labels),\n use only the final target value in that window.\n\n"
101
+
]
102
+
},
103
+
{
104
+
"cell_type": "code",
105
+
"execution_count": null,
106
+
"metadata": {
107
+
"collapsed": false
108
+
},
109
+
"outputs": [],
110
+
"source": [
111
+
"# Example: mapping session names (\"0train\" and \"1test\") to integers\n\nmapping = {\"0train\": 0, \"1test\": 1}\n\nwindows_dataset = create_fixed_length_windows(\n dataset,\n window_size_samples=window_size_samples,\n window_stride_samples=window_stride_samples,\n drop_last_window=True,\n mapping=mapping,\n preload=True,\n)\n\n# View first few targets\nprint(\"Targets for first 10 windows:\")\nprint(windows_dataset.datasets[0].windows.get_metadata()[\"target\"][:10])"
112
+
]
113
+
},
114
+
{
115
+
"cell_type": "markdown",
116
+
"metadata": {},
117
+
"source": [
118
+
"## Example: Rejecting Windows Based on Amplitude\n\nYou can set rejection criteria to exclude windows with extreme values:\n\n"
"## Example: Shifted Windows\n\nYou can also create shifted windows by using ``start_offset_samples`` or ``stop_offset_samples``.\nFor example, start windowing 500 ms later into the recording.\n\n"
0 commit comments