pybemobil 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pybemobil/__init__.py ADDED
@@ -0,0 +1,13 @@
1
+ """PyBeMoBIL: MoBI data loading, preprocessing, and visualization."""
2
+
3
+ from importlib.metadata import PackageNotFoundError, version
4
+
5
+ from pybemobil import io, preproc, viz
6
+ from pybemobil.io import XDFLoader
7
+
8
+ try:
9
+ __version__ = version("pybemobil")
10
+ except PackageNotFoundError:
11
+ __version__ = "unknown"
12
+
13
+ __all__ = ["XDFLoader", "__version__", "io", "preproc", "viz"]
@@ -0,0 +1,19 @@
1
+ # Note regarding montages
2
+
3
+ Matlab and Python assume different coordinate systems for EEG electrode positions. The original `standard_MoBI_128.elc` file is in the Matlab coordinate system, which can lead to incorrect electrode placements when used in Python.
4
+
5
+ When using the montage in Python, please ensure that you use the corrected version (`standard_MoBI_128_corrected.fif`) to avoid any discrepancies in electrode placements.
6
+
7
+ The files are installed with the package. To use the corrected montage:
8
+
9
+ ```python
10
+ from importlib.resources import files
11
+
12
+ from pybemobil.io import XDFLoader
13
+
14
+ montage_dir = files("pybemobil") / "data" / "montages"
15
+ loader = XDFLoader(montage=montage_dir / "standard_MoBI_128_corrected.fif")
16
+ # or: mne.channels.read_dig_fif(montage_dir / "standard_MoBI_128_corrected.fif")
17
+ ```
18
+
19
+ Whenever you work with a montage the first time, load and plot it (montage.plot()) to verify that the electrode positions are correct. If you notice any discrepancies, please correct the coordinates and save the corrected montage.
@@ -0,0 +1,260 @@
1
+ NumberPositions= 128
2
+ UnitPosition mm
3
+ Positions
4
+ 14.779000 76.886600 -23.569850
5
+ 15.277150 81.699700 0.879150
6
+ 12.524950 84.929450 29.713500
7
+ 18.148150 84.977400 53.849850
8
+ 42.306250 72.138050 -28.667500
9
+ 37.205500 79.440200 -2.381450
10
+ 37.121300 80.574750 25.564050
11
+ 48.088600 78.895000 49.972100
12
+ 61.912850 69.316150 -5.231700
13
+ 67.487300 60.830150 24.293200
14
+ 77.244650 66.695600 48.346200
15
+ 90.604450 44.892550 28.872600
16
+ 101.052750 44.997050 47.354350
17
+ 102.444900 20.053400 21.484550
18
+ 113.723000 14.504500 45.660450
19
+ 73.430300 33.286900 -29.951700
20
+ 114.540500 -17.927400 42.820200
21
+ 105.491300 -16.590750 20.345650
22
+ 102.327800 -46.721450 44.365850
23
+ 91.647100 -43.819950 21.804950
24
+ 79.630850 -66.798950 42.096400
25
+ 67.529650 -60.467200 19.394250
26
+ 60.097850 -68.114250 -10.217750
27
+ 49.983850 -80.290450 42.999950
28
+ 38.080750 -80.759500 17.099550
29
+ 38.967150 -76.432700 -10.147750
30
+ 42.997050 -67.987500 -37.237050
31
+ 21.027150 -87.894650 46.390450
32
+ 12.883200 -85.777950 20.691400
33
+ 15.260550 -81.107350 -7.944300
34
+ 11.873750 -75.286750 -31.405000
35
+ 73.534300 -28.039550 -36.420700
36
+ 10.374350 82.773450 83.034250
37
+ 30.801900 69.893850 104.970350
38
+ 18.418950 51.854050 130.378000
39
+ 5.269100 21.193800 148.917500
40
+ 37.787000 79.060050 76.769100
41
+ 57.581600 59.198400 100.879400
42
+ 44.257450 45.163200 124.286000
43
+ 34.316050 22.462700 140.858500
44
+ 64.397900 68.589550 73.825000
45
+ 87.905750 51.453200 74.284050
46
+ 77.400650 44.168200 100.069000
47
+ 64.712700 24.830500 123.495500
48
+ 104.412050 28.074950 74.123150
49
+ 93.413300 14.892350 101.725750
50
+ 111.181150 -2.235800 72.552300
51
+ 74.567400 -2.150150 122.016500
52
+ 94.467200 -19.955900 100.317200
53
+ 105.008950 -33.119150 70.767700
54
+ 49.444400 -2.253450 137.294500
55
+ 64.977100 -30.820400 121.715500
56
+ 79.683750 -48.638600 96.436600
57
+ 87.543650 -56.999400 69.550600
58
+ 64.541050 -73.529750 68.173250
59
+ 35.729000 -29.217600 138.311500
60
+ 45.186350 -50.964850 120.848500
61
+ 56.336150 -65.859750 97.165450
62
+ 38.434450 -83.590550 70.081750
63
+ -8.641150 -2.752900 151.911000
64
+ 5.876850 -30.723450 146.801500
65
+ 18.627450 -59.226500 126.674500
66
+ 29.803000 -76.967300 99.256350
67
+ 11.791050 -88.154900 74.135150
68
+ -33.759700 82.546550 40.356450
69
+ -12.535100 85.160100 60.706950
70
+ -37.659700 78.413450 69.343200
71
+ -62.152650 64.706750 77.831800
72
+ -81.115200 45.321750 82.965200
73
+ -17.611900 79.128350 90.332450
74
+ -44.247800 67.749500 99.726250
75
+ -64.184300 49.120850 106.443800
76
+ -78.756450 20.976850 110.024650
77
+ 1.730250 71.142050 113.577300
78
+ -26.059050 60.656950 122.554350
79
+ -47.881250 42.230950 128.184500
80
+ -60.881800 9.200200 130.593500
81
+ -7.264950 45.899750 138.927000
82
+ -28.640000 22.663050 146.044000
83
+ -36.902800 -4.765050 146.009500
84
+ -28.134550 -33.596400 143.063000
85
+ -8.439050 -52.825100 136.036000
86
+ -60.498950 -19.936300 128.796500
87
+ -45.313200 -50.792100 124.198000
88
+ -25.814955 -68.160000 115.907000
89
+ 0.381600 -77.667350 107.154100
90
+ -81.430400 -5.458100 110.358600
91
+ -76.245050 -34.010650 106.856200
92
+ -61.139100 -57.654200 100.610500
93
+ -39.908000 -74.509450 92.497450
94
+ -15.168300 -84.599050 81.915250
95
+ -76.488700 -54.046750 77.347250
96
+ -57.942200 -70.464050 70.390600
97
+ -37.261600 -80.805700 62.850800
98
+ -10.115000 -88.047900 52.998450
99
+ -25.588950 -84.617650 31.048100
100
+ -44.913950 72.587750 -8.163050
101
+ -66.969950 62.550400 2.179650
102
+ -86.123100 44.683050 10.925000
103
+ -95.353450 24.710550 12.196000
104
+ -44.324700 78.843850 16.968200
105
+ -60.136200 70.283400 23.352900
106
+ -77.988600 56.139050 30.309550
107
+ -91.328400 40.014150 34.654300
108
+ -99.829800 16.925500 34.864500
109
+ -52.556650 74.742400 48.722700
110
+ -73.266450 60.704050 54.589900
111
+ -88.376000 43.752700 59.074600
112
+ -99.502700 18.680200 59.918900
113
+ -92.992750 19.422350 86.752500
114
+ -94.725300 -6.626200 86.138750
115
+ -89.712050 -32.580850 83.220400
116
+ -101.211450 -3.906100 59.290650
117
+ -96.151300 -30.287400 56.445900
118
+ -82.238700 -52.995900 52.218100
119
+ -65.683800 -67.586650 45.831900
120
+ -44.443750 -79.065000 37.945200
121
+ -100.755000 -2.612525 33.880300
122
+ -97.568950 -24.380300 33.049550
123
+ -86.386550 -46.605850 27.930550
124
+ -71.220150 -61.323750 22.096650
125
+ -54.178100 -72.670000 13.526400
126
+ -38.917200 -79.405250 5.649500
127
+ -100.616700 -3.210150 11.869550
128
+ -94.074550 -30.131800 9.442100
129
+ -77.328700 -52.072750 2.409550
130
+ -59.353000 -63.761750 -8.503750
131
+ -38.035850 -71.251600 -18.824700
132
+ Labels
133
+ g1
134
+ g2
135
+ g3
136
+ g4
137
+ g5
138
+ g6
139
+ g7
140
+ g8
141
+ g9
142
+ g10
143
+ g11
144
+ g12
145
+ g13
146
+ g14
147
+ g15
148
+ g16
149
+ g17
150
+ g18
151
+ g19
152
+ g20
153
+ g21
154
+ g22
155
+ g23
156
+ g24
157
+ g25
158
+ g26
159
+ g27
160
+ g28
161
+ g29
162
+ g30
163
+ g31
164
+ g32
165
+ y1
166
+ y2
167
+ y3
168
+ y4
169
+ y5
170
+ y6
171
+ y7
172
+ y8
173
+ y9
174
+ y10
175
+ y11
176
+ y12
177
+ y13
178
+ y14
179
+ y15
180
+ y16
181
+ y17
182
+ y18
183
+ y19
184
+ y20
185
+ y21
186
+ y22
187
+ y23
188
+ y24
189
+ y25
190
+ y26
191
+ y27
192
+ y28
193
+ y29
194
+ y30
195
+ y31
196
+ y32
197
+ r1
198
+ r2
199
+ r3
200
+ r4
201
+ r5
202
+ r6
203
+ r7
204
+ r8
205
+ r9
206
+ r10
207
+ r11
208
+ r12
209
+ r13
210
+ r14
211
+ r15
212
+ r16
213
+ r17
214
+ r18
215
+ r19
216
+ r20
217
+ r21
218
+ r22
219
+ r23
220
+ r24
221
+ r25
222
+ r26
223
+ r27
224
+ r28
225
+ r29
226
+ r30
227
+ r31
228
+ r32
229
+ w1
230
+ w2
231
+ w3
232
+ w4
233
+ w5
234
+ w6
235
+ w7
236
+ w8
237
+ w9
238
+ w10
239
+ w11
240
+ w12
241
+ w13
242
+ w14
243
+ w15
244
+ w16
245
+ w17
246
+ w18
247
+ w19
248
+ w20
249
+ w21
250
+ w22
251
+ w23
252
+ w24
253
+ w25
254
+ w26
255
+ w27
256
+ w28
257
+ w29
258
+ w30
259
+ w31
260
+ w32
@@ -0,0 +1,107 @@
1
+ """BIDS export using mne_bids wrappers in pybemobil.io.
2
+
3
+ Demonstrates:
4
+ - export_to_bids: export a single Raw object to a BIDS directory tree.
5
+ - make_bids_dataset_description: write dataset_description.json.
6
+ - batch_export_to_bids: export multiple subjects/runs in one call.
7
+
8
+ Source data: MNE eegbci motor-imagery dataset (downloaded automatically).
9
+ The BIDS output is written to a temporary directory that is cleaned up at
10
+ the end of the script.
11
+ """
12
+
13
+ # %% Imports
14
+
15
+ import tempfile
16
+ from pathlib import Path
17
+
18
+ import mne
19
+ from mne.datasets import eegbci
20
+
21
+ from pybemobil.io import (
22
+ batch_export_to_bids,
23
+ export_to_bids,
24
+ make_bids_dataset_description,
25
+ )
26
+
27
+ # %% Settings & Constants
28
+
29
+ # eegbci run 6 = motor imagery (left/right hand), subject 1
30
+ SUBJECT_ID = 1
31
+ RUNS = [6, 10] # two runs from the same subject
32
+ LINE_FREQ = 60.0 # PhysioNet data recorded in the USA
33
+
34
+ # %% Load source data (MNE eegbci)
35
+
36
+ raws = []
37
+ for run in RUNS:
38
+ fnames = eegbci.load_data(subject=SUBJECT_ID, runs=[run], update_path=True)
39
+ raw = mne.io.read_raw_edf(fnames[0], preload=True, verbose=False)
40
+ eegbci.standardize(raw) # rename channels to 10-20 labels
41
+ # "colin27_1005" was called "standard_1005" before MNE 1.13
42
+ montage = mne.channels.make_standard_montage("colin27_1005")
43
+ raw.set_montage(montage, verbose=False)
44
+ raws.append(raw)
45
+
46
+ print(f"Loaded {len(raws)} runs, {raws[0].info['nchan']} channels each")
47
+
48
+ # %% Single-run export
49
+
50
+ with tempfile.TemporaryDirectory() as tmpdir:
51
+ bids_root = Path(tmpdir) / "bids_single"
52
+
53
+ # Write dataset_description.json first
54
+ make_bids_dataset_description(
55
+ bids_root=bids_root,
56
+ name="eegbci-motor-imagery-demo",
57
+ authors=["BPN Lab"],
58
+ data_license="CC0",
59
+ )
60
+
61
+ bids_path = export_to_bids(
62
+ raw=raws[0],
63
+ bids_root=bids_root,
64
+ subject="01",
65
+ session="01",
66
+ task="motorImagery",
67
+ run="06",
68
+ line_freq=LINE_FREQ,
69
+ overwrite=True,
70
+ verbose=False,
71
+ )
72
+
73
+ print("Single export written to:", bids_path.fpath)
74
+ print("BIDS tree:")
75
+ for p in sorted(bids_root.rglob("*")):
76
+ if p.is_file():
77
+ print(" ", p.relative_to(bids_root))
78
+
79
+ # %% Batch export
80
+
81
+ with tempfile.TemporaryDirectory() as tmpdir:
82
+ bids_root = Path(tmpdir) / "bids_batch"
83
+
84
+ make_bids_dataset_description(
85
+ bids_root=bids_root,
86
+ name="eegbci-motor-imagery-demo",
87
+ authors=["BPN Lab"],
88
+ )
89
+
90
+ # subject_runs: list of dicts, one entry per run
91
+ subject_runs = [
92
+ {"raw": raws[0], "subject": "01", "session": "01", "run": "06"},
93
+ {"raw": raws[1], "subject": "01", "session": "01", "run": "10"},
94
+ ]
95
+
96
+ bids_paths = batch_export_to_bids(
97
+ subject_runs=subject_runs,
98
+ bids_root=bids_root,
99
+ task="motorImagery",
100
+ line_freq=LINE_FREQ,
101
+ overwrite=True,
102
+ verbose=False,
103
+ )
104
+
105
+ print(f"\nBatch export: {len(bids_paths)} runs written")
106
+ for bp in bids_paths:
107
+ print(" ", bp.fpath)
@@ -0,0 +1,101 @@
1
+ """Rigid body kinematics processing with motion.py utilities.
2
+
3
+ Demonstrates:
4
+ - Building a synthetic MNE Raw object with quaternion and position channels
5
+ following the pybemobil naming convention.
6
+ - find_rigid_bodies(raw) - detect rigid body names from channel labels.
7
+ - process_rigid_body(raw, ...) - convert quaternions to Euler angles, compute
8
+ velocity and acceleration.
9
+ - split_by_rigid_body(raw) - split into per-body Raw sub-objects.
10
+
11
+ The synthetic data is runnable without any real recording.
12
+ Channels follow the convention: <body>_quat_[xyzw], <body>_pos_[xyz].
13
+ """
14
+
15
+ # %% Imports
16
+
17
+ import mne
18
+ import numpy as np
19
+
20
+ from pybemobil.preproc import (
21
+ find_rigid_bodies,
22
+ process_rigid_body,
23
+ split_by_rigid_body,
24
+ )
25
+
26
+ # %% Settings & Constants
27
+
28
+ SFREQ = 120.0 # typical motion capture rate (Hz)
29
+ DURATION = 10.0 # seconds
30
+
31
+ # %% Build synthetic Raw with motion capture channels
32
+
33
+ rng = np.random.default_rng(42)
34
+ n_times = int(SFREQ * DURATION)
35
+
36
+ # Two rigid bodies: "head" and "rhand" (right hand)
37
+ # Quaternion channels: _quat_x, _quat_y, _quat_z, _quat_w
38
+ # Position channels: _pos_x, _pos_y, _pos_z
39
+ ch_names = [
40
+ "head_quat_x",
41
+ "head_quat_y",
42
+ "head_quat_z",
43
+ "head_quat_w",
44
+ "head_pos_x",
45
+ "head_pos_y",
46
+ "head_pos_z",
47
+ "rhand_quat_x",
48
+ "rhand_quat_y",
49
+ "rhand_quat_z",
50
+ "rhand_quat_w",
51
+ "rhand_pos_x",
52
+ "rhand_pos_y",
53
+ "rhand_pos_z",
54
+ ]
55
+ n_ch = len(ch_names)
56
+
57
+ data = rng.standard_normal((n_ch, n_times)) * 0.01
58
+
59
+ # Unit-normalise the quaternion rows so they behave like real orientation data
60
+ for quat_start in (0, 7):
61
+ q = data[quat_start : quat_start + 4]
62
+ data[quat_start : quat_start + 4] = q / np.linalg.norm(q, axis=0, keepdims=True)
63
+
64
+ info = mne.create_info(ch_names=ch_names, sfreq=SFREQ, ch_types=["misc"] * n_ch)
65
+ raw = mne.io.RawArray(data, info, verbose=False)
66
+
67
+ print("Channels in raw:", raw.ch_names)
68
+
69
+ # %% Detect rigid bodies
70
+
71
+ rb_names = find_rigid_bodies(raw)
72
+ print("Detected rigid bodies:", rb_names) # ['head', 'rhand']
73
+
74
+ # %% Process rigid bodies
75
+
76
+ raw_motion, rb_info = process_rigid_body(
77
+ raw,
78
+ lowpass_orient=8.0, # low-pass orientation signals at 8 Hz
79
+ lowpass_pos=8.0, # low-pass position at 8 Hz
80
+ lowpass_deriv=24.0, # low-pass velocity/acceleration at 24 Hz
81
+ compute_velocity=True,
82
+ compute_acceleration=True,
83
+ )
84
+
85
+ # Quaternion channels are replaced by Euler angles; velocity and acceleration
86
+ # channels are appended.
87
+ print("\nChannels after processing:")
88
+ for ch in raw_motion.ch_names:
89
+ print(" ", ch)
90
+
91
+ # rb_info contains a processing summary per body
92
+ for rb, info_entry in rb_info.items():
93
+ print(f"\n{rb}: mode={info_entry['mode']}, channels={info_entry['channels']}")
94
+
95
+ # %% Split by rigid body
96
+
97
+ body_raws = split_by_rigid_body(raw_motion)
98
+
99
+ for name, body_raw in body_raws.items():
100
+ print(f"\n{name}: {body_raw.n_times} samples, {len(body_raw.ch_names)} channels")
101
+ print(" ", body_raw.ch_names)