-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
268 lines (216 loc) · 8.44 KB
/
tools.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
import sys
import os
import inspect
import numpy as np
import nibabel as nib
import scipy.ndimage as sn
import scipy.ndimage.interpolation as sni
import pickle
import pdb
import csv
def upsample(niiFileName, upsampledFile, zoom_values_file='upsampling_log.pickle', isotrop_res=True, upsample_factor=None, polynomial='3'):
"""
Upsample a nifti and save the upsampled image.
The upsampling procedure has been implemented in a way that it is easily revertable.
Example arguments:
niiFileName = '10529_t1.nii.gz'
upsampled_file = '10529_t1_upsampled.nii.gz'
"""
# load the nifti
nii = nib.load(niiFileName)
header = nii.header
affine = nii.affine
# make data type of image to float
out_dtype = np.float32
header['datatype'] = 16 # corresponds to float32
header['bitpix'] = 32 # corresponds to float32
# in case nothing should be done
isotrop_res = bool(int(isotrop_res))
if ((not isotrop_res) and (upsample_factor is None)):
print('Uspampling not requested. Skipping...')
nii.to_filename(upsampledFile)
return nii
# convert input to number
if isotrop_res:
isotrop_res = float(np.min(header.get_zooms()[0:3]))
all_upsampling = [float(zoom)/isotrop_res for zoom in header.get_zooms()[0:3]]
for idx, zoom in enumerate(all_upsampling):
if zoom<1:
all_upsampling[idx]= 1.
else:
all_upsampling[idx]= np.round(zoom)
else:
upsample_factor = float(upsample_factor)
all_upsampling = [upsample_factor for zoom in header.get_zooms()[0:3]]
polynomial = int(polynomial)
old_volume = np.squeeze(nii.get_fdata().astype(float))
# get new volume shape and update upsampling based on rounded numbers
old_shape = old_volume.shape
print(old_shape)
new_shape = tuple([np.round(old_shape[ii]*usampling).astype(int) for ii, usampling in enumerate(all_upsampling)])
print(new_shape)
# upsample image
print('Upsampling volume...')
vol = sn.zoom(old_volume, all_upsampling)
print('Done.')
# update voxel sizes in header
if len(header.get_zooms())==3:
new_zooms = tuple( [header.get_zooms()[ii]/float(all_upsampling[ii]) for ii in np.arange(3)] ) # 3 spatial dimensions
elif len(header.get_zooms())>3:
tmp = [header.get_zooms()[ii]/float(all_upsampling[ii]) for ii in np.arange(3)]
tmp.extend(list(header.get_zooms()[3:]))
new_zooms = tuple(tmp) # 3 spatial dimensions + 1 time
else:
print('Cannot handle this stuff... ')
print(header.get_zooms())
raise Exception('Header has less than 2 entries. 2D?')
header.set_zooms(new_zooms)
# adapt affine according to scaling
all_upsampling.extend([1.]) # time
scaling = np.diag(1./np.asarray(all_upsampling))
affine = np.dot(affine, scaling)
# create new NII
newNii = nib.Nifti1Image(vol.astype(out_dtype), header=header, affine=affine)
# save niftis
newNii.to_filename(upsampledFile)
# save upsampling factors
with open(zoom_values_file, 'wb') as outfile:
pickle.dump([all_upsampling[:-1],polynomial, old_shape], outfile)
return (newNii)
def downsample(niiFileName, downsampled_file, zoom_values_file='upsampling_log.pickle', order=3):
"""
downsample a nifti which has been upsampled with the function above.
Example arguments:
niiFileName = '10529_t1_upsampled.nii.gz'
downsample_file = '10529_t1_downsample.nii.gz'
zoom_values_file = 'upsampling_log.pickle'
"""
# load the nifti
nii = nib.load(niiFileName)
header = nii.header
# make data type of image to float
out_dtype = np.float32
header['datatype'] = 16 # corresponds to float32
header['bitpix'] = 32 # corresponds to float32
downsample_factor=[]
with open(zoom_values_file,'rb') as zfile:
[all_upsampling, polynomial, old_shape] = pickle.load(zfile)
print('Downsampling with scales: ' + str(1./np.asarray(all_upsampling)))
if old_shape:
current_shape = nii.get_fdata().shape
print(old_shape)
downsample_values = np.asarray([float(old_shape[ii])/float(current_shape[ii]) for ii in np.arange(len(old_shape))])
else:
if len(all_upsampling) == 1:
downsample_values = 1./np.asarray(3*all_upsampling)
else:
downsample_values = 1./np.asarray(all_upsampling)
# downsampling image
print('Downsampling volume...')
vol = sn.zoom(nii.get_fdata(), downsample_values, order=int(order))
print('Done.')
# update voxel sizes in header
if len(header.get_zooms())==3:
new_zooms = tuple( [header.get_zooms()[ii]/float(downsample_values[ii]) for ii in np.arange(3)] ) # 3 spatial dimensions
elif len(header.get_zooms())>3:
tmp = [header.get_zooms()[ii]/float(downsample_values[ii]) for ii in np.arange(3)]
tmp.extend(list(header.get_zooms()[3:]))
new_zooms = tuple(tmp) # 3 spatial dimensions + 1 time
else:
print('Cannot handle this stuff... ')
print(header.get_zooms())
raise Exception('Header has less than 2 entries. 2D?')
# new_zooms = tuple( [header.get_zooms()[ii]/float(downsample_values[ii]) for ii in np.arange(len(header.get_zooms()))] )
header.set_zooms(new_zooms)
# adapt affine according to scaling
affine = nii.affine
downsample_values = downsample_values.tolist()
downsample_values.extend([1.]) # time
scaling = np.diag(1./np.asarray(downsample_values))
affine = np.dot(affine, scaling)
# create new NII
newNii = nib.Nifti1Image(vol.astype(out_dtype), header=header, affine=affine)
# save niftis
newNii.to_filename(downsampled_file)
return (newNii)
def binarize(infile, outfile, threshold, infofile=None, name=None, filename=None):
# load file
nii = nib.load(infile)
vol = nii.get_fdata()
vol = (vol>=float(threshold)).astype(int)
# create new NII
newNii = nib.Nifti1Image(vol, header=nii.header, affine=nii.affine)
# save niftis
if outfile != "None":
newNii.to_filename(outfile)
if infofile is not None:
voxel_size = np.prod(np.asarray(nii.header.get_zooms()).astype(float))
volume=np.sum(vol)*voxel_size
# read file
with open(infofile, 'r') as file:
csv_reader = csv.DictReader(file)
old_data = [row for row in csv_reader]
if len(old_data)==0:
old_data = {}
else:
old_data = old_data[0]
data = {}
if (filename is not None) and 'File' not in data.keys():
data['File'] = filename
data.update(old_data)
# add new info
with open(infofile, 'w') as fid:
data[name] = volume
writer = csv.DictWriter(fid, fieldnames=data.keys())
writer.writeheader()
writer.writerows([data])
return (newNii)
functions = {
'binarize': binarize,
'upsample': upsample,
'downsample': downsample
}
def print_help(func, name):
signature = '{script} {name} {args}'.format(
script=sys.argv[0],
name=name,
args=' '.join(inspect.getfullargspec(func).args))
print(signature)
print(func.__doc__)
if __name__ == '__main__':
usage = 'USAGE: \n{script} <command> <arguments>\n Commands: [ {commands} ]\n{script} -h <command>'.format(
script=sys.argv[0],
commands = ' | '.join(functions.keys()),
)
if len(sys.argv) == 1:
print(usage)
sys.exit(1)
command = sys.argv[1]
# Handle help
if command == '-h':
if len(sys.argv) == 2:
print(usage)
else:
func_name = sys.argv[2]
if func_name not in functions:
print("Unknown command " + func_name)
sys.exit(1)
func = functions[func_name]
print_help(func, func_name)
sys.exit(0)
# Handle normal usage
arguments = sys.argv[2:]
func = functions[command]
argspec = inspect.getfullargspec(func)
max = len(argspec.args)
if argspec.defaults is None:
min = max
else:
min = max - len(argspec.defaults)
if argspec.varargs is not None: # varargs means we can accept an unbounded #
max = len(arguments)+1
if not (min <= len(arguments) <= max):
print_help(func, command)
sys.exit(1)
func(*arguments)