-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniftitools.py
executable file
·70 lines (64 loc) · 2.54 KB
/
niftitools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python3
import argparse
import os
import re
import numpy
import nibabel
def file_is_valid(filename):
if not os.path.isfile(filename):
return False
if re.search("\.nii(?:\.gz)$", filename, flags=re.I) is None:
return False
return True
def orient(nifti, outpath=None, diagonal=False):
if type(nifti) is str:
nifti = nibabel.load(nifti)
nifti = nibabel.as_closest_canonical(nifti, diagonal)
if outpath is not None:
nibabel.save(nifti, outpath)
return nifti
def autowindowing(nifti):
if type(nifti) is str:
nifti = nibabel.load(nifti)
data = nifti.get_data().flatten()
if data.size > 1<<16:
data = numpy.random.choice(data, 1<<16)
hist, bins = numpy.histogram(data, bins=1<<8)
if hist[-2]:
# marks are not separated
minval = bins[0]
maxval = bins[-1]
else:
# marks are separated
i = -3
while not hist[i]:
i -= 1
minval = bins[0]
maxval = bins[i]
maxval = minval + (maxval - minval) * 1.1
if maxval > bins[-1]:
maxval = bins[-1]
return (minval + maxval) / 2, maxval - minval
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="action", help="one of the following actions", metavar="ACTION")
subparsers.required = True
parser_header = subparsers.add_parser("header", description="Output the header of a NIfTI file.", help="output the header of a NIfTI file")
parser_header.add_argument("path", help="path to the NIfTI file", metavar="PATH")
parser_affine = subparsers.add_parser("affine", description="Output the affine of a NIfTI file.", help="output the affine of a NIfTI file")
parser_affine.add_argument("path", help="path to the NIfTI file", metavar="PATH")
parser_orient = subparsers.add_parser("orient", description="Orient a NIfTI file.", help="orient a NIfTI file")
parser_orient.add_argument("inpath", help="path to the input NIfTI file", metavar="INPATH")
parser_orient.add_argument("-o", "--outpath", help="path to the output NIfTI file; default INPATH-oriented")
parser_orient.add_argument("-d", "--diagonal", action="store_true", help="apply orientation only if resulting affine is close to diagonal")
args = parser.parse_args()
if args.action == "header":
nifti = nibabel.load(args.path)
print(nibabel.volumeutils.pretty_mapping(nifti.get_header()))
elif args.action == "affine":
nifti = nibabel.load(args.path)
print(nifti.get_affine())
elif args.action == "orient":
if args.outpath is None:
args.outpath = re.sub("(\.nii(?:\.gz))$", "-oriented\\1", args.inpath, flags=re.I)
orient(args.inpath, outpath=args.outpath, diagonal=args.diagonal)