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,111 @@
1
+ """Linear neural mass model.
2
+
3
+ A simple linear model with damping, useful for testing and as a baseline
4
+ for comparison with nonlinear models.
5
+
6
+ References:
7
+ - Used as a canonical test model in TVB for validating simulation pipelines
8
+ and network dynamics.
9
+ """
10
+
11
+ import jax.numpy as jnp
12
+
13
+ from ...core.bunch import Bunch
14
+ from ..base import AbstractDynamics
15
+
16
+
17
+ class Linear(AbstractDynamics):
18
+ """Linear neural mass model with damping.
19
+
20
+ Single-state variable model representing simple damped linear dynamics, useful
21
+ for testing, debugging, and understanding basic coupling mechanisms without
22
+ nonlinear complications.
23
+
24
+ Notes
25
+ -----
26
+ **State equation:**
27
+
28
+ $$\\frac{dx}{dt} = \\gamma x + c_{\\text{delayed}} + c_{\\text{instant}}$$
29
+
30
+ where:
31
+
32
+ - $x$: State variable
33
+ - $\\gamma$: Damping coefficient (must be negative for stability)
34
+ - $c_{\\text{delayed}}$: Long-range coupling input
35
+ - $c_{\\text{instant}}$: Local coupling input (proportional to x)
36
+
37
+ The damping coefficient $\\gamma$ should be negative and its magnitude should
38
+ exceed the node's in-degree to ensure stability. For $\\gamma > 0$, the system
39
+ will exhibit exponential growth.
40
+
41
+ Attributes
42
+ ----------
43
+ STATE_NAMES : tuple of str
44
+ State variable: ``('x',)``
45
+ INITIAL_STATE : tuple of float
46
+ Default initial condition: ``(0.01,)``
47
+ AUXILIARY_NAMES : tuple of str
48
+ No auxiliary variables: ``()``
49
+ COUPLING_INPUTS : dict
50
+ Coupling specification: ``{'instant': 1, 'delayed': 1}``
51
+ DEFAULT_PARAMS : Bunch
52
+ Damping coefficient ``gamma=-10.0`` (must be negative for stability)
53
+ """
54
+
55
+ STATE_NAMES = ("x",)
56
+ INITIAL_STATE = (0.01,)
57
+
58
+ DEFAULT_PARAMS = Bunch(
59
+ gamma=-10.0, # Damping coefficient (must be negative for stability)
60
+ )
61
+
62
+ COUPLING_INPUTS = {
63
+ "instant": 1, # Local coupling (proportional to x)
64
+ "delayed": 1, # Long-range coupling
65
+ }
66
+
67
+ def dynamics(
68
+ self,
69
+ t: float,
70
+ state: jnp.ndarray,
71
+ params: Bunch,
72
+ coupling: Bunch,
73
+ external: Bunch,
74
+ ) -> jnp.ndarray:
75
+ """Compute linear dynamics.
76
+
77
+ Parameters
78
+ ----------
79
+ t : float
80
+ Current time
81
+ state : jnp.ndarray
82
+ Current state with shape ``[1, n_nodes]`` containing x
83
+ params : Bunch
84
+ Model parameters (gamma: damping coefficient)
85
+ coupling : Bunch
86
+ Coupling inputs with attributes:
87
+
88
+ - ``.instant[0]``: local coupling
89
+ - ``.delayed[0]``: long-range coupling
90
+ external : Bunch
91
+ External inputs (currently unused)
92
+
93
+ Returns
94
+ -------
95
+ derivatives : jnp.ndarray
96
+ State derivative with shape ``[1, n_nodes]``
97
+ """
98
+ # Unpack state
99
+ x = state[0]
100
+
101
+ # Unpack coupling
102
+ c_instant = coupling.instant[0] # Local coupling
103
+ c_delayed = coupling.delayed[0] # Long-range coupling
104
+
105
+ # Linear dynamics with damping and coupling
106
+ dx_dt = params.gamma * x + c_delayed + c_instant
107
+
108
+ # Package results
109
+ derivatives = jnp.array([dx_dt])
110
+
111
+ return derivatives
@@ -0,0 +1,137 @@
1
+ """Montbrio-Pazo-Roxin infinite theta neuron model.
2
+
3
+ Mean field model of infinite populations of all-to-all coupled quadratic
4
+ integrate-and-fire (QIF) neurons, derived via the Ott-Antonsen reduction.
5
+
6
+ References:
7
+ - Montbrio, Pazo & Roxin (2015). Macroscopic description for networks of
8
+ spiking neurons. Physical Review X, 5(2), 021028.
9
+ """
10
+
11
+ import jax.numpy as jnp
12
+
13
+ from ...core.bunch import Bunch
14
+ from ..base import AbstractDynamics
15
+
16
+
17
+ class MontbrioPazoRoxin(AbstractDynamics):
18
+ """Montbrio-Pazo-Roxin infinite theta neuron population model.
19
+
20
+ Two-dimensional mean field model describing the Ott-Antonsen reduction of
21
+ infinite all-to-all coupled quadratic integrate-and-fire (QIF) neurons.
22
+
23
+ Notes
24
+ -----
25
+ **State variables:**
26
+
27
+ - $r$: Average firing rate of the population
28
+ - $V$: Average membrane potential of the population
29
+
30
+ **State equations:**
31
+
32
+ $$
33
+ \\begin{aligned}
34
+ \\frac{dr}{dt} &= \\frac{1}{\\tau} \\left(\\frac{\\Delta}{\\pi \\tau} + 2Vr\\right) \\\\
35
+ \\frac{dV}{dt} &= \\frac{1}{\\tau} \\left(V^2 - (\\pi \\tau r)^2 + \\eta + J\\tau r + I + c_r c_{\\text{coup},r} + c_v c_{\\text{coup},V}\\right)
36
+ \\end{aligned}
37
+ $$
38
+
39
+ where $c_{\\text{coup},r}$ and $c_{\\text{coup},V}$ are the combined instant and
40
+ delayed coupling components, and $c_r$, $c_v$ are coupling weights.
41
+
42
+ The model has 2-dimensional coupling allowing independent coupling through firing
43
+ rate (r) and membrane potential (V).
44
+
45
+ Attributes
46
+ ----------
47
+ STATE_NAMES : tuple of str
48
+ State variables: ``('r', 'V')``
49
+ INITIAL_STATE : tuple of float
50
+ Default initial conditions: ``(0.1, 0.0)``
51
+ COUPLING_INPUTS : dict
52
+ Coupling specification: ``{'instant': 2, 'delayed': 2}``
53
+ DEFAULT_PARAMS : Bunch
54
+ Standard parameters for QIF neuron population
55
+
56
+ References
57
+ ----------
58
+ Montbrio, Pazo & Roxin (2015). Macroscopic description for networks of
59
+ spiking neurons. Physical Review X, 5(2), 021028.
60
+ """
61
+
62
+ STATE_NAMES = ("r", "V")
63
+ INITIAL_STATE = (0.1, 0.0)
64
+
65
+ DEFAULT_PARAMS = Bunch(
66
+ tau=1.0, # Characteristic time scale
67
+ I=0.0, # External current
68
+ Delta=1.0, # Width of heterogeneous noise distribution
69
+ J=15.0, # Mean synaptic weight
70
+ eta=-5.0, # Constant external input scaling
71
+ cr=1.0, # Coupling weight through r (firing rate)
72
+ cv=0.0, # Coupling weight through V (membrane potential)
73
+ )
74
+
75
+ COUPLING_INPUTS = {
76
+ "instant": 2, # Local coupling [r-component, V-component]
77
+ "delayed": 2, # Long-range coupling [r-component, V-component]
78
+ }
79
+
80
+ def dynamics(
81
+ self,
82
+ t: float,
83
+ state: jnp.ndarray,
84
+ params: Bunch,
85
+ coupling: Bunch,
86
+ external: Bunch,
87
+ ) -> jnp.ndarray:
88
+ """Compute Montbrio-Pazo-Roxin dynamics.
89
+
90
+ Args:
91
+ t: Current time
92
+ state: State [2, n_nodes] with (r, V)
93
+ params: Model parameters
94
+ coupling: Coupling inputs
95
+ - .instant[0]: local r-coupling
96
+ - .instant[1]: local V-coupling
97
+ - .delayed[0]: long-range r-coupling
98
+ - .delayed[1]: long-range V-coupling
99
+
100
+ Returns:
101
+ derivatives: [2, n_nodes] state derivatives
102
+ """
103
+ # Unpack state variables
104
+ r = state[0] # Average firing rate
105
+ V = state[1] # Average membrane potential
106
+
107
+ # Unpack coupling (both have r and V components)
108
+ c_instant_r = coupling.instant[0] # Local r-coupling
109
+ c_instant_V = coupling.instant[1] # Local V-coupling
110
+ c_delayed_r = coupling.delayed[0] # Long-range r-coupling
111
+ c_delayed_V = coupling.delayed[1] # Long-range V-coupling
112
+
113
+ # Total coupling for each variable
114
+ coupling_r = params.cr * (c_instant_r + c_delayed_r)
115
+ coupling_V = params.cv * (c_instant_V + c_delayed_V)
116
+
117
+ # Mean field dynamics
118
+ # Firing rate dynamics
119
+ dr_dt = (1.0 / params.tau) * (
120
+ params.Delta / (jnp.pi * params.tau) + 2.0 * V * r
121
+ )
122
+
123
+ # Membrane potential dynamics
124
+ dV_dt = (1.0 / params.tau) * (
125
+ V**2
126
+ - (jnp.pi * params.tau * r) ** 2
127
+ + params.eta
128
+ + params.J * params.tau * r
129
+ + params.I
130
+ + coupling_r
131
+ + coupling_V
132
+ )
133
+
134
+ # Package results
135
+ derivatives = jnp.array([dr_dt, dV_dt])
136
+
137
+ return derivatives
@@ -0,0 +1,131 @@
1
+ """Supercritical Hopf bifurcation normal form oscillator.
2
+
3
+ A two-dimensional oscillator in Cartesian coordinates representing the
4
+ normal form of a supercritical Hopf bifurcation, widely used for modeling
5
+ oscillatory dynamics near bifurcation points.
6
+
7
+ References:
8
+ - Deco et al. (2017). The dynamics of resting fluctuations in the brain:
9
+ metastability and its dynamical cortical core. Scientific Reports, 7, 3095.
10
+ - Kuznetsov (2004). Elements of Applied Bifurcation Theory (3rd ed.).
11
+ Springer-Verlag, New York.
12
+ """
13
+
14
+ import jax.numpy as jnp
15
+
16
+ from ...core.bunch import Bunch
17
+ from ..base import AbstractDynamics
18
+
19
+
20
+ class SupHopf(AbstractDynamics):
21
+ """Supercritical Hopf bifurcation oscillator.
22
+
23
+ Two-state model in Cartesian coordinates representing the normal form of a
24
+ supercritical Hopf bifurcation, widely used for modeling oscillatory dynamics
25
+ near bifurcation points.
26
+
27
+ Notes
28
+ -----
29
+ **State equations:**
30
+
31
+ $$
32
+ \\begin{aligned}
33
+ \\frac{dx}{dt} &= (a - x^2 - y^2) x - \\omega y + c_{\\text{delayed},x} + c_{\\text{instant}} \\\\
34
+ \\frac{dy}{dt} &= (a - x^2 - y^2) y + \\omega x + c_{\\text{delayed},y}
35
+ \\end{aligned}
36
+ $$
37
+
38
+ where:
39
+
40
+ - $a$: Bifurcation parameter ($a < 0$: stable fixed point, $a > 0$: limit cycle)
41
+ - $\\omega$: Angular frequency of oscillation
42
+ - Limit cycle amplitude: $\\sqrt{a}$ for $a > 0$
43
+
44
+ The delayed coupling is 2-dimensional, allowing separate coupling for x and y
45
+ components.
46
+
47
+ Attributes
48
+ ----------
49
+ STATE_NAMES : tuple of str
50
+ State variables: ``('x', 'y')``
51
+ INITIAL_STATE : tuple of float
52
+ Default initial conditions: ``(0.1, 0.0)``
53
+ AUXILIARY_NAMES : tuple of str
54
+ No auxiliary variables: ``()``
55
+ COUPLING_INPUTS : dict
56
+ Coupling specification: ``{'instant': 1, 'delayed': 2}``
57
+ DEFAULT_PARAMS : Bunch
58
+ Parameters: ``a=-0.5`` (bifurcation), ``omega=1.0`` (frequency)
59
+
60
+ References
61
+ ----------
62
+ Deco et al. (2017). The dynamics of resting fluctuations in the brain:
63
+ metastability and its dynamical cortical core. Scientific Reports, 7, 3095.
64
+ """
65
+
66
+ STATE_NAMES = ("x", "y")
67
+ INITIAL_STATE = (0.1, 0.0)
68
+
69
+ DEFAULT_PARAMS = Bunch(
70
+ a=-0.5, # Bifurcation parameter (a > 0: oscillations)
71
+ omega=1.0, # Angular frequency (rad/s or Hz)
72
+ )
73
+
74
+ COUPLING_INPUTS = {
75
+ "instant": 1, # Local coupling (x-component only)
76
+ "delayed": 2, # Long-range coupling [x-component, y-component]
77
+ }
78
+
79
+ def dynamics(
80
+ self,
81
+ t: float,
82
+ state: jnp.ndarray,
83
+ params: Bunch,
84
+ coupling: Bunch,
85
+ external: Bunch,
86
+ ) -> jnp.ndarray:
87
+ """Compute supercritical Hopf dynamics.
88
+
89
+ Parameters
90
+ ----------
91
+ t : float
92
+ Current time
93
+ state : jnp.ndarray
94
+ Current state with shape ``[2, n_nodes]`` containing ``(x, y)``
95
+ params : Bunch
96
+ Model parameters (a: bifurcation, omega: frequency)
97
+ coupling : Bunch
98
+ Coupling inputs with attributes:
99
+
100
+ - ``.instant[0]``: local coupling (x only)
101
+ - ``.delayed[0]``: long-range x-coupling
102
+ - ``.delayed[1]``: long-range y-coupling
103
+ external : Bunch
104
+ External inputs (currently unused)
105
+
106
+ Returns
107
+ -------
108
+ derivatives : jnp.ndarray
109
+ State derivatives with shape ``[2, n_nodes]``
110
+ """
111
+ # Unpack state variables
112
+ x = state[0] # x-component
113
+ y = state[1] # y-component
114
+
115
+ # Unpack coupling
116
+ c_instant = coupling.instant[0] # Local coupling (x only)
117
+ c_delayed_x = coupling.delayed[0] # Long-range x-coupling
118
+ c_delayed_y = coupling.delayed[1] # Long-range y-coupling
119
+
120
+ # Amplitude term (Hopf bifurcation nonlinearity)
121
+ r_squared = x**2 + y**2
122
+ amplitude_term = params.a - r_squared
123
+
124
+ # Supercritical Hopf dynamics in Cartesian coordinates
125
+ dx_dt = amplitude_term * x - params.omega * y + c_delayed_x + c_instant
126
+ dy_dt = amplitude_term * y + params.omega * x + c_delayed_y
127
+
128
+ # Package results
129
+ derivatives = jnp.array([dx_dt, dy_dt])
130
+
131
+ return derivatives
@@ -0,0 +1,210 @@
1
+ """Wilson-Cowan neural mass model.
2
+
3
+ Classic two-population model with excitatory and inhibitory interactions,
4
+ based on Wilson & Cowan's seminal work on neural population dynamics.
5
+
6
+ References:
7
+ - Wilson & Cowan (1972). Excitatory and inhibitory interactions in
8
+ localized populations of model neurons. Biophysical Journal, 12, 1-24.
9
+ - Wilson & Cowan (1973). A Mathematical Theory of the Functional Dynamics
10
+ of Cortical and Thalamic Nervous Tissue.
11
+ - Daffertshofer & van Wijk (2011). On the influence of amplitude on the
12
+ connectivity between phases. Frontiers in Neuroinformatics.
13
+ """
14
+
15
+ from typing import Tuple
16
+
17
+ import jax.numpy as jnp
18
+
19
+ from ...core.bunch import Bunch
20
+ from ..base import AbstractDynamics
21
+
22
+
23
+ class WilsonCowan(AbstractDynamics):
24
+ """Wilson-Cowan neural mass model with excitatory and inhibitory populations.
25
+
26
+ Two-population model representing the mean firing rates of excitatory (E) and
27
+ inhibitory (I) neural populations with sigmoid activation functions and
28
+ mutual interactions.
29
+
30
+ Notes
31
+ -----
32
+ **State equations:**
33
+
34
+ $$
35
+ \\begin{aligned}
36
+ \\frac{dE}{dt} &= \\frac{-E + (k_e - r_e E) S_e}{\\tau_e} \\\\
37
+ \\frac{dI}{dt} &= \\frac{-I + (k_i - r_i I) S_i}{\\tau_i}
38
+ \\end{aligned}
39
+ $$
40
+
41
+ **Sigmoid activation functions:**
42
+
43
+ $$
44
+ \\begin{aligned}
45
+ x_e &= \\alpha_e (c_{ee} E - c_{ei} I + P - \\theta_e + c_{\\text{delayed}} + \\text{lc}_e + \\text{lc}_i) \\\\
46
+ x_i &= \\alpha_i (c_{ie} E - c_{ii} I + Q - \\theta_i + \\text{lc}_e + \\text{lc}_i)
47
+ \\end{aligned}
48
+ $$
49
+
50
+ If ``shift_sigmoid=True`` (baseline-corrected):
51
+
52
+ $$S_e = c_e \\left(\\frac{1}{1+\\exp(-a_e(x_e-b_e))} - \\frac{1}{1+\\exp(a_e b_e)}\\right)$$
53
+
54
+ Otherwise (standard sigmoid):
55
+
56
+ $$S_e = \\frac{c_e}{1 + \\exp(-a_e(x_e - b_e))}$$
57
+
58
+ Attributes
59
+ ----------
60
+ STATE_NAMES : tuple of str
61
+ State variables: ``('E', 'I')``
62
+ INITIAL_STATE : tuple of float
63
+ Default initial conditions: ``(0.1, 0.05)``
64
+ AUXILIARY_NAMES : tuple of str
65
+ Auxiliary variables: ``('S_e', 'S_i')`` (sigmoid outputs)
66
+ COUPLING_INPUTS : dict
67
+ Coupling specification: ``{'instant': 2, 'delayed': 1}``
68
+ Instant coupling has 2 components [E, I] feeding into both populations;
69
+ delayed coupling feeds only into E population
70
+ DEFAULT_PARAMS : Bunch
71
+ Standard Wilson-Cowan parameters
72
+
73
+ References
74
+ ----------
75
+ Wilson & Cowan (1972). Excitatory and inhibitory interactions in localized
76
+ populations of model neurons. Biophysical Journal, 12, 1-24.
77
+ """
78
+
79
+ STATE_NAMES = ("E", "I")
80
+ INITIAL_STATE = (0.1, 0.05)
81
+
82
+ AUXILIARY_NAMES = ("S_e", "S_i")
83
+
84
+ DEFAULT_PARAMS = Bunch(
85
+ # Local connectivity weights
86
+ c_ee=12.0, # Excitatory to excitatory
87
+ c_ei=4.0, # Inhibitory to excitatory
88
+ c_ie=13.0, # Excitatory to inhibitory
89
+ c_ii=11.0, # Inhibitory to inhibitory
90
+ # Time constants (ms)
91
+ tau_e=10.0, # Excitatory population
92
+ tau_i=10.0, # Inhibitory population
93
+ # Sigmoid function parameters (excitatory)
94
+ a_e=1.2, # Gain/steepness
95
+ b_e=2.8, # Threshold
96
+ c_e=1.0, # Maximum response
97
+ theta_e=0.0, # Baseline threshold
98
+ # Sigmoid function parameters (inhibitory)
99
+ a_i=1.0, # Gain/steepness
100
+ b_i=4.0, # Threshold
101
+ c_i=1.0, # Maximum response
102
+ theta_i=0.0, # Baseline threshold
103
+ # Response modulation parameters
104
+ r_e=1.0, # Excitatory refractory parameter
105
+ r_i=1.0, # Inhibitory refractory parameter
106
+ k_e=1.0, # Excitatory gain parameter
107
+ k_i=1.0, # Inhibitory gain parameter
108
+ # External inputs
109
+ P=0.0, # External input to excitatory population
110
+ Q=0.0, # External input to inhibitory population
111
+ # Input gain parameters
112
+ alpha_e=1.0, # Excitatory input scaling
113
+ alpha_i=1.0, # Inhibitory input scaling
114
+ # Sigmoid shift option
115
+ shift_sigmoid=False, # Whether to use baseline-corrected sigmoid
116
+ )
117
+
118
+ COUPLING_INPUTS = {
119
+ "instant": 2, # Local coupling [from E, from I]
120
+ "delayed": 1, # Long-range delayed coupling
121
+ }
122
+
123
+ def dynamics(
124
+ self,
125
+ t: float,
126
+ state: jnp.ndarray,
127
+ params: Bunch,
128
+ coupling: Bunch,
129
+ external: Bunch,
130
+ ) -> Tuple[jnp.ndarray, jnp.ndarray]:
131
+ """Compute Wilson-Cowan dynamics.
132
+
133
+ Parameters
134
+ ----------
135
+ t : float
136
+ Current time (ms)
137
+ state : jnp.ndarray
138
+ Current state with shape ``[2, n_nodes]`` containing (E, I)
139
+ params : Bunch
140
+ Model parameters
141
+ coupling : Bunch
142
+ Coupling inputs with attributes:
143
+
144
+ - ``.instant[0]``: local E coupling
145
+ - ``.instant[1]``: local I coupling
146
+ - ``.delayed[0]``: long-range coupling
147
+ external : Bunch
148
+ External inputs (currently unused)
149
+
150
+ Returns
151
+ -------
152
+ derivatives : jnp.ndarray
153
+ State derivatives with shape ``[2, n_nodes]``
154
+ auxiliaries : jnp.ndarray
155
+ Auxiliary variables with shape ``[2, n_nodes]`` containing ``(S_e, S_i)`` sigmoid outputs
156
+ """
157
+ # Unpack state variables
158
+ E = state[0] # Excitatory activity
159
+ I = state[1] # Inhibitory activity
160
+
161
+ # Unpack coupling
162
+ lc_e = coupling.instant[0] # Local coupling from E
163
+ lc_i = coupling.instant[1] # Local coupling from I
164
+ c_delayed = coupling.delayed[0] # Long-range coupling
165
+
166
+ # Compute inputs to populations
167
+ # Both local couplings (E and I) feed into both populations
168
+ # Long-range coupling only feeds into excitatory
169
+ x_e = params.alpha_e * (
170
+ params.c_ee * E
171
+ - params.c_ei * I
172
+ + params.P
173
+ - params.theta_e
174
+ + c_delayed
175
+ + lc_e
176
+ + lc_i
177
+ )
178
+
179
+ x_i = params.alpha_i * (
180
+ params.c_ie * E - params.c_ii * I + params.Q - params.theta_i + lc_e + lc_i
181
+ )
182
+
183
+ # Compute sigmoid activation functions
184
+ if params.shift_sigmoid:
185
+ # Baseline-corrected sigmoid (passes through zero)
186
+ sigmoid_offset_e = 1.0 / (1.0 + jnp.exp(params.a_e * params.b_e))
187
+ S_e = params.c_e * (
188
+ 1.0 / (1.0 + jnp.exp(-params.a_e * (x_e - params.b_e)))
189
+ - sigmoid_offset_e
190
+ )
191
+
192
+ sigmoid_offset_i = 1.0 / (1.0 + jnp.exp(params.a_i * params.b_i))
193
+ S_i = params.c_i * (
194
+ 1.0 / (1.0 + jnp.exp(-params.a_i * (x_i - params.b_i)))
195
+ - sigmoid_offset_i
196
+ )
197
+ else:
198
+ # Standard sigmoid
199
+ S_e = params.c_e / (1.0 + jnp.exp(-params.a_e * (x_e - params.b_e)))
200
+ S_i = params.c_i / (1.0 + jnp.exp(-params.a_i * (x_i - params.b_i)))
201
+
202
+ # Population dynamics
203
+ dE_dt = (-E + (params.k_e - params.r_e * E) * S_e) / params.tau_e
204
+ dI_dt = (-I + (params.k_i - params.r_i * I) * S_i) / params.tau_i
205
+
206
+ # Package results
207
+ derivatives = jnp.array([dE_dt, dI_dt])
208
+ auxiliaries = jnp.array([S_e, S_i])
209
+
210
+ return derivatives, auxiliaries