Exemplo n.º 1
0
def test_generic_random_transformation():
    r"""Test generic Procrustes with randomized transformation matrices."""
    # define arbitrary array and random transformation
    np.random.seed(999)
    array_a = np.random.randint(low=-10, high=10, size=(5, 4))
    array_x = np.random.randint(low=0, high=20, size=(4, 4)) / 10
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=False, scale=False)
    # check transformation is right & error is zero
    assert_almost_equal(res["array_u"], array_x, decimal=6)
    assert_almost_equal(res["error"], 0.0, decimal=6)
Exemplo n.º 2
0
def test_generic_square(m):
    r"""Test generic Procrustes with random square matrices."""
    # random input & transformation arrays (size=mxm)
    array_a = np.random.uniform(-2.0, 2.0, (m, m))
    array_x = np.random.uniform(-2.0, 2.0, (m, m))
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=False, scale=False)
    # check error & arrays
    assert_almost_equal(res.error, 0.0, decimal=6)
    assert_almost_equal(res.t, array_x, decimal=6)
    assert_almost_equal(res.new_a, array_a, decimal=6)
    assert_almost_equal(res.new_b, array_b, decimal=6)
Exemplo n.º 3
0
def test_generic_transformed_translate_scale():
    r"""Test generic Procrustes with translation and scaling."""
    # define arbitrary array and random transformation
    np.random.seed(999)
    array_a = np.random.randint(low=-10, high=10, size=(5, 4))
    array_x = np.array([[0.3, 0., 0.3, 0.6],
                        [0.7, 0.3, 0., 0.8],
                        [0.4, 0., 0.4, 0.5],
                        [0.3, 0.7, 0.1, 0.7]])
    array_b = np.dot(4.5 * (array_a + 9), array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=True, scale=True)
    # check transformation is error is zero
    assert_almost_equal(res["error"], 0.0, decimal=6)
Exemplo n.º 4
0
def test_generic_rectangular(m, n):
    r"""Test generic Procrustes with random rectangular matrices."""
    # random input & transformation arrays (size=mxn)
    array_a = np.random.uniform(-2.0, 2.0, (m, n))
    array_x = np.random.uniform(-3.0, 3.0, (n, n))
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=False, scale=False)
    # check error & arrays
    assert_almost_equal(res.error, 0.0, decimal=6)
    assert_almost_equal(res.new_a, array_a, decimal=6)
    assert_almost_equal(res.new_b, array_b, decimal=6)
    # check transformation array, only if it is unique
    if m >= n:
        assert_almost_equal(res.t, array_x, decimal=6)
Exemplo n.º 5
0
def test_generic_transformed_negative():
    r"""Test generic Procrustes with negative matrices."""
    # define arbitrary array and random transformation
    np.random.seed(999)
    array_a = np.random.randint(low=-10, high=10, size=(5, 4))
    array_x = np.array([[0.3, 0., 0.3, 0.6],
                        [0.7, 0.3, 0., 0.8],
                        [0.4, 0., 0.4, 0.5],
                        [0.3, 0.7, 0.1, 0.7]])
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=False, scale=False)
    # check transformation is right & error is zero
    assert_almost_equal(res["array_u"], array_x, decimal=6)
    assert_almost_equal(res["error"], 0.0, decimal=6)
Exemplo n.º 6
0
def test_generic_square_lapack_driver_and_assertion_error(m):
    r"""Test generic Procrustes with random and non-default lapack driver with assertion error."""
    # random input & transformation arrays (size=mxm)
    array_a = np.random.uniform(-2.0, 2.0, (m, m))
    array_x = np.random.uniform(-2.0, 2.0, (m, m))
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=False, scale=False, use_svd=True)
    # check error & arrays
    assert_almost_equal(res.error, 0.0, decimal=6)
    assert_almost_equal(res.t, array_x, decimal=6)
    assert_almost_equal(res.new_a, array_a, decimal=6)
    assert_almost_equal(res.new_b, array_b, decimal=6)

    # Check assertion error
    assert_raises(TypeError, generic, array_a, array_b, use_svd="not bool")
Exemplo n.º 7
0
def test_generic_rectangular_translate(m, n):
    r"""Test generic Procrustes with random rectangular matrices & translation."""
    # random input, transformation arrays, & translation (size=mxn)
    array_a = np.random.uniform(-4.0, 4.0, (m, n))
    array_x = np.random.uniform(-2.0, 2.0, (n, n))
    array_t = np.repeat(np.random.uniform(-3.0, 3.0, (1, n)), m, axis=0)
    array_b = np.dot(array_a, array_x) + array_t
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=True, scale=False)
    # check error & arrays
    assert_almost_equal(res.error, 0.0, decimal=6)
    assert_almost_equal(res.new_a,
                        array_a - np.mean(array_a, axis=0),
                        decimal=6)
    assert_almost_equal(res.new_b,
                        array_b - np.mean(array_b, axis=0),
                        decimal=6)
Exemplo n.º 8
0
def test_generic_rectangular_scale(m, n):
    r"""Test generic Procrustes with random square matrices & translation."""
    # random input, transformation arrays, & translation (size=mxn)
    array_a = np.random.uniform(-5.0, 5.0, (m, n))
    array_x = np.random.uniform(-3.0, 3.0, (n, n))
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=False, scale=True)
    # check error & arrays
    assert_almost_equal(res.error, 0.0, decimal=6)
    norm_a = np.linalg.norm(array_a)
    norm_b = np.linalg.norm(array_b)
    assert_almost_equal(res.new_a, array_a / norm_a, decimal=6)
    assert_almost_equal(res.new_b, array_b / norm_b, decimal=6)
    # check transformation array, only if it is unique
    if m >= n:
        assert_almost_equal(res.t, array_x * norm_a / norm_b, decimal=6)
Exemplo n.º 9
0
def test_generic_rectangular_translate_scale(m, n):
    r"""Test generic Procrustes with random square matrices & translation."""
    # random input, transformation arrays, & translation (size=mxn)
    array_a = np.random.uniform(-10.0, 10.0, (m, n))
    array_x = np.random.uniform(-7.0, 7.0, (n, n))
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=True, scale=True)
    # check error & arrays
    assert_almost_equal(res.error, 0.0, decimal=6)
    centered_a = array_a - np.mean(array_a, axis=0)
    centered_b = array_b - np.mean(array_b, axis=0)
    assert_almost_equal(res.new_a,
                        centered_a / np.linalg.norm(centered_a),
                        decimal=6)
    assert_almost_equal(res.new_b,
                        centered_b / np.linalg.norm(centered_b),
                        decimal=6)
Exemplo n.º 10
0
def test_generic_transformed():
    r"""Test generic Procrustes with random matrices."""
    # define arbitrary array and random transformation
    array_a = np.array([[3, 3, 5, 9],
                        [2, 6, 9, 9],
                        [1, 3, 5, 2],
                        [0, 4, 2, 9],
                        [9, 6, 0, 3]])
    array_x = np.array([[0.3, 0., 0.3, 0.6],
                        [0.7, 0.3, 0., 0.8],
                        [0.4, 0., 0.4, 0.5],
                        [0.3, 0.7, 0.1, 0.7]])
    array_b = np.dot(array_a, array_x)
    # compute procrustes transformation
    res = generic(array_a, array_b, translate=False, scale=False)
    # check transformation is right & error is zero
    assert_almost_equal(res["array_u"], array_x, decimal=6)
    assert_almost_equal(res["error"], 0.0, decimal=6)