forked from MIC-DKFZ/BodyPartRegression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starter.py
187 lines (154 loc) · 6.13 KB
/
starter.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
"""
Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany
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.
"""
from os import getenv
from os.path import join, exists
from glob import glob
from pathlib import Path
from bpreg.inference.inference_model import InferenceModel
execution_timeout = 10
# Counter to check if smth has been processed
processed_count = 0
workflow_dir = getenv("WORKFLOW_DIR", "None")
workflow_dir = workflow_dir if workflow_dir.lower() != "none" else None
assert workflow_dir is not None
batch_name = getenv("BATCH_NAME", "None")
batch_name = batch_name if batch_name.lower() != "none" else None
assert batch_name is not None
operator_in_dir = getenv("OPERATOR_IN_DIR", "None")
operator_in_dir = operator_in_dir if operator_in_dir.lower() != "none" else None
assert operator_in_dir is not None
operator_out_dir = getenv("OPERATOR_OUT_DIR", "None")
operator_out_dir = operator_out_dir if operator_out_dir.lower() != "none" else None
assert operator_out_dir is not None
gpu_available = getenv("CUDA_VISIBLE_DEVICES", False)
stringify_json = getenv("STRINGIFY_JSON", "False")
if stringify_json.lower() == "false":
stringify_json = False
else:
stringify_json = True
# File-extension to search for in the input-dir
input_file_extension = "*.nii.gz"
# How many processes should be started?
parallel_processes = 3
print("##################################################")
print("#")
print("# Starting operator xyz:")
print("#")
print(f"# workflow_dir: {workflow_dir}")
print(f"# batch_name: {batch_name}")
print(f"# operator_in_dir: {operator_in_dir}")
print(f"# operator_out_dir: {operator_out_dir}")
print(f"# gpu_available: {gpu_available}")
print(f"# stringify_json: {stringify_json}")
print("#")
print("##################################################")
print("#")
print("# Starting processing on BATCH-ELEMENT-level ...")
print("#")
print("##################################################")
print("#")
# initialize model
model_base_dir = "src/models/public_bpr_model/"
model_inference = InferenceModel(
model_base_dir, gpu=gpu_available, warning_to_error=True
)
# Loop for every batch-element (usually series)
batch_folders = [f for f in glob(join("/", workflow_dir, batch_name, "*"))]
for batch_element_dir in batch_folders:
print("#")
print("# Processing batch-element {batch_element_dir}")
print("#")
element_input_dir = join(batch_element_dir, operator_in_dir)
element_output_dir = join(batch_element_dir, operator_out_dir)
# check if input dir is present
if not exists(element_input_dir):
print("#")
print(f"# Input-dir: {element_input_dir} does not exists!")
print("# -> skipping")
print("#")
continue
# creating output dir
Path(element_output_dir).mkdir(parents=True, exist_ok=True)
# creating output dir
input_files = glob(join(element_input_dir, input_file_extension), recursive=True)
print(f"# Found {len(input_files)} input-files!")
# Single process:
# Loop for every input-file found with extension 'input_file_extension'
for input_file in input_files:
filename = input_file.split("/")[-1]
filename = filename.replace(".nii", "").replace(".gz", "") + ".json"
output_path = join(element_output_dir, filename)
model_inference.nifti2json(
nifti_path=input_file,
output_path=output_path,
stringify_json=stringify_json,
)
processed_count += 1
print("#")
print("##################################################")
print("#")
print("# BATCH-ELEMENT-level processing done.")
print("#")
print("##################################################")
print("#")
if processed_count == 0:
print("##################################################")
print("#")
print("# -> No files have been processed so far!")
print("#")
print("# Starting processing on BATCH-LEVEL ...")
print("#")
print("##################################################")
print("#")
batch_input_dir = join("/", workflow_dir, operator_in_dir)
batch_output_dir = join("/", workflow_dir, operator_in_dir)
# check if input dir present
if not exists(batch_input_dir):
print("#")
print(f"# Input-dir: {batch_input_dir} does not exists!")
print("# -> skipping")
print("#")
else:
# creating output dir
Path(batch_output_dir).mkdir(parents=True, exist_ok=True)
# creating output dir
input_files = glob(join(batch_input_dir, input_file_extension), recursive=True)
print(f"# Found {len(input_files)} input-files!")
# Single process:
# Loop for every input-file found with extension 'input_file_extension'
for input_file in input_files:
filename = input_file.split("/")[-1]
filename = filename.replace(".nii", "").replace(".gz", "") + ".json"
output_path = join(element_output_dir, filename)
model_inference.nifti2json(nifti_path=input_file, output_path=output_path)
processed_count += 1
print("#")
print("##################################################")
print("#")
print("# BATCH-LEVEL-level processing done.")
print("#")
print("##################################################")
print("#")
if processed_count == 0:
print("#")
print("##################################################")
print("#")
print("################## ERROR #######################")
print("#")
print("# ----> NO FILES HAVE BEEN PROCESSED!")
print("#")
print("##################################################")
print("#")
exit(1)
else:
print("# DONE #")