Impedance analysis (EIS) with python [impedance.py]

Overview

Introducing the library ** impedance.py ** that can analyze impedance (electrochemical impedance) with python. Fit the measured Nyquist plot to the constructed equivalent circuit model and get the parameters. No need for paid software, so you can use it easily. Basically, I will explain according to the Official Tutorial of impedance.py, and I will supplement it in some places, so I want to read English a lot. For those who do not. The code is put together at the bottom for those who do not need explanation.

Task

I want to analyze impedance using only open source tools

The analysis software that accompanies the measuring device is required for electrochemical impedance analysis, but it is troublesome to analyze each time with a measuring PC. .. .. However, most of the software is paid, and due to the license, it cannot be installed on your own PC. .. ..

What to do and what not to do in this article

To handle

About impedance.py

Do not handle

How to get started with python Check it out with "how to use python"

Dependent environment

As of 2020/07/22

(1) Installation

Since it is registered in PyPI, install it with pip install impedance

(2) Data import [preprocessing]

For measurement data, you can usually read_csv csv and treat it as pandas.Dataframe, but since there is something called preprocessing, I will use it. Since sample data is prepared from the official, download example.csv in the Step 2 column from ** here **

python


from impedance import preprocessing

frequencies, Z = preprocessing.readCSV('./exampleData.csv')
#type(frequencies): <class 'numpy.ndarray'>
#type(frequencies[0]): <class 'numpy.float64'>
#type(Z): <class 'numpy.ndarray'>
#type(Z[0]): <class 'numpy.complex128'>

The contents of exampleData.csv look like this キャプチャ.JPG Column A is the frequency, column B is the real part of Z, column C is the imaginary part of Z, and even if you look at the source code of preprocessing.readCSV, you simply set column A to frequencies and column BC to complex type Z and returned. It was just.

In fact, convenient preprocessing! !!

If you think it's a useless function, there are actually functions that you can use! preprocessing also supports extensions specific to device software (** important here **), and seems to support the following software and extensions.

soft extension
gamry .dta
autolab Comma delimited.csv?)
parstat .txt
zplot .z
versastudio .par
powersuite .txt
biologic .mpt
chinstruments .txt

Looking at the source code, I am very happy because it processes the data using regular expressions etc .: clap: (I used it well with the .mpt file) The usage is as follows

python


from impedance import preprocessing

frequencies, Z = preprocessing.readFile('Path to file', instrument='Software name')
frequencies, Z = preprocessing.ignoreBelowX(frequencies, Z)

You can specify the file format by passing the software name in the above table to the instrument option.

Other than that ・ Cut out the first quadrant preprocessing.ignoreBelowX (freq, Z) -Cut data by frequency preprocessing.cropFrequencies (freq, Z, freqmin = 0, freqmax = None) Is prepared

(3) Creation of equivalent circuit

This time, we will create the following equivalent circuit model as an example. two_time_constants.png

python


from impedance.models.circuits import CustomCircuit

circuit = 'R0-p(R1,C1)-p(R2-Wo1,C2)'
initial_guess = [.01, .01, 100, .01, .05, 100, 1]

circuit = CustomCircuit(circuit, initial_guess=initial_guess)

An equivalent circuit can be created by expressing series with - and parallel withp (,)and passing it as a character string to the CustomCircuit class. At this time, the initial parameters must be passed as a list type to the ʻinitial_guessoption. The circuit elements that can be expressed are summarized in [** here **](https://impedancepy.readthedocs.io/en/latest/circuit-elements.html). I usedCustomCircuit this time, but if you look at the source code, Randles` is also prepared by default.

(4) Fitting

python


circuit.fit(frequencies, Z)

params = circuit.parameters_
#[1.65187261e-02 8.67655045e-03 3.32142565e+00 5.38996281e-03
# 6.30927436e-02 2.32520436e+02 2.19541831e-01]

covs = circuit.conf_
#[1.54227642e-04 1.91273738e-04 1.89536697e-01 2.05799010e-04
# 1.93973976e-03 1.62269546e+01 1.75432523e-02]

Perform the fitting with circuit.fit. It seems that scipy.optimize.curve_fit is used as the fitting algorithm. After fitting, the parameters after fitting and the variance of the parameters in circuit.parameters_ and circuit.conf_, respectively (the diagonal components of the covariance matrix returned by scipy.optimize.curve_fit are extracted). Can be received.

(5) Visualization of results

python


import matplotlib.pyplot as plt
from impedance.visualization import plot_nyquist

Z_fit = circuit.predict(frequencies)

fig, ax = plt.subplots()
plot_nyquist(ax, Z, fmt='o')
plot_nyquist(ax, Z_fit, fmt='-')

plt.legend(['Data', 'Fit'])
plt.show()

example_fit_fig.png

Gets the simulation result calculated from the parameters fitted with circuit.predict (frequencies). Visualization provides visualization based on matplotlib and altair, and nyquist plots are displayed with plot_nyquist. They will also write the axis label carefully. In addition to this, visualization has various items such as plot_bode (bode plot). (Scheduled to be added)

code

python


from matplotlib import pyplot as plt

from impedance import preprocessing
from impedance.models.circuits import CustomCircuit
from impedance.visualization import plot_nyquist


def main():
    frequencies, Z = preprocessing.readCSV('./exampleData.csv')
    frequencies, Z = preprocessing.ignoreBelowX(frequencies, Z)

    circuit = 'R0-p(R1,C1)-p(R2-Wo1,C2)'
    initial_guess = [.01, .01, 100, .01, .05, 100, 1]

    circuit = CustomCircuit(circuit, initial_guess=initial_guess)
    circuit.fit(frequencies, Z)
    print(circuit.parameters_)
    print(circuit.conf_)
    Z_fit = circuit.predict(frequencies)

    fig, ax = plt.subplots()
    plot_nyquist(ax, Z, fmt='o')
    plot_nyquist(ax, Z_fit, fmt='-')

    plt.legend(['Data', 'Fit'])
    plt.show()


if __name__ == '__main__':
    main()

Recommended Posts

Impedance analysis (EIS) with python [impedance.py]
Voice analysis with python
Voice analysis with python
Data analysis with Python
[Python] Morphological analysis with MeCab
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Sentiment analysis with Python (word2vec)
Planar skeleton analysis with Python
Japanese morphological analysis with Python
Muscle jerk analysis with Python
3D skeleton structure analysis with Python
Text mining with Python ① Morphological analysis
Data analysis starting with python (data visualization 1)
Logistic regression analysis Self-made with python
Data analysis starting with python (data visualization 2)
[In-Database Python Analysis Tutorial with SQL Server 2017]
Marketing analysis with Python ① Customer analysis (decyl analysis, RFM analysis)
Machine learning with python (2) Simple regression analysis
2D FEM stress analysis program with Python
Tweet analysis with Python, Mecab and CaboCha
Principal component analysis with Power BI + Python
Data analysis starting with python (data preprocessing-machine learning)
Two-dimensional unsteady heat conduction analysis with Python
Python: Simplified morphological analysis with regular expressions
Statistics with python
Python with Go
Data analysis python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
Medical image analysis with Python 1 (Read MRI image with SimpleITK)
Static analysis of Python code with GitLab CI
Easy Lasso regression analysis with Python (no theory)
Two-dimensional elastic skeleton geometric nonlinear analysis with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Python: Time Series Analysis
Scraping with Python (preparation)
Text mining with Python ① Morphological analysis (re: Linux version)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Data analysis for improving POG 1 ~ Web scraping with Python ~
Run Python with VBA
Handling yaml with python
Serial communication with python
Basket analysis with Spark (1)
Learning Python with ChemTHEATER 05-1