-
Notifications
You must be signed in to change notification settings - Fork 0
/
MRIConverter.py
69 lines (60 loc) · 2.6 KB
/
MRIConverter.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
import os
import emissary
import soaplib
from soaplib.core.service import soap, rpc, DefinitionBase
from soaplib.core.model.primitive import String, Integer
from soaplib.core.model.clazz import ClassModel
from soaplib.core.server import wsgi
CMD_STR = "vendors/MRIConvert-2.0.7/usr/bin/mcverter -v -f {FORMAT} -F -PatientName,-PatientId,-SeriesTime,-StudyId,-StudyDescription,-SeriesNumber,-SequenceName,-ProtocolName,-StudyDate,-SeriesDate,SeriesDescription -o {OUTPUT_PATH} {FIXED_IMAGE} {MOVING_IMAGE}"
class ConvertorResponse(ClassModel):
"""Response object holds the commandline execution response"""
statuscode = Integer
command = String
stdout = String
stderr = String
cwd = String
fixed_output_path = String
moving_output_path = String
def __init__(self, command=None):
self.command = command
self.cwd = '.'
self.statuscode = 0
self.stdout = ""
self.stderr = "Error: I'm sorry I cannot do that, Dave!"
def create_response(out):
if out:
r = ConvertorResponse(' '.join(out.command))
r.statuscode = out.status_code
r.stdout = out.std_out
r.stderr = out.std_err
return r
class MRIConverter(DefinitionBase):
@soap(String, String, String, String, _returns=ConvertorResponse)
def convert(self, img_format, fixed_image_path, moving_image_path, output_path):
command = CMD_STR.format(FORMAT=img_format,
FIXED_IMAGE=fixed_image_path,
MOVING_IMAGE=moving_image_path,
OUTPUT_PATH=output_path)
try:
out = emissary.envoy.run(command)
r = create_response(out)
fixed_filename = os.path.basename(os.path.splitext(fixed_image_path)[0])
moving_filename = os.path.basename(os.path.splitext(moving_image_path)[0])
r.fixed_output_path = os.path.join(output_path, fixed_filename + ".mhd")
r.moving_output_path = os.path.join(output_path, moving_filename + ".mhd")
return r
except OSError, e:
pass
r = ConvertorResponse(command)
r.statuscode = e.errno
return e.strerror
return r
soap_app = soaplib.core.Application([MRIConverter], 'mri', name='MRIConverter')
application = wsgi.Application(soap_app)
if __name__=='__main__':
try:
from wsgiref.simple_server import make_server
server = make_server(host='0.0.0.0', port=8080, app=application)
server.serve_forever()
except ImportError:
print "Error: example server code requires Python >= 2.5"