Context manager for seterr

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Context manager for seterr

Stéfan van der Walt
Hi all,

Since the topic of context managers recently came up, what do you think
of adding a context manager for seterr?

with np.seterr(divide='ignore'):
    frac = num / denom

Stéfan
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Context manager for seterr

Julian Taylor-3
On 15.12.2014 01:12, Stefan van der Walt wrote:
> Hi all,
>
> Since the topic of context managers recently came up, what do you think
> of adding a context manager for seterr?
>
> with np.seterr(divide='ignore'):
>     frac = num / denom
>


already exists as np.errstate:

with np.errstate(divide='ignore'):
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Context manager for seterr

Pauli Virtanen-3
In reply to this post by Stéfan van der Walt
15.12.2014, 02:12, Stefan van der Walt kirjoitti:
> Since the topic of context managers recently came up, what do you think
> of adding a context manager for seterr?
>
> with np.seterr(divide='ignore'):
>     frac = num / denom

There's this:

with np.errstate(divide='ignore'):
     ...



_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Context manager for seterr

Stéfan van der Walt
In reply to this post by Julian Taylor-3
On 2014-12-15 02:23:18, Julian Taylor <[hidden email]> wrote:
> with np.errstate(divide='ignore'):

Perfect, thanks!

Stéfan
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Context manager for seterr

Stéfan van der Walt
In reply to this post by Julian Taylor-3
On 2014-12-15 02:23:18, Julian Taylor <[hidden email]> wrote:
> already exists as np.errstate:
>
> with np.errstate(divide='ignore'):

With 'ignore' a warning is still raised--is this by choice?

>>> import numpy as np
>>> x = np.array([0, 1, 2.])
>>> with np.errstate(divide='ignore'):
...     x/x
...
__main__:2: RuntimeWarning: invalid value encountered in true_divide
array([ nan,   1.,   1.])


(I see it is documented that way as well, so I suspect so.)

Stéfan
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Context manager for seterr

Julian Taylor-3
On 15.12.2014 01:40, Stefan van der Walt wrote:

> On 2014-12-15 02:23:18, Julian Taylor <[hidden email]> wrote:
>> already exists as np.errstate:
>>
>> with np.errstate(divide='ignore'):
>
> With 'ignore' a warning is still raised--is this by choice?
>
>>>> import numpy as np
>>>> x = np.array([0, 1, 2.])
>>>> with np.errstate(divide='ignore'):
> ...     x/x
> ...
> __main__:2: RuntimeWarning: invalid value encountered in true_divide
> array([ nan,   1.,   1.])
>
>
> (I see it is documented that way as well, so I suspect so.)
>

0./0. raises an invalid floating point exception, unlike e.g 1./0. which
raises a zero division exception.
NumPy just bubbles up what the processor does, which means it does not
behave like Python which always raises ZeroDivision also for 0./0.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion