-
Notifications
You must be signed in to change notification settings - Fork 24
/
mri_prep.py
339 lines (290 loc) · 12.2 KB
/
mri_prep.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
"""Data preparation for training."""
import os
import random
import shutil
import subprocess
import zipfile
import numpy as np
import tensorflow as tf
import wget
from mri_util import cfl, fftc, tf_util
tf.logging.set_verbosity(tf.logging.ERROR)
BIN_BART = "bart"
def download_dataset_knee(dir_out, dir_tmp="tmp", verbose=False, do_cleanup=True):
"""Download and unzip knee dataset from mridata.org."""
if not os.path.isdir(dir_out):
os.makedirs(dir_out)
if os.path.isdir(dir_tmp):
print("WARNING! Temporary folder exists (%s)" % dir_tmp)
else:
os.makedirs(dir_tmp)
num_data = 1
for i in range(num_data):
if verbose:
print("Processing data (%d)..." % i)
url = "http://old.mridata.org/knees/fully_sampled/p%d/e1/s1/P%d.zip" % (
i + 1,
i + 1,
)
dir_name_i = os.path.join(dir_out, "data%02d" % i)
if verbose:
print(" dowloading from %s..." % url)
if not os.path.isdir(dir_name_i):
os.makedirs(dir_name_i)
file_download = wget.download(url, out=dir_tmp)
if verbose:
print(" unzipping contents to %s..." % dir_name_i)
with zipfile.ZipFile(file_download, "r") as zip_ref:
for member in zip_ref.namelist():
filename = os.path.basename(member)
if not filename:
continue
file_src = zip_ref.open(member)
file_dest = open(os.path.join(dir_name_i, filename), "wb")
with file_src, file_dest:
shutil.copyfileobj(file_src, file_dest)
if do_cleanup:
if verbose:
print("Cleanup...")
shutil.rmtree(dir_tmp)
if verbose:
print("Done")
def create_masks(
dir_out,
shape_y=320,
shape_z=256,
verbose=False,
acc_y=(1, 2, 3),
acc_z=(1, 2, 3),
shape_calib=1,
variable_density=False,
num_repeat=4,
):
"""Create sampling masks using BART."""
flags = ""
file_fmt = "mask_%0.1fx%0.1f_c%d_%02d"
if variable_density:
flags = flags + " -v "
file_fmt = file_fmt + "_vd"
if not os.path.exists(dir_out):
os.mkdir(dir_out)
for a_y in acc_y:
for a_z in acc_z:
if a_y * a_z != 1:
num_repeat_i = num_repeat
if (a_y == acc_y[-1]) and (a_z == acc_z[-1]):
num_repeat_i = num_repeat_i * 2
for i in range(num_repeat_i):
random_seed = 1e6 * random.random()
file_name = file_fmt % (a_y, a_z, shape_calib, i)
if verbose:
print("creating mask (%s)..." % file_name)
file_name = os.path.join(dir_out, file_name)
cmd = "%s poisson -C %d -Y %d -Z %d -y %d -z %d -s %d %s %s" % (
BIN_BART,
shape_calib,
shape_y,
shape_z,
a_y,
a_z,
random_seed,
flags,
file_name,
)
subprocess.check_output(["bash", "-c", cmd])
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def setup_data_tfrecords(
dir_in_root,
dir_out,
data_divide=(0.75, 0.05, 0.2),
min_shape=[80, 180],
num_maps=1,
crop_maps=False,
verbose=False,
):
"""Setups training data as tfrecords.
prep_data.setup_data('/mnt/raid3/data/Studies_DCE/recon-ccomp6/',
'/mnt/raid3/jycheng/Project/deepspirit/data/train/', verbose=True)
"""
# Check for two echos in here
# Use glob to find if have echo01
if verbose:
print("Directory names:")
print(" Input root: %s" % dir_in_root)
print(" Output root: %s" % dir_out)
file_kspace = "kspace"
file_sensemap = "sensemap"
case_list = os.listdir(dir_in_root)
random.shuffle(case_list)
num_cases = len(case_list)
i_train_1 = np.round(data_divide[0] * num_cases).astype(int)
i_validate_0 = i_train_1 + 1
i_validate_1 = np.round(
data_divide[1] * num_cases).astype(int) + i_validate_0
if not os.path.exists(dir_out):
os.mkdir(dir_out)
if not os.path.exists(os.path.join(dir_out, "train")):
os.mkdir(os.path.join(dir_out, "train"))
if not os.path.exists(os.path.join(dir_out, "validate")):
os.mkdir(os.path.join(dir_out, "validate"))
if not os.path.exists(os.path.join(dir_out, "test")):
os.mkdir(os.path.join(dir_out, "test"))
i_case = 0
for case_name in case_list:
file_kspace_i = os.path.join(dir_in_root, case_name, file_kspace)
file_sensemap_i = os.path.join(dir_in_root, case_name, file_sensemap)
if i_case < i_train_1:
dir_out_i = os.path.join(dir_out, "train")
elif i_case < i_validate_1:
dir_out_i = os.path.join(dir_out, "validate")
else:
dir_out_i = os.path.join(dir_out, "test")
if verbose:
print("Processing [%d] %s..." % (i_case, case_name))
i_case = i_case + 1
kspace = np.squeeze(cfl.read(file_kspace_i))
if (min_shape is None) or (
min_shape[0] <= kspace.shape[1] and min_shape[1] <= kspace.shape[2]
):
if verbose:
print(" Slice shape: (%d, %d)" %
(kspace.shape[1], kspace.shape[2]))
print(" Num channels: %d" % kspace.shape[0])
shape_x = kspace.shape[-1]
kspace = fftc.ifftc(kspace, axis=-1)
kspace = kspace.astype(np.complex64)
# if shape_c_out < shape_c:
# if verbose:
# print(" applying coil compression (%d -> %d)..." %
# (shape_c, shape_c_out))
# shape_cal = 24
# ks_cal = recon.crop(ks, [-1, shape_cal, shape_cal, -1])
# ks_cal = np.reshape(ks_cal, [shape_c,
# shape_cal*shape_cal,
# shape_x])
# cc_mat = coilcomp.calc_gcc_weights_c(ks_cal, shape_c_out)
# ks_cc = np.reshape(ks, [shape_c, -1, shape_x])
# ks_cc = coilcomp.apply_gcc_weights_c(ks_cc, cc_mat)
# ks = np.reshape(ks_cc, [shape_c_out, shape_z, shape_y, shape_x])
cmd_flags = ""
if crop_maps:
cmd_flags = cmd_flags + " -c 1e-9"
cmd_flags = cmd_flags + (" -m %d" % num_maps)
cmd = "%s ecalib %s %s %s" % (
BIN_BART,
cmd_flags,
file_kspace_i,
file_sensemap_i,
)
if verbose:
print(" Estimating sensitivity maps (bart espirit)...")
print(" %s" % cmd)
subprocess.check_call(["bash", "-c", cmd])
sensemap = np.squeeze(cfl.read(file_sensemap_i))
sensemap = np.expand_dims(sensemap, axis=0)
sensemap = sensemap.astype(np.complex64)
if verbose:
print(" Creating tfrecords (%d)..." % shape_x)
for i_x in range(shape_x):
file_out = os.path.join(
dir_out_i, "%s_x%03d.tfrecords" % (case_name, i_x)
)
kspace_x = kspace[:, :, :, i_x]
sensemap_x = sensemap[:, :, :, :, i_x]
example = tf.train.Example(
features=tf.train.Features(
feature={
"name": _bytes_feature(str.encode(case_name)),
"xslice": _int64_feature(i_x),
"ks_shape_x": _int64_feature(kspace.shape[3]),
"ks_shape_y": _int64_feature(kspace.shape[2]),
"ks_shape_z": _int64_feature(kspace.shape[1]),
"ks_shape_c": _int64_feature(kspace.shape[0]),
"map_shape_x": _int64_feature(sensemap.shape[4]),
"map_shape_y": _int64_feature(sensemap.shape[3]),
"map_shape_z": _int64_feature(sensemap.shape[2]),
"map_shape_c": _int64_feature(sensemap.shape[1]),
"map_shape_m": _int64_feature(sensemap.shape[0]),
"ks": _bytes_feature(kspace_x.tostring()),
"map": _bytes_feature(sensemap_x.tostring()),
}
)
)
tf_writer = tf.python_io.TFRecordWriter(file_out)
tf_writer.write(example.SerializeToString())
tf_writer.close()
def process_tfrecord(example, num_channels=None, num_emaps=None):
"""Process TFRecord to actual tensors."""
features = tf.parse_single_example(
example,
features={
"name": tf.FixedLenFeature([], tf.string),
"xslice": tf.FixedLenFeature([], tf.int64),
"ks_shape_x": tf.FixedLenFeature([], tf.int64),
"ks_shape_y": tf.FixedLenFeature([], tf.int64),
"ks_shape_z": tf.FixedLenFeature([], tf.int64),
"ks_shape_c": tf.FixedLenFeature([], tf.int64),
"map_shape_x": tf.FixedLenFeature([], tf.int64),
"map_shape_y": tf.FixedLenFeature([], tf.int64),
"map_shape_z": tf.FixedLenFeature([], tf.int64),
"map_shape_c": tf.FixedLenFeature([], tf.int64),
"map_shape_m": tf.FixedLenFeature([], tf.int64),
"ks": tf.FixedLenFeature([], tf.string),
"map": tf.FixedLenFeature([], tf.string),
},
)
name = features["name"]
xslice = tf.cast(features["xslice"], dtype=tf.int32)
# shape_x = tf.cast(features['shape_x'], dtype=tf.int32)
ks_shape_y = tf.cast(features["ks_shape_y"], dtype=tf.int32)
ks_shape_z = tf.cast(features["ks_shape_z"], dtype=tf.int32)
if num_channels is None:
ks_shape_c = tf.cast(features["ks_shape_c"], dtype=tf.int32)
else:
ks_shape_c = num_channels
map_shape_y = tf.cast(features["map_shape_y"], dtype=tf.int32)
map_shape_z = tf.cast(features["map_shape_z"], dtype=tf.int32)
if num_channels is None:
map_shape_c = tf.cast(features["map_shape_c"], dtype=tf.int32)
else:
map_shape_c = num_channels
if num_emaps is None:
map_shape_m = tf.cast(features["map_shape_m"], dtype=tf.int32)
else:
map_shape_m = num_emaps
with tf.name_scope("kspace"):
ks_record_bytes = tf.decode_raw(features["ks"], tf.float32)
image_shape = [ks_shape_c, ks_shape_z, ks_shape_y]
ks_x = tf.reshape(ks_record_bytes, image_shape + [2])
ks_x = tf_util.channels_to_complex(ks_x)
ks_x = tf.reshape(ks_x, image_shape)
with tf.name_scope("sensemap"):
map_record_bytes = tf.decode_raw(features["map"], tf.float32)
map_shape = [map_shape_m * map_shape_c, map_shape_z, map_shape_y]
map_x = tf.reshape(map_record_bytes, map_shape + [2])
map_x = tf_util.channels_to_complex(map_x)
map_x = tf.reshape(map_x, map_shape)
return name, xslice, ks_x, map_x
def read_tfrecord_with_sess(tf_sess, filename_tfrecord):
"""Read TFRecord for debugging."""
tf_reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer([filename_tfrecord])
_, serialized_example = tf_reader.read(filename_queue)
name, xslice, ks_x, map_x = process_tfrecord(serialized_example)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=tf_sess, coord=coord)
name, xslice, ks_x, map_x = tf_sess.run([name, xslice, ks_x, map_x])
coord.request_stop()
coord.join(threads)
return {"name": name, "xslice": xslice, "ks": ks_x, "sensemap": map_x}
def read_tfrecord(filename_tfrecord):
"""Read TFRecord for debugging."""
session_config = tf.ConfigProto()
session_config.gpu_options.allow_growth = True
tf_sess = tf.Session(config=session_config)
data = read_tfrecord_with_sess(tf_sess, filename_tfrecord)
tf_sess.close()
return data