Functions for changing global ufunc configuration
This provides helpers which wrap umath.geterrobj
and umath.seterrobj
Class | _unspecified |
Undocumented |
Class | errstate |
errstate(**kwargs) |
Function | _setdef |
Undocumented |
Function | getbufsize |
Return the size of the buffer used in ufuncs. |
Function | geterr |
Get the current way of handling floating-point errors. |
Function | geterrcall |
Return the current callback function used on floating-point errors. |
Function | setbufsize |
Set the size of the buffer used in ufuncs. |
Function | seterr |
Set how floating-point errors are handled. |
Function | seterrcall |
Set the floating-point error callback function or log object. |
Variable | _errdict |
Undocumented |
Variable | _errdict_rev |
Undocumented |
Variable | _Unspecified |
Undocumented |
Return the size of the buffer used in ufuncs.
Get the current way of handling floating-point errors.
geterrcall, seterr, seterrcall
For complete documentation of the types of floating-point exceptions and
treatment options, see seterr
.
>>> np.geterr() {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'} >>> np.arange(3.) / np.arange(3.) array([nan, 1., 1.])
>>> oldsettings = np.seterr(all='warn', over='raise') >>> np.geterr() {'divide': 'warn', 'over': 'raise', 'under': 'warn', 'invalid': 'warn'} >>> np.arange(3.) / np.arange(3.) array([nan, 1., 1.])
Return the current callback function used on floating-point errors.
When the error handling for a floating-point error (one of "divide",
"over", "under", or "invalid") is set to 'call' or 'log', the function
that is called or the log instance that is written to is returned by
geterrcall
. This function or log instance has been set with
seterrcall
.
seterrcall
,
None is returned.seterrcall, seterr, geterr
For complete documentation of the types of floating-point exceptions and
treatment options, see seterr
.
>>> np.geterrcall() # we did not yet set a handler, returns None
>>> oldsettings = np.seterr(all='call') >>> def err_handler(type, flag): ... print("Floating point error (%s), with flag %s" % (type, flag)) >>> oldhandler = np.seterrcall(err_handler) >>> np.array([1, 2, 3]) / 0.0 Floating point error (divide by zero), with flag 1 array([inf, inf, inf])
>>> cur_handler = np.geterrcall() >>> cur_handler is err_handler True
Set how floating-point errors are handled.
Note that operations on integer scalar types (such as int16
) are
handled like floating point, and are affected by these settings.
Set treatment for all types of floating-point errors at once:
RuntimeWarning
(via the Python warnings
module).FloatingPointError
.seterrcall
function.seterrcall
.The default is not to change the current behavior.
seterrcall : Set a callback function for the 'call' mode. geterr, geterrcall, errstate
The floating-point exceptions are defined in the IEEE 754 standard [1]:
[1] | https://en.wikipedia.org/wiki/IEEE_754 |
>>> old_settings = np.seterr(all='ignore') #seterr to known value >>> np.seterr(over='raise') {'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'} >>> np.seterr(**old_settings) # reset to default {'divide': 'ignore', 'over': 'raise', 'under': 'ignore', 'invalid': 'ignore'}
>>> np.int16(32000) * np.int16(3) 30464 >>> old_settings = np.seterr(all='warn', over='raise') >>> np.int16(32000) * np.int16(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> FloatingPointError: overflow encountered in short_scalars
>>> old_settings = np.seterr(all='print') >>> np.geterr() {'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'} >>> np.int16(32000) * np.int16(3) 30464
Set the floating-point error callback function or log object.
There are two ways to capture floating-point error messages. The first
is to set the error-handler to 'call', using seterr
. Then, set
the function to call using this function.
The second is to set the error-handler to 'log', using seterr
.
Floating-point errors then trigger a call to the 'write' method of
the provided object.
Function to call upon floating-point errors ('call'-mode) or object whose 'write' method is used to log such message ('log'-mode).
The call function takes two arguments. The first is a string describing the type of error (such as "divide by zero", "overflow", "underflow", or "invalid value"), and the second is the status flag. The flag is a byte, whose four least-significant bits indicate the type of error, one of "divide", "over", "under", "invalid":
[0 0 0 0 divide over under invalid]
In other words, flags = divide + 2*over + 4*under + 8*invalid.
If an object is provided, its write method should take one argument, a string.
seterr, geterr, geterrcall
Callback upon error:
>>> def err_handler(type, flag): ... print("Floating point error (%s), with flag %s" % (type, flag)) ...
>>> saved_handler = np.seterrcall(err_handler) >>> save_err = np.seterr(all='call')
>>> np.array([1, 2, 3]) / 0.0 Floating point error (divide by zero), with flag 1 array([inf, inf, inf])
>>> np.seterrcall(saved_handler) <function err_handler at 0x...> >>> np.seterr(**save_err) {'divide': 'call', 'over': 'call', 'under': 'call', 'invalid': 'call'}
Log error message:
>>> class Log: ... def write(self, msg): ... print("LOG: %s" % msg) ...
>>> log = Log() >>> saved_handler = np.seterrcall(log) >>> save_err = np.seterr(all='log')
>>> np.array([1, 2, 3]) / 0.0 LOG: Warning: divide by zero encountered in true_divide array([inf, inf, inf])
>>> np.seterrcall(saved_handler) <numpy.core.numeric.Log object at 0x...> >>> np.seterr(**save_err) {'divide': 'log', 'over': 'log', 'under': 'log', 'invalid': 'log'}