11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33"""
4- Print the raw header from the provided file or only the specified keys.
5- Support trk, nii and mgz files.
4+ Prints the raw header from the provided file or only the specified keys.
5+ Supports trk, nii and mgz files.
6+
7+ By default, a simple summary view is shown. Use --all for a more complete
8+ display.
69"""
710
811import argparse
912import logging
1013import pprint
1114
1215import nibabel as nib
16+ from nibabel import aff2axcodes
1317
1418from scilpy .io .utils import assert_inputs_exist , add_verbose_arg
1519from scilpy .utils .filenames import split_name_with_nii
@@ -23,10 +27,11 @@ def _build_arg_parser():
2327
2428 p .add_argument ('in_file' ,
2529 help = 'Input file (trk, nii and mgz).' )
26- p .add_argument ('--keys' , nargs = '+' ,
27- help = 'Print only the specified keys.' )
28- p .add_argument ('--print_affine' , action = 'store_true' ,
29- help = "Print nibabel's affine." )
30+ g = p .add_mutually_exclusive_group ()
31+ g .add_argument ('--raw_header' , action = 'store_true' ,
32+ help = "Print the whole header as received by nibabel." )
33+ g .add_argument ('--keys' , nargs = '+' ,
34+ help = "Print only the specified keys from nibabel's header" )
3035
3136 add_verbose_arg (p )
3237
@@ -55,16 +60,44 @@ def main():
5560 parser .error ('Key {} is not in the header of {}.' .format (key ,
5661 args .in_file ))
5762 print (" '{}': {}" .format (key , header [key ]))
58- else :
63+ elif args . raw_header :
5964 pp = pprint .PrettyPrinter (indent = 1 )
6065 pp .pprint (header )
61-
62- if args .print_affine :
63- if in_extension in ['.tck' , '.trk' ]:
66+ else :
67+ # mrinfo-type of print.
68+
69+ # Getting info:
70+ if in_extension in ['.nii' , '.nii.gz' , '.mgz' ]:
71+ img = nib .load (args .in_file ) # Data type and affine require img
72+ affine = img .affine
73+ dtype = img .get_data_dtype ().type
74+ if in_extension in ['.nii' , '.nii.gz' ]:
75+ nb_dims = header ['dim' ][0 ]
76+ dims = header ['dim' ][1 :1 + nb_dims ]
77+ vox_size = header ['dim' ][5 :5 + nb_dims ]
78+ elif in_extension == '.mgz' :
79+ dims = header ['dims' ]
80+ vox_size = header ['delta' ]
81+ else :
6482 affine = nib .streamlines .load (args .in_file , lazy_load = True ).affine
65- else : # in_extension in ['.nii', '.nii.gz', '.mgz']:
66- affine = nib .load (args .in_file ).affine
67- print (" '{}': {}" .format ('affine' , affine ))
83+
84+ # 'dimensions' and 'voxel_sizes' exist for both tck and trk
85+ dims = header ['dimensions' ]
86+ vox_size = header ['voxel_sizes' ]
87+
88+ if in_extension == '.trk' :
89+ dtype = 'float32' # always float32 for trk
90+ else :
91+ dtype = header ['datatype' ]
92+
93+ print ("**************************************" )
94+ print ("File name: {}" .format (args .in_file ))
95+ print ("**************************************" )
96+ print (" Dimensions: {}" .format (dims ))
97+ print (" Voxel size: {}" .format (vox_size ))
98+ print (" Datatype: {}" .format (dtype ))
99+ print (" Orientation: {}" .format (aff2axcodes (affine )))
100+ print (" Afine (vox2rasmm):\n {}" .format (affine ))
68101
69102
70103if __name__ == "__main__" :
0 commit comments