|
1 | 1 | """ Tests for the parrec2nii exe code |
2 | 2 | """ |
3 | 3 | import imp, numpy |
| 4 | +from numpy.testing import (assert_almost_equal, |
| 5 | + assert_array_equal) |
| 6 | +from nose.tools import assert_equal |
4 | 7 | from mock import Mock, MagicMock |
5 | 8 | from numpy import array as npa |
| 9 | +from test_parrec import EG_PAR, VARY_PAR |
| 10 | +from os.path import dirname, join, isfile, basename |
| 11 | +from ..tmpdirs import InTemporaryDirectory |
6 | 12 |
|
7 | 13 | AN_OLD_AFFINE = numpy.array( |
8 | 14 | [[-3.64994708, 0., 1.83564171, 123.66276611], |
9 | 15 | [0., -3.75, 0., 115.617], |
10 | 16 | [0.86045705, 0., 7.78655376, -27.91161211], |
11 | 17 | [0., 0., 0., 1.]]) |
12 | 18 |
|
| 19 | +PAR_AFFINE = numpy.array( |
| 20 | +[[ -3.64994708, 0. , 1.83564171, 107.63076611], |
| 21 | + [ 0. , 3.75, 0. , -118.125 ], |
| 22 | + [ 0.86045705, 0. , 7.78655376, -58.25061211], |
| 23 | + [ 0. , 0. , 0. , 1. ]]) |
| 24 | + |
13 | 25 | def test_parrec2nii_sets_qform_with_code2(): |
| 26 | + """Unit test that ensures that set_qform() is called on the new header. |
| 27 | + """ |
14 | 28 | parrec2nii = imp.load_source('parrec2nii', 'bin/parrec2nii') |
15 | 29 | parrec2nii.verbose.switch = False |
16 | 30 |
|
@@ -43,4 +57,35 @@ def test_parrec2nii_sets_qform_with_code2(): |
43 | 57 |
|
44 | 58 | infile = 'nonexistent.PAR' |
45 | 59 | parrec2nii.proc_file(infile, opts) |
46 | | - nhdr.set_qform.assert_called_with(pr_hdr.get_affine(), code=2) |
| 60 | + nhdr.set_qform.assert_called_with(pr_hdr.get_affine(), code=2) |
| 61 | + |
| 62 | +def test_parrec2nii_save_load_qform_code(): |
| 63 | + """Tests that after parrec2nii saves file, it has the qform 'code' |
| 64 | + set to '2', which means 'aligned', so that other software, e.g. FSL |
| 65 | + picks up the qform. |
| 66 | + """ |
| 67 | + import nibabel |
| 68 | + parrec2nii = imp.load_source('parrec2nii', 'bin/parrec2nii') |
| 69 | + parrec2nii.verbose.switch = False |
| 70 | + |
| 71 | + opts = Mock() |
| 72 | + opts.outdir = None |
| 73 | + opts.scaling = 'off' |
| 74 | + opts.minmax = [1, 1] |
| 75 | + opts.store_header = False |
| 76 | + opts.bvs = False |
| 77 | + opts.vol_info = False |
| 78 | + opts.dwell_time = False |
| 79 | + opts.compressed = False |
| 80 | + |
| 81 | + with InTemporaryDirectory() as pth: |
| 82 | + opts.outdir = pth |
| 83 | + for fname in [EG_PAR, VARY_PAR]: |
| 84 | + parrec2nii.proc_file(fname, opts) |
| 85 | + outfname = join(pth, basename(fname)).replace('.PAR', '.nii') |
| 86 | + assert isfile(outfname) |
| 87 | + img = nibabel.load(outfname) |
| 88 | + assert_almost_equal(img.get_affine(), PAR_AFFINE, 4) |
| 89 | + assert_array_equal(img.header['qform_code'], 2) |
| 90 | + assert_array_equal(img.header['sform_code'], 2) |
| 91 | + |
0 commit comments