def test_CompositionOperator_direct3(self): ig = self.ig data = self.data G = GradientOperator(domain_geometry=ig) Id1 = 2 * IdentityOperator(ig) Id2 = IdentityOperator(ig) d = CompositionOperator(G, Id2) out1 = G.direct(data) d_out = d.direct(data) d1 = Id2.direct(data) d2 = G.direct(d1) numpy.testing.assert_array_almost_equal( d2.get_item(0).as_array(), d_out.get_item(0).as_array()) numpy.testing.assert_array_almost_equal( d2.get_item(1).as_array(), d_out.get_item(1).as_array()) G2Id = G.compose(2 * Id2) d2g = G2Id.direct(data) numpy.testing.assert_array_almost_equal( d2g.get_item(0).as_array(), 2 * d_out.get_item(0).as_array()) numpy.testing.assert_array_almost_equal( d2g.get_item(1).as_array(), 2 * d_out.get_item(1).as_array())
def test_NestedBlockDataContainer2(self): M, N = 2, 3 ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N) ag = ig u = ig.allocate(1) op1 = GradientOperator(ig) op2 = IdentityOperator(ig, ag) operator = BlockOperator(op1, op2, shape=(2, 1)) d1 = op1.direct(u) d2 = op2.direct(u) d = operator.direct(u) dd = operator.domain_geometry() ww = operator.range_geometry() c1 = d + d c2 = 2 * d c3 = d / (d + 0.0001) c5 = d.get_item(0).power(2).sum()
def test_IdentityOperator(self): ig = ImageGeometry(10, 20, 30) img = ig.allocate() print(img.shape, ig.shape) self.assertTrue(img.shape == (30, 20, 10)) self.assertEqual(img.sum(), 0) Id = IdentityOperator(ig) y = Id.direct(img) numpy.testing.assert_array_equal(y.as_array(), img.as_array())
def test_IdentityOperator(self): print("test_IdentityOperator") ig = ImageGeometry(10, 20, 30) img = ig.allocate() # img.fill(numpy.ones((30,20,10))) self.assertTrue(img.shape == (30, 20, 10)) #self.assertEqual(img.sum(), 2*float(10*20*30)) self.assertEqual(img.sum(), 0.) Id = IdentityOperator(ig) y = Id.direct(img) numpy.testing.assert_array_equal(y.as_array(), img.as_array())
def tests_for_LS_weightedLS(self): ig = ImageGeometry(40, 30) numpy.random.seed(1) A = IdentityOperator(ig) b = ig.allocate('random') x = ig.allocate('random') c = numpy.float64(0.3) weight = ig.allocate('random') D = DiagonalOperator(weight) norm_weight = numpy.float64(D.norm()) print("norm_weight", norm_weight) f1 = LeastSquares(A, b, c, weight) f2 = LeastSquares(A, b, c) print("Check LS vs wLS") # check Lipshitz numpy.testing.assert_almost_equal(f2.L, 2 * c * (A.norm()**2)) print("unwrapped", 2. * c * norm_weight * (A.norm()**2)) print("f1.L", f1.L) numpy.testing.assert_almost_equal( f1.L, numpy.float64(2.) * c * norm_weight * (A.norm()**2)) print("Lipschitz is ... OK") # check call with weight res1 = c * (A.direct(x) - b).dot(weight * (A.direct(x) - b)) res2 = f1(x) numpy.testing.assert_almost_equal(res1, res2) print("Call is ... OK") # check call without weight #res1 = c * (A.direct(x)-b).dot((A.direct(x) - b)) res1 = c * (A.direct(x) - b).squared_norm() res2 = f2(x) numpy.testing.assert_almost_equal(res1, res2) print("Call without weight is ... OK") # check gradient with weight out = ig.allocate(None) res1 = f1.gradient(x) #out = f1.gradient(x) f1.gradient(x, out=out) res2 = 2 * c * A.adjoint(weight * (A.direct(x) - b)) numpy.testing.assert_array_almost_equal(res1.as_array(), res2.as_array()) numpy.testing.assert_array_almost_equal(out.as_array(), res2.as_array()) print("GradientOperator is ... OK") # check gradient without weight out = ig.allocate() res1 = f2.gradient(x) f2.gradient(x, out=out) res2 = 2 * c * A.adjoint(A.direct(x) - b) numpy.testing.assert_array_almost_equal(res1.as_array(), res2.as_array()) numpy.testing.assert_array_almost_equal(out.as_array(), res2.as_array()) print("GradientOperator without weight is ... OK") print( "Compare Least Squares with DiagonalOperator + CompositionOperator" ) ig2 = ImageGeometry(100, 100, 100) A = IdentityOperator(ig2) b = ig2.allocate('random') x = ig2.allocate('random') c = 0.3 weight = ig2.allocate('random') weight_operator = DiagonalOperator(weight.sqrt()) tmp_A = CompositionOperator(weight_operator, A) tmp_b = weight_operator.direct(b) f1 = LeastSquares(tmp_A, tmp_b, c) f2 = LeastSquares(A, b, c, weight) t0 = timer() res1 = f1(x) t1 = timer() print("Time with Composition+Diagonal is {}".format(t1 - t0)) t2 = timer() res2 = f2(x) t3 = timer() print("Time with LS + weight is {}".format(t3 - t2)) numpy.testing.assert_almost_equal(res1, res2, decimal=2)