예제 #1
0
    def test_5d_grid_range(self):
        a_grid = gryds.Grid((10, 10, 10, 10, 10))

        self.assertEqual(a_grid.grid[0, 9, 0, 0, 0, 0],
                         np.array(0.9, dtype=DTYPE))
        self.assertEqual(a_grid.grid[4, 0, 0, 0, 0, 9],
                         np.array(0.9, dtype=DTYPE))
예제 #2
0
    def test_5d_downscaling(self):
        matrix = np.zeros((5, 6))
        for i in range(5):
            matrix[i, i] = 1
        matrix[2, 2] = 1.5

        trf = gryds.LinearTransformation(
            matrix)  # scale grid by 150$ isotropically

        grid = gryds.Grid((2, 3, 4, 5, 6))
        new_grid = grid.transform(trf)

        # The original grid runs from 0 to 0.9
        # The transformed grid should run from 0 to 1.35
        np.testing.assert_equal(new_grid.grid[0, 0], np.array(0, DTYPE))
        np.testing.assert_almost_equal(
            new_grid.grid[2, :, :, 3],
            np.array(1.125, DTYPE),  # 1.5 * 0.75
            decimal=6)

        # The jacobian of this transformation should be 1 everywhere, i.e. no
        # scaling should have happened
        np.testing.assert_almost_equal(
            grid.jacobian_det(trf),
            np.array(1.5, DTYPE),  # i.e. 1.5^5
            decimal=4)
예제 #3
0
    def test_2d_grid_scaling(self):
        a_grid = gryds.Grid((10, 20))

        new_grid = a_grid.scaled_to((3, 4))
        self.assertAlmostEqual(new_grid.grid[0, 9, 0],
                               np.array(2.7, dtype=DTYPE),
                               places=6)
        self.assertAlmostEqual(new_grid.grid[1, 0, 19],
                               np.array(3.8, dtype=DTYPE),
                               places=6)
        def test_bspline_2d_folding(self):
            bspline_grid = np.array([
                # Folds the top half of the image slightly over the bottom half.
                [[0.51, 0.51], [-0.5, -0.5]],
                [[0, 0], [0, 0]]
            ])
            trf = gryds.BSplineTransformationCuda(bspline_grid, order=1)

            grid = gryds.Grid((100, 20))

            # The jacobian of this transformation should be below 0 everywhere
            self.assertTrue(np.all(grid.jacobian_det(trf) < 0))
예제 #5
0
    def test_3d_45_deg_rotation_with_center(self):
        trf = gryds.AffineTransformation(
            ndim=3,
            angles=[0, 0.25 * np.pi, 0],
            center_of=np.zeros((10, 20, 20)))  # rotate grid 45° anticlockwise

        grid = gryds.Grid((10, 20, 20))

        # The jacobian of this transformation should be 1 everywhere, i.e. no
        # scaling should have happened
        np.testing.assert_almost_equal(grid.jacobian_det(trf),
                                       np.array(1, DTYPE),
                                       decimal=5)
예제 #6
0
    def test_5d_grid_scaling(self):
        a_grid = gryds.Grid((10, 10, 10, 10, 10))

        new_grid = a_grid.scaled_to((3, 4, 5, 6, 7))

        self.assertAlmostEqual(
            new_grid.grid[0, 9, 0, 0, 0, 0],
            np.array(2.7, dtype=DTYPE),  # 3 x 9 / 10
            places=6)
        self.assertAlmostEqual(
            new_grid.grid[4, 0, 0, 0, 0, 9],
            np.array(6.3, dtype=DTYPE),  # 7 x 9 / 10
            places=6)
예제 #7
0
    def test_bspline_2d(self):
        bspline_grid = np.array([[[0.1, 0], [0, 0]], [[0, 0], [0, 0]]])
        trf = gryds.BSplineTransformation(bspline_grid)

        grid = gryds.Grid((10, 20))
        new_grid = grid.transform(trf)

        # The top left has been displaced by 10% or 0.1 pixels in the i-direction
        np.testing.assert_almost_equal(new_grid.grid[0, 0, 0],
                                       np.array(0.1, DTYPE))

        # The jacobian of this transformation should NOT be 1 everywhere, i.e.
        # scaling should have happened, and the new volume should be smaller
        # as the top left has been folded in
        self.assertTrue(np.all(grid.jacobian_det(trf) < np.array(1, DTYPE)))
예제 #8
0
    def test_2d_90_deg_rotation(self):
        trf = gryds.AffineTransformation(ndim=2, angles=[0.5 * np.pi]) # rotate grid 90 degrees clockwise

        grid = gryds.Grid((10, 20))
        new_grid = grid.transform(trf)

        # The grid runs from 0 to 0.95 on the j-axis
        # 90 deg rot means the i-axis will run from 0 to -0.95
        np.testing.assert_equal(new_grid.grid[0, 0, 0], np.array(0, DTYPE))
        np.testing.assert_equal(new_grid.grid[0, 0, -1], np.array(-0.95, DTYPE))

        # The jacobian of this transformation should be 1 everywhere, i.e. no
        # scaling should have happened
        np.testing.assert_almost_equal(
            grid.jacobian_det(trf),
            np.array(1, DTYPE),
            decimal=4)
예제 #9
0
    def test_translation_bspline_2d(self):
        bspline_grid = np.ones((2, 2, 2))
        trf = gryds.BSplineTransformation(bspline_grid)

        grid = gryds.Grid((10, 20))
        new_grid = grid.transform(trf)

        # The grid runs from 0 to 0.9 on the i-axis
        # Translation by 100% will mean that the i-axis will now run from 1 to 1.9
        np.testing.assert_equal(new_grid.grid[0, 0, 0], np.array(1, DTYPE))
        np.testing.assert_equal(new_grid.grid[0, -1, 0], np.array(1.9, DTYPE))

        # The jacobian of this transformation should be 1 everywhere, i.e. no
        # scaling should have happened
        np.testing.assert_almost_equal(grid.jacobian_det(trf),
                                       np.array(1, DTYPE),
                                       decimal=4)
    def test_2d_translation(self):
        trf = gryds.TranslationTransformation(
            [0.1, 0])  # move grid 10% downwards (moves image 10% upwards)

        grid = gryds.Grid((10, 20))
        new_grid = grid.transform(trf)

        # The original grid runs from 0 to 0.9 for the i-coordinates
        # The transformed grid should run from 0.1 to 1
        np.testing.assert_equal(new_grid.grid[0, 0], np.array(0.1, DTYPE))
        np.testing.assert_equal(new_grid.grid[0, 9], np.array(1.0, DTYPE))

        # The jacobian of this transformation should be 1 everywhere, i.e. no
        # scaling should have happened
        np.testing.assert_almost_equal(grid.jacobian_det(trf),
                                       np.array(1, DTYPE),
                                       decimal=4)
예제 #11
0
    def test_5d_translation(self):
        trf = gryds.TranslationTransformation([0, 0, 0.1, 0, 0])

        grid = gryds.Grid((10, 10, 10, 10, 10))
        new_grid = grid.transform(trf)

        # The original grid runs from 0 to 0.9
        # The transformed grid should run from 0.1 to 1
        self.assertTrue(np.all(new_grid.grid[2, :, :, 0] == np.array(0.1, DTYPE)))
        self.assertTrue(np.all(new_grid.grid[2, :, :, 9] == np.array(1.0, DTYPE)))

        # The jacobian of this transformation should be 1 everywhere, i.e. no
        # scaling should have happened
        np.testing.assert_almost_equal(
            grid.jacobian_det(trf),
            np.array(1, DTYPE),
            decimal=4)
def random_deformation_gen(image, bspline_shape=(3, 3), std=0.15):
    assert image.shape[0] == image.shape[1]
    bspline_grid_shape = (len(image.shape), ) + bspline_shape
    bspline_grid = np.random.rand(*bspline_grid_shape) * std

    a_bspline_transformation = gryds.BSplineTransformation(bspline_grid)
    an_image_interpolator = gryds.Interpolator(image, order=1)

    an_image_grid = gryds.Grid(
        image.shape)  # makes a Grid the size of the image
    a_deformed_image_grid = an_image_grid.transform(a_bspline_transformation)
    a_deformed_image = an_image_interpolator.resample(a_deformed_image_grid)

    f = (a_deformed_image_grid.grid - an_image_grid.grid) * image.shape[0]
    flow = np.ndarray(image.shape + (len(image.shape), ))

    for i in range(len(image.shape)):
        flow[..., i] = f[i, ...]

    return a_deformed_image, flow
예제 #13
0
    def test_2d_downscaling(self):
        trf = gryds.AffineTransformation(
            ndim=2, scaling=[1.5, 1])  # scale grid by 150% isotropically

        grid = gryds.Grid((10, 20))
        new_grid = grid.transform(trf)

        # The original grid runs from 0 to 0.9 for the i-coordinates
        # The transformed grid should run from 0 to 1.35
        np.testing.assert_equal(new_grid.grid[0, 0], np.array(0, DTYPE))
        np.testing.assert_almost_equal(new_grid.grid[0, 9],
                                       np.array(1.35, DTYPE),
                                       decimal=6)

        # The jacobian of this transformation should be 1 everywhere, i.e. no
        # scaling should have happened
        np.testing.assert_almost_equal(
            grid.jacobian_det(trf),
            np.array(1.5, DTYPE),  # i.e. 1.5*1.5
            decimal=4)
예제 #14
0
 def test_grid_wrong_scale_shape(self):
     a_grid = gryds.Grid((2, 2))
     self.assertRaises(ValueError, a_grid.scaled_to, [1, 2, 3])
예제 #15
0
 def test_grid_init(self):
     gryds.Grid(grid=np.zeros((2, 10, 10)))
예제 #16
0
 def test_grid_repr(self):
     a_grid = gryds.Grid((2, 2))
     self.assertEqual(str(a_grid), 'Grid(2D, 2x2)')
예제 #17
0
 def test_grid_repr(self):
     a_grid = gryds.Grid((2, 2))
     self.assertEqual(
         str(a_grid),
         'gryds.interpolators.grid.Grid(\n\t[[[0.  0. ]\n\t  [0.5 0.5]]\n\t\n\t [[0.  0.5]\n\t  [0.  0.5]]]\n)'
     )
예제 #18
0
    def test_2d_grid_ranges(self):
        a_grid = gryds.Grid((10, 20))

        self.assertEqual(a_grid.grid[0, 9, 0], np.array(0.9, dtype=DTYPE))
        self.assertEqual(a_grid.grid[1, 0, 19], np.array(0.95, dtype=DTYPE))