def test_biweight_midvariance_small(): data = [1, 3, 5, 500, 2] var = biweight_midvariance(data) assert_allclose(var, 2.9238456) # verified with R var = biweight_midvariance(data, modify_sample_size=True) assert_allclose(var, 2.3390765)
def test_biweight_32bit_runtime_warnings(): """Regression test for #6905.""" with NumpyRNGContext(12345): data = np.random.random(100).astype(np.float32) data[50] = 30000. biweight_scale(data) biweight_midvariance(data)
def test_biweight_midvariance_small(): data = [1, 3, 5, 500, 2] var = biweight_midvariance(data) assert_allclose(var, 2.9238456) # verified with R var = biweight_midvariance(data, modify_sample_size=True) assert_allclose(var, 2.3390765)
def test_biweight_midvariance_constant_axis_3d(): shape = (10, 5, 2) data = np.ones(shape) cbl = biweight_midvariance(data, axis=0) assert_allclose(cbl, np.zeros((shape[1], shape[2]))) cbl = biweight_midvariance(data, axis=1) assert_allclose(cbl, np.zeros((shape[0], shape[2]))) cbl = biweight_midvariance(data, axis=2) assert_allclose(cbl, np.zeros((shape[0], shape[1])))
def test_biweight_midvariance_constant_axis_3d(): shape = (10, 5, 2) data = np.ones(shape) cbl = biweight_midvariance(data, axis=0) assert_allclose(cbl, np.zeros((shape[1], shape[2]))) cbl = biweight_midvariance(data, axis=1) assert_allclose(cbl, np.zeros((shape[0], shape[2]))) cbl = biweight_midvariance(data, axis=2) assert_allclose(cbl, np.zeros((shape[0], shape[1])))
def test_biweight_scale(): # NOTE: biweight_scale is covered by biweight_midvariance tests data = [1, 3, 5, 500, 2] scl = biweight_scale(data) var = biweight_midvariance(data) assert_allclose(scl, np.sqrt(var)) data = np.ma.masked_invalid([1, 3, 5, 500, 2, np.nan]) data[0] = np.nan scl = biweight_scale(data, ignore_nan=True) var = biweight_midvariance(data, ignore_nan=True) assert_allclose(scl, np.sqrt(var))
def test_biweight_32bit_runtime_warnings(): """Regression test for #6905.""" with NumpyRNGContext(12345): data = np.random.random(100).astype(np.float32) data[50] = 30000. with catch_warnings(RuntimeWarning) as warning_lines: biweight_scale(data) assert len(warning_lines) == 0 with catch_warnings(RuntimeWarning) as warning_lines: biweight_midvariance(data) assert len(warning_lines) == 0
def test_biweight_32bit_runtime_warnings(): """Regression test for #6905.""" with NumpyRNGContext(12345): data = np.random.random(100).astype(np.float32) data[50] = 30000. with catch_warnings(RuntimeWarning) as warning_lines: biweight_scale(data) assert len(warning_lines) == 0 with catch_warnings(RuntimeWarning) as warning_lines: biweight_midvariance(data) assert len(warning_lines) == 0
def test_biweight_midvariance_constant_axis_2d(): shape = (10, 5) data = np.ones(shape) cbl = biweight_midvariance(data, axis=0) assert_allclose(cbl, np.zeros(shape[1])) cbl = biweight_midvariance(data, axis=1) assert_allclose(cbl, np.zeros(shape[0])) data = np.arange(50).reshape(10, 5) data[2] = 100. data[7] = 2. bw = biweight_midvariance(data, axis=1) assert_allclose(bw[2], 0.) assert_allclose(bw[7], 0.)
def test_biweight_midvariance_ignore_nan(): data1d = np.array([1, 3, 5, 500, 2, np.nan]) data2d = np.array([data1d, data1d]) assert np.isnan(biweight_midvariance(data1d, ignore_nan=False)) biw_var = biweight_midvariance(data1d[:-1], ignore_nan=False) biw_var_nonan = biweight_midvariance(data1d, ignore_nan=True) assert_equal(biw_var_nonan, biw_var) assert_equal(biweight_midvariance(data2d, axis=0, ignore_nan=True), [0., 0., 0., 0., 0., np.nan]) assert_equal(biweight_midvariance(data2d, axis=1, ignore_nan=True), [biw_var_nonan, biw_var_nonan])
def test_biweight_midvariance_constant_axis_2d(): shape = (10, 5) data = np.ones(shape) cbl = biweight_midvariance(data, axis=0) assert_allclose(cbl, np.zeros(shape[1])) cbl = biweight_midvariance(data, axis=1) assert_allclose(cbl, np.zeros(shape[0])) data = np.arange(50).reshape(10, 5) data[2] = 100. data[7] = 2. bw = biweight_midvariance(data, axis=1) assert_allclose(bw[2], 0.) assert_allclose(bw[7], 0.)
def test_biweight_midcovariance_midvariance(): """ Test that biweight_midcovariance diagonal elements agree with biweight_midvariance. """ rng = np.random.RandomState(1) d = rng.normal(0, 2, size=(100, 3)) cov = biweight_midcovariance(d) var = [biweight_midvariance(a) for a in d] assert_allclose(cov.diagonal(), var) cov2 = biweight_midcovariance(d, modify_sample_size=True) var2 = [biweight_midvariance(a, modify_sample_size=True) for a in d] assert_allclose(cov2.diagonal(), var2)
def test_biweight_midvariance_axis_3d(): """Test a 3D array with the axis keyword.""" with NumpyRNGContext(12345): nz = 3 ny = 4 nx = 5 data = np.random.normal(5, 2, (nz, ny, nx)) bw = biweight_midvariance(data, axis=0) assert bw.shape == (ny, nx) y = 0 bwi = [] for i in range(nx): bwi.append(biweight_midvariance(data[:, y, i])) bwi = np.array(bwi) assert_allclose(bw[y], bwi)
def test_biweight_midvariance_axis_3d(): """Test a 3D array with the axis keyword.""" with NumpyRNGContext(12345): nz = 3 ny = 4 nx = 5 data = np.random.normal(5, 2, (nz, ny, nx)) bw = biweight_midvariance(data, axis=0) assert bw.shape == (ny, nx) y = 0 bwi = [] for i in range(nx): bwi.append(biweight_midvariance(data[:, y, i])) bwi = np.array(bwi) assert_allclose(bw[y], bwi)
def test_biweight_midcovariance_midvariance(): """ Test that biweight_midcovariance diagonal elements agree with biweight_midvariance. """ rng = np.random.RandomState(1) d = rng.normal(0, 2, size=(100, 3)) cov = biweight_midcovariance(d) var = [biweight_midvariance(a) for a in d] assert_allclose(cov.diagonal(), var) cov2 = biweight_midcovariance(d, modify_sample_size=True) var2 = [biweight_midvariance(a, modify_sample_size=True) for a in d] assert_allclose(cov2.diagonal(), var2)
def test_biweight_midvariance_axis(): """Test a 2D array with the axis keyword.""" with NumpyRNGContext(12345): ny = 100 nx = 200 data = np.random.normal(5, 2, (ny, nx)) bw = biweight_midvariance(data, axis=0) bwi = [] for i in range(nx): bwi.append(biweight_midvariance(data[:, i])) bwi = np.array(bwi) assert_allclose(bw, bwi) bw = biweight_midvariance(data, axis=1) bwi = [] for i in range(ny): bwi.append(biweight_midvariance(data[i, :])) bwi = np.array(bwi) assert_allclose(bw, bwi)
def test_biweight_midvariance_axis(): """Test a 2D array with the axis keyword.""" with NumpyRNGContext(12345): ny = 100 nx = 200 data = np.random.normal(5, 2, (ny, nx)) bw = biweight_midvariance(data, axis=0) bwi = [] for i in range(nx): bwi.append(biweight_midvariance(data[:, i])) bwi = np.array(bwi) assert_allclose(bw, bwi) bw = biweight_midvariance(data, axis=1) bwi = [] for i in range(ny): bwi.append(biweight_midvariance(data[i, :])) bwi = np.array(bwi) assert_allclose(bw, bwi)
def test_biweight_midvariance_5127(): # test a regression introduced in #5127 rand = np.random.default_rng(12345) data = rand.normal(loc=0., scale=20., size=(100, 100)) var = biweight_midvariance(data) assert_allclose(var, 409.87135608846205)
def test_biweight_midcovariance_1d(): d = [0, 1, 2] cov = biweight_midcovariance(d) var = biweight_midvariance(d) assert_allclose(cov, [[var]])
def test_biweight_scale(): # NOTE: biweight_scale is covered by biweight_midvariance tests data = [1, 3, 5, 500, 2] scl = biweight_scale(data) var = biweight_midvariance(data) assert_allclose(scl, np.sqrt(var))
def test_biweight_midvariance(): with NumpyRNGContext(12345): # test that it runs randvar = np.random.randn(10000) var = biweight_midvariance(randvar) assert_allclose(var, 1.0, rtol=0.02)
def test_biweight_midvariance_constant_axis(): bw = biweight_midvariance(np.ones((10, 5))) assert bw == 0.0
def test_biweight_midvariance_masked(): data1d = np.array([1, 3, 5, 500, 2, np.nan]) data2d = np.array([data1d, data1d]) data1d_masked = np.ma.masked_invalid(data1d) data2d_masked = np.ma.masked_invalid(data2d) assert_equal(biweight_midvariance(data1d, ignore_nan=True), biweight_midvariance(data1d_masked)) assert_equal(biweight_midvariance(data2d, ignore_nan=True), biweight_midvariance(data2d_masked)) bw_loc = biweight_midvariance(data2d, ignore_nan=True, axis=1) bw_loc_masked = biweight_midvariance(data2d_masked, axis=1) assert isinstance(bw_loc_masked, np.ma.MaskedArray) assert ~np.any(bw_loc_masked.mask) # mask is all False assert_equal(bw_loc, bw_loc_masked.data) bw_loc = biweight_midvariance(data2d, ignore_nan=True, axis=0) bw_loc_masked = biweight_midvariance(data2d_masked, axis=0) assert_equal(bw_loc_masked.data[:-1], bw_loc[:-1]) assert bw_loc_masked.mask[-1] # last mask element is True data1d_masked.data[0] = np.nan # unmasked NaN assert np.isnan(biweight_midvariance(data1d_masked)) assert_equal(biweight_midvariance(data1d_masked, ignore_nan=True), biweight_midvariance(data1d[1:], ignore_nan=True)) # ensure that input masked array is not modified assert np.isnan(data1d_masked[0])
def test_biweight_midvariance_5127(): # test a regression introduced in #5127 rand = np.random.RandomState(12345) data = rand.normal(loc=0., scale=20., size=(100, 100)) var = biweight_midvariance(data) assert_allclose(var, 406.86938710817344) # verified with R
def test_biweight_scale(): # NOTE: biweight_scale is covered by biweight_midvariance tests data = [1, 3, 5, 500, 2] scl = biweight_scale(data) var = biweight_midvariance(data) assert_allclose(scl, np.sqrt(var))
def test_biweight_midcovariance_1d(): d = [0, 1, 2] cov = biweight_midcovariance(d) var = biweight_midvariance(d) assert_allclose(cov, [[var]])
def test_biweight_midvariance_5127(): # test a regression introduced in #5127 rand = np.random.RandomState(12345) data = rand.normal(loc=0., scale=20., size=(100, 100)) var = biweight_midvariance(data) assert_allclose(var, 406.86938710817344) # verified with R
def test_biweight_midvariance(): with NumpyRNGContext(12345): # test that it runs randvar = np.random.randn(10000) var = biweight_midvariance(randvar) assert_allclose(var, 1.0, rtol=0.02)
def test_biweight_midvariance_constant_axis(): bw = biweight_midvariance(np.ones((10, 5))) assert bw == 0.0