From 419998a851f77df4aecb5f914c3576dc4acdbb81 Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Tue, 9 Jan 2018 18:29:17 -0800 Subject: [PATCH 1/8] forced voxel order to be upper case when read from the header in .trk file --- nibabel/streamlines/trk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nibabel/streamlines/trk.py b/nibabel/streamlines/trk.py index f525451865..af794bb82e 100644 --- a/nibabel/streamlines/trk.py +++ b/nibabel/streamlines/trk.py @@ -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]) From 47405452a14977d8af7243cdf77b74a6dbe57862 Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Thu, 11 Jan 2018 12:18:31 -0800 Subject: [PATCH 2/8] added test for loading .trk with lowercase voxel order in hdr --- nibabel/streamlines/tests/test_trk.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/nibabel/streamlines/tests/test_trk.py b/nibabel/streamlines/tests/test_trk.py index 1d3463926d..c4c10bbcbd 100644 --- a/nibabel/streamlines/tests/test_trk.py +++ b/nibabel/streamlines/tests/test_trk.py @@ -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 = {} @@ -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 From c3d86a54b37b7f18977eea5b07f3c119719b0b39 Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Tue, 16 Jan 2018 12:59:51 -0800 Subject: [PATCH 3/8] moved the .upper() to axcodes2ornt so that lowercase voxel orientations are supported --- nibabel/orientations.py | 1 + nibabel/streamlines/trk.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/nibabel/orientations.py b/nibabel/orientations.py index bc85173c1c..3a0fc7fbff 100644 --- a/nibabel/orientations.py +++ b/nibabel/orientations.py @@ -349,6 +349,7 @@ def axcodes2ornt(axcodes, labels=None): [ 0., -1.], [ 2., 1.]]) """ + axcodes = axcodes.upper() if labels is None: labels = list(zip('LPI', 'RAS')) n_axes = len(axcodes) diff --git a/nibabel/streamlines/trk.py b/nibabel/streamlines/trk.py index af794bb82e..f525451865 100644 --- a/nibabel/streamlines/trk.py +++ b/nibabel/streamlines/trk.py @@ -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').upper()) + header_ornt = axcodes2ornt(vox_order.decode('latin1')) affine_ornt = axcodes2ornt(affine_ornt) ornt = nib.orientations.ornt_transform(header_ornt, affine_ornt) M = nib.orientations.inv_ornt_aff(ornt, header[Field.DIMENSIONS]) From 1bb0752fea9a8ff46d952b49dc77259fbc5c3bb5 Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Tue, 23 Jan 2018 08:45:02 -0800 Subject: [PATCH 4/8] changed upper case modification to support tuples, not just strings in axcodes2ornt --- nibabel/orientations.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/nibabel/orientations.py b/nibabel/orientations.py index 3a0fc7fbff..c1d961f30f 100644 --- a/nibabel/orientations.py +++ b/nibabel/orientations.py @@ -349,7 +349,12 @@ def axcodes2ornt(axcodes, labels=None): [ 0., -1.], [ 2., 1.]]) """ - axcodes = axcodes.upper() + + upper_str = [] + for c in axcodes: + upper_str.append(c.upper()) + axcodes = tuple(upper_str) + if labels is None: labels = list(zip('LPI', 'RAS')) n_axes = len(axcodes) From 63cee0a2bd007459f4e793993b57249c6d58d315 Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Tue, 23 Jan 2018 09:42:05 -0800 Subject: [PATCH 5/8] loop to comprehension --- nibabel/orientations.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nibabel/orientations.py b/nibabel/orientations.py index c1d961f30f..b9c6bcbc2d 100644 --- a/nibabel/orientations.py +++ b/nibabel/orientations.py @@ -350,10 +350,7 @@ def axcodes2ornt(axcodes, labels=None): [ 2., 1.]]) """ - upper_str = [] - for c in axcodes: - upper_str.append(c.upper()) - axcodes = tuple(upper_str) + axcodes = tuple(c.upper() for c in axcodes) if labels is None: labels = list(zip('LPI', 'RAS')) From 55713a0dbb83d9d55b16cc518a4ceb4322d79c16 Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Mon, 29 Jan 2018 17:04:24 -0800 Subject: [PATCH 6/8] added test for tuple/string and upper/lowercase axcodes --- nibabel/tests/test_orientations.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nibabel/tests/test_orientations.py b/nibabel/tests/test_orientations.py index c003193364..8037e3f13a 100644 --- a/nibabel/tests/test_orientations.py +++ b/nibabel/tests/test_orientations.py @@ -313,6 +313,11 @@ def test_axcodes2ornt(): [2, 1]] ) + # test that format of axcode works for tuples and upper/lower case + lia_ornt = [[0, -1], [2, -1], [1, 1]] + for lia_axcodes in ('LIA', 'lia', ('L', 'I', 'A'), ('l', 'i', 'a')): + assert_array_equal(axcodes2ornt(lia_axcodes), lia_ornt) + def test_aff2axcodes(): assert_equal(aff2axcodes(np.eye(4)), tuple('RAS')) From 540983d44f10ef596a039adb717fda1b39ad0c89 Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Mon, 29 Jan 2018 17:08:41 -0800 Subject: [PATCH 7/8] fix issue: breaks when labels is not None. --- nibabel/orientations.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nibabel/orientations.py b/nibabel/orientations.py index b9c6bcbc2d..aa40e84f21 100644 --- a/nibabel/orientations.py +++ b/nibabel/orientations.py @@ -350,9 +350,8 @@ def axcodes2ornt(axcodes, labels=None): [ 2., 1.]]) """ - axcodes = tuple(c.upper() for c in axcodes) - if labels is None: + axcodes = tuple(c.upper() for c in axcodes) labels = list(zip('LPI', 'RAS')) n_axes = len(axcodes) ornt = np.ones((n_axes, 2), dtype=np.int8) * np.nan From 9a74880bb120392e46676fbc990fd5121bb5d15c Mon Sep 17 00:00:00 2001 From: kesshijordan Date: Mon, 29 Jan 2018 17:57:35 -0800 Subject: [PATCH 8/8] removed empty line; fixed condition c is None --- nibabel/orientations.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nibabel/orientations.py b/nibabel/orientations.py index aa40e84f21..5510de4bf8 100644 --- a/nibabel/orientations.py +++ b/nibabel/orientations.py @@ -349,9 +349,8 @@ def axcodes2ornt(axcodes, labels=None): [ 0., -1.], [ 2., 1.]]) """ - if labels is None: - axcodes = tuple(c.upper() for c in axcodes) + axcodes = tuple(c.upper() if c is not None else None for c in axcodes) labels = list(zip('LPI', 'RAS')) n_axes = len(axcodes) ornt = np.ones((n_axes, 2), dtype=np.int8) * np.nan