Exemple #1
0
def test_ScaledArray_array():
    for N in range(3, 5):
        for P in range(3, 5):
            array = np.random.rand(N, P) + 1
            std = np.diag(1 / np.std(array, axis=0))
            mu = np.mean(array, axis=0)

            sarray = ScaledCenterArray(scale=False, center=False)
            sarray.fit(da.array(array))
            np.testing.assert_array_almost_equal(array, sarray.array)
            np.testing.assert_array_almost_equal(array.T, sarray.T.array)

            # With Scale but No Center
            # B = AD
            b_array = array.dot(std)
            sarray = ScaledCenterArray(scale=True, center=False)
            sarray.fit(da.array(array))
            np.testing.assert_array_almost_equal(b_array, sarray.array)
            np.testing.assert_array_almost_equal(b_array.T, sarray.T.array)

            # With Center but No Scale:
            # B = (A - U)
            b_array = array - mu
            sarray = ScaledCenterArray(scale=False, center=True)
            sarray.fit(da.array(array))
            np.testing.assert_array_almost_equal(b_array, sarray.array)
            np.testing.assert_array_almost_equal(b_array.T, sarray.T.array)

            # With Center and  Scale:
            # (A - U)'D'D(A - U)x
            b_array = (array - mu).dot(std)
            sarray = ScaledCenterArray(scale=True, center=True)
            sarray.fit(da.array(array))
            np.testing.assert_array_almost_equal(b_array, sarray.array)
            np.testing.assert_array_almost_equal(b_array.T, sarray.T.array)
Exemple #2
0
def test_ScaledArray_sym_mat_mult():
    for N in range(2, 5):
        for P in range(2, 5):
            array = np.random.rand(N, P) + 1
            std = np.diag(1/np.std(array, axis=0))
            mu = np.mean(array, axis=0)
            for factor in [None, 'n', 'p']:
                if factor is None:
                    f = 1
                elif factor == 'n':
                    f = N
                else:
                    f = P
                for K in range(1, 5):
                    for squeeze in [True, False]:
                        x = np.random.rand(N, K)
                        if squeeze:
                            x = np.squeeze(x)

                            for fit_x in [x, None]:

                                # With No Scale or Center
                                # x = A'Ax
                                result = array.dot(array.T.dot(x))/f
                                assert result.shape == x.shape
                                sarray = ScaledCenterArray(scale=False, center=False, factor=factor)
                                sarray.fit(da.array(array), x=fit_x)
                                np.testing.assert_array_equal(result, sarray.sym_mat_mult(x))

                                # With Scale but No Center
                                # B = AD
                                b_array = array.dot(std)
                                result = b_array.dot(b_array.T.dot(x))/f
                                assert result.shape == x.shape
                                sarray = ScaledCenterArray(scale=True, center=False, factor=factor)
                                sarray.fit(da.array(array), x=fit_x)
                                np.testing.assert_array_almost_equal(result, sarray.sym_mat_mult(x))

                                # With Center but No Scale:
                                # B = (A - U)
                                b_array = array - mu
                                result = b_array.dot(b_array.T.dot(x))/f
                                sarray = ScaledCenterArray(scale=False, center=True, factor=factor)
                                sarray.fit(da.array(array), x=fit_x)
                                np.testing.assert_array_almost_equal(result, sarray.sym_mat_mult(x))

                                # With Center and  Scale:
                                # (A - U)'D'D(A - U)x
                                result = (array - mu).dot(std).dot(std).dot((array - mu).T.dot(x))/f
                                sarray = ScaledCenterArray(scale=True, center=True, factor=factor)
                                sarray.fit(da.array(array), x=fit_x)
                                np.testing.assert_array_almost_equal(result, sarray.sym_mat_mult(x))
Exemple #3
0
def test_array_shapes():
    N, K = 10, 7
    array = da.array(np.random.rand(N, K))
    sarray = ScaledCenterArray()
    sarray.fit(array)

    assert sarray.shape == (N, K)
    assert sarray.T.shape == (K, N)
Exemple #4
0
def test__getitem__base_array_case2():
    for N in range(2, 5):
        for P in range(2, 5):
            array = da.array(np.random.rand(N , P))
            sarray = ScaledCenterArray()
            sarray.fit(array)
            for i in range(2, N):
                for j in range(2, P):
                    np.testing.assert_array_equal(sarray[0:i, 0:j]._array, array[0:i, 0:j])
Exemple #5
0
def test__getitem__mult_case():
    for N in range(3, 5):
        for P in range(3, 5):
            array = np.random.rand(N, P) + 1
            for K in range(1, 5):
                for sub_N in range(2, N):
                    sub_array = array[0:sub_N, :]
                    std = np.diag(1 / np.std(sub_array, axis=0))
                    mu = np.mean(sub_array, axis=0)
                    for squeeze in [True, False]:
                        x = np.random.rand(P, K)
                        if squeeze:
                            x = np.squeeze(x)
                        for fit_x in [x, None]:
                            # With No Scale or Center
                            # x = A'Ax
                            result = sub_array.dot(x)
                            sarray = ScaledCenterArray(scale=False, center=False)
                            sarray.fit(da.array(array), x=fit_x)
                            np.testing.assert_array_almost_equal(result, sarray[0:sub_N, :].dot(x))

                            # With Scale but No Center
                            # B = AD
                            b_array = sub_array.dot(std)
                            result = b_array.dot(x)
                            sarray = ScaledCenterArray(scale=True, center=False)
                            sarray.fit(da.array(array), x=fit_x)
                            np.testing.assert_array_almost_equal(result, sarray[0:sub_N, :].dot(x))

                            # With Center but No Scale:
                            # B = (A - U)
                            b_array = sub_array - mu
                            result = b_array.dot(x)
                            sarray = ScaledCenterArray(scale=False, center=True)
                            sarray.fit(da.array(array), x=fit_x)
                            np.testing.assert_array_almost_equal(result, sarray[0:sub_N, :].dot(x))

                            # With Center and  Scale:
                            # (A - U)'D'D(A - U)x
                            b_array = (sub_array - mu).dot(std)
                            result = b_array.dot(x)
                            sarray = ScaledCenterArray(scale=True, center=True)
                            sarray.fit(da.array(array), x=fit_x)
                            np.testing.assert_array_almost_equal(result, sarray[0:sub_N, :].dot(x))
Exemple #6
0
def test_array_id():
    array = da.array(np.random.rand(10, 7))
    x = da.array(np.random.rand(10,5))
    sarray = ScaledCenterArray(scale=True, center=True)
    sarray.fit(da.array(array), x=x)
    sarray_T = sarray.T
    assert id(sarray._array) == id(sarray_T._array)
    assert id(sarray.center_vector) == id(sarray_T.center_vector)
    assert id(sarray._array_moment.scale_matrix) == id(sarray_T._array_moment.scale_matrix)
    assert id(sarray._array_moment.sym_scale_matrix) == id(sarray_T._array_moment.sym_scale_matrix)
Exemple #7
0
def test_ScaledArray_T_dot():
    for N in range(2, 5):
        for P in range(2, 5):
            array = np.random.rand(N, P) + 1
            std = np.diag(1/np.std(array, axis=0))
            mu = np.mean(array, axis=0)
            for K in range(1, 5):
                for squeeze in [True, False]:
                    x = np.random.rand(N, K)
                    if squeeze:
                        x = np.squeeze(x)
                    for fit_x in [x, None]:
                        # With No Scale or Center
                        # x = A'Ax
                        result = array.T.dot(x)
                        sarray = ScaledCenterArray(scale=False, center=False)
                        sarray.fit(da.array(array), x=fit_x)
                        np.testing.assert_array_almost_equal(result, sarray.T.dot(x))

                        # With Scale but No Center
                        # B = AD
                        b_array = array.dot(std).T
                        result = b_array.dot(x)
                        sarray = ScaledCenterArray(scale=True, center=False)
                        sarray.fit(da.array(array), x=fit_x)
                        np.testing.assert_array_almost_equal(result, sarray.T.dot(x))

                        # With Center but No Scale:
                        # B = (A - U)
                        b_array = (array - mu).T
                        result = b_array.dot(x)
                        sarray = ScaledCenterArray(scale=False, center=True)
                        sarray.fit(da.array(array), x=fit_x)
                        np.testing.assert_array_almost_equal(result, sarray.T.dot(x))

                        # With Center and  Scale:
                        # (A - U)'D'D(A - U)x
                        b_array = (array - mu).dot(std).T
                        result = b_array.dot(x)
                        sarray = ScaledCenterArray(scale=True, center=True)
                        sarray.fit(da.array(array), x=fit_x)
                        np.testing.assert_array_almost_equal(result, sarray.T.dot(x))
Exemple #8
0
def test_array_tranpose_tranpose():
    array = da.array(np.random.rand(7, 10))
    x = da.array(np.random.rand(10, 5))
    sarray = ScaledCenterArray(scale=True, center=True)
    sarray.fit(da.array(array))

    s_array_T_T = sarray.T.T
    assert id(sarray._array) == id(s_array_T_T._array)

    assert id(sarray) == id(s_array_T_T)

    np.testing.assert_array_equal(sarray.dot(x), s_array_T_T.dot(x))
Exemple #9
0
def test__getitem__base_array_case1():
    array = da.array(np.random.rand(3, 3))
    sarray = ScaledCenterArray()
    sarray.fit(array)
    np.testing.assert_array_equal(sarray[:, :]._array, sarray._array)
Exemple #10
0
def test_bad_x():
    array = da.array(np.random.rand(3, 3))
    x = da.array(np.random.rand(3,3,3))
    sarray = ScaledCenterArray()
    with pytest.raises(ValueError):
        sarray.fit(array, x)