def test_asarray2d_series(): # case: pd.Series a = np.zeros((3, )) ser = pd.Series(a) result = asarray2d(ser) assert result.shape[1] >= 1 assert_array_equal(result, asarray2d(a))
def test_compute_epsilon(): # data looks like this: # | x # | x # | x # | x # | x # |---------| # x = np.array([ [0.5, 0.5], [1.5, 1.5], [2.5, 2.5], [3.5, 3.5], [4.5, 4.5], ]) # note k is 3 and distance is chebyshev expected_epsilon = np.array([ [2 * 3.0], [2 * 2.0], [2 * 2.0], # has two neighbors at distance 2 [2 * 2.0], [2 * 3.0], ]) epsilon = _compute_epsilon(x) assert_array_equal(expected_epsilon, epsilon)
def test_skipna(a, b, c, how): if c is not None: a1, b1, c1 = skipna(a, b, c, how=how) else: a1, b1 = skipna(a, b, how=how) c1 = None assert a1.shape[0] == b1.shape[0] assert a1.shape[1:] == a.shape[1:] assert b1.shape[1:] == b.shape[1:] if c is not None: assert c1.shape[0] == a1.shape[0] assert c1.shape[1:] == c.shape[1:] if how == 'left' or how == 'any': assert not np.isnan(a1).any() if how == 'any': assert not np.isnan(b1).any() if how == 'all': left_nans = np.isnan(a1) if left_nans.ndim > 1: left_nans = left_nans.any(axis=1) right_nans = np.isnan(b1) if right_nans.ndim > 1: right_nans = right_nans.any(axis=1) assert not (left_nans & right_nans).any() # symmetry of b and c if c is not None: _, b2, c2 = skipna(a, b, c, how=how) _, c3, b3 = skipna(a, c, b, how=how) assert_array_equal(b2, b3) assert_array_equal(c2, c3)
def test_asarray2d_shape_n(): # case: second dimension not present a = np.zeros((3, )) result = asarray2d(a) expected_shape = (3, 1) assert result.shape == expected_shape assert_array_equal(np.ravel(result), a)
def test_compute_empirical_probability(): x = [1, 1, 2, 3, 2, 1, 1, 2] expected_pk = np.array([4 / 8, 3 / 8, 1 / 8]) expected_events = np.array([[1], [2], [3]]) pk, events = _compute_empirical_probability(x) assert_array_equal(expected_pk, pk) assert_array_equal(expected_events, events)
def test_asarray2d_df(): # case: pd.DataFrame a = np.zeros((3, 2)) df = pd.DataFrame(a) result = asarray2d(df) assert result.shape == df.shape assert result.shape[1] >= 1 assert_array_equal(result, a)
def test_assert_array_equal(): a = np.arange(10 * 7).reshape(10, 7) b = a.copy() assert_array_equal(a, b) c = np.arange(9).reshape(3, 3) d = c + 1 with pytest.raises(AssertionError): assert_array_equal(c, d)
def test_simple_function_transformer(): def func(x): return x + 5 data = np.arange(30) trans = ballet.eng.SimpleFunctionTransformer(func) trans.fit(data) data_trans = trans.transform(data) data_func = func(data) assert_array_equal(data_trans, data_func)
def test_box_cox_transformer(): threshold = 0.0 lmbda = 0.0 trans = ballet.eng.misc.BoxCoxTransformer(threshold=threshold, lmbda=lmbda) skewed = [0., 0., 0., 0., 1.] unskewed = [0., 0., 0., 0., 0.] exp_skew_res = boxcox1p(skewed, lmbda) exp_unskew_res = unskewed # test on DF, one skewed column df = pd.DataFrame() df['skewed'] = skewed df['unskewed'] = unskewed df_res = trans.fit_transform(df) assert isinstance(df_res, pd.DataFrame) assert 'skewed' in df_res.columns assert 'unskewed' in df_res.columns assert_array_almost_equal(df_res['skewed'], exp_skew_res) # test on DF, no skewed columns df_unskewed = pd.DataFrame() df_unskewed['col1'] = unskewed df_unskewed['col2'] = unskewed df_unskewed_res = trans.fit_transform(df_unskewed) assert isinstance(df_unskewed_res, pd.DataFrame) assert 'col1' in df_unskewed_res.columns assert 'col2' in df_unskewed_res.columns assert_array_equal(df_unskewed_res['col1'], exp_unskew_res) assert_array_equal(df_unskewed_res['col2'], exp_unskew_res) # test on skewed Series ser_skewed = pd.Series(skewed) ser_skewed_res = trans.fit_transform(ser_skewed) assert isinstance(ser_skewed_res, pd.Series) assert_array_almost_equal(ser_skewed_res, exp_skew_res) # test on unskewed Series ser_unskewed = pd.Series(unskewed) ser_unskewed_res = trans.fit_transform(ser_unskewed) assert isinstance(ser_unskewed_res, pd.Series) assert_array_equal(ser_unskewed_res, exp_unskew_res) # test on array arr = np.array([unskewed, skewed]).T arr_res = trans.fit_transform(arr) arr_exp = np.vstack((exp_unskew_res, exp_skew_res)).T assert_array_almost_equal(arr_res, arr_exp)
def test_asarray2d_shape_n_x_1(): # case: second dimension == 1 a = np.zeros((3, 1)) result = asarray2d(a) assert_array_equal(result, a)
def test_countunique(z, expected): nunique = countunique(z) assert_array_equal(nunique, expected)