1
1
""" Tests for the parrec2nii exe code
2
2
"""
3
- import imp
4
3
from os .path import join , isfile , basename
5
4
6
5
import numpy
9
8
import nibabel
10
9
from nibabel import parrec2nii_cmd as parrec2nii
11
10
12
- from mock import Mock , MagicMock
11
+ from mock import Mock , MagicMock , patch
13
12
from nose .tools import assert_true
14
13
from numpy .testing import (assert_almost_equal , assert_array_equal )
15
14
30
29
[ 0. , 0. , 0. , 1. ]])
31
30
32
31
33
- def teardown ():
34
- # Reload tested module to clear run-time settings in tests
35
- imp .reload (parrec2nii )
36
-
37
-
38
- def test_parrec2nii_sets_qform_with_code2 ():
39
- """Unit test that ensures that set_qform() is called on the new header.
40
- """
41
- imp .reload (parrec2nii )
32
+ @patch ('nibabel.parrec2nii_cmd.verbose' )
33
+ @patch ('nibabel.parrec2nii_cmd.io_orientation' )
34
+ @patch ('nibabel.parrec2nii_cmd.nifti1' )
35
+ @patch ('nibabel.parrec2nii_cmd.pr' )
36
+ def test_parrec2nii_sets_qform_sform_code1 (* args ):
37
+ # Check that set_sform(), set_qform() are called on the new header.
42
38
parrec2nii .verbose .switch = False
43
39
44
- parrec2nii .io_orientation = Mock ()
45
- parrec2nii .io_orientation .return_value = [[0 , 1 ],[1 , 1 ],[2 , 1 ]] # LAS+
40
+ parrec2nii .io_orientation .return_value = [[0 , 1 ],[1 , 1 ],[2 , 1 ]] # LAS+
46
41
47
- parrec2nii .nifti1 = Mock ()
48
42
nimg = Mock ()
49
43
nhdr = MagicMock ()
50
44
nimg .header = nhdr
51
45
parrec2nii .nifti1 .Nifti1Image .return_value = nimg
52
46
53
- parrec2nii .pr = Mock ()
54
47
pr_img = Mock ()
55
48
pr_hdr = Mock ()
56
49
pr_hdr .get_data_scaling .return_value = (npa ([]), npa ([]))
@@ -70,15 +63,15 @@ def test_parrec2nii_sets_qform_with_code2():
70
63
71
64
infile = 'nonexistent.PAR'
72
65
parrec2nii .proc_file (infile , opts )
73
- nhdr .set_qform .assert_called_with (pr_hdr .get_affine (), code = 2 )
66
+ nhdr .set_qform .assert_called_with (AN_OLD_AFFINE , code = 1 )
67
+ nhdr .set_sform .assert_called_with (AN_OLD_AFFINE , code = 1 )
74
68
75
69
76
- def test_parrec2nii_save_load_qform_code ():
77
- """Tests that after parrec2nii saves file, it has the qform 'code'
78
- set to '2', which means 'aligned', so that other software, e.g. FSL
79
- picks up the qform.
80
- """
81
- imp .reload (parrec2nii )
70
+ @patch ('nibabel.parrec2nii_cmd.verbose' )
71
+ def test_parrec2nii_save_load_qform_code (* args ):
72
+ # Tests that after parrec2nii saves file, it has the sform and qform 'code'
73
+ # set to '1', which means 'scanner', so that other software, e.g. FSL picks
74
+ # up the qform.
82
75
parrec2nii .verbose .switch = False
83
76
84
77
opts = Mock ()
@@ -98,6 +91,6 @@ def test_parrec2nii_save_load_qform_code():
98
91
outfname = join (pth , basename (fname )).replace ('.PAR' , '.nii' )
99
92
assert_true (isfile (outfname ))
100
93
img = nibabel .load (outfname )
101
- assert_almost_equal (img .get_affine () , PAR_AFFINE , 4 )
102
- assert_array_equal (img .header ['qform_code' ], 2 )
103
- assert_array_equal (img .header ['sform_code' ], 2 )
94
+ assert_almost_equal (img .affine , PAR_AFFINE , 4 )
95
+ assert_array_equal (img .header ['qform_code' ], 1 )
96
+ assert_array_equal (img .header ['sform_code' ], 1 )
0 commit comments