Skip to content

Commit 34261e5

Browse files
authored
Bug fixes (#25)
* Improve the handling of parameter normalization in simul_fit * Improve the handling of constant parsing * boost test coverage
1 parent 91daece commit 34261e5

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

nleis/nleis.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ def __init__(self, circuit_1='', circuit_2='', initial_guess=[],
9494
'Either circuit_1 or circuit_2 cannot be empty')
9595
edited_circuit = ''
9696
for elem in elements_1:
97-
nl_elem = elem[0:-1]+'n'+elem[-1]
97+
raw_elem = get_element_from_name(elem)
98+
len_raw_elem = len(raw_elem)
99+
nl_elem = elem[0:len_raw_elem] + 'n' + elem[len_raw_elem:]
100+
# elem[0:-1]+'n'+elem[-1]
98101
if nl_elem in elements_2:
99102
edited_circuit += '-' + nl_elem
100103
else:
@@ -160,9 +163,12 @@ def __init__(self, circuit_1='', circuit_2='', initial_guess=[],
160163
len_elem = len(raw_elem)
161164
nl_elem = elem[0:len_elem]+'n'+elem[len_elem:]
162165
raw_nl_elem = get_element_from_name(nl_elem)
163-
if raw_nl_elem in circuit_elements.keys():
166+
allowed_elems = circuit_elements.keys()
167+
if raw_nl_elem in allowed_elems:
164168
self.constants_2[nl_elem] = self.constants[elem]
165169
else:
170+
# this code is kept here to ignore
171+
# EIS only constants in constnats_2
166172
self.constants_2[elem] = self.constants[elem]
167173

168174
if raw_elem[-1] == 'n':
@@ -174,11 +180,9 @@ def __init__(self, circuit_1='', circuit_2='', initial_guess=[],
174180
+ f'number of parameters ({raw_num_params})')
175181

176182
num_params = check_and_eval(raw_elem[0:-1]).num_params
177-
len_elem = len(raw_elem[0:-1])
178183
if param_num < num_params:
179-
self.constants_1[
180-
elem[0:len_elem] +
181-
elem[len_elem+1:]] = self.constants[elem]
184+
self.constants_1[elem.replace(
185+
'n', '')] = self.constants[elem]
182186
self.constants_2[elem] = self.constants[elem]
183187

184188
else:

nleis/nleis_fitting.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,17 @@ def simul_fit(frequencies, Z1, Z2, circuit_1, circuit_2, edited_circuit,
187187
inf_in_bounds = np.any(np.isinf(bounds[0])) \
188188
or np.any(np.isinf(bounds[1]))
189189
if inf_in_bounds:
190-
lb = np.where(bounds[0] == -np.inf, -1e10, bounds[0])
191-
ub = np.where(bounds[1] == np.inf, 1e10, bounds[1])
190+
lb = np.where(np.array(bounds[0]) == -np.inf, -1e10, bounds[0])
191+
ub = np.where(np.array(bounds[1]) == np.inf, 1e10, bounds[1])
192192
bounds = (lb, ub)
193193
warnings.warn("inf is detected in the bounds, "
194194
"to enable parameter normalization, "
195195
"the bounds has been capped at 1e10. "
196196
"You can disable parameter normalization "
197197
"by set param_norm to False .")
198-
# ub = bounds[1]
198+
else:
199+
ub = bounds[1]
200+
199201
bounds = bounds/ub
200202
else:
201203
ub = np.ones(len(bounds[1]))

nleis/nleis_tests/test_nleis.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,20 @@ def test_EISandNLEIS():
8787
1e-3, 1e-3, 1e-3, 1e-2, 1000, 0,
8888
# TDS1 + additioal nonlinear parameters
8989
]
90+
# check correct assignment of constants
91+
# when EIS element is supplied
92+
NLEIS_circuit = EISandNLEIS(
93+
circ_str_1, circ_str_2, initial_guess=initial_guess,
94+
constants={'L0': 1e-7, 'TDS1_3': 1})
95+
96+
assert {'L0': 1e-7, 'TDS1_3': 1} == NLEIS_circuit.constants_1
97+
assert {'TDSn1_3': 1} == NLEIS_circuit.constants_2
9098

99+
# check correct assignment of constants
100+
# when 2nd-NLEIS element is supplied
91101
NLEIS_circuit = EISandNLEIS(
92102
circ_str_1, circ_str_2, initial_guess=initial_guess,
93103
constants={'L0': 1e-7, 'TDSn1_6': 0})
94-
95104
assert {'L0': 1e-7} == NLEIS_circuit.constants_1
96105
assert {'TDSn1_6': 0} == NLEIS_circuit.constants_2
97106

@@ -161,9 +170,17 @@ def test_EISandNLEIS():
161170

162171
with pytest.raises(ValueError):
163172
NLEIS_circuit = EISandNLEIS(
164-
circ_str_1, circ_str_2='', initial_guess=[1],
173+
circ_str_1, circ_str_2, initial_guess=[1],
165174
constants={'L0': 1e-7, 'TDSn1_6': 0})
166175

176+
# check that wrong constants input raise ValueError
177+
# when the wrong constants is applied for EIS_circuit
178+
179+
with pytest.raises(ValueError):
180+
NLEIS_circuit = EISandNLEIS(
181+
circ_str_1, circ_str_2, initial_guess=initial_guess,
182+
constants={'L0': 1e-7, 'TDS1_10': 0})
183+
167184

168185
def test_eq():
169186
NLEIS_circuit = NLEISCustomCircuit()

nleis/nleis_tests/test_nleis_fitting.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ def test_buildCircuit():
288288
constants={})[0].replace(' ', '') == \
289289
'R([100],[1000.0,5.0,0.01])'
290290

291+
# Test two elements circuit with constants for one elements
292+
circuit = 'R1-R2'
293+
params = [100]
294+
frequencies = [1000.0, 5.0, 0.01]
295+
296+
assert buildCircuit(circuit, frequencies, *params,
297+
constants={'R2': 1})[0].replace(' ', '') == \
298+
's([R([100],[1000.0,5.0,0.01]),' +\
299+
'R([1],[1000.0,5.0,0.01])])'
300+
291301

292302
def test_mae():
293303
a = np.array([2 + 4*1j, 3 + 2*1j])
@@ -365,10 +375,19 @@ def test_simul_fit():
365375
edited_circuit, initial_guess, constants_1={},
366376
constants_2={},
367377
bounds=None, opt='max', cost=0.5, max_f=10,
368-
param_norm=True, positive=True)
378+
param_norm=False, positive=False)
369379

370380
assert np.allclose(p, results, rtol=1e-3, atol=1e-3)
371381

382+
# test the option of parameter normalization
383+
with pytest.warns(UserWarning, match="inf is detected in the bounds"):
384+
bounds = set_default_bounds(edited_circuit)
385+
p, perror = simul_fit(f, Z1, Z2, circ_str_1, circ_str_2,
386+
edited_circuit, initial_guess, constants_1={},
387+
constants_2={},
388+
bounds=bounds, opt='max', cost=0.5, max_f=10,
389+
param_norm=True, positive=True)
390+
372391

373392
def test_individual_parameters():
374393

@@ -446,8 +465,6 @@ def test_individual_parameters():
446465
constants_1={'TDS1_0': 0}, constants_2={'TDSn0_6': 0.1})
447466
assert np.allclose(p1, [1e-7, 1e-3, 5e-3, 1e-3, 10.0,
448467
1e-02, 100.0, 1e-3, 1e-03, 1e-02, 1000.0])
449-
print(p1)
450-
print(p2)
451468
assert np.allclose(p2, [5e-3, 1e-3, 10, 1e-2, 100, 10])
452469

453470
# test the case when only one electrode is contributing to the 2nd-NLEIS

0 commit comments

Comments
 (0)