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))
        def test_bspline_2d(self):
            bspline_grid = np.array([[[0.1, 0], [0, 0]], [[0, 0], [0, 0]]])
            trf = gryds.BSplineTransformationCuda(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)))
        def test_translation_bspline_2d(self):
            bspline_grid = np.ones((2, 2, 2))
            trf = gryds.BSplineTransformationCuda(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)
Exemplo n.º 4
0
import os
import sys
sys.path.append(os.path.abspath('..'))
import gryds
import numpy as np
from cProfile import Profile
from pstats import Stats
import time
import matplotlib.pyplot as plt
import seaborn as sns

bsp = gryds.BSplineTransformation(0.01 * (np.random.rand(3, 32, 32, 32) - 0.5), order=1)
bsp_cuda = gryds.BSplineTransformationCuda(0.01 * (np.random.rand(3, 32, 32, 32) - 0.5), order=1)
N = 1

Ns = range(0, 15, 1)
M = 2

image = np.random.rand(N, 128, 128)
intp = gryds.BSplineInterpolatorCuda(image)
intp.transform(bsp)

times = []
for i in range(M):
    ts = []
    for N in Ns:
        print(i, N)
        image = np.random.rand(N, 128, 128)
        intp = gryds.Interpolator(image, order=1)
        t0 = time.time()
        intp.transform(bsp, bsp, bsp)