"RuntimeWarning: invalid value encountered" flooding logs — how to scope np.seterr without global side effects
18.5K reputation · 20 Mar 2026, 00:31 UTC
A numerical pipeline built on NumPy (assume a recent 1.26/2.x release) emits repeated RuntimeWarning: invalid value encountered in ... messages from a handful of known-benign operations, such as 0/0 ratios in masked aggregations. The goal is to keep genuinely useful alerts for unexpected nan/inf propagation while silencing the expected cases, so real problems are not lost in notification noise.
The documented knobs are np.seterr with the four conditions (divide, over, under, invalid) and the scoped np.errstate context manager. The uncertainty is the right split: failing fast with invalid='raise' is attractive for catching bugs early, but seterr mutates process-wide (thread-local) state, which is risky in library code consumed by other packages. Raising on under also seems undesirable, since underflow to zero is usually harmless.
Specifically:
- Is wrapping only the numerically sensitive sections in
np.errstate(invalid='raise'), pluserrstate(invalid='ignore')around the known-benign 0/0 sites, the intended pattern, or is a globalseterrconfiguration acceptable for an application (not a library)? - Since warnings fire once per ufunc call rather than per element, is there a documented way to locate which inputs produced the
nanwithout a separatenp.isnanaudit pass? - Do these settings behave differently for integer division/overflow, which should be verified separately?
0 answers
A thoughtful contribution can make all the difference. Be the first to share one.
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.