def test_dot_test(self):
        Grad3 = GradientOperator(self.ig3,
                                 correlation='Space',
                                 backend='numpy')

        # self.assertAlmostEqual(lhs3, rhs3)
        self.assertTrue(LinearOperator.dot_test(Grad3, verbose=True,
                                                decimal=4))
        self.assertTrue(LinearOperator.dot_test(Grad3, verbose=True,
                                                decimal=4))
Ejemplo n.º 2
0
    def test_GradientOperator_linearity(self):

        nc, nz, ny, nx = 3, 4, 5, 6
        ig = ImageGeometry(voxel_num_x=nx, voxel_num_y=ny, voxel_num_z=nz, channels=nc)

        grad = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='c')   
        self.assertTrue(LinearOperator.dot_test(grad))

        grad = GradientOperator(ig, bnd_cond='Periodic', correlation='SpaceChannels', backend='c')
        self.assertTrue(LinearOperator.dot_test(grad))

        grad = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='numpy')
        self.assertTrue(LinearOperator.dot_test(grad))

        grad = GradientOperator(ig, bnd_cond='Periodic', correlation='SpaceChannels', backend='numpy')
        self.assertTrue(LinearOperator.dot_test(grad))
Ejemplo n.º 3
0
    def test_SymmetrisedGradientOperator3b(self):
        ###########################################################################
        # 3D geometry no channels
        #ig3 = ImageGeometry(N, M, K)
        Grad3 = GradientOperator(self.ig3, correlation='Space')

        E3 = SymmetrisedGradientOperator(Grad3.range_geometry())
        numpy.random.seed(1)
        u3 = E3.domain_geometry().allocate('random')
        w3 = E3.range_geometry().allocate('random', symmetry=True)
        #
        lhs3 = E3.direct(u3).dot(w3)
        rhs3 = u3.dot(E3.adjoint(w3))
        # with numpy 1.11 and py 3.5 decimal = 3
        decimal = 4
        npv = version.parse(numpy.version.version)
        if npv.major == 1 and npv.minor == 11:
            print("########## SETTING decimal to 3 ###########")
            decimal = 3
        numpy.testing.assert_almost_equal(lhs3, rhs3, decimal=decimal)
        # self.assertAlmostEqual(lhs3, rhs3,  )
        print("*******", lhs3, rhs3, abs((rhs3 - lhs3) / rhs3), 1.5 * 10**(-4),
              abs((rhs3 - lhs3) / rhs3) < 1.5 * 10**(-4))
        self.assertTrue(
            LinearOperator.dot_test(E3,
                                    range_init=w3,
                                    domain_init=u3,
                                    decimal=decimal))
    def test_dot_test2(self):
        Grad3 = GradientOperator(self.ig3,
                                 correlation='SpaceChannel',
                                 backend='c')

        # self.assertAlmostEqual(lhs3, rhs3)
        # self.assertTrue( LinearOperator.dot_test(Grad3 , verbose=True))
        self.assertTrue(LinearOperator.dot_test(Grad3, decimal=4,
                                                verbose=True))
Ejemplo n.º 5
0
    def test_GradientOperator_complex_data(self):

        # make complex dtype
        self.ig_2D.dtype = numpy.complex
        x = self.ig_2D.allocate('random')

        Grad = GradientOperator(domain_geometry=self.ig_2D)

        res1 = Grad.direct(x)
        res2 = Grad.range.allocate()
        Grad.direct(x, out=res2)

        numpy.testing.assert_array_almost_equal(res1[0].as_array(),
                                                res2[0].as_array())
        numpy.testing.assert_array_almost_equal(res1[1].as_array(),
                                                res2[1].as_array())

        # check dot_test
        for sd in [5, 10, 15]:
            self.assertTrue(LinearOperator.dot_test(Grad, seed=sd))
Ejemplo n.º 6
0
    def test_GradientOperator_linearity(self):

        for geom in self.list_geometries:
            for bnd in self.bconditions:
                for backend in self.backend:
                    for corr in self.correlation:
                        for method in self.method:

                            Grad = GradientOperator(geom,
                                                    bnd_cond=bnd,
                                                    backend=backend,
                                                    correlation=corr,
                                                    method=method)
                            try:
                                for sd in [5, 10, 15]:
                                    self.assertTrue(
                                        LinearOperator.dot_test(Grad, seed=sd))
                            except:
                                self.print_assertion_info(
                                    geom, bnd, backend, corr, method, None)
                                raise
    def test_SymmetrisedGradientOperator3b(self):
        ###########################################################################
        # 3D geometry no channels
        #ig3 = ImageGeometry(N, M, K)
        Grad3 = GradientOperator(self.ig3, correlation='Space')

        E3 = SymmetrisedGradientOperator(Grad3.range_geometry())
        numpy.random.seed(1)
        u3 = E3.domain_geometry().allocate('random')
        w3 = E3.range_geometry().allocate('random', symmetry=True)
        #
        lhs3 = E3.direct(u3).dot(w3)
        rhs3 = u3.dot(E3.adjoint(w3))

        numpy.testing.assert_almost_equal(lhs3, rhs3, decimal=3)
        print("*******", lhs3, rhs3, abs((rhs3 - lhs3) / rhs3), 1.5 * 10**(-4),
              abs((rhs3 - lhs3) / rhs3) < 1.5 * 10**(-4))
        self.assertTrue(
            LinearOperator.dot_test(E3,
                                    range_init=w3,
                                    domain_init=u3,
                                    decimal=3))
Ejemplo n.º 8
0
    def test_GradientOperator_for_pseudo_2D_geometries(self):

        numpy.random.seed(1)
        # ImageGeometry shape (5,5,1)
        ig1 = ImageGeometry(voxel_num_x=1,
                            voxel_num_y=5,
                            voxel_num_z=5,
                            voxel_size_x=0.4,
                            voxel_size_y=0.2,
                            voxel_size_z=0.6)
        # ImageGeometry shape (1,5,5)
        ig2 = ImageGeometry(voxel_num_x=5,
                            voxel_num_y=5,
                            voxel_num_z=1,
                            voxel_size_x=0.1,
                            voxel_size_y=0.2,
                            voxel_size_z=0.4)
        # ImageGeometry shape (5,1,5)
        ig3 = ImageGeometry(voxel_num_x=5,
                            voxel_num_y=1,
                            voxel_num_z=5,
                            voxel_size_x=0.6,
                            voxel_size_y=0.4,
                            voxel_size_z=0.3)

        data1 = ig1.allocate('random')
        data2 = ig2.allocate('random')
        data3 = ig3.allocate('random')

        data = [data1, data2, data3]
        ig = [ig1, ig2, ig3]

        for i in range(3):

            ########################################
            ##### Test Gradient numpy backend  #####
            ########################################
            Grad_numpy = GradientOperator(ig[i], backend='numpy')
            res1 = Grad_numpy.direct(data[i])
            res2 = Grad_numpy.range_geometry().allocate()
            Grad_numpy.direct(data[i], out=res2)

            # test direct with and without out
            numpy.testing.assert_array_almost_equal(res1[0].as_array(),
                                                    res2[0].as_array())
            numpy.testing.assert_array_almost_equal(res1[1].as_array(),
                                                    res2[1].as_array())

            # test adjoint with and without out
            res3 = Grad_numpy.adjoint(res1)
            res4 = Grad_numpy.domain_geometry().allocate()
            Grad_numpy.adjoint(res2, out=res4)
            numpy.testing.assert_array_almost_equal(res3.as_array(),
                                                    res4.as_array())

            # test dot_test
            for sd in [5, 10, 15]:
                self.assertTrue(LinearOperator.dot_test(Grad_numpy, seed=sd))

            # test shape of output of direct
            self.assertEqual(res1[0].shape, ig[i].shape)
            self.assertEqual(res1.shape, (2, 1))

            ########################################
            ##### Test Gradient c backend  #####
            ########################################
            Grad_c = GradientOperator(ig[i], backend='c')

            # test direct with and without out
            res5 = Grad_c.direct(data[i])
            res6 = Grad_c.range_geometry().allocate() * 0.
            Grad_c.direct(data[i], out=res6)

            numpy.testing.assert_array_almost_equal(res5[0].as_array(),
                                                    res6[0].as_array())
            numpy.testing.assert_array_almost_equal(res5[1].as_array(),
                                                    res6[1].as_array())

            # test adjoint
            res7 = Grad_c.adjoint(res5)
            res8 = Grad_c.domain_geometry().allocate() * 0.
            Grad_c.adjoint(res5, out=res8)
            numpy.testing.assert_array_almost_equal(res7.as_array(),
                                                    res8.as_array())

            # test dot_test
            for sd in [5, 10, 15]:
                self.assertTrue(LinearOperator.dot_test(Grad_c, seed=sd))

            # test direct numpy vs direct c backends (with and without out)
            numpy.testing.assert_array_almost_equal(res5[0].as_array(),
                                                    res1[0].as_array())
            numpy.testing.assert_array_almost_equal(res6[1].as_array(),
                                                    res2[1].as_array())