|
| 1 | +"""Demo: pass a Python model instance to the widget and read back tuned parameters. |
| 2 | +
|
| 3 | +This demonstrates the ``model=`` argument to ``PhasePlaneWidget``, |
| 4 | +which accepts any ``BaseModel`` subclass instance. After the user |
| 5 | +interacts with the widget (dragging sliders, clicking the phase plane), |
| 6 | +the current parameter values can be read back from the ``.params`` |
| 7 | +traitlet. |
| 8 | +
|
| 9 | +If the model name is registered in ``MODEL_REGISTRY`` the JavaScript |
| 10 | +front-end will recognise it and carry out all computation client-side. |
| 11 | +For arbitrary custom models that are *not* in the built-in registry use |
| 12 | +:func:`phase_plane` instead (see ``custom_model_demo.py``). |
| 13 | +""" |
| 14 | + |
| 15 | +from tvb_phaseplane import PhasePlaneWidget, MPRModel |
| 16 | + |
| 17 | +# ------------------------------------------------------------------ |
| 18 | +# 1. Instantiate a model class and optionally override defaults |
| 19 | +# ------------------------------------------------------------------ |
| 20 | +model = MPRModel() |
| 21 | + |
| 22 | +# You can pre-configure parameters before creating the widget |
| 23 | +# (the widget will pick these up as its initial slider positions) |
| 24 | +initial_params = { |
| 25 | + "delta": 1.0, |
| 26 | + "eta_bar": -5.0, |
| 27 | + "J": 15.0, |
| 28 | + "I": 0.0, |
| 29 | +} |
| 30 | + |
| 31 | +# Override the model's default parameter values with your own |
| 32 | +for k, v in initial_params.items(): |
| 33 | + model.default_params[k] = v |
| 34 | + |
| 35 | +# ------------------------------------------------------------------ |
| 36 | +# 2. Pass the instance to PhasePlaneWidget |
| 37 | +# ------------------------------------------------------------------ |
| 38 | +widget = PhasePlaneWidget(model=model) |
| 39 | + |
| 40 | +# The widget is now live — display it in Jupyter / VS Code |
| 41 | +widget |
| 42 | + |
| 43 | +# ------------------------------------------------------------------ |
| 44 | +# 3. After user interaction, read back tuned parameters |
| 45 | +# ------------------------------------------------------------------ |
| 46 | +# Run this cell *after* adjusting sliders in the widget: |
| 47 | +print("Current parameter values after tuning:") |
| 48 | +for name, value in widget.params.items(): |
| 49 | + print(f" {name:12s} = {value:.4f}") |
| 50 | + |
| 51 | +# You can also programmatically set parameters and trigger updates: |
| 52 | +# widget.params["J"] = 20.0 |
| 53 | + |
| 54 | +# ------------------------------------------------------------------ |
| 55 | +# 4. Read back computed data (nullclines, fixed points, trajectory) |
| 56 | +# ------------------------------------------------------------------ |
| 57 | +print(f"\nFixed points detected: {len(widget.fixed_points)}") |
| 58 | +for fp in widget.fixed_points[:5]: |
| 59 | + print(f" x={fp[0]:.4f}, y={fp[1]:.4f}, type={fp[2]}") |
| 60 | + |
| 61 | +print(f"\nTrajectory points: {len(widget.trajectory)}") |
| 62 | + |
| 63 | +# ------------------------------------------------------------------ |
| 64 | +# 5. Export the tuned configuration to standalone HTML |
| 65 | +# ------------------------------------------------------------------ |
| 66 | +widget.to_standalone_html("mpr_tuned.html", title="MPR Model – Tuned Parameters") |
| 67 | +print("\nExported to 'mpr_tuned.html' with current parameter values.") |
0 commit comments