Skip to content

Commit 37df260

Browse files
authored
Fix numpy v2 (#298)
1 parent 82b5654 commit 37df260

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

impedance/models/circuits/elements.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def wrapper(p, f):
4545
)
4646
else:
4747
circuit_elements[func.__name__] = wrapper
48+
# Adding numpy to circuit_elements for proper evaluation with
49+
# numpy>=2.0.0 because the scalar representation was changed.
50+
# "Scalars are now printed as np.float64(3.0) rather than just 3.0."
51+
# https://numpy.org/doc/2.0/release/2.0.0-notes.html
52+
# #representation-of-numpy-scalars-changed
53+
circuit_elements["np"] = np
4854

4955
return wrapper
5056

impedance/tests/test_circuit_elements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_each_element():
7878
input_vals = [0.1, 0.2, 0.3, 0.4]
7979
for key, f in circuit_elements.items():
8080
# don't test the outputs of series and parallel functions
81-
if key not in ["s", "p"]:
81+
if key not in ["s", "p", "np"]:
8282
num_inputs = f.num_params
8383
val = f(input_vals[:num_inputs], freqs)
8484
assert np.isclose(val, correct_vals[key]).all()

impedance/validation.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ def fit_linKK(f, ts, M, Z, fit_type='real', add_cap=False):
264264

265265

266266
def eval_linKK(elements, ts, f):
267-
""" Builds a circuit of RC elements to be used in LinKK """
268-
circuit_string = 's([R({},{}),'.format([elements[0]], f.tolist())
267+
"""Builds a circuit of RC elements to be used in LinKK"""
268+
circuit_string = f"s([R({[elements[0]]},{f.tolist()}),"
269269

270-
for (Rk, tk) in zip(elements[1:], ts):
271-
circuit_string += f'K({[Rk, tk]},{f.tolist()}),'
270+
for Rk, tk in zip(elements[1:], ts):
271+
circuit_string += f"K({[Rk, tk]},{f.tolist()}),"
272272

273-
circuit_string += 'L({},{}),'.format([elements[-1]], f.tolist())
273+
circuit_string += f"L({[elements[-1]]},{f.tolist()}),"
274274
if elements.size == (ts.size + 3):
275-
circuit_string += 'C({},{}),'.format([1/elements[-2]], f.tolist())
275+
circuit_string += f"C({[1 / elements[-2]]},{f.tolist()}),"
276276

277277
circuit_string = circuit_string.strip(',')
278278
circuit_string += '])'

0 commit comments

Comments
 (0)