File size: 8,518 Bytes
a3290d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
import argparse
import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"

from comp2comp.aaa import aaa
from comp2comp.aortic_calcium import (
    aortic_calcium,
    aortic_calcium_visualization,
)
from comp2comp.contrast_phase.contrast_phase import ContrastPhaseDetection
from comp2comp.hip import hip
from comp2comp.inference_pipeline import InferencePipeline
from comp2comp.io import io
from comp2comp.liver_spleen_pancreas import (
    liver_spleen_pancreas,
    liver_spleen_pancreas_visualization,
)
from comp2comp.muscle_adipose_tissue import (
    muscle_adipose_tissue,
    muscle_adipose_tissue_visualization,
)
from comp2comp.spine import spine
from comp2comp.utils import orientation
from comp2comp.utils.process import process_3d

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"

### AAA Pipeline

def AAAPipelineBuilder(path, args):
    pipeline = InferencePipeline(
      [
        AxialCropperPipelineBuilder(path, args),
        aaa.AortaSegmentation(),
        aaa.AortaDiameter(),
        aaa.AortaMetricsSaver()
      ]
    )
    return pipeline

def MuscleAdiposeTissuePipelineBuilder(args):
    pipeline = InferencePipeline(
        [
            muscle_adipose_tissue.MuscleAdiposeTissueSegmentation(
                16, args.muscle_fat_model
            ),
            muscle_adipose_tissue.MuscleAdiposeTissuePostProcessing(),
            muscle_adipose_tissue.MuscleAdiposeTissueComputeMetrics(),
            muscle_adipose_tissue_visualization.MuscleAdiposeTissueVisualizer(),
            muscle_adipose_tissue.MuscleAdiposeTissueH5Saver(),
            muscle_adipose_tissue.MuscleAdiposeTissueMetricsSaver(),
        ]
    )
    return pipeline


def MuscleAdiposeTissueFullPipelineBuilder(args):
    pipeline = InferencePipeline(
        [io.DicomFinder(args.input_path), MuscleAdiposeTissuePipelineBuilder(args)]
    )
    return pipeline


def SpinePipelineBuilder(path, args):
    pipeline = InferencePipeline(
        [
            io.DicomToNifti(path),
            spine.SpineSegmentation(args.spine_model, save=True),
            orientation.ToCanonical(),
            spine.SpineComputeROIs(args.spine_model),
            spine.SpineMetricsSaver(),
            spine.SpineCoronalSagittalVisualizer(format="png"),
            spine.SpineReport(format="png"),
        ]
    )
    return pipeline


def AxialCropperPipelineBuilder(path, args):
    pipeline = InferencePipeline(
        [
            io.DicomToNifti(path),
            spine.SpineSegmentation(args.spine_model),
            orientation.ToCanonical(),
            spine.AxialCropper(lower_level="L5", upper_level="L1", save=True),
        ]
    )
    return pipeline


def SpineMuscleAdiposeTissuePipelineBuilder(path, args):
    pipeline = InferencePipeline(
        [
            SpinePipelineBuilder(path, args),
            spine.SpineFindDicoms(),
            MuscleAdiposeTissuePipelineBuilder(args),
            spine.SpineMuscleAdiposeTissueReport(),
        ]
    )
    return pipeline


def LiverSpleenPancreasPipelineBuilder(path, args):
    pipeline = InferencePipeline(
        [
            io.DicomToNifti(path),
            liver_spleen_pancreas.LiverSpleenPancreasSegmentation(),
            orientation.ToCanonical(),
            liver_spleen_pancreas_visualization.LiverSpleenPancreasVisualizer(),
            liver_spleen_pancreas_visualization.LiverSpleenPancreasMetricsPrinter(),
        ]
    )
    return pipeline


def AorticCalciumPipelineBuilder(path, args):
    pipeline = InferencePipeline(
        [
            io.DicomToNifti(path),
            spine.SpineSegmentation(model_name="ts_spine"),
            orientation.ToCanonical(),
            aortic_calcium.AortaSegmentation(),
            orientation.ToCanonical(),
            aortic_calcium.AorticCalciumSegmentation(),
            aortic_calcium.AorticCalciumMetrics(),
            aortic_calcium_visualization.AorticCalciumVisualizer(),
            aortic_calcium_visualization.AorticCalciumPrinter(),
        ]
    )
    return pipeline


def ContrastPhasePipelineBuilder(path, args):
    pipeline = InferencePipeline([io.DicomToNifti(path), ContrastPhaseDetection(path)])
    return pipeline


def HipPipelineBuilder(path, args):
    pipeline = InferencePipeline(
        [
            io.DicomToNifti(path),
            hip.HipSegmentation(args.hip_model),
            orientation.ToCanonical(),
            hip.HipComputeROIs(args.hip_model),
            hip.HipMetricsSaver(),
            hip.HipVisualizer(),
        ]
    )
    return pipeline


def AllPipelineBuilder(path, args):
    pipeline = InferencePipeline(
        [
            io.DicomToNifti(path),
            SpineMuscleAdiposeTissuePipelineBuilder(path, args),
            LiverSpleenPancreasPipelineBuilder(path, args),
            HipPipelineBuilder(path, args),
        ]
    )
    return pipeline


def argument_parser():
    base_parser = argparse.ArgumentParser(add_help=False)
    base_parser.add_argument("--input_path", "-i", type=str, required=True)
    base_parser.add_argument("--output_path", "-o", type=str)
    base_parser.add_argument("--save_segmentations", action="store_true")
    base_parser.add_argument("--overwrite_outputs", action="store_true")

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest="pipeline", help="Pipeline to run")

    # Add the help option to each subparser
    muscle_adipose_tissue_parser = subparsers.add_parser(
        "muscle_adipose_tissue", parents=[base_parser]
    )
    muscle_adipose_tissue_parser.add_argument(
        "--muscle_fat_model", default="abCT_v0.0.1", type=str
    )

    # Spine
    spine_parser = subparsers.add_parser("spine", parents=[base_parser])
    spine_parser.add_argument("--spine_model", default="ts_spine", type=str)

    # Spine + muscle + fat
    spine_muscle_adipose_tissue_parser = subparsers.add_parser(
        "spine_muscle_adipose_tissue", parents=[base_parser]
    )
    spine_muscle_adipose_tissue_parser.add_argument(
        "--muscle_fat_model", default="stanford_v0.0.2", type=str
    )
    spine_muscle_adipose_tissue_parser.add_argument(
        "--spine_model", default="ts_spine", type=str
    )

    liver_spleen_pancreas = subparsers.add_parser(
        "liver_spleen_pancreas", parents=[base_parser]
    )

    aortic_calcium = subparsers.add_parser("aortic_calcium", parents=[base_parser])

    contrast_phase_parser = subparsers.add_parser(
        "contrast_phase", parents=[base_parser]
    )

    hip_parser = subparsers.add_parser("hip", parents=[base_parser])
    hip_parser.add_argument(
        "--hip_model",
        default="ts_hip",
        type=str,
    )

    # AAA
    aorta_diameter_parser = subparsers.add_parser("aaa", help="aorta diameter", parents=[base_parser])

    aorta_diameter_parser.add_argument(
        "--aorta_model",
        default="ts_spine",
        type=str,
        help="aorta model to use for inference",
    )

    aorta_diameter_parser.add_argument(
        "--spine_model",
        default="ts_spine",
        type=str,
        help="spine model to use for inference",
    )

    all_parser = subparsers.add_parser("all", parents=[base_parser])
    all_parser.add_argument(
        "--muscle_fat_model",
        default="abCT_v0.0.1",
        type=str,
    )
    all_parser.add_argument(
        "--spine_model",
        default="ts_spine",
        type=str,
    )
    all_parser.add_argument(
        "--hip_model",
        default="ts_hip",
        type=str,
    )
    return parser


def main():
    args = argument_parser().parse_args()
    if args.pipeline == "spine_muscle_adipose_tissue":
        process_3d(args, SpineMuscleAdiposeTissuePipelineBuilder)
    elif args.pipeline == "spine":
        process_3d(args, SpinePipelineBuilder)
    elif args.pipeline == "contrast_phase":
        process_3d(args, ContrastPhasePipelineBuilder)
    elif args.pipeline == "liver_spleen_pancreas":
        process_3d(args, LiverSpleenPancreasPipelineBuilder)
    elif args.pipeline == "aortic_calcium":
        process_3d(args, AorticCalciumPipelineBuilder)
    elif args.pipeline == "hip":
        process_3d(args, HipPipelineBuilder)
    elif args.pipeline == "aaa":
        process_3d(args, AAAPipelineBuilder)
    elif args.pipeline == "all":
        process_3d(args, AllPipelineBuilder)
    else:
        raise AssertionError("{} command not supported".format(args.action))


if __name__ == "__main__":
    main()