def test_grid(): input = CoordinateSystem('ij', 'input') output = CoordinateSystem('xy', 'output') def f(ij): i = ij[:,0] j = ij[:,1] return np.array([i**2+j,j**3+i]).T cmap = CoordinateMap(input, output, f) grid = Grid(cmap) eval = ArrayCoordMap.from_shape(cmap, (50,40)) assert_true(np.allclose(grid[0:50,0:40].values, eval.values))
def test_grid32(): # Check that we can use a float32 input and output uv32 = CoordinateSystem('uv', 'input', np.float32) xyz32 = CoordinateSystem('xyz', 'output', np.float32) surface32 = CoordinateMap(uv32, xyz32, parametric_mapping) g = Grid(surface32) xyz_grid = g[-1:1:201j, -1:1:101j] x, y, z = xyz_grid.transposed_values yield assert_equal, x.shape, (201, 101) yield assert_equal, y.shape, (201, 101) yield assert_equal, z.shape, (201, 101) yield assert_equal, x.dtype, np.dtype(np.float32)
def __init__(self, matrix, coords, dtype=None): dtype = dtype or self.dtype if not isinstance(coords, CoordinateSystem): coords = CoordinateSystem(coords, 'space', coord_dtype=dtype) else: coords = CoordinateSystem(coords.coord_names, 'space', dtype) Linear.__init__(self, coords, coords, matrix.astype(dtype)) if not self.validate(): raise ValueError('this matrix is not an element of %s' % ` self.__class__ `) if not self.coords.coord_dtype == self.dtype: raise ValueError('the input coordinates builtin ' 'dtype should agree with self.dtype')
def test_grid32_c128(): # Check that we can use a float32 input and complex128 output uv32 = CoordinateSystem('uv', 'input', np.float32) xyz128 = CoordinateSystem('xyz', 'output', np.complex128) def par_c128(x): return parametric_mapping(x).astype(np.complex128) surface = CoordinateMap(uv32, xyz128, par_c128) g = Grid(surface) xyz_grid = g[-1:1:201j, -1:1:101j] x, y, z = xyz_grid.transposed_values yield assert_equal, x.shape, (201, 101) yield assert_equal, y.shape, (201, 101) yield assert_equal, z.shape, (201, 101) yield assert_equal, x.dtype, np.dtype(np.complex128)
def test_eval_slice(): input = CoordinateSystem('ij', 'input') output = CoordinateSystem('xy', 'output') def f(ij): i = ij[:,0] j = ij[:,1] return np.array([i**2+j,j**3+i]).T cmap = CoordinateMap(input, output, f) cmap = CoordinateMap(input, output, f) grid = Grid(cmap) e = grid[0:50,0:40] ee = e[0:20:3] yield assert_equal, ee.shape, (7,40) yield assert_equal, ee.values.shape, (280,2) yield assert_equal, ee.transposed_values.shape, (2,7,40) ee = e[0:20:2,3] yield assert_equal, ee.values.shape, (10,2) yield assert_equal, ee.transposed_values.shape, (2,10) yield assert_equal, ee.shape, (10,)
def convert3DCoordsTo4D(orig): # Get the data from the original coordinates domain = orig.function_domain outrange = orig.function_range affine = orig.affine print(type(affine), affine) # Add the temporal dimension mod_domain = CoordinateSystem(domain.coord_names + ('t', ), domain.name, domain.coord_dtype) mod_range = CoordinateSystem(outrange.coord_names + ('t', ), outrange.name, outrange.coord_dtype) # Temporal is a little trickier in the affine matrix # Create a new matrix mod_affine = np.zeros((5, 5)) # Set up the diagonals mod_affine[0, 0] = affine[0, 0] mod_affine[1, 1] = affine[1, 1] mod_affine[2, 2] = affine[2, 2] mod_affine[ 3, 3] = 2 # This is the new temporal dimension: each volume is 2s apart mod_affine[ 4, 4] = 1 # This is the last row in the matrix and should be all 0 except this column # Pull the last column of the first 3 rows into the new matrix mod_affine[0, 4] = affine[0, 3] mod_affine[1, 4] = affine[1, 3] mod_affine[2, 4] = affine[2, 3] print(mod_affine) modified = AffineTransform(mod_domain, mod_range, mod_affine) return modified
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: """ Parametrized surfaces using a CoordinateMap """ import numpy as np from nose.tools import assert_equal from nipy.core.api import CoordinateMap, CoordinateSystem from nipy.core.api import Grid uv = CoordinateSystem('uv', 'input') xyz = CoordinateSystem('xyz', 'output') def parametric_mapping(vals): """ Parametrization of the surface x**2-y**2*z**2+z**3=0 """ u = vals[:, 0] v = vals[:, 1] o = np.array([v * (u**2 - v**2), u, u**2 - v**2]).T return o """ Let's check that indeed this is a parametrization of that surface """