Skip to content

Commit 5f104bc

Browse files
committed
Suppress warning 'invalid value encountered in sign'
During autoscaling of a probscaled axis, limit_range_for_scale is called with (0, 1, inf) and returns inf, 1. The inverse transform then yields nan, -2.32... for these values. The nan-value raises the warning in _approx_erf (Windows only, see numpy issue #8945). Sign(nan) shouldn't raise a warning as the docs explain it returns nan for nan input.
1 parent 958b33a commit 5f104bc

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

probscale/probscale.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy
2+
import warnings
23
from matplotlib.scale import ScaleBase
34
from matplotlib.ticker import (
45
FixedLocator,
@@ -29,7 +30,9 @@ def _approx_erf(cls, x):
2930
"""
3031

3132
guts = -x**2 * (4.0 / numpy.pi + cls._A * x**2) / (1.0 + cls._A * x**2)
32-
return numpy.sign(x) * numpy.sqrt(1.0 - numpy.exp(guts))
33+
with warnings.catch_warnings():
34+
warnings.filterwarnings('ignore', 'invalid value encountered in sign')
35+
return numpy.sign(x) * numpy.sqrt(1.0 - numpy.exp(guts))
3336

3437
@classmethod
3538
def _approx_inv_erf(cls, z):
@@ -41,7 +44,9 @@ def _approx_inv_erf(cls, z):
4144

4245
_b = (2 / numpy.pi / cls._A) + (0.5 * numpy.log(1 - z**2))
4346
_c = numpy.log(1 - z**2) / cls._A
44-
return numpy.sign(z) * numpy.sqrt(numpy.sqrt(_b**2 - _c) - _b)
47+
with warnings.catch_warnings():
48+
warnings.filterwarnings('ignore', 'invalid value encountered in sign')
49+
return numpy.sign(z) * numpy.sqrt(numpy.sqrt(_b**2 - _c) - _b)
4550

4651
@classmethod
4752
def ppf(cls, q):

probscale/tests/test_probscale.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ def test_minimal_norm_cdf(mn, mn_input):
7272
assert numpy.all(numpy.abs(diff) < 0.001)
7373

7474

75+
def test_sign_with_nan_no_warning(mn):
76+
with pytest.warns(None) as record:
77+
res = mn._approx_erf(numpy.nan)
78+
assert not record
79+
assert numpy.isnan(res)
80+
81+
82+
def test_sign_with_nan_no_warning(mn):
83+
with pytest.warns(None) as record:
84+
res = mn._approx_inv_erf(numpy.nan)
85+
assert not record
86+
assert numpy.isnan(res)
87+
88+
7589
@pytest.mark.mpl_image_compare(
7690
baseline_dir='baseline_images/test_probscale',
7791
tolerance=TOLERANCE,

0 commit comments

Comments
 (0)