def test_translation(self): image = np.array([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]], dtype=DTYPE) intp = gryds.Interpolator(image, mode='mirror') trf1 = gryds.TranslationTransformation([0.1, 0]) trf2 = gryds.TranslationTransformation([-0.1, 0]) trf = gryds.ComposedTransformation(trf2, trf1) new_image = intp.transform(trf) np.testing.assert_almost_equal(image, new_image)
def test_rotation_translation(self): image = np.array([[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]], dtype=DTYPE) intp = gryds.Interpolator(image, mode='mirror') trf1 = gryds.TranslationTransformation([0.1, 0]) trf2 = gryds.AffineTransformation(ndim=2, angles=[0.1]) trf3 = gryds.AffineTransformation(ndim=2, angles=[-0.1]) trf4 = gryds.TranslationTransformation([-0.1, 0]) trf = gryds.ComposedTransformation(trf1, trf2, trf3, trf4) new_image = intp.transform(trf) np.testing.assert_almost_equal(image, new_image, decimal=6)
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)
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 test_repr(self): self.assertEqual(str(gryds.TranslationTransformation([3, 4])), 'TranslationTransformation(2D, t=[3 4])')
def test_base_interpolator_transform(self): trf = gryds.TranslationTransformation([1, 2, 3, 4, 5]) im = np.random.rand(10, 10, 10, 10, 10) intp = gryds.base.Interpolator(im) self.assertRaises(NotImplementedError, intp.transform, trf)
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(np.random.rand(3, 32, 32, 32), order=1) bsp = gryds.TranslationTransformation([0.1, 0.2, 0.3]) N = 1 Ns = range(0, 151, 1) M = 10 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()
def test_incompatible_error(self): trf1 = gryds.TranslationTransformation([0.1, 0, 0]) trf2 = gryds.TranslationTransformation([-0.1, 0]) self.assertRaises(ValueError, gryds.ComposedTransformation, trf2, trf1)
def augment(imA, imB): """ Input day0 and day4 image of the same rat An augmented version of both is returned """ #"3D" to 2D imA = imA.reshape((256, 256)) imB = imB.reshape((256, 256)) # Define random deformation matrix random_grid = np.random.rand(2, 3, 3) random_grid -= 0.5 random_grid /= 10 # Define B-Spline transformation matrix bspline = gryds.BSplineTransformation(random_grid) # Define random translation matrix a_translation = gryds.TranslationTransformation( [random.uniform(-0.03, 0.03), random.uniform(-0.03, 0.03)]) # Define random rotation matrix a_rotation = gryds.AffineTransformation( ndim=2, angles=[random.uniform(-np.pi / 24, np.pi / 24)], # ± 7 degrees center=[0.5, 0.5]) # Define an image interpolater an_image_interpolatorA = gryds.Interpolator(imA) an_image_interpolatorB = gryds.Interpolator(imB) # Combine all operations and apply the same augmentation to day0 and day4 composed = gryds.ComposedTransformation(bspline, a_rotation, a_translation) transformed_imageA = an_image_interpolatorA.transform(composed) transformed_imageB = an_image_interpolatorB.transform(composed) # Define noise augmentation mu = 0.0 sigma = random.uniform(0., 0.05) noise_mapA = np.random.normal(mu, sigma, size=np.size(imA)).reshape( (256, 256)) noise_mapB = np.random.normal(mu, sigma, size=np.size(imB)).reshape( (256, 256)) noise_mapA[transformed_imageA < 1e-2] = 0. noise_mapB[transformed_imageB < 1e-2] = 0. # Add noise to augmented image transformed_imageA = transformed_imageA + noise_mapA transformed_imageB = transformed_imageB + noise_mapB # Flip L/R (half of the time) perform_flip = random.choice([False, True]) if perform_flip: transformed_imageA = np.fliplr(transformed_imageA) transformed_imageB = np.fliplr(transformed_imageB) return [ transformed_imageA.reshape((256, 256, 1)), transformed_imageB.reshape((256, 256, 1)) ]