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:
valfloat or ndarray

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_meanfloat, optional

Mean of the Gaussian prior.

prior_stdfloat, 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 is lb + 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.

lbfloat

Lower bound, added to the value.

devicetorch.device

Device to store the parameter on.

Attributes:
valtorch.Tensor

The stored (possibly log-scaled) value. Use value() to get the parameter’s actual value.

prior_meantorch.Tensor

Prior mean, if a prior was given.

prior_precisiontorch.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.

Methods

npValue()

to(device)

Move the parameter to device (a torch.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:
ndarray

The parameter value(s), including the lower bound and undoing the log transform if asLog is set.

to(device)#

Move the parameter to device (a torch.device).

value()#
Returns:
torch.Tensor

The parameter value(s), including the lower bound and undoing the log transform if asLog is 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 Timeseries objects, and simulation results come back as Timeseries (in fit.lastRec). The plotting functions in whobpyt.visualization also take them. Inside the models, a plain dictionary of tensors is used instead.

Parameters:
datandarray or torch.Tensor, shape (num_regions, ts_length)

The time series. NumPy arrays are converted to a tensor.

step_sizefloat

Time between samples, in seconds.

modalitystr

Name of the modality, for example 'eeg'.

Attributes:
datatorch.Tensor, shape (num_regions, ts_length)

The time series.

step_sizefloat

Time between samples, in seconds.

modalitystr

Name of the modality.

numNodesint

Number of regions or channels.

lengthint

Number of time points.

Methods

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).

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:
TPperWindowInt

The number of time points in the window that will be back propagated

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_infoDict

Information about model being tracked during training.

track_paramsList

List of parameter names being tracked during training.

lossList

A list of loss values over training.

connectivityList

A list of connectivity values over training.

leadfieldList

A list of leadfield matrices over training.

fit_paramsDict

A dictionary of lists where the key is the parameter name and the value is the list of parameter values over training.

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:
newValuendarray

Current state of a lead field matrix being tracked.

appendLoss(newValue)#

Append Trainig Loss

Parameters:
newValueFloat

The loss value of objective function being tracked.

appendParam(newValues)#

Append Fit Parameters

Parameters:
newValuesDict

Dictionary with current states of each model parameter being tracked.

appendSC(newValue)#

Append Network Connections

Parameters:
newValuendarray

Current state of the structural connectivity being tracked.

appendSC_p2e(newValue)#

Append Network Connections

Parameters:
newValuendarray

Current state of the structural connectivity being tracked.

appendSC_p2i(newValue)#

Append Network Connections

Parameters:
newValuendarray

Current state of the structural connectivity being tracked.

appendSC_p2p(newValue)#

Append Network Connections

Parameters:
newValuendarray

Current state of the structural connectivity being tracked.

reset()#

Resets the attributes of the model to a pre-training state.

save(filename)#
Parameters:
filenameString

The filename to use to save the TrainingStats as a pickle object.