-
Notifications
You must be signed in to change notification settings - Fork 117
Description
Title: NameError: name 'np' is not defined during circuit evaluation (missing numpy in eval namespace)
Description:
impedance versions ≥1.x produce a NameError during equivalent-circuit fitting when evaluating the circuit string.
The error occurs because np (numpy) is used in the generated circuit expression but is not included in the circuit_elements dictionary passed to eval().
Error traceback:
NameError: name 'np' is not defined
File ".../impedance/models/circuits.py", line XXX
return eval(circuit_string, circuit_elements)
Cause:
eval() is executed like this:
eval(circuit_string, circuit_elements)
But the circuit string contains np (e.g., np.power, np.sqrt).
Since np is not present in circuit_elements, the evaluation fails.
Older versions of the library did not show this problem.
Minimal reproducible example
from impedance.models.circuits import CustomCircuit
circuit = 'R0-p(CPE1,R1)'
initial_guess = [10, 1e-5, 0.9, 50]
frequencies = [...]
Z = [...]
circuit_model = CustomCircuit(circuit, initial_guess)
circuit_model.fit(frequencies, Z)
Produces:
NameError: name 'np' is not defined
Proposed fix
Modify circuits.py to inject numpy into the evaluation namespace:
import numpy as np
circuit_elements['np'] = np
This ensures circuit expressions that rely on numpy evaluate properly.
Workaround
Users can manually patch it:
from impedance.models.elements import circuit_elements
import numpy as np
circuit_elements['np'] = np
Circuit expressions should evaluate without raising NameError.