From 44efa7535999bbfd92d8675b7d463fd6e290f41c Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Mon, 15 Aug 2016 14:38:46 -0700 Subject: [PATCH 1/3] RF+API: add qform, sform code 1 for parrec2nii As discussed in https://github.com/nipy/nibabel/pull/478 --- nibabel/parrec2nii_cmd.py | 3 ++- nibabel/tests/test_parrec2nii.py | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/nibabel/parrec2nii_cmd.py b/nibabel/parrec2nii_cmd.py index 95f5f74459..dc26870ff6 100644 --- a/nibabel/parrec2nii_cmd.py +++ b/nibabel/parrec2nii_cmd.py @@ -221,7 +221,8 @@ def proc_file(infile, opts): nhdr = nimg.header nhdr.set_data_dtype(out_dtype) nhdr.set_slope_inter(slope, intercept) - nhdr.set_qform(affine, code=2) + nhdr.set_sform(affine, code=1) + nhdr.set_qform(affine, code=1) if 'parse' in opts.minmax: # need to get the scaled data diff --git a/nibabel/tests/test_parrec2nii.py b/nibabel/tests/test_parrec2nii.py index 2008988d83..157f3328a0 100644 --- a/nibabel/tests/test_parrec2nii.py +++ b/nibabel/tests/test_parrec2nii.py @@ -70,7 +70,8 @@ def test_parrec2nii_sets_qform_with_code2(): infile = 'nonexistent.PAR' parrec2nii.proc_file(infile, opts) - nhdr.set_qform.assert_called_with(pr_hdr.get_affine(), code=2) + nhdr.set_qform.assert_called_with(AN_OLD_AFFINE, code=1) + nhdr.set_sform.assert_called_with(AN_OLD_AFFINE, code=1) def test_parrec2nii_save_load_qform_code(): @@ -98,6 +99,6 @@ def test_parrec2nii_save_load_qform_code(): outfname = join(pth, basename(fname)).replace('.PAR', '.nii') assert_true(isfile(outfname)) img = nibabel.load(outfname) - assert_almost_equal(img.get_affine(), PAR_AFFINE, 4) - assert_array_equal(img.header['qform_code'], 2) - assert_array_equal(img.header['sform_code'], 2) + assert_almost_equal(img.affine, PAR_AFFINE, 4) + assert_array_equal(img.header['qform_code'], 1) + assert_array_equal(img.header['sform_code'], 1) From bf8f0878bbff77ee704dd3fd4d2b9da80dc537e6 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Tue, 16 Aug 2016 09:37:47 -0700 Subject: [PATCH 2/3] RF: rewrite comments with new behavior From review by CM. --- nibabel/tests/test_parrec2nii.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/nibabel/tests/test_parrec2nii.py b/nibabel/tests/test_parrec2nii.py index 157f3328a0..1f544f5c63 100644 --- a/nibabel/tests/test_parrec2nii.py +++ b/nibabel/tests/test_parrec2nii.py @@ -35,9 +35,8 @@ def teardown(): imp.reload(parrec2nii) -def test_parrec2nii_sets_qform_with_code2(): - """Unit test that ensures that set_qform() is called on the new header. - """ +def test_parrec2nii_sets_qform_sform_code1(): + # Unit test that ensures that set_qform() is called on the new header. imp.reload(parrec2nii) parrec2nii.verbose.switch = False @@ -75,10 +74,9 @@ def test_parrec2nii_sets_qform_with_code2(): def test_parrec2nii_save_load_qform_code(): - """Tests that after parrec2nii saves file, it has the qform 'code' - set to '2', which means 'aligned', so that other software, e.g. FSL - picks up the qform. - """ + # Tests that after parrec2nii saves file, it has the sform and qform 'code' + # set to '1', which means 'scanner', so that other software, e.g. FSL picks + # up the qform. imp.reload(parrec2nii) parrec2nii.verbose.switch = False From 12afd01fd8cac00930539bac75b4a4677b201fdd Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Tue, 16 Aug 2016 19:27:11 -0700 Subject: [PATCH 3/3] RF: do a more fancy Mock dance Use decorators to patch in the stuff we need for testing. --- nibabel/tests/test_parrec2nii.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/nibabel/tests/test_parrec2nii.py b/nibabel/tests/test_parrec2nii.py index 1f544f5c63..aa018b24d0 100644 --- a/nibabel/tests/test_parrec2nii.py +++ b/nibabel/tests/test_parrec2nii.py @@ -1,6 +1,5 @@ """ Tests for the parrec2nii exe code """ -import imp from os.path import join, isfile, basename import numpy @@ -9,7 +8,7 @@ import nibabel from nibabel import parrec2nii_cmd as parrec2nii -from mock import Mock, MagicMock +from mock import Mock, MagicMock, patch from nose.tools import assert_true from numpy.testing import (assert_almost_equal, assert_array_equal) @@ -30,26 +29,21 @@ [ 0. , 0. , 0. , 1. ]]) -def teardown(): - # Reload tested module to clear run-time settings in tests - imp.reload(parrec2nii) - - -def test_parrec2nii_sets_qform_sform_code1(): - # Unit test that ensures that set_qform() is called on the new header. - imp.reload(parrec2nii) +@patch('nibabel.parrec2nii_cmd.verbose') +@patch('nibabel.parrec2nii_cmd.io_orientation') +@patch('nibabel.parrec2nii_cmd.nifti1') +@patch('nibabel.parrec2nii_cmd.pr') +def test_parrec2nii_sets_qform_sform_code1(*args): + # Check that set_sform(), set_qform() are called on the new header. parrec2nii.verbose.switch = False - parrec2nii.io_orientation = Mock() - parrec2nii.io_orientation.return_value = [[0, 1],[1, 1],[2, 1]] # LAS+ + parrec2nii.io_orientation.return_value = [[0, 1],[1, 1],[2, 1]] # LAS+ - parrec2nii.nifti1 = Mock() nimg = Mock() nhdr = MagicMock() nimg.header = nhdr parrec2nii.nifti1.Nifti1Image.return_value = nimg - parrec2nii.pr = Mock() pr_img = Mock() pr_hdr = Mock() pr_hdr.get_data_scaling.return_value = (npa([]), npa([])) @@ -73,11 +67,11 @@ def test_parrec2nii_sets_qform_sform_code1(): nhdr.set_sform.assert_called_with(AN_OLD_AFFINE, code=1) -def test_parrec2nii_save_load_qform_code(): +@patch('nibabel.parrec2nii_cmd.verbose') +def test_parrec2nii_save_load_qform_code(*args): # Tests that after parrec2nii saves file, it has the sform and qform 'code' # set to '1', which means 'scanner', so that other software, e.g. FSL picks # up the qform. - imp.reload(parrec2nii) parrec2nii.verbose.switch = False opts = Mock()