test_mghimage.py 2.02 KB
Newer Older
Paul McCarthy's avatar
Paul McCarthy committed
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
#
# test_mghimage.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#


import os.path as op
10
import            shutil
Paul McCarthy's avatar
Paul McCarthy committed
11
12
13
14

import numpy   as np
import nibabel as nib

Paul McCarthy's avatar
Paul McCarthy committed
15
16
17
18
import fsl.utils.tempdir    as tempdir
import fsl.transform.affine as affine
import fsl.data.mghimage    as fslmgh
import fsl.data.image       as fslimage
19
20
21


datadir = op.join(op.abspath(op.dirname(__file__)), 'testdata')
Paul McCarthy's avatar
Paul McCarthy committed
22
23
24
25
26
27


def test_MGHImage():

    testfile = op.join(datadir, 'example.mgz')

Paul McCarthy's avatar
Paul McCarthy committed
28
    # Load from a file
Paul McCarthy's avatar
Paul McCarthy committed
29
30
    img      = fslmgh.MGHImage(testfile)
    nbimg    = nib.load(testfile)
31
    v2s      = nbimg.header.get_vox2ras_tkr()
Paul McCarthy's avatar
Paul McCarthy committed
32
    w2s      = affine.concat(v2s, affine.invert(nbimg.affine))
33

Paul McCarthy's avatar
Paul McCarthy committed
34
    assert np.all(np.isclose(img[:],             np.asanyarray(nbimg.dataobj)))
35
36
    assert np.all(np.isclose(img.voxToWorldMat,  nbimg.affine))
    assert np.all(np.isclose(img.voxToSurfMat,   v2s))
Paul McCarthy's avatar
Paul McCarthy committed
37
    assert np.all(np.isclose(img.surfToVoxMat,   affine.invert(v2s)))
38
    assert np.all(np.isclose(img.worldToSurfMat, w2s))
Paul McCarthy's avatar
Paul McCarthy committed
39
    assert np.all(np.isclose(img.surfToWorldMat, affine.invert(w2s)))
Paul McCarthy's avatar
Paul McCarthy committed
40

41
42
    assert img.name         == op.basename(testfile)
    assert img.dataSource   == testfile
Paul McCarthy's avatar
Paul McCarthy committed
43
44
45
46
    assert img.mghImageFile == testfile

    # Load from an in-memory nibabel object
    img = fslmgh.MGHImage(nbimg)
Paul McCarthy's avatar
Paul McCarthy committed
47
    assert np.all(np.isclose(img[:],            np.asanyarray(nbimg.dataobj)))
Paul McCarthy's avatar
Paul McCarthy committed
48
    assert np.all(np.isclose(img.voxToWorldMat, nbimg.affine))
49
    assert img.dataSource   is None
Paul McCarthy's avatar
Paul McCarthy committed
50
    assert img.mghImageFile is None
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69


def test_MGHImage_save():

    testfile = op.join(datadir, 'example.mgz')

    with tempdir.tempdir():

        shutil.copy(testfile, 'example.mgz')

        testfile = 'example.mgz'

        img = fslmgh.MGHImage(testfile)

        img.save()

        expfile = op.abspath(fslimage.addExt('example', mustExist=False))

        assert img.dataSource == op.abspath(expfile)
Paul McCarthy's avatar
Paul McCarthy committed
70
71
72
73
74
75


def test_voxToSurfMat():
    testfile = op.join(datadir, 'example.mgz')
    img = fslmgh.MGHImage(testfile)
    assert np.all(np.isclose(img.voxToSurfMat, fslmgh.voxToSurfMat(img)))