Data types#
- class whobpyt.datatypes.Parameter(val, prior_mean=None, prior_std=None, fit_par=False, asLog=False, asRand=True, lb=0, device=torch.device('cpu'))#
A model parameter: its value and, optionally, a prior and whether it is learnable.
- Parameters:
- val
floatorndarray The value, or an array of values (for example one per region). For a learnable parameter with a prior and
asRand=True, this is replaced by a random draw from the prior (only its shape is used for arrays).- prior_mean
float, optional Mean of the Gaussian prior.
- prior_std
float, optional Standard deviation of the Gaussian prior.
- fit_parbool
If True, the value is learned during training. If a prior is also given, the prior’s mean and precision are learned too.
- asLogbool
If True,
val(and the prior) are given on a log scale and the parameter’s value islb + exp(val), which keeps it positive.- asRandbool
If True (and the parameter is learnable with a prior), start from a random draw from the prior instead of
val.- lb
float Lower bound, added to the value.
- device
torch.device Device to store the parameter on.
- val
- Attributes:
- val
torch.Tensor The stored (possibly log-scaled) value. Use
value()to get the parameter’s actual value.- prior_mean
torch.Tensor Prior mean, if a prior was given.
- prior_precision
torch.Tensor Prior precision (1 / variance), if a prior was given.
- fit_parbool
Whether the value is learned.
- fit_hyperbool
Whether the prior mean and precision are learned.
- val
Methods
npValue()to(device)Move the parameter to
device(atorch.device).value()Examples
>>> from whobpyt.datatypes import Parameter as par >>> A = par(3.25) # fixed >>> a = par(100, 100, 2, True) # learnable, prior N(100, 2**2) >>> mu = par(np.log(1.5), np.log(1.5), 0.1, True, True, lb=0.1) # positive
- npValue()#
- Returns:
ndarrayThe parameter value(s), including the lower bound and undoing the log transform if
asLogis set.
- to(device)#
Move the parameter to
device(atorch.device).
- value()#
- Returns:
torch.TensorThe parameter value(s), including the lower bound and undoing the log transform if
asLogis set. Gradients flow through it.
- class whobpyt.datatypes.Timeseries(data, step_size, modality='')#
A recording or simulation: a regions (or channels) x time array.
Empirical data are passed to the fitting classes as
Timeseriesobjects, and simulation results come back asTimeseries(infit.lastRec). The plotting functions inwhobpyt.visualizationalso take them. Inside the models, a plain dictionary of tensors is used instead.- Parameters:
- data
ndarrayortorch.Tensor, shape (num_regions, ts_length) The time series. NumPy arrays are converted to a tensor.
- step_size
float Time between samples, in seconds.
- modality
str Name of the modality, for example
'eeg'.
- data
- Attributes:
- data
torch.Tensor, shape (num_regions, ts_length) The time series.
- step_size
float Time between samples, in seconds.
- modality
str Name of the modality.
- numNodes
int Number of regions or channels.
- length
int Number of time points.
- data
Methods
Return the data as a NumPy array, shape
(num_regions, ts_length).npTS()Return the data as a NumPy array, shape
(num_regions, ts_length).Return the transposed data as a NumPy array, shape
(ts_length, num_regions).pyTS()Return the data as a tensor, shape
(num_regions, ts_length).windowedTensor(TPperWindow)This method is called by the Model_fitting Class during training to reshape the data into windowed segments (adds another dimension).
- npNodeByTime()#
Return the data as a NumPy array, shape
(num_regions, ts_length).
- npTS()#
Return the data as a NumPy array, shape
(num_regions, ts_length).
- npTimeByNodes()#
Return the transposed data as a NumPy array, shape
(ts_length, num_regions).
- pyTS()#
Return the data as a tensor, shape
(num_regions, ts_length).
- windowedTensor(TPperWindow)#
This method is called by the Model_fitting Class during training to reshape the data into windowed segments (adds another dimension).
- Parameters:
- TPperWindow
Int The number of time points in the window that will be back propagated
- TPperWindow
- Returns:
- Tensor: num_windows x num_regions x window_length
The time series data in a windowed format
- class whobpyt.datatypes.TrainingStats(model)#
- This class is responsible for recording stats during training (it replaces OutputNM) including:
The training and validation losses over time
The change in model parameters over time
changing hyper parameters over time like learing rates TODO
These are things typically recorded on a per window/epoch basis
- Attributes:
- model_info
Dict Information about model being tracked during training.
- track_params
List List of parameter names being tracked during training.
- loss
List A list of loss values over training.
- connectivity
List A list of connectivity values over training.
- leadfield
List A list of leadfield matrices over training.
- fit_params
Dict A dictionary of lists where the key is the parameter name and the value is the list of parameter values over training.
- model_info
Methods
appendLF(newValue)Append Lead Field Loss
appendLoss(newValue)Append Trainig Loss
appendParam(newValues)Append Fit Parameters
appendSC(newValue)Append Network Connections
appendSC_p2e(newValue)Append Network Connections
appendSC_p2i(newValue)Append Network Connections
appendSC_p2p(newValue)Append Network Connections
reset()Resets the attributes of the model to a pre-training state.
save(filename)- appendLF(newValue)#
Append Lead Field Loss
- Parameters:
- newValue
ndarray Current state of a lead field matrix being tracked.
- newValue
- appendLoss(newValue)#
Append Trainig Loss
- Parameters:
- newValue
Float The loss value of objective function being tracked.
- newValue
- appendParam(newValues)#
Append Fit Parameters
- Parameters:
- newValues
Dict Dictionary with current states of each model parameter being tracked.
- newValues
- appendSC(newValue)#
Append Network Connections
- Parameters:
- newValue
ndarray Current state of the structural connectivity being tracked.
- newValue
- appendSC_p2e(newValue)#
Append Network Connections
- Parameters:
- newValue
ndarray Current state of the structural connectivity being tracked.
- newValue
- appendSC_p2i(newValue)#
Append Network Connections
- Parameters:
- newValue
ndarray Current state of the structural connectivity being tracked.
- newValue
- appendSC_p2p(newValue)#
Append Network Connections
- Parameters:
- newValue
ndarray Current state of the structural connectivity being tracked.
- newValue
- reset()#
Resets the attributes of the model to a pre-training state.