Exemple #1
0
def test_toFnirt():
    def check(got, exp):
        tol = dict(atol=1e-5, rtol=1e-5)
        assert np.all(np.isclose(got.data, exp.data, **tol))
        assert got.src.sameSpace(exp.src)
        assert got.ref.sameSpace(exp.ref)
        assert got.srcSpace == 'fsl'
        assert got.refSpace == 'fsl'

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

    spaces = it.permutations(('voxel', 'fsl', 'world'), 2)

    for from_, to in spaces:
        field = nonlinear.convertDeformationSpace(basefield, from_, to)
        got = fnirt.toFnirt(field)
        check(got, basefield)

    src = fslimage.Image(op.join(datadir, 'src'))
    ref = fslimage.Image(op.join(datadir, 'ref'))
    coef = fnirt.readFnirt(op.join(datadir, 'coefficientfield'), src, ref)
    got = fnirt.toFnirt(coef)
    check(got, coef)
Exemple #2
0
def test_readWriteNonLinearX5():
    datadir = op.join(op.dirname(__file__), 'testdata', 'nonlinear')
    dffile = op.join(datadir, 'displacementfield.nii.gz')
    srcfile = op.join(datadir, 'src.nii.gz')
    reffile = op.join(datadir, 'ref.nii.gz')

    src = fslimage.Image(srcfile)
    ref = fslimage.Image(reffile)
    dfield = fnirt.readFnirt(dffile, src, ref)
    wdfield = nonlinear.convertDeformationSpace(dfield, 'world', 'world')

    with tempdir.tempdir():

        # field must be world->world
        with pytest.raises(x5.X5Error):
            x5.writeNonLinearX5('nonlinear.x5', dfield)

        x5.writeNonLinearX5('nonlinear.x5', wdfield)

        gotdfield = x5.readNonLinearX5('nonlinear.x5')

        assert gotdfield.src.sameSpace(src)
        assert gotdfield.ref.sameSpace(ref)
        assert gotdfield.srcSpace == wdfield.srcSpace
        assert gotdfield.refSpace == wdfield.refSpace
        assert gotdfield.deformationType == wdfield.deformationType
        assert np.all(np.isclose(gotdfield.data, wdfield.data))

        with h5py.File('nonlinear.x5', 'r') as f:
            assert f.attrs['Type'] == 'nonlinear'
            _check_metadata(f)
            _check_deformation(f['/Transform'], wdfield)
            _check_space(f['/A'], ref)
            _check_space(f['/B'], src)
Exemple #3
0
def test_fromFnirt():

    basefield, basexform = _random_affine_field()
    src = basefield.src
    ref = basefield.ref
    spaces = list(it.permutations(('voxel', 'fsl', 'world'), 2))

    for from_, to in spaces:

        got = fnirt.fromFnirt(basefield, from_, to)

        assert got.srcSpace == to
        assert got.refSpace == from_

        coords = [
            np.random.randint(0, basefield.shape[0], 5),
            np.random.randint(0, basefield.shape[1], 5),
            np.random.randint(0, basefield.shape[2], 5)
        ]
        coords = np.array(coords).T

        coords = affine.transform(coords, ref.getAffine('voxel', from_))

        aff = affine.concat(src.getAffine('fsl', to), basexform,
                            ref.getAffine(from_, 'fsl'))

        got = got.transform(coords)
        exp = affine.transform(coords, aff)

        enan = np.isnan(exp)
        gnan = np.isnan(got)

        assert np.all(np.isclose(enan, gnan))
        assert np.all(np.isclose(exp[~enan], got[~gnan]))

    # Converting from a FNIRT coefficient field
    src = fslimage.Image(op.join(datadir, 'src'))
    ref = fslimage.Image(op.join(datadir, 'ref'))
    coef = fnirt.readFnirt(op.join(datadir, 'coefficientfield'), src, ref)
    disp = fnirt.readFnirt(op.join(datadir, 'displacementfield'), src, ref)

    for from_, to in spaces:

        cnv = fnirt.fromFnirt(coef, from_, to)
        exp = nonlinear.convertDeformationSpace(disp, from_, to)
        tol = dict(atol=1e-5, rtol=1e-5)
        assert np.all(np.isclose(cnv.data, exp.data, **tol))
Exemple #4
0
def test_convertDeformationSpace():

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

    # generate reference fsl->fsl coordinate mappings

    # For each combination of srcspace->tospace
    # Generate random coordinates, check that
    # displacements are correct
    spaces = ['fsl', 'voxel', 'world']
    spaces = list(it.combinations_with_replacement(spaces, 2))
    spaces = spaces + [(r, s) for s, r in spaces]
    spaces = list(set(spaces))

    for from_, to in spaces:

        refcoords = [
            np.random.randint(0, basefield.shape[0], 5),
            np.random.randint(0, basefield.shape[1], 5),
            np.random.randint(0, basefield.shape[2], 5)
        ]
        refcoords = np.array(refcoords, dtype=np.int).T
        refcoords = affine.transform(refcoords, ref.voxToScaledVoxMat)
        srccoords = basefield.transform(refcoords)

        field = nonlinear.convertDeformationSpace(basefield, from_, to)
        premat = ref.getAffine('fsl', from_)
        postmat = src.getAffine('fsl', to)

        input = affine.transform(refcoords, premat)
        expect = affine.transform(srccoords, postmat)

        got = field.transform(input)
        enan = np.isnan(expect)
        gnan = np.isnan(got)

        assert np.all(np.isclose(enan, gnan))
        assert np.all(np.isclose(expect[~enan], got[~gnan]))