|
| 1 | +# Copyright 2021-2025 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | +import logging |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +from pydicom.sr.codedict import codes # Required for setting SegmentDescription attributes. |
| 15 | +from spleen_seg_operator import SpleenSegOperator |
| 16 | + |
| 17 | +from monai.deploy.conditions import CountCondition |
| 18 | +from monai.deploy.core import Application |
| 19 | +from monai.deploy.operators.dicom_data_loader_operator import DICOMDataLoaderOperator |
| 20 | +from monai.deploy.operators.dicom_seg_writer_operator import DICOMSegmentationWriterOperator, SegmentDescription |
| 21 | +from monai.deploy.operators.dicom_series_selector_operator import DICOMSeriesSelectorOperator |
| 22 | +from monai.deploy.operators.dicom_series_to_volume_operator import DICOMSeriesToVolumeOperator |
| 23 | +from monai.deploy.operators.stl_conversion_operator import STLConversionOperator |
| 24 | + |
| 25 | + |
| 26 | +class AIRemoteInferSpleenSegApp(Application): |
| 27 | + def __init__(self, *args, **kwargs): |
| 28 | + """Creates an application instance.""" |
| 29 | + |
| 30 | + super().__init__(*args, **kwargs) |
| 31 | + self._logger = logging.getLogger("{}.{}".format(__name__, type(self).__name__)) |
| 32 | + |
| 33 | + def run(self, *args, **kwargs): |
| 34 | + # This method calls the base class to run. Can be omitted if simply calling through. |
| 35 | + self._logger.info(f"Begin {self.run.__name__}") |
| 36 | + super().run(*args, **kwargs) |
| 37 | + self._logger.info(f"End {self.run.__name__}") |
| 38 | + |
| 39 | + def compose(self): |
| 40 | + """Creates the app specific operators and chain them up in the processing DAG.""" |
| 41 | + |
| 42 | + # Use Commandline options over environment variables to init context. |
| 43 | + app_context = Application.init_app_context(self.argv) |
| 44 | + self._logger.debug(f"Begin {self.compose.__name__}") |
| 45 | + app_input_path = Path(app_context.input_path) |
| 46 | + app_output_path = Path(app_context.output_path) |
| 47 | + model_path = Path(app_context.model_path) |
| 48 | + |
| 49 | + self._logger.info(f"App input and output path: {app_input_path}, {app_output_path}") |
| 50 | + |
| 51 | + # instantiates the SDK built-in operator(s). |
| 52 | + study_loader_op = DICOMDataLoaderOperator( |
| 53 | + self, CountCondition(self, 1), input_folder=app_input_path, name="dcm_loader_op" |
| 54 | + ) |
| 55 | + series_selector_op = DICOMSeriesSelectorOperator(self, rules=Sample_Rules_Text, name="series_selector_op") |
| 56 | + series_to_vol_op = DICOMSeriesToVolumeOperator(self, name="series_to_vol_op") |
| 57 | + |
| 58 | + # Model specific inference operator, supporting MONAI transforms. |
| 59 | + spleen_seg_op = SpleenSegOperator( |
| 60 | + self, app_context=app_context, model_name="spleen_ct", model_path=model_path, name="seg_op" |
| 61 | + ) |
| 62 | + |
| 63 | + # Create DICOM Seg writer providing the required segment description for each segment with |
| 64 | + # the actual algorithm and the pertinent organ/tissue. |
| 65 | + # The segment_label, algorithm_name, and algorithm_version are limited to 64 chars. |
| 66 | + # https://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_6.2.html |
| 67 | + # User can Look up SNOMED CT codes at, e.g. |
| 68 | + # https://bioportal.bioontology.org/ontologies/SNOMEDCT |
| 69 | + |
| 70 | + _algorithm_name = "3D segmentation of the Spleen from a CT series" |
| 71 | + _algorithm_family = codes.DCM.ArtificialIntelligence |
| 72 | + _algorithm_version = "0.1.0" |
| 73 | + |
| 74 | + segment_descriptions = [ |
| 75 | + SegmentDescription( |
| 76 | + segment_label="Spleen", |
| 77 | + segmented_property_category=codes.SCT.Organ, |
| 78 | + segmented_property_type=codes.SCT.Spleen, |
| 79 | + algorithm_name=_algorithm_name, |
| 80 | + algorithm_family=_algorithm_family, |
| 81 | + algorithm_version=_algorithm_version, |
| 82 | + ), |
| 83 | + ] |
| 84 | + |
| 85 | + custom_tags = {"SeriesDescription": "AI generated Seg, not for clinical use."} |
| 86 | + |
| 87 | + dicom_seg_writer = DICOMSegmentationWriterOperator( |
| 88 | + self, |
| 89 | + segment_descriptions=segment_descriptions, |
| 90 | + custom_tags=custom_tags, |
| 91 | + output_folder=app_output_path, |
| 92 | + name="dcm_seg_writer_op", |
| 93 | + ) |
| 94 | + |
| 95 | + # Create the processing pipeline, by specifying the source and destination operators, and |
| 96 | + # ensuring the output from the former matches the input of the latter, in both name and type. |
| 97 | + self.add_flow(study_loader_op, series_selector_op, {("dicom_study_list", "dicom_study_list")}) |
| 98 | + self.add_flow( |
| 99 | + series_selector_op, series_to_vol_op, {("study_selected_series_list", "study_selected_series_list")} |
| 100 | + ) |
| 101 | + self.add_flow(series_to_vol_op, spleen_seg_op, {("image", "image")}) |
| 102 | + |
| 103 | + # Note below the dicom_seg_writer requires two inputs, each coming from a source operator. |
| 104 | + self.add_flow( |
| 105 | + series_selector_op, dicom_seg_writer, {("study_selected_series_list", "study_selected_series_list")} |
| 106 | + ) |
| 107 | + self.add_flow(spleen_seg_op, dicom_seg_writer, {("seg_image", "seg_image")}) |
| 108 | + |
| 109 | + # Create the surface mesh STL conversion operator and add it to the app execution flow, if needed, by |
| 110 | + # uncommenting the following couple lines. |
| 111 | + stl_conversion_op = STLConversionOperator( |
| 112 | + self, output_file=app_output_path.joinpath("stl/spleen.stl"), name="stl_conversion_op" |
| 113 | + ) |
| 114 | + self.add_flow(spleen_seg_op, stl_conversion_op, {("pred", "image")}) |
| 115 | + |
| 116 | + self._logger.debug(f"End {self.compose.__name__}") |
| 117 | + |
| 118 | + |
| 119 | +# This is a sample series selection rule in JSON, simply selecting CT series. |
| 120 | +# If the study has more than 1 CT series, then all of them will be selected. |
| 121 | +# Please see more detail in DICOMSeriesSelectorOperator. |
| 122 | +# For list of string values, e.g. "ImageType": ["PRIMARY", "ORIGINAL"], it is a match if all elements |
| 123 | +# are all in the multi-value attribute of the DICOM series. |
| 124 | + |
| 125 | +Sample_Rules_Text = """ |
| 126 | +{ |
| 127 | + "selections": [ |
| 128 | + { |
| 129 | + "name": "CT Series", |
| 130 | + "conditions": { |
| 131 | + "StudyDescription": "(.*?)", |
| 132 | + "Modality": "(?i)CT", |
| 133 | + "SeriesDescription": "(.*?)", |
| 134 | + "ImageType": ["PRIMARY", "ORIGINAL"] |
| 135 | + } |
| 136 | + } |
| 137 | + ] |
| 138 | +} |
| 139 | +""" |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + # Creates the app and test it standalone. |
| 143 | + AIRemoteInferSpleenSegApp().run() |
0 commit comments