Skip to content

Commit 87c1d5a

Browse files
committed
BF: Was saving unwanted information in a TCK's header.
1 parent 8ca88bd commit 87c1d5a

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

nibabel/streamlines/tck.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,15 @@ def _write_header(fileobj, header):
230230
from the beginning of the TCK header).
231231
"""
232232
# Fields to exclude
233-
exclude = [Field.MAGIC_NUMBER, Field.NB_STREAMLINES,
234-
"datatype", "file"]
233+
exclude = [Field.MAGIC_NUMBER, Field.NB_STREAMLINES, Field.ENDIANNESS,
234+
"count", "datatype", "file"]
235235

236236
lines = []
237237
lines.append(header[Field.MAGIC_NUMBER])
238238
lines.append("count: {0:010}".format(header[Field.NB_STREAMLINES]))
239239
lines.append("datatype: Float32LE") # Always Float32LE.
240240
lines.extend(["{0}: {1}".format(k, v)
241-
for k, v in header.items() if k not in exclude])
241+
for k, v in header.items() if k not in exclude and not k.startswith("_")])
242242
lines.append("file: . ") # Manually add this last field.
243243
out = "\n".join((asstr(line).replace('\n', '\t') for line in lines))
244244
fileobj.write(asbytes(out))
@@ -281,7 +281,7 @@ def _read_header(fileobj):
281281

282282
with Opener(fileobj) as f:
283283
# Read magic number
284-
magic_number = asstr(f.fobj.readline())
284+
magic_number = asstr(f.fobj.readline()).strip()
285285

286286
# Read all key-value pairs contained in the header.
287287
buf = asstr(f.fobj.readline())

nibabel/streamlines/tests/test_tck.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,18 @@ def test_load_write_file(self):
162162
loaded_tck = TckFile.load(fname, lazy_load=False)
163163
assert_tractogram_equal(loaded_tck.tractogram, tck.tractogram)
164164

165+
# Check that the written file is the same as the one read.
166+
tck_file.seek(0, os.SEEK_SET)
167+
assert_equal(tck_file.read(), open(fname, 'rb').read())
168+
165169
# Save tractogram that has an affine_to_rasmm.
166170
for lazy_load in [False, True]:
167171
tck = TckFile.load(DATA['simple_tck_fname'], lazy_load=lazy_load)
168172
affine = np.eye(4)
169173
affine[0, 0] *= -1 # Flip in X
170174
tractogram = Tractogram(tck.streamlines, affine_to_rasmm=affine)
171175

172-
new_tck = TckFile(tractogram)
176+
new_tck = TckFile(tractogram, tck.header)
173177
tck_file = BytesIO()
174178
new_tck.save(tck_file)
175179
tck_file.seek(0, os.SEEK_SET)

0 commit comments

Comments
 (0)