Fitting

pypalmsens.fitting

This module contains the public api for circuit fitting.

Classes:

  • Parameter – Set or update Parameter attributes.

  • Parameters – Tuple-like container class for parameters.

  • FitResult – Container for fitting results.

  • CircuitModel – Fit an equivalent circuit model.

Parameter

Parameter(symbol, value=None, min=None, max=None, fixed=None)

Set or update Parameter attributes.

Attributes:

  • symbol (str) – Name of the parameter (not used in minimization).

  • value (link:#typing.Optional[link:#float]) – Initial value of the parameter.

  • min (link:#typing.Optional[link:#float]) – Minimum (lower bound) for the parameter.

  • max (link:#typing.Optional[link:#float]) – Maximum (upper bound) for the parameter.

  • fixed (link:#typing.Optional[link:#bool]) – If True, fix the value for this parameter.

symbol

symbol: str

Name of the parameter (not used in minimization).

value

value: Optional[float] = None

Initial value of the parameter.

min

min: Optional[float] = None

Minimum (lower bound) for the parameter.

max

max: Optional[float] = None

Maximum (upper bound) for the parameter.

fixed

fixed: Optional[bool] = None

If True, fix the value for this parameter.

Parameters

Parameters(cdc)

Tuple-like container class for parameters.

This class is instantiated from the CDC code and contains default parameters. This ensures that the length and type of parameters match that of CircuitModel. Update the parameters in this class and pass to CircuitModel.fit().

Parameters:

  • cdc (str) – Genererate fitting parameters for this CDC.

Attributes:

  • cdc – CDC code used to generate parameter listing.

cdc

cdc = cdc

CDC code used to generate parameter listing.

FitResult

FitResult(cdc, parameters, error, chisq, n_iter, exit_code)

Container for fitting results.

Functions:

Attributes:

  • cdc (str) – Circuit model CDC values.

  • parameters (link:#list[link:#float]) – Optimized parameters for CDC.

  • error (link:#list[link:#float]) – Error (%) on parameters.

  • chisq (float) – Chi-squared goodness of fit statistic.

  • n_iter (int) – Total number of iterations.

  • exit_code (str) – Exit code for the minimization.

cdc

cdc: str

Circuit model CDC values.

parameters

parameters: list[float]

Optimized parameters for CDC.

error

error: list[float]

Error (%) on parameters.

chisq

chisq: float

Chi-squared goodness of fit statistic.

n_iter

n_iter: int

Total number of iterations.

exit_code

exit_code: str

Exit code for the minimization.

from_psfitresult

from_psfitresult(result, cdc)

Construct fitresult from SDK FitResult.

from_eisdata

from_eisdata(data)

Construct fitresulf from EISData.

get_psmodel

get_psmodel(data)

Get SDK Circuit model object

get_nyquist

get_nyquist(data)

Calculate observed and calculated nyquist curves.

Parameters:

  • data (EISData) – Input EIS data.

Returns:

  • calc, meas : tuple[Curve, Curve] – Returns the nyquist curve calculated from the model parameters and the measured curve from the EIS data.

get_bode_z

get_bode_z(data)

Calculate observed and calculated Bode curve Z vs Frequency.

Parameters:

  • data (EISData) – Input EIS data.

Returns:

  • calc, meas : tuple[Curve, Curve] – Returns the nyquist curve calculated from the model parameters and the measured curve from the EIS data.

get_bode_phase

get_bode_phase(data)

Calculate observed and calculated Bode curve phase vs Frequency.

Parameters:

  • data (EISData) – Input EIS data.

Returns:

  • calc, meas : tuple[Curve, Curve] – Returns the nyquist curve calculated from the model parameters and the measured curve from the EIS data.

plot_nyquist

plot_nyquist(data)

Make nyquist plot.

Parameters:

  • data (EISData) – Input EIS data.

Returns:

  • fig (Figure) – Returns matplotlib figure object. use fig.show() to render plot.

plot_bode

plot_bode(data)

Make bode plot.

Parameters:

  • data (EISData) – Input EIS data.

Returns:

  • fig (Figure) – Returns matplotlib figure object. use fig.show() to render plot.

CircuitModel

CircuitModel(cdc, algorithm='leastsq', max_iterations=500, min_delta_error=1e-09, min_delta_step=1e-12, min_freq=None, max_freq=None, tolerance=0.0001, lambda_start=0.01, lambda_factor=10.0, _last_result=None, _last_psfitter=None)

Fit an equivalent circuit model.

The class takes a CDC string as a required argument to set up the model.

The other parameters are optional and can be used to tweak the minimization. The model supports fitting over a specified frequency range and adjustment of exit conditions (i.e. max # iterations, min delta error, min parameter step size).

Optionally you can change the initial values of the parameters, their min/max bounds or fix their value.

Example:

model = CircuitModel('R(RC)')
result = model.fit(eis_data)

Functions:

  • default_parameters – Get default parameters. Use this to modify parameter values.

  • fit – Fit circuit model.

Attributes:

  • cdc (str) – Sets the circuit specified in the CDC string.

  • algorithm (link:#typing.Literal[‘leastsq’, ‘nelder-mead’]) – Name of the fitting method to use.

  • max_iterations (int) – Maximum number of iterations.

  • min_delta_error (float) – Minimum convergence error.

  • min_delta_step (float) – Minimum convergence step.

  • min_freq (link:#typing.Optional[link:#float]) – Minimum fitting frequency in Hz.

  • max_freq (link:#typing.Optional[link:#float]) – Maximum fitting frequency in Hz.

  • tolerance (float) – Convergence tolerance. Nelder-Mead only (default = 1e-4).

  • lambda_start (float) – Start lambda value. Levenberg-Marquardt only (default = 0.01).

  • lambda_factor (float) – Lambda Scaling Factor. Levenberg-Marquardt only (default = 10).

  • last_result – Store last fit result.

  • last_psfitter – Store reference to last SDK fitting object.

cdc

cdc: str

Sets the circuit specified in the CDC string.

algorithm

algorithm: Literal['leastsq', 'nelder-mead'] = 'leastsq'

Name of the fitting method to use.

Valid values are: leastsq (Levenberg-Marquardt), nelder-mead

max_iterations

max_iterations: int = 500

Maximum number of iterations.

Minimization terminates once it reaches this number of steps (default = 500).

min_delta_error

min_delta_error: float = 1e-09

Minimum convergence error.

Minimization converges if the residual (squared difference) falls below this value (default = 1e-9).

min_delta_step

min_delta_step: float = 1e-12

Minimum convergence step.

Minimization converges if the difference in parameter values falls below this value (default = 1e-12).

min_freq

min_freq: Optional[float] = None

Minimum fitting frequency in Hz.

max_freq

max_freq: Optional[float] = None

Maximum fitting frequency in Hz.

tolerance

tolerance: float = 0.0001

Convergence tolerance. Nelder-Mead only (default = 1e-4).

lambda_start

lambda_start: float = 0.01

Start lambda value. Levenberg-Marquardt only (default = 0.01).

lambda_factor

lambda_factor: float = 10.0

Lambda Scaling Factor. Levenberg-Marquardt only (default = 10).

last_result

last_result

Store last fit result.

last_psfitter

last_psfitter

Store reference to last SDK fitting object.

default_parameters

default_parameters()

Get default parameters. Use this to modify parameter values.

Returns:

  • parameters (Parameters) – Default parameters for CDC.

fit

fit(data, *, parameters=None)

Fit circuit model.

Parameters:

  • data (EISData) – Input data.

  • parameters (link:#typing.Optional[link:#collections.abc.Sequence[link:#float] | link:#pypalmsens._fitting.Parameters]) – Optional initial parameters for fit. Can be passed as Parameters object or list of values.

Returns:

  • result (FitResult) – Returns dataclass with fit results. Can also be accessed via .last_result.