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:
circuit : str

Circuit string.

Returns:
length : int

Length of circuit.

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

Checks if an element is valid, then evaluates it.

Parameters:
element : str

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:
frequencies : numpy array

Frequencies

impedances : numpy array of dtype ‘complex128’

Impedances

circuit : string

String defining the equivalent circuit to be fit

initial_guess : list of floats

Initial guesses for the fit parameters

constants : dictionary, optional

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

bounds : 2-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_modulus : bool, 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_opt : bool, 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_values : list of floats

best fit parameters for specified equivalent circuit

p_errors : list 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:
circuit : str

Circuit string.

Returns:
extracted_elements : list

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:
circuit : string

String defining the equivalent circuit to be fit

constants : dictionary, optional

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

Returns:
bounds : 2-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