Code architecture#
WhoBPyT is organised around four kinds of interchangeable building block. Fitting a model means choosing one of each:
Building block |
Sub-package |
Role |
|---|---|---|
Model |
|
Simulates brain activity: a neural mass model for every region, coupled through the connectome, plus a measurement model that maps region activity to the recorded signal (for example a lead field for EEG). |
Parameters |
|
Hold each model parameter’s value, its prior, and whether it is learnable. |
Objective function |
|
Scores how well a simulation matches the data. |
Fitting paradigm |
|
Runs the simulate-compare-update loop that fits the parameters. |
How the implementation works, including the numerical solver, delays, priors and the training loop, is described on the Algorithms and other implementation details page.
Typical workflow#
from whobpyt.datatypes import Parameter as par, Timeseries
from whobpyt.models.jansen_rit import JansenRitModel, JansenRitParams
from whobpyt.optimization.custom_cost_JR import CostsJR
from whobpyt.run import ModelFitting
emp = Timeseries(eeg, step_size) # the data
params = JansenRitParams(a=par(100, 100, 2, True), ...) # what to fit
model = JansenRitModel(params, sc=sc, lm=lm, dist=dist, ...)
fit = ModelFitting(model, CostsJR(model))
fit.train(u=stimulus, empRecs=[emp], num_epochs=50, TPperWindow=20)
fit.evaluate(u=stimulus, empRec=emp, TPperWindow=20)
See Getting started: simulate and fit a whole-brain model for a complete, runnable version.
What’s included#
- Models (
whobpyt.models) JansenRitModelandJansenRitParams: a Jansen-Rit neural mass model with distance-dependent delays, learnable connection gains, and a lead field to simulate EEG/MEG.- Data types (
whobpyt.datatypes) Parameter(a value with an optional prior),Timeseries(a recording or simulation),TrainingStats(loss and parameter history), and the abstract base classes listed below.- Objective functions (
whobpyt.optimization) CostsTS(time-series error),CostsFCandCostsFixedFC(functional connectivity),CostsFixedPSD(power spectrum),CostsMean(target mean value of a state variable), andCostsJR(time-series error plus parameter priors, used with the Jansen-Rit model).CostsPSDis marked as no longer supported in the code.- Fitting paradigms (
whobpyt.run) ModelFittingtrains on consecutive windows of a recording and is the one used in the examples.FittingFNGFPGandFittingBatchare specialised alternatives for long simulations and batched training.- Other modules
whobpyt.datasetsfetches the example datasets (see Datasets),whobpyt.visualizationhas plotting helpers, andwhobpyt.deprkeeps the original model code from the Momi 2023, Momi 2025 and Ismail 2026 papers. Their replications in the examples gallery use it, and the model fits saved with it, to reproduce the published results exactly.
Extending WhoBPyT#
New components should inherit from the abstract classes in
whobpyt.datatypes. They define the methods the other classes rely on, so
a new model, cost or fitting paradigm works with the existing ones.
AbstractParamsContainer for a model’s
Parameterobjects.AbstractNeuralModelA
torch.nn.Module. ImplementcreateICandcreateDelayIC(initial states and delay history) andforward(external, hx, hE), which simulates one window. ItssetModelParametersmethod registers the learnable parameters with PyTorch.AbstractLossImplement
main_loss(simData, empData).prior_lossadds the penalty for learnable parameters straying from their priors.AbstractFittingImplement
train,evaluateandsimulate.