tvboptim 0.2.0__tar.gz → 0.2.1__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.0 → tvboptim-0.2.1}/PKG-INFO +17 -11
- {tvboptim-0.2.0 → tvboptim-0.2.1}/README.md +13 -10
- {tvboptim-0.2.0 → tvboptim-0.2.1}/pyproject.toml +8 -2
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/__init__.py +2 -2
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solve.py +410 -40
- tvboptim-0.2.1/src/tvboptim/tvbo/__init__.py +3 -0
- tvboptim-0.2.1/src/tvboptim/tvbo/prepare_experiment.py +148 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim.egg-info/PKG-INFO +17 -11
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim.egg-info/SOURCES.txt +2 -1
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim.egg-info/requires.txt +3 -0
- tvboptim-0.2.0/src/tvboptim/model.py +0 -111
- {tvboptim-0.2.0 → tvboptim-0.2.1}/LICENSE +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/setup.cfg +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/data/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/data/loaders.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/execution.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/core/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/core/bunch.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/core/network.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/base.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/linear.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/subspace.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/tvb.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/base.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/lorenz.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/coombes_byrne.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/epileptor.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/generic_2d_oscillator.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/jansen_rit.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/kuramoto.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/larter_breakspear.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/linear.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/montbrio_pazo_roxin.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/sup_hopf.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/wilson_cowan.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang_exc_inh.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/external_input/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/external_input/base.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/external_input/data.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/external_input/parametric.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/graph/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/graph/base.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/graph/sparse.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/graph/utils.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/noise/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/noise/base.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/noise/gaussian.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/result.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/base.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/diffrax.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/native.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/utils/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/utils/printer.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/observations/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/observations/observation.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/observations/tvb_monitors/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/observations/tvb_monitors/bold.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/observations/tvb_monitors/downsampling.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/optim/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/optim/callbacks.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/optim/optax.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/types/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/types/parameter.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/types/spaces.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/types/stateutils.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/utils/__init__.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/utils/caching.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/utils/utils.py +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim.egg-info/dependency_links.txt +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim.egg-info/top_level.txt +0 -0
- {tvboptim-0.2.0 → tvboptim-0.2.1}/tests/test_execution.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tvboptim
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
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
|
|
@@ -38,29 +38,35 @@ Requires-Dist: quartodoc>=0.6.0; extra == "dev"
|
|
|
38
38
|
Requires-Dist: jupyter>=1.0.0; extra == "dev"
|
|
39
39
|
Requires-Dist: ipykernel; extra == "dev"
|
|
40
40
|
Provides-Extra: docs
|
|
41
|
+
Requires-Dist: tvb-library; extra == "docs"
|
|
41
42
|
Requires-Dist: jupyter-cache>=1.0.1; extra == "docs"
|
|
43
|
+
Requires-Dist: nibabel; extra == "docs"
|
|
44
|
+
Requires-Dist: nilearn; extra == "docs"
|
|
42
45
|
Dynamic: license-file
|
|
43
46
|
|
|
44
47
|
<p align="center">
|
|
45
|
-
<img src="
|
|
48
|
+
<img src="https://raw.githubusercontent.com/virtual-twin/tvboptim/main/docs/images/tvboptim.png" width="60%">
|
|
46
49
|
</p>
|
|
47
50
|
|
|
48
51
|
# TVB-Optim
|
|
49
52
|
|
|
50
53
|
[](https://github.com/virtual-twin/tvboptim/actions/workflows/python-package.yml)
|
|
51
|
-
[](https://github.com/virtual-twin/tvboptim/actions/workflows/ruff.yml)
|
|
52
55
|
[](https://www.python.org/downloads/)
|
|
56
|
+
[](https://pypi.org/project/tvboptim/)
|
|
57
|
+
<!-- [](https://pypi.org/project/tvboptim/) -->
|
|
58
|
+
<!-- [](https://codecov.io/gh/virtual-twin/tvboptim) -->
|
|
53
59
|
|
|
54
60
|
[JAX](https://jax.readthedocs.io/en/latest/)-based framework for brain network simulation and gradient-based optimization.
|
|
55
61
|
|
|
56
|
-
**[Documentation](https://virtual-twin.github.io/tvboptim)** | **[Get Started](https://virtual-twin.github.io/tvboptim/get_started.html)** | **[
|
|
62
|
+
**[Documentation](https://virtual-twin.github.io/tvboptim)** | **[Get Started](https://virtual-twin.github.io/tvboptim/get_started.html)** | **[Network Dynamics](https://virtual-twin.github.io/tvboptim/network_dynamics/network_dynamics.html)**
|
|
57
63
|
|
|
58
64
|
## Key Features
|
|
59
65
|
|
|
60
|
-
- **Gradient-based optimization
|
|
61
|
-
- **Performance
|
|
62
|
-
- **Flexible & extensible
|
|
63
|
-
- **Intuitive parameter control
|
|
66
|
+
- **Gradient-based optimization:** Fit thousands of parameters using automatic differentiation through the entire simulation pipeline
|
|
67
|
+
- **Performance:** JAX-powered with seamless GPU/TPU scaling
|
|
68
|
+
- **Flexible & extensible:** Build models with [Network Dynamics](https://virtual-twin.github.io/tvboptim/network_dynamics/network_dynamics.html), a composable framework for whole-brain modeling. Existing TVB workflows supported via [TVB-O](https://github.com/virtual-twin/tvbo).
|
|
69
|
+
- **Intuitive parameter control:** Mark values for optimization with [Parameter()](https://virtual-twin.github.io/tvboptim/parameters_and_optimization.html). Define exploration spaces with [Axes](https://virtual-twin.github.io/tvboptim/axes_and_spaces.html) for automatic parallel evaluation via JAX vmap/pmap.
|
|
64
70
|
|
|
65
71
|
## Installation
|
|
66
72
|
|
|
@@ -120,11 +126,11 @@ See the [full example with visualization](https://virtual-twin.github.io/tvbopti
|
|
|
120
126
|
|
|
121
127
|
## How It Works
|
|
122
128
|
|
|
123
|
-
**Functional Composition
|
|
129
|
+
**Functional Composition:** Models are composable functions that can be inspected, modified, and extended. Build complexity incrementally by wrapping functions.
|
|
124
130
|
|
|
125
|
-
**End-to-End Differentiability
|
|
131
|
+
**End-to-End Differentiability:** Compute gradients through the full pipeline: neural dynamics → hemodynamic models → empirical observables.
|
|
126
132
|
|
|
127
|
-
**Interoperability
|
|
133
|
+
**Interoperability:** Convert existing TVB simulations to JAX-compatible functions via the TVB-O framework.
|
|
128
134
|
|
|
129
135
|
## Contributing
|
|
130
136
|
|
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="
|
|
2
|
+
<img src="https://raw.githubusercontent.com/virtual-twin/tvboptim/main/docs/images/tvboptim.png" width="60%">
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
# TVB-Optim
|
|
6
6
|
|
|
7
7
|
[](https://github.com/virtual-twin/tvboptim/actions/workflows/python-package.yml)
|
|
8
|
-
[](https://github.com/virtual-twin/tvboptim/actions/workflows/ruff.yml)
|
|
9
9
|
[](https://www.python.org/downloads/)
|
|
10
|
+
[](https://pypi.org/project/tvboptim/)
|
|
11
|
+
<!-- [](https://pypi.org/project/tvboptim/) -->
|
|
12
|
+
<!-- [](https://codecov.io/gh/virtual-twin/tvboptim) -->
|
|
10
13
|
|
|
11
14
|
[JAX](https://jax.readthedocs.io/en/latest/)-based framework for brain network simulation and gradient-based optimization.
|
|
12
15
|
|
|
13
|
-
**[Documentation](https://virtual-twin.github.io/tvboptim)** | **[Get Started](https://virtual-twin.github.io/tvboptim/get_started.html)** | **[
|
|
16
|
+
**[Documentation](https://virtual-twin.github.io/tvboptim)** | **[Get Started](https://virtual-twin.github.io/tvboptim/get_started.html)** | **[Network Dynamics](https://virtual-twin.github.io/tvboptim/network_dynamics/network_dynamics.html)**
|
|
14
17
|
|
|
15
18
|
## Key Features
|
|
16
19
|
|
|
17
|
-
- **Gradient-based optimization
|
|
18
|
-
- **Performance
|
|
19
|
-
- **Flexible & extensible
|
|
20
|
-
- **Intuitive parameter control
|
|
20
|
+
- **Gradient-based optimization:** Fit thousands of parameters using automatic differentiation through the entire simulation pipeline
|
|
21
|
+
- **Performance:** JAX-powered with seamless GPU/TPU scaling
|
|
22
|
+
- **Flexible & extensible:** Build models with [Network Dynamics](https://virtual-twin.github.io/tvboptim/network_dynamics/network_dynamics.html), a composable framework for whole-brain modeling. Existing TVB workflows supported via [TVB-O](https://github.com/virtual-twin/tvbo).
|
|
23
|
+
- **Intuitive parameter control:** Mark values for optimization with [Parameter()](https://virtual-twin.github.io/tvboptim/parameters_and_optimization.html). Define exploration spaces with [Axes](https://virtual-twin.github.io/tvboptim/axes_and_spaces.html) for automatic parallel evaluation via JAX vmap/pmap.
|
|
21
24
|
|
|
22
25
|
## Installation
|
|
23
26
|
|
|
@@ -77,11 +80,11 @@ See the [full example with visualization](https://virtual-twin.github.io/tvbopti
|
|
|
77
80
|
|
|
78
81
|
## How It Works
|
|
79
82
|
|
|
80
|
-
**Functional Composition
|
|
83
|
+
**Functional Composition:** Models are composable functions that can be inspected, modified, and extended. Build complexity incrementally by wrapping functions.
|
|
81
84
|
|
|
82
|
-
**End-to-End Differentiability
|
|
85
|
+
**End-to-End Differentiability:** Compute gradients through the full pipeline: neural dynamics → hemodynamic models → empirical observables.
|
|
83
86
|
|
|
84
|
-
**Interoperability
|
|
87
|
+
**Interoperability:** Convert existing TVB simulations to JAX-compatible functions via the TVB-O framework.
|
|
85
88
|
|
|
86
89
|
## Contributing
|
|
87
90
|
|
|
@@ -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.1"
|
|
8
8
|
description = "Optimization tools for The Virtual Brain"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [
|
|
@@ -63,7 +63,10 @@ dev = [
|
|
|
63
63
|
]
|
|
64
64
|
docs = [
|
|
65
65
|
# "tvbo",
|
|
66
|
+
"tvb-library",
|
|
66
67
|
"jupyter-cache>=1.0.1",
|
|
68
|
+
"nibabel",
|
|
69
|
+
"nilearn",
|
|
67
70
|
]
|
|
68
71
|
|
|
69
72
|
# Standard PEP 621 optional dependencies (used by PyPI)
|
|
@@ -77,8 +80,11 @@ dev = [
|
|
|
77
80
|
"ipykernel",
|
|
78
81
|
]
|
|
79
82
|
docs = [
|
|
80
|
-
# "tvbo",
|
|
83
|
+
# "tvbo",
|
|
84
|
+
"tvb-library",
|
|
81
85
|
"jupyter-cache>=1.0.1",
|
|
86
|
+
"nibabel",
|
|
87
|
+
"nilearn",
|
|
82
88
|
]
|
|
83
89
|
|
|
84
90
|
[tool.ruff]
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
__version__ = "0.1.0"
|
|
4
4
|
|
|
5
5
|
from . import data
|
|
6
|
-
from .
|
|
6
|
+
from .tvbo.prepare_experiment import HAS_TVBO, prepare
|
|
7
7
|
from .types import parameter, spaces
|
|
8
8
|
|
|
9
|
-
__all__ = ["spaces", "
|
|
9
|
+
__all__ = ["spaces", "prepare", "HAS_TVBO", "parameter", "data"]
|
|
@@ -64,26 +64,211 @@ def prepare(
|
|
|
64
64
|
t1: float = 1.0,
|
|
65
65
|
dt: float = 0.1,
|
|
66
66
|
) -> Tuple[Callable, Bunch]:
|
|
67
|
-
"""Prepare network
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
-
|
|
86
|
-
|
|
67
|
+
"""Prepare network dynamics model for simulation.
|
|
68
|
+
|
|
69
|
+
Transforms a network dynamics model into a JAX-compiled simulation function
|
|
70
|
+
and corresponding configuration object. Supports both native solvers (Euler, Heun)
|
|
71
|
+
and Diffrax solvers with different feature sets and performance characteristics.
|
|
72
|
+
|
|
73
|
+
The preparation process optimizes the model for efficient execution by pre-compiling
|
|
74
|
+
closures, pre-allocating buffers, and structuring data for JAX transformations.
|
|
75
|
+
|
|
76
|
+
Parameters
|
|
77
|
+
----------
|
|
78
|
+
network : Network
|
|
79
|
+
Network dynamics model containing:
|
|
80
|
+
|
|
81
|
+
- **dynamics** : Neural mass/population model (e.g., ReducedWongWang, JansenRit)
|
|
82
|
+
- **couplings** : Inter-region coupling functions (can be delayed or instantaneous)
|
|
83
|
+
- **graph** : Connectivity structure (weights, delays, distances)
|
|
84
|
+
- **noise** : Optional stochastic process (additive/multiplicative)
|
|
85
|
+
- **externals** : Optional external inputs (e.g., stimulation)
|
|
86
|
+
|
|
87
|
+
solver : NativeSolver or DiffraxSolver
|
|
88
|
+
Integration method. Two solver families available:
|
|
89
|
+
|
|
90
|
+
**NativeSolver** (Euler, Heun):
|
|
91
|
+
- Fixed time step integration
|
|
92
|
+
- Supports **all features**: delays, noise, stateful operations
|
|
93
|
+
- Optimized for jax.lax.scan
|
|
94
|
+
- Best for most brain network simulations
|
|
95
|
+
|
|
96
|
+
**DiffraxSolver** (Tsit5, Dopri5, etc.):
|
|
97
|
+
- Adaptive time stepping
|
|
98
|
+
- **Stateless only**: no delayed coupling, no history buffers
|
|
99
|
+
- Useful for stiff ODEs or when adaptive stepping is required
|
|
100
|
+
- Raises ValueError if network has delays
|
|
101
|
+
|
|
102
|
+
t0 : float, optional
|
|
103
|
+
Simulation start time, by default 0.0
|
|
104
|
+
t1 : float, optional
|
|
105
|
+
Simulation end time, by default 1.0
|
|
106
|
+
dt : float, optional
|
|
107
|
+
Integration time step, by default 0.1
|
|
108
|
+
|
|
109
|
+
- For NativeSolver: Fixed step size used throughout simulation
|
|
110
|
+
- For DiffraxSolver: Initial step size (dt0) for adaptive controller
|
|
111
|
+
|
|
112
|
+
Returns
|
|
113
|
+
-------
|
|
114
|
+
solve_function : Callable
|
|
115
|
+
Pure JAX function for running simulation.
|
|
116
|
+
|
|
117
|
+
Signature: ``solve_function(config) -> results``
|
|
118
|
+
|
|
119
|
+
The function is JIT-compiled and supports:
|
|
120
|
+
|
|
121
|
+
- Automatic differentiation (jax.grad, jax.jacobian)
|
|
122
|
+
- Vectorization (jax.vmap)
|
|
123
|
+
- Parallel execution (jax.pmap)
|
|
124
|
+
|
|
125
|
+
config : Bunch
|
|
126
|
+
Configuration PyTree containing:
|
|
127
|
+
|
|
128
|
+
- **dynamics** : Dynamics model parameters
|
|
129
|
+
- **coupling** : Coupling parameters (one entry per coupling)
|
|
130
|
+
- **external** : External input parameters (one entry per input)
|
|
131
|
+
- **noise** : Noise parameters (if stochastic)
|
|
132
|
+
- **graph** : Graph structure (weights, delays)
|
|
133
|
+
- **initial_state** : Initial conditions [n_states, n_nodes]
|
|
134
|
+
- **_internal** : Precomputed data (coupling indices, noise samples, etc.)
|
|
135
|
+
|
|
136
|
+
Raises
|
|
137
|
+
------
|
|
138
|
+
ValueError
|
|
139
|
+
If using DiffraxSolver with delayed coupling (network.max_delay > 0).
|
|
140
|
+
Diffrax solvers cannot maintain history buffers due to internal loop control.
|
|
141
|
+
|
|
142
|
+
Examples
|
|
143
|
+
--------
|
|
144
|
+
**Basic Usage with Native Solver**
|
|
145
|
+
|
|
146
|
+
>>> from tvboptim.experimental.network_dynamics import Network, prepare
|
|
147
|
+
>>> from tvboptim.experimental.network_dynamics.dynamics import ReducedWongWang
|
|
148
|
+
>>> from tvboptim.experimental.network_dynamics.solvers import Euler
|
|
149
|
+
>>> from tvboptim.experimental.network_dynamics.coupling import LinearCoupling
|
|
150
|
+
>>> from tvboptim.experimental.network_dynamics.graph import DenseGraph
|
|
151
|
+
>>> import jax.numpy as jnp
|
|
152
|
+
>>>
|
|
153
|
+
>>> # Create network components
|
|
154
|
+
>>> dynamics = ReducedWongWang()
|
|
155
|
+
>>> coupling = LinearCoupling(incoming_states='S', G=1.0)
|
|
156
|
+
>>> weights = jnp.ones((68, 68)) # 68 brain regions
|
|
157
|
+
>>> graph = DenseGraph(weights)
|
|
158
|
+
>>>
|
|
159
|
+
>>> # Build network
|
|
160
|
+
>>> network = Network(dynamics, coupling, graph)
|
|
161
|
+
>>>
|
|
162
|
+
>>> # Prepare for simulation
|
|
163
|
+
>>> model_fn, config = prepare(network, Euler(), t0=0, t1=100, dt=0.1)
|
|
164
|
+
>>>
|
|
165
|
+
>>> # Run simulation
|
|
166
|
+
>>> results = model_fn(config)
|
|
167
|
+
>>> print(results.data.shape) # [n_timesteps, n_voi, n_nodes]
|
|
168
|
+
|
|
169
|
+
**With Delayed Coupling (Native Solver Only)**
|
|
170
|
+
|
|
171
|
+
>>> from tvboptim.experimental.network_dynamics.coupling import DelayedLinearCoupling
|
|
172
|
+
>>> from tvboptim.experimental.network_dynamics.graph import DenseDelayGraph
|
|
173
|
+
>>>
|
|
174
|
+
>>> # Create graph with heterogeneous delays
|
|
175
|
+
>>> delays = jnp.array([...]) # [n_nodes, n_nodes] delay matrix in ms
|
|
176
|
+
>>> graph = DenseDelayGraph(weights, delays)
|
|
177
|
+
>>>
|
|
178
|
+
>>> # Delayed coupling requires history buffer
|
|
179
|
+
>>> coupling = DelayedLinearCoupling(incoming_states='S', G=2.0)
|
|
180
|
+
>>> network = Network(dynamics, coupling, graph)
|
|
181
|
+
>>>
|
|
182
|
+
>>> # Only NativeSolver supports delays
|
|
183
|
+
>>> model_fn, config = prepare(network, Euler(), t0=0, t1=100, dt=0.1)
|
|
184
|
+
|
|
185
|
+
**With Adaptive Stepping (Diffrax Solver)**
|
|
186
|
+
|
|
187
|
+
>>> from tvboptim.experimental.network_dynamics.solvers import DiffraxSolver
|
|
188
|
+
>>> import diffrax
|
|
189
|
+
>>>
|
|
190
|
+
>>> # Diffrax solver with adaptive time stepping
|
|
191
|
+
>>> solver = DiffraxSolver(
|
|
192
|
+
... diffrax.Tsit5(),
|
|
193
|
+
... saveat=diffrax.SaveAt(ts=jnp.arange(0, 100, 0.1))
|
|
194
|
+
... )
|
|
195
|
+
>>>
|
|
196
|
+
>>> # Network must NOT have delays for Diffrax
|
|
197
|
+
>>> network = Network(dynamics, LinearCoupling(...), graph)
|
|
198
|
+
>>> model_fn, config = prepare(network, solver, t0=0, t1=100, dt=0.1)
|
|
199
|
+
>>> solution = model_fn(config) # Returns diffrax.Solution object
|
|
200
|
+
|
|
201
|
+
**With Stochastic Dynamics**
|
|
202
|
+
|
|
203
|
+
>>> from tvboptim.experimental.network_dynamics.noise import AdditiveNoise
|
|
204
|
+
>>> import jax
|
|
205
|
+
>>>
|
|
206
|
+
>>> # Add noise to network
|
|
207
|
+
>>> noise = AdditiveNoise(state_indices=[0], sigma=0.01, key=jax.random.PRNGKey(0))
|
|
208
|
+
>>> network = Network(dynamics, coupling, graph, noise=noise)
|
|
209
|
+
>>>
|
|
210
|
+
>>> # Prepare with noise (pre-generates noise samples)
|
|
211
|
+
>>> model_fn, config = prepare(network, Euler(), t0=0, t1=100, dt=0.1)
|
|
212
|
+
|
|
213
|
+
**Modifying Parameters**
|
|
214
|
+
|
|
215
|
+
>>> # Config is a PyTree - parameters can be modified
|
|
216
|
+
>>> import copy
|
|
217
|
+
>>> config_modified = copy.deepcopy(config)
|
|
218
|
+
>>> config_modified.dynamics.G = 2.5 # Change global coupling
|
|
219
|
+
>>> config_modified.coupling.default.G = 1.5 # Change coupling strength
|
|
220
|
+
>>>
|
|
221
|
+
>>> # Run with modified parameters
|
|
222
|
+
>>> results_modified = model_fn(config_modified)
|
|
223
|
+
|
|
224
|
+
Notes
|
|
225
|
+
-----
|
|
226
|
+
**Preparation Steps (NativeSolver):**
|
|
227
|
+
|
|
228
|
+
1. Prepare all couplings (create history buffers for delays if needed)
|
|
229
|
+
2. Build config structure with flattened parameters and graph
|
|
230
|
+
3. Pre-generate noise samples if stochastic (one sample per timestep)
|
|
231
|
+
4. Pre-compile coupling computation closures (avoid dict lookups in scan)
|
|
232
|
+
5. Pre-compile state update closures (for history buffer management)
|
|
233
|
+
6. Return pure function optimized for jax.lax.scan
|
|
234
|
+
|
|
235
|
+
**Preparation Steps (DiffraxSolver):**
|
|
236
|
+
|
|
237
|
+
1. Validate network has no delays (raises ValueError if found)
|
|
238
|
+
2. Prepare stateless coupling/external input data
|
|
239
|
+
3. Build config with parameters and precomputed data
|
|
240
|
+
4. Create Diffrax vector field and control term (for SDEs)
|
|
241
|
+
5. Return pure function wrapping diffrax.diffeqsolve
|
|
242
|
+
|
|
243
|
+
**Solver Selection Guidelines:**
|
|
244
|
+
|
|
245
|
+
Use **NativeSolver** (Euler, Heun) when:
|
|
246
|
+
|
|
247
|
+
- Network has delayed coupling
|
|
248
|
+
- Need full control over integration loop
|
|
249
|
+
- Want optimal performance with jax.lax.scan
|
|
250
|
+
- Standard brain network simulation
|
|
251
|
+
|
|
252
|
+
Use **DiffraxSolver** when:
|
|
253
|
+
|
|
254
|
+
- Network has no delays (stateless)
|
|
255
|
+
- Need adaptive time stepping for stiff systems
|
|
256
|
+
- Want access to advanced Diffrax features
|
|
257
|
+
- Require error control and step size adaptation
|
|
258
|
+
|
|
259
|
+
**Performance Notes:**
|
|
260
|
+
|
|
261
|
+
- Native solvers use jax.lax.scan for optimal compile-time optimization
|
|
262
|
+
- Pre-compilation of closures eliminates runtime overhead
|
|
263
|
+
- History buffers for delays use efficient circular indexing
|
|
264
|
+
- Noise samples are pre-generated to avoid per-step RNG calls
|
|
265
|
+
|
|
266
|
+
See Also
|
|
267
|
+
--------
|
|
268
|
+
solve : High-level interface that calls prepare() and executes immediately
|
|
269
|
+
Network : Network dynamics model container
|
|
270
|
+
NativeSolver : Fixed-step integration methods (Euler, Heun)
|
|
271
|
+
DiffraxSolver : Adaptive-step integration using Diffrax library
|
|
87
272
|
"""
|
|
88
273
|
# Prepare all couplings (creates history buffers, computes indices, etc.)
|
|
89
274
|
coupling_data_dict, coupling_state_dict_init = network.prepare(dt)
|
|
@@ -415,26 +600,211 @@ def prepare(
|
|
|
415
600
|
t1: float = 1.0,
|
|
416
601
|
dt: float = 0.1,
|
|
417
602
|
) -> Tuple[Callable, Bunch]:
|
|
418
|
-
"""Prepare network
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
-
|
|
434
|
-
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
603
|
+
"""Prepare network dynamics model for simulation.
|
|
604
|
+
|
|
605
|
+
Transforms a network dynamics model into a JAX-compiled simulation function
|
|
606
|
+
and corresponding configuration object. Supports both native solvers (Euler, Heun)
|
|
607
|
+
and Diffrax solvers with different feature sets and performance characteristics.
|
|
608
|
+
|
|
609
|
+
The preparation process optimizes the model for efficient execution by pre-compiling
|
|
610
|
+
closures, pre-allocating buffers, and structuring data for JAX transformations.
|
|
611
|
+
|
|
612
|
+
Parameters
|
|
613
|
+
----------
|
|
614
|
+
network : Network
|
|
615
|
+
Network dynamics model containing:
|
|
616
|
+
|
|
617
|
+
- **dynamics** : Neural mass/population model (e.g., ReducedWongWang, JansenRit)
|
|
618
|
+
- **couplings** : Inter-region coupling functions (can be delayed or instantaneous)
|
|
619
|
+
- **graph** : Connectivity structure (weights, delays, distances)
|
|
620
|
+
- **noise** : Optional stochastic process (additive/multiplicative)
|
|
621
|
+
- **externals** : Optional external inputs (e.g., stimulation)
|
|
622
|
+
|
|
623
|
+
solver : NativeSolver or DiffraxSolver
|
|
624
|
+
Integration method. Two solver families available:
|
|
625
|
+
|
|
626
|
+
**NativeSolver** (Euler, Heun):
|
|
627
|
+
- Fixed time step integration
|
|
628
|
+
- Supports **all features**: delays, noise, stateful operations
|
|
629
|
+
- Optimized for jax.lax.scan
|
|
630
|
+
- Best for most brain network simulations
|
|
631
|
+
|
|
632
|
+
**DiffraxSolver** (Tsit5, Dopri5, etc.):
|
|
633
|
+
- Adaptive time stepping
|
|
634
|
+
- **Stateless only**: no delayed coupling, no history buffers
|
|
635
|
+
- Useful for stiff ODEs or when adaptive stepping is required
|
|
636
|
+
- Raises ValueError if network has delays
|
|
637
|
+
|
|
638
|
+
t0 : float, optional
|
|
639
|
+
Simulation start time, by default 0.0
|
|
640
|
+
t1 : float, optional
|
|
641
|
+
Simulation end time, by default 1.0
|
|
642
|
+
dt : float, optional
|
|
643
|
+
Integration time step, by default 0.1
|
|
644
|
+
|
|
645
|
+
- For NativeSolver: Fixed step size used throughout simulation
|
|
646
|
+
- For DiffraxSolver: Initial step size (dt0) for adaptive controller
|
|
647
|
+
|
|
648
|
+
Returns
|
|
649
|
+
-------
|
|
650
|
+
solve_function : Callable
|
|
651
|
+
Pure JAX function for running simulation.
|
|
652
|
+
|
|
653
|
+
Signature: ``solve_function(config) -> results``
|
|
654
|
+
|
|
655
|
+
The function is JIT-compiled and supports:
|
|
656
|
+
|
|
657
|
+
- Automatic differentiation (jax.grad, jax.jacobian)
|
|
658
|
+
- Vectorization (jax.vmap)
|
|
659
|
+
- Parallel execution (jax.pmap)
|
|
660
|
+
|
|
661
|
+
config : Bunch
|
|
662
|
+
Configuration PyTree containing:
|
|
663
|
+
|
|
664
|
+
- **dynamics** : Dynamics model parameters
|
|
665
|
+
- **coupling** : Coupling parameters (one entry per coupling)
|
|
666
|
+
- **external** : External input parameters (one entry per input)
|
|
667
|
+
- **noise** : Noise parameters (if stochastic)
|
|
668
|
+
- **graph** : Graph structure (weights, delays)
|
|
669
|
+
- **initial_state** : Initial conditions [n_states, n_nodes]
|
|
670
|
+
- **_internal** : Precomputed data (coupling indices, noise samples, etc.)
|
|
671
|
+
|
|
672
|
+
Raises
|
|
673
|
+
------
|
|
674
|
+
ValueError
|
|
675
|
+
If using DiffraxSolver with delayed coupling (network.max_delay > 0).
|
|
676
|
+
Diffrax solvers cannot maintain history buffers due to internal loop control.
|
|
677
|
+
|
|
678
|
+
Examples
|
|
679
|
+
--------
|
|
680
|
+
**Basic Usage with Native Solver**
|
|
681
|
+
|
|
682
|
+
>>> from tvboptim.experimental.network_dynamics import Network, prepare
|
|
683
|
+
>>> from tvboptim.experimental.network_dynamics.dynamics import ReducedWongWang
|
|
684
|
+
>>> from tvboptim.experimental.network_dynamics.solvers import Euler
|
|
685
|
+
>>> from tvboptim.experimental.network_dynamics.coupling import LinearCoupling
|
|
686
|
+
>>> from tvboptim.experimental.network_dynamics.graph import DenseGraph
|
|
687
|
+
>>> import jax.numpy as jnp
|
|
688
|
+
>>>
|
|
689
|
+
>>> # Create network components
|
|
690
|
+
>>> dynamics = ReducedWongWang()
|
|
691
|
+
>>> coupling = LinearCoupling(incoming_states='S', G=1.0)
|
|
692
|
+
>>> weights = jnp.ones((68, 68)) # 68 brain regions
|
|
693
|
+
>>> graph = DenseGraph(weights)
|
|
694
|
+
>>>
|
|
695
|
+
>>> # Build network
|
|
696
|
+
>>> network = Network(dynamics, coupling, graph)
|
|
697
|
+
>>>
|
|
698
|
+
>>> # Prepare for simulation
|
|
699
|
+
>>> model_fn, config = prepare(network, Euler(), t0=0, t1=100, dt=0.1)
|
|
700
|
+
>>>
|
|
701
|
+
>>> # Run simulation
|
|
702
|
+
>>> results = model_fn(config)
|
|
703
|
+
>>> print(results.data.shape) # [n_timesteps, n_voi, n_nodes]
|
|
704
|
+
|
|
705
|
+
**With Delayed Coupling (Native Solver Only)**
|
|
706
|
+
|
|
707
|
+
>>> from tvboptim.experimental.network_dynamics.coupling import DelayedLinearCoupling
|
|
708
|
+
>>> from tvboptim.experimental.network_dynamics.graph import DenseDelayGraph
|
|
709
|
+
>>>
|
|
710
|
+
>>> # Create graph with heterogeneous delays
|
|
711
|
+
>>> delays = jnp.array([...]) # [n_nodes, n_nodes] delay matrix in ms
|
|
712
|
+
>>> graph = DenseDelayGraph(weights, delays)
|
|
713
|
+
>>>
|
|
714
|
+
>>> # Delayed coupling requires history buffer
|
|
715
|
+
>>> coupling = DelayedLinearCoupling(incoming_states='S', G=2.0)
|
|
716
|
+
>>> network = Network(dynamics, coupling, graph)
|
|
717
|
+
>>>
|
|
718
|
+
>>> # Only NativeSolver supports delays
|
|
719
|
+
>>> model_fn, config = prepare(network, Euler(), t0=0, t1=100, dt=0.1)
|
|
720
|
+
|
|
721
|
+
**With Adaptive Stepping (Diffrax Solver)**
|
|
722
|
+
|
|
723
|
+
>>> from tvboptim.experimental.network_dynamics.solvers import DiffraxSolver
|
|
724
|
+
>>> import diffrax
|
|
725
|
+
>>>
|
|
726
|
+
>>> # Diffrax solver with adaptive time stepping
|
|
727
|
+
>>> solver = DiffraxSolver(
|
|
728
|
+
... diffrax.Tsit5(),
|
|
729
|
+
... saveat=diffrax.SaveAt(ts=jnp.arange(0, 100, 0.1))
|
|
730
|
+
... )
|
|
731
|
+
>>>
|
|
732
|
+
>>> # Network must NOT have delays for Diffrax
|
|
733
|
+
>>> network = Network(dynamics, LinearCoupling(...), graph)
|
|
734
|
+
>>> model_fn, config = prepare(network, solver, t0=0, t1=100, dt=0.1)
|
|
735
|
+
>>> solution = model_fn(config) # Returns diffrax.Solution object
|
|
736
|
+
|
|
737
|
+
**With Stochastic Dynamics**
|
|
738
|
+
|
|
739
|
+
>>> from tvboptim.experimental.network_dynamics.noise import AdditiveNoise
|
|
740
|
+
>>> import jax
|
|
741
|
+
>>>
|
|
742
|
+
>>> # Add noise to network
|
|
743
|
+
>>> noise = AdditiveNoise(state_indices=[0], sigma=0.01, key=jax.random.PRNGKey(0))
|
|
744
|
+
>>> network = Network(dynamics, coupling, graph, noise=noise)
|
|
745
|
+
>>>
|
|
746
|
+
>>> # Prepare with noise (pre-generates noise samples)
|
|
747
|
+
>>> model_fn, config = prepare(network, Euler(), t0=0, t1=100, dt=0.1)
|
|
748
|
+
|
|
749
|
+
**Modifying Parameters**
|
|
750
|
+
|
|
751
|
+
>>> # Config is a PyTree - parameters can be modified
|
|
752
|
+
>>> import copy
|
|
753
|
+
>>> config_modified = copy.deepcopy(config)
|
|
754
|
+
>>> config_modified.dynamics.G = 2.5 # Change global coupling
|
|
755
|
+
>>> config_modified.coupling.default.G = 1.5 # Change coupling strength
|
|
756
|
+
>>>
|
|
757
|
+
>>> # Run with modified parameters
|
|
758
|
+
>>> results_modified = model_fn(config_modified)
|
|
759
|
+
|
|
760
|
+
Notes
|
|
761
|
+
-----
|
|
762
|
+
**Preparation Steps (NativeSolver):**
|
|
763
|
+
|
|
764
|
+
1. Prepare all couplings (create history buffers for delays if needed)
|
|
765
|
+
2. Build config structure with flattened parameters and graph
|
|
766
|
+
3. Pre-generate noise samples if stochastic (one sample per timestep)
|
|
767
|
+
4. Pre-compile coupling computation closures (avoid dict lookups in scan)
|
|
768
|
+
5. Pre-compile state update closures (for history buffer management)
|
|
769
|
+
6. Return pure function optimized for jax.lax.scan
|
|
770
|
+
|
|
771
|
+
**Preparation Steps (DiffraxSolver):**
|
|
772
|
+
|
|
773
|
+
1. Validate network has no delays (raises ValueError if found)
|
|
774
|
+
2. Prepare stateless coupling/external input data
|
|
775
|
+
3. Build config with parameters and precomputed data
|
|
776
|
+
4. Create Diffrax vector field and control term (for SDEs)
|
|
777
|
+
5. Return pure function wrapping diffrax.diffeqsolve
|
|
778
|
+
|
|
779
|
+
**Solver Selection Guidelines:**
|
|
780
|
+
|
|
781
|
+
Use **NativeSolver** (Euler, Heun) when:
|
|
782
|
+
|
|
783
|
+
- Network has delayed coupling
|
|
784
|
+
- Need full control over integration loop
|
|
785
|
+
- Want optimal performance with jax.lax.scan
|
|
786
|
+
- Standard brain network simulation
|
|
787
|
+
|
|
788
|
+
Use **DiffraxSolver** when:
|
|
789
|
+
|
|
790
|
+
- Network has no delays (stateless)
|
|
791
|
+
- Need adaptive time stepping for stiff systems
|
|
792
|
+
- Want access to advanced Diffrax features
|
|
793
|
+
- Require error control and step size adaptation
|
|
794
|
+
|
|
795
|
+
**Performance Notes:**
|
|
796
|
+
|
|
797
|
+
- Native solvers use jax.lax.scan for optimal compile-time optimization
|
|
798
|
+
- Pre-compilation of closures eliminates runtime overhead
|
|
799
|
+
- History buffers for delays use efficient circular indexing
|
|
800
|
+
- Noise samples are pre-generated to avoid per-step RNG calls
|
|
801
|
+
|
|
802
|
+
See Also
|
|
803
|
+
--------
|
|
804
|
+
solve : High-level interface that calls prepare() and executes immediately
|
|
805
|
+
Network : Network dynamics model container
|
|
806
|
+
NativeSolver : Fixed-step integration methods (Euler, Heun)
|
|
807
|
+
DiffraxSolver : Adaptive-step integration using Diffrax library
|
|
438
808
|
"""
|
|
439
809
|
# =========================================================================
|
|
440
810
|
# VALIDATION: Check for unsupported features
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""TVBO experiment preparation with unified dispatch interface.
|
|
2
|
+
|
|
3
|
+
This module extends the prepare() multimethod with support for TVBO SimulationExperiment
|
|
4
|
+
objects. It uses conditional dispatch registration to make TVBO an optional dependency.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Callable, Tuple
|
|
8
|
+
|
|
9
|
+
from plum import dispatch
|
|
10
|
+
|
|
11
|
+
# Import the prepare multimethod from network_dynamics to extend it
|
|
12
|
+
from tvboptim.experimental.network_dynamics.solve import prepare
|
|
13
|
+
|
|
14
|
+
__all__ = ["prepare", "HAS_TVBO"]
|
|
15
|
+
|
|
16
|
+
# ============================================================================
|
|
17
|
+
# OPTIONAL TVBO DISPATCH
|
|
18
|
+
# ============================================================================
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
import jax
|
|
22
|
+
import jax.numpy as jnp
|
|
23
|
+
from tvbo.export.experiment import SimulationExperiment
|
|
24
|
+
|
|
25
|
+
@dispatch
|
|
26
|
+
def prepare(
|
|
27
|
+
experiment: SimulationExperiment,
|
|
28
|
+
t0: float = 0.0,
|
|
29
|
+
t1: float = 100.0,
|
|
30
|
+
dt: float = 0.1,
|
|
31
|
+
enable_x64: bool = True,
|
|
32
|
+
replace_temporal_averaging: bool = False,
|
|
33
|
+
return_new_ics: bool = False,
|
|
34
|
+
scalar_pre: bool = False,
|
|
35
|
+
bold_fft_convolve: bool = True,
|
|
36
|
+
small_dt: bool = False,
|
|
37
|
+
**kwargs,
|
|
38
|
+
) -> Tuple[Callable, Any]:
|
|
39
|
+
"""Convert TVBO SimulationExperiment to JAX-compatible model function and state.
|
|
40
|
+
|
|
41
|
+
This function transforms a TVBO simulation experiment into a JAX-compiled
|
|
42
|
+
model function and corresponding state object for efficient brain simulation.
|
|
43
|
+
The resulting model supports automatic differentiation and parallel execution.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
experiment : tvbo.export.experiment.SimulationExperiment
|
|
48
|
+
TVBO SimulationExperiment containing model, connectivity, coupling,
|
|
49
|
+
integration, and monitor specifications.
|
|
50
|
+
t0 : float, optional
|
|
51
|
+
Start time for simulation. Default is 0.0.
|
|
52
|
+
Note: Currently not used, reserved for future integration.
|
|
53
|
+
t1 : float, optional
|
|
54
|
+
End time for simulation. Default is 100.0.
|
|
55
|
+
Note: Currently not used, reserved for future integration.
|
|
56
|
+
dt : float, optional
|
|
57
|
+
Time step for simulation. Default is 0.1.
|
|
58
|
+
Note: Currently not used, reserved for future integration.
|
|
59
|
+
enable_x64 : bool, optional
|
|
60
|
+
If True, use float64 precision; otherwise float32. Transforms all arrays
|
|
61
|
+
in state to correct precision and sets JAX config 'jax_enable_x64'.
|
|
62
|
+
Default is True.
|
|
63
|
+
replace_temporal_averaging : bool, optional
|
|
64
|
+
If False, BOLD uses TemporalAverage monitor as TVB does. If True,
|
|
65
|
+
uses faster SubSample monitor with similar results. Default is False.
|
|
66
|
+
return_new_ics : bool, optional
|
|
67
|
+
If True, model returns updated initial conditions TimeSeries along
|
|
68
|
+
with simulation output for continuing simulations. Changes output
|
|
69
|
+
from result to [result, initial_conditions]. Default is False.
|
|
70
|
+
scalar_pre : bool, optional
|
|
71
|
+
If True, applies performance optimization replacing dot product with
|
|
72
|
+
matmul in coupling term. Only works with scalar-only pre expressions,
|
|
73
|
+
no delays, and when pre expression has single x_j occurrence.
|
|
74
|
+
Default is False.
|
|
75
|
+
bold_fft_convolve : bool, optional
|
|
76
|
+
If True, BOLD monitor uses FFT convolution instead of dot product.
|
|
77
|
+
Faster for most cases, time doesn't scale with BOLD period. Dot
|
|
78
|
+
product can be faster for large period values. Default is True.
|
|
79
|
+
small_dt : bool, optional
|
|
80
|
+
Uses full history storage for faster simulations at small dt. Can
|
|
81
|
+
cause memory explosion under jax.grad transformation. Default is False.
|
|
82
|
+
**kwargs : dict
|
|
83
|
+
Additional keyword arguments passed to experiment.execute().
|
|
84
|
+
|
|
85
|
+
Returns
|
|
86
|
+
-------
|
|
87
|
+
tuple[Callable, Any]
|
|
88
|
+
A tuple containing (model_function, state) where:
|
|
89
|
+
|
|
90
|
+
- model_function : Callable that takes state and returns simulation results
|
|
91
|
+
- state : JAX PyTree containing all simulation parameters and initial conditions
|
|
92
|
+
|
|
93
|
+
Examples
|
|
94
|
+
--------
|
|
95
|
+
>>> from tvbo.export.experiment import SimulationExperiment
|
|
96
|
+
>>> from tvboptim import prepare
|
|
97
|
+
>>>
|
|
98
|
+
>>> # Create TVBO experiment
|
|
99
|
+
>>> experiment = SimulationExperiment(...)
|
|
100
|
+
>>>
|
|
101
|
+
>>> # Convert to JAX
|
|
102
|
+
>>> model, state = prepare(experiment, enable_x64=True, scalar_pre=True)
|
|
103
|
+
>>>
|
|
104
|
+
>>> # Run simulation
|
|
105
|
+
>>> result = model(state)
|
|
106
|
+
>>> raw_data, bold_data = result
|
|
107
|
+
>>>
|
|
108
|
+
>>> # Use with JAX transformations
|
|
109
|
+
>>> grad_fn = jax.grad(lambda s: model(s)[0].data.sum())
|
|
110
|
+
>>> gradients = grad_fn(state)
|
|
111
|
+
|
|
112
|
+
Notes
|
|
113
|
+
-----
|
|
114
|
+
The returned model function is JAX-compiled and supports:
|
|
115
|
+
|
|
116
|
+
- Automatic differentiation with jax.grad, jax.jacobian
|
|
117
|
+
- Parallel execution with jax.vmap, jax.pmap
|
|
118
|
+
- Just-in-time compilation for optimal performance
|
|
119
|
+
- Integration with JAX ecosystem (optax, equinox, etc.)
|
|
120
|
+
|
|
121
|
+
The state object is a JAX PyTree that can be used with all JAX transformations
|
|
122
|
+
and contains Parameter objects for optimization workflows.
|
|
123
|
+
"""
|
|
124
|
+
state = experiment.collect_state()
|
|
125
|
+
jax.config.update("jax_enable_x64", enable_x64)
|
|
126
|
+
|
|
127
|
+
if enable_x64:
|
|
128
|
+
state = state.convert_dtype(target_dtype=jnp.float64)
|
|
129
|
+
else:
|
|
130
|
+
state = state.convert_dtype(target_dtype=jnp.float32)
|
|
131
|
+
|
|
132
|
+
_module = experiment.execute(
|
|
133
|
+
format="jax",
|
|
134
|
+
replace_temporal_averaging=replace_temporal_averaging,
|
|
135
|
+
return_new_ics=return_new_ics,
|
|
136
|
+
scalar_pre=scalar_pre,
|
|
137
|
+
bold_fft_convolve=bold_fft_convolve,
|
|
138
|
+
small_dt=small_dt,
|
|
139
|
+
**kwargs,
|
|
140
|
+
)
|
|
141
|
+
simulator = _module["kernel"]
|
|
142
|
+
|
|
143
|
+
return simulator, state
|
|
144
|
+
|
|
145
|
+
HAS_TVBO = True
|
|
146
|
+
|
|
147
|
+
except ImportError:
|
|
148
|
+
HAS_TVBO = False
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tvboptim
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
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
|
|
@@ -38,29 +38,35 @@ Requires-Dist: quartodoc>=0.6.0; extra == "dev"
|
|
|
38
38
|
Requires-Dist: jupyter>=1.0.0; extra == "dev"
|
|
39
39
|
Requires-Dist: ipykernel; extra == "dev"
|
|
40
40
|
Provides-Extra: docs
|
|
41
|
+
Requires-Dist: tvb-library; extra == "docs"
|
|
41
42
|
Requires-Dist: jupyter-cache>=1.0.1; extra == "docs"
|
|
43
|
+
Requires-Dist: nibabel; extra == "docs"
|
|
44
|
+
Requires-Dist: nilearn; extra == "docs"
|
|
42
45
|
Dynamic: license-file
|
|
43
46
|
|
|
44
47
|
<p align="center">
|
|
45
|
-
<img src="
|
|
48
|
+
<img src="https://raw.githubusercontent.com/virtual-twin/tvboptim/main/docs/images/tvboptim.png" width="60%">
|
|
46
49
|
</p>
|
|
47
50
|
|
|
48
51
|
# TVB-Optim
|
|
49
52
|
|
|
50
53
|
[](https://github.com/virtual-twin/tvboptim/actions/workflows/python-package.yml)
|
|
51
|
-
[](https://github.com/virtual-twin/tvboptim/actions/workflows/ruff.yml)
|
|
52
55
|
[](https://www.python.org/downloads/)
|
|
56
|
+
[](https://pypi.org/project/tvboptim/)
|
|
57
|
+
<!-- [](https://pypi.org/project/tvboptim/) -->
|
|
58
|
+
<!-- [](https://codecov.io/gh/virtual-twin/tvboptim) -->
|
|
53
59
|
|
|
54
60
|
[JAX](https://jax.readthedocs.io/en/latest/)-based framework for brain network simulation and gradient-based optimization.
|
|
55
61
|
|
|
56
|
-
**[Documentation](https://virtual-twin.github.io/tvboptim)** | **[Get Started](https://virtual-twin.github.io/tvboptim/get_started.html)** | **[
|
|
62
|
+
**[Documentation](https://virtual-twin.github.io/tvboptim)** | **[Get Started](https://virtual-twin.github.io/tvboptim/get_started.html)** | **[Network Dynamics](https://virtual-twin.github.io/tvboptim/network_dynamics/network_dynamics.html)**
|
|
57
63
|
|
|
58
64
|
## Key Features
|
|
59
65
|
|
|
60
|
-
- **Gradient-based optimization
|
|
61
|
-
- **Performance
|
|
62
|
-
- **Flexible & extensible
|
|
63
|
-
- **Intuitive parameter control
|
|
66
|
+
- **Gradient-based optimization:** Fit thousands of parameters using automatic differentiation through the entire simulation pipeline
|
|
67
|
+
- **Performance:** JAX-powered with seamless GPU/TPU scaling
|
|
68
|
+
- **Flexible & extensible:** Build models with [Network Dynamics](https://virtual-twin.github.io/tvboptim/network_dynamics/network_dynamics.html), a composable framework for whole-brain modeling. Existing TVB workflows supported via [TVB-O](https://github.com/virtual-twin/tvbo).
|
|
69
|
+
- **Intuitive parameter control:** Mark values for optimization with [Parameter()](https://virtual-twin.github.io/tvboptim/parameters_and_optimization.html). Define exploration spaces with [Axes](https://virtual-twin.github.io/tvboptim/axes_and_spaces.html) for automatic parallel evaluation via JAX vmap/pmap.
|
|
64
70
|
|
|
65
71
|
## Installation
|
|
66
72
|
|
|
@@ -120,11 +126,11 @@ See the [full example with visualization](https://virtual-twin.github.io/tvbopti
|
|
|
120
126
|
|
|
121
127
|
## How It Works
|
|
122
128
|
|
|
123
|
-
**Functional Composition
|
|
129
|
+
**Functional Composition:** Models are composable functions that can be inspected, modified, and extended. Build complexity incrementally by wrapping functions.
|
|
124
130
|
|
|
125
|
-
**End-to-End Differentiability
|
|
131
|
+
**End-to-End Differentiability:** Compute gradients through the full pipeline: neural dynamics → hemodynamic models → empirical observables.
|
|
126
132
|
|
|
127
|
-
**Interoperability
|
|
133
|
+
**Interoperability:** Convert existing TVB simulations to JAX-compatible functions via the TVB-O framework.
|
|
128
134
|
|
|
129
135
|
## Contributing
|
|
130
136
|
|
|
@@ -3,7 +3,6 @@ README.md
|
|
|
3
3
|
pyproject.toml
|
|
4
4
|
src/tvboptim/__init__.py
|
|
5
5
|
src/tvboptim/execution.py
|
|
6
|
-
src/tvboptim/model.py
|
|
7
6
|
src/tvboptim.egg-info/PKG-INFO
|
|
8
7
|
src/tvboptim.egg-info/SOURCES.txt
|
|
9
8
|
src/tvboptim.egg-info/dependency_links.txt
|
|
@@ -64,6 +63,8 @@ src/tvboptim/observations/tvb_monitors/downsampling.py
|
|
|
64
63
|
src/tvboptim/optim/__init__.py
|
|
65
64
|
src/tvboptim/optim/callbacks.py
|
|
66
65
|
src/tvboptim/optim/optax.py
|
|
66
|
+
src/tvboptim/tvbo/__init__.py
|
|
67
|
+
src/tvboptim/tvbo/prepare_experiment.py
|
|
67
68
|
src/tvboptim/types/__init__.py
|
|
68
69
|
src/tvboptim/types/parameter.py
|
|
69
70
|
src/tvboptim/types/spaces.py
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
# %%
|
|
2
|
-
import jax
|
|
3
|
-
import jax.numpy as jnp
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def jaxify(
|
|
7
|
-
experiment,
|
|
8
|
-
enable_x64: bool = True,
|
|
9
|
-
replace_temporal_averaging: bool = False,
|
|
10
|
-
return_new_ics: bool = False,
|
|
11
|
-
scalar_pre: bool = False,
|
|
12
|
-
bold_fft_convolve: bool = True,
|
|
13
|
-
small_dt: bool = False,
|
|
14
|
-
**kwargs,
|
|
15
|
-
):
|
|
16
|
-
"""Convert TVBO SimulationExperiment to JAX-compatible model function and state.
|
|
17
|
-
|
|
18
|
-
This function transforms a TVBO simulation experiment into a JAX-compiled
|
|
19
|
-
model function and corresponding state object for efficient brain simulation.
|
|
20
|
-
The resulting model supports automatic differentiation and parallel execution.
|
|
21
|
-
|
|
22
|
-
Parameters
|
|
23
|
-
----------
|
|
24
|
-
experiment : tvbo.export.experiment.SimulationExperiment
|
|
25
|
-
TVBO SimulationExperiment containing model, connectivity, coupling,
|
|
26
|
-
integration, and monitor specifications.
|
|
27
|
-
enable_x64 : bool, optional
|
|
28
|
-
If True, use float64 precision; otherwise float32. Transforms all arrays
|
|
29
|
-
in state to correct precision and sets JAX config 'jax_enable_x64'.
|
|
30
|
-
Default is True.
|
|
31
|
-
replace_temporal_averaging : bool, optional
|
|
32
|
-
If False, BOLD uses TemporalAverage monitor as TVB does. If True,
|
|
33
|
-
uses faster SubSample monitor with similar results. Default is False.
|
|
34
|
-
return_new_ics : bool, optional
|
|
35
|
-
If True, model returns updated initial conditions TimeSeries along
|
|
36
|
-
with simulation output for continuing simulations. Changes output
|
|
37
|
-
from result to [result, initial_conditions]. Default is False.
|
|
38
|
-
scalar_pre : bool, optional
|
|
39
|
-
If True, applies performance optimization replacing dot product with
|
|
40
|
-
matmul in coupling term. Only works with scalar-only pre expressions,
|
|
41
|
-
no delays, and when pre expression has single x_j occurrence.
|
|
42
|
-
Default is False.
|
|
43
|
-
bold_fft_convolve : bool, optional
|
|
44
|
-
If True, BOLD monitor uses FFT convolution instead of dot product.
|
|
45
|
-
Faster for most cases, time doesn't scale with BOLD period. Dot
|
|
46
|
-
product can be faster for large period values. Default is True.
|
|
47
|
-
small_dt : bool, optional
|
|
48
|
-
Uses full history storage for faster simulations at small dt. Can
|
|
49
|
-
cause memory explosion under jax.grad transformation. Default is False.
|
|
50
|
-
**kwargs : dict
|
|
51
|
-
Additional keyword arguments passed to experiment.execute().
|
|
52
|
-
|
|
53
|
-
Returns
|
|
54
|
-
-------
|
|
55
|
-
tuple[Callable, Any]
|
|
56
|
-
A tuple containing (model_function, state) where:
|
|
57
|
-
|
|
58
|
-
- model_function : Callable that takes state and returns simulation results
|
|
59
|
-
- state : JAX PyTree containing all simulation parameters and initial conditions
|
|
60
|
-
|
|
61
|
-
Examples
|
|
62
|
-
--------
|
|
63
|
-
>>> from tvbo.export.experiment import SimulationExperiment
|
|
64
|
-
>>> from tvboptim import jaxify
|
|
65
|
-
>>>
|
|
66
|
-
>>> # Create TVBO experiment
|
|
67
|
-
>>> experiment = SimulationExperiment(...)
|
|
68
|
-
>>>
|
|
69
|
-
>>> # Convert to JAX
|
|
70
|
-
>>> model, state = jaxify(experiment, enable_x64=True, scalar_pre=True)
|
|
71
|
-
>>>
|
|
72
|
-
>>> # Run simulation
|
|
73
|
-
>>> result = model(state)
|
|
74
|
-
>>> raw_data, bold_data = result
|
|
75
|
-
>>>
|
|
76
|
-
>>> # Use with JAX transformations
|
|
77
|
-
>>> grad_fn = jax.grad(lambda s: model(s)[0].data.sum())
|
|
78
|
-
>>> gradients = grad_fn(state)
|
|
79
|
-
|
|
80
|
-
Notes
|
|
81
|
-
-----
|
|
82
|
-
The returned model function is JAX-compiled and supports:
|
|
83
|
-
|
|
84
|
-
- Automatic differentiation with jax.grad, jax.jacobian
|
|
85
|
-
- Parallel execution with jax.vmap, jax.pmap
|
|
86
|
-
- Just-in-time compilation for optimal performance
|
|
87
|
-
- Integration with JAX ecosystem (optax, equinox, etc.)
|
|
88
|
-
|
|
89
|
-
The state object is a JAX PyTree that can be used with all JAX transformations
|
|
90
|
-
and contains Parameter objects for optimization workflows.
|
|
91
|
-
"""
|
|
92
|
-
state = experiment.collect_state()
|
|
93
|
-
jax.config.update("jax_enable_x64", enable_x64)
|
|
94
|
-
|
|
95
|
-
if enable_x64:
|
|
96
|
-
state = state.convert_dtype(target_dtype=jnp.float64)
|
|
97
|
-
else:
|
|
98
|
-
state = state.convert_dtype(target_dtype=jnp.float32)
|
|
99
|
-
|
|
100
|
-
_module = experiment.execute(
|
|
101
|
-
format="jax",
|
|
102
|
-
replace_temporal_averaging=replace_temporal_averaging,
|
|
103
|
-
return_new_ics=return_new_ics,
|
|
104
|
-
scalar_pre=scalar_pre,
|
|
105
|
-
bold_fft_convolve=bold_fft_convolve,
|
|
106
|
-
small_dt=small_dt,
|
|
107
|
-
**kwargs,
|
|
108
|
-
)
|
|
109
|
-
simulator = _module["kernel"]
|
|
110
|
-
|
|
111
|
-
return simulator, state
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/core/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/core/network.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/__init__.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/base.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/linear.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/subspace.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/coupling/tvb.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/__init__.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/base.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/lorenz.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/dynamics/tvb/linear.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/external_input/base.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/external_input/data.py
RENAMED
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/graph/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/graph/sparse.py
RENAMED
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/noise/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/noise/gaussian.py
RENAMED
|
File without changes
|
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/__init__.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/base.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/diffrax.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/solvers/native.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/utils/__init__.py
RENAMED
|
File without changes
|
{tvboptim-0.2.0 → tvboptim-0.2.1}/src/tvboptim/experimental/network_dynamics/utils/printer.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|