Skip to content

FIX: Set TRK byte order to little-endian before filling values #782

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 2, 2019
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
4 changes: 2 additions & 2 deletions nibabel/brikhead.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ def __init__(self, info):
--------
>>> fname = os.path.join(datadir, 'example4d+orig.HEAD')
>>> header = AFNIHeader(parse_AFNI_header(fname))
>>> header.get_data_dtype()
dtype('int16')
>>> header.get_data_dtype().str
'<i2'
>>> header.get_zooms()
(3.0, 3.0, 3.0, 3.0)
>>> header.get_data_shape()
Expand Down
16 changes: 10 additions & 6 deletions nibabel/streamlines/trk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from nibabel.openers import Opener
from nibabel.py3k import asstr
from nibabel.volumeutils import (native_code, swapped_code)
from nibabel.volumeutils import (native_code, swapped_code, endian_codes)
from nibabel.orientations import (aff2axcodes, axcodes2ornt)

from .array_sequence import create_arraysequences_from_generator
Expand Down Expand Up @@ -266,10 +266,14 @@ def is_correct_format(cls, fileobj):
return magic_number == cls.MAGIC_NUMBER

@classmethod
def _default_structarr(cls):
def _default_structarr(cls, endianness=None):
""" Return an empty compliant TRK header as numpy structured array
"""
st_arr = np.zeros((), dtype=header_2_dtype)
dt = header_2_dtype
if endianness is not None:
endianness = endian_codes[endianness]
dt = dt.newbyteorder(endianness)
st_arr = np.zeros((), dtype=dt)

# Default values
st_arr[Field.MAGIC_NUMBER] = cls.MAGIC_NUMBER
Expand All @@ -283,10 +287,10 @@ def _default_structarr(cls):
return st_arr

@classmethod
def create_empty_header(cls):
def create_empty_header(cls, endianness=None):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in passing, since it's the only other place _default_structarr is being called.

""" Return an empty compliant TRK header as dict
"""
st_arr = cls._default_structarr()
st_arr = cls._default_structarr(endianness)
return dict(zip(st_arr.dtype.names, st_arr.tolist()))

@classmethod
Expand Down Expand Up @@ -396,7 +400,7 @@ def save(self, fileobj):
of the TRK header data).
"""
# Enforce little-endian byte order for header
header = self._default_structarr().newbyteorder('<')
header = self._default_structarr(endianness='little')

# Override hdr's fields by those contained in `header`.
for k, v in self.header.items():
Expand Down