Skip to content

RF+API: add qform, sform code 1 for parrec2nii #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nibabel/parrec2nii_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 18 additions & 25 deletions nibabel/tests/test_parrec2nii.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
""" Tests for the parrec2nii exe code
"""
import imp
from os.path import join, isfile, basename

import numpy
Expand All @@ -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)

Expand All @@ -30,27 +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_with_code2():
"""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([]))
Expand All @@ -70,15 +63,15 @@ 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():
"""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.
"""
imp.reload(parrec2nii)
@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.
parrec2nii.verbose.switch = False

opts = Mock()
Expand All @@ -98,6 +91,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)