@@ -100,8 +100,8 @@ def get_affine_trackvis_to_rasmm(header):
100100 affine = np .dot (scale , affine )
101101
102102 # TrackVis considers coordinate (0,0,0) to be the corner of the
103- # voxel whereas streamlines returned assume (0,0,0) to be the
104- # center of the voxel. Thus, streamlines are shifted of half a voxel.
103+ # voxel whereas streamlines returned assumes (0,0,0) to be the
104+ # center of the voxel. Thus, streamlines are shifted by half a voxel.
105105 offset = np .eye (4 )
106106 offset [:- 1 , - 1 ] -= 0.5
107107 affine = np .dot (offset , affine )
@@ -132,21 +132,24 @@ def encode_value_in_name(value, name, max_name_len=20):
132132 """ Encodes a value in the last two bytes of a string.
133133
134134 If `value` is one, then there is no encoding and the last two bytes
135- are left untouched. This function also verify that the length of name is
136- less than `max_name_len`.
135+ are left untouched. Otherwise, the byte before the last will be
136+ set to \x00 and the last byte will correspond to the value.
137+
138+ This function also verifies that the length of name is less
139+ than `max_name_len`.
137140
138141 Parameters
139142 ----------
140- value : int
141- Integer value to encode.
142- name : str
143+ value : byte
144+ Integer value between 0 and 255 to encode.
145+ name : bytes
143146 Name in which the last two bytes will serve to encode `value`.
144147 max_name_len : int, optional
145148 Maximum length name can have.
146149
147150 Returns
148151 -------
149- encoded_name : str
152+ encoded_name : bytes
150153 Name containing the encoded value.
151154 """
152155
@@ -161,9 +164,10 @@ def encode_value_in_name(value, name, max_name_len=20):
161164 ).format (name , max_name_len - 2 )
162165 raise ValueError (msg )
163166
167+ name = name .ljust (max_name_len , '\x00 ' )
164168 if value > 1 :
165169 # Use the last two bytes of `name` to store `value`.
166- name = (asbytes (name [:18 ]. ljust ( 18 , ' \x00 ' ) ) + b'\x00 ' +
170+ name = (asbytes (name [:max_name_len - 2 ] ) + b'\x00 ' +
167171 np .array (value , dtype = np .int8 ).tostring ())
168172
169173 return name
@@ -183,10 +187,10 @@ class TrkReader(object):
183187 ----
184188 TrackVis (so its file format: TRK) considers the streamline coordinate
185189 (0,0,0) to be in the corner of the voxel whereas NiBabel's streamlines
186- internal representation (Voxel space) assume (0,0,0) to be in the
190+ internal representation (Voxel space) assumes (0,0,0) to be in the
187191 center of the voxel.
188192
189- Thus, streamlines are shifted of half a voxel on load and are shifted
193+ Thus, streamlines are shifted by half a voxel on load and are shifted
190194 back on save.
191195 """
192196 def __init__ (self , fileobj ):
@@ -461,10 +465,10 @@ class TrkFile(TractogramFile):
461465 ----
462466 TrackVis (so its file format: TRK) considers the streamline coordinate
463467 (0,0,0) to be in the corner of the voxel whereas NiBabel's streamlines
464- internal representation (Voxel space) assume (0,0,0) to be in the
468+ internal representation (Voxel space) assumes (0,0,0) to be in the
465469 center of the voxel.
466470
467- Thus, streamlines are shifted of half a voxel on load and are shifted
471+ Thus, streamlines are shifted by half a voxel on load and are shifted
468472 back on save.
469473 """
470474
@@ -515,8 +519,9 @@ def is_correct_format(cls, fileobj):
515519 otherwise returns False.
516520 """
517521 with Opener (fileobj ) as f :
518- magic_number = f .read (5 )
519- f .seek (- 5 , os .SEEK_CUR )
522+ magic_len = len (cls .MAGIC_NUMBER )
523+ magic_number = f .read (magic_len )
524+ f .seek (- magic_len , os .SEEK_CUR )
520525 return magic_number == cls .MAGIC_NUMBER
521526
522527 @classmethod
0 commit comments