def test_affine_transform(): # Test affine transform m = 20 x1d = np.arange(m) x2d = x1d[:, None] x22d = np.c_[x2d, x2d] # Error if both of linear and affine components are None assert_raises(AffineError, affine_transform, None, None) assert_raises(AffineError, linear_transform, None) # With linear None and 0 affine offset - identity transform for x in (x1d, x2d, x22d): trans = affine_transform(None, np.zeros((m, 1))) assert_array_almost_equal(trans.affine_map(x), x) assert_array_almost_equal(trans.linear_map(x), x) assert_array_almost_equal(trans.adjoint_map(x), x) # With linear eye and None affine offset - identity again trans = affine_transform(np.eye(m), None) assert_array_almost_equal(trans.affine_map(x), x) assert_array_almost_equal(trans.linear_map(x), x) assert_array_almost_equal(trans.adjoint_map(x), x) # affine_transform as input trans = affine_transform(trans, None) assert_array_almost_equal(trans.affine_map(x), x) assert_array_almost_equal(trans.linear_map(x), x) assert_array_almost_equal(trans.adjoint_map(x), x) # diag trans = affine_transform(np.ones(m), None, True) assert_array_almost_equal(trans.affine_map(x), x) assert_array_almost_equal(trans.linear_map(x), x) assert_array_almost_equal(trans.adjoint_map(x), x)
def test_composition2(): X1 = np.random.standard_normal((20, 30)) X2 = np.random.standard_normal((30, 10)) X3 = np.random.standard_normal((10, 20)) b1 = np.random.standard_normal(20) b2 = np.random.standard_normal(30) b3 = np.random.standard_normal(10) L1 = affine_transform(X1, b1) L2 = affine_transform(X2, b2) L3 = affine_transform(X3, b3) z = np.random.standard_normal(20) w = np.random.standard_normal(20) comp = composition(L1, L2, L3) assert_array_almost_equal(comp.linear_map(z), np.dot(X1, np.dot(X2, np.dot(X3, z)))) assert_array_almost_equal(comp.adjoint_map(w), np.dot(X3.T, np.dot(X2.T, np.dot(X1.T, w)))) assert_array_almost_equal( comp.affine_map(z), np.dot(X1, np.dot(X2, np.dot(X3, z) + b3) + b2) + b1)
def test_affine_transform(): # Test affine transform m = 20 x1d = np.arange(m) x2d = x1d[:,None] x22d = np.c_[x2d, x2d] # Error if both of linear and affine components are None assert_raises(AffineError, affine_transform, None, None) # With linear None and 0 affine offset - identity transform for x in (x1d, x2d, x22d): trans = affine_transform(None, np.zeros((m,1))) assert_array_equal(trans.affine_map(x), x) assert_array_equal(trans.linear_map(x), x) assert_array_equal(trans.adjoint_map(x), x) # With linear eye and None affine offset - identity again trans = affine_transform(np.eye(m), None) assert_array_equal(trans.affine_map(x), x) assert_array_equal(trans.linear_map(x), x) assert_array_equal(trans.adjoint_map(x), x) # affine_transform as input trans = affine_transform(trans, None) assert_array_equal(trans.affine_map(x), x) assert_array_equal(trans.linear_map(x), x) assert_array_equal(trans.adjoint_map(x), x) # diag trans = affine_transform(np.ones(m), None, True) assert_array_equal(trans.affine_map(x), x) assert_array_equal(trans.linear_map(x), x) assert_array_equal(trans.adjoint_map(x), x)
def test_composition(): X1 = np.random.standard_normal((20,30)) X2 = np.random.standard_normal((30,10)) b1 = np.random.standard_normal(20) b2 = np.random.standard_normal(30) L1 = affine_transform(X1, b1) L2 = affine_transform(X2, b2) z = np.random.standard_normal(10) w = np.random.standard_normal(20) comp = composition(L1,L2) assert_array_equal(comp.linear_map(z), np.dot(X1, np.dot(X2, z))) assert_array_equal(comp.adjoint_map(w), np.dot(X2.T, np.dot(X1.T, w))) assert_array_equal(comp.affine_map(z), np.dot(X1, np.dot(X2, z)+b2)+b1)
def test_subsample_cols(): X = np.random.standard_normal((50, 20)) L = ra.affine_transform(X, None) Xs = subsample_columns(L, range(5)) Xs - X[:, range(5)] np.testing.assert_allclose(Xs, X[:, range(5)])
def test_adjoint(): X = np.random.standard_normal((20, 30)) b = np.random.standard_normal(20) L = affine_transform(X, b) z = np.random.standard_normal(30) w = np.random.standard_normal(20) A = adjoint(L) assert_array_almost_equal(A.linear_map(w), L.adjoint_map(w)) assert_array_almost_equal(A.affine_map(w), L.adjoint_map(w)) assert_array_almost_equal(A.adjoint_map(z), L.linear_map(z))
def test_adjoint(): X = np.random.standard_normal((20,30)) b = np.random.standard_normal(20) L = affine_transform(X, b) z = np.random.standard_normal(30) w = np.random.standard_normal(20) A = adjoint(L) assert_array_equal(A.linear_map(w), L.adjoint_map(w)) assert_array_equal(A.affine_map(w), L.adjoint_map(w)) assert_array_equal(A.adjoint_map(z), L.linear_map(z))
def test_selector(): X = np.arange(30).reshape((6,5)) offset = np.arange(6) transform = affine_transform(X, offset) apply_to_first5 = selector(slice(0,5), (20,), transform) apply_to_first5.affine_map(np.arange(20)) apply_to_first5.linear_map(np.arange(20)) apply_to_first5.adjoint_map(np.arange(6)) just_select = selector(slice(0,5), (20,)) just_select.affine_map(np.arange(20)) just_select.linear_map(np.arange(20)) just_select.adjoint_map(np.arange(5)) np.testing.assert_allclose(np.arange(5), just_select.linear_map(np.arange(20)))
def test_selector(): X = np.arange(30).reshape((6, 5)) offset = np.arange(6) transform = affine_transform(X, offset) apply_to_first5 = selector(slice(0, 5), (20, ), transform) apply_to_first5.affine_map(np.arange(20)) apply_to_first5.linear_map(np.arange(20)) apply_to_first5.adjoint_map(np.arange(6)) just_select = selector(slice(0, 5), (20, )) just_select.affine_map(np.arange(20)) just_select.linear_map(np.arange(20)) just_select.adjoint_map(np.arange(5)) np.testing.assert_allclose(np.arange(5), just_select.linear_map(np.arange(20)))