def test_FiniteDifference(self): print("test FiniteDifference") ## N, M = 2, 3 numpy.random.seed(1) ig = ImageGeometry(N, M) Id = IdentityOperator(ig) FD = FiniteDifferenceOperator(ig, direction=0, bnd_cond='Neumann') u = FD.domain_geometry().allocate('random') res = FD.domain_geometry().allocate(ImageGeometry.RANDOM) FD.adjoint(u, out=res) w = FD.adjoint(u) self.assertNumpyArrayEqual(res.as_array(), w.as_array()) res = Id.domain_geometry().allocate(ImageGeometry.RANDOM) Id.adjoint(u, out=res) w = Id.adjoint(u) self.assertNumpyArrayEqual(res.as_array(), w.as_array()) self.assertNumpyArrayEqual(u.as_array(), w.as_array()) G = GradientOperator(ig) u = G.range_geometry().allocate(ImageGeometry.RANDOM) res = G.domain_geometry().allocate() G.adjoint(u, out=res) w = G.adjoint(u) self.assertNumpyArrayEqual(res.as_array(), w.as_array()) u = G.domain_geometry().allocate(ImageGeometry.RANDOM) res = G.range_geometry().allocate() G.direct(u, out=res) w = G.direct(u) self.assertBlockDataContainerEqual(res, w) # 2D M, N = 2, 3 ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N, voxel_size_x=0.1, voxel_size_y=0.4) x = ig.allocate('random') labels = ["horizontal_y", "horizontal_x"] for i, dir in enumerate(labels): FD1 = FiniteDifferenceOperator(ig, direction=i) res1 = FD1.direct(x) res1b = FD1.adjoint(x) FD2 = FiniteDifferenceOperator(ig, direction=labels[i]) res2 = FD2.direct(x) res2b = FD2.adjoint(x) numpy.testing.assert_almost_equal(res1.as_array(), res2.as_array()) numpy.testing.assert_almost_equal(res1b.as_array(), res2b.as_array()) print("Check 2D FiniteDiff for label {}".format(labels[i])) # 2D + chan M, N, K = 2, 3, 4 ig1 = ImageGeometry(voxel_num_x=M, voxel_num_y=N, channels=K, voxel_size_x=0.1, voxel_size_y=0.4) print(ig1.dimension_labels) x = ig1.allocate('random') labels = ["channel", "horizontal_y", "horizontal_x"] for i, dir in enumerate(labels): FD1 = FiniteDifferenceOperator(ig1, direction=i) res1 = FD1.direct(x) res1b = FD1.adjoint(x) FD2 = FiniteDifferenceOperator(ig1, direction=labels[i]) res2 = FD2.direct(x) res2b = FD2.adjoint(x) numpy.testing.assert_almost_equal(res1.as_array(), res2.as_array()) numpy.testing.assert_almost_equal(res1b.as_array(), res2b.as_array()) print("Check for 2D chan for FiniteDiff label {}".format( labels[i]))
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)