Skip to content

Commit

Permalink
Fix spurious matplotlib errors
Browse files Browse the repository at this point in the history
Thanks to jklymak, who both helped trace this and suggested the
solution.
  • Loading branch information
Zac-HD committed Mar 9, 2018
1 parent 6df5cd3 commit d9cf4d5
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions properties/test_plotting.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""
Property-based tests for plotting methods.
Note that Hypothesis can find a 2x2 array that causes an internal error
for *every plot method tested*. Most are inside matplotlib. Good luck.
Note that we have to be careful when testing plotting functions:
1. Large floats can overflow, but this is not considered a MPL bug
2. Plotting is stateful, so we have to clean up after each call
With those details taken care of, they all pass!
"""
from __future__ import absolute_import, division, print_function

import pytest
import matplotlib.pyplot as plt

from hypothesis import given, settings
import hypothesis.strategies as st
Expand All @@ -19,35 +21,40 @@
settings.deadline = None


two_dimensional_array = npst.arrays(
dtype=st.one_of(
npst.unsigned_integer_dtypes(),
npst.integer_dtypes(),
npst.floating_dtypes(),
),
shape=npst.array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=4),
)
# This strategy is fairly complicated. First, we pick some kwargs: either
# an integer dtype with no restrictions on elements, or a flaoting dtype with
# elements that won't overflow.
two_dimensional_array = st.sampled_from([
dict(dtype=npst.unsigned_integer_dtypes() | npst.integer_dtypes()),
dict(dtype=npst.floating_dtypes(), elements=st.floats(-2.**100, 2.**100)),
# Then, we "flatmap" this into an arrays strategy - ie create a strategy using
# the kwargs above and a shape, then draw a value from that.
]).flatmap(lambda kwargs: npst.arrays(
**kwargs, shape=st.tuples(st.integers(2, 5), st.integers(2, 5)),
))


@pytest.mark.xfail
@given(two_dimensional_array)
def test_imshow(arr):
# Note that we clear the plot after each call - otherwise we just get
# errors from polluting state with too many colormaps!
xr.DataArray(arr).plot.imshow()
plt.clf()


@pytest.mark.xfail
@given(two_dimensional_array)
def test_pcolormesh(arr):
xr.DataArray(arr).plot.pcolormesh()
plt.clf()


@pytest.mark.xfail
@given(two_dimensional_array)
def test_contour(arr):
xr.DataArray(arr).plot.contour()
plt.clf()


@pytest.mark.xfail
@given(two_dimensional_array)
def test_contourf(arr):
xr.DataArray(arr).plot.contourf()
plt.clf()

0 comments on commit d9cf4d5

Please sign in to comment.