From cd98df16a3eb2630742d05338729dd67bcc09b88 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 19 Jan 2017 20:59:56 -0500 Subject: [PATCH 1/4] Run nose2pytest on mlab tests. --- lib/matplotlib/tests/test_mlab.py | 223 +++++++++++++++--------------- 1 file changed, 111 insertions(+), 112 deletions(-) diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index 796b220f495c..73369d0ae513 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -9,8 +9,7 @@ import numpy.ma.testutils as matest import numpy as np import datetime as datetime -from nose.tools import (assert_equal, assert_almost_equal, assert_not_equal, - assert_true, assert_raises) +from nose.tools import (assert_almost_equal, assert_raises) import matplotlib.mlab as mlab import matplotlib.cbook as cbook @@ -35,11 +34,11 @@ def test_colinear_pca(self): def test_prctile(self): # test odd lengths x = [1, 2, 3] - assert_equal(mlab.prctile(x, 50), np.median(x)) + assert mlab.prctile(x, 50) == np.median(x) # test even lengths x = [1, 2, 3, 4] - assert_equal(mlab.prctile(x, 50), np.median(x)) + assert mlab.prctile(x, 50) == np.median(x) # derived from email sent by jason-sage to MPL-user on 20090914 ob1 = [1, 1, 2, 2, 1, 2, 4, 3, 2, 2, 2, 3, @@ -89,7 +88,7 @@ def test_logspace_none(self): res = mlab.logspace(xmin, xmax, N) targ = np.logspace(np.log10(xmin), np.log10(xmax), N) assert_array_equal(targ, res) - assert_equal(res.size, 0) + assert res.size == 0 def test_logspace_single(self): xmin = .03 @@ -98,7 +97,7 @@ def test_logspace_single(self): res = mlab.logspace(xmin, xmax, N) targ = np.logspace(np.log10(xmin), np.log10(xmax), N) assert_array_equal(targ, res) - assert_equal(res.size, 1) + assert res.size == 1 class Test_stride(CleanupTestCase): @@ -165,106 +164,106 @@ def test_stride_repeat_n_lt_1_ValueError(self): def test_stride_repeat_n1_axis0(self): x = np.arange(10) y = mlab.stride_repeat(x, 1) - assert_equal((1, ) + x.shape, y.shape) + assert (1, ) + x.shape == y.shape assert_array_equal(x, y.flat) - assert_true(self.get_base(y) is x) + assert self.get_base(y) is x def test_stride_repeat_n1_axis1(self): x = np.arange(10) y = mlab.stride_repeat(x, 1, axis=1) - assert_equal(x.shape + (1, ), y.shape) + assert x.shape + (1, ) == y.shape assert_array_equal(x, y.flat) - assert_true(self.get_base(y) is x) + assert self.get_base(y) is x def test_stride_repeat_n5_axis0(self): x = np.arange(10) y = mlab.stride_repeat(x, 5) yr = np.repeat(x[np.newaxis], 5, axis=0) - assert_equal(yr.shape, y.shape) + assert yr.shape == y.shape assert_array_equal(yr, y) - assert_equal((5, ) + x.shape, y.shape) - assert_true(self.get_base(y) is x) + assert (5, ) + x.shape == y.shape + assert self.get_base(y) is x def test_stride_repeat_n5_axis1(self): x = np.arange(10) y = mlab.stride_repeat(x, 5, axis=1) yr = np.repeat(x[np.newaxis], 5, axis=0).T - assert_equal(yr.shape, y.shape) + assert yr.shape == y.shape assert_array_equal(yr, y) - assert_equal(x.shape + (5, ), y.shape) - assert_true(self.get_base(y) is x) + assert x.shape + (5, ) == y.shape + assert self.get_base(y) is x def test_stride_windows_n1_noverlap0_axis0(self): x = np.arange(10) y = mlab.stride_windows(x, 1) yt = self.calc_window_target(x, 1) - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal((1, ) + x.shape, y.shape) - assert_true(self.get_base(y) is x) + assert (1, ) + x.shape == y.shape + assert self.get_base(y) is x def test_stride_windows_n1_noverlap0_axis1(self): x = np.arange(10) y = mlab.stride_windows(x, 1, axis=1) yt = self.calc_window_target(x, 1).T - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal(x.shape + (1, ), y.shape) - assert_true(self.get_base(y) is x) + assert x.shape + (1, ) == y.shape + assert self.get_base(y) is x def test_stride_windows_n5_noverlap0_axis0(self): x = np.arange(100) y = mlab.stride_windows(x, 5) yt = self.calc_window_target(x, 5) - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal((5, 20), y.shape) - assert_true(self.get_base(y) is x) + assert (5, 20) == y.shape + assert self.get_base(y) is x def test_stride_windows_n5_noverlap0_axis1(self): x = np.arange(100) y = mlab.stride_windows(x, 5, axis=1) yt = self.calc_window_target(x, 5).T - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal((20, 5), y.shape) - assert_true(self.get_base(y) is x) + assert (20, 5) == y.shape + assert self.get_base(y) is x def test_stride_windows_n15_noverlap2_axis0(self): x = np.arange(100) y = mlab.stride_windows(x, 15, 2) yt = self.calc_window_target(x, 15, 2) - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal((15, 7), y.shape) - assert_true(self.get_base(y) is x) + assert (15, 7) == y.shape + assert self.get_base(y) is x def test_stride_windows_n15_noverlap2_axis1(self): x = np.arange(100) y = mlab.stride_windows(x, 15, 2, axis=1) yt = self.calc_window_target(x, 15, 2).T - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal((7, 15), y.shape) - assert_true(self.get_base(y) is x) + assert (7, 15) == y.shape + assert self.get_base(y) is x def test_stride_windows_n13_noverlapn3_axis0(self): x = np.arange(100) y = mlab.stride_windows(x, 13, -3) yt = self.calc_window_target(x, 13, -3) - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal((13, 6), y.shape) - assert_true(self.get_base(y) is x) + assert (13, 6) == y.shape + assert self.get_base(y) is x def test_stride_windows_n13_noverlapn3_axis1(self): x = np.arange(100) y = mlab.stride_windows(x, 13, -3, axis=1) yt = self.calc_window_target(x, 13, -3).T - assert_equal(yt.shape, y.shape) + assert yt.shape == y.shape assert_array_equal(yt, y) - assert_equal((6, 13), y.shape) - assert_true(self.get_base(y) is x) + assert (6, 13) == y.shape + assert self.get_base(y) is x def test_stride_windows_n32_noverlap0_axis0_unflatten(self): n = 32 @@ -272,7 +271,7 @@ def test_stride_windows_n32_noverlap0_axis0_unflatten(self): x1 = np.tile(x, (21, 1)) x2 = x1.flatten() y = mlab.stride_windows(x2, n) - assert_equal(y.shape, x1.T.shape) + assert y.shape == x1.T.shape assert_array_equal(y, x1.T) def test_stride_windows_n32_noverlap0_axis1_unflatten(self): @@ -281,7 +280,7 @@ def test_stride_windows_n32_noverlap0_axis1_unflatten(self): x1 = np.tile(x, (21, 1)) x2 = x1.flatten() y = mlab.stride_windows(x2, n, axis=1) - assert_equal(y.shape, x1.shape) + assert y.shape == x1.shape assert_array_equal(y, x1) def test_stride_ensure_integer_type(self): @@ -406,7 +405,7 @@ def test_csv2txt_basic(self): truth = (' x y s s2\n' ' 1.000 2 foo bing \n' ' 2.000 3 bar blah ').splitlines() - assert_equal(mlab.rec2txt(a).splitlines(), truth) + assert mlab.rec2txt(a).splitlines() == truth class Test_window(CleanupTestCase): @@ -487,8 +486,8 @@ def test_apply_window_hanning_1D(self): window1 = mlab.window_hanning(np.ones(x.shape[0])) y, window2 = mlab.apply_window(x, window, return_window=True) yt = window(x) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) @@ -497,8 +496,8 @@ def test_apply_window_hanning_1D_axis0(self): window = mlab.window_hanning y = mlab.apply_window(x, window, axis=0, return_window=False) yt = window(x) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_els_1D_axis0(self): @@ -507,8 +506,8 @@ def test_apply_window_hanning_els_1D_axis0(self): window1 = mlab.window_hanning y = mlab.apply_window(x, window, axis=0, return_window=False) yt = window1(x) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D_axis0(self): @@ -518,8 +517,8 @@ def test_apply_window_hanning_2D_axis0(self): yt = np.zeros_like(x) for i in range(x.shape[1]): yt[:, i] = window(x[:, i]) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_els1_2D_axis0(self): @@ -530,8 +529,8 @@ def test_apply_window_hanning_els1_2D_axis0(self): yt = np.zeros_like(x) for i in range(x.shape[1]): yt[:, i] = window1(x[:, i]) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_els2_2D_axis0(self): @@ -542,8 +541,8 @@ def test_apply_window_hanning_els2_2D_axis0(self): yt = np.zeros_like(x) for i in range(x.shape[1]): yt[:, i] = window1*x[:, i] - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) @@ -553,8 +552,8 @@ def test_apply_window_hanning_els3_2D_axis0(self): window1 = mlab.window_hanning(np.ones(x.shape[0])) y, window2 = mlab.apply_window(x, window, axis=0, return_window=True) yt = mlab.apply_window(x, window1, axis=0, return_window=False) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) @@ -565,8 +564,8 @@ def test_apply_window_hanning_2D_axis1(self): yt = np.zeros_like(x) for i in range(x.shape[0]): yt[i, :] = window(x[i, :]) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D__els1_axis1(self): @@ -577,8 +576,8 @@ def test_apply_window_hanning_2D__els1_axis1(self): yt = np.zeros_like(x) for i in range(x.shape[0]): yt[i, :] = window1(x[i, :]) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D_els2_axis1(self): @@ -589,8 +588,8 @@ def test_apply_window_hanning_2D_els2_axis1(self): yt = np.zeros_like(x) for i in range(x.shape[0]): yt[i, :] = window1 * x[i, :] - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) assert_array_equal(window1, window2) @@ -600,8 +599,8 @@ def test_apply_window_hanning_2D_els3_axis1(self): window1 = mlab.window_hanning(np.ones(x.shape[1])) y = mlab.apply_window(x, window, axis=1, return_window=False) yt = mlab.apply_window(x, window1, axis=1, return_window=False) - assert_equal(yt.shape, y.shape) - assert_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape == y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_stride_windows_hanning_2D_n13_noverlapn3_axis0(self): @@ -610,8 +609,8 @@ def test_apply_window_stride_windows_hanning_2D_n13_noverlapn3_axis0(self): yi = mlab.stride_windows(x, n=13, noverlap=2, axis=0) y = mlab.apply_window(yi, window, axis=0, return_window=False) yt = self.check_window_apply_repeat(x, window, 13, 2) - assert_equal(yt.shape, y.shape) - assert_not_equal(x.shape, y.shape) + assert yt.shape == y.shape + assert x.shape != y.shape assert_allclose(yt, y, atol=1e-06) def test_apply_window_hanning_2D_stack_axis1(self): @@ -687,31 +686,31 @@ def test_detrend_none_0D_zeros(self): input = 0. targ = input res = mlab.detrend_none(input) - assert_equal(input, targ) + assert input == targ def test_detrend_none_0D_zeros_axis1(self): input = 0. targ = input res = mlab.detrend_none(input, axis=1) - assert_equal(input, targ) + assert input == targ def test_detrend_str_none_0D_zeros(self): input = 0. targ = input res = mlab.detrend(input, key='none') - assert_equal(input, targ) + assert input == targ def test_detrend_detrend_none_0D_zeros(self): input = 0. targ = input res = mlab.detrend(input, key=mlab.detrend_none) - assert_equal(input, targ) + assert input == targ def test_detrend_none_0D_off(self): input = 5.5 targ = input res = mlab.detrend_none(input) - assert_equal(input, targ) + assert input == targ def test_detrend_none_1D_off(self): input = self.sig_off @@ -735,7 +734,7 @@ def test_detrend_none_1D_base_slope_off_list(self): input = self.sig_base + self.sig_slope + self.sig_off targ = input.tolist() res = mlab.detrend_none(input.tolist()) - assert_equal(res, targ) + assert res == targ def test_detrend_none_2D(self): arri = [self.sig_base, @@ -1459,13 +1458,13 @@ def createStim(self, fstims, iscomplex, sides, nsides, len_x=None, self.NFFT_density_real = NFFT_density_real def check_freqs(self, vals, targfreqs, resfreqs, fstims): - assert_true(resfreqs.argmin() == 0) - assert_true(resfreqs.argmax() == len(resfreqs)-1) + assert resfreqs.argmin() == 0 + assert resfreqs.argmax() == len(resfreqs)-1 assert_allclose(resfreqs, targfreqs, atol=1e-06) for fstim in fstims: i = np.abs(resfreqs - fstim).argmin() - assert_true(vals[i] > vals[i+2]) - assert_true(vals[i] > vals[i-2]) + assert vals[i] > vals[i+2] + assert vals[i] > vals[i-2] def check_maxfreq(self, spec, fsp, fstims): # skip the test if there are no frequencies @@ -1559,8 +1558,8 @@ def test_spectral_helper_psd(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_density, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] def test_spectral_helper_magnitude_specgram(self): freqs = self.freqs_specgram @@ -1575,8 +1574,8 @@ def test_spectral_helper_magnitude_specgram(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] def test_spectral_helper_magnitude_magnitude_spectrum(self): freqs = self.freqs_spectrum @@ -1591,8 +1590,8 @@ def test_spectral_helper_magnitude_magnitude_spectrum(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_spectrum, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], 1) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == 1 def test_csd(self): freqs = self.freqs_density @@ -1603,7 +1602,7 @@ def test_csd(self): pad_to=self.pad_to_density, sides=self.sides) assert_allclose(fsp, freqs, atol=1e-06) - assert_equal(spec.shape, freqs.shape) + assert spec.shape == freqs.shape def test_psd(self): freqs = self.freqs_density @@ -1613,7 +1612,7 @@ def test_psd(self): noverlap=self.nover_density, pad_to=self.pad_to_density, sides=self.sides) - assert_equal(spec.shape, freqs.shape) + assert spec.shape == freqs.shape self.check_freqs(spec, freqs, fsp, self.fstims) def test_psd_detrend_mean_func_offset(self): @@ -1863,7 +1862,7 @@ def test_psd_windowarray(self): sides=self.sides, window=np.ones(self.NFFT_density_real)) assert_allclose(fsp, freqs, atol=1e-06) - assert_equal(spec.shape, freqs.shape) + assert spec.shape == freqs.shape def test_psd_windowarray_scale_by_freq(self): freqs = self.freqs_density @@ -1906,7 +1905,7 @@ def test_complex_spectrum(self): sides=self.sides, pad_to=self.pad_to_spectrum) assert_allclose(fsp, freqs, atol=1e-06) - assert_equal(spec.shape, freqs.shape) + assert spec.shape == freqs.shape def test_magnitude_spectrum(self): freqs = self.freqs_spectrum @@ -1914,7 +1913,7 @@ def test_magnitude_spectrum(self): Fs=self.Fs, sides=self.sides, pad_to=self.pad_to_spectrum) - assert_equal(spec.shape, freqs.shape) + assert spec.shape == freqs.shape self.check_maxfreq(spec, fsp, self.fstims) self.check_freqs(spec, freqs, fsp, self.fstims) @@ -1925,7 +1924,7 @@ def test_angle_spectrum(self): sides=self.sides, pad_to=self.pad_to_spectrum) assert_allclose(fsp, freqs, atol=1e-06) - assert_equal(spec.shape, freqs.shape) + assert spec.shape == freqs.shape def test_phase_spectrum(self): freqs = self.freqs_spectrum @@ -1934,7 +1933,7 @@ def test_phase_spectrum(self): sides=self.sides, pad_to=self.pad_to_spectrum) assert_allclose(fsp, freqs, atol=1e-06) - assert_equal(spec.shape, freqs.shape) + assert spec.shape == freqs.shape def test_specgram_auto(self): freqs = self.freqs_specgram @@ -1949,8 +1948,8 @@ def test_specgram_auto(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] # since we are using a single freq, all time slices # should be about the same @@ -1973,8 +1972,8 @@ def test_specgram_default(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] # since we are using a single freq, all time slices # should be about the same @@ -1997,8 +1996,8 @@ def test_specgram_psd(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] # since we are using a single freq, all time slices # should be about the same if np.abs(spec.max()) != 0: @@ -2019,8 +2018,8 @@ def test_specgram_complex(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] self.check_freqs(specm, freqs, fsp, self.fstims) @@ -2037,8 +2036,8 @@ def test_specgram_magnitude(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] # since we are using a single freq, all time slices # should be about the same if np.abs(spec.max()) != 0: @@ -2059,8 +2058,8 @@ def test_specgram_angle(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] def test_specgram_phase(self): freqs = self.freqs_specgram @@ -2076,8 +2075,8 @@ def test_specgram_phase(self): assert_allclose(fsp, freqs, atol=1e-06) assert_allclose(t, self.t_specgram, atol=1e-06) - assert_equal(spec.shape[0], freqs.shape[0]) - assert_equal(spec.shape[1], self.t_specgram.shape[0]) + assert spec.shape[0] == freqs.shape[0] + assert spec.shape[1] == self.t_specgram.shape[0] def test_psd_csd_equal(self): freqs = self.freqs_density @@ -2953,7 +2952,7 @@ def test_scalar_covariance_dataset(self): multidim_data = [np.random.randn(n_basesample) for i in range(5)] kde = mlab.GaussianKDE(multidim_data, bw_method=0.5) - assert_equal(kde.covariance_factor(), 0.5) + assert kde.covariance_factor() == 0.5 def test_callable_covariance_dataset(self): """Use a multi-dimensional array as the dataset and test the callable's @@ -2965,7 +2964,7 @@ def test_callable_covariance_dataset(self): def callable_fun(x): return 0.55 kde = mlab.GaussianKDE(multidim_data, bw_method=callable_fun) - assert_equal(kde.covariance_factor(), 0.55) + assert kde.covariance_factor() == 0.55 def test_callable_singledim_dataset(self): """Use a single-dimensional array as the dataset and test the @@ -3042,22 +3041,22 @@ def test_contiguous_regions(): # Starts and ends with True mask = [True]*a + [False]*b + [True]*c expected = [(0, a), (a+b, a+b+c)] - assert_equal(mlab.contiguous_regions(mask), expected) + assert mlab.contiguous_regions(mask) == expected d, e = 6, 7 # Starts with True ends with False mask = mask + [False]*e - assert_equal(mlab.contiguous_regions(mask), expected) + assert mlab.contiguous_regions(mask) == expected # Starts with False ends with True mask = [False]*d + mask[:-e] expected = [(d, d+a), (d+a+b, d+a+b+c)] - assert_equal(mlab.contiguous_regions(mask), expected) + assert mlab.contiguous_regions(mask) == expected # Starts and ends with False mask = mask + [False]*e - assert_equal(mlab.contiguous_regions(mask), expected) + assert mlab.contiguous_regions(mask) == expected # No True in mask - assert_equal(mlab.contiguous_regions([False]*5), []) + assert mlab.contiguous_regions([False]*5) == [] # Empty mask - assert_equal(mlab.contiguous_regions([]), []) + assert mlab.contiguous_regions([]) == [] def test_psd_onesided_norm(): From 54f36837658490f78238dc32124930b70e401729 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 19 Jan 2017 22:19:33 -0500 Subject: [PATCH 2/4] Use pytest.raises instead of nose in mlab tests. --- lib/matplotlib/tests/test_mlab.py | 185 ++++++++++++++++++------------ 1 file changed, 109 insertions(+), 76 deletions(-) diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index 73369d0ae513..680a14c1c702 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -9,7 +9,8 @@ import numpy.ma.testutils as matest import numpy as np import datetime as datetime -from nose.tools import (assert_almost_equal, assert_raises) +import pytest +from nose.tools import (assert_almost_equal) import matplotlib.mlab as mlab import matplotlib.cbook as cbook @@ -123,43 +124,53 @@ def calc_window_target(self, x, NFFT, noverlap=0): def test_stride_windows_2D_ValueError(self): x = np.arange(10)[np.newaxis] - assert_raises(ValueError, mlab.stride_windows, x, 5) + with pytest.raises(ValueError): + mlab.stride_windows(x, 5) def test_stride_windows_0D_ValueError(self): x = np.array(0) - assert_raises(ValueError, mlab.stride_windows, x, 5) + with pytest.raises(ValueError): + mlab.stride_windows(x, 5) def test_stride_windows_noverlap_gt_n_ValueError(self): x = np.arange(10) - assert_raises(ValueError, mlab.stride_windows, x, 2, 3) + with pytest.raises(ValueError): + mlab.stride_windows(x, 2, 3) def test_stride_windows_noverlap_eq_n_ValueError(self): x = np.arange(10) - assert_raises(ValueError, mlab.stride_windows, x, 2, 2) + with pytest.raises(ValueError): + mlab.stride_windows(x, 2, 2) def test_stride_windows_n_gt_lenx_ValueError(self): x = np.arange(10) - assert_raises(ValueError, mlab.stride_windows, x, 11) + with pytest.raises(ValueError): + mlab.stride_windows(x, 11) def test_stride_windows_n_lt_1_ValueError(self): x = np.arange(10) - assert_raises(ValueError, mlab.stride_windows, x, 0) + with pytest.raises(ValueError): + mlab.stride_windows(x, 0) def test_stride_repeat_2D_ValueError(self): x = np.arange(10)[np.newaxis] - assert_raises(ValueError, mlab.stride_repeat, x, 5) + with pytest.raises(ValueError): + mlab.stride_repeat(x, 5) def test_stride_repeat_axis_lt_0_ValueError(self): x = np.array(0) - assert_raises(ValueError, mlab.stride_repeat, x, 5, axis=-1) + with pytest.raises(ValueError): + mlab.stride_repeat(x, 5, axis=-1) def test_stride_repeat_axis_gt_1_ValueError(self): x = np.array(0) - assert_raises(ValueError, mlab.stride_repeat, x, 5, axis=2) + with pytest.raises(ValueError): + mlab.stride_repeat(x, 5, axis=2) def test_stride_repeat_n_lt_1_ValueError(self): x = np.arange(10) - assert_raises(ValueError, mlab.stride_repeat, x, 0) + with pytest.raises(ValueError): + mlab.stride_repeat(x, 0) def test_stride_repeat_n1_axis0(self): x = np.arange(10) @@ -337,7 +348,8 @@ def test_rec2csv_bad_shape_ValueError(self): (str('y'), float)]) # the bad recarray should trigger a ValueError for having ndim > 1. - assert_raises(ValueError, mlab.rec2csv, bad, self.fd) + with pytest.raises(ValueError): + mlab.rec2csv(bad, self.fd) def test_csv2rec_names_with_comments(self): self.fd.write('# comment\n1,2,3\n4,5,6\n') @@ -460,25 +472,26 @@ def test_window_hanning_ones(self): def test_apply_window_1D_axis1_ValueError(self): x = self.sig_rand window = mlab.window_hanning - assert_raises(ValueError, mlab.apply_window, x, window, axis=1, - return_window=False) + with pytest.raises(ValueError): + mlab.apply_window(x, window, axis=1, return_window=False) def test_apply_window_1D_els_wrongsize_ValueError(self): x = self.sig_rand window = mlab.window_hanning(np.ones(x.shape[0]-1)) - assert_raises(ValueError, mlab.apply_window, x, window) + with pytest.raises(ValueError): + mlab.apply_window(x, window) def test_apply_window_0D_ValueError(self): x = np.array(0) window = mlab.window_hanning - assert_raises(ValueError, mlab.apply_window, x, window, axis=1, - return_window=False) + with pytest.raises(ValueError): + mlab.apply_window(x, window, axis=1, return_window=False) def test_apply_window_3D_ValueError(self): x = self.sig_rand[np.newaxis][np.newaxis] window = mlab.window_hanning - assert_raises(ValueError, mlab.apply_window, x, window, axis=1, - return_window=False) + with pytest.raises(ValueError): + mlab.apply_window(x, window, axis=1, return_window=False) def test_apply_window_hanning_1D(self): x = self.sig_rand @@ -1157,43 +1170,53 @@ def test_demean_2D_axism1(self): def test_detrend_bad_key_str_ValueError(self): input = self.sig_slope[np.newaxis] - assert_raises(ValueError, mlab.detrend, input, key='spam') + with pytest.raises(ValueError): + mlab.detrend(input, key='spam') def test_detrend_bad_key_var_ValueError(self): input = self.sig_slope[np.newaxis] - assert_raises(ValueError, mlab.detrend, input, key=5) + with pytest.raises(ValueError): + mlab.detrend(input, key=5) def test_detrend_mean_0D_d0_ValueError(self): input = 5.5 - assert_raises(ValueError, mlab.detrend_mean, input, axis=0) + with pytest.raises(ValueError): + mlab.detrend_mean(input, axis=0) def test_detrend_0D_d0_ValueError(self): input = 5.5 - assert_raises(ValueError, mlab.detrend, input, axis=0) + with pytest.raises(ValueError): + mlab.detrend(input, axis=0) def test_detrend_mean_1D_d1_ValueError(self): input = self.sig_slope - assert_raises(ValueError, mlab.detrend_mean, input, axis=1) + with pytest.raises(ValueError): + mlab.detrend_mean(input, axis=1) def test_detrend_1D_d1_ValueError(self): input = self.sig_slope - assert_raises(ValueError, mlab.detrend, input, axis=1) + with pytest.raises(ValueError): + mlab.detrend(input, axis=1) def test_demean_1D_d1_ValueError(self): input = self.sig_slope - assert_raises(ValueError, mlab.demean, input, axis=1) + with pytest.raises(ValueError): + mlab.demean(input, axis=1) def test_detrend_mean_2D_d2_ValueError(self): input = self.sig_slope[np.newaxis] - assert_raises(ValueError, mlab.detrend_mean, input, axis=2) + with pytest.raises(ValueError): + mlab.detrend_mean(input, axis=2) def test_detrend_2D_d2_ValueError(self): input = self.sig_slope[np.newaxis] - assert_raises(ValueError, mlab.detrend, input, axis=2) + with pytest.raises(ValueError): + mlab.detrend(input, axis=2) def test_demean_2D_d2_ValueError(self): input = self.sig_slope[np.newaxis] - assert_raises(ValueError, mlab.demean, input, axis=2) + with pytest.raises(ValueError): + mlab.demean(input, axis=2) def test_detrend_linear_0D_zeros(self): input = 0. @@ -1257,7 +1280,8 @@ def test_detrend_linear_1d_slope_off_list(self): def test_detrend_linear_2D_ValueError(self): input = self.sig_slope[np.newaxis] - assert_raises(ValueError, mlab.detrend_linear, input) + with pytest.raises(ValueError): + mlab.detrend_linear(input) def test_detrend_str_linear_2d_slope_off_axis0(self): arri = [self.sig_off, @@ -1492,58 +1516,59 @@ def check_maxfreq(self, spec, fsp, fstims): def test_spectral_helper_raises_complex_same_data(self): # test that mode 'complex' cannot be used if x is not y - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, y=self.y+1, mode='complex') + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, y=self.y+1, mode='complex') def test_spectral_helper_raises_magnitude_same_data(self): # test that mode 'magnitude' cannot be used if x is not y - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, y=self.y+1, mode='magnitude') + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, y=self.y+1, mode='magnitude') def test_spectral_helper_raises_angle_same_data(self): # test that mode 'angle' cannot be used if x is not y - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, y=self.y+1, mode='angle') + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, y=self.y+1, mode='angle') def test_spectral_helper_raises_phase_same_data(self): # test that mode 'phase' cannot be used if x is not y - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, y=self.y+1, mode='phase') + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, y=self.y+1, mode='phase') def test_spectral_helper_raises_unknown_mode(self): # test that unknown value for mode cannot be used - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, mode='spam') + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, mode='spam') def test_spectral_helper_raises_unknown_sides(self): # test that unknown value for sides cannot be used - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, y=self.y, sides='eggs') + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, y=self.y, sides='eggs') def test_spectral_helper_raises_noverlap_gt_NFFT(self): # test that noverlap cannot be larger than NFFT - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, y=self.y, NFFT=10, noverlap=20) + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, y=self.y, NFFT=10, noverlap=20) def test_spectral_helper_raises_noverlap_eq_NFFT(self): # test that noverlap cannot be equal to NFFT - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, NFFT=10, noverlap=10) + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, NFFT=10, noverlap=10) def test_spectral_helper_raises_winlen_ne_NFFT(self): # test that the window length cannot be different from NFFT - assert_raises(ValueError, mlab._spectral_helper, - x=self.y, y=self.y, NFFT=10, window=np.ones(9)) + with pytest.raises(ValueError): + mlab._spectral_helper(x=self.y, y=self.y, NFFT=10, + window=np.ones(9)) def test_single_spectrum_helper_raises_mode_default(self): # test that mode 'default' cannot be used with _single_spectrum_helper - assert_raises(ValueError, mlab._single_spectrum_helper, - x=self.y, mode='default') + with pytest.raises(ValueError): + mlab._single_spectrum_helper(x=self.y, mode='default') def test_single_spectrum_helper_raises_mode_psd(self): # test that mode 'psd' cannot be used with _single_spectrum_helper - assert_raises(ValueError, mlab._single_spectrum_helper, - x=self.y, mode='psd') + with pytest.raises(ValueError): + mlab._single_spectrum_helper(x=self.y, mode='psd') def test_spectral_helper_psd(self): freqs = self.freqs_density @@ -1648,8 +1673,8 @@ def test_psd_detrend_mean_func_offset(self): assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal - assert_raises(AssertionError, - assert_allclose, spec_b, spec_c, atol=1e-08) + with pytest.raises(AssertionError): + assert_allclose(spec_b, spec_c, atol=1e-08) def test_psd_detrend_mean_str_offset(self): if self.NFFT_density is None: @@ -1684,8 +1709,8 @@ def test_psd_detrend_mean_str_offset(self): assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal - assert_raises(AssertionError, - assert_allclose, spec_b, spec_c, atol=1e-08) + with pytest.raises(AssertionError): + assert_allclose(spec_b, spec_c, atol=1e-08) def test_psd_detrend_linear_func_trend(self): if self.NFFT_density is None: @@ -1720,8 +1745,8 @@ def test_psd_detrend_linear_func_trend(self): assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal - assert_raises(AssertionError, - assert_allclose, spec_b, spec_c, atol=1e-08) + with pytest.raises(AssertionError): + assert_allclose(spec_b, spec_c, atol=1e-08) def test_psd_detrend_linear_str_trend(self): if self.NFFT_density is None: @@ -1756,8 +1781,8 @@ def test_psd_detrend_linear_str_trend(self): assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal - assert_raises(AssertionError, - assert_allclose, spec_b, spec_c, atol=1e-08) + with pytest.raises(AssertionError): + assert_allclose(spec_b, spec_c, atol=1e-08) def test_psd_window_hanning(self): if self.NFFT_density is None: @@ -1800,8 +1825,8 @@ def test_psd_window_hanning(self): assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal - assert_raises(AssertionError, - assert_allclose, spec_b, spec_c, atol=1e-08) + with pytest.raises(AssertionError): + assert_allclose(spec_b, spec_c, atol=1e-08) def test_psd_window_hanning_detrend_linear(self): if self.NFFT_density is None: @@ -1849,8 +1874,8 @@ def test_psd_window_hanning_detrend_linear(self): assert_array_equal(fsp_b, fsp_c) assert_allclose(spec_g, spec_c, atol=1e-08) # these should not be almost equal - assert_raises(AssertionError, - assert_allclose, spec_b, spec_c, atol=1e-08) + with pytest.raises(AssertionError): + assert_allclose(spec_b, spec_c, atol=1e-08) def test_psd_windowarray(self): freqs = self.freqs_density @@ -2832,10 +2857,10 @@ def get_z(x, y): np.testing.assert_array_almost_equal(zi, correct_zi, 5) # Decreasing xi or yi should raise ValueError. - assert_raises(ValueError, mlab.griddata, x, y, z, xi[::-1], yi, - interp='nn') - assert_raises(ValueError, mlab.griddata, x, y, z, xi, yi[::-1], - interp='nn') + with pytest.raises(ValueError): + mlab.griddata(x, y, z, xi[::-1], yi, interp='nn') + with pytest.raises(ValueError): + mlab.griddata(x, y, z, xi, yi[::-1], interp='nn') # Passing 2D xi and yi arrays to griddata. xi, yi = np.meshgrid(xi, yi) @@ -2905,17 +2930,20 @@ def test_kde_bandwidth_method(self): class Test_gaussian_kde_custom(object): def test_no_data(self): """Pass no data into the GaussianKDE class.""" - assert_raises(ValueError, mlab.GaussianKDE, []) + with pytest.raises(ValueError): + mlab.GaussianKDE([]) def test_single_dataset_element(self): """Pass a single dataset element into the GaussianKDE class.""" - assert_raises(ValueError, mlab.GaussianKDE, [42]) + with pytest.raises(ValueError): + mlab.GaussianKDE([42]) def test_silverman_multidim_dataset(self): """Use a multi-dimensional array as the dataset and test silverman's output""" x1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - assert_raises(np.linalg.LinAlgError, mlab.GaussianKDE, x1, "silverman") + with pytest.raises(np.linalg.LinAlgError): + mlab.GaussianKDE(x1, "silverman") def test_silverman_singledim_dataset(self): """Use a single dimension list as the dataset and test silverman's @@ -2929,7 +2957,8 @@ def test_scott_multidim_dataset(self): """Use a multi-dimensional array as the dataset and test scott's output """ x1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - assert_raises(np.linalg.LinAlgError, mlab.GaussianKDE, x1, "scott") + with pytest.raises(np.linalg.LinAlgError): + mlab.GaussianKDE(x1, "scott") def test_scott_singledim_dataset(self): """Use a single-dimensional array as the dataset and test scott's @@ -2942,7 +2971,8 @@ def test_scott_singledim_dataset(self): def test_scalar_empty_dataset(self): """Use an empty array as the dataset and test the scalar's cov factor """ - assert_raises(ValueError, mlab.GaussianKDE, [], bw_method=5) + with pytest.raises(ValueError): + mlab.GaussianKDE([], bw_method=5) def test_scalar_covariance_dataset(self): """Use a dataset and test a scalar's cov factor @@ -2982,7 +3012,8 @@ def test_wrong_bw_method(self): np.random.seed(8765678) n_basesample = 50 data = np.random.randn(n_basesample) - assert_raises(ValueError, mlab.GaussianKDE, data, bw_method="invalid") + with pytest.raises(ValueError): + mlab.GaussianKDE(data, bw_method="invalid") class Test_gaussian_kde_evaluate(object): @@ -3008,7 +3039,8 @@ def test_evaluate_inv_dim(self): multidim_data = np.random.randn(n_basesample) kde = mlab.GaussianKDE(multidim_data) x2 = [[1], [2], [3]] - assert_raises(ValueError, kde.evaluate, x2) + with pytest.raises(ValueError): + kde.evaluate(x2) def test_evaluate_dim_and_num(self): """ Tests if evaluated against a one by one array""" @@ -3024,7 +3056,8 @@ def test_evaluate_point_dim_not_one(self): x1 = np.arange(3, 10, 2) x2 = [np.arange(3, 10, 2), np.arange(3, 10, 2)] kde = mlab.GaussianKDE(x1) - assert_raises(ValueError, kde.evaluate, x2) + with pytest.raises(ValueError): + kde.evaluate(x2) def test_evaluate_equal_dim_and_num_lt(self): """Test when line 3810 fails""" From cdd2bcb2848bf0feea4aaada1b51dfcdf972fa90 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 19 Jan 2017 22:29:32 -0500 Subject: [PATCH 3/4] Use assert_almost_equal from NumPy in mlab tests. --- lib/matplotlib/tests/test_mlab.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index 680a14c1c702..a38f037d77db 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -5,12 +5,12 @@ import tempfile -from numpy.testing import assert_allclose, assert_array_equal +from numpy.testing import (assert_allclose, assert_almost_equal, + assert_array_equal) import numpy.ma.testutils as matest import numpy as np import datetime as datetime import pytest -from nose.tools import (assert_almost_equal) import matplotlib.mlab as mlab import matplotlib.cbook as cbook @@ -2922,9 +2922,9 @@ def test_kde_bandwidth_method(self): xs = np.linspace(-7, 7, 51) kdepdf = gkde.evaluate(xs) kdepdf2 = gkde2.evaluate(xs) - assert_almost_equal(kdepdf.all(), kdepdf2.all()) + assert kdepdf.all() == kdepdf2.all() kdepdf3 = gkde3.evaluate(xs) - assert_almost_equal(kdepdf.all(), kdepdf3.all()) + assert kdepdf.all() == kdepdf3.all() class Test_gaussian_kde_custom(object): From 90a10f4ae280c91e3977f7b6c71ddfbd78602b66 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 20 Jan 2017 16:55:02 -0500 Subject: [PATCH 4/4] Remove mlab from default testing list. AppVeyor doesn't install pytest, so the nose build fails with this one. --- lib/matplotlib/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index d6d713cee761..9bb967b61b6c 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1502,7 +1502,6 @@ def _jupyter_nbextension_paths(): 'matplotlib.tests.test_legend', 'matplotlib.tests.test_lines', 'matplotlib.tests.test_mathtext', - 'matplotlib.tests.test_mlab', 'matplotlib.tests.test_offsetbox', 'matplotlib.tests.test_patches', 'matplotlib.tests.test_path',