Skip to content

Commit dd6b5dc

Browse files
authored
Merge pull request #1272 from EmmaRenauld/fix_issue1122
Nicer print_info. Now ~ the same for all data types!
2 parents c93ad87 + 2508d3a commit dd6b5dc

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

src/scilpy/cli/scil_header_print_info.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
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

811
import argparse
912
import logging
1013
import pprint
1114

1215
import nibabel as nib
16+
from nibabel import aff2axcodes
1317

1418
from scilpy.io.utils import assert_inputs_exist, add_verbose_arg
1519
from 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

70103
if __name__ == "__main__":

src/scilpy/cli/tests/test_header_print_info.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ def test_execution_img(script_runner, monkeypatch):
2424
assert ret.success
2525

2626

27+
def test_execution_img_keys(script_runner, monkeypatch):
28+
monkeypatch.chdir(os.path.expanduser(tmp_dir.name))
29+
in_img = os.path.join(SCILPY_HOME, 'others', 'fa.nii.gz')
30+
ret = script_runner.run(['scil_header_print_info', in_img,
31+
'--keys', 'pixdim'])
32+
assert ret.success
33+
34+
35+
def test_execution_img_raw(script_runner, monkeypatch):
36+
monkeypatch.chdir(os.path.expanduser(tmp_dir.name))
37+
in_img = os.path.join(SCILPY_HOME, 'others', 'fa.nii.gz')
38+
ret = script_runner.run(['scil_header_print_info', in_img,
39+
'--raw_header'])
40+
assert ret.success
41+
2742
def test_execution_tractogram(script_runner, monkeypatch):
2843
monkeypatch.chdir(os.path.expanduser(tmp_dir.name))
2944
in_tracto = os.path.join(SCILPY_HOME, 'others', 'IFGWM.trk')

0 commit comments

Comments
 (0)