Exemple #1
0
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))
Exemple #2
0
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))
Exemple #3
0
def test_2d_from_3d():
    # Resample a 3d image on a 2d affine grid
    # This example creates a coordmap that coincides with
    # the 10th slice of an image, and checks that
    # resampling agrees with the data in the 10th slice.
    shape = (100, 90, 80)
    g = AffineTransform.from_params('ijk', 'xyz', np.diag([0.5, 0.5, 0.5, 1]))
    i = Image(np.ones(shape), g)
    i.get_data()[50:55, 40:55, 30:33] = 3.
    a = np.identity(4)
    g2 = ArrayCoordMap.from_shape(g, shape)[10]
    ir = resample(i, g2.coordmap, a, g2.shape)
    assert_array_almost_equal(ir.get_data(), i[10].get_data())
def test_2d_from_3d():
    # Resample a 3d image on a 2d affine grid
    # This example creates a coordmap that coincides with
    # the 10th slice of an image, and checks that
    # resampling agrees with the data in the 10th slice.
    shape = (100, 90, 80)
    g = AffineTransform.from_params("ijk", "xyz", np.diag([0.5, 0.5, 0.5, 1]))
    i = Image(np.ones(shape), g)
    i[50:55, 40:55, 30:33] = 3.0
    a = np.identity(4)
    g2 = ArrayCoordMap.from_shape(g, shape)[10]
    ir = resample(i, g2.coordmap, a, g2.shape)
    yield assert_array_almost_equal, np.asarray(ir), np.asarray(i[10])
Exemple #5
0
def test_32():
    class O32(MG.O):
        dtype = np.float32

        def validate(self, M=None):
            """
            Check that the matrix is (almost) orthogonal.
            """
            if M is None:
                M = self.matrix
            return np.allclose(np.identity(self.ndims[0], dtype=self.dtype), np.dot(M.T, M), atol=1.0e-06)

    a = random_orth(3).matrix.astype(np.float32)

    A = O32(a, 'xyz')
    B = O32(random_orth(3).matrix.astype(np.float32), 'xyz')
    C = MG.product(A, B)
    yield assert_equal, C.dtype, np.float32

    ev = ArrayCoordMap.from_shape(C, (20,30,40))
    yield assert_equal, ev.values.dtype, np.float32
def test_32():
    class O32(MG.O):
        dtype = np.float32

        def validate(self, M=None):
            """
            Check that the matrix is (almost) orthogonal.
            """
            if M is None:
                M = self.matrix
            return np.allclose(np.identity(self.ndims[0], dtype=self.dtype), np.dot(M.T, M), atol=1.0e-06)

    a = random_orth(3).matrix.astype(np.float32)

    A = O32(a, 'xyz')
    B = O32(random_orth(3).matrix.astype(np.float32), 'xyz')
    C = MG.product(A, B)
    yield assert_equal, C.dtype, np.float32

    ev = ArrayCoordMap.from_shape(C, (20,30,40))
    yield assert_equal, ev.values.dtype, np.float32
Exemple #7
0
def resample(image, target, mapping, shape, order=3, **interp_kws):
    """
    Resample an image to a target CoordinateMap with a "world-to-world" mapping
    and spline interpolation of a given order.

    Here, "world-to-world" refers to the fact that mapping should be
    a callable that takes a physical coordinate in "target"
    and gives a physical coordinate in "image". 

    Parameters
    ----------
    image : Image instance that is to be resampled
    target :target CoordinateMap for output image
    mapping : transformation from target.function_range
               to image.coordmap.function_range, i.e. 'world-to-world mapping'
               Can be specified in three ways: a callable, a
               tuple (A, b) representing the mapping y=dot(A,x)+b
               or a representation of this in homogeneous coordinates. 
    shape : shape of output array, in target.function_domain
    order : what order of interpolation to use in `scipy.ndimage`
    interp_kws : keyword arguments for ndimage interpolator routine

    Returns
    -------
    output : Image instance with interpolated data and output.coordmap == target
                  
    """

    if not callable(mapping):
        if type(mapping) is type(()):
            A, b = mapping
            ndimout = b.shape[0]
            ndimin = A.shape[1]
            mapping  = np.zeros((ndimout+1, ndimin+1))
            mapping[:ndimout,:ndimin] = A
            mapping[:ndimout,-1] = b
            mapping[-1,-1] = 1.

     # image world to target world mapping

        TW2IW = AffineTransform(target.function_range, image.coordmap.function_range, mapping)
    else:
        TW2IW = CoordinateMap(mapping, target.function_range, image.coordmap.function_range)

    function_domain = target.function_domain
    function_range = image.coordmap.function_range

    # target voxel to image world mapping
    TV2IW = compose(TW2IW, target)

    # CoordinateMap describing mapping from target voxel to
    # image world coordinates

    if not isinstance(TV2IW, AffineTransform):
        # interpolator evaluates image at values image.coordmap.function_range,
        # i.e. physical coordinates rather than voxel coordinates
        grid = ArrayCoordMap.from_shape(TV2IW, shape)
        interp = ImageInterpolator(image, order=order)
        idata = interp.evaluate(grid.transposed_values, **interp_kws)
        del(interp)
    else:
        TV2IV = compose(image.coordmap.inverse(), TV2IW)
        if isinstance(TV2IV, AffineTransform):
            A, b = affines.to_matrix_vector(TV2IV.affine)
            data = np.asarray(image)
            idata = affine_transform(data, A,
                                     offset=b,
                                     output_shape=shape,
                                     output=data.dtype,
                                     order=order,
                                     **interp_kws)
        else:
            interp = ImageInterpolator(image, order=order)
            grid = ArrayCoordMap.from_shape(TV2IV, shape)
            idata = interp.evaluate(grid.values, **interp_kws)
            del(interp)
            
    return Image(idata, target)