-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiba_gen.py
More file actions
executable file
·160 lines (127 loc) · 5.54 KB
/
Copy pathmiba_gen.py
File metadata and controls
executable file
·160 lines (127 loc) · 5.54 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import sys
import argparse
import nibabel as nb
import numpy as np
def parse_args():
parser = argparse.ArgumentParser(description='Generate a MIBA by averaging over DVRs')
parser.add_argument('-i', '--in-files', nargs='+', help='The list of files to average over', required=True)
parser.add_argument('-o', '--out-file', default=None, help='The output file')
op = parser.add_mutually_exclusive_group()
op.add_argument('--mean', action='store_const', dest='op', const='mean', default='mean',
help='Take the mean of the files in --in-files (default)')
op.add_argument('--std', action='store_const', dest='op', const='std',
help='Take the standard deviation of the files in --in-files')
op.add_argument('--diff', action='store_const', dest='op', const='diff',
help='Take the difference (all files after the first in --in-files is subtracted from the first)')
op.add_argument('--diff-const', action='store_const', dest='op', const='diff-const',
help='The value in --const-val is subtracted from the first file in --in-files (other in-files are ignored)')
op.add_argument('--div-const', action='store_const', dest='op', const='div-const',
help='The first file in --in-files is divided by the value in --const-val (other in-files are ignored)')
parser.add_argument('-cv', '--const-val', default=None, type=float, help='Value for --diff-const or --div-const')
return parser.parse_args()
def mean(in_files, out_file):
num_files = len(in_files)
if out_file is None:
out_file="out.nii.gz"
print("num_files: ", num_files)
print("out_file: ", out_file)
cumulative_data = None
for file in in_files:
print("loading", file)
vol = nb.load(file)
if cumulative_data is None:
cumulative_data = np.array(vol.dataobj)
else:
cumulative_data = cumulative_data + np.array(vol.dataobj)
mean_data = cumulative_data / num_files
mean_vol = nb.nifti1.Nifti1Image(mean_data, None, header=vol.header.copy())
print("writing", out_file)
nb.save(mean_vol, out_file)
def std(in_files, out_file):
num_files = len(in_files)
if out_file is None:
out_file="out.nii.gz"
print("num_files: ", num_files)
print("out_file: ", out_file)
cumulative_data = None
for file in in_files:
print("loading", file)
vol = nb.load(file)
if cumulative_data is None:
cumulative_data = np.array(vol.dataobj)
else:
cumulative_data = cumulative_data + np.array(vol.dataobj)
mean_data = cumulative_data / num_files
cumulative_data = None
for file in in_files:
print("loading", file)
vol = nb.load(file)
if cumulative_data is None:
cumulative_data = (np.array(vol.dataobj) - mean_data)**2
else:
cumulative_data = cumulative_data + (np.array(vol.dataobj) - mean_data)**2
std_data = np.sqrt(cumulative_data / num_files)
std_vol = nb.nifti1.Nifti1Image(std_data, None, header=vol.header.copy())
print("writing", out_file)
nb.save(std_vol, out_file)
def diff(in_files, out_file):
num_files = len(in_files)
if out_file is None:
out_file="out.nii.gz"
print("num_files: ", num_files)
print("out_file: ", out_file)
cumulative_data = None
for file in in_files:
print("loading", file)
vol = nb.load(file)
if cumulative_data is None:
cumulative_data = np.array(vol.dataobj)
else:
cumulative_data = cumulative_data - np.array(vol.dataobj)
diff_vol = nb.nifti1.Nifti1Image(cumulative_data, None, header=vol.header.copy())
print("writing", out_file)
nb.save(diff_vol, out_file)
def diff_const(in_files, out_file, const_val):
num_files = len(in_files)
if num_files > 1:
print('More than one input file specified; all but the first will be ignored')
if out_file is None:
out_file="out.nii.gz"
print("num_files: ", num_files)
print("out_file: ", out_file)
print("const_val: ", const_val)
vol = nb.load(in_files[0])
diff_data = np.array(vol.dataobj) - const_val
diff_vol = nb.nifti1.Nifti1Image(diff_data, None, header=vol.header.copy())
print("writing", out_file)
nb.save(diff_vol, out_file)
def div_const(in_files, out_file, const_val):
num_files = len(in_files)
if num_files > 1:
print('More than one input file specified; all but the first will be ignored')
if out_file is None:
out_file="out.nii.gz"
print("num_files: ", num_files)
print("out_file: ", out_file)
print("const_val: ", const_val)
vol = nb.load(in_files[0])
div_data = np.array(vol.dataobj) / const_val
div_vol = nb.nifti1.Nifti1Image(div_data, None, header=vol.header.copy())
print("writing", out_file)
nb.save(div_vol, out_file)
def main():
args = parse_args()
if (args.op=='mean'):
mean(args.in_files, args.out_file)
if (args.op=='std'):
std(args.in_files, args.out_file)
elif (args.op=='diff'):
diff(args.in_files, args.out_file)
elif (args.op=='diff-const'):
diff_const(args.in_files, args.out_file, args.const_val)
elif (args.op=='div-const'):
div_const(args.in_files, args.out_file, args.const_val)
return 0
if __name__ == "__main__":
sys.exit(main())