Example #1
0
def _affine_field(src, ref, xform, srcSpace, refSpace, shape=None, fv2w=None):

    if shape is None: shape = ref.shape[:3]
    if fv2w is None: fv2w = ref.getAffine('voxel', 'world')

    rx, ry, rz = np.meshgrid(np.arange(shape[0]),
                             np.arange(shape[1]),
                             np.arange(shape[2]),
                             indexing='ij')

    rvoxels = np.vstack((rx.flatten(), ry.flatten(), rz.flatten())).T
    f2r = affine.concat(ref.getAffine('world', refSpace), fv2w)
    rcoords = affine.transform(rvoxels, f2r)
    scoords = affine.transform(rcoords, xform)

    field = np.zeros(list(shape[:3]) + [3])
    field[:] = (scoords - rcoords).reshape(*it.chain(shape, [3]))
    field = nonlinear.DeformationField(field,
                                       src,
                                       ref,
                                       srcSpace=srcSpace,
                                       refSpace=refSpace,
                                       xform=fv2w,
                                       header=ref.header,
                                       defType='relative')
    return field
Example #2
0
def _random_affine_field():

    src = _random_image()
    ref = _random_image()

    # our test field just encodes an affine
    xform = affine.compose(np.random.randint(2, 5, 3),
                           np.random.randint(1, 10, 3), np.random.random(3))

    rx, ry, rz = np.meshgrid(np.arange(ref.shape[0]),
                             np.arange(ref.shape[1]),
                             np.arange(ref.shape[2]),
                             indexing='ij')

    rvoxels = np.vstack((rx.flatten(), ry.flatten(), rz.flatten())).T
    rcoords = affine.transform(rvoxels, ref.voxToScaledVoxMat)
    scoords = affine.transform(rcoords, xform)

    field = np.zeros(list(ref.shape[:3]) + [3])
    field[:] = (scoords - rcoords).reshape(*it.chain(ref.shape, [3]))
    field = nonlinear.DeformationField(field,
                                       src,
                                       ref,
                                       header=ref.header,
                                       defType='relative')
    return field, xform
Example #3
0
def test_applyDeformation_worldAligned():
    refv2w   = affine.scaleOffsetXform([1, 1, 1], [10,   10,   10])
    fieldv2w = affine.scaleOffsetXform([2, 2, 2], [10.5, 10.5, 10.5])
    src2ref  = refv2w
    ref2src  = affine.invert(src2ref)

    srcdata = np.random.randint(1, 65536, (10, 10, 10))

    src   = fslimage.Image(srcdata)
    ref   = fslimage.Image(srcdata, xform=src2ref)
    field = _affine_field(src, ref, ref2src, 'world', 'world',
                          shape=(5, 5, 5), fv2w=fieldv2w)

    field = nonlinear.DeformationField(
        nonlinear.convertDeformationType(field, 'absolute'),
        header=field.header,
        src=src,
        ref=ref,
        srcSpace='world',
        refSpace='world',
        defType='absolute')

    expect, xf = resample.resampleToReference(
        src, ref, matrix=src2ref, order=1, mode='constant', cval=0)
    result = nonlinear.applyDeformation(
        src, field, order=1, mode='constant', cval=0)

    expect = expect[1:-1, 1:-1, 1:-1]
    result = result[1:-1, 1:-1, 1:-1]

    assert np.all(np.isclose(expect, result))
Example #4
0
def test_detectDeformationType():
    relfield = _random_field()
    coords = _field_coords(relfield)
    absfield = nonlinear.DeformationField(relfield.data + coords,
                                          src=relfield.src,
                                          xform=relfield.voxToWorldMat)

    assert nonlinear.detectDeformationType(relfield) == 'relative'
    assert nonlinear.detectDeformationType(absfield) == 'absolute'
Example #5
0
def _random_field():

    src = _random_image()
    vx, vy, vz = np.random.randint(10, 50, 3)
    dx, dy, dz = np.random.randint(1, 10, 3)

    field = (np.random.random((vx, vy, vz, 3)) - 0.5) * 10
    aff = affine.compose((dx, dy, dz), np.random.randint(1, 100, 3),
                         np.random.random(3) * np.pi / 2)

    return nonlinear.DeformationField(field, src=src, xform=aff)
Example #6
0
def test_DeformationField_transform():

    relfield, xform = _random_affine_field()
    src = relfield.src
    ref = relfield.ref

    rx, ry, rz = np.meshgrid(np.arange(ref.shape[0]),
                             np.arange(ref.shape[1]),
                             np.arange(ref.shape[2]),
                             indexing='ij')
    rvoxels = np.vstack((rx.flatten(), ry.flatten(), rz.flatten())).T
    rcoords = affine.transform(rvoxels, ref.voxToScaledVoxMat)
    scoords = affine.transform(rcoords, xform)
    svoxels = affine.transform(scoords, src.scaledVoxToVoxMat)

    absfield = np.zeros(list(ref.shape[:3]) + [3])
    absfield[:] = scoords.reshape(*it.chain(ref.shape, [3]))
    absfield = nonlinear.DeformationField(absfield,
                                          src,
                                          ref,
                                          header=ref.header,
                                          defType='absolute')

    got = relfield.transform(rcoords)
    assert np.all(np.isclose(got, scoords))
    got = absfield.transform(rcoords)
    assert np.all(np.isclose(got, scoords))

    # test single set of coords
    got = absfield.transform(rcoords[0])
    assert np.all(np.isclose(got, scoords[0]))

    got = relfield.transform(rvoxels, from_='voxel', to='voxel')
    assert np.all(np.isclose(got, svoxels))
    got = absfield.transform(rvoxels, from_='voxel', to='voxel')
    assert np.all(np.isclose(got, svoxels))

    # test out of bounds are returned as nan
    rvoxels = np.array([[-1, -1, -1], [0, 0, 0]])
    rcoords = affine.transform(rvoxels, ref.voxToScaledVoxMat)
    scoords = affine.transform(rcoords, xform)
    svoxels = affine.transform(scoords, src.scaledVoxToVoxMat)

    got = relfield.transform(rcoords)
    assert np.all(np.isnan(got[0, :]))
    assert np.all(np.isclose(got[1, :], scoords[1, :]))
    got = absfield.transform(rcoords)
    assert np.all(np.isnan(got[0, :]))
    assert np.all(np.isclose(got[1, :], scoords[1, :]))
Example #7
0
def test_convertDeformationType():

    relfield = _random_field()
    coords = _field_coords(relfield)
    absfield = nonlinear.DeformationField(relfield.data + coords,
                                          src=relfield.src,
                                          xform=relfield.voxToWorldMat)

    gotconvrel1 = nonlinear.convertDeformationType(relfield)
    gotconvabs1 = nonlinear.convertDeformationType(absfield)
    gotconvrel2 = nonlinear.convertDeformationType(relfield, 'absolute')
    gotconvabs2 = nonlinear.convertDeformationType(absfield, 'relative')

    tol = dict(atol=1e-3, rtol=1e-3)

    assert np.all(np.isclose(gotconvrel1, absfield.data, **tol))
    assert np.all(np.isclose(gotconvabs1, relfield.data, **tol))
    assert np.all(np.isclose(gotconvrel2, absfield.data, **tol))
    assert np.all(np.isclose(gotconvabs2, relfield.data, **tol))