Skip to content

About

EISFitpython is a Python module that performs simultaneous χ²-based fitting of multiple impedance spectra using a single, unified cost function. This approach enhances the reliability of parameter correlation estimates and reduces standard errors, leading to more robust and confident parameter determination.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

EISFitpython

EISFitpython is a Python package for fitting equivalent circuits to electrochemical impedance spectroscopy (EIS) data. You write the circuit as a text string, for example (R1|Q1)+(R2|Q2)+Q3. You can fit one spectrum, a batch of spectra, or a whole temperature series under a single cost function. In a series fit each parameter is either global (one value shared by all spectra) or local (one value per spectrum).

Why fit the whole series

The impedance of solid ion conductors is usually measured at several temperatures, and each spectrum is usually fitted on its own. That works at low temperature. As the temperature rises, the bulk response moves out of the measured frequency window and the bulk and grain-boundary arcs merge, so a single spectrum no longer determines the high-frequency parameters. Their standard errors grow, and the activation energy derived from them can be biased.

EISFitpython fits the whole series at once. Parameters that should not change with temperature, such as the bulk CPE parameters, are fitted as global. Thermally activated resistances stay local. All standard errors and correlations then come from one covariance matrix.

The package is written for researchers who measure variable-temperature impedance of solid ion conductors and want a scripted, reproducible analysis in Python.

What it does

  • Reads circuit strings: + for series, | for parallel, parentheses for nested groups
  • Supports resistor, capacitor, inductor, constant phase element, Warburg, finite-length Warburg, Gerischer and finite-length Gerischer
  • Fits a single spectrum, a batch, or a temperature series with global and local parameters
  • Uses modulus, proportional or unit weighting with scipy.optimize.curve_fit (Levenberg-Marquardt by default)
  • Reports fitted parameters, standard errors, the correlation matrix and fit statistics (chi-squared, R-squared, AICc)
  • Calculates conductivity, activation energy and effective capacitance
  • Gives the error of the effective capacitance by a method you select: bootstrap refits, error propagation with the covariance matrix, or standard error propagation
  • Reads NEISYS, text and CSV files
  • Draws Nyquist, Bode and modulus plots as vector graphics

Installation

Requires Python 3.9 or higher. NumPy, SciPy, Matplotlib and pandas are installed automatically.

git clone https://github.com/Sheyigold/EISFitpython.git
cd EISFitpython
python -m pip install .          # add -e for an editable/development install

After installation the package is importable as EISFitpython, for example from EISFitpython import data_extraction as dt. See docs/installation.md for other options.

Quick start

Both examples use the measured spectra in EIS_Data/ and run from the repository root. Reports and figures are written to the working directory.

Fit one spectrum

from EISFitpython import data_extraction as dt
from EISFitpython import EISFit_main as em

files = dt.get_eis_files(base_path='EIS_Data', subfolder='Example-2')
f, Z = dt.readNEISYS(files[1])                    # NEISYS spectrometer file
f, Z = dt.trim_data(f, Z, fmin=5, fmax=None)      # keep the points at 5 Hz and above

# Your own data: dt.readTXT('my_data.txt') or dt.readCSV('my_data.csv')
# for three columns f, Z', Z"

circuit = "(R1|Q1)+(R2|Q2)+Q3"                    # bulk, grain boundary, electrode
params = [4.8e4, 1.3e-10, 0.8, 9.2e4, 1.3e-9, 0.9, 5.7e-7, 0.6]   # starting values
UB = []  # Upper bounds
LB = []  # Lower bounds

popt, perror, Z_fit = em.full_EIS_report(
    f, Z, params, circuit,
    UB=UB, LB=LB,
    weight_mtd='M',      # Modulus weighting
    method='lm',         # Levenberg-Marquardt algorithm
    single_chi='No',
    plot_type='both'     # Nyquist and Bode plots
)

Fit a temperature series with global and local parameters

A parameter is global or local according to its starting value. A single number is shared by all spectra. A list with one value per spectrum is fitted separately for each.

import numpy as np
from EISFitpython import data_extraction as dt
from EISFitpython import singlechi as sc

filenames = dt.get_eis_files(base_path='EIS_Data', subfolder='Example-3-4')
f, Z = dt.stack_NEISYS_files(filenames)           # seven spectra, stacked

temps = np.array([140, 150, 160, 170, 180, 190, 200])  # in Celsius
N = len(temps)

circuit = "(R1|Q1)+(R2|Q2)+Q3"
params = (
    [1.8e5, 1.3e5, 9.8e4, 7.1e4, 5.3e4, 3.2e4, 2.0e4],  # R1: one value per temperature, local
    1.3e-11,                                             # Q1: single number, global
    0.9,                                                 # n1: single number, global
    [2.2e6, 1.2e6, 7.1e5, 4.2e5, 2.5e5, 1.6e5, 1.0e5],  # R2: local
    [1.48e-10] * N,                                      # Q2: local
    [0.9] * N,                                           # n2: local
    [1.27e-6] * N,                                       # Q3: local
    [0.5] * N,                                           # n3: local
)

fit_params, fit_perror, Z_fit = sc.Single_chi_report(f, Z, params, temps, circuit, weight_mtd='M')

The fit returns 44 parameters: six local parameters for each of the seven spectra, and the two global parameters Q1 and n1. Example_scripts/Example4 continues from here with the Arrhenius analysis and the effective capacitance.

Documentation

The documentation site (source in docs/) has the theoretical background, the user guides, a description of the output files and an API reference for every public function. To build it locally, run python -m pip install -e ".[docs]" and then mkdocs serve.

Examples

Example_scripts/ has four worked examples with measured data:

  • Example1/Example1-simulate.py: circuit simulation
  • Example2/Example2-EISfit.py: fit of one spectrum
  • Example3/Example3-Batchfit.py: batch fit of a temperature series with Arrhenius analysis
  • Example4/Example4-Singlechi.py: global-local fit under one cost function

Each folder holds the reports and figures that its script produces.

Testing

python -m pip install -e ".[test]"
python -m pytest tests

The test suite has one file per module. It checks the element impedances and the circuit parser against formulas written out by hand, the fit on data with known parameters, the fit statistics, the uncertainty estimates and the derived quantities. tests/test_examples.py repeats the fits of Examples 2 to 4 and compares them with the stored reports.

Contributing and support

To report a problem, open an issue on GitHub and include the circuit string, the fit settings, the package version and a small data file that reproduces it. Questions go to the issue tracker as well.

To contribute code, fork the repository, add a test for each change and run python -m pytest tests. Open a pull request when all tests pass and say why the change is needed.

Citation

If you use this code in your research, please cite:

S. C. Adediwura, N. Mathew, J. Schmedt auf der Günne, J. Mater. Chem. A 12 (2024) 15847–15857.
https://doi.org/10.1039/d3ta06237f

License

EISFitpython is distributed under the MIT License. See LICENSE.txt.

Author

Sheyi Clement Adediwura (c) 2024

About

EISFitpython is a Python module that performs simultaneous χ²-based fitting of multiple impedance spectra using a single, unified cost function. This approach enhances the reliability of parameter correlation estimates and reduces standard errors, leading to more robust and confident parameter determination.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages