From cd75c01e0af4a57bef34b7bc7cdaea2f41e4d302 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 12 Jul 2014 16:39:20 -0500 Subject: [PATCH 01/11] Filter warnings in rcparams test. --- lib/matplotlib/tests/test_rcparams.py | 45 ++++++++++++++------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 9d6d0613db76..e1859c6f5fe5 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -5,6 +5,7 @@ import os import sys +import warnings import matplotlib as mpl from matplotlib.tests import assert_str_equal @@ -97,29 +98,31 @@ def test_RcParams_class(): def test_Bug_2543(): # Test that it possible to add all values to itself / deepcopy - # This was not possible because validate_bool_maybe_none did not + # This was not possible because validate_bool_maybe_none did not # accept None as an argument. # https://github.com/matplotlib/matplotlib/issues/2543 - with mpl.rc_context(): - _copy = mpl.rcParams.copy() - for key in six.iterkeys(_copy): - mpl.rcParams[key] = _copy[key] - mpl.rcParams['text.dvipnghack'] = None - with mpl.rc_context(): - from copy import deepcopy - _deep_copy = deepcopy(mpl.rcParams) - from matplotlib.rcsetup import validate_bool_maybe_none, validate_bool - # real test is that this does not raise - assert_true(validate_bool_maybe_none(None) is None) - assert_true(validate_bool_maybe_none("none") is None) - _fonttype = mpl.rcParams['svg.fonttype'] - assert_true(_fonttype == mpl.rcParams['svg.embed_char_paths']) - with mpl.rc_context(): - mpl.rcParams['svg.embed_char_paths'] = False - assert_true(mpl.rcParams['svg.fonttype'] == "none") + with warnings.catch_warnings(): + warnings.filterwarnings('ignore', message='.*(deprecated|obsolete)', category=UserWarning) + with mpl.rc_context(): + _copy = mpl.rcParams.copy() + for key in six.iterkeys(_copy): + mpl.rcParams[key] = _copy[key] + mpl.rcParams['text.dvipnghack'] = None + with mpl.rc_context(): + from copy import deepcopy + _deep_copy = deepcopy(mpl.rcParams) + from matplotlib.rcsetup import validate_bool_maybe_none, validate_bool + # real test is that this does not raise + assert_true(validate_bool_maybe_none(None) is None) + assert_true(validate_bool_maybe_none("none") is None) + _fonttype = mpl.rcParams['svg.fonttype'] + assert_true(_fonttype == mpl.rcParams['svg.embed_char_paths']) + with mpl.rc_context(): + mpl.rcParams['svg.embed_char_paths'] = False + assert_true(mpl.rcParams['svg.fonttype'] == "none") def test_Bug_2543_newer_python(): - # only split from above because of the usage of assert_raises + # only split from above because of the usage of assert_raises # as a context manager, which only works in 2.7 and above if sys.version_info[:2] < (2, 7): raise nose.SkipTest("assert_raises as context manager not supported with Python < 2.7") @@ -130,8 +133,8 @@ def test_Bug_2543_newer_python(): validate_bool(None) with assert_raises(ValueError): with mpl.rc_context(): - mpl.rcParams['svg.fonttype'] = True - + mpl.rcParams['svg.fonttype'] = True + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From c56ece1dc7c432004b4fd6c7090dd3949b33ecd2 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 12 Jul 2014 17:34:44 -0500 Subject: [PATCH 02/11] Suppress some more warnings. These are genuine too so we filter them --- lib/matplotlib/tests/test_subplots.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index b54158bb6de5..50c6563bf3c2 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -1,6 +1,7 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) +import warnings import six from six.moves import xrange @@ -105,10 +106,14 @@ def test_exceptions(): # TODO should this test more options? assert_raises(ValueError, plt.subplots, 2, 2, sharex='blah') assert_raises(ValueError, plt.subplots, 2, 2, sharey='blah') - assert_raises(ValueError, plt.subplots, 2, 2, -1) - # uncomment this for 1.5 - # assert_raises(ValueError, plt.subplots, 2, 2, 0) - assert_raises(ValueError, plt.subplots, 2, 2, 5) + with warnings.catch_warnings(): + warnings.filterwarnings('ignore', + message='.*sharex\ argument\ to\ subplots', + category=UserWarning) + assert_raises(ValueError, plt.subplots, 2, 2, -1) + # uncomment this for 1.5 + # assert_raises(ValueError, plt.subplots, 2, 2, 0) + assert_raises(ValueError, plt.subplots, 2, 2, 5) @image_comparison(baseline_images=['subplots_offset_text'], remove_text=False) From 155e1f7a6c61ad3f94ad5e99150ad414e674331a Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Mon, 21 Jul 2014 16:27:47 +0100 Subject: [PATCH 03/11] Catch warnings in tightlayout test. These are warnings that are raised because tightlayout does not work fully automatic on gridspec. However, the point of the test is doing this with some manual help so this works as expected. --- lib/matplotlib/tests/test_tightlayout.py | 64 +++++++++++++----------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index 14e1528faeba..19782233c303 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -2,6 +2,7 @@ unicode_literals) import six +import warnings import numpy as np @@ -10,12 +11,14 @@ from nose.tools import assert_raises from numpy.testing import assert_array_equal + def example_plot(ax, fontsize=12): - ax.plot([1, 2]) - ax.locator_params(nbins=3) - ax.set_xlabel('x-label', fontsize=fontsize) - ax.set_ylabel('y-label', fontsize=fontsize) - ax.set_title('Title', fontsize=fontsize) + ax.plot([1, 2]) + ax.locator_params(nbins=3) + ax.set_xlabel('x-label', fontsize=fontsize) + ax.set_ylabel('y-label', fontsize=fontsize) + ax.set_title('Title', fontsize=fontsize) + @image_comparison(baseline_images=['tight_layout1']) def test_tight_layout1(): @@ -81,50 +84,51 @@ def test_tight_layout5(): fig = plt.figure() ax = plt.subplot(111) - arr = np.arange(100).reshape((10,10)) + arr = np.arange(100).reshape((10, 10)) ax.imshow(arr, interpolation="none") plt.tight_layout() - @image_comparison(baseline_images=['tight_layout6']) def test_tight_layout6(): 'Test tight_layout for gridspec' - fig = plt.figure() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", UserWarning) + fig = plt.figure() - import matplotlib.gridspec as gridspec + import matplotlib.gridspec as gridspec - gs1 = gridspec.GridSpec(2, 1) - ax1 = fig.add_subplot(gs1[0]) - ax2 = fig.add_subplot(gs1[1]) + gs1 = gridspec.GridSpec(2, 1) + ax1 = fig.add_subplot(gs1[0]) + ax2 = fig.add_subplot(gs1[1]) - example_plot(ax1) - example_plot(ax2) + example_plot(ax1) + example_plot(ax2) - gs1.tight_layout(fig, rect=[0, 0, 0.5, 1]) + gs1.tight_layout(fig, rect=[0, 0, 0.5, 1]) - gs2 = gridspec.GridSpec(3, 1) + gs2 = gridspec.GridSpec(3, 1) - for ss in gs2: - ax = fig.add_subplot(ss) - example_plot(ax) - ax.set_title("") - ax.set_xlabel("") + for ss in gs2: + ax = fig.add_subplot(ss) + example_plot(ax) + ax.set_title("") + ax.set_xlabel("") - ax.set_xlabel("x-label", fontsize=12) + ax.set_xlabel("x-label", fontsize=12) - gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.45) + gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.45) - top = min(gs1.top, gs2.top) - bottom = max(gs1.bottom, gs2.bottom) + top = min(gs1.top, gs2.top) + bottom = max(gs1.bottom, gs2.bottom) - gs1.tight_layout(fig, rect=[None, 0 + (bottom-gs1.bottom), - 0.5, 1 - (gs1.top-top)]) - gs2.tight_layout(fig, rect=[0.5, 0 + (bottom-gs2.bottom), - None, 1 - (gs2.top-top)], - h_pad=0.45) + gs1.tight_layout(fig, rect=[None, 0 + (bottom-gs1.bottom), + 0.5, 1 - (gs1.top-top)]) + gs2.tight_layout(fig, rect=[0.5, 0 + (bottom-gs2.bottom), + None, 1 - (gs2.top-top)], + h_pad=0.45) @image_comparison(baseline_images=['tight_layout7']) From 669c379dcc30e50987350c75063f8b65d0111c77 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 11:13:38 +0200 Subject: [PATCH 04/11] Add note about disabling warnings in test_rcparams --- lib/matplotlib/tests/test_rcparams.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index e1859c6f5fe5..928393ecd5c7 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -101,8 +101,13 @@ def test_Bug_2543(): # This was not possible because validate_bool_maybe_none did not # accept None as an argument. # https://github.com/matplotlib/matplotlib/issues/2543 + # We filter warnings at this stage since a number of them are raised + # for deprecated rcparams as they should. We dont want these in the + # printed in the test suite. with warnings.catch_warnings(): - warnings.filterwarnings('ignore', message='.*(deprecated|obsolete)', category=UserWarning) + warnings.filterwarnings('ignore', + message='.*(deprecated|obsolete)', + category=UserWarning) with mpl.rc_context(): _copy = mpl.rcParams.copy() for key in six.iterkeys(_copy): From 855407bba6a1cfcadc89d51f524236439216bdd9 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 11:16:02 +0200 Subject: [PATCH 05/11] Add note to test_subplots --- lib/matplotlib/tests/test_subplots.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index 50c6563bf3c2..4400cbc4beb9 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -106,10 +106,12 @@ def test_exceptions(): # TODO should this test more options? assert_raises(ValueError, plt.subplots, 2, 2, sharex='blah') assert_raises(ValueError, plt.subplots, 2, 2, sharey='blah') + # We filter warnings in this test which are genuine since + # the pount of this test is to ensure that this raises. with warnings.catch_warnings(): warnings.filterwarnings('ignore', - message='.*sharex\ argument\ to\ subplots', - category=UserWarning) + message='.*sharex\ argument\ to\ subplots', + category=UserWarning) assert_raises(ValueError, plt.subplots, 2, 2, -1) # uncomment this for 1.5 # assert_raises(ValueError, plt.subplots, 2, 2, 0) From 8727205ced5b0037b8e7e45d1addba19f6e5efc6 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 11:17:56 +0200 Subject: [PATCH 06/11] Add note to test_tightlayout --- lib/matplotlib/tests/test_tightlayout.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index 19782233c303..e6935cae8c6b 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -94,6 +94,9 @@ def test_tight_layout5(): def test_tight_layout6(): 'Test tight_layout for gridspec' + # This raises warnings since tight layout cannot + # do this fully automatically. But the test is + # correct since the layout is manually edited with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) fig = plt.figure() From 57040ac2be7d2c0196050929819ec2bc27e27311 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 11:44:07 +0200 Subject: [PATCH 07/11] Suppress deprecation warning for Delaunay module in the test suite --- lib/matplotlib/tests/test_delaunay.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_delaunay.py b/lib/matplotlib/tests/test_delaunay.py index 49f10f811c93..373f77404fe0 100644 --- a/lib/matplotlib/tests/test_delaunay.py +++ b/lib/matplotlib/tests/test_delaunay.py @@ -3,10 +3,16 @@ import six from six.moves import xrange +import warnings import numpy as np from matplotlib.testing.decorators import image_comparison, knownfailureif -from matplotlib.delaunay.triangulate import Triangulation +from matplotlib.cbook import MatplotlibDeprecationWarning + +with warnings.catch_warnings(): + # the module is deprecated. The tests should be removed when the module is. + warnings.simplefilter('ignore', MatplotlibDeprecationWarning) + from matplotlib.delaunay.triangulate import Triangulation from matplotlib import pyplot as plt import matplotlib as mpl From 1b7856e27006c4c09157f6741f6ebe1eaa3e09e7 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 12:49:28 +0200 Subject: [PATCH 08/11] Suppress warning due to NaNs in this test. --- lib/matplotlib/tests/test_mlab.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index 0cc991f05ebf..d811c8eefe3d 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -2714,7 +2714,10 @@ def get_z(x, y): z_masked = np.ma.array(z, mask=[False, False, False, True, False]) correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, get_z(xi, yi)) zi = mlab.griddata(x, y, z_masked, xi, yi, interp='linear') - np.testing.assert_array_almost_equal(zi, correct_zi_masked) + with np.errstate(invalid='ignore'): + # The array contains a number of NaNs so suppress the + # numpy warnings here. + np.testing.assert_array_almost_equal(zi, correct_zi_masked) np.testing.assert_array_equal(np.ma.getmask(zi), np.ma.getmask(correct_zi_masked)) From 58ac6a841e323e6dd0be4152c9492054ea05c098 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 18:21:27 +0200 Subject: [PATCH 09/11] Suppress warning when handling NaNs in streamplot --- lib/matplotlib/tests/test_streamplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_streamplot.py b/lib/matplotlib/tests/test_streamplot.py index d040fccb12a5..75b9e082da25 100644 --- a/lib/matplotlib/tests/test_streamplot.py +++ b/lib/matplotlib/tests/test_streamplot.py @@ -42,7 +42,8 @@ def test_masks_and_nans(): mask[40:60, 40:60] = 1 U = np.ma.array(U, mask=mask) U[:20, :20] = np.nan - plt.streamplot(X, Y, U, V, color=U, cmap=plt.cm.Blues) + with np.errstate(invalid='ignore'): + plt.streamplot(X, Y, U, V, color=U, cmap=plt.cm.Blues) @cleanup From edffd3caa0f0a451ee1e12ae10f410754df5a3fb Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 18:58:15 +0200 Subject: [PATCH 10/11] Use assert_array_almost_equal from numpy.ma This does not raise warnings about NaNs in the masked part of the array. --- lib/matplotlib/tests/test_triangulation.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_triangulation.py b/lib/matplotlib/tests/test_triangulation.py index 90a8fe9d23d1..87719c29a63d 100644 --- a/lib/matplotlib/tests/test_triangulation.py +++ b/lib/matplotlib/tests/test_triangulation.py @@ -9,6 +9,7 @@ from nose.tools import assert_equal, assert_raises from numpy.testing import assert_array_equal, assert_array_almost_equal,\ assert_array_less +import numpy.ma.testutils as matest from matplotlib.testing.decorators import image_comparison import matplotlib.cm as cm from matplotlib.path import Path @@ -323,7 +324,7 @@ def test_triinterp(): xs, ys = np.meshgrid(xs, ys) for interp in (linear_interp, cubic_min_E, cubic_geom): zs = interp(xs, ys) - assert_array_almost_equal(zs, (1.23*xs - 4.79*ys)) + matest.assert_array_almost_equal(zs, (1.23*xs - 4.79*ys)) mask = (xs >= 1) * (xs <= 2) * (ys >= 1) * (ys <= 2) assert_array_equal(zs.mask, mask) @@ -697,7 +698,8 @@ def z(x, y): interp_z0[interp_key] = interp(xs0, ys0) # storage else: interpz = interp(xs, ys) - assert_array_almost_equal(interpz, interp_z0[interp_key]) + matest.assert_array_almost_equal(interpz, + interp_z0[interp_key]) scale_factor = 987654.3210 for scaled_axis in ('x', 'y'): @@ -723,7 +725,7 @@ def z(x, y): # 1 axis... for interp_key in ['lin', 'min_E', 'geom']: interpz = dic_interp[interp_key](xs, ys) - assert_array_almost_equal(interpz, interp_z0[interp_key]) + matest.assert_array_almost_equal(interpz, interp_z0[interp_key]) @image_comparison(baseline_images=['tri_smooth_contouring'], From 827393e0a6f7dd6c9516747b2cd8e67a48a19234 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sat, 26 Jul 2014 19:01:47 +0200 Subject: [PATCH 11/11] Use ma.testutils rather than a filter for warnings with NaN in masked arrays in test_mlab --- lib/matplotlib/tests/test_mlab.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index d811c8eefe3d..36935793e05a 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -6,6 +6,7 @@ import tempfile from numpy.testing import assert_allclose, assert_array_equal +import numpy.ma.testutils as matest import numpy as np from nose.tools import (assert_equal, assert_almost_equal, assert_not_equal, assert_true, assert_raises) @@ -2714,10 +2715,7 @@ def get_z(x, y): z_masked = np.ma.array(z, mask=[False, False, False, True, False]) correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, get_z(xi, yi)) zi = mlab.griddata(x, y, z_masked, xi, yi, interp='linear') - with np.errstate(invalid='ignore'): - # The array contains a number of NaNs so suppress the - # numpy warnings here. - np.testing.assert_array_almost_equal(zi, correct_zi_masked) + matest.assert_array_almost_equal(zi, correct_zi_masked) np.testing.assert_array_equal(np.ma.getmask(zi), np.ma.getmask(correct_zi_masked))