-
Notifications
You must be signed in to change notification settings - Fork 0
/
motioncorrection_1st_boundingbox.py
executable file
·350 lines (319 loc) · 12.2 KB
/
motioncorrection_1st_boundingbox.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python
experiment_dir = '/Users/eija/Desktop/prostate_MR/pipelinedata'
param_rigid = 'Par0001translation.txt'
param_BSpline = 'Par0001bspline08_mod.txt'
#mask_matfile_basedir = '/Users/eija/Desktop/prostate_MR/PET_MR_dwis/Carimas27projectfiles_Hb_work_all_noGS/ROI_mat_files'
mask_matfile_basedir_hB = '/Users/eija/Desktop/prostate_MR/PET_MR_dwis/Carimas27projectfiles_Hb_work_all_noGS_for_Motion_Cor/ROI_mat_files'
mask_matfile_basedir_lB = '/Users/eija/Desktop/prostate_MR/PET_MR_dwis/Carimas27projectfiles_Lb_work_2rep/ROI_mat_files'
#
# Splits subject ID into parts
#
# subjectid - subject ID
#
def split_subjectid(subjectid):
parts = subjectid.split('_')
patient_no_str = parts[0]
patientname_str = parts[1]
bset_str = parts[2]
rep_str = parts[3]
return patient_no_str, patientname_str, bset_str, rep_str
#
# Resolves ROI that is a square-shaped bounding box around ROI pixels
#
# ROIpixel_array - 2-dimensional array
# padding - number of empty pixels around ROI
#
def resolve_boundingbox(ROIpixel_array, padding):
import numpy as np
# Find minimum and maximum coordinates [xmin,xmax,ymin,ymax]
bounds = [float("inf"), float("-inf"), float("inf"), float("-inf")]
xlen = ROIpixel_array.shape[0]
ylen = ROIpixel_array.shape[1]
for xi in range(xlen):
for yi in range(ylen):
if ROIpixel_array[xi][yi] != 0:
if xi < bounds[0]:
bounds[0] = xi
if xi > bounds[1]:
bounds[1] = xi
if yi < bounds[2]:
bounds[2] = yi
if yi > bounds[3]:
bounds[3] = yi
# Add padding
bounds[0] = bounds[0] - padding
bounds[1] = bounds[1] + padding
bounds[2] = bounds[2] - padding
bounds[3] = bounds[3] + padding
if bounds[0] < 0:
bounds[0] = 0
if bounds[1] > xlen-1:
bounds[1] = xlen-1
if bounds[2] < 0:
bounds[2] = 0
if bounds[3] > ylen-1:
bounds[3] = ylen-1
# Create bounding box ROI
outROI = np.zeros(ROIpixel_array.shape)
for xi in range(bounds[0], bounds[1]+1):
for yi in range(bounds[2], bounds[3]+1):
outROI[xi][yi] = 1
return outROI, bounds
#
# Resolve mat-filename containing ROI masks
#
# output_prefix - output prefix
#
def resolve_matfilename(output_prefix):
# Resolve mat-file name
parts = output_prefix.split('_')
patient_no_str, patientname_str, bset_str, rep_str = split_subjectid(output_prefix)
# if not (bset_str=='hB' or bset_str=='lB'):
if not (bset_str=='hB'):
raise Exception((output_prefix + " UNSUPPORTED B-SET"))
if (bset_str=='hB'):
matfilename = mask_matfile_basedir_hB + '/' + patient_no_str + '_' + rep_str + '_DICOMconverted.mat'
if (bset_str=='lB'):
matfilename = mask_matfile_basedir_lB + '/' + patient_no_str + '_' + rep_str + '_DICOMconverted.mat'
return matfilename
#
# Get mask image in DICOM from mat-file data
#
# output_prefix - output prefix
# input_shape - input frame shape
# input_plans - DICOM sample slices
# matfilename - mat-file containing ROIs
# ROIindexes - ROI indexes that are used to create bounding mask
# padding - number of empty pixels around ROI
#
def get_boundsmask(output_prefix, input_shape, input_plans, matfilename, ROIindexes, padding):
import scipy.io
import os
import numpy as np
import copy
mat = scipy.io.loadmat(matfilename)
# Get list of ROIs
ROIs = mat['ROIs'].tolist()[0]
# Get list of slices where ROIs are located
ROIslices = mat['ROIslices'][0].tolist()
# Create and write mask images
print str(len(ROIs)) + " ROIs"
shape = [input_shape[0], input_shape[1]]
# Create mask around combined ROIs
ROIpixel_array_combined = np.zeros(shape)
for roi_i in range(len(ROIindexes)):
ROIlist = ROIs[ROIindexes[roi_i]].tolist()
ROIname = str(ROIlist[0][0][0][0])
ROIpixel_array = ROIlist[0][0][1]
print "catenating " + ROIname
ROIpixel_array_combined = ROIpixel_array_combined + ROIpixel_array
for xi in range(shape[0]):
for yi in range(shape[1]):
if ROIpixel_array_combined[xi][yi] != 0:
ROIpixel_array_combined[xi][yi] = 1
ROIpixel_array, bounds = resolve_boundingbox(ROIpixel_array_combined, padding)
# Add z bounds to make [xmin,xmax,ymin,ymax,zmin,zmax]
bounds.append(0)
bounds.append(input_shape[2]-1)
ROI_filenames = []
dcmio = DicomIO.DicomIO()
# Resolve ROI data
ROIlist = ROIs[roi_i].tolist()
ROIname = "Boundingbox"
print ROIname
#print ROIpixel_array
# Resolve output name
out_dir = experiment_dir + '/' + output_prefix + '/' + 'ROImask' + str(roi_i+1) + '_' + ROIname
# Place mask into intensity values
output_frame = []
#print str(len(input_frame[0])) + " slices of size " + str(shape)
for slice_i in range(input_shape[2]):
slice = copy.deepcopy(input_plans[slice_i])
if slice_i != ROIslices[0]:
#print "zero-slice:" + str(slice_i) + " " + str(shape)
slice.PixelData = np.zeros(shape).astype(np.uint16).tostring()
else:
#print " ROI-slice:" + str(slice_i) + " " + str(ROIpixel_array.shape)
slice.PixelData = ROIpixel_array.astype(np.uint16).tostring()
output_frame.append(slice)
# Create output directory if it does not exist
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# Write data
filenames = dcmio.WriteDICOM_frames(out_dir, [output_frame], 'IM')
ROI_filenames.append(filenames[ROIslices[0]])
return out_dir, ROI_filenames, ROIslices[0], bounds
#
# Get subvolumes
#
# dwidcm - DICOM source data
# volume_list - list of volume indexes for output
# bounds - bounds of subvolumes
# output_prefix - output prefix
#
def get_subvolumes(input_dir, volume_list, bounds, output_prefix):
import dicom
import DicomIO
import shutil
import numpy as np
dcmio = DicomIO.DicomIO()
from nipype.utils.filemanip import split_filename
# resolve output directory and volumes
out_dir_base = experiment_dir + '/' + output_prefix + '/' + 'subvolumes'
filenames_all = []
outdirs_all = []
out_vols_all = []
for vol_i in range(len(volume_list)):
out_dir = out_dir_base + '_' + str(volume_list[vol_i])
out_vols = []
dwivolume = dwidcm[volume_list[vol_i]]
#take subregion from volume
for slice_i in range(len(dwivolume)):
pixel_array = dwivolume[slice_i].pixel_array[bounds[2]:bounds[3],bounds[0]:bounds[1]]
dwivolume[slice_i].PixelData = pixel_array.astype(np.uint16).tostring()
dwivolume[slice_i].Columns = bounds[1]-bounds[0]
dwivolume[slice_i].Rows = bounds[3]-bounds[2]
#append volume to lists
out_vols.append(dwivolume)
out_vols_all.append(dwivolume)
# Create output directory if it does not exist
if not os.path.exists(out_dir):
os.makedirs(out_dir)
else:
shutil.rmtree(out_dir)
os.makedirs(out_dir)
filenames = dcmio.WriteDICOM_frames(out_dir, out_vols, 'IM')
filenames_all.append(filenames)
outdirs_all.append(out_dir)
return outdirs_all, filenames_all, out_vols_all
#
# Convert multi-slice tiff 2 DICOM
#
# in_files - single TIFF input file (.tiff) for each frame
# dicomdir - output DICOM directory
# plans - DICOM header templates for output, frames X slices
# out_prefix - subject specific prefix
#
def singletiff2multidicom(in_files, dicomdir, plans, out_prefix):
import DicomIO
import numpy as np
import os
import shutil
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import tifffile as tiff
outdir = experiment_dir + '/' + out_prefix + '/' + dicomdir
if not os.path.exists(outdir):
os.makedirs(outdir)
else:
shutil.rmtree(outdir)
os.makedirs(outdir)
# Resolve new frame list
out_vols = plans
for file_i in range(len(in_files)):
print "Reading " + in_files[file_i]
ds = tiff.imread(in_files[file_i])
no_slices = ds.shape[0]
for z_i in range(no_slices):
out_vols[file_i][z_i].PixelData = ds[z_i].astype(np.uint16).tostring()
dcmio = DicomIO.DicomIO()
filenames = dcmio.WriteDICOM_frames(outdir, out_vols, 'IM')
return outdir, filenames
#
# Convert single-slice DICOM 2 DICOM
#
# in_dirs - single DICOM input directory for each frame
# dicomdir - output DICOM directory
# plans - DICOM header templates for output, frames X slices
# out_prefix - subject specific prefix
#
def multidicom2multidicom(in_dirs, dicomdir, plans, out_prefix):
import dicom
import DicomIO
import numpy as np
import os
import shutil
outdir = experiment_dir + '/' + out_prefix + '/' + dicomdir
if not os.path.exists(outdir):
os.makedirs(outdir)
else:
shutil.rmtree(outdir)
os.makedirs(outdir)
# Resolve new frame list
out_vols = plans
dcmio = DicomIO.DicomIO()
for dir_i in range(len(in_dirs)):
print "Reading " + in_dirs[dir_i]
frame_list = dcmio.ReadDICOM_frames(in_dirs[dir_i])
no_slices = len(frame_list[0])
for z_i in range(no_slices):
out_vols[dir_i][z_i].PixelData = frame_list[0][z_i].pixel_array.astype(np.uint16).tostring()
dcmio = DicomIO.DicomIO()
filenames = dcmio.WriteDICOM_frames(outdir, out_vols, 'IM')
return outdir, filenames
from argparse import ArgumentParser
import sys
import os
import DicomIO
import conversions as conv
import time
import numpy as np
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--dicomdir", dest="dicomdir", help="dicomdir", required=True)
parser.add_argument("--subject", dest="subject", help="subject id", required=True)
args = parser.parse_args()
errors = 0
# Create output directory if it does not exist
if not os.path.exists((experiment_dir + '/' + args.subject)):
os.makedirs((experiment_dir + '/' + args.subject))
if os.path.exists((experiment_dir + '/' + args.subject + '/' + 'elapsed_time.txt')):
print "SKIPPING"
sys.exit(0)
else:
print "EXECUTING"
matfilename = resolve_matfilename(args.subject)
if not os.path.exists(matfilename):
print (matfilename + " DOES NOT EXIST")
sys.exit(1)
dcmio = DicomIO.DicomIO()
start_time = time.time()
print "READING DICOM [" + args.dicomdir + "]"
try:
dwidcm = dcmio.ReadDICOM_frames(args.dicomdir)
dwishape = [dwidcm[0][0].pixel_array.shape[0], dwidcm[0][0].pixel_array.shape[1], len(dwidcm[0])]
except:
errors = errors + 1
sys.exit(1)
print "RESOLVING BOUNDS"
# try:
mask_file, mask_file_ROIslice_filename, ROIslice_i, bounds = get_boundsmask(args.subject, dwishape, dwidcm[0], matfilename, [0], 20)
np.savetxt((experiment_dir + '/' + args.subject + '/' + 'subregion.txt'),bounds, fmt='%f', header=('subject ' + args.subject))
#except Exception as inst:
#errors = errors + 1
#print type(inst) # the exception instance
#print inst.args # arguments stored in .args
#print inst # __str__ allows args to be printed directly
#sys.exit(1)
# Extract first volume from dwi
print "RESOLVING SUBVOLUMES"
try:
subvol_dirs, subvol_filenames_all, subvols_orig = get_subvolumes(dwidcm, range(len(dwidcm)), bounds, args.subject)
except Exception as inst:
errors = errors + 1
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # __str__ allows args to be printed directly
sys.exit(1)
# Write motioncorrected as single multifile DICOM subfolder
print "COMBINING ORIGINAL SUB-WINDOWED"
try:
multidicom2multidicom(subvol_dirs, 'Noncorrected', subvols_orig, args.subject)
except Exception as inst:
errors = errors + 1
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # __str__ allows args to be printed directly
sys.exit(1)
sys.exit(0)