Skip to content

Fitting one spectrum

This guide shows how to read one spectrum and fit a circuit to it. The fit function writes a report, the fitted curve and the figures.

A complete example

The example uses a measured spectrum from the repository. Run it from the repository root.

from EISFitpython import data_extraction as data
from EISFitpython import EISFit_main as fit

files = data.get_eis_files("EIS_Data", subfolder="Example-2")
f, Z = data.readNEISYS(files[1])
f, Z = data.trim_data(f, Z, fmin=5.0)        # fit the points at 5 Hz and above

circuit = "(R1|Q1)+(R2|Q2)+Q3"
initial = [4.8e4, 1.3e-10, 0.8, 9.2e4, 1.3e-9, 0.9, 5.7e-7, 0.6]

popt, perror, Z_fit = fit.full_EIS_report(
    f,
    Z,
    initial,
    circuit,
    UB=[],                # no upper bounds
    LB=[],                # no lower bounds
    weight_mtd="M",       # modulus weighting
    method="lm",          # Levenberg-Marquardt
    single_chi="No",
    plot_type="both",     # Nyquist and Bode figures
)

initial contains one start value for each circuit parameter. For the order of the values, see Circuit models.

The function returns three results:

  • popt: the fitted parameters,
  • perror: their standard errors,
  • Z_fit: the fitted impedance.

The function prints the report. It also writes the report, the fitted data and the figures into the working directory (see Generated files).

A fit needs good start values. Read each resistance from the Nyquist plot: it is the width of the related arc. Start each CPE exponent near 0.9.

Weighting

The fit minimizes the sum of the squared differences between the data and the model. The weighting sets the influence of each point on this sum.

Code Name The fit divides each difference by Effect
M Modulus \(\lvert Z\rvert\) of that point Points with small impedance and points with large impedance have equal influence. This is the default
P Proportional \(Z'\) for the real part, \(Z''\) for the imaginary part The relative error of each part is equal for all points. The fit fails if one value of \(Z'\) or \(Z''\) is zero
U Unit 1 No weighting. The points with the largest impedance control the fit

For the related equations, see the theoretical background.

Bounds and the fit method

The fit function gives method to scipy.optimize.curve_fit.

  • method="lm" (Levenberg-Marquardt) fits without bounds. Give empty lists for UB and LB. If you give bounds, SciPy stops with an error.
  • method="trf" and method="dogbox" use a lower bound and an upper bound for each parameter. LB and UB have the same order as the parameters.
lower = [0.0, 0.0, 0.0, 0.0]
upper = [1e9, 1e9, 1.0, 1e3]

fit_stats, popt, perror, corr = fit.EISFit(
    f,
    Z,
    params=[5.0, 20.0, 1e-6, 0.9],
    circuit="R1+(R2|Q1)",
    UB=upper,
    LB=lower,
    weight_mtd="M",
    method="trf",
)

Examine a fit with bounds

The parameters of a circuit have very different sizes. A resistance can be 10⁵ Ω, and a CPE coefficient can be 10⁻¹⁰. A method with bounds can then stop before the minimum, and it gives no error message.

For the example spectrum above, the fit without bounds gives a chi-squared of 0.0043. The same fit with bounds stops at 0.53 with trf and at 8.0 with dogbox.

Before you use the parameters of a fit with bounds, compare its chi-squared with the chi-squared of a fit without bounds.

Fit statistics

EISFit returns a dictionary with these items:

Key Meaning
chi2 Sum of the squared, weighted differences between the data and the model
R_chi2 Reduced chi-squared: chi2 divided by the degrees of freedom
dof Degrees of freedom: two times the number of frequencies, minus the number of parameters
R_squared Coefficient of determination. It uses the same weights as chi2
adj_R_squared R_squared with a correction for the number of parameters
AICc Corrected Akaike information criterion, \(N\ln(\chi^2/N) + 2k + 2k(k+1)/(N-k-1)\), with \(N\) data values and \(k\) parameters
RMSE Root mean square of the weighted differences
SER Square root of the reduced chi-squared
MAE Mean of the absolute weighted differences
F_statistic, p_value F statistic of the fit and its p-value
conf_intervals Half of the width of the 95 % confidence interval of each parameter
N_points, N_params Number of data values (real parts and imaginary parts) and number of parameters
pcov Covariance matrix of the fitted parameters

Two notes help you to read these numbers:

  • With modulus weighting or proportional weighting, the differences are relative. RMSE is then a fraction of the impedance, not a value in ohms. An RMSE of 0.007 is a typical deviation of 0.7 %.
  • AICc compares different circuits. Fit them to the same data with the same weighting, and prefer the circuit with the lower value. One value alone has no meaning.

The three fit functions

Function Operation
full_EIS_report Fits and prints the report. Writes the report, the fitted data and the figures
fit_report Fits and prints the parameter table and the correlation matrix. Writes no files
EISFit Fits and returns the statistics, the parameters, their standard errors and the correlation matrix. Prints nothing

For a temperature series, see Batch and Arrhenius analysis and Global-local fitting.