generated from FNNDSC/python-chrisapp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcm2img.py
163 lines (146 loc) · 6.45 KB
/
dcm2img.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
#!/usr/bin/env python
import sys
from pathlib import Path
from argparse import ArgumentParser, Namespace, ArgumentDefaultsHelpFormatter
from importlib.metadata import Distribution
from chris_plugin import chris_plugin, PathMapper
from med2image import med2image
import pudb
import os
__pkg = Distribution.from_name(__package__)
__version__ = __pkg.version
DISPLAY_TITLE = r"""
_ _ _____ _
| | | | / __ (_)
_ __ | |______ __| | ___ _ __ ___ `' / /'_ _ __ ___ __ _
| '_ \| |______/ _` |/ __| '_ ` _ \ / / | | '_ ` _ \ / _` |
| |_) | | | (_| | (__| | | | | |./ /__| | | | | | | (_| |
| .__/|_| \__,_|\___|_| |_| |_|\_____/_|_| |_| |_|\__, |
| | __/ |
|_| |___/
"""
parser = ArgumentParser(description='''
A ChRIS plugin that converts medical images (typically DICOM) to more
friendly JPG/PNG format. This plugin is closely related to ``pl-med2img``
and supports all the same arguments; however here the code uses a more
intelligent mapper/filter to allow for trivial in-plugin parallelization.
For more information, see https://github.com/FNNDSC/pl-dcm2img
''', formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-V', '--version',
action = 'version',
version = f'$(prog)s {__version__}'
)
parser.add_argument('-i', '--inputFile',
dest = 'inputFile',
help = 'name of the input file within the inputDir',
default = ''
)
parser.add_argument("--inputFileSubStr",
help = "input file substring to tag a file in the inputDir",
dest = 'inputFileSubStr',
default = ''
)
parser.add_argument('-o', '--outputFileStem',
dest = 'outputFileStem',
help = 'output file stem name (with optional extension)',
default = 'sample'
)
parser.add_argument('-t', '--outputFileType',
dest = 'outputFileType',
default = '',
help = 'output image file format'
)
parser.add_argument('-s', '--sliceToConvert',
dest = 'sliceToConvert',
default = "-1",
help = 'slice to convert (for 3D data)'
)
parser.add_argument('-f', '--frameToConvert',
dest = 'frameToConvert',
default = "-1",
help = 'frame to convert (for 4D data)'
)
parser.add_argument('--printElapsedTime',
dest = 'printElapsedTime',
action = 'store_true',
default = False,
help = 'print program run time'
)
parser.add_argument('-r', '--reslice',
dest = 'reslice',
action = 'store_true',
default = False,
help = 'save images along x, y, z directions -- 3D input only'
)
parser.add_argument('--showSlices',
dest = 'showSlices',
action = 'store_true',
default = False,
help = 'show slices that are converted'
)
parser.add_argument('--func',
dest = 'func',
default = '',
help = 'apply the specified transformation function before saving'
)
parser.add_argument('-y', '--synopsis',
dest = 'synopsis',
action = 'store_true',
default = False,
help = 'short synopsis'
)
parser.add_argument("--convertOnlySingleDICOM",
help = "if specified, only convert the specific input DICOM",
dest = 'convertOnlySingleDICOM',
action = 'store_true',
default = False
)
parser.add_argument("--verbosity",
help = "verbosity level",
default = '1',
dest = 'verbosity'
)
parser.add_argument("--glob",
help = "glob expression",
default = '**/',
dest = 'glob'
)
parser.add_argument('--rot',
help = "3D slice/dimenstion rotation vector",
dest = 'rot',
default = "110"
)
parser.add_argument('--rotAngle',
help = "3D slice/dimenstion rotation angle",
dest = 'rotAngle',
default = "90"
)
# documentation: https://fnndsc.github.io/chris_plugin/chris_plugin.html#chris_plugin
@chris_plugin(
parser = parser,
title = 'pl-dcm2img',
category = 'Image conversion', # ref. https://chrisstore.co/plugins
min_memory_limit = '100Mi', # supported units: Mi, Gi
min_cpu_limit = '1000m', # millicores, e.g. "1000m" = 1 CPU core
min_gpu_limit = 0 # set min_gpu_limit=1 to enable GPU
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
print(DISPLAY_TITLE, file=sys.stderr)
print('Version: %s' % f'{__version__}')
for k,v in options.__dict__.items():
print("%25s: [%s]" % (k, v))
print("")
pudb.set_trace()
mapper = PathMapper(inputdir, outputdir, glob='*/**/', only_files = False)
for input, output in mapper:
os.chdir('/' + inputdir.name)
options.inputDir = '/' + inputdir.name + '/' + input.name
options.outputDir = '/' + outputdir.name + '/' + output.name
imgConverter = med2image.object_factoryCreate(options).C_convert
if imgConverter:
imgConverter.tic()
imgConverter.run()
if options.printElapsedTime:
print("Elapsed time = %f seconds" % imgConverter.toc())
if __name__ == '__main__':
main()