Base classes#

Inherit from these classes to add a new model, objective function or fitting paradigm that works with the rest of WhoBPyT. See Code architecture for an overview.

class whobpyt.datatypes.AbstractParams(**kwargs)#

Base class for a model’s parameter container.

Subclasses set each model parameter as an attribute holding a Parameter, typically with defaults that keyword arguments can override (see JansenRitParams). The parameters may belong to the neural mass model and/or the measurement model.

Methods

getFittedNames()

Return the names of the learnable parameters.

to(device)

Move all parameters to device (a torch.device).

getFittedNames()#

Return the names of the learnable parameters.

Returns:
list of str

The name of every parameter with fit_par=True, plus <name>_m and <name>_v_inv for every parameter whose prior mean and precision are learned (fit_hyper=True).

to(device)#

Move all parameters to device (a torch.device).

class whobpyt.datatypes.AbstractNeuralModel(params)#

Base class for models (typically neural mass models) that can be fitted.

A model is a torch.nn.Module that simulates one window of activity per call to forward(). The measurement model (for example a lead field for EEG) may be part of the model.

Subclasses should set state_names and output_names, call setModelParameters() in their __init__, and implement createIC(), createDelayIC() and forward().

Parameters:
paramsAbstractParams

The model’s parameters.

Attributes:
state_nameslist of str

Names of the model’s state variables.

output_nameslist of str

Names of the model’s outputs. The first is used by the objective functions by default.

track_paramslist of str

Names of the learnable parameters, recorded during training.

params_fitteddict

'modelparameter' and 'hyperparameter' lists of tensors that the optimizers update. Set by setModelParameters().

commit_hashstr or None

WhoBPyT’s git commit, if running from a git checkout.

Methods

createDelayIC(ver)

Return the initial history of state variables needed for delays.

createIC(ver)

Return initial conditions for the model's state variables.

forward(external, hx, hE)

Simulate one window.

info()

Return the model's state names, output names, tracked parameters and commit.

setModelParameters()

Register the learnable parameters with PyTorch.

createDelayIC(ver)#

Return the initial history of state variables needed for delays.

The default returns a dummy tensor, for models without delays.

createIC(ver)#

Return initial conditions for the model’s state variables.

forward(external, hx, hE)#

Simulate one window.

Parameters:
externaltorch.Tensor

External input for the window.

hxtorch.Tensor

Current state.

hEtorch.Tensor

History used for delayed inputs.

Returns:
next_statedict

Simulated time series of each state variable and output, plus 'current_state', the state at the end of the window.

hEtorch.Tensor

Updated history.

info()#

Return the model’s state names, output names, tracked parameters and commit.

setModelParameters()#

Register the learnable parameters with PyTorch.

Every Parameter in params with fit_par=True becomes a torch.nn.Parameter and is added to params_fitted['modelparameter']; learnable priors (fit_hyper=True) are added to params_fitted['hyperparameter']. Call this in the subclass’s __init__.

class whobpyt.datatypes.AbstractLoss(simKey=None, model=None, device=torch.device('cpu'))#

Base class for objective functions.

Subclasses implement main_loss(), which compares simulated and empirical data. A full objective can combine it with prior_loss(), as CostsJR does.

Parameters:
simKeystr

Which of the model’s outputs or state variables to compare with the data, for example 'eeg'.

modelAbstractNeuralModel

The model being fitted. Needed by prior_loss().

devicetorch.device

Device to compute on.

Methods

main_loss(simData, empData)

Return the loss comparing simulated and empirical data.

prior_loss()

Return the penalties keeping learnable parameters near their priors.

main_loss(simData, empData)#

Return the loss comparing simulated and empirical data.

Any other information the loss needs should be given when the object is created, so the fitting classes can call every loss the same way.

Parameters:
simDatadict of torch.Tensor

Simulated time series, keyed by variable name. Usually accessed as simData[self.simKey].

empDatatorch.Tensor

The target: an empirical time series or a summary measure computed from one.

Returns:
torch.Tensor

A scalar loss to back-propagate.

prior_loss()#

Return the penalties keeping learnable parameters near their priors.

For every parameter whose prior is learned (fit_hyper=True), the penalty is the precision-weighted squared distance between the value and the prior mean, minus the log precision.

Returns:
list of torch.Tensor

One penalty per parameter; sum them to add to the loss.

class whobpyt.datatypes.AbstractFitting(model: AbstractNeuralModel, cost: AbstractLoss, device=torch.device('cpu'))#

Base class for fitting paradigms.

A fitting paradigm runs a model, compares its output with data using an objective function, and updates the model’s learnable parameters. Subclasses implement train(), evaluate() and simulate().

Parameters:
modelAbstractNeuralModel

The model to fit.

costAbstractLoss

The objective function.

devicetorch.device

Device to compute on.

Attributes:
trainingStatsTrainingStats

Loss and parameter values recorded during training.

lastRecdict of Timeseries or None

The simulated time series from the most recent run.

Methods

evaluate(*args, **kwargs)

Run the model and compare it with data, without updating parameters.

save(filename)

Save this object, including the model and training history, with pickle.

simulate(*args, **kwargs)

Run the model for a fixed duration.

train(*args, **kwargs)

Fit the model's learnable parameters to data.

evaluate(*args, **kwargs)#

Run the model and compare it with data, without updating parameters.

save(filename)#

Save this object, including the model and training history, with pickle.

simulate(*args, **kwargs)#

Run the model for a fixed duration.

train(*args, **kwargs)#

Fit the model’s learnable parameters to data.