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.
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. .. ..
About impedance.py
How to get started with python Check it out with "how to use python"
As of 2020/07/22
Since it is registered in PyPI, install it with pip install impedance
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
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.
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
This time, we will create the following equivalent circuit model as an example.
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 used
CustomCircuit this time, but if you look at the source code,
Randles` is also prepared by default.
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.
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()
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)
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