Skip to content

Commit aae6dee

Browse files
committed
BF: endian fixes for streamlines code
Getting errors reading and writing on big-endian machines: http://nipy.bic.berkeley.edu/builders/nibabel-py2.7-ppc/builds/295/steps/shell_5/logs/stdio Closes #471
1 parent a4724b7 commit aae6dee

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

nibabel/streamlines/trk.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ def decode_value_from_name(encoded_name):
227227

228228
def create_empty_header():
229229
""" Return an empty compliant TRK header. """
230-
header = np.zeros(1, dtype=header_2_dtype)
230+
# Enforce little-endian header
231+
header = np.zeros(1, dtype=header_2_dtype).newbyteorder('<')
231232

232233
# Default values
233234
header[Field.MAGIC_NUMBER] = TrkFile.MAGIC_NUMBER
@@ -499,20 +500,20 @@ def save(self, fileobj):
499500
for d in t.data_for_points.values())):
500501
raise DataError("Missing scalars for some points!")
501502

502-
points = np.asarray(t.streamline, dtype=f4_dtype)
503-
scalars = [np.asarray(t.data_for_points[k], dtype=f4_dtype)
503+
points = np.asarray(t.streamline)
504+
scalars = [np.asarray(t.data_for_points[k])
504505
for k in data_for_points_keys]
505-
scalars = np.concatenate([np.ndarray((len(points), 0),
506-
dtype=f4_dtype)
506+
scalars = np.concatenate([np.ndarray((len(points), 0),)
507507
] + scalars, axis=1)
508-
properties = [np.asarray(t.data_for_streamline[k],
509-
dtype=f4_dtype)
508+
properties = [np.asarray(t.data_for_streamline[k])
510509
for k in data_for_streamline_keys]
511-
properties = np.concatenate([np.array([], dtype=f4_dtype)
512-
] + properties)
510+
properties = np.concatenate(
511+
[np.array([])] + properties).astype(f4_dtype)
513512

514513
data = struct.pack(i4_dtype.str[:-1], len(points))
515-
data += np.concatenate([points, scalars], axis=1).tostring()
514+
pts_scalars = np.concatenate(
515+
[points, scalars], axis=1).astype(f4_dtype)
516+
data += pts_scalars.tostring()
516517
data += properties.tostring()
517518
f.write(data)
518519

0 commit comments

Comments
 (0)