Skip to content

Commit 5087e3b

Browse files
authored
Add configuration to allow using itk or pydicom_seg for DICOM SEG (#1854)
* Add configuration to allow using ITK or pydicom_seg for generating DICOM SEG Signed-off-by: José Frias <josefrias@bmd-software.com> * Fix mypy issue when dealing with str and converting to bool in MONAI_LABEL_USE_ITK_FOR_DICOM_SEG config Signed-off-by: José Frias <josefrias@bmd-software.com> --------- Signed-off-by: José Frias <josefrias@bmd-software.com>
1 parent db7b3ab commit 5087e3b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

monailabel/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111
import os
12+
from distutils.util import strtobool
1213
from importlib.metadata import distributions
1314
from typing import Any, Dict, List, Optional
1415

@@ -115,6 +116,10 @@ class Settings(BaseSettings):
115116
else "https://huggingface.co/facebook/sam2-hiera-large/resolve/main/sam2_hiera_l.yaml"
116117
)
117118

119+
MONAI_LABEL_USE_ITK_FOR_DICOM_SEG: bool = bool(
120+
strtobool(os.environ.get("MONAI_LABEL_USE_ITK_FOR_DICOM_SEG", "True"))
121+
)
122+
118123
model_config = SettingsConfigDict(
119124
env_file=".env",
120125
case_sensitive=True,

monailabel/datastore/utils/convert.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from monai.transforms import LoadImage
2424
from pydicom.filereader import dcmread
2525

26+
from monailabel.config import settings
2627
from monailabel.datastore.utils.colors import GENERIC_ANATOMY_COLORS
2728
from monailabel.transform.writer import write_itk
2829
from monailabel.utils.others.generic import run_command
@@ -80,7 +81,12 @@ def binary_to_image(reference_image, label, dtype=np.uint8, file_ext=".nii.gz"):
8081
return output_file
8182

8283

83-
def nifti_to_dicom_seg(series_dir, label, label_info, file_ext="*", use_itk=True) -> str:
84+
def nifti_to_dicom_seg(series_dir, label, label_info, file_ext="*", use_itk=None) -> str:
85+
86+
# Only use config if no explicit override
87+
if use_itk is None:
88+
use_itk = settings.MONAI_LABEL_USE_ITK_FOR_DICOM_SEG
89+
8490
start = time.time()
8591

8692
label_np, meta_dict = LoadImage(image_only=False)(label)

monailabel/endpoints/infer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def run_inference(
183183
suffixes = [".nii", ".nii.gz", ".nrrd"]
184184
image_path = [image_uri.replace(suffix, "") for suffix in suffixes if image_uri.endswith(suffix)][0]
185185
res_img = result.get("file") if result.get("file") else result.get("label")
186-
dicom_seg_file = nifti_to_dicom_seg(image_path, res_img, p.get("label_info"), use_itk=True)
186+
dicom_seg_file = nifti_to_dicom_seg(image_path, res_img, p.get("label_info"))
187187
result["dicom_seg"] = dicom_seg_file
188188

189189
return send_response(instance.datastore(), result, output, background_tasks)

0 commit comments

Comments
 (0)