@@ -206,17 +206,14 @@ def save(self, fileobj):
206
206
207
207
for t in tractogram :
208
208
s = t .streamline .astype (dtype )
209
- f .write (asbytes (np .r_ [s .astype ('<f4' ), self .FIBER_DELIMITER ].tostring ()))
209
+ f .write (asbytes (np .r_ [s .astype ('<f4' ),
210
+ self .FIBER_DELIMITER ].tostring ()))
210
211
nb_streamlines += 1
211
212
212
- # Add the EOF_DELIMITER.
213
- f .write (asbytes (self .EOF_DELIMITER .tostring ()))
214
-
215
213
header [Field .NB_STREAMLINES ] = nb_streamlines
216
214
217
- ## Overwrite the streamlines count in the header.
218
- #self.file.seek(self.count_offset, os.SEEK_SET)
219
- #self.file.write(asbytes("count: {0:010}\n".format(self.header[Field.NB_STREAMLINES])))
215
+ # Add the EOF_DELIMITER.
216
+ f .write (asbytes (self .EOF_DELIMITER .tostring ()))
220
217
221
218
# Overwrite header with updated one.
222
219
f .seek (beginning , os .SEEK_SET )
@@ -233,26 +230,29 @@ def _write_header(fileobj, header):
233
230
from the beginning of the TCK header).
234
231
"""
235
232
# Fields to exclude
236
- exclude = [Field .MAGIC_NUMBER , Field .NB_STREAMLINES , "datatype" , "file" ]
233
+ exclude = [Field .MAGIC_NUMBER , Field .NB_STREAMLINES ,
234
+ "datatype" , "file" ]
237
235
238
236
lines = []
239
237
lines .append (header [Field .MAGIC_NUMBER ])
240
238
lines .append ("count: {0:010}" .format (header [Field .NB_STREAMLINES ]))
241
- lines .append ("datatype: Float32LE" ) # We always use Float32LE to save TCK files.
242
- lines .extend (["{0}: {1}" .format (k , v ) for k , v in header .items () if k not in exclude ])
239
+ lines .append ("datatype: Float32LE" ) # Always Float32LE.
240
+ lines .extend (["{0}: {1}" .format (k , v )
241
+ for k , v in header .items () if k not in exclude ])
243
242
lines .append ("file: . " ) # Manually add this last field.
244
243
out = "\n " .join ((asstr (line ).replace ('\n ' , '\t ' ) for line in lines ))
245
244
fileobj .write (asbytes (out ))
246
245
247
- # Compute offset to the beginning of the binary data
248
- tentative_offset = len (out ) + 5 # +5 is for "\nEND\n" added just before the data.
246
+ # Compute offset to the beginning of the binary data.
247
+ # We add 5 for "\nEND\n" that will be added just before the data.
248
+ tentative_offset = len (out ) + 5
249
249
250
- # Take in account the number of characters needed to write ' offset' in ASCII.
250
+ # Count the number of characters needed to write the offset in ASCII.
251
251
offset = tentative_offset + len (str (tentative_offset ))
252
252
253
- # Corner case: the new ' offset' needs one more character to write it in ASCII
253
+ # The new offset might need one more character to write it in ASCII.
254
254
# e.g. offset = 98 (i.e. 2 char.), so offset += 2 = 100 (i.e. 3 char.)
255
- # thus the final offset = 101.
255
+ # thus the final offset = 101.
256
256
if len (str (tentative_offset )) != len (str (offset )):
257
257
offset += 1 # +1, we need one more character for that new digit.
258
258
0 commit comments