-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathconnection.py
executable file
·417 lines (339 loc) · 17.5 KB
/
connection.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
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
import constants
import ismrmrd
import ctypes
import os
from datetime import datetime
import h5py
import random
import threading
import logging
import socket
import numpy as np
class Connection:
def __init__(self, socket, savedata, savedataFile = "", savedataFolder = "", savedataGroup = "dataset"):
self.savedata = savedata
self.savedataFile = savedataFile
self.savedataFolder = savedataFolder
self.savedataGroup = savedataGroup
self.mrdFilePath = None
self.dset = None
self.socket = socket
self.is_exhausted = False
self.sentAcqs = 0
self.sentImages = 0
self.sentWaveforms = 0
self.recvAcqs = 0
self.recvImages = 0
self.recvWaveforms = 0
self.lock = threading.Lock()
self.handlers = {
constants.MRD_MESSAGE_CONFIG_FILE: self.read_config_file,
constants.MRD_MESSAGE_CONFIG_TEXT: self.read_config_text,
constants.MRD_MESSAGE_METADATA_XML_TEXT: self.read_metadata,
constants.MRD_MESSAGE_CLOSE: self.read_close,
constants.MRD_MESSAGE_TEXT: self.read_text,
constants.MRD_MESSAGE_ISMRMRD_ACQUISITION: self.read_acquisition,
constants.MRD_MESSAGE_ISMRMRD_WAVEFORM: self.read_waveform,
constants.MRD_MESSAGE_ISMRMRD_IMAGE: self.read_image
}
def create_save_file(self):
if self.savedata is True:
# Create savedata folder, if necessary
if ((self.savedataFolder) and (not os.path.exists(self.savedataFolder))):
os.makedirs(self.savedataFolder)
logging.debug("Created folder " + self.savedataFolder + " to save incoming data")
if (self.savedataFile):
self.mrdFilePath = self.savedataFile
else:
self.mrdFilePath = os.path.join(self.savedataFolder, "MRD_input_" + datetime.now().strftime("%Y-%m-%d-%H%M%S" + "_" + str(random.randint(0,100)) + ".h5"))
# Create HDF5 file to store incoming MRD data
logging.info("Incoming data will be saved to: '%s' in group '%s'", self.mrdFilePath, self.savedataGroup)
self.dset = ismrmrd.Dataset(self.mrdFilePath, self.savedataGroup)
self.dset._file.require_group(self.savedataGroup)
def save_additional_config(self, configAdditionalText):
if self.savedata is True:
if self.dset is None:
self.create_save_file()
self.dset._file.require_group("dataset")
dsetConfigAdditional = self.dset._dataset.require_dataset('configAdditional',shape=(1,), dtype=h5py.special_dtype(vlen=bytes))
dsetConfigAdditional[0] = bytes(configAdditionalText, 'utf-8')
def send_logging(self, level, contents):
try:
formatted_contents = "%s %s" % (level, contents)
except:
logging.warning("Unsupported logging level: " + level)
formatted_contents = contents
self.send_text(formatted_contents)
def __iter__(self):
while not self.is_exhausted:
yield self.next()
def __next__(self):
return self.next()
def read(self, nbytes):
return self.socket.recv(nbytes, socket.MSG_WAITALL)
def peek(self, nbytes):
return self.socket.recv(nbytes, socket.MSG_PEEK)
def next(self):
with self.lock:
id = self.read_mrd_message_identifier()
if (self.is_exhausted == True):
return
handler = self.handlers.get(id, lambda: Connection.unknown_message_identifier(id))
return handler()
def shutdown_close(self):
# Encapsulate shutdown in a try block because the socket may have
# already been closed on the other side
try:
self.socket.shutdown(socket.SHUT_RDWR)
except:
pass
self.socket.close()
logging.info("Socket closed")
@staticmethod
def unknown_message_identifier(identifier):
logging.error("Received unknown message type: %d", identifier)
raise StopIteration
def read_mrd_message_identifier(self):
try:
identifier_bytes = self.read(constants.SIZEOF_MRD_MESSAGE_IDENTIFIER)
except ConnectionResetError:
logging.error("Connection closed unexpectedly")
self.is_exhausted = True
return
if (len(identifier_bytes) == 0):
self.is_exhausted = True
return
return constants.MrdMessageIdentifier.unpack(identifier_bytes)[0]
def peek_mrd_message_identifier(self):
try:
identifier_bytes = self.peek(constants.SIZEOF_MRD_MESSAGE_IDENTIFIER)
except ConnectionResetError:
logging.error("Connection closed unexpectedly")
self.is_exhausted = True
return
if (len(identifier_bytes) == 0):
self.is_exhausted = True
return
return constants.MrdMessageIdentifier.unpack(identifier_bytes)[0]
def read_mrd_message_length(self):
length_bytes = self.read(constants.SIZEOF_MRD_MESSAGE_LENGTH)
return constants.MrdMessageLength.unpack(length_bytes)[0]
# ----- MRD_MESSAGE_CONFIG_FILE (1) ----------------------------------------
# This message contains the file name of a configuration file used for
# image reconstruction/post-processing. The file must exist on the server.
# Message consists of:
# ID ( 2 bytes, unsigned short)
# Config file name (1024 bytes, char )
def send_config_file(self, filename):
with self.lock:
logging.info("--> Sending MRD_MESSAGE_CONFIG_FILE (1)")
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_CONFIG_FILE))
self.socket.send(constants.MrdMessageConfigurationFile.pack(filename.encode()))
def read_config_file(self):
logging.info("<-- Received MRD_MESSAGE_CONFIG_FILE (1)")
config_file_bytes = self.read(constants.SIZEOF_MRD_MESSAGE_CONFIGURATION_FILE)
config_file = constants.MrdMessageConfigurationFile.unpack(config_file_bytes)[0]
config_file = config_file.split(b'\x00',1)[0].decode('utf-8') # Strip off null terminators in fixed 1024 size
logging.debug(" " + config_file)
if (config_file == "savedataonly"):
logging.info("Save data, but no processing based on config")
if self.savedata is True:
logging.debug("Saving data is already enabled")
else:
self.savedata = True
self.create_save_file()
if self.savedata is True:
if self.dset is None:
self.create_save_file()
self.dset._file.require_group("dataset")
dsetConfigFile = self.dset._dataset.require_dataset('config_file',shape=(1,), dtype=h5py.special_dtype(vlen=bytes))
dsetConfigFile[0] = bytes(config_file, 'utf-8')
return config_file
# ----- MRD_MESSAGE_CONFIG_TEXT (2) --------------------------------------
# This message contains the configuration information (text contents) used
# for image reconstruction/post-processing. Text is null-terminated.
# Message consists of:
# ID ( 2 bytes, unsigned short)
# Length ( 4 bytes, uint32_t )
# Config text data ( variable, char )
def send_config_text(self, contents):
with self.lock:
logging.info("--> Sending MRD_MESSAGE_CONFIG_TEXT (2)")
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_CONFIG_TEXT))
contents_with_nul = '%s\0' % contents # Add null terminator
self.socket.send(constants.MrdMessageLength.pack(len(contents_with_nul.encode())))
self.socket.send(contents_with_nul.encode())
def read_config_text(self):
logging.info("<-- Received MRD_MESSAGE_CONFIG_TEXT (2)")
length = self.read_mrd_message_length()
config = self.read(length)
config = config.split(b'\x00',1)[0].decode('utf-8') # Strip off null teminator
if self.savedata is True:
if self.dset is None:
self.create_save_file()
self.dset._file.require_group("dataset")
dsetConfig = self.dset._dataset.require_dataset('config',shape=(1,), dtype=h5py.special_dtype(vlen=bytes))
dsetConfig[0] = bytes(config, 'utf-8')
return config
# ----- MRD_MESSAGE_METADATA_XML_TEXT (3) -----------------------------------
# This message contains the metadata for the entire dataset, formatted as
# MRD XML flexible data header text. Text is null-terminated.
# Message consists of:
# ID ( 2 bytes, unsigned short)
# Length ( 4 bytes, uint32_t )
# Text xml data ( variable, char )
def send_metadata(self, contents):
with self.lock:
logging.info("--> Sending MRD_MESSAGE_METADATA_XML_TEXT (3)")
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_METADATA_XML_TEXT))
contents_with_nul = '%s\0' % contents # Add null terminator
self.socket.send(constants.MrdMessageLength.pack(len(contents_with_nul.encode())))
self.socket.send(contents_with_nul.encode())
def read_metadata(self):
logging.info("<-- Received MRD_MESSAGE_METADATA_XML_TEXT (3)")
length = self.read_mrd_message_length()
metadata = self.read(length)
metadata = metadata.split(b'\x00',1)[0].decode('utf-8') # Strip off null teminator
if self.savedata is True:
if self.dset is None:
self.create_save_file()
logging.debug(" Saving XML header to file")
self.dset.write_xml_header(bytes(metadata, 'utf-8'))
return metadata
# ----- MRD_MESSAGE_CLOSE (4) ----------------------------------------------
# This message signals that all data has been sent (either from server or client).
def send_close(self):
with self.lock:
logging.info("--> Sending MRD_MESSAGE_CLOSE (4)")
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_CLOSE))
def read_close(self):
logging.info("<-- Received MRD_MESSAGE_CLOSE (4)")
if self.savedata is True:
if self.dset is None:
self.create_save_file()
logging.debug("Closing file %s", self.dset._file.filename)
self.dset.close()
self.dset = None
self.is_exhausted = True
return
# ----- MRD_MESSAGE_TEXT (5) -----------------------------------
# This message contains arbitrary text data.
# Message consists of:
# ID ( 2 bytes, unsigned short)
# Length ( 4 bytes, uint32_t )
# Text data ( variable, char )
def send_text(self, contents):
with self.lock:
logging.info("--> Sending MRD_MESSAGE_TEXT (5)")
logging.info(" %s", contents)
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_TEXT))
contents_with_nul = '%s\0' % contents # Add null terminator
self.socket.send(constants.MrdMessageLength.pack(len(contents_with_nul.encode())))
self.socket.send(contents_with_nul.encode())
def read_text(self):
logging.info("<-- Received MRD_MESSAGE_TEXT (5)")
length = self.read_mrd_message_length()
text = self.read(length)
text = text.split(b'\x00',1)[0].decode('utf-8') # Strip off null teminator
logging.info(" %s", text)
return text
# ----- MRD_MESSAGE_ISMRMRD_ACQUISITION (1008) -----------------------------
# This message contains raw k-space data from a single readout.
# Message consists of:
# ID ( 2 bytes, unsigned short)
# Fixed header ( 340 bytes, mixed )
# Trajectory ( variable, float )
# Raw k-space data ( variable, float )
def send_acquisition(self, acquisition):
with self.lock:
self.sentAcqs += 1
if (self.sentAcqs == 1) or (self.sentAcqs % 100 == 0):
logging.info("--> Sending MRD_MESSAGE_ISMRMRD_ACQUISITION (1008) (total: %d)", self.sentAcqs)
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_ISMRMRD_ACQUISITION))
acquisition.serialize_into(self.socket.send)
def read_acquisition(self):
self.recvAcqs += 1
if (self.recvAcqs == 1) or (self.recvAcqs % 100 == 0):
logging.info("<-- Received MRD_MESSAGE_ISMRMRD_ACQUISITION (1008) (total: %d)", self.recvAcqs)
acq = ismrmrd.Acquisition.deserialize_from(self.read)
if self.savedata is True:
if self.dset is None:
self.create_save_file()
self.dset.append_acquisition(acq)
return acq
# ----- MRD_MESSAGE_ISMRMRD_IMAGE (1022) -----------------------------------
# This message contains a single [x y z cha] image.
# Message consists of:
# ID ( 2 bytes, unsigned short)
# Fixed header ( 198 bytes, mixed )
# Attribute length ( 8 bytes, uint64_t )
# Attribute data ( variable, char )
# Image data ( variable, variable )
def send_image(self, images):
with self.lock:
if not isinstance(images, list):
images = [images]
logging.info("--> Sending MRD_MESSAGE_ISMRMRD_IMAGE (1022) (%d images)", len(images))
for image in images:
if image is None:
continue
self.sentImages += 1
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_ISMRMRD_IMAGE))
image.serialize_into(self.socket.send)
# Explicit version of serialize_into() for more verbose debugging
# self.socket.send(image.getHead())
# self.socket.send(constants.MrdMessageAttribLength.pack(len(image.attribute_string)))
# self.socket.send(bytes(image.attribute_string, 'utf-8'))
# self.socket.send(bytes(image.data))
def read_image(self):
self.recvImages += 1
logging.info("<-- Received MRD_MESSAGE_ISMRMRD_IMAGE (1022)")
# return ismrmrd.Image.deserialize_from(self.read)
# Explicit version of deserialize_from() for more verbose debugging
logging.debug(" Reading in %d bytes of image header", ctypes.sizeof(ismrmrd.ImageHeader))
header_bytes = self.read(ctypes.sizeof(ismrmrd.ImageHeader))
attribute_length_bytes = self.read(ctypes.sizeof(ctypes.c_uint64))
attribute_length = ctypes.c_uint64.from_buffer_copy(attribute_length_bytes)
logging.debug(" Reading in %d bytes of attributes", attribute_length.value)
attribute_bytes = self.read(attribute_length.value)
if (attribute_length.value > 25000):
logging.debug(" Attributes (truncated): %s", attribute_bytes[0:24999].decode('utf-8'))
else:
logging.debug(" Attributes: %s", attribute_bytes.decode('utf-8'))
image = ismrmrd.Image(header_bytes, attribute_bytes.split(b'\x00',1)[0].decode('utf-8')) # Strip off null teminator
logging.info(" Image is size %d x %d x %d with %d channels of type %s", image.getHead().matrix_size[0], image.getHead().matrix_size[1], image.getHead().matrix_size[2], image.channels, ismrmrd.get_dtype_from_data_type(image.data_type))
def calculate_number_of_entries(nchannels, xs, ys, zs):
return nchannels * xs * ys * zs
nentries = calculate_number_of_entries(image.channels, *image.getHead().matrix_size)
nbytes = nentries * ismrmrd.get_dtype_from_data_type(image.data_type).itemsize
logging.debug("Reading in %d bytes of image data", nbytes)
data_bytes = self.read(nbytes)
image.data.ravel()[:] = np.frombuffer(data_bytes, dtype=ismrmrd.get_dtype_from_data_type(image.data_type))
if self.savedata is True:
if self.dset is None:
self.create_save_file()
self.dset.append_image("image_%d" % image.image_series_index, image)
return image
# ----- MRD_MESSAGE_ISMRMRD_WAVEFORM (1026) -----------------------------
# This message contains abitrary (e.g. physio) waveform data.
# Message consists of:
# ID ( 2 bytes, unsigned short)
# Fixed header ( 240 bytes, mixed )
# Waveform data ( variable, uint32_t )
def send_waveform(self, waveform):
with self.lock:
self.sentWaveforms += 1
if (self.sentWaveforms == 1) or (self.sentWaveforms % 100 == 0):
logging.info("--> Sending MRD_MESSAGE_ISMRMRD_WAVEFORM (1026) (total: %d)", self.sentWaveforms)
self.socket.send(constants.MrdMessageIdentifier.pack(constants.MRD_MESSAGE_ISMRMRD_WAVEFORM))
waveform.serialize_into(self.socket.send)
def read_waveform(self):
self.recvWaveforms += 1
if (self.recvWaveforms == 1) or (self.recvWaveforms % 100 == 0):
logging.info("<-- Received MRD_MESSAGE_ISMRMRD_WAVEFORM (1026) (total: %d)", self.recvWaveforms)
waveform = ismrmrd.Waveform.deserialize_from(self.read)
if self.savedata is True:
if self.dset is None:
self.create_save_file()
self.dset.append_waveform(waveform)
return waveform