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 an ndarray of shape (n_samples, n_traces, time steps) where n_samples is the number of parameter sets that have been evaluated, and n_traces is the number of stimuli.
  • data_traces (ndarray) – The target traces to which the model should be compared. An ndarray 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:

ndarray

check_values(feat_list)[source]

Removes all the None values and checks for array features

feat_to_err(d1, d2)[source]
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 return DIMENSIONLESS.

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 trace
Returns: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 an ndarray of shape (n_samples, n_traces, time steps), where n_samples are the number of different parameter sets that have been evaluated, and n_traces are the number of input stimuli.
  • data_traces (ndarray) – The target traces to which the model should be compared. An ndarray 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:

ndarray

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 the normalization 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
revert_normalization(error)

Revert the normalization to recover the error before normalization.

Parameters:error (Quantity or float) – The normalized error.
Returns:raw_error – The error before normalization
Return type:Quantity or float
class brian2modelfitting.metric.GammaFactor(**kwds)[source]

Bases: brian2modelfitting.metric.SpikeMetric

Calculate gamma factors between goal and calculated spike trains, with precision delta.

Parameters:

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 is False, or a rate-difference dependent term if rate_correction` is ``True. In both cases, the best possible error value (i.e. for a perfect match between spike trains) is 0.

References

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 as model_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:

ndarray

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 return DIMENSIONLESS.

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 trace
Returns: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 as model_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:

ndarray

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 the normalization 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
revert_normalization(error)

Revert the normalization to recover the error before normalization.

Parameters:error (Quantity or float) – The normalized error.
Returns:raw_error – The error before normalization
Return type:Quantity or float
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 an ndarray of shape (n_samples, n_traces, time steps) where n_samples is the number of parameter sets that have been evaluated, and n_traces is the number of stimuli.
  • data_traces (ndarray) – The target traces to which the model should be compared. An ndarray 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:

ndarray

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 return DIMENSIONLESS.

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 trace
Returns: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 an ndarray of shape (n_samples, n_traces, time steps), where n_samples are the number of different parameter sets that have been evaluated, and n_traces are the number of input stimuli.
  • data_traces (ndarray) – The target traces to which the model should be compared. An ndarray 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:

ndarray

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 the normalization 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
revert_normalization(error)[source]

Revert the normalization to recover the error before normalization.

Parameters:error (Quantity or float) – The normalized error.
Returns:raw_error – The error before normalization
Return type:Quantity or float
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:
Returns:

Total error for each set of parameters, i.e. an array of shape (n_samples, ).

Return type:

ndarray

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 return DIMENSIONLESS.

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 trace
Returns: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 the normalization 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
revert_normalization(error)[source]

Revert the normalization to recover the error before normalization.

Parameters:error (Quantity or float) – The normalized error.
Returns:raw_error – The error before normalization
Return type:Quantity or float
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 as model_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:

ndarray

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 return DIMENSIONLESS.

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 trace
Returns: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 as model_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:

ndarray

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 the normalization 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
revert_normalization(error)

Revert the normalization to recover the error before normalization.

Parameters:error (Quantity or float) – The normalized error.
Returns:raw_error – The error before normalization
Return type:Quantity or float
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 an ndarray of shape (n_samples, n_traces, time steps) where n_samples is the number of parameter sets that have been evaluated, and n_traces is the number of stimuli.
  • data_traces (ndarray) – The target traces to which the model should be compared. An ndarray 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:

ndarray

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 return DIMENSIONLESS.

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 trace
Returns: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 an ndarray of shape (n_samples, n_traces, time steps), where n_samples are the number of different parameter sets that have been evaluated, and n_traces are the number of input stimuli.
  • data_traces (ndarray) – The target traces to which the model should be compared. An ndarray 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:

ndarray

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 the normalization 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
revert_normalization(error)

Revert the normalization to recover the error before normalization.

Parameters:error (Quantity or float) – The normalized error.
Returns:raw_error – The error before normalization
Return type:Quantity or float
brian2modelfitting.metric.calc_eFEL(traces, inp_times, feat_list, dt)[source]
brian2modelfitting.metric.firing_rate(spikes)[source]

Returns rate of the spike train

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:
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). Without rate_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:

float

brian2modelfitting.metric.normalize_weights(t_weights)[source]