API reference¶
fasteis.Element
¶
A single circuit element.
Each variant is a subclass taking its fields as arguments.
They are combined with Series() and Parallel().
R
¶
C
¶
L
¶
La
¶
Cpe
¶
Bases: Element
Constant phase element, Z = 1 / (q * (jw) ** alpha).
Reduces to a capacitor of q farads at alpha = 1, and to a resistor
of 1 / q ohm at alpha = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
float
|
Admittance prefactor in |
required |
alpha
|
float
|
Fractional exponent in [0, 1]. |
required |
W
¶
Wo
¶
Ws
¶
G
¶
Gs
¶
K
¶
Zarc
¶
Bases: Element
Depressed semicircle, Z = r / (1 + (jw*tau_k) ** gamma).
A resistor and constant phase element in parallel, as a single element.
Reduces to K at gamma = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
float
|
Polarisation resistance in ohm. |
required |
tau_k
|
float
|
Relaxation time constant in s. |
required |
gamma
|
float
|
Depression exponent in [0, 1]. |
required |
Tlmq
¶
Bases: Element
Transmission line with a constant phase element surface impedance.
Z = sqrt(r_ion*zs) / tanh(sqrt(r_ion/zs)) for a distributed pore of
surface impedance zs = 1 / (qs * (jw) ** gamma).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r_ion
|
float
|
Ionic pore resistance in ohm. |
required |
qs
|
float
|
Surface admittance prefactor in |
required |
gamma
|
float
|
Fractional exponent in [0, 1]. |
required |
T
¶
Bases: Element
General transmission line.
Z = a*coth(beta)/beta + b*cosech(beta)/beta, where
beta = sqrt(k_tau + jw*tau).
From Electrochimica Acta 38, 2653 (1993), DOI: 10.1016/0013-4686(93)85083-B
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
Coefficient of the coth term, in |
required |
b
|
float
|
Coefficient of the cosech term, in |
required |
k_tau
|
float
|
Charge-transfer rate constant times |
required |
tau
|
float
|
Transport time constant in s. |
required |
fasteis.Circuit
¶
An equivalent circuit: elements in series and parallel, with their values.
Build one by parsing a topology string, or by composing elements with
Series() and Parallel().
__init__
¶
__init__(s: str) -> None
Parse a circuit topology string, e.g. "R0-(R1,CPE1)".
The string carries no parameter values, so every element starts at a
placeholder default. Set real values with with_values(),
with_named_values(), or fit(guess_init=True).
Also accepts the name of a built-in circuit, e.g. "randles"; see
ml_circuits().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Topology string, or the name of a built-in circuit. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
The string is not valid circuit syntax. |
ml_circuits
staticmethod
¶
Built-in circuits that have trained initial-parameter models.
These get a guessed starting point from fit() by default.
guess
¶
guess(
frequencies: _FloatArray | _DataFrame,
impedances: _ComplexArray | None = None,
*,
weights: str | None = None,
) -> list[float]
Machine-learning guess of starting parameters for this topology.
Series and parallel elements may be written in any order and with any
labels: (R1,C2)-R3 uses the same model as R0-(R1,C1). K and Zarc
reach the models trained on the pairs they abbreviate, so R0-Zarc1
uses the R0-(R1,CPE1) model and gets its guess back as r, tau_k
and gamma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frequencies
|
_FloatArray | _DataFrame
|
Frequencies in Hz, or a battery data format dataframe with frequencies and impedances. |
required |
impedances
|
_ComplexArray | None
|
Measured complex impedances in ohm, one per frequency. Leave as None when passing a dataframe. |
None
|
weights
|
str | None
|
Path to a custom |
None
|
Returns:
| Type | Description |
|---|---|
list[float]
|
Starting values, in |
Raises:
| Type | Description |
|---|---|
ValueError
|
No model has been trained for this topology, or
|
param_names
¶
Parameter names, in the order used throughout this class.
with_values() consumes values in this order, and
with_named_values() expects these as keys.
with_values
¶
with_values(values: _FloatArray) -> Circuit
Rebuild this circuit with a new flat parameter vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
_FloatArray
|
One value per parameter, in |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
The wrong number of values was supplied, or a value is
outside |
with_named_values
¶
Rebuild this circuit with parameter values looked up by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
dict[str, float]
|
Values keyed by |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Names are missing or unrecognised, or a value is
outside |
impedance
¶
Impedance of this circuit at each frequency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frequencies
|
_FloatArray | _DataFrame
|
Frequencies in Hz, or a battery data format dataframe to take the frequency column from. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[complex128]
|
Complex impedances in ohm, one per frequency. |
param_bounds
¶
Physical-validity (lo, hi) bounds, in param_names() order.
Fractional exponents are bounded to [0, 1]; every other parameter is
a positive magnitude with hi set to inf.
param_units
¶
Physical units, in param_names() order. "-" means dimensionless.
residuals
¶
residuals(
params: _FloatArray,
frequencies: _FloatArray | _DataFrame,
impedances: _ComplexArray | None = None,
*,
weight: Literal["modulus", "unit"] = "modulus",
) -> list[float]
Weighted residual vector for an arbitrary parameter vector.
The same building block fit() uses internally, exposed so that an
external optimiser such as scipy.optimize.least_squares can be used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
_FloatArray
|
Parameter values, in |
required |
frequencies
|
_FloatArray | _DataFrame
|
Frequencies in Hz, or a battery data format dataframe carrying the whole spectrum. |
required |
impedances
|
_ComplexArray | None
|
Measured complex impedances in ohm. Leave as None when passing a dataframe. |
None
|
weight
|
Literal['modulus', 'unit']
|
Divide each residual by the modulus of the measured point, or leave it unweighted. |
'modulus'
|
Returns:
| Type | Description |
|---|---|
list[float]
|
Real and imaginary parts interleaved, |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
jacobian
¶
jacobian(
params: _FloatArray,
frequencies: _FloatArray | _DataFrame,
impedances: _ComplexArray | None = None,
*,
weight: Literal["modulus", "unit"] = "modulus",
) -> list[list[float]]
Central-difference Jacobian of residuals() at params.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
_FloatArray
|
Parameter values, in |
required |
frequencies
|
_FloatArray | _DataFrame
|
Frequencies in Hz, or a battery data format dataframe with frequencies and impedances. |
required |
impedances
|
_ComplexArray | None
|
Measured complex impedances in ohm. Leave as None when passing a dataframe. |
None
|
weight
|
Literal['modulus', 'unit']
|
Weighting scheme, matching |
'modulus'
|
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
Shape |
list[list[float]]
|
residuals and columns are parameters, as |
list[list[float]]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
fit
¶
fit(
frequencies: _FloatArray | _DataFrame,
impedances: _ComplexArray | None = None,
*,
guess_init: bool | None = None,
weights: str | None = None,
weight: Literal["modulus", "unit"] = "modulus",
method: Literal[
"levenberg_marquardt",
"particle_swarm",
"nelder_mead",
"differential_evolution",
"simulated_annealing",
"basin_hopping",
] = "levenberg_marquardt",
fixed: Mapping[str, float]
| str
| Sequence[str]
| None = None,
max_iterations: int = 200,
ftol: float = 1e-08,
xtol: float = 1e-08,
num_particles: int = 200,
generations: int = 1000,
nelder_mead_iterations: int = 2000,
de_evaluations: int = 20000,
sa_iterations: int = 5000,
sa_initial_temperature: float = 2.0,
basin_hopping_hops: int = 20,
basin_hopping_step_size: float = 1.0,
basin_hopping_temperature: float = 1.0,
seed: int | None = None,
) -> FitResult
Fit this circuit's parameters to a measured spectrum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frequencies
|
_FloatArray | _DataFrame
|
Frequencies in Hz, or a battery data format dataframe with frequencies and impedances. |
required |
impedances
|
_ComplexArray | None
|
Measured complex impedances in ohm. Leave as None when passing a dataframe. |
None
|
guess_init
|
bool | None
|
Left unset, start from a machine-learning guess whenever the circuit was not given explicit starting values, warning and falling back to placeholders if no model has been trained. True always guesses, and raises rather than warns when it cannot. False never guesses. |
None
|
weights
|
str | None
|
Path to a custom |
None
|
weight
|
Literal['modulus', 'unit']
|
Divide each residual by the modulus of the measured point, or leave it unweighted. |
'modulus'
|
method
|
Literal['levenberg_marquardt', 'particle_swarm', 'nelder_mead', 'differential_evolution', 'simulated_annealing', 'basin_hopping']
|
Optimiser to run. |
'levenberg_marquardt'
|
fixed
|
Mapping[str, float] | str | Sequence[str] | None
|
Parameters to hold constant, keyed by |
None
|
max_iterations
|
int
|
Iteration cap for |
200
|
ftol
|
float
|
Cost-change convergence tolerance for |
1e-08
|
xtol
|
float
|
Parameter-change convergence tolerance for
|
1e-08
|
num_particles
|
int
|
Swarm size for |
200
|
generations
|
int
|
Generation cap for |
1000
|
nelder_mead_iterations
|
int
|
Iteration cap for |
2000
|
de_evaluations
|
int
|
Evaluation budget for |
20000
|
sa_iterations
|
int
|
Iteration cap for |
5000
|
sa_initial_temperature
|
float
|
Starting temperature for
|
2.0
|
basin_hopping_hops
|
int
|
Number of hops for |
20
|
basin_hopping_step_size
|
float
|
Perturbation size per hop. |
1.0
|
basin_hopping_temperature
|
float
|
Acceptance temperature for
|
1.0
|
seed
|
int | None
|
Fixes the random draws of the stochastic methods. |
None
|
Returns:
| Type | Description |
|---|---|
FitResult
|
The fitted parameters, their uncertainties, and fit diagnostics. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
fasteis.FitResult
¶
Outcome of Circuit.fit().
circuit
instance-attribute
¶
circuit: Circuit
The fitted circuit, carrying the optimised parameter values.
stderr
instance-attribute
¶
Standard errors of parameters, or None when they cannot be estimated.
Taken from the diagonal of the inverted Gauss-Newton matrix, scaled by
chi_square for each degree of freedom. None when the fit has no spare
degrees of freedom, or when that matrix is singular. NaN for parameters
held by fixed.
impedance_evals
instance-attribute
¶
impedance_evals: int
Number of impedance sweeps calculated during fit, including Jacobians and restarts.