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_CompositionOperator_adjoint2(self):
        ig = self.ig
        data = self.data
        G = GradientOperator(domain_geometry=ig)

        Id1 = 2 * IdentityOperator(ig)
        Id2 = IdentityOperator(ig)

        d = CompositionOperator(G, Id1)
        da = d.direct(data)

        out1 = G.adjoint(da)
        out2 = d.adjoint(da)

        numpy.testing.assert_array_almost_equal(out2.as_array(),
                                                2 * out1.as_array())
    def test_CompositionOperator_direct1(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)
        out2 = d.direct(data)

        numpy.testing.assert_array_almost_equal(
            out2.get_item(0).as_array(),
            out1.get_item(0).as_array())
        numpy.testing.assert_array_almost_equal(
            out2.get_item(1).as_array(),
            out1.get_item(1).as_array())
Example #4
0
    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)