8
8
import warnings
9
9
10
10
import numpy as np
11
- from numpy .compat .py3k import asbytes , asstr
12
11
13
12
from nibabel .openers import Opener
14
13
@@ -44,7 +43,7 @@ class TckFile(TractogramFile):
44
43
.. [#] http://nipy.org/nibabel/coordinate_systems.html#voxel-coordinates-are-in-voxel-space
45
44
"""
46
45
# Constants
47
- MAGIC_NUMBER = "mrtrix tracks"
46
+ MAGIC_NUMBER = b "mrtrix tracks"
48
47
SUPPORTS_DATA_PER_POINT = False # Not yet
49
48
SUPPORTS_DATA_PER_STREAMLINE = False # Not yet
50
49
@@ -94,7 +93,7 @@ def is_correct_format(cls, fileobj):
94
93
magic_number = f .read (len (cls .MAGIC_NUMBER ))
95
94
f .seek (- len (cls .MAGIC_NUMBER ), os .SEEK_CUR )
96
95
97
- return asstr ( magic_number ) == cls .MAGIC_NUMBER
96
+ return magic_number == cls .MAGIC_NUMBER
98
97
99
98
@classmethod
100
99
def create_empty_header (cls ):
@@ -230,7 +229,7 @@ def save(self, fileobj):
230
229
header [Field .NB_STREAMLINES ] = nb_streamlines
231
230
232
231
# Add the EOF_DELIMITER.
233
- f .write (asbytes ( self .EOF_DELIMITER .tobytes () ))
232
+ f .write (self .EOF_DELIMITER .tobytes ())
234
233
self ._finalize_header (f , header , offset = beginning )
235
234
236
235
@staticmethod
@@ -251,41 +250,36 @@ def _write_header(fileobj, header):
251
250
"count" , "datatype" , "file" ] # Fields being replaced.
252
251
253
252
lines = []
254
- lines .append (asstr (header [Field .MAGIC_NUMBER ]))
255
253
lines .append (f"count: { header [Field .NB_STREAMLINES ]:010} " )
256
254
lines .append ("datatype: Float32LE" ) # Always Float32LE.
257
255
lines .extend ([f"{ k } : { v } "
258
256
for k , v in header .items ()
259
257
if k not in exclude and not k .startswith ("_" )])
260
- lines .append ("file: . " ) # Manually add this last field.
261
258
out = "\n " .join (lines )
262
259
263
260
# Check the header is well formatted.
264
261
if out .count ("\n " ) > len (lines ) - 1 : # \n only allowed between lines.
265
262
msg = f"Key-value pairs cannot contain '\\ n':\n { out } "
266
263
raise HeaderError (msg )
267
264
268
- if out .count (":" ) > len (lines ) - 1 :
265
+ if out .count (":" ) > len (lines ):
269
266
# : only one per line (except the last one which contains END).
270
267
msg = f"Key-value pairs cannot contain ':':\n { out } "
271
268
raise HeaderError (msg )
272
269
270
+ out = header [Field .MAGIC_NUMBER ] + b"\n " + out .encode ('utf-8' )
271
+
272
+ # Compute data offset considering the offset string representation
273
+ # headers + "file" header + END + \n's
274
+ hdr_offset = len (out ) + 8 + 3 + 3
275
+ offset_repr = f'{ hdr_offset } '
276
+
277
+ # Adding the offset may increase one char to the offset repr
278
+ hdr_offset += len (f'{ hdr_offset + len (offset_repr )} ' )
279
+
273
280
# Write header to file.
274
- fileobj .write (asbytes (out ))
275
-
276
- hdr_len_no_offset = len (out ) + 5
277
- # Need to add number of bytes to store offset as decimal string. We
278
- # start with estimate without string, then update if the
279
- # offset-as-decimal-string got longer after adding length of the
280
- # offset string.
281
- new_offset = - 1
282
- old_offset = hdr_len_no_offset
283
- while new_offset != old_offset :
284
- old_offset = new_offset
285
- new_offset = hdr_len_no_offset + len (str (old_offset ))
286
-
287
- fileobj .write (asbytes (str (new_offset ) + "\n " ))
288
- fileobj .write (asbytes ("END\n " ))
281
+ fileobj .write (out )
282
+ fileobj .write (f'\n file: . { hdr_offset } \n END\n ' .encode ('utf-8' ))
289
283
290
284
@classmethod
291
285
def _read_header (cls , fileobj ):
@@ -320,7 +314,7 @@ def _read_header(cls, fileobj):
320
314
# Read magic number
321
315
magic_number = f .read (len (cls .MAGIC_NUMBER ))
322
316
323
- if asstr ( magic_number ) != cls .MAGIC_NUMBER :
317
+ if magic_number != cls .MAGIC_NUMBER :
324
318
raise HeaderError (f"Invalid magic number: { magic_number } " )
325
319
326
320
hdr [Field .MAGIC_NUMBER ] = magic_number
@@ -331,7 +325,7 @@ def _read_header(cls, fileobj):
331
325
332
326
# Read all key-value pairs contained in the header, stop at EOF
333
327
for n_line , line in enumerate (f , 1 ):
334
- line = asstr ( line ).strip ()
328
+ line = line . decode ( 'utf-8' ).strip ()
335
329
336
330
if not line : # Skip empty lines
337
331
continue
0 commit comments