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

Skip to content

Commit e92a280

Browse files
committed
Move pytest warning catchers
1 parent 8b54e6b commit e92a280

File tree

1 file changed

+106
-97
lines changed

1 file changed

+106
-97
lines changed

lib/matplotlib/tests/test_mlab.py

Lines changed: 106 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def test_colinear_pca():
3838
a = mlab.PCA._get_colinear()
3939
pca = mlab.PCA(a)
4040

41-
assert_allclose(pca.fracs[2:], 0., atol=1e-8)
42-
assert_allclose(pca.Y[:, 2:], 0., atol=1e-8)
41+
assert_allclose(pca.fracs[2:], 0., atol=1e-8)
42+
assert_allclose(pca.Y[:, 2:], 0., atol=1e-8)
4343

4444

4545
@pytest.mark.parametrize('input', [
@@ -82,9 +82,9 @@ def test_prctile(input, percentile):
8282
def test_logspace(xmin, xmax, N):
8383
with pytest.warns(MatplotlibDeprecationWarning):
8484
res = mlab.logspace(xmin, xmax, N)
85-
targ = np.logspace(np.log10(xmin), np.log10(xmax), N)
86-
assert_allclose(targ, res)
87-
assert res.size == N
85+
targ = np.logspace(np.log10(xmin), np.log10(xmax), N)
86+
assert_allclose(targ, res)
87+
assert res.size == N
8888

8989

9090
class TestStride(object):
@@ -222,43 +222,43 @@ def tempcsv():
222222

223223

224224
def test_recarray_csv_roundtrip(tempcsv):
225+
expected = np.recarray((99,),
226+
[(str('x'), float),
227+
(str('y'), float),
228+
(str('t'), float)])
229+
# initialising all values: uninitialised memory sometimes produces
230+
# floats that do not round-trip to string and back.
231+
expected['x'][:] = np.linspace(-1e9, -1, 99)
232+
expected['y'][:] = np.linspace(1, 1e9, 99)
233+
expected['t'][:] = np.linspace(0, 0.01, 99)
225234
with pytest.warns(MatplotlibDeprecationWarning):
226-
expected = np.recarray((99,),
227-
[(str('x'), float),
228-
(str('y'), float),
229-
(str('t'), float)])
230-
# initialising all values: uninitialised memory sometimes produces
231-
# floats that do not round-trip to string and back.
232-
expected['x'][:] = np.linspace(-1e9, -1, 99)
233-
expected['y'][:] = np.linspace(1, 1e9, 99)
234-
expected['t'][:] = np.linspace(0, 0.01, 99)
235-
236235
mlab.rec2csv(expected, tempcsv)
237236
tempcsv.seek(0)
238237
actual = mlab.csv2rec(tempcsv)
239238

240-
assert_allclose(expected['x'], actual['x'])
241-
assert_allclose(expected['y'], actual['y'])
242-
assert_allclose(expected['t'], actual['t'])
239+
assert_allclose(expected['x'], actual['x'])
240+
assert_allclose(expected['y'], actual['y'])
241+
assert_allclose(expected['t'], actual['t'])
243242

244243

245244
def test_rec2csv_bad_shape_ValueError(tempcsv):
246-
with pytest.warns(MatplotlibDeprecationWarning):
247-
bad = np.recarray((99, 4), [(str('x'), float),
248-
(str('y'), float)])
245+
bad = np.recarray((99, 4), [(str('x'), float),
246+
(str('y'), float)])
249247

250-
# the bad recarray should trigger a ValueError for having ndim > 1.
248+
# the bad recarray should trigger a ValueError for having ndim > 1.
249+
with pytest.warns(MatplotlibDeprecationWarning):
251250
with pytest.raises(ValueError):
252251
mlab.rec2csv(bad, tempcsv)
253252

254253

255254
def test_csv2rec_names_with_comments(tempcsv):
255+
256+
tempcsv.write('# comment\n1,2,3\n4,5,6\n')
257+
tempcsv.seek(0)
256258
with pytest.warns(MatplotlibDeprecationWarning):
257-
tempcsv.write('# comment\n1,2,3\n4,5,6\n')
258-
tempcsv.seek(0)
259259
array = mlab.csv2rec(tempcsv, names='a,b,c')
260-
assert len(array) == 2
261-
assert len(array.dtype) == 3
260+
assert len(array) == 2
261+
assert len(array.dtype) == 3
262262

263263

264264
@pytest.mark.parametrize('input, kwargs', [
@@ -282,31 +282,31 @@ def test_csv2rec_names_with_comments(tempcsv):
282282
{'yearfirst': True}),
283283
], ids=['usdate', 'dayfirst', 'yearfirst'])
284284
def test_csv2rec_dates(tempcsv, input, kwargs):
285+
tempcsv.write(input)
286+
expected = [datetime.datetime(2014, 1, 11, 0, 0),
287+
datetime.datetime(1976, 3, 5, 0, 0, 1),
288+
datetime.datetime(1983, 7, 9, 17, 17, 34),
289+
datetime.datetime(2054, 6, 20, 14, 31, 45),
290+
datetime.datetime(2000, 10, 31, 11, 50, 23)]
291+
tempcsv.seek(0)
285292
with pytest.warns(MatplotlibDeprecationWarning):
286-
tempcsv.write(input)
287-
expected = [datetime.datetime(2014, 1, 11, 0, 0),
288-
datetime.datetime(1976, 3, 5, 0, 0, 1),
289-
datetime.datetime(1983, 7, 9, 17, 17, 34),
290-
datetime.datetime(2054, 6, 20, 14, 31, 45),
291-
datetime.datetime(2000, 10, 31, 11, 50, 23)]
292-
tempcsv.seek(0)
293293
array = mlab.csv2rec(tempcsv, names='a', **kwargs)
294-
assert_array_equal(array['a'].tolist(), expected)
294+
assert_array_equal(array['a'].tolist(), expected)
295295

296296

297297
def test_rec2txt_basic():
298+
# str() calls around field names necessary b/c as of numpy 1.11
299+
# dtype doesn't like unicode names (caused by unicode_literals import)
300+
a = np.array([(1.0, 2, 'foo', 'bing'),
301+
(2.0, 3, 'bar', 'blah')],
302+
dtype=np.dtype([(str('x'), np.float32),
303+
(str('y'), np.int8),
304+
(str('s'), str, 3),
305+
(str('s2'), str, 4)]))
306+
truth = (' x y s s2\n'
307+
' 1.000 2 foo bing \n'
308+
' 2.000 3 bar blah ').splitlines()
298309
with pytest.warns(MatplotlibDeprecationWarning):
299-
# str() calls around field names necessary b/c as of numpy 1.11
300-
# dtype doesn't like unicode names (caused by unicode_literals import)
301-
a = np.array([(1.0, 2, 'foo', 'bing'),
302-
(2.0, 3, 'bar', 'blah')],
303-
dtype=np.dtype([(str('x'), np.float32),
304-
(str('y'), np.int8),
305-
(str('s'), str, 3),
306-
(str('s2'), str, 4)]))
307-
truth = (' x y s s2\n'
308-
' 1.000 2 foo bing \n'
309-
' 2.000 3 bar blah ').splitlines()
310310
assert mlab.rec2txt(a).splitlines() == truth
311311

312312

@@ -2145,72 +2145,77 @@ def test_psd_windowarray_equal(self):
21452145

21462146

21472147
def test_griddata_linear():
2148+
# z is a linear function of x and y.
2149+
def get_z(x, y):
2150+
return 3.0*x - y
2151+
2152+
# Passing 1D xi and yi arrays to griddata.
2153+
x = np.asarray([0.0, 1.0, 0.0, 1.0, 0.5])
2154+
y = np.asarray([0.0, 0.0, 1.0, 1.0, 0.5])
2155+
z = get_z(x, y)
2156+
xi = [0.2, 0.4, 0.6, 0.8]
2157+
yi = [0.1, 0.3, 0.7, 0.9]
21482158
with pytest.warns(MatplotlibDeprecationWarning):
2149-
# z is a linear function of x and y.
2150-
def get_z(x, y):
2151-
return 3.0*x - y
2152-
2153-
# Passing 1D xi and yi arrays to griddata.
2154-
x = np.asarray([0.0, 1.0, 0.0, 1.0, 0.5])
2155-
y = np.asarray([0.0, 0.0, 1.0, 1.0, 0.5])
2156-
z = get_z(x, y)
2157-
xi = [0.2, 0.4, 0.6, 0.8]
2158-
yi = [0.1, 0.3, 0.7, 0.9]
21592159
zi = mlab.griddata(x, y, z, xi, yi, interp='linear')
2160-
xi, yi = np.meshgrid(xi, yi)
2161-
np.testing.assert_array_almost_equal(zi, get_z(xi, yi))
2160+
xi, yi = np.meshgrid(xi, yi)
2161+
np.testing.assert_array_almost_equal(zi, get_z(xi, yi))
21622162

2163-
# Passing 2D xi and yi arrays to griddata.
2163+
# Passing 2D xi and yi arrays to griddata.
2164+
with pytest.warns(MatplotlibDeprecationWarning):
21642165
zi = mlab.griddata(x, y, z, xi, yi, interp='linear')
2165-
np.testing.assert_array_almost_equal(zi, get_z(xi, yi))
2166+
np.testing.assert_array_almost_equal(zi, get_z(xi, yi))
21662167

2167-
# Masking z array.
2168-
z_masked = np.ma.array(z, mask=[False, False, False, True, False])
2169-
correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, get_z(xi, yi))
2168+
# Masking z array.
2169+
z_masked = np.ma.array(z, mask=[False, False, False, True, False])
2170+
correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, get_z(xi, yi))
2171+
with pytest.warns(MatplotlibDeprecationWarning):
21702172
zi = mlab.griddata(x, y, z_masked, xi, yi, interp='linear')
2171-
matest.assert_array_almost_equal(zi, correct_zi_masked)
2172-
np.testing.assert_array_equal(np.ma.getmask(zi),
2173-
np.ma.getmask(correct_zi_masked))
2173+
matest.assert_array_almost_equal(zi, correct_zi_masked)
2174+
np.testing.assert_array_equal(np.ma.getmask(zi),
2175+
np.ma.getmask(correct_zi_masked))
21742176

21752177

21762178
@pytest.mark.xfail(not HAS_NATGRID, reason='natgrid not installed')
21772179
def test_griddata_nn():
2180+
# z is a linear function of x and y.
2181+
def get_z(x, y):
2182+
return 3.0*x - y
2183+
2184+
# Passing 1D xi and yi arrays to griddata.
2185+
x = np.asarray([0.0, 1.0, 0.0, 1.0, 0.5])
2186+
y = np.asarray([0.0, 0.0, 1.0, 1.0, 0.5])
2187+
z = get_z(x, y)
2188+
xi = [0.2, 0.4, 0.6, 0.8]
2189+
yi = [0.1, 0.3, 0.7, 0.9]
2190+
correct_zi = [[0.49999252, 1.0999978, 1.7000030, 2.3000080],
2191+
[0.29999208, 0.8999978, 1.5000029, 2.1000059],
2192+
[-0.1000099, 0.4999943, 1.0999964, 1.6999979],
2193+
[-0.3000128, 0.2999894, 0.8999913, 1.4999933]]
21782194
with pytest.warns(MatplotlibDeprecationWarning):
2179-
# z is a linear function of x and y.
2180-
def get_z(x, y):
2181-
return 3.0*x - y
2182-
2183-
# Passing 1D xi and yi arrays to griddata.
2184-
x = np.asarray([0.0, 1.0, 0.0, 1.0, 0.5])
2185-
y = np.asarray([0.0, 0.0, 1.0, 1.0, 0.5])
2186-
z = get_z(x, y)
2187-
xi = [0.2, 0.4, 0.6, 0.8]
2188-
yi = [0.1, 0.3, 0.7, 0.9]
2189-
correct_zi = [[0.49999252, 1.0999978, 1.7000030, 2.3000080],
2190-
[0.29999208, 0.8999978, 1.5000029, 2.1000059],
2191-
[-0.1000099, 0.4999943, 1.0999964, 1.6999979],
2192-
[-0.3000128, 0.2999894, 0.8999913, 1.4999933]]
21932195
zi = mlab.griddata(x, y, z, xi, yi, interp='nn')
2194-
np.testing.assert_array_almost_equal(zi, correct_zi, 5)
2196+
np.testing.assert_array_almost_equal(zi, correct_zi, 5)
21952197

2198+
with pytest.warns(MatplotlibDeprecationWarning):
21962199
# Decreasing xi or yi should raise ValueError.
21972200
with pytest.raises(ValueError):
21982201
mlab.griddata(x, y, z, xi[::-1], yi, interp='nn')
21992202
with pytest.raises(ValueError):
22002203
mlab.griddata(x, y, z, xi, yi[::-1], interp='nn')
22012204

2202-
# Passing 2D xi and yi arrays to griddata.
2203-
xi, yi = np.meshgrid(xi, yi)
2205+
# Passing 2D xi and yi arrays to griddata.
2206+
xi, yi = np.meshgrid(xi, yi)
2207+
with pytest.warns(MatplotlibDeprecationWarning):
22042208
zi = mlab.griddata(x, y, z, xi, yi, interp='nn')
2205-
np.testing.assert_array_almost_equal(zi, correct_zi, 5)
2209+
np.testing.assert_array_almost_equal(zi, correct_zi, 5)
22062210

2207-
# Masking z array.
2208-
z_masked = np.ma.array(z, mask=[False, False, False, True, False])
2209-
correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, correct_zi)
2211+
# Masking z array.
2212+
z_masked = np.ma.array(z, mask=[False, False, False, True, False])
2213+
correct_zi_masked = np.ma.masked_where(xi + yi > 1.0, correct_zi)
2214+
with pytest.warns(MatplotlibDeprecationWarning):
22102215
zi = mlab.griddata(x, y, z_masked, xi, yi, interp='nn')
2211-
np.testing.assert_array_almost_equal(zi, correct_zi_masked, 5)
2212-
np.testing.assert_array_equal(np.ma.getmask(zi),
2213-
np.ma.getmask(correct_zi_masked))
2216+
np.testing.assert_array_almost_equal(zi, correct_zi_masked, 5)
2217+
np.testing.assert_array_equal(np.ma.getmask(zi),
2218+
np.ma.getmask(correct_zi_masked))
22142219

22152220

22162221
#*****************************************************************
@@ -2411,22 +2416,26 @@ def test_contiguous_regions():
24112416
# Starts and ends with True
24122417
mask = [True]*a + [False]*b + [True]*c
24132418
expected = [(0, a), (a+b, a+b+c)]
2414-
assert mlab.contiguous_regions(mask) == expected
2419+
with pytest.warns(MatplotlibDeprecationWarning):
2420+
assert mlab.contiguous_regions(mask) == expected
24152421
d, e = 6, 7
24162422
# Starts with True ends with False
24172423
mask = mask + [False]*e
2418-
assert mlab.contiguous_regions(mask) == expected
2424+
with pytest.warns(MatplotlibDeprecationWarning):
2425+
assert mlab.contiguous_regions(mask) == expected
24192426
# Starts with False ends with True
24202427
mask = [False]*d + mask[:-e]
24212428
expected = [(d, d+a), (d+a+b, d+a+b+c)]
2422-
assert mlab.contiguous_regions(mask) == expected
2429+
with pytest.warns(MatplotlibDeprecationWarning):
2430+
assert mlab.contiguous_regions(mask) == expected
24232431
# Starts and ends with False
24242432
mask = mask + [False]*e
2425-
assert mlab.contiguous_regions(mask) == expected
2426-
# No True in mask
2427-
assert mlab.contiguous_regions([False]*5) == []
2428-
# Empty mask
2429-
assert mlab.contiguous_regions([]) == []
2433+
with pytest.warns(MatplotlibDeprecationWarning):
2434+
assert mlab.contiguous_regions(mask) == expected
2435+
# No True in mask
2436+
assert mlab.contiguous_regions([False]*5) == []
2437+
# Empty mask
2438+
assert mlab.contiguous_regions([]) == []
24302439

24312440

24322441
def test_psd_onesided_norm():

0 commit comments

Comments
 (0)