Circuit fitting
pypalmsens.fitting
Define classes for circuit fitting.
Classes:
-
CircuitModel–Fit an equivalent circuit model.
-
FitResult–Container for fitting results.
-
Parameter–Set or update Parameter attributes.
-
Parameters–Tuple-like container class for parameters.
CircuitModel
dataclass
CircuitModel(cdc: str, algorithm: Literal['leastsq', 'nelder-mead'] = 'leastsq', max_iterations: int = 500, min_delta_error: float = 1e-09, min_delta_step: float = 1e-12, min_freq: None | float = None, max_freq: None | float = None, tolerance: float = 0.0001, lambda_start: float = 0.01, lambda_factor: float = 10.0, _last_result: None | FitResult = None, _last_psfitter: None | FitAlgorithm = 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)
Methods:
-
default_parameters–Get default parameters. Use this to modify parameter values.
-
fit–Fit circuit model.
Attributes:
-
algorithm(Literal['leastsq', 'nelder-mead']) –Name of the fitting method to use.
-
cdc(str) –Sets the circuit specified in the CDC string.
-
lambda_factor(float) –Lambda Scaling Factor. Levenberg-Marquardt only (default = 10).
-
lambda_start(float) –Start lambda value. Levenberg-Marquardt only (default = 0.01).
-
last_psfitter–Store reference to last SDK fitting object.
-
last_result–Store last fit result.
-
max_freq(None | float) –Maximum fitting frequency in Hz.
-
max_iterations(int) –Maximum number of iterations.
-
min_delta_error(float) –Minimum convergence error.
-
min_delta_step(float) –Minimum convergence step.
-
min_freq(None | float) –Minimum fitting frequency in Hz.
-
tolerance(float) –Convergence tolerance. Nelder-Mead only (default = 1e-4).
algorithm
class-attribute
instance-attribute
algorithm: Literal['leastsq', 'nelder-mead'] = 'leastsq'
Name of the fitting method to use.
Valid values are: leastsq (Levenberg-Marquardt), nelder-mead
cdc
instance-attribute
cdc: str
Sets the circuit specified in the CDC string.
For more information, see: https://www.utwente.nl/en/tnw/ims/publications/downloads/cdc-explained.pdf
lambda_factor
class-attribute
instance-attribute
lambda_factor: float = 10.0
Lambda Scaling Factor. Levenberg-Marquardt only (default = 10).
lambda_start
class-attribute
instance-attribute
lambda_start: float = 0.01
Start lambda value. Levenberg-Marquardt only (default = 0.01).
last_psfitter
property
last_psfitter
Store reference to last SDK fitting object.
last_result
property
last_result
Store last fit result.
max_freq
class-attribute
instance-attribute
max_freq: None | float = None
Maximum fitting frequency in Hz.
max_iterations
class-attribute
instance-attribute
max_iterations: int = 500
Maximum number of iterations.
Minimization terminates once it reaches this number of steps (default = 500).
min_delta_error
class-attribute
instance-attribute
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
class-attribute
instance-attribute
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
class-attribute
instance-attribute
min_freq: None | float = None
Minimum fitting frequency in Hz.
tolerance
class-attribute
instance-attribute
tolerance: float = 0.0001
Convergence tolerance. Nelder-Mead only (default = 1e-4).
default_parameters
default_parameters() -> Parameters
Get default parameters. Use this to modify parameter values.
Returns:
-
parameters(Parameters) –Default parameters for CDC.
Source code in src/pypalmsens/_fitting.py
377 378 379 380 381 382 383 384 385 | |
fit
fit(data: EISData, *, parameters: None | Sequence[float] | Parameters = None) -> FitResult
Fit circuit model.
Parameters:
-
(dataEISData) –Input data.
-
(parametersOptional[Sequence[float] | Parameters], default:None) –Optional initial parameters for fit. Can be passed as
Parametersobject or list of values.
Returns:
-
result(FitResult) –Returns dataclass with fit results. Can also be accessed via
.last_result.
Source code in src/pypalmsens/_fitting.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
FitResult
dataclass
FitResult(cdc: str, parameters: list[float], error: list[float] | None, chisq: float, n_iter: int, exit_code: str)
Container for fitting results.
Methods:
-
from_eisdata–Construct fitresulf from EISData.
-
from_psfitresult–Construct fitresult from SDK FitResult.
-
from_psfitresult_nelder_mead–Construct fitresult from SDK FitResult.
-
get_bode_phase–Calculate observed and calculated Bode curve phase vs Frequency.
-
get_bode_z–Calculate observed and calculated Bode curve Z vs Frequency.
-
get_nyquist–Calculate observed and calculated nyquist curves.
-
get_psmodel–Get SDK Circuit model object.
-
plot_bode–Make bode plot using matplotlib.
-
plot_nyquist–Make nyquist plot using matplotlib.
Attributes:
-
cdc(str) –Circuit model CDC values.
-
chisq(float) –Chi-squared goodness of fit statistic.
-
error(list[float] | None) –Error (%) on parameters.
-
exit_code(str) –Exit code for the minimization.
-
n_iter(int) –Total number of iterations.
-
parameters(list[float]) –Optimized parameters for CDC.
from_eisdata
classmethod
from_eisdata(data: EISData)
Construct fitresulf from EISData.
Source code in src/pypalmsens/_fitting.py
168 169 170 171 172 173 174 175 176 177 178 | |
from_psfitresult
classmethod
from_psfitresult(result: FitResult, cdc: str)
Construct fitresult from SDK FitResult.
Source code in src/pypalmsens/_fitting.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
from_psfitresult_nelder_mead
classmethod
from_psfitresult_nelder_mead(result: FitResult, cdc: str)
Construct fitresult from SDK FitResult.
Source code in src/pypalmsens/_fitting.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
get_bode_phase
Calculate observed and calculated Bode curve phase vs Frequency.
Parameters:
-
(dataEISData) –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.
Source code in src/pypalmsens/_fitting.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
get_bode_z
Calculate observed and calculated Bode curve Z vs Frequency.
Parameters:
-
(dataEISData) –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.
Source code in src/pypalmsens/_fitting.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
get_nyquist
Calculate observed and calculated nyquist curves.
Parameters:
-
(dataEISData) –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.
Source code in src/pypalmsens/_fitting.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
get_psmodel
get_psmodel(data: EISData) -> CircuitModel
Get SDK Circuit model object.
Source code in src/pypalmsens/_fitting.py
180 181 182 183 184 185 186 | |
plot_bode
Make bode plot using matplotlib.
Parameters:
-
(dataEISData) –Input EIS data.
Returns:
-
fig(Figure) –Returns matplotlib figure object. use
fig.show()to render plot.
Source code in src/pypalmsens/_fitting.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
plot_nyquist
Make nyquist plot using matplotlib.
Parameters:
-
(dataEISData) –Input EIS data.
Returns:
-
fig(Figure) –Returns matplotlib figure object. use
fig.show()to render plot.
Source code in src/pypalmsens/_fitting.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
Parameter
dataclass
Parameter(symbol: str, value: None | float = None, min: None | float = None, max: None | float = None, fixed: None | bool = None)
Set or update Parameter attributes.
Attributes:
-
fixed(None | bool) –If True, fix the value for this parameter.
-
max(None | float) –Maximum (upper bound) for the parameter.
-
min(None | float) –Minimum (lower bound) for the parameter.
-
symbol(str) –Name of the parameter (not used in minimization).
-
value(None | float) –Initial value of the parameter.
fixed
class-attribute
instance-attribute
fixed: None | bool = None
If True, fix the value for this parameter.
max
class-attribute
instance-attribute
max: None | float = None
Maximum (upper bound) for the parameter.
min
class-attribute
instance-attribute
min: None | float = None
Minimum (lower bound) for the parameter.
Parameters
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:
-
(cdcstr) –Genererate fitting parameters for this CDC.
Attributes:
Source code in src/pypalmsens/_fitting.py
73 74 75 76 77 78 79 80 81 | |