Skip to content

MRG: fix lower-case TRK codes, add error message #600

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
Feb 20, 2018
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
9 changes: 7 additions & 2 deletions nibabel/orientations.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,13 @@ def axcodes2ornt(axcodes, labels=None):
[ 0., -1.],
[ 2., 1.]])
"""
if labels is None:
labels = list(zip('LPI', 'RAS'))
labels = list(zip('LPI', 'RAS')) if labels is None else labels
allowed_labels = sum([list(L) for L in labels], []) + [None]
if len(allowed_labels) != len(set(allowed_labels)):
raise ValueError('Duplicate labels in {}'.format(allowed_labels))
if not set(axcodes).issubset(allowed_labels):
raise ValueError('Not all axis codes {} in label set {}'
.format(list(axcodes), allowed_labels))
n_axes = len(axcodes)
ornt = np.ones((n_axes, 2), dtype=np.int8) * np.nan
for code_idx, code in enumerate(axcodes):
Expand Down
13 changes: 12 additions & 1 deletion nibabel/streamlines/tests/test_trk.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..tractogram_file import HeaderError, HeaderWarning

from .. import trk as trk_module
from ..trk import TrkFile, encode_value_in_name, decode_value_from_name
from ..trk import TrkFile, encode_value_in_name, decode_value_from_name, get_affine_trackvis_to_rasmm
from ..header import Field

DATA = {}
Expand Down Expand Up @@ -110,6 +110,17 @@ def trk_with_bytes(self, trk_key='simple_trk_fname', endian='<'):
return trk_struct, trk_bytes

def test_load_file_with_wrong_information(self):
# Simulate a TRK file where `voxel_order` is lowercase.
trk_struct1, trk_bytes1 = self.trk_with_bytes()
trk_struct1[Field.VOXEL_ORDER] = b'LAS'
trk1 = TrkFile.load(BytesIO(trk_bytes1))
trk_struct2, trk_bytes2 = self.trk_with_bytes()
trk_struct2[Field.VOXEL_ORDER] = b'las'
trk2 = TrkFile.load(BytesIO(trk_bytes2))
trk1_aff2rasmm = get_affine_trackvis_to_rasmm(trk1.header)
trk2_aff2rasmm = get_affine_trackvis_to_rasmm(trk2.header)
assert_array_equal(trk1_aff2rasmm,trk2_aff2rasmm)

# Simulate a TRK file where `count` was not provided.
trk_struct, trk_bytes = self.trk_with_bytes()
trk_struct[Field.NB_STREAMLINES] = 0
Expand Down
2 changes: 1 addition & 1 deletion nibabel/streamlines/trk.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get_affine_trackvis_to_rasmm(header):
if hasattr(vox_order, 'item'): # structured array
vox_order = header[Field.VOXEL_ORDER].item()
affine_ornt = "".join(aff2axcodes(header[Field.VOXEL_TO_RASMM]))
header_ornt = axcodes2ornt(vox_order.decode('latin1'))
header_ornt = axcodes2ornt(vox_order.decode('latin1').upper())
affine_ornt = axcodes2ornt(affine_ornt)
ornt = nib.orientations.ornt_transform(header_ornt, affine_ornt)
M = nib.orientations.inv_ornt_aff(ornt, header[Field.DIMENSIONS])
Expand Down
29 changes: 24 additions & 5 deletions nibabel/tests/test_orientations.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,8 @@ def test_axcodes2ornt():
)

# default is RAS output directions
assert_array_equal(axcodes2ornt(('R', 'A', 'S')),
[[0, 1],
[1, 1],
[2, 1]]
)
default = np.c_[range(3), [1] * 3]
assert_array_equal(axcodes2ornt(('R', 'A', 'S')), default)

# dropped axes produce None
assert_array_equal(axcodes2ornt(('R', None, 'S')),
Expand All @@ -313,6 +310,28 @@ def test_axcodes2ornt():
[2, 1]]
)

# Missing axcodes raise an error
assert_array_equal(axcodes2ornt('RAS'), default)
assert_raises(ValueError, axcodes2ornt, 'rAS')
# None is OK as axis code
assert_array_equal(axcodes2ornt(('R', None, 'S')),
[[0, 1],
[np.nan, np.nan],
[2, 1]])
# Bad axis code with None also raises error.
assert_raises(ValueError, axcodes2ornt, ('R', None, 's'))
# Axis codes checked with custom labels
labels = ('SD', 'BF', 'lh')
assert_array_equal(axcodes2ornt('BlD', labels),
[[1, -1],
[2, -1],
[0, 1]])
assert_raises(ValueError, axcodes2ornt, 'blD', labels)

# Duplicate labels
assert_raises(ValueError, axcodes2ornt, 'blD', ('SD', 'BF', 'lD'))
assert_raises(ValueError, axcodes2ornt, 'blD', ('SD', 'SF', 'lD'))


def test_aff2axcodes():
assert_equal(aff2axcodes(np.eye(4)), tuple('RAS'))
Expand Down