Braindecode 1.5.0.dev982__py3-none-any.whl → 1.5.0.dev984__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.
@@ -298,34 +298,95 @@ def _build_windowed_repr(
298
298
 
299
299
 
300
300
  def _zarr_to_memmap(zarr_path, group_name):
301
- """Decompress a zarr array to a float64 ``.npy`` memmap (one-time, atomic).
302
-
303
- Saved as float64 so MNE objects can wrap the memmap zero-copy
304
- (MNE requires float64 internally). Copy-on-write mode (``'c'``)
305
- means preprocessing writes go to RAM, the file stays untouched.
301
+ """Materialise a zarr array as a float64 ``.npy`` memmap, race-free.
302
+
303
+ Decompress the ``group_name`` array inside ``zarr_path`` into a
304
+ float64 ``.npy`` file inside a sibling ``.<zarr>_memmap/`` cache
305
+ directory and return the path to that file. The dtype is float64
306
+ because MNE objects wrap the memmap zero-copy and MNE requires
307
+ float64 internally. Callers open the result with
308
+ ``np.load(path, mmap_mode='c')`` so preprocessing writes land in
309
+ anonymous copy-on-write pages and never mutate the on-disk file.
310
+
311
+ Concurrency contract
312
+ --------------------
313
+ Safe to call from any number of threads or processes concurrently
314
+ on the same ``(zarr_path, group_name)``, on local POSIX, NFSv3,
315
+ Lustre and SMB. The published ``.npy`` file is **created exactly
316
+ once and never replaced**: subsequent callers either hit the
317
+ already-materialised file on the fast path or lose the
318
+ publication race and discard their own copy.
319
+
320
+ The atomic publication step is ``os.link``, not ``rename``.
321
+ ``os.link(tmp, dst)`` either creates ``dst`` pointing at ``tmp``'s
322
+ inode or fails with :class:`FileExistsError` when ``dst`` already
323
+ exists — the "create only if absent" primitive we need, available
324
+ on every POSIX filesystem. ``rename`` was the wrong primitive:
325
+ it atomically *replaces* its destination, which unlinks the inode
326
+ that concurrent readers may already have ``mmap``'d. On NFSv3
327
+ that produced ``.nfsXXXX`` silly-rename files and intermittent
328
+ ``SIGBUS`` when workers page-faulted on the unlinked inode.
329
+
330
+ Under ``N`` concurrent writers, every racer materialises the data
331
+ into its own per-pid temp file and then tries to publish it; at
332
+ most one ``os.link`` wins, and the losers silently ``unlink``
333
+ their temp file in a ``finally`` block. The wasted I/O scales
334
+ with the number of racers; for the workloads braindecode targets
335
+ (a handful of concurrent workers per group) it is negligible.
336
+
337
+ Crash recovery: if a writer dies mid-run the only debris is a
338
+ ``<group>.<pid>.tmp.npy`` file. Such files are namespaced by pid
339
+ so they can never collide with a live writer, they are never
340
+ read by the fast path, and they do not prevent subsequent
341
+ callers from producing a correct result. Caches written by
342
+ earlier braindecode versions remain valid: the fast path only
343
+ requires ``<group>.npy`` to exist.
306
344
  """
307
345
  zarr_p = Path(zarr_path)
308
346
  cache_dir = zarr_p.parent / f".{zarr_p.name}_memmap"
309
347
  npy_path = cache_dir / f"{group_name}.npy"
310
348
 
311
- if not npy_path.exists():
312
- arr = zarr.open(zarr_path, mode="r")[group_name]["data"]
313
- cache_dir.mkdir(parents=True, exist_ok=True)
314
- tmp_path = cache_dir / f"{group_name}.{os.getpid()}.tmp.npy"
315
- # Write chunk-by-chunk to avoid materializing the full array in RAM
349
+ # Fast path: the file is already fully materialised. A single
350
+ # stat() is all the steady-state workload pays.
351
+ if npy_path.exists():
352
+ return npy_path
353
+
354
+ cache_dir.mkdir(parents=True, exist_ok=True)
355
+ arr = zarr.open(zarr_path, mode="r")[group_name]["data"]
356
+ tmp_path = cache_dir / f"{group_name}.{os.getpid()}.tmp.npy"
357
+ try:
358
+ # Write chunk-by-chunk to avoid materialising the whole zarr
359
+ # array in RAM.
316
360
  mm = np.lib.format.open_memmap(
317
361
  tmp_path, mode="w+", dtype=np.float64, shape=arr.shape
318
362
  )
319
- chunk0 = arr.chunks[0] if hasattr(arr, "chunks") else arr.shape[0]
320
- for start in range(0, arr.shape[0], chunk0):
321
- end = min(start + chunk0, arr.shape[0])
322
- mm[start:end] = np.asarray(arr[start:end], dtype=np.float64)
323
- mm.flush()
324
- del mm
325
363
  try:
326
- tmp_path.rename(npy_path)
327
- except OSError:
328
- tmp_path.unlink(missing_ok=True)
364
+ chunk0 = arr.chunks[0] if hasattr(arr, "chunks") else arr.shape[0]
365
+ for start in range(0, arr.shape[0], chunk0):
366
+ end = min(start + chunk0, arr.shape[0])
367
+ mm[start:end] = np.asarray(arr[start:end], dtype=np.float64)
368
+ mm.flush()
369
+ finally:
370
+ del mm
371
+
372
+ # Atomic publication. ``os.link`` never replaces an existing
373
+ # destination, so the published inode is immutable for the
374
+ # lifetime of the cache directory. Concurrent readers never
375
+ # see their mmap'd inode vanish from under them.
376
+ try:
377
+ os.link(tmp_path, npy_path)
378
+ except FileExistsError:
379
+ # Another writer beat us to it. Their file is the
380
+ # canonical one; ours is discarded in the finally block.
381
+ pass
382
+ finally:
383
+ # Always remove our private per-pid temp file. It is
384
+ # namespaced by pid so this cannot clobber a concurrent
385
+ # writer's in-progress tmp file.
386
+ try:
387
+ os.unlink(tmp_path)
388
+ except FileNotFoundError:
389
+ pass
329
390
 
330
391
  return npy_path
331
392
 
@@ -1,5 +1,8 @@
1
1
  from __future__ import annotations
2
2
 
3
+ # Authors: Sarthak Tayal <sarthaktayal2@gmail.com>
4
+ #
5
+ # License: BSD (3-clause)
3
6
  import math
4
7
 
5
8
  from einops.layers.torch import Rearrange
@@ -275,8 +278,6 @@ class AttentionBaseNet(EEGModuleMixin, nn.Module):
275
278
  activation: type[nn.Module] = nn.ELU,
276
279
  extra_params: bool = False,
277
280
  ):
278
- super(AttentionBaseNet, self).__init__()
279
-
280
281
  super().__init__(
281
282
  n_outputs=n_outputs,
282
283
  n_chans=n_chans,
@@ -1,3 +1,7 @@
1
+ # Authors: Sarthak Tayal <sarthaktayal2@gmail.com>
2
+ #
3
+ # License: BSD (3-clause)
4
+
1
5
  import torch
2
6
  import torch.nn as nn
3
7
  import torch.nn.functional as F
@@ -51,11 +55,11 @@ class SyncNet(EEGModuleMixin, nn.Module):
51
55
  The initialization range for omega parameters using uniform
52
56
  distribution. Default is (0, 1).
53
57
  beta_init_values : tuple of float, optional
54
- The initialization range for beta parameters using uniform
55
- distribution. Default is (0, 1). Default is (0, 0.05).
58
+ The initialization range for beta (decay) parameters using uniform
59
+ distribution. Default is (0, 0.05).
56
60
  phase_init_values : tuple of float, optional
57
- The initialization range for phase parameters using `normal`
58
- distribution. Default is (0, 1). Default is (0, 0.05).
61
+ The initialization mean and standard deviation for phase
62
+ parameters using normal distribution. Default is (0, 0.05).
59
63
 
60
64
 
61
65
  Notes
@@ -146,12 +150,12 @@ class SyncNet(EEGModuleMixin, nn.Module):
146
150
  # Phase Shift
147
151
  self.phi_ini = nn.Parameter(
148
152
  torch.FloatTensor(1, 1, self.n_chans, self.num_filters).normal_(
149
- self.beta_init_values[0], self.beta_init_values[1]
153
+ self.phase_init_values[0], self.phase_init_values[1]
150
154
  )
151
155
  )
152
156
  self.beta = nn.Parameter(
153
157
  torch.FloatTensor(1, 1, 1, self.num_filters).uniform_(
154
- self.phase_init_values[0], self.phase_init_values[1]
158
+ self.beta_init_values[0], self.beta_init_values[1]
155
159
  )
156
160
  )
157
161
 
@@ -185,21 +189,21 @@ class SyncNet(EEGModuleMixin, nn.Module):
185
189
  # Output: (batch_size, n_chans, 1, n_times)
186
190
 
187
191
  # Compute the oscillatory component
192
+ # Shape: (1, filter_width, n_chans, num_filters)
188
193
  W_osc = self.amplitude * torch.cos(self.t * self.omega + self.phi_ini)
189
- # W_osc is (1, filter_width, n_chans, 1)
190
194
 
191
195
  # Compute the decay component
192
- t_squared = torch.pow(self.t, 2) # Shape: (filter_width,)
193
- t_squared_beta = t_squared * self.beta # Shape: (filter_width, num_filters)
196
+ # Shape: (1, filter_width, 1, num_filters)
197
+ t_squared = torch.pow(self.t, 2)
198
+ t_squared_beta = t_squared * self.beta
194
199
  W_decay = torch.exp(-t_squared_beta)
195
- # W_osc is (1, filter_width, 1, 1)
196
200
 
197
201
  # Combine oscillatory and decay components
198
- # W shape: (1, n_chans, num_filters, filter_width)
202
+ # Shape: (1, filter_width, n_chans, num_filters)
199
203
  W = W_osc * W_decay
200
- # W shape will be: (1, filter_width, n_chans, 1)
201
204
 
202
- W = W.view(self.num_filters, self.n_chans, 1, self.filter_width)
205
+ # Permute to conv2d weight shape (out_channels, in_channels, kH, kW)
206
+ W = W.permute(3, 2, 0, 1).contiguous()
203
207
 
204
208
  # Apply convolution
205
209
  x_padded = self.pad_input(x.float())
@@ -1,4 +1,4 @@
1
- # Authors: Bruno Aristimunha <b.aristimunha>
1
+ # Authors: Bruno Aristimunha <b.aristimunha@gmail.com>
2
2
  #
3
3
  # License: BSD (3-clause)
4
4
 
braindecode/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "1.5.0.dev982"
1
+ __version__ = "1.5.0.dev984"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: braindecode
3
- Version: 1.5.0.dev982
3
+ Version: 1.5.0.dev984
4
4
  Summary: Deep learning software to decode EEG, ECG or MEG signals
5
5
  Author-email: Robin Tibor Schirrmeister <robintibor@gmail.com>, Bruno Aristimunha Pinto <b.aristimunha@gmail.com>, Alexandre Gramfort <agramfort@meta.com>
6
6
  Maintainer-email: Alexandre Gramfort <agramfort@meta.com>, Bruno Aristimunha Pinto <b.aristimunha@gmail.com>, Robin Tibor Schirrmeister <robintibor@gmail.com>
@@ -3,13 +3,13 @@ braindecode/classifier.py,sha256=7kC_oY_UzHEes_WWdCvEpiA1ZKxMeuLL5tIPp5rfcpg,962
3
3
  braindecode/eegneuralnet.py,sha256=xjE6aPZdCQPs29NIpy_m1GLMMC2WZ3Db0Fuh1-xE1h4,13827
4
4
  braindecode/regressor.py,sha256=KiMJpqCUPWA2k2JWk9HGYTzeoBqJ4gAKEudeUVcFZY4,9266
5
5
  braindecode/util.py,sha256=f8bNIwt-SwsHqheH_BADQxTtA9oPt3Lb7GFnoI-Huwc,14101
6
- braindecode/version.py,sha256=NViteJgXY2XCTtVnQLPK1Ak2pdsVODMJMzeRYTw6fbc,29
6
+ braindecode/version.py,sha256=5WqXRAeL_e0Aq78aGQsveuaWwGcV_OgtE7s9XQXznkc,29
7
7
  braindecode/augmentation/__init__.py,sha256=4xune2QUK6KHMKsAqijF7I9eeiVbP0wEoQJjCNLNcKM,1081
8
8
  braindecode/augmentation/base.py,sha256=OJ1shOljI1yTY9zh2qWxQwivlY43sfx9Q-MAyMhxtPs,7338
9
9
  braindecode/augmentation/functional.py,sha256=q2k6mAXrujYlOZUndcjZN8e8b-6oJF1gGsORAI23hyE,43998
10
10
  braindecode/augmentation/transforms.py,sha256=x-3pwX0PtMHfSnPLGKNXbpTSk7j17Ci2FG_-646scg4,47268
11
11
  braindecode/datasets/__init__.py,sha256=rVOBadwqYBiMz5kl7nGiBOmMgr11xvjS4nuzzZTOn1U,1102
12
- braindecode/datasets/base.py,sha256=ghLcjCJqvUkJhtgHuZ5bHKIhDEUmaQrt-U2jw8wQ7T8,61697
12
+ braindecode/datasets/base.py,sha256=7qFEJNOAe_05evfqxx-yrlUfG0eWaCuKkfOlj32ShwU,64701
13
13
  braindecode/datasets/bbci.py,sha256=SCm7OnCObotILQ0B1EdmZPoyJtzsRXpeU_gNKtqQLSc,19288
14
14
  braindecode/datasets/bcicomp.py,sha256=YWIsRYFvwBFHnd9CRxo_BBQWFg-rg0UirRTsd0Ml2Oc,7550
15
15
  braindecode/datasets/chb_mit.py,sha256=XGUVtADLHLTa5Ldanyn8msAJY0SQh-eV9keHnWO6n3A,7231
@@ -41,7 +41,7 @@ braindecode/functional/functions.py,sha256=G29BPqIndSdvJiilDBZovklK_einjzFDyfn-3
41
41
  braindecode/functional/initialization.py,sha256=Nn773hEMWVYDYeunFv73tdxdtcYwXFC7M4HzrfWsaHQ,1385
42
42
  braindecode/models/__init__.py,sha256=sLJIAT_N2ZblDjt9sbOLQvfdTLWWmxgV8VzfS0nIqYs,3071
43
43
  braindecode/models/atcnet.py,sha256=JzLV92WYWlhQol8AqE9xeWvlnWN_uswYhVOkMnuBCXo,32231
44
- braindecode/models/attentionbasenet.py,sha256=nIvKen5FTuGBrdkodpIxfNekYuoTg-DpjXjlbrR06ZQ,30454
44
+ braindecode/models/attentionbasenet.py,sha256=k7ar7aEjANudPu7krAZsRx-ag61ugirS1Xn7qFKNfWg,30483
45
45
  braindecode/models/attn_sleep.py,sha256=F9x4spTtzfiCC1h9UYITmIDQeJW6_2CXTZktZX9R0RE,17950
46
46
  braindecode/models/base.py,sha256=yGJgr0f5rD-gJZ5Msw9FzGzVO-x_re-6jOSK8Iht6x4,27923
47
47
  braindecode/models/bendr.py,sha256=kqIKtIrgHhIdwYfBFwAJ1g69YbdpA8bs2C2e2GUT0rY,25643
@@ -85,10 +85,10 @@ braindecode/models/sleep_stager_chambon_2018.py,sha256=WvhQGPhBGRBXr07QLKZjHDncv
85
85
  braindecode/models/sparcnet.py,sha256=jGK-HMThncg9A0kUqXvREKYieYqOCQzpsYAe1pTub_0,14000
86
86
  braindecode/models/sstdpn.py,sha256=zFM-_FROgX3pVHOX5AOln1PNYiSipwfGHVoCZZHDN5s,34219
87
87
  braindecode/models/summary.csv,sha256=GGX-0vE6koMLKW-fzBX11OS5tKg9hgY9vPd2e1FGA6w,8508
88
- braindecode/models/syncnet.py,sha256=3Ry6XrvQG5muGpWOrcVflUvylYFtL_kigXHvsvCzims,8406
88
+ braindecode/models/syncnet.py,sha256=CaQt8do0gf8ydmsOPs1FpNEYyJ_R0CVu_hEDJuMEbHE,8420
89
89
  braindecode/models/tcn.py,sha256=ssHaixrCRalGCoP3--wY3ztGwcFQNFta5CeH1nefYas,8237
90
90
  braindecode/models/tidnet.py,sha256=IA29vDhcH5LdrZaDDkMu1IAi3mZ39AsS4xA1MYTD-c0,11844
91
- braindecode/models/tsinception.py,sha256=poBR9lzbaem9MoMY_NqOUBBhSCw9PcEsHk1SkcSU0dY,10364
91
+ braindecode/models/tsinception.py,sha256=hPoDR21ynFscTO31r9aD6vPVm-uPnYCq1ne2ffTA8fI,10374
92
92
  braindecode/models/usleep.py,sha256=VaIRbTSuKisD_W6fMwLlY5bNiH41fBnfx0k-iiLHWpw,17316
93
93
  braindecode/models/util.py,sha256=QSku9ZlXTyJzMuTLG-VlPPifTe2_6huo7i9-zQnhZho,17728
94
94
  braindecode/modules/__init__.py,sha256=NChqITh6yMcagoYFVkQC14aJjCAGkRO8fYQrH_0Zv1k,1977
@@ -119,9 +119,9 @@ braindecode/training/scoring.py,sha256=WRkwqbitA3m_dzRnGp2ZIZPge5Nhx9gAEQhIHzeH4
119
119
  braindecode/visualization/__init__.py,sha256=4EER_xHqZIDzEvmgUEm7K1bgNKpyZAIClR9ZCkMuY4M,240
120
120
  braindecode/visualization/confusion_matrices.py,sha256=qIWMLEHow5CJ7PhGggD8mnD55Le6xhma9HSzt4R33fc,9509
121
121
  braindecode/visualization/gradients.py,sha256=-NuWIlpOeSR2OWGfUl6jn1RqdNPxmiQNLPCckWb8SRE,2330
122
- braindecode-1.5.0.dev982.dist-info/licenses/LICENSE.txt,sha256=7rg7k6hyj8m9whQ7dpKbqnCssoOEx_Mbtqb4uSOjljE,1525
123
- braindecode-1.5.0.dev982.dist-info/licenses/NOTICE.txt,sha256=sOxuTbalPxTM8H6VqtvGbXCt_BoOF7JevEYG_knqbm4,620
124
- braindecode-1.5.0.dev982.dist-info/METADATA,sha256=HyPHY1J4w21v0GHxsAhvIHIWW2gVUkZEt_ZxAeZ6lxA,10099
125
- braindecode-1.5.0.dev982.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
126
- braindecode-1.5.0.dev982.dist-info/top_level.txt,sha256=pHsWQmSy0uhIez62-HA9j0iaXKvSbUL39ifFRkFnChA,12
127
- braindecode-1.5.0.dev982.dist-info/RECORD,,
122
+ braindecode-1.5.0.dev984.dist-info/licenses/LICENSE.txt,sha256=7rg7k6hyj8m9whQ7dpKbqnCssoOEx_Mbtqb4uSOjljE,1525
123
+ braindecode-1.5.0.dev984.dist-info/licenses/NOTICE.txt,sha256=sOxuTbalPxTM8H6VqtvGbXCt_BoOF7JevEYG_knqbm4,620
124
+ braindecode-1.5.0.dev984.dist-info/METADATA,sha256=gs4siQJaVjCx8UszxeuPfWYFF_FPk2aagFqBzmIZbTI,10099
125
+ braindecode-1.5.0.dev984.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
126
+ braindecode-1.5.0.dev984.dist-info/top_level.txt,sha256=pHsWQmSy0uhIez62-HA9j0iaXKvSbUL39ifFRkFnChA,12
127
+ braindecode-1.5.0.dev984.dist-info/RECORD,,