Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix some test warnings #7336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/afm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
... 'fonts', 'afm', 'ptmr8a.afm')
>>>
>>> from matplotlib.afm import AFM
>>> afm = AFM(open(afm_fname))
>>> with open(afm_fname) as fh:
... afm = AFM(fh)
>>> afm.string_width_height('What the heck?')
(6220.0, 694)
>>> afm.get_fontname()
Expand Down
7 changes: 3 additions & 4 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,9 @@ def grab_frame(self, **savefig_kwargs):
try:
# Tell the figure to save its data to the sink, using the
# frame format and dpi.
myframesink = self._frame_sink()
self.fig.savefig(myframesink, format=self.frame_format,
dpi=self.dpi, **savefig_kwargs)
myframesink.close()
with self._frame_sink() as myframesink:
self.fig.savefig(myframesink, format=self.frame_format,
dpi=self.dpi, **savefig_kwargs)

except RuntimeError:
out, err = self._proc.communicate()
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/stackplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def stackplot(axes, x, *args, **kwargs):
center = np.zeros(n)
total = np.sum(y, 0)
# multiply by 1/total (or zero) to avoid infinities in the division:
inv_total = np.where(total > 0, 1./total, 0)
inv_total = np.zeros_like(total)
mask = total > 0
inv_total[mask] = 1.0 / total[mask]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inv_total = np.reciprocal(total)
inv_total[total <= 0] = 0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.reciprocal also warns about division by zero, which is the main reason for this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I have missed the warning message when tried np.reciprocal

increase = np.hstack((y[:, 0:1], np.diff(y)))
below_size = total - stack
below_size += 0.5 * y
Expand Down
9 changes: 8 additions & 1 deletion lib/matplotlib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@

import six

import warnings

from nose.tools import assert_equal

from matplotlib.cbook import MatplotlibDeprecationWarning
from matplotlib.testing.decorators import knownfailureif
from pylab import *
with warnings.catch_warnings():
warnings.filterwarnings('ignore',
'The finance module has been deprecated in mpl 2',
MatplotlibDeprecationWarning)
from pylab import *
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the warning makes any sense?
This will also create a merge conflict to master.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's a question for a different issue/PR, I think.

Anything done here will cause a conflict with master, probably. We could backport your entire change to this file, but I thought that might be a bit drastic.



def test_simple():
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def get_transform(self):
@cleanup
def test_picking():
fig, ax = plt.subplots()
col = ax.scatter([0], [0], [1000])
col = ax.scatter([0], [0], [1000], picker=True)
fig.savefig(io.BytesIO(), dpi=fig.dpi)

class MouseEvent(object):
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,18 @@ def test_contour_shape_mismatch_4():
try:
ax.contour(b, g, z)
except TypeError as exc:
print(exc.args[0])
assert re.match(
r'Shape of x does not match that of z: ' +
r'found \(9L?, 9L?\) instead of \(9L?, 10L?\)\.',
exc.args[0]) is not None
exc.args[0]) is not None, exc.args[0]

try:
ax.contour(g, b, z)
except TypeError as exc:
assert re.match(
r'Shape of y does not match that of z: ' +
r'found \(9L?, 9L?\) instead of \(9L?, 10L?\)\.',
exc.args[0]) is not None
exc.args[0]) is not None, exc.args[0]


@cleanup
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/tests/test_cycles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings

from matplotlib.testing.decorators import image_comparison, cleanup
from matplotlib.cbook import MatplotlibDeprecationWarning
import matplotlib.pyplot as plt
import numpy as np
from nose.tools import assert_raises
Expand Down Expand Up @@ -184,6 +185,7 @@ def test_cycle_reset():
fig, ax = plt.subplots()
# Need to double-check the old set/get_color_cycle(), too
with warnings.catch_warnings():
warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
prop = next(ax._get_lines.prop_cycler)
ax.set_color_cycle(['c', 'm', 'y', 'k'])
assert prop != next(ax._get_lines.prop_cycler)
Expand Down
14 changes: 6 additions & 8 deletions lib/matplotlib/tests/test_simplification.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
import matplotlib.pyplot as plt

from pylab import *
import numpy as np
from matplotlib import patches, path, transforms

from nose.tools import raises
Expand All @@ -24,7 +22,7 @@
@image_comparison(baseline_images=['clipping'], remove_text=True)
def test_clipping():
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*pi*t)
s = np.sin(2*np.pi*t)

fig = plt.figure()
ax = fig.add_subplot(111)
Expand Down Expand Up @@ -101,16 +99,16 @@ def test_simplify_curve():
def test_hatch():
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_patch(Rectangle((0, 0), 1, 1, fill=False, hatch="/"))
ax.add_patch(plt.Rectangle((0, 0), 1, 1, fill=False, hatch="/"))
ax.set_xlim((0.45, 0.55))
ax.set_ylim((0.45, 0.55))

@image_comparison(baseline_images=['fft_peaks'], remove_text=True)
def test_fft_peaks():
fig = plt.figure()
t = arange(65536)
t = np.arange(65536)
ax = fig.add_subplot(111)
p1 = ax.plot(abs(fft(sin(2*pi*.01*t)*blackman(len(t)))))
p1 = ax.plot(abs(np.fft.fft(np.sin(2*np.pi*.01*t)*np.blackman(len(t)))))

path = p1[0].get_path()
transform = p1[0].get_transform()
Expand Down Expand Up @@ -163,7 +161,7 @@ def test_start_with_moveto():
@cleanup
@raises(OverflowError)
def test_throw_rendering_complexity_exceeded():
rcParams['path.simplify'] = False
plt.rcParams['path.simplify'] = False
xx = np.arange(200000)
yy = np.random.rand(200000)
yy[1000] = np.nan
Expand All @@ -173,7 +171,7 @@ def test_throw_rendering_complexity_exceeded():
try:
fig.savefig(io.BytesIO())
finally:
rcParams['path.simplify'] = True
plt.rcParams['path.simplify'] = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test has cleanup decorator on it, why there is a manual cleanup?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know; I just backported the warning fixes from master (i.e., removal of pylab.)


@image_comparison(baseline_images=['clipper_edge'], remove_text=True)
def test_clipper():
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_streamplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_masks_and_nans():
X, Y, U, V = velocity_field()
mask = np.zeros(U.shape, dtype=bool)
mask[40:60, 40:60] = 1
U = np.ma.array(U, mask=mask)
U[:20, :20] = np.nan
U = np.ma.array(U, mask=mask)
with np.errstate(invalid='ignore'):
plt.streamplot(X, Y, U, V, color=U, cmap=plt.cm.Blues)

Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ def test_ScalarFormatter_offset_value():
formatter = ax.get_xaxis().get_major_formatter()

def check_offset_for(left, right, offset):
ax.set_xlim(left, right)
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', 'Attempting to set identical',
UserWarning)
ax.set_xlim(left, right)
assert_equal(len(w), 1 if left == right else 0)

# Update ticks.
next(ax.get_xaxis().iter_ticks())
assert_equal(formatter.offset, offset)
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/tests/test_type1font.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def test_Type1Font():
font = t1f.Type1Font(filename)
slanted = font.transform({'slant': 1})
condensed = font.transform({'extend': 0.5})
rawdata = open(filename, 'rb').read()
with open(filename, 'rb') as fd:
rawdata = fd.read()
assert_equal(font.parts[0], rawdata[0x0006:0x10c5])
assert_equal(font.parts[1], rawdata[0x10cb:0x897f])
assert_equal(font.parts[2], rawdata[0x8985:0x8ba6])
Expand Down