-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathinvertcontrast.py
executable file
·381 lines (306 loc) · 16.4 KB
/
invertcontrast.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import ismrmrd
import os
import itertools
import logging
import traceback
import numpy as np
import numpy.fft as fft
import xml.dom.minidom
import base64
import ctypes
import re
import mrdhelper
import constants
from time import perf_counter
# Folder for debug output files
debugFolder = "/tmp/share/debug"
def process(connection, config, metadata):
logging.info("Config: \n%s", config)
# Metadata should be MRD formatted header, but may be a string
# if it failed conversion earlier
try:
# Disabled due to incompatibility between PyXB and Python 3.8:
# https://github.com/pabigot/pyxb/issues/123
# # logging.info("Metadata: \n%s", metadata.toxml('utf-8'))
logging.info("Incoming dataset contains %d encodings", len(metadata.encoding))
logging.info("First encoding is of type '%s', with a matrix size of (%s x %s x %s) and a field of view of (%s x %s x %s)mm^3",
metadata.encoding[0].trajectory,
metadata.encoding[0].encodedSpace.matrixSize.x,
metadata.encoding[0].encodedSpace.matrixSize.y,
metadata.encoding[0].encodedSpace.matrixSize.z,
metadata.encoding[0].encodedSpace.fieldOfView_mm.x,
metadata.encoding[0].encodedSpace.fieldOfView_mm.y,
metadata.encoding[0].encodedSpace.fieldOfView_mm.z)
except:
logging.info("Improperly formatted metadata: \n%s", metadata)
# Continuously parse incoming data parsed from MRD messages
currentSeries = 0
acqGroup = []
imgGroup = []
waveformGroup = []
try:
for item in connection:
# ----------------------------------------------------------
# Raw k-space data messages
# ----------------------------------------------------------
if isinstance(item, ismrmrd.Acquisition):
# Accumulate all imaging readouts in a group
if (not item.is_flag_set(ismrmrd.ACQ_IS_NOISE_MEASUREMENT) and
not item.is_flag_set(ismrmrd.ACQ_IS_PARALLEL_CALIBRATION) and
not item.is_flag_set(ismrmrd.ACQ_IS_PHASECORR_DATA) and
not item.is_flag_set(ismrmrd.ACQ_IS_NAVIGATION_DATA)):
acqGroup.append(item)
# When this criteria is met, run process_raw() on the accumulated
# data, which returns images that are sent back to the client.
if item.is_flag_set(ismrmrd.ACQ_LAST_IN_SLICE):
logging.info("Processing a group of k-space data")
image = process_raw(acqGroup, connection, config, metadata)
connection.send_image(image)
acqGroup = []
# ----------------------------------------------------------
# Image data messages
# ----------------------------------------------------------
elif isinstance(item, ismrmrd.Image):
# When this criteria is met, run process_group() on the accumulated
# data, which returns images that are sent back to the client.
# e.g. when the series number changes:
if item.image_series_index != currentSeries:
logging.info("Processing a group of images because series index changed to %d", item.image_series_index)
currentSeries = item.image_series_index
image = process_image(imgGroup, connection, config, metadata)
connection.send_image(image)
imgGroup = []
# Only process magnitude images -- send phase images back without modification (fallback for images with unknown type)
if (item.image_type is ismrmrd.IMTYPE_MAGNITUDE) or (item.image_type == 0):
imgGroup.append(item)
else:
tmpMeta = ismrmrd.Meta.deserialize(item.attribute_string)
tmpMeta['Keep_image_geometry'] = 1
item.attribute_string = tmpMeta.serialize()
connection.send_image(item)
continue
# ----------------------------------------------------------
# Waveform data messages
# ----------------------------------------------------------
elif isinstance(item, ismrmrd.Waveform):
waveformGroup.append(item)
elif item is None:
break
else:
logging.error("Unsupported data type %s", type(item).__name__)
# Extract raw ECG waveform data. Basic sorting to make sure that data
# is time-ordered, but no additional checking for missing data.
# ecgData has shape (5 x timepoints)
if len(waveformGroup) > 0:
waveformGroup.sort(key = lambda item: item.time_stamp)
ecgData = [item.data for item in waveformGroup if item.waveform_id == 0]
if len(ecgData) > 0:
ecgData = np.concatenate(ecgData,1)
# Process any remaining groups of raw or image data. This can
# happen if the trigger condition for these groups are not met.
# This is also a fallback for handling image data, as the last
# image in a series is typically not separately flagged.
if len(acqGroup) > 0:
logging.info("Processing a group of k-space data (untriggered)")
image = process_raw(acqGroup, connection, config, metadata)
connection.send_image(image)
acqGroup = []
if len(imgGroup) > 0:
logging.info("Processing a group of images (untriggered)")
image = process_image(imgGroup, connection, config, metadata)
connection.send_image(image)
imgGroup = []
except Exception as e:
logging.error(traceback.format_exc())
connection.send_logging(constants.MRD_LOGGING_ERROR, traceback.format_exc())
finally:
connection.send_close()
def process_raw(group, connection, config, metadata):
if len(group) == 0:
return []
# Start timer
tic = perf_counter()
# Create folder, if necessary
if not os.path.exists(debugFolder):
os.makedirs(debugFolder)
logging.debug("Created folder " + debugFolder + " for debug output files")
# Format data into single [cha PE RO phs] array
lin = [acquisition.idx.kspace_encode_step_1 for acquisition in group]
phs = [acquisition.idx.phase for acquisition in group]
# Use the zero-padded matrix size
data = np.zeros((group[0].data.shape[0],
metadata.encoding[0].encodedSpace.matrixSize.y,
metadata.encoding[0].encodedSpace.matrixSize.x,
max(phs)+1),
group[0].data.dtype)
rawHead = [None]*(max(phs)+1)
for acq, lin, phs in zip(group, lin, phs):
if (lin < data.shape[1]) and (phs < data.shape[3]):
# TODO: Account for asymmetric echo in a better way
data[:,lin,-acq.data.shape[1]:,phs] = acq.data
# center line of k-space is encoded in user[5]
if (rawHead[phs] is None) or (np.abs(acq.getHead().idx.kspace_encode_step_1 - acq.getHead().idx.user[5]) < np.abs(rawHead[phs].idx.kspace_encode_step_1 - rawHead[phs].idx.user[5])):
rawHead[phs] = acq.getHead()
# Flip matrix in RO/PE to be consistent with ICE
data = np.flip(data, (1, 2))
logging.debug("Raw data is size %s" % (data.shape,))
np.save(debugFolder + "/" + "raw.npy", data)
# Fourier Transform
data = fft.fftshift( data, axes=(1, 2))
data = fft.ifft2( data, axes=(1, 2))
data = fft.ifftshift(data, axes=(1, 2))
data *= np.prod(data.shape) # FFT scaling for consistency with ICE
# Sum of squares coil combination
# Data will be [PE RO phs]
data = np.abs(data)
data = np.square(data)
data = np.sum(data, axis=0)
data = np.sqrt(data)
logging.debug("Image data is size %s" % (data.shape,))
np.save(debugFolder + "/" + "img.npy", data)
# Remove readout oversampling
offset = int((data.shape[1] - metadata.encoding[0].reconSpace.matrixSize.x)/2)
data = data[:,offset:offset+metadata.encoding[0].reconSpace.matrixSize.x]
# Remove phase oversampling
offset = int((data.shape[0] - metadata.encoding[0].reconSpace.matrixSize.y)/2)
data = data[offset:offset+metadata.encoding[0].reconSpace.matrixSize.y,:]
logging.debug("Image without oversampling is size %s" % (data.shape,))
np.save(debugFolder + "/" + "imgCrop.npy", data)
# Measure processing time
toc = perf_counter()
strProcessTime = "Total processing time: %.2f ms" % ((toc-tic)*1000.0)
logging.info(strProcessTime)
# Send this as a text message back to the client
connection.send_logging(constants.MRD_LOGGING_INFO, strProcessTime)
# Format as ISMRMRD image data
imagesOut = []
for phs in range(data.shape[2]):
# Create new MRD instance for the processed image
# data has shape [PE RO phs], i.e. [y x].
# from_array() should be called with 'transpose=False' to avoid warnings, and when called
# with this option, can take input as: [cha z y x], [z y x], or [y x]
tmpImg = ismrmrd.Image.from_array(data[...,phs], transpose=False)
# Set the header information
tmpImg.setHead(mrdhelper.update_img_header_from_raw(tmpImg.getHead(), rawHead[phs]))
tmpImg.field_of_view = (ctypes.c_float(metadata.encoding[0].reconSpace.fieldOfView_mm.x),
ctypes.c_float(metadata.encoding[0].reconSpace.fieldOfView_mm.y),
ctypes.c_float(metadata.encoding[0].reconSpace.fieldOfView_mm.z))
tmpImg.image_index = phs
# Set ISMRMRD Meta Attributes
tmpMeta = ismrmrd.Meta()
tmpMeta['DataRole'] = 'Image'
tmpMeta['ImageProcessingHistory'] = ['FIRE', 'PYTHON']
tmpMeta['Keep_image_geometry'] = 1
xml = tmpMeta.serialize()
logging.debug("Image MetaAttributes: %s", xml)
tmpImg.attribute_string = xml
imagesOut.append(tmpImg)
# Call process_image() to invert image contrast
imagesOut = process_image(imagesOut, connection, config, metadata)
return imagesOut
def process_image(images, connection, config, metadata):
if len(images) == 0:
return []
# Create folder, if necessary
if not os.path.exists(debugFolder):
os.makedirs(debugFolder)
logging.debug("Created folder " + debugFolder + " for debug output files")
logging.debug("Processing data with %d images of type %s", len(images), ismrmrd.get_dtype_from_data_type(images[0].data_type))
# Note: The MRD Image class stores data as [cha z y x]
# Extract image data into a 5D array of size [img cha z y x]
data = np.stack([img.data for img in images])
head = [img.getHead() for img in images]
meta = [ismrmrd.Meta.deserialize(img.attribute_string) for img in images]
# Reformat data to [y x z cha img], i.e. [row col] for the first two dimensions
data = data.transpose((3, 4, 2, 1, 0))
# Display MetaAttributes for first image
logging.debug("MetaAttributes[0]: %s", ismrmrd.Meta.serialize(meta[0]))
# Optional serialization of ICE MiniHeader
if 'IceMiniHead' in meta[0]:
logging.debug("IceMiniHead[0]: %s", base64.b64decode(meta[0]['IceMiniHead']).decode('utf-8'))
logging.debug("Original image data is size %s" % (data.shape,))
np.save(debugFolder + "/" + "imgOrig.npy", data)
if ('parameters' in config) and ('options' in config['parameters']) and (config['parameters']['options'] == 'complex'):
# Complex images are requested
data = data.astype(np.complex64)
maxVal = data.max()
else:
# Determine max value (12 or 16 bit)
BitsStored = 12
if (mrdhelper.get_userParameterLong_value(metadata, "BitsStored") is not None):
BitsStored = mrdhelper.get_userParameterLong_value(metadata, "BitsStored")
maxVal = 2**BitsStored - 1
# Normalize and convert to int16
data = data.astype(np.float64)
data *= maxVal/data.max()
data = np.around(data)
data = data.astype(np.int16)
# Invert image contrast
data = maxVal-data
data = np.abs(data)
np.save(debugFolder + "/" + "imgInverted.npy", data)
currentSeries = 0
# Re-slice back into 2D images
imagesOut = [None] * data.shape[-1]
for iImg in range(data.shape[-1]):
# Create new MRD instance for the inverted image
# Transpose from convenience shape of [y x z cha] to MRD Image shape of [cha z y x]
# from_array() should be called with 'transpose=False' to avoid warnings, and when called
# with this option, can take input as: [cha z y x], [z y x], or [y x]
imagesOut[iImg] = ismrmrd.Image.from_array(data[...,iImg].transpose((3, 2, 0, 1)), transpose=False)
# Create a copy of the original fixed header and update the data_type
# (we changed it to int16 from all other types)
oldHeader = head[iImg]
oldHeader.data_type = imagesOut[iImg].data_type
# Set the image_type to match the data_type for complex data
if (imagesOut[iImg].data_type == ismrmrd.DATATYPE_CXFLOAT) or (imagesOut[iImg].data_type == ismrmrd.DATATYPE_CXDOUBLE):
oldHeader.image_type = ismrmrd.IMTYPE_COMPLEX
# Unused example, as images are grouped by series before being passed into this function now
# oldHeader.image_series_index = currentSeries
# Increment series number when flag detected (i.e. follow ICE logic for splitting series)
if mrdhelper.get_meta_value(meta[iImg], 'IceMiniHead') is not None:
if mrdhelper.extract_minihead_bool_param(base64.b64decode(meta[iImg]['IceMiniHead']).decode('utf-8'), 'BIsSeriesEnd') is True:
currentSeries += 1
imagesOut[iImg].setHead(oldHeader)
# Create a copy of the original ISMRMRD Meta attributes and update
tmpMeta = meta[iImg]
tmpMeta['DataRole'] = 'Image'
tmpMeta['ImageProcessingHistory'] = ['PYTHON', 'INVERT']
tmpMeta['WindowCenter'] = str((maxVal+1)/2)
tmpMeta['WindowWidth'] = str((maxVal+1))
tmpMeta['SequenceDescriptionAdditional'] = 'FIRE'
tmpMeta['Keep_image_geometry'] = 1
if ('parameters' in config) and ('options' in config['parameters']):
# Example for sending ROIs
if config['parameters']['options'] == 'roi':
logging.info("Creating ROI_example")
tmpMeta['ROI_example'] = create_example_roi(data.shape)
# Example for setting colormap
if config['parameters']['options'] == 'colormap':
tmpMeta['LUTFileName'] = 'MicroDeltaHotMetal.pal'
# Add image orientation directions to MetaAttributes if not already present
if tmpMeta.get('ImageRowDir') is None:
tmpMeta['ImageRowDir'] = ["{:.18f}".format(oldHeader.read_dir[0]), "{:.18f}".format(oldHeader.read_dir[1]), "{:.18f}".format(oldHeader.read_dir[2])]
if tmpMeta.get('ImageColumnDir') is None:
tmpMeta['ImageColumnDir'] = ["{:.18f}".format(oldHeader.phase_dir[0]), "{:.18f}".format(oldHeader.phase_dir[1]), "{:.18f}".format(oldHeader.phase_dir[2])]
metaXml = tmpMeta.serialize()
logging.debug("Image MetaAttributes: %s", xml.dom.minidom.parseString(metaXml).toprettyxml())
logging.debug("Image data has %d elements", imagesOut[iImg].data.size)
imagesOut[iImg].attribute_string = metaXml
return imagesOut
# Create an example ROI <3
def create_example_roi(img_size):
t = np.linspace(0, 2*np.pi)
x = 16*np.power(np.sin(t), 3)
y = -13*np.cos(t) + 5*np.cos(2*t) + 2*np.cos(3*t) + np.cos(4*t)
# Place ROI in bottom right of image, offset and scaled to 10% of the image size
x = (x-np.min(x)) / (np.max(x) - np.min(x))
y = (y-np.min(y)) / (np.max(y) - np.min(y))
x = (x * 0.10*np.min(img_size[:2])) + (img_size[1]-0.2*np.min(img_size[:2]))
y = (y * 0.10*np.min(img_size[:2])) + (img_size[1]-0.2*np.min(img_size[:2]))
rgb = (1,0,0) # Red, green, blue color -- normalized to 1
thickness = 1 # Line thickness
style = 0 # Line style (0 = solid, 1 = dashed)
visibility = 1 # Line visibility (0 = false, 1 = true)
roi = mrdhelper.create_roi(x, y, rgb, thickness, style, visibility)
return roi