禁用源自scipy的警告

gli*_*nka 6 python error-handling numpy scipy

当我将函数集成在不同的参数值时,我经常遇到如下错误:

 lsoda--  warning..internal t (=r1) and h (=r2) are
   such that in the machine, t + h = t on the next step  
   (h = step size). solver will continue anyway
Run Code Online (Sandbox Code Playgroud)

虽然我理解错误的技术方面,但我不知道如何抑制它被打印到stdout.我已经研究过numpy.seterr(),但是由于此错误消息似乎是从底层的C/Fortran代码生成的,因此它没有任何效果.虽然python script.py > /dev/null有效,但它显然也隐藏了可能打印的其他有用信息.

是否有某种方法可以禁用这些lsoda警告和错误,或以其他方式阻止它们被打印到stdout

unu*_*tbu 5

使用JF Sebastian的stdout_redirected,您可以将文件描述符级别的 stdout重定向到os.devnull.

with stdout_redirected():
    soln2 = integrate.odeint(f, y2, t2, mxstep = 5000)
Run Code Online (Sandbox Code Playgroud)

除此之外with-statement,stdout仍然被打印出来.在里面with-suite,stdout被压制了.

例如,这里是Caeiro的odeint示例,其中lsoda警告被抑制stdout_redirected.

import os
import sys
import contextlib
import numpy as np
import scipy.integrate as integrate
from numpy import pi

def fileno(file_or_fd):
    fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)()
    if not isinstance(fd, int):
        raise ValueError("Expected a file (`.fileno()`) or a file descriptor")
    return fd

@contextlib.contextmanager
def stdout_redirected(to=os.devnull, stdout=None):
    """
    https://stackoverflow.com/a/22434262/190597 (J.F. Sebastian)
    """
    if stdout is None:
       stdout = sys.stdout

    stdout_fd = fileno(stdout)
    # copy stdout_fd before it is overwritten
    #NOTE: `copied` is inheritable on Windows when duplicating a standard stream
    with os.fdopen(os.dup(stdout_fd), 'wb') as copied: 
        stdout.flush()  # flush library buffers that dup2 knows nothing about
        try:
            os.dup2(fileno(to), stdout_fd)  # $ exec >&to
        except ValueError:  # filename
            with open(to, 'wb') as to_file:
                os.dup2(to_file.fileno(), stdout_fd)  # $ exec > to
        try:
            yield stdout # allow code to be run with the redirected stdout
        finally:
            # restore stdout to its previous value
            #NOTE: dup2 makes stdout_fd inheritable unconditionally
            stdout.flush()
            os.dup2(copied.fileno(), stdout_fd)  # $ exec >&copied


#Constants and parameters
alpha=1/137.
k=1.e-9     
T=40.    
V= 6.e-6
r = 6.9673e12
u = 1.51856e7

#defining dy/dt's
def f(y, t):
       A, B, C, D, E = y
       # the model equations
       f0 = 1.519e21*(-2*k/T*(k - (alpha/pi)*(B+V))*A) 
       f1 = ((3*B**2 + 3*C**2 + 6*B*C + 2*pi**2*B*T + pi**2*T**2)**-1
             *(-f0*alpha/(3*pi**3) - 2*r*(B**3 + 3*B*C**2 + pi**2*T**2*B) 
               - u*(D**3 - E**3)))
       f2 = u*(D**3 - E**3)/(3*C**2)
       f3 = -u*(D**3 - E**3)/(3*D**2)
       f4 = u*(D**3 - E**3)/(3*E**2) + r*(B**3 + 3*B*C**2 + pi**2*T**2*B)/(3*E**2)
       return [f0, f1, f2, f3, f4]


t  = np.linspace(1e-15, 1e-10, 1000000)   # time grid
y2 = [2e13, 0, 50, 50, 25]
t2  = np.linspace(1.e-10, 1.e-5, 1000000)  

with stdout_redirected():
    soln2 = integrate.odeint(f, y2, t2, mxstep = 5000)
Run Code Online (Sandbox Code Playgroud)

lsoda写入stdout的警告被抑制.