forked from kspaceKelvin/python-ismrmrd-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
471 lines (379 loc) · 20.4 KB
/
filter.py
File metadata and controls
471 lines (379 loc) · 20.4 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import ismrmrd
import os
import itertools
import logging
import traceback
import numpy as np
import numpy.fft as fft
from scipy.ndimage import median_filter
import matplotlib.pyplot as plt
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, mrdHeader):
logging.info("Config: \n%s", config)
# mrdHeader should be xml formatted MRD 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("MRD header: \n%s", mrdHeader.toxml('utf-8'))
logging.info("Incoming dataset contains %d encodings", len(mrdHeader.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",
mrdHeader.encoding[0].trajectory,
mrdHeader.encoding[0].encodedSpace.matrixSize.x,
mrdHeader.encoding[0].encodedSpace.matrixSize.y,
mrdHeader.encoding[0].encodedSpace.matrixSize.z,
mrdHeader.encoding[0].encodedSpace.fieldOfView_mm.x,
mrdHeader.encoding[0].encodedSpace.fieldOfView_mm.y,
mrdHeader.encoding[0].encodedSpace.fieldOfView_mm.z)
except:
logging.info("Improperly formatted MRD header: \n%s", mrdHeader)
# 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, mrdHeader)
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, mrdHeader)
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, mrdHeader)
connection.send_image(image)
acqGroup = []
if len(imgGroup) > 0:
logging.info("Processing a group of images (untriggered)")
image = process_image(imgGroup, connection, config, mrdHeader)
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(acqGroup, connection, config, mrdHeader):
if len(acqGroup) == 0:
return []
logging.info(f'-----------------------------------------------')
logging.info(f' process_raw called with {len(acqGroup)} readouts')
logging.info(f'-----------------------------------------------')
# 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 acqGroup]
phs = [acquisition.idx.phase for acquisition in acqGroup]
# Use the zero-padded matrix size
data = np.zeros((acqGroup[0].data.shape[0],
mrdHeader.encoding[0].encodedSpace.matrixSize.y,
mrdHeader.encoding[0].encodedSpace.matrixSize.x,
max(phs)+1),
acqGroup[0].data.dtype)
rawHead = [None]*(max(phs)+1)
for acq, lin, phs in zip(acqGroup, 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
if mrdHeader.encoding[0].reconSpace.matrixSize.x != 0:
offset = int((data.shape[1] - mrdHeader.encoding[0].reconSpace.matrixSize.x)/2)
data = data[:,offset:offset+mrdHeader.encoding[0].reconSpace.matrixSize.x]
# Remove phase oversampling
if mrdHeader.encoding[0].reconSpace.matrixSize.y != 0:
offset = int((data.shape[0] - mrdHeader.encoding[0].reconSpace.matrixSize.y)/2)
data = data[offset:offset+mrdHeader.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(mrdHeader.encoding[0].reconSpace.fieldOfView_mm.x),
ctypes.c_float(mrdHeader.encoding[0].reconSpace.fieldOfView_mm.y),
ctypes.c_float(mrdHeader.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, mrdHeader)
return imagesOut
def process_image(imgGroup, connection, config, mrdHeader):
if len(imgGroup) == 0:
return []
logging.info(f'-----------------------------------------------')
logging.info(f' process_image called with {len(imgGroup)} images')
logging.info(f'-----------------------------------------------')
# 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(imgGroup), imgGroup[0].data.dtype)
# 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 imgGroup])
head = [img.getHead() for img in imgGroup]
meta = [ismrmrd.Meta.deserialize(img.attribute_string) for img in imgGroup]
# 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 mrdhelper.get_json_config_param(config, '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(mrdHeader, "BitsStored") is not None):
BitsStored = mrdhelper.get_userParameterLong_value(mrdHeader, "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)
# Apply median filter
filterSize = mrdhelper.get_json_config_param(config, 'filterSize', default=0, type='int')
if filterSize > 0:
logging.info(f'Applying median filter with size {filterSize}')
data = median_filter(data, size=filterSize)
np.save(debugFolder + "/" + "imgFiltered.npy", data)
if mrdhelper.get_json_config_param(config, 'options') == 'rgb':
logging.info('Converting data into RGB')
if data.shape[3] != 1:
logging.error("Multi-channel data is not supported")
return []
# Normalize to (0.0, 1.0) as expected by get_cmap()
data = data.astype(np.float32)
data -= data.min()
data *= 1/data.max()
# Apply colormap
cmap = plt.get_cmap('jet')
rgb = cmap(data)
# Remove alpha channel
# Resulting shape is [row col z rgb img]
rgb = rgb[...,0:-1]
rgb = rgb.transpose((0, 1, 2, 5, 4, 3))
rgb = np.squeeze(rgb, 5)
# MRD RGB images must be uint16 in range (0, 255)
rgb *= 255
data = rgb.astype(np.uint16)
np.save(debugFolder + "/" + "imgRGB.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
if mrdhelper.get_json_config_param(config, 'options') == 'rgb':
# Set RGB parameters
oldHeader.image_type = 6 # To be defined as ismrmrd.IMTYPE_RGB
oldHeader.channels = 3 # RGB "channels". This is set by from_array, but need to be explicit as we're copying the old header instead
# 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', 'FILT']
tmpMeta['WindowCenter'] = str((maxVal+1)/2)
tmpMeta['WindowWidth'] = str((maxVal+1))
tmpMeta['SequenceDescriptionAdditional'] = 'FILT'
tmpMeta['Keep_image_geometry'] = 1
if mrdhelper.get_json_config_param(config, 'options') == 'roi':
# Example for sending ROIs
logging.info("Creating ROI_example")
tmpMeta['ROI_example'] = create_example_roi(data.shape)
if mrdhelper.get_json_config_param(config, 'options') == 'colormap':
# Example for setting colormap
tmpMeta['LUTFileName'] = 'MicroDeltaHotMetal.pal'
if mrdhelper.get_json_config_param(config, 'options') == 'rgb':
# Example for setting RGB
tmpMeta['SequenceDescriptionAdditional'] = 'FIRE_RGB'
tmpMeta['ImageProcessingHistory'].append('RGB')
# RGB images have no windowing
del tmpMeta['WindowCenter']
del tmpMeta['WindowWidth']
# RGB images shouldn't undergo further processing, e.g. orientation or distortion correction
tmpMeta['InternalSend'] = 1
# Note the filtering in the ImageComments
if filterSize > 0:
tmpMeta['ImageComments'] = f'Median filter size {filterSize}'
# Add additional comments passed from config
comments = mrdhelper.get_json_config_param(config, 'comments', default='')
if comments != '':
if tmpMeta.get('ImageComments') is None:
tmpMeta['ImageComments'] = comments
else:
tmpMeta['ImageComments'] = tmpMeta['ImageComments'] + '\n' + comments
# 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
# Send a copy of original (unmodified) images back too
if mrdhelper.get_json_config_param(config, 'sendOriginal', default=False, type='bool') == True:
stack = traceback.extract_stack()
if stack[-2].name == 'process_raw':
logging.warning('sendOriginal is true, but input was raw data, so no original images to return!')
else:
logging.info('Sending a copy of original unmodified images due to sendOriginal set to True')
# In reverse order so that they'll be in correct order as we insert them to the front of the list
for image in reversed(imgGroup):
# Create a copy to not modify the original inputs
tmpImg = image
# Change the series_index to have a different series
tmpImg.image_series_index = 99
# Ensure Keep_image_geometry is set to not reverse image orientation
tmpMeta = ismrmrd.Meta.deserialize(tmpImg.attribute_string)
tmpMeta['Keep_image_geometry'] = 1
tmpImg.attribute_string = tmpMeta.serialize()
imagesOut.insert(0, tmpImg)
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[0]-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