brian2modelfitting.metric module¶

class
brian2modelfitting.metric.
FeatureMetric
(stim_times, feat_list, weights=None, combine=None, t_start=0. * second, normalization=1.0)[source]¶ Bases:
brian2modelfitting.metric.TraceMetric

calc
(model_traces, data_traces, dt)¶ Perform the error calculation across all parameters, calculate error between each output trace and corresponding simulation.
Parameters:  model_traces (
ndarray
) – Traces that should be evaluated and compared to the target data. Provided as anndarray
of shape(n_samples, n_traces, time steps)
wheren_samples
is the number of parameter sets that have been evaluated, andn_traces
is the number of stimuli.  data_traces (
ndarray
) – The target traces to which the model should be compared. Anndarray
of shape(n_traces, time steps)
.  dt (
Quantity
) – The length of a single time step.
Returns: Total error for each set of parameters, i.e. an array of shape
(n_samples, )
.Return type:  model_traces (

get_dimensions
(output_dim)¶ The physical dimensions of the error. In metrics such as
MSEMetric
, this depends on the dimensions of the output variable (e.g. if the output variable has units of volts, the mean squared error will have units of volt²); in other metrics, e.g.FeatureMetric
, this cannot be defined in a meaningful way since the metric combines different types of errors. In cases where defining dimensions is not meaningful, this method should returnDIMENSIONLESS
.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the error. Return type: Dimension

get_errors
(features)[source]¶ Function weights features/multiple errors into one final error per each set of parameters.
The output of the function has to take shape of (n_samples,).
Parameters: features ( ndarray
) – 2D array of shape(n_samples, n_traces)
with the features/errors for each simulated traceReturns: Errors for each parameter set, i.e. of shape (n_samples, )
Return type: ndarray

get_features
(traces, output, dt)[source]¶ Calculate the features/errors for each simulated trace, by comparing it to the corresponding data trace.
Parameters:  model_traces (
ndarray
) – Traces that should be evaluated and compared to the target data. Provided as anndarray
of shape(n_samples, n_traces, time steps)
, wheren_samples
are the number of different parameter sets that have been evaluated, andn_traces
are the number of input stimuli.  data_traces (
ndarray
) – The target traces to which the model should be compared. Anndarray
of shape(n_traces, time steps)
.  dt (
Quantity
) – The length of a single time step.
Returns: An
ndarray
of shape(n_samples, n_traces)
returning the error/feature value for each simulated trace.Return type:  model_traces (

get_normalized_dimensions
(output_dim)¶ The physical dimensions of the normalized error. This will be the same as the dimensions returned by
get_dimensions
if thenormalization
is not used or set to a dimensionless value.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the normalized error. Return type: Dimension


class
brian2modelfitting.metric.
GammaFactor
(**kwds)[source]¶ Bases:
brian2modelfitting.metric.SpikeMetric
Calculate gamma factors between goal and calculated spike trains, with precision delta.
Parameters:  delta (
Quantity
) – time window  time (
Quantity
) – total length of experiment  rate_correction (bool) – Whether to include an error term that penalizes differences in firing
rate, following Clopath et al., Neurocomputing (2007). Defaults to
True
.
Notes
The gamma factor is commonly defined as 1 for a perfect match and 0 for a match not better than random (negative values are possible if the match is worse than expected by chance). Since we use the gamma factor as an error to be minimized, the calculated term is actually r  gamma_factor, where r is 1 if
rate_correction
isFalse
, or a ratedifference dependent term ifrate_correction` is ``True
. In both cases, the best possible error value (i.e. for a perfect match between spike trains) is 0.References
 R. Jolivet et al. “A Benchmark Test for a Quantitative Assessment of Simple Neuron Models.” Journal of Neuroscience Methods, 169, no. 2 (2008): 417–24.
 C. Clopath et al. “Predicting Neuronal Activity with Simple Models of the Threshold Type: Adaptive Exponential IntegrateandFire Model with Two Compartments.” Neurocomputing, 70, no. 10 (2007): 1668–73.

calc
(model_spikes, data_spikes, dt)¶ Perform the error calculation across all parameters, calculate error between each output trace and corresponding simulation.
Parameters:  model_spikes (list of list of
ndarray
) – A nested list structure for the spikes generated by the model: a list where each element contains the results for a single parameter set. Each of these results is a list for each of the input traces, where the elements of this list are numpy arrays of spike times (without units, i.e. in seconds).  data_spikes (list of
ndarray
) – The target spikes for the fitting, represented in the same way asmodel_spikes
, i.e. as a list of spike times for each input stimulus.  dt (
Quantity
) – The length of a single time step.
Returns: Total error for each set of parameters, i.e. an array of shape
(n_samples, )
Return type:  model_spikes (list of list of

get_dimensions
(output_dim)¶ The physical dimensions of the error. In metrics such as
MSEMetric
, this depends on the dimensions of the output variable (e.g. if the output variable has units of volts, the mean squared error will have units of volt²); in other metrics, e.g.FeatureMetric
, this cannot be defined in a meaningful way since the metric combines different types of errors. In cases where defining dimensions is not meaningful, this method should returnDIMENSIONLESS
.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the error. Return type: Dimension

get_errors
(features)[source]¶ Function weights features/multiple errors into one final error per each set of parameters.
The output of the function has to take shape of (n_samples,).
Parameters: features ( ndarray
) – 2D array of shape(n_samples, n_traces)
with the features/errors for each simulated traceReturns: Errors for each parameter set, i.e. of shape (n_samples, )
Return type: ndarray

get_features
(traces, output, dt)[source]¶ Calculate the features/errors for each simulated spike train by comparing it to the corresponding data spike train.
Parameters:  model_spikes (list of list of
ndarray
) – A nested list structure for the spikes generated by the model: a list where each element contains the results for a single parameter set. Each of these results is a list for each of the input traces, where the elements of this list are numpy arrays of spike times (without units, i.e. in seconds).  data_spikes (list of
ndarray
) – The target spikes for the fitting, represented in the same way asmodel_spikes
, i.e. as a list of spike times for each input stimulus.  dt (
Quantity
) – The length of a single time step.
Returns: An
ndarray
of shape(n_samples, n_traces)
returning the error/feature value for each simulated trace.Return type:  model_spikes (list of list of

get_normalized_dimensions
(output_dim)¶ The physical dimensions of the normalized error. This will be the same as the dimensions returned by
get_dimensions
if thenormalization
is not used or set to a dimensionless value.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the normalized error. Return type: Dimension
 delta (

class
brian2modelfitting.metric.
MSEMetric
(**kwds)[source]¶ Bases:
brian2modelfitting.metric.TraceMetric
Mean Square Error between goal and calculated output.

calc
(model_traces, data_traces, dt)¶ Perform the error calculation across all parameters, calculate error between each output trace and corresponding simulation.
Parameters:  model_traces (
ndarray
) – Traces that should be evaluated and compared to the target data. Provided as anndarray
of shape(n_samples, n_traces, time steps)
wheren_samples
is the number of parameter sets that have been evaluated, andn_traces
is the number of stimuli.  data_traces (
ndarray
) – The target traces to which the model should be compared. Anndarray
of shape(n_traces, time steps)
.  dt (
Quantity
) – The length of a single time step.
Returns: Total error for each set of parameters, i.e. an array of shape
(n_samples, )
.Return type:  model_traces (

get_dimensions
(output_dim)[source]¶ The physical dimensions of the error. In metrics such as
MSEMetric
, this depends on the dimensions of the output variable (e.g. if the output variable has units of volts, the mean squared error will have units of volt²); in other metrics, e.g.FeatureMetric
, this cannot be defined in a meaningful way since the metric combines different types of errors. In cases where defining dimensions is not meaningful, this method should returnDIMENSIONLESS
.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the error. Return type: Dimension

get_errors
(features)[source]¶ Function weights features/multiple errors into one final error per each set of parameters.
The output of the function has to take shape of (n_samples,).
Parameters: features ( ndarray
) – 2D array of shape(n_samples, n_traces)
with the features/errors for each simulated traceReturns: Errors for each parameter set, i.e. of shape (n_samples, )
Return type: ndarray

get_features
(model_traces, data_traces, dt)[source]¶ Calculate the features/errors for each simulated trace, by comparing it to the corresponding data trace.
Parameters:  model_traces (
ndarray
) – Traces that should be evaluated and compared to the target data. Provided as anndarray
of shape(n_samples, n_traces, time steps)
, wheren_samples
are the number of different parameter sets that have been evaluated, andn_traces
are the number of input stimuli.  data_traces (
ndarray
) – The target traces to which the model should be compared. Anndarray
of shape(n_traces, time steps)
.  dt (
Quantity
) – The length of a single time step.
Returns: An
ndarray
of shape(n_samples, n_traces)
returning the error/feature value for each simulated trace.Return type:  model_traces (

get_normalized_dimensions
(output_dim)[source]¶ The physical dimensions of the normalized error. This will be the same as the dimensions returned by
get_dimensions
if thenormalization
is not used or set to a dimensionless value.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the normalized error. Return type: Dimension


class
brian2modelfitting.metric.
Metric
(**kwds)[source]¶ Bases:
object
Metric abstract class to define functions required for a custom metric To be used with modelfitting Fitters.

calc
(model_results, data_results, dt)[source]¶ Perform the error calculation across all parameter sets by comparing the simulated to the experimental data.
Parameters:  model_results – Results generated by the model. The type and shape of this data
depends on the fitting problem. See
TraceMetric.calc
andSpikeMetric.calc
.  data_results – The experimental data that the model is fit against. See
TraceMetric.calc
andSpikeMetric.calc
for the type/shape of the data.  dt (
Quantity
) – The length of a single time step.
Returns: Total error for each set of parameters, i.e. an array of shape
(n_samples, )
.Return type:  model_results – Results generated by the model. The type and shape of this data
depends on the fitting problem. See

get_dimensions
(output_dim)[source]¶ The physical dimensions of the error. In metrics such as
MSEMetric
, this depends on the dimensions of the output variable (e.g. if the output variable has units of volts, the mean squared error will have units of volt²); in other metrics, e.g.FeatureMetric
, this cannot be defined in a meaningful way since the metric combines different types of errors. In cases where defining dimensions is not meaningful, this method should returnDIMENSIONLESS
.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the error. Return type: Dimension

get_errors
(features)[source]¶ Function weights features/multiple errors into one final error per each set of parameters.
The output of the function has to take shape of (n_samples,).
Parameters: features ( ndarray
) – 2D array of shape(n_samples, n_traces)
with the features/errors for each simulated traceReturns: Errors for each parameter set, i.e. of shape (n_samples, )
Return type: ndarray

get_features
(model_results, target_results, dt)[source]¶ Function calculates features / errors for each of the input traces.
The output of the function has to take shape of (n_samples, n_traces).

get_normalized_dimensions
(output_dim)[source]¶ The physical dimensions of the normalized error. This will be the same as the dimensions returned by
get_dimensions
if thenormalization
is not used or set to a dimensionless value.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the normalized error. Return type: Dimension


class
brian2modelfitting.metric.
SpikeMetric
(**kwds)[source]¶ Bases:
brian2modelfitting.metric.Metric
A metric for comparing the spike trains.

calc
(model_spikes, data_spikes, dt)[source]¶ Perform the error calculation across all parameters, calculate error between each output trace and corresponding simulation.
Parameters:  model_spikes (list of list of
ndarray
) – A nested list structure for the spikes generated by the model: a list where each element contains the results for a single parameter set. Each of these results is a list for each of the input traces, where the elements of this list are numpy arrays of spike times (without units, i.e. in seconds).  data_spikes (list of
ndarray
) – The target spikes for the fitting, represented in the same way asmodel_spikes
, i.e. as a list of spike times for each input stimulus.  dt (
Quantity
) – The length of a single time step.
Returns: Total error for each set of parameters, i.e. an array of shape
(n_samples, )
Return type:  model_spikes (list of list of

get_dimensions
(output_dim)¶ The physical dimensions of the error. In metrics such as
MSEMetric
, this depends on the dimensions of the output variable (e.g. if the output variable has units of volts, the mean squared error will have units of volt²); in other metrics, e.g.FeatureMetric
, this cannot be defined in a meaningful way since the metric combines different types of errors. In cases where defining dimensions is not meaningful, this method should returnDIMENSIONLESS
.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the error. Return type: Dimension

get_errors
(features)¶ Function weights features/multiple errors into one final error per each set of parameters.
The output of the function has to take shape of (n_samples,).
Parameters: features ( ndarray
) – 2D array of shape(n_samples, n_traces)
with the features/errors for each simulated traceReturns: Errors for each parameter set, i.e. of shape (n_samples, )
Return type: ndarray

get_features
(model_spikes, data_spikes, dt)[source]¶ Calculate the features/errors for each simulated spike train by comparing it to the corresponding data spike train.
Parameters:  model_spikes (list of list of
ndarray
) – A nested list structure for the spikes generated by the model: a list where each element contains the results for a single parameter set. Each of these results is a list for each of the input traces, where the elements of this list are numpy arrays of spike times (without units, i.e. in seconds).  data_spikes (list of
ndarray
) – The target spikes for the fitting, represented in the same way asmodel_spikes
, i.e. as a list of spike times for each input stimulus.  dt (
Quantity
) – The length of a single time step.
Returns: An
ndarray
of shape(n_samples, n_traces)
returning the error/feature value for each simulated trace.Return type:  model_spikes (list of list of

get_normalized_dimensions
(output_dim)¶ The physical dimensions of the normalized error. This will be the same as the dimensions returned by
get_dimensions
if thenormalization
is not used or set to a dimensionless value.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the normalized error. Return type: Dimension


class
brian2modelfitting.metric.
TraceMetric
(**kwds)[source]¶ Bases:
brian2modelfitting.metric.Metric
Input traces have to be shaped into 2D array.

calc
(model_traces, data_traces, dt)[source]¶ Perform the error calculation across all parameters, calculate error between each output trace and corresponding simulation.
Parameters:  model_traces (
ndarray
) – Traces that should be evaluated and compared to the target data. Provided as anndarray
of shape(n_samples, n_traces, time steps)
wheren_samples
is the number of parameter sets that have been evaluated, andn_traces
is the number of stimuli.  data_traces (
ndarray
) – The target traces to which the model should be compared. Anndarray
of shape(n_traces, time steps)
.  dt (
Quantity
) – The length of a single time step.
Returns: Total error for each set of parameters, i.e. an array of shape
(n_samples, )
.Return type:  model_traces (

get_dimensions
(output_dim)¶ The physical dimensions of the error. In metrics such as
MSEMetric
, this depends on the dimensions of the output variable (e.g. if the output variable has units of volts, the mean squared error will have units of volt²); in other metrics, e.g.FeatureMetric
, this cannot be defined in a meaningful way since the metric combines different types of errors. In cases where defining dimensions is not meaningful, this method should returnDIMENSIONLESS
.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the error. Return type: Dimension

get_errors
(features)¶ Function weights features/multiple errors into one final error per each set of parameters.
The output of the function has to take shape of (n_samples,).
Parameters: features ( ndarray
) – 2D array of shape(n_samples, n_traces)
with the features/errors for each simulated traceReturns: Errors for each parameter set, i.e. of shape (n_samples, )
Return type: ndarray

get_features
(model_traces, data_traces, dt)[source]¶ Calculate the features/errors for each simulated trace, by comparing it to the corresponding data trace.
Parameters:  model_traces (
ndarray
) – Traces that should be evaluated and compared to the target data. Provided as anndarray
of shape(n_samples, n_traces, time steps)
, wheren_samples
are the number of different parameter sets that have been evaluated, andn_traces
are the number of input stimuli.  data_traces (
ndarray
) – The target traces to which the model should be compared. Anndarray
of shape(n_traces, time steps)
.  dt (
Quantity
) – The length of a single time step.
Returns: An
ndarray
of shape(n_samples, n_traces)
returning the error/feature value for each simulated trace.Return type:  model_traces (

get_normalized_dimensions
(output_dim)¶ The physical dimensions of the normalized error. This will be the same as the dimensions returned by
get_dimensions
if thenormalization
is not used or set to a dimensionless value.Parameters: output_dim ( Dimension
) – The dimensions of the output variable.Returns: dim – The physical dimensions of the normalized error. Return type: Dimension


brian2modelfitting.metric.
get_gamma_factor
(model, data, delta, time, dt, rate_correction=True)[source]¶ Calculate gamma factor between model and target spike trains, with precision delta.
Parameters:  model (
list
orndarray
) – model trace  data (
list
orndarray
) – data trace  delta (
Quantity
) – time window  dt (
Quantity
) – time step  time (
Quantity
) – total time of the simulation  rate_correction (bool) –
Whether to include an error term that penalizes differences in firing rate, following Clopath et al., Neurocomputing (2007).
Returns: An error based on the Gamma factor. If
rate_correction
is used, then the returned error is \(1 + 2\frac{\lvert r_\mathrm{data}  r_\mathrm{model}\rvert}{r_\mathrm{data}}  \Gamma\) (with \(r_\mathrm{data}\) and \(r_\mathrm{model}\) being the firing rates in the data/model, and \(\Gamma\) the coincidence factor). Withoutrate_correction
, the error is \(1  \Gamma\). Note that the coincidence factor \(\Gamma\) has a maximum value of 1 (when the two spike trains are exactly identical) and a value of 0 if there are only as many coincidences as expected from two homogeneous Poisson processes of the same rate. It can also take negative values if there are fewer coincidences than expected by chance.Return type:  model (