# Fitting¶

impedance.models.circuits.fitting.buildCircuit(circuit, frequencies, *parameters, constants=None, eval_string='', index=0)[source]

recursive function that transforms a circuit, parameters, and frequencies into a string that can be evaluated

Parameters
circuit: str
frequencies: list/tuple/array of floats
parameters: list/tuple/array of floats
constants: dict
Returns
eval_string: str

Python expression for calculating the resulting fit

index: int

Tracks parameter index through recursive calling of the function

impedance.models.circuits.fitting.calculateCircuitLength(circuit)[source]

Calculates the number of elements in the circuit.

Parameters
circuitstr

Circuit string.

Returns
lengthint

Length of circuit.

impedance.models.circuits.fitting.check_and_eval(element)[source]

Checks if an element is valid, then evaluates it.

Parameters
elementstr

Circuit element.

Returns
Evaluated element.
Raises
ValueError

Raised if an element is not in the list of allowed elements.

impedance.models.circuits.fitting.circuit_fit(frequencies, impedances, circuit, initial_guess, constants={}, bounds=None, weight_by_modulus=False, global_opt=False, **kwargs)[source]

Main function for fitting an equivalent circuit to data.

By default, this function uses scipy.optimize.curve_fit to fit the equivalent circuit. This function generally works well for simple circuits. However, the final results may be sensitive to the initial conditions for more complex circuits. In these cases, the scipy.optimize.basinhopping global optimization algorithm can be used to attempt a better fit.

Parameters
frequenciesnumpy array

Frequencies

impedancesnumpy array of dtype ‘complex128’

Impedances

circuitstring

String defining the equivalent circuit to be fit

initial_guesslist of floats

Initial guesses for the fit parameters

constantsdictionary, optional

Parameters and their values to hold constant during fitting (e.g. {“RO”: 0.1}). Defaults to {}

bounds2-tuple of array_like, optional

Lower and upper bounds on parameters. Defaults to bounds on all parameters of 0 and np.inf, except the CPE alpha which has an upper bound of 1

weight_by_modulusbool, optional

Uses the modulus of each data (|Z|) as the weighting factor. Standard weighting scheme when experimental variances are unavailable. Only applicable when global_opt = False

global_optbool, optional

If global optimization should be used (uses the basinhopping algorithm). Defaults to False

kwargs

Keyword arguments passed to scipy.optimize.curve_fit or scipy.optimize.basinhopping

Returns
p_valueslist of floats

best fit parameters for specified equivalent circuit

p_errorslist of floats

one standard deviation error estimates for fit parameters

Notes

Need to do a better job of handling errors in fitting. Currently, an error of -1 is returned.

impedance.models.circuits.fitting.extract_circuit_elements(circuit)[source]

Extracts circuit elements from a circuit string.

Parameters
circuitstr

Circuit string.

Returns
extracted_elementslist

list of extracted elements.

impedance.models.circuits.fitting.rmse(a, b)[source]

A function which calculates the root mean squared error between two vectors.

Notes

$RMSE = \sqrt{\frac{1}{n}(a-b)^2}$
impedance.models.circuits.fitting.set_default_bounds(circuit, constants={})[source]

This function sets default bounds for optimization.

set_default_bounds sets bounds of 0 and np.inf for all parameters, except the CPE and La alphas which have an upper bound of 1.

Parameters
circuitstring

String defining the equivalent circuit to be fit

constantsdictionary, optional

Parameters and their values to hold constant during fitting (e.g. {“RO”: 0.1}). Defaults to {}

Returns
bounds2-tuple of array_like

Lower and upper bounds on parameters.

impedance.models.circuits.fitting.wrapCircuit(circuit, constants)[source]

wraps function so we can pass the circuit string