tvboptim 0.2.4__tar.gz → 0.2.6__tar.gz
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-0.2.4/src/tvboptim.egg-info → tvboptim-0.2.6}/PKG-INFO +2 -1
- {tvboptim-0.2.4 → tvboptim-0.2.6}/pyproject.toml +3 -1
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/__init__.py +1 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/execution.py +49 -5
- tvboptim-0.2.6/src/tvboptim/experimental/network_dynamics/analysis/__init__.py +3 -0
- tvboptim-0.2.6/src/tvboptim/experimental/network_dynamics/analysis/lyapunov.py +174 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/core/network.py +7 -7
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/coupling/__init__.py +7 -2
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/coupling/base.py +33 -2
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/result.py +49 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/solve.py +526 -103
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/solvers/native.py +4 -2
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/utils/history.py +8 -3
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/utils/printer.py +1 -1
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/observations/tvb_monitors/bold.py +1 -1
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/observations/tvb_monitors/downsampling.py +26 -3
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/tvbo/prepare_experiment.py +3 -3
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/types/spaces.py +217 -29
- {tvboptim-0.2.4 → tvboptim-0.2.6/src/tvboptim.egg-info}/PKG-INFO +2 -1
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim.egg-info/SOURCES.txt +2 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim.egg-info/requires.txt +1 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/tests/test_execution.py +119 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/LICENSE +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/README.md +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/setup.cfg +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/connectivity/dTOR/data.npz +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/connectivity/dk_average/data.npz +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/functional/dk_average/data.npz +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/loaders.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/plotting/dk_average/aparc+aseg-mni_09c.nii.gz +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/plotting/dk_average/fs_default.txt +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/data/plotting/dk_average/fs_default_freesurfer_idx.csv +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/core/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/core/bunch.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/coupling/linear.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/coupling/subspace.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/coupling/tvb.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/base.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/lorenz.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/coombes_byrne.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/epileptor.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/generic_2d_oscillator.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/jansen_rit.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/kuramoto.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/larter_breakspear.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/linear.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/montbrio_pazo_roxin.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/sup_hopf.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/wilson_cowan.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang_exc_inh.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/external_input/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/external_input/base.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/external_input/data.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/external_input/parametric.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/graph/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/graph/base.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/graph/sparse.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/graph/utils.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/noise/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/noise/base.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/noise/gaussian.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/solvers/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/solvers/base.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/solvers/diffrax.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/utils/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/observations/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/observations/observation.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/observations/tvb_monitors/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/optim/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/optim/callbacks.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/optim/optax.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/tvbo/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/types/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/types/parameter.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/types/stateutils.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/utils/__init__.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/utils/caching.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/utils/utils.py +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim.egg-info/dependency_links.txt +0 -0
- {tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tvboptim
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.6
|
|
4
4
|
Summary: Optimization tools for The Virtual Brain
|
|
5
5
|
Author-email: Marius Pille <marius.pille@bih-charite.de>
|
|
6
6
|
License-Expression: EUPL-1.2
|
|
@@ -43,6 +43,7 @@ Requires-Dist: tvb-library; extra == "docs"
|
|
|
43
43
|
Requires-Dist: jupyter-cache>=1.0.1; extra == "docs"
|
|
44
44
|
Requires-Dist: nibabel; extra == "docs"
|
|
45
45
|
Requires-Dist: nilearn; extra == "docs"
|
|
46
|
+
Requires-Dist: griffe<2.0.0; extra == "docs"
|
|
46
47
|
Dynamic: license-file
|
|
47
48
|
|
|
48
49
|
<p align="center">
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "tvboptim"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.6"
|
|
8
8
|
description = "Optimization tools for The Virtual Brain"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [
|
|
@@ -76,6 +76,7 @@ docs = [
|
|
|
76
76
|
"jupyter-cache>=1.0.1",
|
|
77
77
|
"nibabel",
|
|
78
78
|
"nilearn",
|
|
79
|
+
"griffe<2.0.0"
|
|
79
80
|
]
|
|
80
81
|
|
|
81
82
|
# Standard PEP 621 optional dependencies (used by PyPI)
|
|
@@ -94,6 +95,7 @@ docs = [
|
|
|
94
95
|
"jupyter-cache>=1.0.1",
|
|
95
96
|
"nibabel",
|
|
96
97
|
"nilearn",
|
|
98
|
+
"griffe<2.0.0"
|
|
97
99
|
]
|
|
98
100
|
|
|
99
101
|
[tool.ruff]
|
|
@@ -2,8 +2,11 @@ from abc import ABC
|
|
|
2
2
|
|
|
3
3
|
import jax
|
|
4
4
|
import jax.numpy as jnp
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pandas as pd
|
|
5
7
|
from tqdm import tqdm
|
|
6
8
|
|
|
9
|
+
from tvboptim.types.spaces import _keypath_to_name
|
|
7
10
|
from tvboptim.types.stateutils import combine_state
|
|
8
11
|
|
|
9
12
|
|
|
@@ -16,7 +19,46 @@ class Result(ABC):
|
|
|
16
19
|
Result type to provide unified indexing
|
|
17
20
|
"""
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
space = None
|
|
23
|
+
|
|
24
|
+
def to_dataframe(self) -> pd.DataFrame:
|
|
25
|
+
"""Convert results (and parameters) to a pandas DataFrame.
|
|
26
|
+
|
|
27
|
+
Each row corresponds to one parameter combination. If a space
|
|
28
|
+
reference is stored, parameter columns are included. Result
|
|
29
|
+
column names are derived from the pytree key paths of the
|
|
30
|
+
result structure.
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
pd.DataFrame
|
|
35
|
+
"""
|
|
36
|
+
# Parameter columns
|
|
37
|
+
if self.space is not None:
|
|
38
|
+
df = self.space.to_dataframe()
|
|
39
|
+
else:
|
|
40
|
+
df = pd.DataFrame()
|
|
41
|
+
|
|
42
|
+
# Result columns
|
|
43
|
+
sample = self[0]
|
|
44
|
+
paths_and_leaves, treedef = jax.tree_util.tree_flatten_with_path(sample)
|
|
45
|
+
col_names = [_keypath_to_name(p) for p, _ in paths_and_leaves]
|
|
46
|
+
|
|
47
|
+
# Handle name collisions with param columns
|
|
48
|
+
for i, name in enumerate(col_names):
|
|
49
|
+
if name in df.columns:
|
|
50
|
+
col_names[i] = f"result.{name}"
|
|
51
|
+
|
|
52
|
+
result_data = {name: [] for name in col_names}
|
|
53
|
+
for result in self:
|
|
54
|
+
leaves = jax.tree.leaves(result)
|
|
55
|
+
for name, leaf in zip(col_names, leaves):
|
|
56
|
+
result_data[name].append(np.asarray(leaf))
|
|
57
|
+
|
|
58
|
+
for name, values in result_data.items():
|
|
59
|
+
df[name] = values
|
|
60
|
+
|
|
61
|
+
return df
|
|
20
62
|
|
|
21
63
|
|
|
22
64
|
class SequentialExecution(Execution):
|
|
@@ -95,12 +137,13 @@ class SequentialExecution(Execution):
|
|
|
95
137
|
jax.block_until_ready(self.model(state, *self.args, **self.kwargs))
|
|
96
138
|
)
|
|
97
139
|
# results.append(jax.block_until_ready(self.model(state, *self.args, **self.kwargs)))
|
|
98
|
-
return SequentialResult(results)
|
|
140
|
+
return SequentialResult(results, space=self.statespace)
|
|
99
141
|
|
|
100
142
|
|
|
101
143
|
class SequentialResult(Result):
|
|
102
|
-
def __init__(self, results):
|
|
144
|
+
def __init__(self, results, space=None):
|
|
103
145
|
self.results = results
|
|
146
|
+
self.space = space
|
|
104
147
|
|
|
105
148
|
def __iter__(self):
|
|
106
149
|
return iter(self.results)
|
|
@@ -215,13 +258,14 @@ class ParallelExecution(Execution):
|
|
|
215
258
|
res = jax.block_until_ready(
|
|
216
259
|
jax.pmap(self.map_model, in_axes=0)(self.diff_state)
|
|
217
260
|
)
|
|
218
|
-
return ParallelResult(res, self.space.N, self.n_vmap, self.n_pmap)
|
|
261
|
+
return ParallelResult(res, self.space.N, self.n_vmap, self.n_pmap, space=self.space)
|
|
219
262
|
|
|
220
263
|
|
|
221
264
|
class ParallelResult(Result):
|
|
222
|
-
def __init__(self, results, N, n_vmap, n_pmap):
|
|
265
|
+
def __init__(self, results, N, n_vmap, n_pmap, space=None):
|
|
223
266
|
self.results = results
|
|
224
267
|
self.N = N
|
|
268
|
+
self.space = space
|
|
225
269
|
self.n_vmap = n_vmap
|
|
226
270
|
self.n_pmap = n_pmap
|
|
227
271
|
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import jax.numpy as jnp
|
|
3
|
+
|
|
4
|
+
from .. import prepare
|
|
5
|
+
from ..core.bunch import Bunch
|
|
6
|
+
from ..solvers import Heun
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def lyapunov(network, solver=None, t=1000.0, n=10, d0=1e-9, dt=0.1, t0=0.0):
|
|
10
|
+
"""
|
|
11
|
+
Estimate the maximum Lyapunov exponent for a network.
|
|
12
|
+
|
|
13
|
+
Uses Benettin's rescaling algorithm: two nearby trajectories are
|
|
14
|
+
simulated for n segments of length t ms; their divergence rate is
|
|
15
|
+
averaged to estimate the MLE.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
network : Network
|
|
20
|
+
solver : solver instance, optional (default: Heun())
|
|
21
|
+
t : float — segment duration in ms
|
|
22
|
+
n : int — number of rescaling steps
|
|
23
|
+
d0 : float — initial perturbation magnitude
|
|
24
|
+
dt : float — integration timestep in ms
|
|
25
|
+
t0 : float — simulation start time
|
|
26
|
+
|
|
27
|
+
Returns
|
|
28
|
+
-------
|
|
29
|
+
float — estimated maximum Lyapunov exponent (1/ms)
|
|
30
|
+
|
|
31
|
+
Notes
|
|
32
|
+
-----
|
|
33
|
+
For networks with delayed coupling the delay history is held fixed
|
|
34
|
+
across segments (good approximation when t >> max_delay). Run a
|
|
35
|
+
warmup via network.update_history(result) before calling this
|
|
36
|
+
function to initialise the delay buffer from a settled trajectory.
|
|
37
|
+
"""
|
|
38
|
+
if solver is None:
|
|
39
|
+
solver = Heun()
|
|
40
|
+
solve_fn, config = prepare(network, solver, t0=t0, t1=t0 + t, dt=dt)
|
|
41
|
+
return _lyapunov(solve_fn, config, t=t, n=n, d0=d0)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _lyapunov(solve_fn, config, t, n=10, d0=1e-9):
|
|
45
|
+
"""Low-level MLE estimation using a pre-built solve_fn / config."""
|
|
46
|
+
u_init = config.initial_state.dynamics # [n_states, n_nodes]
|
|
47
|
+
D = u_init.size
|
|
48
|
+
u2_init = u_init + d0 / jnp.sqrt(D)
|
|
49
|
+
|
|
50
|
+
def _solve_from(u):
|
|
51
|
+
new_is = Bunch({**config.initial_state, 'dynamics': u})
|
|
52
|
+
new_cfg = Bunch({**config, 'initial_state': new_is})
|
|
53
|
+
return solve_fn(new_cfg).ys[-1]
|
|
54
|
+
|
|
55
|
+
@jax.jit
|
|
56
|
+
def _scan(u1, u2):
|
|
57
|
+
def _step(carry, _):
|
|
58
|
+
u1, u2, log_sum = carry
|
|
59
|
+
new_u1 = _solve_from(u1)
|
|
60
|
+
new_u2 = _solve_from(u2)
|
|
61
|
+
d = jnp.sqrt(jnp.sum((new_u1 - new_u2) ** 2))
|
|
62
|
+
log_sum = log_sum + jnp.log(d / d0)
|
|
63
|
+
u2 = new_u1 + (new_u2 - new_u1) / (d / d0)
|
|
64
|
+
return (new_u1, u2, log_sum), None
|
|
65
|
+
|
|
66
|
+
init = (u1, u2, jnp.zeros(()))
|
|
67
|
+
(_, _, log_sum), _ = jax.lax.scan(_step, init, None, length=n)
|
|
68
|
+
return log_sum
|
|
69
|
+
|
|
70
|
+
log_sum = _scan(u_init, u2_init)
|
|
71
|
+
return float(log_sum / (n * t))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def lyapunov_spectrum(network, solver=None, t=1000.0, n=10, k=None,
|
|
75
|
+
dt=0.1, t0=0.0, mode="jvp", d0=1e-9):
|
|
76
|
+
"""
|
|
77
|
+
Estimate the Lyapunov spectrum of a network.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
network : Network
|
|
82
|
+
solver : solver instance, optional (default: Heun())
|
|
83
|
+
t : float — segment duration in ms
|
|
84
|
+
n : int — number of rescaling steps
|
|
85
|
+
k : int — number of exponents to compute (default: all D)
|
|
86
|
+
dt : float — integration timestep in ms
|
|
87
|
+
t0 : float — simulation start time
|
|
88
|
+
mode : str — "jvp" (default) uses tangent-space propagation via
|
|
89
|
+
jax.linearize; exact and efficient for differentiable
|
|
90
|
+
systems. "sim" uses finite-difference with d0-scaled
|
|
91
|
+
perturbations; works for non-differentiable systems.
|
|
92
|
+
d0 : float — perturbation magnitude (only used when mode="sim")
|
|
93
|
+
|
|
94
|
+
Returns
|
|
95
|
+
-------
|
|
96
|
+
jnp.ndarray — top k Lyapunov exponents sorted descending (1/ms)
|
|
97
|
+
"""
|
|
98
|
+
if solver is None:
|
|
99
|
+
solver = Heun()
|
|
100
|
+
solve_fn, config = prepare(network, solver, t0=t0, t1=t0 + t, dt=dt)
|
|
101
|
+
if mode == "jvp":
|
|
102
|
+
return _lyapunov_spectrum_jvp(solve_fn, config, t=t, n=n, k=k)
|
|
103
|
+
elif mode == "sim":
|
|
104
|
+
return _lyapunov_spectrum_sim(solve_fn, config, t=t, n=n, k=k, d0=d0)
|
|
105
|
+
else:
|
|
106
|
+
raise ValueError(f"Unknown mode {mode!r}, expected 'jvp' or 'sim'")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _lyapunov_spectrum_sim(solve_fn, config, t, n=10, k=None, d0=1e-9):
|
|
110
|
+
"""Spectrum via finite-difference (k+1 trajectories per step)."""
|
|
111
|
+
u_init = config.initial_state.dynamics
|
|
112
|
+
shape = u_init.shape
|
|
113
|
+
D = u_init.size
|
|
114
|
+
if k is None:
|
|
115
|
+
k = D
|
|
116
|
+
Q_init = jnp.eye(D, k) # [D, k] — first k columns of identity
|
|
117
|
+
|
|
118
|
+
def _solve_flat(u_flat):
|
|
119
|
+
u = u_flat.reshape(shape)
|
|
120
|
+
new_is = Bunch({**config.initial_state, 'dynamics': u})
|
|
121
|
+
new_cfg = Bunch({**config, 'initial_state': new_is})
|
|
122
|
+
return solve_fn(new_cfg).ys[-1].flatten()
|
|
123
|
+
|
|
124
|
+
@jax.jit
|
|
125
|
+
def _scan(u_flat, Q):
|
|
126
|
+
def _step(carry, _):
|
|
127
|
+
u_flat, Q, log_sum = carry
|
|
128
|
+
new_u = _solve_flat(u_flat)
|
|
129
|
+
perturbed = u_flat + d0 * Q.T # [k, D]
|
|
130
|
+
new_perturbed = jax.vmap(_solve_flat)(perturbed) # [k, D]
|
|
131
|
+
delta = (new_perturbed - new_u) / d0 # [k, D]
|
|
132
|
+
Q_new, R = jnp.linalg.qr(delta.T) # [D, k], [k, k]
|
|
133
|
+
log_sum = log_sum + jnp.log(jnp.abs(jnp.diag(R)))
|
|
134
|
+
return (new_u, Q_new, log_sum), None
|
|
135
|
+
|
|
136
|
+
init = (u_flat, Q, jnp.zeros(k))
|
|
137
|
+
(_, _, log_sum), _ = jax.lax.scan(_step, init, None, length=n)
|
|
138
|
+
return log_sum
|
|
139
|
+
|
|
140
|
+
log_sum = _scan(u_init.flatten(), Q_init)
|
|
141
|
+
return jnp.sort(log_sum / (n * t))[::-1]
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _lyapunov_spectrum_jvp(solve_fn, config, t, n=10, k=None):
|
|
145
|
+
"""Spectrum via tangent-space propagation (linearize + vmap)."""
|
|
146
|
+
u_init = config.initial_state.dynamics
|
|
147
|
+
shape = u_init.shape
|
|
148
|
+
D = u_init.size
|
|
149
|
+
if k is None:
|
|
150
|
+
k = D
|
|
151
|
+
Q_init = jnp.eye(D, k) # [D, k]
|
|
152
|
+
|
|
153
|
+
def _solve_flat(u_flat):
|
|
154
|
+
u = u_flat.reshape(shape)
|
|
155
|
+
new_is = Bunch({**config.initial_state, 'dynamics': u})
|
|
156
|
+
new_cfg = Bunch({**config, 'initial_state': new_is})
|
|
157
|
+
return solve_fn(new_cfg).ys[-1].flatten()
|
|
158
|
+
|
|
159
|
+
@jax.jit
|
|
160
|
+
def _scan(u_flat, Q):
|
|
161
|
+
def _step(carry, _):
|
|
162
|
+
u_flat, Q, log_sum = carry
|
|
163
|
+
new_u, jvp_fn = jax.linearize(_solve_flat, u_flat)
|
|
164
|
+
tangents = jax.vmap(jvp_fn)(Q.T) # [k, D]
|
|
165
|
+
Q_new, R = jnp.linalg.qr(tangents.T) # [D, k], [k, k]
|
|
166
|
+
log_sum = log_sum + jnp.log(jnp.abs(jnp.diag(R)))
|
|
167
|
+
return (new_u, Q_new, log_sum), None
|
|
168
|
+
|
|
169
|
+
init = (u_flat, Q, jnp.zeros(k))
|
|
170
|
+
(_, _, log_sum), _ = jax.lax.scan(_step, init, None, length=n)
|
|
171
|
+
return log_sum
|
|
172
|
+
|
|
173
|
+
log_sum = _scan(u_init.flatten(), Q_init)
|
|
174
|
+
return jnp.sort(log_sum / (n * t))[::-1]
|
{tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/core/network.py
RENAMED
|
@@ -94,7 +94,7 @@ class Network:
|
|
|
94
94
|
self.noise = noise
|
|
95
95
|
|
|
96
96
|
# Normalize coupling input to dict format with validation
|
|
97
|
-
self.
|
|
97
|
+
self.coupling = self._normalize_couplings(coupling, dynamics)
|
|
98
98
|
|
|
99
99
|
# Normalize external input to dict format with validation
|
|
100
100
|
|
|
@@ -313,7 +313,7 @@ class Network:
|
|
|
313
313
|
coupling_data_dict = Bunch()
|
|
314
314
|
coupling_state_dict = Bunch()
|
|
315
315
|
|
|
316
|
-
for name, coupling in self.
|
|
316
|
+
for name, coupling in self.coupling.items():
|
|
317
317
|
data, state = coupling.prepare(self, dt, t0, t1)
|
|
318
318
|
coupling_data_dict[name] = data
|
|
319
319
|
coupling_state_dict[name] = state
|
|
@@ -367,12 +367,12 @@ class Network:
|
|
|
367
367
|
coupling_inputs = Bunch()
|
|
368
368
|
|
|
369
369
|
for name, n_dims in self.dynamics.COUPLING_INPUTS.items():
|
|
370
|
-
if name not in self.
|
|
370
|
+
if name not in self.coupling:
|
|
371
371
|
# Missing coupling - use zeros
|
|
372
372
|
coupling_inputs[name] = jnp.zeros((n_dims, self.graph.n_nodes))
|
|
373
373
|
else:
|
|
374
374
|
# Compute coupling
|
|
375
|
-
coupling = self.
|
|
375
|
+
coupling = self.coupling[name]
|
|
376
376
|
data = coupling_data_dict[name]
|
|
377
377
|
state_data = coupling_state_dict[name]
|
|
378
378
|
coupling_inputs[name] = coupling.compute(
|
|
@@ -436,7 +436,7 @@ class Network:
|
|
|
436
436
|
"""
|
|
437
437
|
new_states = Bunch()
|
|
438
438
|
|
|
439
|
-
for name, coupling in self.
|
|
439
|
+
for name, coupling in self.coupling.items():
|
|
440
440
|
new_states[name] = coupling.update_state(
|
|
441
441
|
coupling_data_dict[name],
|
|
442
442
|
coupling_state_dict[name],
|
|
@@ -487,7 +487,7 @@ class Network:
|
|
|
487
487
|
|
|
488
488
|
# Couplings as nested dict
|
|
489
489
|
p.coupling = Bunch()
|
|
490
|
-
for name, coupling in self.
|
|
490
|
+
for name, coupling in self.coupling.items():
|
|
491
491
|
p.coupling[name] = coupling.params
|
|
492
492
|
|
|
493
493
|
# External inputs as nested dict
|
|
@@ -587,7 +587,7 @@ class Network:
|
|
|
587
587
|
"Network(",
|
|
588
588
|
f" dynamics={self.dynamics.__class__.__name__}",
|
|
589
589
|
f" nodes={self.graph.n_nodes}",
|
|
590
|
-
f"
|
|
590
|
+
f" coupling={list(self.coupling.keys())}",
|
|
591
591
|
]
|
|
592
592
|
|
|
593
593
|
if self.externals:
|
{tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/coupling/__init__.py
RENAMED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
"""Coupling functions for network interactions."""
|
|
2
2
|
|
|
3
|
-
from .base import
|
|
3
|
+
from .base import (
|
|
4
|
+
AbstractCoupling,
|
|
5
|
+
BufferStrategy,
|
|
6
|
+
DelayedCoupling,
|
|
7
|
+
InstantaneousCoupling,
|
|
8
|
+
)
|
|
4
9
|
from .linear import (
|
|
5
10
|
DelayedDifferenceCoupling,
|
|
6
11
|
DelayedLinearCoupling,
|
|
@@ -8,8 +13,8 @@ from .linear import (
|
|
|
8
13
|
FastLinearCoupling,
|
|
9
14
|
LinearCoupling,
|
|
10
15
|
)
|
|
11
|
-
from .tvb import DelayedSigmoidalJansenRit, SigmoidalJansenRit
|
|
12
16
|
from .subspace import SubspaceCoupling
|
|
17
|
+
from .tvb import DelayedSigmoidalJansenRit, SigmoidalJansenRit
|
|
13
18
|
|
|
14
19
|
__all__ = [
|
|
15
20
|
"AbstractCoupling",
|
{tvboptim-0.2.4 → tvboptim-0.2.6}/src/tvboptim/experimental/network_dynamics/coupling/base.py
RENAMED
|
@@ -11,11 +11,11 @@ from typing import Literal, Optional, Tuple
|
|
|
11
11
|
|
|
12
12
|
import jax.numpy as jnp
|
|
13
13
|
|
|
14
|
-
BufferStrategy = Literal["roll", "circular", "preallocated"]
|
|
15
|
-
|
|
16
14
|
from ..core.bunch import Bunch
|
|
17
15
|
from ..graph.base import AbstractGraph
|
|
18
16
|
|
|
17
|
+
BufferStrategy = Literal["roll", "circular", "preallocated"]
|
|
18
|
+
|
|
19
19
|
# ============================================================================
|
|
20
20
|
# Helper Functions
|
|
21
21
|
# ============================================================================
|
|
@@ -221,6 +221,37 @@ class AbstractCoupling(ABC):
|
|
|
221
221
|
"""
|
|
222
222
|
pass
|
|
223
223
|
|
|
224
|
+
def precompute(
|
|
225
|
+
self,
|
|
226
|
+
coupling_data: Bunch,
|
|
227
|
+
params: Bunch,
|
|
228
|
+
graph: AbstractGraph,
|
|
229
|
+
) -> Bunch:
|
|
230
|
+
"""Called once per forward pass, inside JIT, before the scan.
|
|
231
|
+
|
|
232
|
+
Use this to compute quantities that depend on optimisable parameters
|
|
233
|
+
(e.g. wLRE, wFFI) combined with static graph data (e.g. W), such that
|
|
234
|
+
gradients flow through the parameters while the computation is only
|
|
235
|
+
performed once per simulation call rather than once per integration step.
|
|
236
|
+
|
|
237
|
+
The returned Bunch is passed to compute() as the coupling_data argument,
|
|
238
|
+
replacing the static coupling_data from prepare(). It may contain both
|
|
239
|
+
the original static fields and new JAX-traced fields.
|
|
240
|
+
|
|
241
|
+
Default implementation is a no-op (returns coupling_data unchanged),
|
|
242
|
+
so existing couplings require no modification.
|
|
243
|
+
|
|
244
|
+
Args:
|
|
245
|
+
coupling_data: Static data returned by prepare().
|
|
246
|
+
params: Current coupling parameters (JAX-traced, gradients flow).
|
|
247
|
+
graph: Network graph (weights, delays etc.)
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
Updated coupling_data Bunch, potentially with additional
|
|
251
|
+
JAX-traced fields.
|
|
252
|
+
"""
|
|
253
|
+
return coupling_data
|
|
254
|
+
|
|
224
255
|
@abstractmethod
|
|
225
256
|
def compute(
|
|
226
257
|
self,
|
|
@@ -59,6 +59,55 @@ class NativeSolution:
|
|
|
59
59
|
return f"NativeSolution(shape={self.ys.shape}, t=[{self.ts[0]:.2f}, {self.ts[-1]:.2f}])"
|
|
60
60
|
|
|
61
61
|
|
|
62
|
+
@tree_util.register_pytree_node_class
|
|
63
|
+
class DiffraxSolution(NativeSolution):
|
|
64
|
+
"""Solution wrapper for Diffrax solvers, compatible with NativeSolution interface.
|
|
65
|
+
|
|
66
|
+
Wraps a diffrax Solution object and exposes the NativeSolution interface
|
|
67
|
+
(.ts, .ys, .dt), while preserving access to the full diffrax solution.
|
|
68
|
+
|
|
69
|
+
Attributes:
|
|
70
|
+
_solution: The underlying diffrax Solution object
|
|
71
|
+
dt: Effective save time step inferred from saveat.ts at prepare time,
|
|
72
|
+
or None if saveat was not ts-based
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def __init__(self, solution, dt=None):
|
|
76
|
+
self._solution = solution
|
|
77
|
+
self.dt = dt
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def ts(self):
|
|
81
|
+
return self._solution.ts
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def ys(self):
|
|
85
|
+
return self._solution.ys
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def stats(self):
|
|
89
|
+
return self._solution.stats
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def result(self):
|
|
93
|
+
return self._solution.result
|
|
94
|
+
|
|
95
|
+
def tree_flatten(self):
|
|
96
|
+
children = (self._solution,)
|
|
97
|
+
aux_data = {"dt": self.dt}
|
|
98
|
+
return children, aux_data
|
|
99
|
+
|
|
100
|
+
@classmethod
|
|
101
|
+
def tree_unflatten(cls, aux_data, children):
|
|
102
|
+
return cls(children[0], dt=aux_data.get("dt"))
|
|
103
|
+
|
|
104
|
+
def __repr__(self):
|
|
105
|
+
return (
|
|
106
|
+
f"DiffraxSolution(shape={self.ys.shape}, "
|
|
107
|
+
f"t=[{self.ts[0]:.2f}, {self.ts[-1]:.2f}], dt={self.dt})"
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
62
111
|
def wrap_native_result(
|
|
63
112
|
trajectory: jnp.ndarray, t0: float, t1: float, dt: float
|
|
64
113
|
) -> NativeSolution:
|