tvboptim 0.2.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.
Files changed (70) hide show
  1. tvboptim/__init__.py +9 -0
  2. tvboptim/data/__init__.py +12 -0
  3. tvboptim/data/loaders.py +99 -0
  4. tvboptim/execution.py +268 -0
  5. tvboptim/experimental/__init__.py +15 -0
  6. tvboptim/experimental/network_dynamics/__init__.py +28 -0
  7. tvboptim/experimental/network_dynamics/core/__init__.py +6 -0
  8. tvboptim/experimental/network_dynamics/core/bunch.py +67 -0
  9. tvboptim/experimental/network_dynamics/core/network.py +686 -0
  10. tvboptim/experimental/network_dynamics/coupling/__init__.py +22 -0
  11. tvboptim/experimental/network_dynamics/coupling/base.py +1005 -0
  12. tvboptim/experimental/network_dynamics/coupling/linear.py +466 -0
  13. tvboptim/experimental/network_dynamics/coupling/subspace.py +509 -0
  14. tvboptim/experimental/network_dynamics/coupling/tvb.py +202 -0
  15. tvboptim/experimental/network_dynamics/dynamics/__init__.py +7 -0
  16. tvboptim/experimental/network_dynamics/dynamics/base.py +541 -0
  17. tvboptim/experimental/network_dynamics/dynamics/lorenz.py +207 -0
  18. tvboptim/experimental/network_dynamics/dynamics/tvb/__init__.py +29 -0
  19. tvboptim/experimental/network_dynamics/dynamics/tvb/coombes_byrne.py +154 -0
  20. tvboptim/experimental/network_dynamics/dynamics/tvb/epileptor.py +200 -0
  21. tvboptim/experimental/network_dynamics/dynamics/tvb/generic_2d_oscillator.py +149 -0
  22. tvboptim/experimental/network_dynamics/dynamics/tvb/jansen_rit.py +158 -0
  23. tvboptim/experimental/network_dynamics/dynamics/tvb/kuramoto.py +120 -0
  24. tvboptim/experimental/network_dynamics/dynamics/tvb/larter_breakspear.py +221 -0
  25. tvboptim/experimental/network_dynamics/dynamics/tvb/linear.py +111 -0
  26. tvboptim/experimental/network_dynamics/dynamics/tvb/montbrio_pazo_roxin.py +137 -0
  27. tvboptim/experimental/network_dynamics/dynamics/tvb/sup_hopf.py +131 -0
  28. tvboptim/experimental/network_dynamics/dynamics/tvb/wilson_cowan.py +210 -0
  29. tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang.py +124 -0
  30. tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang_exc_inh.py +177 -0
  31. tvboptim/experimental/network_dynamics/external_input/__init__.py +20 -0
  32. tvboptim/experimental/network_dynamics/external_input/base.py +152 -0
  33. tvboptim/experimental/network_dynamics/external_input/data.py +203 -0
  34. tvboptim/experimental/network_dynamics/external_input/parametric.py +270 -0
  35. tvboptim/experimental/network_dynamics/graph/__init__.py +14 -0
  36. tvboptim/experimental/network_dynamics/graph/base.py +706 -0
  37. tvboptim/experimental/network_dynamics/graph/sparse.py +885 -0
  38. tvboptim/experimental/network_dynamics/graph/utils.py +0 -0
  39. tvboptim/experimental/network_dynamics/noise/__init__.py +6 -0
  40. tvboptim/experimental/network_dynamics/noise/base.py +162 -0
  41. tvboptim/experimental/network_dynamics/noise/gaussian.py +71 -0
  42. tvboptim/experimental/network_dynamics/result.py +78 -0
  43. tvboptim/experimental/network_dynamics/solve.py +721 -0
  44. tvboptim/experimental/network_dynamics/solvers/__init__.py +15 -0
  45. tvboptim/experimental/network_dynamics/solvers/base.py +46 -0
  46. tvboptim/experimental/network_dynamics/solvers/diffrax.py +63 -0
  47. tvboptim/experimental/network_dynamics/solvers/native.py +308 -0
  48. tvboptim/experimental/network_dynamics/utils/__init__.py +3 -0
  49. tvboptim/experimental/network_dynamics/utils/printer.py +555 -0
  50. tvboptim/model.py +111 -0
  51. tvboptim/observations/__init__.py +3 -0
  52. tvboptim/observations/observation.py +32 -0
  53. tvboptim/observations/tvb_monitors/__init__.py +11 -0
  54. tvboptim/observations/tvb_monitors/bold.py +290 -0
  55. tvboptim/observations/tvb_monitors/downsampling.py +185 -0
  56. tvboptim/optim/__init__.py +4 -0
  57. tvboptim/optim/callbacks.py +333 -0
  58. tvboptim/optim/optax.py +379 -0
  59. tvboptim/types/__init__.py +35 -0
  60. tvboptim/types/parameter.py +769 -0
  61. tvboptim/types/spaces.py +943 -0
  62. tvboptim/types/stateutils.py +109 -0
  63. tvboptim/utils/__init__.py +16 -0
  64. tvboptim/utils/caching.py +82 -0
  65. tvboptim/utils/utils.py +265 -0
  66. tvboptim-0.2.0.dist-info/METADATA +157 -0
  67. tvboptim-0.2.0.dist-info/RECORD +70 -0
  68. tvboptim-0.2.0.dist-info/WHEEL +5 -0
  69. tvboptim-0.2.0.dist-info/licenses/LICENSE +278 -0
  70. tvboptim-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,466 @@
1
+ """Concrete coupling implementations.
2
+
3
+ This module provides common coupling functions:
4
+ - LinearCoupling: Simple linear coupling
5
+ - DifferenceCoupling: Coupling based on state differences
6
+ - DelayedLinearCoupling: Linear coupling with delays
7
+ """
8
+
9
+ import jax.numpy as jnp
10
+
11
+ from ..core.bunch import Bunch
12
+ from .base import DelayedCoupling, InstantaneousCoupling
13
+
14
+
15
+ class LinearCoupling(InstantaneousCoupling):
16
+ """Simple linear coupling function.
17
+
18
+ Standard linear coupling with gain and offset parameters, implementing the
19
+ transformation:
20
+
21
+ $$c = G \\cdot \\sum_{j} w_{ij} x_j + b$$
22
+
23
+ where $w_{ij}$ are the graph weights, $x_j$ are the incoming states, $G$ is the
24
+ global coupling strength, and $b$ is an offset term.
25
+
26
+ Parameters
27
+ ----------
28
+ incoming_states : str or list of str
29
+ State name(s) to collect from connected nodes (required)
30
+ local_states : str or list of str, optional
31
+ State name(s) from current node (default: ``[]``)
32
+
33
+ Attributes
34
+ ----------
35
+ N_OUTPUT_STATES : int
36
+ Number of output coupling states: ``1``
37
+ DEFAULT_PARAMS : Bunch
38
+ Default parameters: ``G=1.0`` (global coupling strength), ``b=0.0`` (offset/bias)
39
+
40
+ Examples
41
+ --------
42
+ >>> # Couple via 'x' state from connected nodes
43
+ >>> coupling = LinearCoupling(incoming_states='x', G=1.0)
44
+ >>>
45
+ >>> # With offset
46
+ >>> coupling = LinearCoupling(incoming_states='x', G=0.5, b=0.1)
47
+ """
48
+
49
+ N_OUTPUT_STATES = 1
50
+ DEFAULT_PARAMS = Bunch(G=1.0, b=0.0)
51
+
52
+ # No pre() override - uses default identity which returns per-edge format
53
+ # This matches v1 LinearCoupling behavior
54
+
55
+ def post(
56
+ self, summed_inputs: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
57
+ ) -> jnp.ndarray:
58
+ """Apply linear transformation to summed inputs.
59
+
60
+ Parameters
61
+ ----------
62
+ summed_inputs : jnp.ndarray
63
+ Summed coupling inputs with shape ``[n_inputs, n_nodes]``
64
+ local_states : jnp.ndarray
65
+ Local states (not used)
66
+ params : Bunch
67
+ Parameters with G (coupling strength) and b (offset)
68
+
69
+ Returns
70
+ -------
71
+ jnp.ndarray
72
+ Transformed coupling with shape ``[n_inputs, n_nodes]``
73
+ """
74
+ return params.G * summed_inputs + params.b
75
+
76
+
77
+ class FastLinearCoupling(InstantaneousCoupling):
78
+ """Fast linear coupling using vectorized mode.
79
+
80
+ Uses local states (instead of incoming states) to trigger vectorized matrix
81
+ multiplication instead of per-edge element-wise operations. This is significantly
82
+ faster for dense all-to-all connectivity patterns.
83
+
84
+ Implements the same transformation as LinearCoupling:
85
+
86
+ $$c = G \\cdot \\sum_{j} w_{ij} x_j + b$$
87
+
88
+ but uses optimized matrix operations.
89
+
90
+ Parameters
91
+ ----------
92
+ local_states : str or list of str
93
+ State name(s) from current node (required for vectorized mode)
94
+
95
+ Attributes
96
+ ----------
97
+ N_OUTPUT_STATES : int
98
+ Number of output coupling states: ``1``
99
+ DEFAULT_PARAMS : Bunch
100
+ Default parameters: ``G=1.0`` (coupling strength), ``b=0.0`` (offset/bias)
101
+
102
+ Notes
103
+ -----
104
+ Use this variant when you have dense connectivity and want better performance.
105
+ For sparse graphs, the standard LinearCoupling may be more efficient.
106
+
107
+ Examples
108
+ --------
109
+ >>> # Fast vectorized coupling for dense networks
110
+ >>> coupling = FastLinearCoupling(local_states='S', G=1.0)
111
+ """
112
+
113
+ N_OUTPUT_STATES = 1
114
+ DEFAULT_PARAMS = Bunch(G=1.0, b=0.0)
115
+
116
+ def pre(self, incoming_states, local_states, params):
117
+ """Return local states to trigger vectorized mode.
118
+
119
+ By returning [n_local, n_nodes] (2D), we trigger the vectorized
120
+ path which uses matmul instead of per-edge ops.
121
+ """
122
+ # Return local states (2D) instead of incoming states (3D)
123
+ # This triggers vectorized mode: summed = pre_states @ graph.weights
124
+ return local_states
125
+
126
+ def post(self, summed_inputs, local_states, params):
127
+ """Apply linear transformation to summed inputs."""
128
+ return params.G * summed_inputs + params.b
129
+
130
+
131
+ class DifferenceCoupling(InstantaneousCoupling):
132
+ """Diffusive coupling based on state differences.
133
+
134
+ Computes coupling based on the difference between incoming and local states,
135
+ implementing:
136
+
137
+ $$c_i = G \\cdot \\sum_{j} w_{ij} (x_j - x_i)$$
138
+
139
+ This type of coupling is useful for synchronization and consensus dynamics,
140
+ as it drives nodes toward common states. Automatically uses sparse-optimized
141
+ computation for sparse graphs.
142
+
143
+ Parameters
144
+ ----------
145
+ incoming_states : str or list of str
146
+ State name(s) to collect from connected nodes (required)
147
+ local_states : str or list of str
148
+ State name(s) from current node (required for computing differences)
149
+
150
+ Attributes
151
+ ----------
152
+ N_OUTPUT_STATES : int
153
+ Number of output coupling states: ``1``
154
+ DEFAULT_PARAMS : Bunch
155
+ Default parameters: ``G=1.0`` (global coupling strength)
156
+
157
+ Examples
158
+ --------
159
+ >>> # Diffusive coupling via 'x' state
160
+ >>> coupling = DifferenceCoupling(incoming_states='x', local_states='x', G=1.0)
161
+ """
162
+
163
+ N_OUTPUT_STATES = 1
164
+ DEFAULT_PARAMS = Bunch(G=1.0)
165
+
166
+ def pre(
167
+ self, incoming_states: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
168
+ ) -> jnp.ndarray:
169
+ """Compute difference between incoming and local states (per-edge).
170
+
171
+ Args:
172
+ incoming_states: States from connected nodes in per-edge format
173
+ [n_incoming, n_nodes_target, n_nodes_source]
174
+ local_states: States from current node [n_local, n_nodes]
175
+ params: Coupling parameters (not used in pre)
176
+
177
+ Returns:
178
+ State differences [n_incoming, n_nodes, n_nodes] (per-edge)
179
+ """
180
+ # Broadcast local_states to per-edge format and compute difference
181
+ # local_states[:, :, None] gives [n_local, n_nodes, 1]
182
+ # incoming_states has shape [n_incoming, n_nodes, n_nodes]
183
+ return incoming_states - local_states[:, :, None]
184
+
185
+ def post(
186
+ self, summed_inputs: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
187
+ ) -> jnp.ndarray:
188
+ """Apply coupling strength to summed differences.
189
+
190
+ Args:
191
+ summed_inputs: Summed differences [n_inputs, n_nodes]
192
+ local_states: Local states (not used)
193
+ params: Bunch with G
194
+
195
+ Returns:
196
+ Scaled coupling [n_inputs, n_nodes]
197
+ """
198
+ return params.G * summed_inputs
199
+
200
+
201
+ class SigmoidCoupling(InstantaneousCoupling):
202
+ """Linear coupling with sigmoid post-processing.
203
+
204
+ Applies a sigmoid nonlinearity after linear transformation, implementing:
205
+
206
+ $$c = G \\cdot \\sigma\\left(s \\cdot (a \\cdot \\sum_{j} w_{ij} x_j + b - m)\\right)$$
207
+
208
+ where $\\sigma(x) = 1/(1+e^{-x})$ is the sigmoid function, $s$ is the slope,
209
+ and $m$ is the midpoint. Useful for saturating coupling effects.
210
+
211
+ Parameters
212
+ ----------
213
+ incoming_states : str or list of str
214
+ State name(s) to collect from connected nodes (required)
215
+ local_states : str or list of str, optional
216
+ State name(s) from current node (default: ``[]``)
217
+
218
+ Attributes
219
+ ----------
220
+ N_OUTPUT_STATES : int
221
+ Number of output coupling states: ``1``
222
+ DEFAULT_PARAMS : Bunch
223
+ Default parameters: ``G=1.0`` (global coupling strength), ``a=1.0`` (input scaling),
224
+ ``b=0.0`` (offset), ``slope=1.0`` (sigmoid steepness), ``midpoint=0.0`` (sigmoid center)
225
+
226
+ Examples
227
+ --------
228
+ >>> coupling = SigmoidCoupling(incoming_states='x', G=1.0, slope=2.0, midpoint=0.0)
229
+ """
230
+
231
+ N_OUTPUT_STATES = 1
232
+ DEFAULT_PARAMS = Bunch(G=1.0, a=1.0, b=0.0, slope=1.0, midpoint=0.0)
233
+
234
+ def post(
235
+ self, summed_inputs: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
236
+ ) -> jnp.ndarray:
237
+ """Apply linear transformation followed by sigmoid.
238
+
239
+ Args:
240
+ summed_inputs: Summed coupling inputs [n_inputs, n_nodes]
241
+ local_states: Local states (not used)
242
+ params: Bunch with G, a, b, slope, midpoint
243
+
244
+ Returns:
245
+ Sigmoid-transformed coupling [n_inputs, n_nodes]
246
+ """
247
+ import jax.nn
248
+
249
+ linear = params.a * summed_inputs + params.b
250
+ sigmoid = jax.nn.sigmoid(params.slope * (linear - params.midpoint))
251
+ return params.G * sigmoid
252
+
253
+
254
+ class TanhCoupling(InstantaneousCoupling):
255
+ """Coupling with hyperbolic tangent saturation.
256
+
257
+ Applies tanh nonlinearity for symmetric saturation, implementing:
258
+
259
+ $$c = G \\cdot \\tanh\\left(s \\cdot \\sum_{j} w_{ij} x_j\\right)$$
260
+
261
+ where $s$ is the scaling factor. The tanh function provides symmetric saturation
262
+ in the range $(-1, 1)$.
263
+
264
+ Parameters
265
+ ----------
266
+ incoming_states : str or list of str
267
+ State name(s) to collect from connected nodes (required)
268
+ local_states : str or list of str, optional
269
+ State name(s) from current node (default: ``[]``)
270
+
271
+ Attributes
272
+ ----------
273
+ N_OUTPUT_STATES : int
274
+ Number of output coupling states: ``1``
275
+ DEFAULT_PARAMS : Bunch
276
+ Default parameters: ``G=0.5`` (global coupling strength), ``scale=2.0`` (scaling before tanh)
277
+
278
+ Examples
279
+ --------
280
+ >>> coupling = TanhCoupling(incoming_states='x', G=0.5, scale=2.0)
281
+ """
282
+
283
+ N_OUTPUT_STATES = 1
284
+ DEFAULT_PARAMS = Bunch(G=0.5, scale=2.0)
285
+
286
+ def post(
287
+ self, summed_inputs: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
288
+ ) -> jnp.ndarray:
289
+ """Apply tanh saturation to summed inputs.
290
+
291
+ Args:
292
+ summed_inputs: Summed coupling inputs [n_inputs, n_nodes]
293
+ local_states: Local states (not used)
294
+ params: Bunch with G, scale
295
+
296
+ Returns:
297
+ Tanh-transformed coupling [n_inputs, n_nodes]
298
+ """
299
+ return params.G * jnp.tanh(params.scale * summed_inputs)
300
+
301
+
302
+ # Delayed coupling variants
303
+
304
+
305
+ class DelayedLinearCoupling(DelayedCoupling):
306
+ """Linear coupling with transmission delays.
307
+
308
+ Standard linear coupling with delays between nodes, implementing:
309
+
310
+ $$c_i(t) = G \\cdot \\sum_{j} w_{ij} x_j(t - \\tau_{ij}) + b$$
311
+
312
+ where $\\tau_{ij}$ are the transmission delays between nodes.
313
+
314
+ Parameters
315
+ ----------
316
+ incoming_states : str or list of str
317
+ State name(s) to collect from connected nodes (required)
318
+ local_states : str or list of str, optional
319
+ State name(s) from current node (default: ``[]``)
320
+
321
+ Attributes
322
+ ----------
323
+ N_OUTPUT_STATES : int
324
+ Number of output coupling states: ``1``
325
+ DEFAULT_PARAMS : Bunch
326
+ Default parameters: ``G=1.0`` (coupling strength), ``b=0.0`` (offset/bias)
327
+
328
+ Examples
329
+ --------
330
+ >>> coupling = DelayedLinearCoupling(incoming_states='S', G=1.0)
331
+ """
332
+
333
+ N_OUTPUT_STATES = 1
334
+ DEFAULT_PARAMS = Bunch(G=1.0, b=0.0)
335
+
336
+ def post(
337
+ self, summed_inputs: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
338
+ ) -> jnp.ndarray:
339
+ """Apply linear transformation to summed delayed inputs.
340
+
341
+ Args:
342
+ summed_inputs: Summed delayed coupling inputs [n_inputs, n_nodes]
343
+ local_states: Local states (not used)
344
+ params: Bunch with G, b
345
+
346
+ Returns:
347
+ Transformed coupling [n_inputs, n_nodes]
348
+ """
349
+ return params.G * summed_inputs + params.b
350
+
351
+
352
+ class DelayedDifferenceCoupling(DelayedCoupling):
353
+ """Diffusive coupling with transmission delays.
354
+
355
+ Computes coupling based on the difference between delayed incoming states
356
+ and current local states, implementing:
357
+
358
+ $$c_i(t) = G \\cdot \\sum_{j} w_{ij} (x_j(t - \\tau_{ij}) - x_i(t))$$
359
+
360
+ This combines diffusive coupling with delayed transmission, useful for modeling
361
+ synchronization dynamics with finite propagation speeds.
362
+
363
+ Parameters
364
+ ----------
365
+ incoming_states : str or list of str
366
+ State name(s) to collect from connected nodes (required)
367
+ local_states : str or list of str
368
+ State name(s) from current node (required for computing differences)
369
+
370
+ Attributes
371
+ ----------
372
+ N_OUTPUT_STATES : int
373
+ Number of output coupling states: ``1``
374
+ DEFAULT_PARAMS : Bunch
375
+ Default parameters: ``G=1.0`` (global coupling strength)
376
+
377
+ Examples
378
+ --------
379
+ >>> coupling = DelayedDifferenceCoupling(incoming_states='x', local_states='x', G=1.0)
380
+ """
381
+
382
+ N_OUTPUT_STATES = 1
383
+ DEFAULT_PARAMS = Bunch(G=1.0)
384
+
385
+ def pre(
386
+ self, delayed_states: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
387
+ ) -> jnp.ndarray:
388
+ """Compute difference between delayed and local states (per-edge).
389
+
390
+ Args:
391
+ delayed_states: Delayed states from history in per-edge format
392
+ [n_incoming, n_nodes_target, n_nodes_source]
393
+ local_states: Current local states [n_local, n_nodes]
394
+ params: Coupling parameters (not used in pre)
395
+
396
+ Returns:
397
+ State differences [n_incoming, n_nodes, n_nodes] (per-edge)
398
+ """
399
+ # Broadcast local_states to per-edge format and compute difference
400
+ # local_states[:, :, None] gives [n_local, n_nodes, 1]
401
+ return delayed_states - local_states[:, :, None]
402
+
403
+ def post(
404
+ self, summed_inputs: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
405
+ ) -> jnp.ndarray:
406
+ """Apply coupling strength to summed delayed differences.
407
+
408
+ Args:
409
+ summed_inputs: Summed differences [n_inputs, n_nodes]
410
+ local_states: Local states (not used)
411
+ params: Bunch with G
412
+
413
+ Returns:
414
+ Scaled coupling [n_inputs, n_nodes]
415
+ """
416
+ return params.G * summed_inputs
417
+
418
+
419
+ class DelayedSigmoidCoupling(DelayedCoupling):
420
+ """Sigmoid coupling with transmission delays.
421
+
422
+ Applies sigmoid nonlinearity to delayed coupling inputs, implementing:
423
+
424
+ $$c_i(t) = G \\cdot \\sigma\\left(s \\cdot \\left(\\sum_{j} w_{ij} x_j(t - \\tau_{ij}) - m\\right)\\right)$$
425
+
426
+ where $\\sigma(x) = 1/(1+e^{-x})$ is the sigmoid function, $s$ is the slope,
427
+ and $m$ is the midpoint.
428
+
429
+ Parameters
430
+ ----------
431
+ incoming_states : str or list of str
432
+ State name(s) to collect from connected nodes (required)
433
+
434
+ Attributes
435
+ ----------
436
+ N_OUTPUT_STATES : int
437
+ Number of output coupling states: ``1``
438
+ DEFAULT_PARAMS : Bunch
439
+ Default parameters: ``G=1.0`` (global coupling strength), ``slope=1.0`` (sigmoid steepness),
440
+ ``midpoint=0.0`` (sigmoid center)
441
+
442
+ Examples
443
+ --------
444
+ >>> coupling = DelayedSigmoidCoupling(incoming_states='x', G=1.0, slope=1.0, midpoint=0.0)
445
+ """
446
+
447
+ N_OUTPUT_STATES = 1
448
+ DEFAULT_PARAMS = Bunch(G=1.0, slope=1.0, midpoint=0.0)
449
+
450
+ def post(
451
+ self, summed_inputs: jnp.ndarray, local_states: jnp.ndarray, params: Bunch
452
+ ) -> jnp.ndarray:
453
+ """Apply sigmoid to summed delayed inputs.
454
+
455
+ Args:
456
+ summed_inputs: Summed delayed coupling inputs [n_inputs, n_nodes]
457
+ local_states: Local states (not used)
458
+ params: Bunch with G, slope, midpoint
459
+
460
+ Returns:
461
+ Sigmoid-transformed coupling [n_inputs, n_nodes]
462
+ """
463
+ import jax.nn
464
+
465
+ sigmoid = jax.nn.sigmoid(params.slope * (summed_inputs - params.midpoint))
466
+ return params.G * sigmoid