/
__init__.py
443 lines (355 loc) · 15.1 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from .fitting import circuit_fit, buildCircuit
from .fitting import calculateCircuitLength, check_and_eval
from impedance.plotting import plot_altair, plot_bode, plot_nyquist
from .elements import R, C, L, W, A, E, G, T, s, p # noqa: F401
import json
import matplotlib.pyplot as plt
import numpy as np
class BaseCircuit:
""" Base class for equivalent circuit models """
def __init__(self, initial_guess=[], constants=None, name=None):
""" Base constructor for any equivalent circuit model
Parameters
----------
initial_guess: numpy array
Initial guess of the circuit values
constants : dict
(Optional) Parameters and values to hold constant during fitting
(e.g. {"R0": 0.1})
name : str
(Optional) Name for the circuit
"""
# if supplied, check that initial_guess is valid and store
initial_guess = list(filter(None, initial_guess))
for i in initial_guess:
assert isinstance(i, (float, int, np.int32, np.float64)),\
'value {} in initial_guess is not a number'.format(i)
# initalize class attributes
self.initial_guess = initial_guess
if constants is not None:
self.constants = constants
else:
self.constants = {}
self.name = name
# initialize fit parameters and confidence intervals
self.parameters_ = None
self.conf_ = None
def __eq__(self, other):
if self.__class__ == other.__class__:
matches = []
for key, value in self.__dict__.items():
if isinstance(value, np.ndarray):
matches.append((value == other.__dict__[key]).all())
else:
matches.append(value == other.__dict__[key])
return np.array(matches).all()
else:
raise TypeError('Comparing object is not of the same type.')
def fit(self, frequencies, impedance, method=None, bounds=None):
""" Fit the circuit model
Parameters
----------
frequencies: numpy array
Frequencies
impedance: numpy array of dtype 'complex128'
Impedance values to fit
method: {‘lm’, ‘trf’, ‘dogbox’}, optional
Name of method to pass to scipy.optimize.curve_fit
bounds: 2-tuple of array_like, optional
Lower and upper bounds on parameters. Defaults to bounds on all
parameters of 0 and np.inf, except the CPE alpha
which has an upper bound of 1
Returns
-------
self: returns an instance of self
"""
# check that inputs are valid:
# frequencies: array of numbers
# impedance: array of complex numbers
# impedance and frequency match in length
assert isinstance(frequencies, np.ndarray),\
'frequencies is not of type np.ndarray'
assert isinstance(frequencies[0], (float, int, np.int32, np.float64)),\
'frequencies does not contain a number'
assert isinstance(impedance, np.ndarray),\
'impedance is not of type np.ndarray'
assert isinstance(impedance[0], (complex, np.complex128)),\
'impedance does not contain complex numbers'
assert len(frequencies) == len(impedance),\
'mismatch in length of input frequencies and impedances'
if self.initial_guess is not None:
parameters, conf = circuit_fit(frequencies, impedance,
self.circuit, self.initial_guess,
self.constants, method=method,
bounds=bounds)
self.parameters_ = parameters
if conf is not None:
self.conf_ = conf
else:
# TODO auto calc guess
raise ValueError('no initial guess supplied')
return self
def _is_fit(self):
""" check if model has been fit (parameters_ is not None) """
if self.parameters_ is not None:
return True
else:
return False
def predict(self, frequencies, use_initial=False):
""" Predict impedance using a fit equivalent circuit model
Parameters
----------
frequencies: numpy array
Frequencies
use_initial: boolean
If true and a fit was already completed,
use the initial parameters instead
Returns
-------
impedance: numpy array of dtype 'complex128'
Predicted impedance
"""
# check that inputs are valid:
# frequencies: array of numbers
assert isinstance(frequencies, np.ndarray),\
'frequencies is not of type np.ndarray'
assert isinstance(frequencies[0], (float, int, np.int32, np.float64)),\
'frequencies does not contain a number'
if self._is_fit() and not use_initial:
return eval(buildCircuit(self.circuit, frequencies,
*self.parameters_,
constants=self.constants, eval_string='',
index=0)[0])
else:
print("Simulating circuit based on initial parameters")
return eval(buildCircuit(self.circuit, frequencies,
*self.initial_guess,
constants=self.constants, eval_string='',
index=0)[0])
def get_param_names(self):
""" Converts circuit string to names and units """
# parse the element names from the circuit string
names = self.circuit.replace('p', '').replace('(', '').replace(')', '')
names = names.replace(',', '-').replace(' ', '').split('-')
full_names, all_units = [], []
for name in names:
num_params = check_and_eval(name[0]).num_params
units = check_and_eval(name[0]).units
if num_params > 1:
for j in range(num_params):
full_name = '{}_{}'.format(name, j)
if full_name not in self.constants.keys():
full_names.append(full_name)
all_units.append(units[j])
else:
if name not in self.constants.keys():
full_names.append(name)
all_units.append(units[0])
return full_names, all_units
def __str__(self):
""" Defines the pretty printing of the circuit"""
to_print = '\n'
if self.name is not None:
to_print += 'Name: {}\n'.format(self.name)
to_print += 'Circuit string: {}\n'.format(self.circuit)
to_print += "Fit: {}\n".format(self._is_fit())
if len(self.constants) > 0:
to_print += '\nConstants:\n'
for name, value in self.constants.items():
units = check_and_eval(name[0]).units
if '_' in name:
unit = units[int(name.split('_')[-1])]
else:
unit = units[0]
to_print += ' {:>5} = {:.2e} [{}]\n'.format(name, value, unit)
names, units = self.get_param_names()
to_print += '\nInitial guesses:\n'
for name, unit, param in zip(names, units, self.initial_guess):
to_print += ' {:>5} = {:.2e} [{}]\n'.format(name, param, unit)
if self._is_fit():
params, confs = self.parameters_, self.conf_
to_print += '\nFit parameters:\n'
for name, unit, param, conf in zip(names, units, params, confs):
to_print += ' {:>5} = {:.2e}'.format(name, param)
to_print += ' (+/- {:.2e}) [{}]\n'.format(conf, unit)
return to_print
def plot(self, ax=None, f_data=None, Z_data=None, kind='altair', **kwargs):
""" visualizes the model and optional data as a nyquist,
bode, or altair (interactive) plots
Parameters
----------
ax: matplotlib.axes
axes to plot on
f_data: np.array of type float
Frequencies of input data (for Bode plots)
Z_data: np.array of type complex
Impedance data to plot
kind: {'altair', 'nyquist', 'bode'}
type of plot to visualize
Other Parameters
----------------
**kwargs : optional
If kind is 'nyquist' or 'bode', used to specify additional
`matplotlib.pyplot.Line2D` properties like linewidth,
line color, marker color, and labels.
If kind is 'altair', used to specify nyquist height as `size`
Returns
-------
ax: matplotlib.axes
axes of the created nyquist plot
"""
if kind == 'nyquist':
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
if Z_data is not None:
ax = plot_nyquist(ax, Z_data, ls='', marker='s', **kwargs)
if self._is_fit():
if f_data is not None:
f_pred = f_data
else:
f_pred = np.logspace(5, -3)
Z_fit = self.predict(f_pred)
ax = plot_nyquist(ax, Z_fit, ls='-', marker='', **kwargs)
return ax
elif kind == 'bode':
if ax is None:
fig, ax = plt.subplots(nrows=2, figsize=(5, 5))
if Z_data is not None:
ax = plot_bode(ax, f_data, Z_data, ls='', marker='s', **kwargs)
if self._is_fit():
if f_data is not None:
f_pred = f_data
else:
f_pred = np.logspace(5, -3)
Z_fit = self.predict(f_pred)
ax = plot_bode(ax, f_pred, Z_fit, ls='-', marker='', **kwargs)
return ax
elif kind == 'altair':
plot_dict = {}
if Z_data is not None and f_data is not None:
plot_dict['data'] = {'f': f_data, 'Z': Z_data}
if self._is_fit():
if f_data is not None:
f_pred = f_data
else:
f_pred = np.logspace(5, -3)
Z_fit = self.predict(f_pred)
if self.name is not None:
name = self.name
else:
name = 'fit'
plot_dict[name] = {'f': f_pred, 'Z': Z_fit, 'fmt': '-'}
chart = plot_altair(plot_dict, **kwargs)
return chart
else:
raise ValueError("Kind must be one of 'altair'," +
f"'nyquist', or 'bode' (recieved {kind})")
def save(self, filepath):
""" Exports a model to JSON
Parameters
----------
filepath: str
Destination for exporting model object
"""
model_string = self.circuit
model_name = self.name
initial_guess = self.initial_guess
if self._is_fit():
parameters_ = list(self.parameters_)
model_conf_ = list(self.conf_)
data_dict = {"Name": model_name,
"Circuit String": model_string,
"Initial Guess": initial_guess,
"Constants": self.constants,
"Fit": True,
"Parameters": parameters_,
"Confidence": model_conf_,
}
else:
data_dict = {"Name": model_name,
"Circuit String": model_string,
"Initial Guess": initial_guess,
"Constants": self.constants,
"Fit": False}
with open(filepath, 'w') as f:
json.dump(data_dict, f)
def load(self, filepath, fitted_as_initial=False):
""" Imports a model from JSON
Parameters
----------
filepath: str
filepath to JSON file to load model from
fitted_as_initial: bool
If true, loads the model's fitted parameters
as initial guesses
Otherwise, loads the model's initial and
fitted parameters as a completed model
"""
json_data_file = open(filepath, 'r')
json_data = json.load(json_data_file)
model_name = json_data["Name"]
if model_name == 'None':
model_name = None
model_string = json_data["Circuit String"]
model_initial_guess = json_data["Initial Guess"]
model_constants = json_data["Constants"]
self.initial_guess = model_initial_guess
self.circuit = model_string
self.constants = model_constants
self.name = model_name
if json_data["Fit"]:
if fitted_as_initial:
self.initial_guess = np.array(json_data['Parameters'])
else:
self.parameters_ = np.array(json_data["Parameters"])
self.conf_ = np.array(json_data["Confidence"])
class Randles(BaseCircuit):
""" A Randles circuit model class """
def __init__(self, CPE=False, **kwargs):
""" Constructor for the Randles' circuit class
Parameters
----------
initial_guess: numpy array
Initial guess of the circuit values
CPE: boolean
Use a constant phase element instead of a capacitor
"""
super().__init__(**kwargs)
if CPE:
self.name = 'Randles w/ CPE'
self.circuit = 'R0-p(R1,E1)-W1'
else:
self.name = 'Randles'
self.circuit = 'R0-p(R1,C1)-W1'
circuit_len = calculateCircuitLength(self.circuit)
assert len(self.initial_guess) + len(self.constants) == circuit_len, \
'The number of initial guesses + ' + \
'the number of constants ({})'.format(len(self.initial_guess)) + \
' needs to be equal to ' + \
'the circuit length ({})'.format(circuit_len)
class CustomCircuit(BaseCircuit):
def __init__(self, circuit='', **kwargs):
""" Constructor for a customizable equivalent circuit model
Parameters
----------
initial_guess: numpy array
Initial guess of the circuit values
circuit: string
A string that should be interpreted as an equivalent circuit
Notes
-----
A custom circuit is defined as a string comprised of elements in series
(separated by a `-`) and elements in parallel (grouped as (x,y)).
Elements with two or more parameters are separated by a forward slash
(`/`).
Example:
Randles circuit is given by 'R0-p(R1,C1)-W1/W2'
"""
super().__init__(**kwargs)
self.circuit = circuit
circuit_len = calculateCircuitLength(self.circuit)
assert len(self.initial_guess) + len(self.constants) == circuit_len, \
'The number of initial guesses + ' + \
'the number of constants ({})'.format(len(self.initial_guess)) + \
' needs to be equal to ' + \
'the circuit length ({})'.format(circuit_len)