Skip to content

Commit a91cb47

Browse files
matthew-brettMarcCote
authored andcommitted
RF: refactor trk file object str
Refactor using big format string and compiled dictionary of variables.
1 parent ebe702c commit a91cb47

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

nibabel/streamlines/trk.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import struct
88
import warnings
99
import itertools
10+
import string
1011

1112
import numpy as np
1213
import nibabel as nib
@@ -781,34 +782,34 @@ def __str__(self):
781782
info : string
782783
Header information relevant to the TRK format.
783784
"""
784-
hdr = self.header
785-
786-
info = ""
787-
info += "\nMAGIC NUMBER: {0}".format(hdr[Field.MAGIC_NUMBER])
788-
info += "\nv.{0}".format(hdr['version'])
789-
info += "\ndim: {0}".format(hdr[Field.DIMENSIONS])
790-
info += "\nvoxel_sizes: {0}".format(hdr[Field.VOXEL_SIZES])
791-
info += "\norgin: {0}".format(hdr[Field.ORIGIN])
792-
info += "\nnb_scalars: {0}".format(hdr[Field.NB_SCALARS_PER_POINT])
793-
info += "\nscalar_name:\n {0}".format(
794-
"\n".join(map(asstr, hdr['scalar_name'])))
795-
info += "\nnb_properties: {0}".format(
796-
hdr[Field.NB_PROPERTIES_PER_STREAMLINE])
797-
info += "\nproperty_name:\n {0}".format(
798-
"\n".join(map(asstr, hdr['property_name'])))
799-
info += "\nvox_to_world: {0}".format(hdr[Field.VOXEL_TO_RASMM])
800-
info += "\nvoxel_order: {0}".format(hdr[Field.VOXEL_ORDER])
801-
info += "\nimage_orientation_patient: {0}".format(
802-
hdr['image_orientation_patient'])
803-
info += "\npad1: {0}".format(hdr['pad1'])
804-
info += "\npad2: {0}".format(hdr['pad2'])
805-
info += "\ninvert_x: {0}".format(hdr['invert_x'])
806-
info += "\ninvert_y: {0}".format(hdr['invert_y'])
807-
info += "\ninvert_z: {0}".format(hdr['invert_z'])
808-
info += "\nswap_xy: {0}".format(hdr['swap_xy'])
809-
info += "\nswap_yz: {0}".format(hdr['swap_yz'])
810-
info += "\nswap_zx: {0}".format(hdr['swap_zx'])
811-
info += "\nn_count: {0}".format(hdr[Field.NB_STREAMLINES])
812-
info += "\nhdr_size: {0}".format(hdr['hdr_size'])
813-
814-
return info
785+
vars = self.header.copy()
786+
for attr in dir(Field):
787+
if attr[0] in string.ascii_uppercase:
788+
hdr_field = getattr(Field, attr)
789+
if hdr_field in vars:
790+
vars[attr] = vars[hdr_field]
791+
vars['scalar_names'] = '\n'.join(map(asstr, vars['scalar_name']))
792+
vars['property_names'] = "\n".join(map(asstr, vars['property_name']))
793+
return """\
794+
MAGIC NUMBER: {MAGIC_NUMBER}
795+
v.{version}
796+
dim: {DIMENSIONS}
797+
voxel_sizes: {VOXEL_SIZES}
798+
orgin: {ORIGIN}
799+
nb_scalars: {NB_SCALARS_PER_POINT}
800+
scalar_name:\n {scalar_names}
801+
nb_properties: {NB_PROPERTIES_PER_STREAMLINE}
802+
property_name:\n {property_names}
803+
vox_to_world: {VOXEL_TO_RASMM}
804+
voxel_order: {VOXEL_ORDER}
805+
image_orientation_patient: {image_orientation_patient}
806+
pad1: {pad1}
807+
pad2: {pad2}
808+
invert_x: {invert_x}
809+
invert_y: {invert_y}
810+
invert_z: {invert_z}
811+
swap_xy: {swap_xy}
812+
swap_yz: {swap_yz}
813+
swap_zx: {swap_zx}
814+
n_count: {NB_STREAMLINES}
815+
hdr_size: {hdr_size}""".format(**vars)

0 commit comments

Comments
 (0)