11""" Tests for the parrec2nii exe code
22"""
3- import imp
43from os .path import join , isfile , basename
54
65import numpy
98import nibabel
109from nibabel import parrec2nii_cmd as parrec2nii
1110
12- from mock import Mock , MagicMock
11+ from mock import Mock , MagicMock , patch
1312from nose .tools import assert_true
1413from numpy .testing import (assert_almost_equal , assert_array_equal )
1514
3029 [ 0. , 0. , 0. , 1. ]])
3130
3231
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.
4238 parrec2nii .verbose .switch = False
4339
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+
4641
47- parrec2nii .nifti1 = Mock ()
4842 nimg = Mock ()
4943 nhdr = MagicMock ()
5044 nimg .header = nhdr
5145 parrec2nii .nifti1 .Nifti1Image .return_value = nimg
5246
53- parrec2nii .pr = Mock ()
5447 pr_img = Mock ()
5548 pr_hdr = Mock ()
5649 pr_hdr .get_data_scaling .return_value = (npa ([]), npa ([]))
@@ -70,15 +63,15 @@ def test_parrec2nii_sets_qform_with_code2():
7063
7164 infile = 'nonexistent.PAR'
7265 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 )
7468
7569
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.
8275 parrec2nii .verbose .switch = False
8376
8477 opts = Mock ()
@@ -98,6 +91,6 @@ def test_parrec2nii_save_load_qform_code():
9891 outfname = join (pth , basename (fname )).replace ('.PAR' , '.nii' )
9992 assert_true (isfile (outfname ))
10093 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