Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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:
elem = get_element_from_name(name)
num_params = check_and_eval(elem).num_params
units = check_and_eval(elem).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
eval_string += "s(["
split = series
elif parallel is not None and len(parallel) > 1:
eval_string += "p(["
split = parallel
for i, elem in enumerate(split):
if ',' in elem or '-' in elem:
eval_string, index = buildCircuit(elem, frequencies,
*parameters,
constants=constants,
eval_string=eval_string,
index=index)
else:
param_string = ""
raw_elem = get_element_from_name(elem)
elem_number = check_and_eval(raw_elem).num_params
param_list = []
for j in range(elem_number):
if elem_number > 1:
current_elem = elem + '_{}'.format(j)
else:
current_elem = elem
if current_elem in constants.keys():
param_list.append(constants[current_elem])
else:
param_list.append(parameters[index])
index += 1
param_string += str(param_list)
new = raw_elem + '(' + param_string + ',' + str(frequencies) + ')'
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():
elem = get_element_from_name(name)
units = check_and_eval(elem).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)
---------
Need to do a better job of handling errors in fitting.
Currently, an error of -1 is returned.
"""
f = frequencies
Z = impedances
# extract the elements from the circuit
extracted_elements = extract_circuit_elements(circuit)
# set upper and lower bounds on a per-element basis
if bounds is None:
lb, ub = [], []
for elem in extracted_elements:
raw_element = get_element_from_name(elem)
for i in range(check_and_eval(raw_element).num_params):
if elem in constants or elem + '_{}'.format(i) in constants:
continue
if raw_element in ['CPE', 'La'] and i == 1:
ub.append(1)
else:
ub.append(np.inf)
lb.append(0)
bounds = ((lb), (ub))
popt, pcov = curve_fit(wrapCircuit(circuit, constants), f,
np.hstack([Z.real, Z.imag]), p0=initial_guess,
method=method, bounds=bounds, maxfev=100000,
ftol=1E-13)
perror = np.sqrt(np.diag(pcov))
def calculateCircuitLength(circuit):
length = 0
if circuit:
extracted_elements = extract_circuit_elements(circuit)
for elem in extracted_elements:
raw_element = get_element_from_name(elem)
num_params = check_and_eval(raw_element).num_params
length += num_params
return length