Skip to content

fix: add numpy to eval globals in eval_linKK for numpy 2.x compatibility#334

Open
alexbatgithub wants to merge 1 commit into
ECSHackWeek:mainfrom
alexbatgithub:patch-1
Open

fix: add numpy to eval globals in eval_linKK for numpy 2.x compatibility#334
alexbatgithub wants to merge 1 commit into
ECSHackWeek:mainfrom
alexbatgithub:patch-1

Conversation

@alexbatgithub
Copy link
Copy Markdown

numpy 2.x changed scalar repr (e.g. np.float64(0.001) instead of 0.001), causing NameError when eval_linKK evaluated the generated circuit string in worker processes. Adding 'np': np to the eval globals dict resolves this.

numpy 2.x changed scalar repr (e.g. np.float64(0.001) instead of 0.001), causing NameError when eval_linKK evaluated the generated circuit string in worker processes. Adding 'np': np to the eval globals dict resolves this.
@alexbatgithub
Copy link
Copy Markdown
Author

Tested with Python 3.14.5 and numpy version: 2.4.6
test.py or:

import multiprocessing
import numpy as np
from impedance.validation import linKK


def _worker(q):
    try:
        f = np.logspace(-1, 4, 20)
        Z = 0.01 / (1 + 1j * 2 * np.pi * f * 1e-3) + 0.005
        linKK(f, Z, c=0.85, max_M=8, fit_type="complex", add_cap=True)
        q.put(("OK", None))
    except Exception as e:
        q.put(("ERROR", f"{type(e).__name__}: {e}"))


if __name__ == "__main__":
    f = np.logspace(-1, 4, 20)
    Z = 0.01 / (1 + 1j * 2 * np.pi * f * 1e-3) + 0.005

    print("Single-process call: ", end="", flush=True)
    linKK(f, Z, c=0.85, max_M=8, fit_type="complex", add_cap=True)
    print("OK")

    print("Spawned-process call: ", end="", flush=True)
    ctx = multiprocessing.get_context("spawn")
    q = ctx.Queue()
    p = ctx.Process(target=_worker, args=(q,))
    p.start()
    p.join(timeout=30)
    if p.exitcode is None:
        p.terminate()
        print("TIMED OUT")
    else:
        status, msg = q.get()
        print("OK" if status == "OK" else f"FAILED — {msg}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant