Introduction
This Python module is for fitting electrical models to measured impedance data. It also includes a command-line program to assist with automated fitting.
Installation
To install from pypi use:
$ pip install zfitpy
To install from sources use:
$ pip install .
zfit
Here's an example of how model fitting can be performed using a Python script:
from zfitpy import zfit
net = "(CPE('K', 'alpha') | R('R2')) + R('R1')"
ranges = {'R1': (1e-3, 1e3), 'K': (1e-3, 1e3), 'alpha': (-1, 1), 'R2': (100, 1e4)}
data, fitmodel = zfit('E4990A-example1.csv', net, ranges, Ns=10)
print(fitmodel)
print(fitmodel.error)
Here Ns specifies the number of steps in each search range for a brute force search. It can be explicitly defined for each search range, for example,
ranges = {'R1': (1e-3, 1e3, 10), 'K': (1e-3, 1e3, 20), 'alpha': (-1, 1), 'R2': (100, 1e4)}
The default optimizer is brute force, however, this is only suitable for models with few parameters and few steps in each search range. The optimizer can be changed with the method argument to zfit, for example,
data, fitmodel = zfit('E4990A-example1.csv', net, ranges, method='trf')
method can be 'brute', 'trf', or 'dogbox'.
The fit between the measured data and best-fit model can be plotted using:
from zfitpy import Plotter
plotter = Plotter()
plotter.fit(data, fitmodel)
The error between the measured data and best-fit model can be plotted using:
from zfitpy import Plotter
plotter = Plotter()
plotter.error(data, fitmodel)
The model can be drawn using:
fitmodel.draw()
The draw method takes an optional filename to save the drawing. Supported file formats are 'pdf', 'jpg', 'png', and 'pgf', for example,
fitmodel.draw('model.pdf')
Caveats
A parameter name cannot be a substring of another parameter name, i.e., 'R' cannot be used if there is a parameter 'R1'.
zfitpy
zfitpy is a command-line Python program. It is designed for fitting electrical models to impedance data. For example:
$ zfitpy --net "L('L1') + (R('R1') | (L('L2') + R('R2')))" --ranges="{'R1':(0,5e3),'L1':(1e-3,20e-3),'R2':(0,0.1),'L2':(1e-3,20e-3)}" --input demo/E4990A-example1.csv --plot-error
The network is specified using Lcapy notation for networks. This example uses a network comprised of a parallel combination of RL series networks. The network can be drawn using:
$ zfitpy --net "L('L1') + (R('R1') | (L('L2') + R('R2')))" --draw
The network in this example has four parameters: R1, L1, R2, and
L2. A brute force search is performed for each component using the
specified ranges; this is refined with a finishing search. The ranges
are specified as a Python dictionary, keyed by component name, with
the range for each component specified as a tuple. The number of
steps in each range is 20 can be altered with the --steps option.
The impedance of the data and model can be plotted using:
$ zfitpy --plot-fit --net "L('L1') + (R('R1') | (L('L2') + R('R2')))" --ranges="{'R1':(0,5e3),'L1':(1e-3,20e-3),'R2':(0,0.1),'L2':(1e-3,20e-3)}" --input demo/E4990A-example1.csv
The impedance error between the data and model can be plotted using:
$ zfitpy --plot-error --net "L('L1') + (R('R1') | (L('L2') + R('R2')))" --ranges="{'R1':(0,5e3),'L1':(1e-3,20e-3),'R2':(0,0.1),'L2':(1e-3,20e-3)}" --input demo/E4990A-example1.csv
Here's another network using a constant phase element (CPE).
$ zfitpy --net "(CPE('K', 'alpha') | R('R2')) + R('R1')" --draw
$ zfitpy --plot-error --net "(CPE('K', 'alpha') | R('R2')) + R('R1')" --ranges="{'R1':(0,1e3),'K':(1e-3,1e3),'alpha':(-1,1),'R2':(1e2,1e4)}" --input demo/E4990A-example1.csv
The data format for the plots depends on the extension. matplotlib is used for the plotting and so the pdf, png, pgf, and jpg formats are all supported. For example:
$ zfitpy --net "CPE('K', 'alpha')" --draw --output CPE.png
The data can be plotted without fitting if the ranges option is not specified. For example:
$ zfitpy --plot-data --input demo/E4990A-example1.csv
A Nyquist plot is generated if the --nyquist option is specified. Magnitude and phase is plotted is the --magphase option is specified. The plot style can be altered using the --style option to specify a Matplotlib style file.
Other command line options for zfitpy can be found with the --help option.
Here's another example that loads the network and ranges from files. It uses a Matplotlib style file and annotates the title with the model, the optimization method, and the rms error.
$ zfitpy --net RL2.net --ranges=RL2.ranges --input data/data.csv --plot-error --method='brute' --title='%model, %method, %rmse' --style=z.mplstyle --output RL2-brute.pdf
Metadata
Release files for zfitpy 0.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| zfitpy-0.6.1.tar.gz | 15.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| zfitpy-0.6.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.8 kB
Release files / zfitpy-0.6.1.tar.gz
| Download URL | zfitpy-0.6.1.tar.gz |
|---|---|
| Size | 15.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ec34c4ab33ee8a500f1fcf420109180f62e5e165059620ae840f98e87e849965
|
|
BLAKE2b-256 checksum How to use checksums |
ba872908c0f132fc51da13db682739ebda2940c1e02fb7ad417111d855fb02ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Release files / zfitpy-0.6.1-py3-none-any.whl
| Download URL | zfitpy-0.6.1-py3-none-any.whl |
|---|---|
| Size | 17.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a7e7836c0c334817d0ffb1e19ee0b3edda7c36583f08daedc20c14902a637d9d
|
|
BLAKE2b-256 checksum How to use checksums |
e82a34e0d690ba17c8adbd1d35d8295d6d3a814af6e7259cc13c8cd59773a8fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|