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.
- tvboptim/__init__.py +9 -0
- tvboptim/data/__init__.py +12 -0
- tvboptim/data/loaders.py +99 -0
- tvboptim/execution.py +268 -0
- tvboptim/experimental/__init__.py +15 -0
- tvboptim/experimental/network_dynamics/__init__.py +28 -0
- tvboptim/experimental/network_dynamics/core/__init__.py +6 -0
- tvboptim/experimental/network_dynamics/core/bunch.py +67 -0
- tvboptim/experimental/network_dynamics/core/network.py +686 -0
- tvboptim/experimental/network_dynamics/coupling/__init__.py +22 -0
- tvboptim/experimental/network_dynamics/coupling/base.py +1005 -0
- tvboptim/experimental/network_dynamics/coupling/linear.py +466 -0
- tvboptim/experimental/network_dynamics/coupling/subspace.py +509 -0
- tvboptim/experimental/network_dynamics/coupling/tvb.py +202 -0
- tvboptim/experimental/network_dynamics/dynamics/__init__.py +7 -0
- tvboptim/experimental/network_dynamics/dynamics/base.py +541 -0
- tvboptim/experimental/network_dynamics/dynamics/lorenz.py +207 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/__init__.py +29 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/coombes_byrne.py +154 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/epileptor.py +200 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/generic_2d_oscillator.py +149 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/jansen_rit.py +158 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/kuramoto.py +120 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/larter_breakspear.py +221 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/linear.py +111 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/montbrio_pazo_roxin.py +137 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/sup_hopf.py +131 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/wilson_cowan.py +210 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang.py +124 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang_exc_inh.py +177 -0
- tvboptim/experimental/network_dynamics/external_input/__init__.py +20 -0
- tvboptim/experimental/network_dynamics/external_input/base.py +152 -0
- tvboptim/experimental/network_dynamics/external_input/data.py +203 -0
- tvboptim/experimental/network_dynamics/external_input/parametric.py +270 -0
- tvboptim/experimental/network_dynamics/graph/__init__.py +14 -0
- tvboptim/experimental/network_dynamics/graph/base.py +706 -0
- tvboptim/experimental/network_dynamics/graph/sparse.py +885 -0
- tvboptim/experimental/network_dynamics/graph/utils.py +0 -0
- tvboptim/experimental/network_dynamics/noise/__init__.py +6 -0
- tvboptim/experimental/network_dynamics/noise/base.py +162 -0
- tvboptim/experimental/network_dynamics/noise/gaussian.py +71 -0
- tvboptim/experimental/network_dynamics/result.py +78 -0
- tvboptim/experimental/network_dynamics/solve.py +721 -0
- tvboptim/experimental/network_dynamics/solvers/__init__.py +15 -0
- tvboptim/experimental/network_dynamics/solvers/base.py +46 -0
- tvboptim/experimental/network_dynamics/solvers/diffrax.py +63 -0
- tvboptim/experimental/network_dynamics/solvers/native.py +308 -0
- tvboptim/experimental/network_dynamics/utils/__init__.py +3 -0
- tvboptim/experimental/network_dynamics/utils/printer.py +555 -0
- tvboptim/model.py +111 -0
- tvboptim/observations/__init__.py +3 -0
- tvboptim/observations/observation.py +32 -0
- tvboptim/observations/tvb_monitors/__init__.py +11 -0
- tvboptim/observations/tvb_monitors/bold.py +290 -0
- tvboptim/observations/tvb_monitors/downsampling.py +185 -0
- tvboptim/optim/__init__.py +4 -0
- tvboptim/optim/callbacks.py +333 -0
- tvboptim/optim/optax.py +379 -0
- tvboptim/types/__init__.py +35 -0
- tvboptim/types/parameter.py +769 -0
- tvboptim/types/spaces.py +943 -0
- tvboptim/types/stateutils.py +109 -0
- tvboptim/utils/__init__.py +16 -0
- tvboptim/utils/caching.py +82 -0
- tvboptim/utils/utils.py +265 -0
- tvboptim-0.2.0.dist-info/METADATA +157 -0
- tvboptim-0.2.0.dist-info/RECORD +70 -0
- tvboptim-0.2.0.dist-info/WHEEL +5 -0
- tvboptim-0.2.0.dist-info/licenses/LICENSE +278 -0
- tvboptim-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Solver implementations for Network Dynamics."""
|
|
2
|
+
|
|
3
|
+
from .base import AbstractSolver, NativeSolver
|
|
4
|
+
from .diffrax import DiffraxSolver
|
|
5
|
+
from .native import BoundedSolver, Euler, Heun, RungeKutta4
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"AbstractSolver",
|
|
9
|
+
"NativeSolver",
|
|
10
|
+
"DiffraxSolver",
|
|
11
|
+
"Euler",
|
|
12
|
+
"Heun",
|
|
13
|
+
"RungeKutta4",
|
|
14
|
+
"BoundedSolver",
|
|
15
|
+
]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Base solver classes for Network Dynamics.
|
|
2
|
+
|
|
3
|
+
This module defines the abstract base classes for all solver types.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import Callable
|
|
8
|
+
|
|
9
|
+
import jax.numpy as jnp
|
|
10
|
+
|
|
11
|
+
from ..core.bunch import Bunch
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class AbstractSolver(ABC):
|
|
15
|
+
"""Base class for all solver types."""
|
|
16
|
+
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class NativeSolver(AbstractSolver):
|
|
21
|
+
"""Base class for Network Dynamics's native solvers (manual implementations)."""
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def step(
|
|
25
|
+
self,
|
|
26
|
+
dynamics_fn: Callable,
|
|
27
|
+
t: float,
|
|
28
|
+
state: jnp.ndarray,
|
|
29
|
+
coupling_input: jnp.ndarray,
|
|
30
|
+
dt: float,
|
|
31
|
+
params: Bunch,
|
|
32
|
+
) -> jnp.ndarray:
|
|
33
|
+
"""Single integration step.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
dynamics_fn: Network dynamics function
|
|
37
|
+
t: Current time
|
|
38
|
+
state: Current state, shape [n_states, n_nodes]
|
|
39
|
+
coupling_input: Coupling input, shape [n_coupling_inputs, n_nodes]
|
|
40
|
+
dt: Timestep
|
|
41
|
+
params: Network parameters
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
next_state: Updated state, shape [n_states, n_nodes]
|
|
45
|
+
"""
|
|
46
|
+
pass
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Diffrax-based solvers with advanced features."""
|
|
2
|
+
|
|
3
|
+
import diffrax
|
|
4
|
+
from diffrax import SaveAt
|
|
5
|
+
|
|
6
|
+
from .base import AbstractSolver
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class DiffraxSolver(AbstractSolver):
|
|
10
|
+
"""Wrapper for Diffrax solvers with advanced features.
|
|
11
|
+
|
|
12
|
+
This class forwards the most commonly used diffeqsolve parameters explicitly
|
|
13
|
+
and allows access to all other parameters via **kwargs.
|
|
14
|
+
|
|
15
|
+
Important Notes:
|
|
16
|
+
- When max_steps is specified, Diffrax may pad solution arrays (ts, ys)
|
|
17
|
+
with inf values beyond the actual integration steps taken.
|
|
18
|
+
- Users should filter finite values in post-processing if needed:
|
|
19
|
+
|
|
20
|
+
>>> finite_mask = jnp.isfinite(solution.ts)
|
|
21
|
+
>>> ts_filtered = solution.ts[finite_mask]
|
|
22
|
+
>>> ys_filtered = solution.ys[finite_mask]
|
|
23
|
+
|
|
24
|
+
- To avoid inf-padding, explicitly specify saveat with exact time points:
|
|
25
|
+
|
|
26
|
+
>>> solver = DiffraxSolver(
|
|
27
|
+
... solver=diffrax.Euler(),
|
|
28
|
+
... saveat=diffrax.SaveAt(ts=jnp.linspace(0, 100, 1000))
|
|
29
|
+
... )
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
solver,
|
|
35
|
+
# Common parameters made explicit
|
|
36
|
+
saveat=None,
|
|
37
|
+
stepsize_controller=None,
|
|
38
|
+
max_steps=4096,
|
|
39
|
+
# Forward everything else
|
|
40
|
+
**kwargs,
|
|
41
|
+
):
|
|
42
|
+
"""Initialize with Diffrax solver instance and options.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
solver: Diffrax solver instance (e.g., diffrax.Heun())
|
|
46
|
+
saveat: SaveAt instance for output control
|
|
47
|
+
stepsize_controller: Step size controller for adaptive stepping
|
|
48
|
+
max_steps: Maximum integration steps
|
|
49
|
+
**kwargs: Additional arguments passed to diffeqsolve (e.g., adjoint, event,
|
|
50
|
+
throw, progress_meter, solver_state, controller_state, made_jump, etc.)
|
|
51
|
+
See https://docs.kidger.site/diffrax/api/diffeqsolve/ for full list.
|
|
52
|
+
"""
|
|
53
|
+
self.solver = solver
|
|
54
|
+
self.saveat = saveat if saveat is not None else SaveAt(steps=True)
|
|
55
|
+
self.stepsize_controller = (
|
|
56
|
+
stepsize_controller
|
|
57
|
+
if stepsize_controller is not None
|
|
58
|
+
else diffrax.ConstantStepSize()
|
|
59
|
+
)
|
|
60
|
+
self.max_steps = max_steps
|
|
61
|
+
|
|
62
|
+
# Store additional kwargs for diffeqsolve
|
|
63
|
+
self.diffrax_kwargs = kwargs
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""Native solvers for network system.
|
|
2
|
+
|
|
3
|
+
This module provides native solver implementations that work with the
|
|
4
|
+
multi-coupling architecture. These solvers work with wrapped dynamics
|
|
5
|
+
functions that already include coupling computation.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from typing import Callable
|
|
9
|
+
|
|
10
|
+
import jax.numpy as jnp
|
|
11
|
+
|
|
12
|
+
from ..core.bunch import Bunch
|
|
13
|
+
from .base import AbstractSolver
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class NativeSolver(AbstractSolver):
|
|
17
|
+
"""Base class for Network Dynamics native solvers.
|
|
18
|
+
|
|
19
|
+
Solvers integrate only the dynamics state. Coupling state management
|
|
20
|
+
happens outside the solver in the scan loop.
|
|
21
|
+
|
|
22
|
+
The dynamics_fn passed to step() should already have coupling computation
|
|
23
|
+
bundled in via closure, so the solver only needs to worry about time
|
|
24
|
+
integration of the dynamics state.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def step(
|
|
28
|
+
self,
|
|
29
|
+
dynamics_fn: Callable,
|
|
30
|
+
t: float,
|
|
31
|
+
state: jnp.ndarray,
|
|
32
|
+
dt: float,
|
|
33
|
+
params: Bunch,
|
|
34
|
+
noise_sample: jnp.ndarray = 0.0,
|
|
35
|
+
) -> tuple[jnp.ndarray, jnp.ndarray]:
|
|
36
|
+
"""Single integration step for dynamics state.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
dynamics_fn: Dynamics function (t, state, params) -> (derivatives, auxiliaries)
|
|
40
|
+
Coupling is already computed inside via closure
|
|
41
|
+
t: Current time
|
|
42
|
+
state: Network state array [n_states, n_nodes]
|
|
43
|
+
dt: Time step
|
|
44
|
+
params: Parameters (params.dynamics, etc.)
|
|
45
|
+
noise_sample: Scaled noise for SDEs [n_states, n_nodes]
|
|
46
|
+
Default 0.0 for deterministic systems
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Tuple of (next_state, auxiliaries):
|
|
50
|
+
- next_state: Updated network state array [n_states, n_nodes]
|
|
51
|
+
- auxiliaries: Auxiliary variables array [n_auxiliaries, n_nodes]
|
|
52
|
+
Empty array if no auxiliaries
|
|
53
|
+
"""
|
|
54
|
+
raise NotImplementedError("Subclasses must implement step()")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Euler(NativeSolver):
|
|
58
|
+
"""Euler method
|
|
59
|
+
|
|
60
|
+
For ODEs: Standard Euler when noise_sample=0
|
|
61
|
+
For SDEs: Euler-Maruyama when noise_sample provided
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
def step(
|
|
65
|
+
self,
|
|
66
|
+
dynamics_fn: Callable,
|
|
67
|
+
t: float,
|
|
68
|
+
state: jnp.ndarray,
|
|
69
|
+
dt: float,
|
|
70
|
+
params: Bunch,
|
|
71
|
+
noise_sample: jnp.ndarray = 0.0,
|
|
72
|
+
) -> tuple[jnp.ndarray, jnp.ndarray]:
|
|
73
|
+
"""Euler integration step: y_{n+1} = y_n + dt * f(t, y_n) + noise.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
dynamics_fn: Dynamics function (t, state, params) -> (derivatives, auxiliaries)
|
|
77
|
+
t: Current time
|
|
78
|
+
state: Current state [n_states, n_nodes]
|
|
79
|
+
dt: Time step
|
|
80
|
+
params: Parameters
|
|
81
|
+
noise_sample: Pre-scaled noise increment
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
Tuple of (next_state, auxiliaries):
|
|
85
|
+
- next_state: Next state [n_states, n_nodes]
|
|
86
|
+
- auxiliaries: Auxiliary variables at current time [n_auxiliaries, n_nodes]
|
|
87
|
+
"""
|
|
88
|
+
# Compute derivatives and auxiliaries (coupling already inside dynamics_fn)
|
|
89
|
+
result = dynamics_fn(t, state, params)
|
|
90
|
+
|
|
91
|
+
# Handle both formats: just derivatives or (derivatives, auxiliaries)
|
|
92
|
+
if isinstance(result, tuple):
|
|
93
|
+
derivatives, auxiliaries = result
|
|
94
|
+
else:
|
|
95
|
+
derivatives = result
|
|
96
|
+
auxiliaries = jnp.array([]) # Empty array for no auxiliaries
|
|
97
|
+
|
|
98
|
+
# Euler step for state integration
|
|
99
|
+
next_state = state + dt * derivatives + noise_sample
|
|
100
|
+
|
|
101
|
+
return next_state, auxiliaries
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class Heun(NativeSolver):
|
|
105
|
+
"""Heun's method (improved Euler).
|
|
106
|
+
|
|
107
|
+
Two-stage method with predictor-corrector structure.
|
|
108
|
+
For SDEs: Stochastic Heun method.
|
|
109
|
+
|
|
110
|
+
Note: Uses coupling assumption that coupling is slow relative to dt,
|
|
111
|
+
so we can reuse the same coupling for both stages.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
def step(
|
|
115
|
+
self,
|
|
116
|
+
dynamics_fn: Callable,
|
|
117
|
+
t: float,
|
|
118
|
+
state: jnp.ndarray,
|
|
119
|
+
dt: float,
|
|
120
|
+
params: Bunch,
|
|
121
|
+
noise_sample: jnp.ndarray = 0.0,
|
|
122
|
+
) -> tuple[jnp.ndarray, jnp.ndarray]:
|
|
123
|
+
"""Heun integration step with predictor-corrector.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
dynamics_fn: Dynamics function (t, state, params) -> (derivatives, auxiliaries)
|
|
127
|
+
Same coupling used for both evaluations
|
|
128
|
+
t: Current time
|
|
129
|
+
state: Current state [n_states, n_nodes]
|
|
130
|
+
dt: Time step
|
|
131
|
+
params: Parameters
|
|
132
|
+
noise_sample: Pre-scaled noise increment
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
Tuple of (next_state, auxiliaries):
|
|
136
|
+
- next_state: Next state [n_states, n_nodes]
|
|
137
|
+
- auxiliaries: Auxiliary variables from FIRST evaluation [n_auxiliaries, n_nodes]
|
|
138
|
+
(represents valid state at current time t)
|
|
139
|
+
"""
|
|
140
|
+
# First evaluation: k1 = f(t, y_n)
|
|
141
|
+
result1 = dynamics_fn(t, state, params)
|
|
142
|
+
|
|
143
|
+
# Handle both formats and extract first evaluation results
|
|
144
|
+
if isinstance(result1, tuple):
|
|
145
|
+
k1, auxiliaries = result1 # Keep auxiliaries from first evaluation
|
|
146
|
+
else:
|
|
147
|
+
k1 = result1
|
|
148
|
+
auxiliaries = jnp.array([])
|
|
149
|
+
|
|
150
|
+
# Predictor step
|
|
151
|
+
y_pred = state + dt * k1 + noise_sample
|
|
152
|
+
|
|
153
|
+
# Second evaluation: k2 = f(t + dt, y_pred)
|
|
154
|
+
# Coupling assumption: reuse same coupling (expensive to recompute)
|
|
155
|
+
result2 = dynamics_fn(t + dt, y_pred, params)
|
|
156
|
+
|
|
157
|
+
# Extract k2 (discard auxiliaries from second evaluation)
|
|
158
|
+
if isinstance(result2, tuple):
|
|
159
|
+
k2 = result2[0]
|
|
160
|
+
else:
|
|
161
|
+
k2 = result2
|
|
162
|
+
|
|
163
|
+
# Corrector: average drift, apply noise once
|
|
164
|
+
next_state = state + dt * 0.5 * (k1 + k2) + noise_sample
|
|
165
|
+
|
|
166
|
+
return next_state, auxiliaries
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class RungeKutta4(NativeSolver):
|
|
170
|
+
"""Classical 4th order Runge-Kutta method (RK4).
|
|
171
|
+
|
|
172
|
+
Four-stage explicit method with high accuracy for smooth ODEs.
|
|
173
|
+
For SDEs: Applies noise at the end of the RK4 step.
|
|
174
|
+
|
|
175
|
+
Note: Uses coupling assumption that coupling is slow relative to dt,
|
|
176
|
+
so we can reuse the same coupling for all four stages.
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
def step(
|
|
180
|
+
self,
|
|
181
|
+
dynamics_fn: Callable,
|
|
182
|
+
t: float,
|
|
183
|
+
state: jnp.ndarray,
|
|
184
|
+
dt: float,
|
|
185
|
+
params: Bunch,
|
|
186
|
+
noise_sample: jnp.ndarray = 0.0,
|
|
187
|
+
) -> tuple[jnp.ndarray, jnp.ndarray]:
|
|
188
|
+
"""RK4 integration step with four evaluations.
|
|
189
|
+
|
|
190
|
+
Args:
|
|
191
|
+
dynamics_fn: Dynamics function (t, state, params) -> (derivatives, auxiliaries)
|
|
192
|
+
Same coupling used for all evaluations
|
|
193
|
+
t: Current time
|
|
194
|
+
state: Current state [n_states, n_nodes]
|
|
195
|
+
dt: Time step
|
|
196
|
+
params: Parameters
|
|
197
|
+
noise_sample: Pre-scaled noise increment (applied at end of step)
|
|
198
|
+
|
|
199
|
+
Returns:
|
|
200
|
+
Tuple of (next_state, auxiliaries):
|
|
201
|
+
- next_state: Next state [n_states, n_nodes]
|
|
202
|
+
- auxiliaries: Auxiliary variables from FIRST evaluation [n_auxiliaries, n_nodes]
|
|
203
|
+
(represents valid state at current time t)
|
|
204
|
+
"""
|
|
205
|
+
# First evaluation: k1 = f(t, y_n)
|
|
206
|
+
result1 = dynamics_fn(t, state, params)
|
|
207
|
+
|
|
208
|
+
# Handle both formats and extract first evaluation results
|
|
209
|
+
if isinstance(result1, tuple):
|
|
210
|
+
k1, auxiliaries = result1 # Keep auxiliaries from first evaluation
|
|
211
|
+
else:
|
|
212
|
+
k1 = result1
|
|
213
|
+
auxiliaries = jnp.array([])
|
|
214
|
+
|
|
215
|
+
# Second evaluation: k2 = f(t + dt/2, y_n + dt/2 * k1)
|
|
216
|
+
result2 = dynamics_fn(t + 0.5 * dt, state + 0.5 * dt * k1, params)
|
|
217
|
+
k2 = result2[0] if isinstance(result2, tuple) else result2
|
|
218
|
+
|
|
219
|
+
# Third evaluation: k3 = f(t + dt/2, y_n + dt/2 * k2)
|
|
220
|
+
result3 = dynamics_fn(t + 0.5 * dt, state + 0.5 * dt * k2, params)
|
|
221
|
+
k3 = result3[0] if isinstance(result3, tuple) else result3
|
|
222
|
+
|
|
223
|
+
# Fourth evaluation: k4 = f(t + dt, y_n + dt * k3)
|
|
224
|
+
result4 = dynamics_fn(t + dt, state + dt * k3, params)
|
|
225
|
+
k4 = result4[0] if isinstance(result4, tuple) else result4
|
|
226
|
+
|
|
227
|
+
# RK4 combination: y_{n+1} = y_n + dt/6 * (k1 + 2*k2 + 2*k3 + k4) + noise
|
|
228
|
+
next_state = state + (dt / 6.0) * (k1 + 2.0 * k2 + 2.0 * k3 + k4) + noise_sample
|
|
229
|
+
|
|
230
|
+
return next_state, auxiliaries
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class BoundedSolver(NativeSolver):
|
|
234
|
+
"""Wrapper that enforces hard bounds on solver output via clipping.
|
|
235
|
+
|
|
236
|
+
Wraps any native solver and clips the output state to specified bounds.
|
|
237
|
+
Useful for ensuring states remain in valid ranges (e.g., firing rates ≥ 0).
|
|
238
|
+
|
|
239
|
+
The bounds support flexible broadcasting:
|
|
240
|
+
- Scalar: same bound for all states/nodes
|
|
241
|
+
- [n_states]: different bounds per state variable
|
|
242
|
+
- [n_states, n_nodes]: different bounds per state per node
|
|
243
|
+
|
|
244
|
+
Use -jnp.inf or jnp.inf to disable clipping for specific states/nodes.
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
base_solver: The underlying solver to wrap
|
|
248
|
+
low: Lower bound(s) for state clipping (default: -inf, no clipping)
|
|
249
|
+
high: Upper bound(s) for state clipping (default: +inf, no clipping)
|
|
250
|
+
|
|
251
|
+
Example:
|
|
252
|
+
# Ensure all states stay in [0, 1]
|
|
253
|
+
solver = BoundedSolver(Euler(), low=0.0, high=1.0)
|
|
254
|
+
|
|
255
|
+
# Different bounds per state variable
|
|
256
|
+
solver = BoundedSolver(
|
|
257
|
+
Heun(),
|
|
258
|
+
low=jnp.array([0.0, -5.0]), # state 0: ≥0, state 1: ≥-5
|
|
259
|
+
high=jnp.array([1.0, 5.0]) # state 0: ≤1, state 1: ≤5
|
|
260
|
+
)
|
|
261
|
+
"""
|
|
262
|
+
|
|
263
|
+
def __init__(
|
|
264
|
+
self,
|
|
265
|
+
base_solver: NativeSolver,
|
|
266
|
+
low: float | jnp.ndarray = -jnp.inf,
|
|
267
|
+
high: float | jnp.ndarray = jnp.inf,
|
|
268
|
+
):
|
|
269
|
+
self.base_solver = base_solver
|
|
270
|
+
self.low = low
|
|
271
|
+
self.high = high
|
|
272
|
+
|
|
273
|
+
def step(
|
|
274
|
+
self,
|
|
275
|
+
dynamics_fn: Callable,
|
|
276
|
+
t: float,
|
|
277
|
+
state: jnp.ndarray,
|
|
278
|
+
dt: float,
|
|
279
|
+
params: Bunch,
|
|
280
|
+
noise_sample: jnp.ndarray = 0.0,
|
|
281
|
+
) -> tuple[jnp.ndarray, jnp.ndarray]:
|
|
282
|
+
"""Integration step with state clipping.
|
|
283
|
+
|
|
284
|
+
Delegates to base solver, then clips output state to bounds.
|
|
285
|
+
Input state is not clipped (already clipped from previous iteration).
|
|
286
|
+
|
|
287
|
+
Args:
|
|
288
|
+
dynamics_fn: Dynamics function (t, state, params) -> (derivatives, auxiliaries)
|
|
289
|
+
t: Current time
|
|
290
|
+
state: Current state [n_states, n_nodes]
|
|
291
|
+
dt: Time step
|
|
292
|
+
params: Parameters
|
|
293
|
+
noise_sample: Pre-scaled noise increment
|
|
294
|
+
|
|
295
|
+
Returns:
|
|
296
|
+
Tuple of (next_state, auxiliaries):
|
|
297
|
+
- next_state: Clipped next state [n_states, n_nodes]
|
|
298
|
+
- auxiliaries: Auxiliary variables [n_auxiliaries, n_nodes]
|
|
299
|
+
"""
|
|
300
|
+
# Delegate to wrapped solver
|
|
301
|
+
next_state, auxiliaries = self.base_solver.step(
|
|
302
|
+
dynamics_fn, t, state, dt, params, noise_sample
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
# Clip output to bounds
|
|
306
|
+
next_state = jnp.clip(next_state, self.low, self.high)
|
|
307
|
+
|
|
308
|
+
return next_state, auxiliaries
|