-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (132 loc) · 5.39 KB
/
main.py
File metadata and controls
152 lines (132 loc) · 5.39 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
# Copyright 2022 Diagnostic Image Analysis Group, Radboudumc, Nijmegen, The Netherlands
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import gc
import sys
from argparse import ArgumentParser
from pathlib import Path
import SimpleITK
from src.kidney_abnormality_segmentation.config import (
default_input_path,
default_model_path,
default_output_path,
)
from src.kidney_abnormality_segmentation.postprocessing.postprocess_segmentation_mask import (
postprocess_segmentation_mask,
)
from src.kidney_abnormality_segmentation.preprocessing.extract_roi import extract_roi
from src.kidney_abnormality_segmentation.segmentation.segment_ct_image import (
segment_ct_image,
)
from src.kidney_abnormality_segmentation.utils import resample_volume, stem
def build_parser() -> ArgumentParser:
"""Create and return the CLI argument parser."""
p = ArgumentParser(description="Process images and perform segmentation")
p.add_argument(
"--use-cropping",
action="store_true",
default=False,
help="Enable ROI cropping based on TotalSegmentator (default: disabled).",
)
# Use Path typing and safer argument names (avoid shadowing builtins like `input`)
p.add_argument(
"--input-path",
type=Path,
default=default_input_path(),
help="Input folder path (defaults to /input or $INPUT_PATH).",
)
p.add_argument(
"--output-path",
type=Path,
default=default_output_path(),
help="Output folder path (defaults to /output or $OUTPUT_PATH).",
)
p.add_argument(
"--model-path",
type=Path,
default=default_model_path(),
help="Model weights folder path (defaults to /opt/ml/model or $MODEL_PATH).",
)
return p
def run():
parser = build_parser()
args = parser.parse_args()
# List all CT files under /input
ct_folder = args.input_path
if not ct_folder.exists():
raise FileNotFoundError(f"Input folder does not exist: {ct_folder}")
if not ct_folder.is_dir():
raise NotADirectoryError(f"Input path is not a directory: {ct_folder}")
try:
all_cts = list(ct_folder.rglob("*.mha")) + list(ct_folder.rglob("*.nii.gz"))
except PermissionError as e:
raise PermissionError(f"Cannot access {args.input_path}: {e}") from e
if not all_cts:
print(f"No CT files found under {ct_folder}")
sys.exit(1)
print(f"[run] Found {len(all_cts)} input CTs to process")
crop_roi = args.use_cropping
for input_ct_image_path in all_cts:
print(f"[run] Processing {input_ct_image_path.name}")
if input_ct_image_path.name.startswith("."):
print(f"[run] Skipping {input_ct_image_path.name} because not an image.")
continue
image_name = stem(str(input_ct_image_path))
file_extension = (
".mha" if str(input_ct_image_path).endswith(".mha") else ".nii.gz"
)
out_folder = args.output_path
out_folder.mkdir(parents=True, exist_ok=True)
# find output
out_path = out_folder / f"{image_name}{file_extension}"
if out_path.is_file():
print(
f"[run] Skipping {input_ct_image_path.name} because output segmentation already exists for this image."
)
continue
# 3) Decide what to hand to segment_ct_image:
# - If cropping: read into memory, crop, then pass the cropped SITK.Image.
# - If no cropping: NEVER read the full CT. Pass the filepath string instead.
orig_spacing = SimpleITK.ReadImage(str(input_ct_image_path)).GetSpacing()
if crop_roi:
print("[run] Cropping ROI; will read full CT into memory.")
full_ct = SimpleITK.ReadImage(str(input_ct_image_path))
input_for_seg = extract_roi(full_ct)
# free the full CT
del full_ct
gc.collect()
else:
print(
"[run] No cropping requested; will segment from disk without reading full CT."
)
input_for_seg = str(input_ct_image_path)
# 4) Segment (this now never loads the full on-disk CT into RAM)
print("[run] Calling segment_ct_image() …")
segmentation_sitk = segment_ct_image(input_for_seg, str(args.model_path))
# 5) Free any remaining cropped image if it was in RAM
if isinstance(input_for_seg, SimpleITK.Image):
del input_for_seg
gc.collect()
# 6) Postprocess & write out
post_sitk = postprocess_segmentation_mask(segmentation_sitk)
final_image = resample_volume(
post_sitk,
new_spacing=orig_spacing,
interpolator=SimpleITK.sitkNearestNeighbor,
)
print(f"[run] Writing final mask to: {out_path}")
SimpleITK.WriteImage(final_image, str(out_path))
print("[run] Done.")
return 0
if __name__ == "__main__":
sys.exit(run())