|
12 | 12 | import os |
13 | 13 | from pathlib import Path |
14 | 14 | from random import randint |
15 | | -from typing import TYPE_CHECKING, List, Union |
| 15 | +from typing import TYPE_CHECKING, List, NamedTuple, Optional, Sequence, Union |
16 | 16 |
|
17 | 17 | import numpy as np |
| 18 | +from typeguard import typechecked |
18 | 19 |
|
19 | 20 | from monai.deploy.utils.importutil import optional_import |
20 | 21 | from monai.deploy.utils.version import get_sdk_semver |
|
39 | 40 | from monai.deploy.core.domain.dicom_series_selection import StudySelectedSeries |
40 | 41 |
|
41 | 42 |
|
| 43 | +class Code(NamedTuple): |
| 44 | + """Namedtuple for representation of a coded concept consisting of the |
| 45 | + actual code *value*, the coding *scheme designator*, the code *meaning* |
| 46 | + (and optionally the coding *scheme version*). |
| 47 | + """ |
| 48 | + value: str |
| 49 | + scheme_designator: str |
| 50 | + meaning: str |
| 51 | + scheme_version: Optional[str] = None |
| 52 | + |
| 53 | + |
| 54 | +class SegmentDescription: |
| 55 | + |
| 56 | + @typechecked |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + segment_number: int, |
| 60 | + segment_label: str, |
| 61 | + segmented_property_category: Code, |
| 62 | + segmented_property_type: Code, |
| 63 | + algorithm_name: str, |
| 64 | + algorithm_version: str, |
| 65 | + algorithm_family: Code = codes.DCM.ArtificialIntelligence, |
| 66 | + tracking_uid: Optional[str] = None, |
| 67 | + tracking_id: Optional[str] = None, |
| 68 | + anatomic_regions: Optional[Sequence[Code]] = None, |
| 69 | + primary_anatomic_structures: Optional[Sequence[Code]] = None |
| 70 | + ): |
| 71 | + self._segment_number = segment_number |
| 72 | + self._segment_label = segment_label |
| 73 | + self._segmented_property_category = pydicom_Code(*segmented_property_category) |
| 74 | + self._segmented_property_type = pydicom_Code(*segmented_property_type) |
| 75 | + self._tracking_id = tracking_id |
| 76 | + if anatomic_regions is not None: |
| 77 | + self._anatomic_regions = [pydicom_Code(*ar) for ar in anatomic_regions] |
| 78 | + else: |
| 79 | + self._anatomic_regions = None |
| 80 | + if primary_anatomic_structures is not None: |
| 81 | + self._primary_anatomic_structures = [pydicom_Code(*pas) for pas in primary_anatomic_structures] |
| 82 | + else: |
| 83 | + self._primary_anatomic_structures = None |
| 84 | + |
| 85 | + # Generate a UID if one was not provided |
| 86 | + if tracking_id is not None and tracking_uid is None: |
| 87 | + tracking_uid = hd.UID() |
| 88 | + self._tracking_uid = tracking_uid |
| 89 | + |
| 90 | + self._algorithm_identification = hd.AlgorithmIdentificationSequence( |
| 91 | + name=algorithm_name, |
| 92 | + family=algorithm_family, |
| 93 | + version=algorithm_version, |
| 94 | + ) |
| 95 | + |
| 96 | + def to_segment_description(self) -> hd.seg.SegmentDescription: |
| 97 | + return hd.seg.SegmentDescription( |
| 98 | + segment_number=self._segment_number, |
| 99 | + segment_label=self._segment_label, |
| 100 | + segmented_property_category=self._segment_label, |
| 101 | + segmented_property_type=self._segmented_property_type, |
| 102 | + algorithm_identification=self._algorithm_identification, |
| 103 | + algorithm_type="AUTOMATIC", |
| 104 | + tracking_uid=self._tracking_uid, |
| 105 | + tracking_id=self._tracking_id, |
| 106 | + anatomic_regions=self._anatomic_regions, |
| 107 | + primary_anatomic_structures=self._primary_anatomic_structures, |
| 108 | + ) |
| 109 | + |
| 110 | + |
42 | 111 | @md.input("seg_image", Image, IOType.IN_MEMORY) |
43 | 112 | @md.input("study_selected_series_list", List[StudySelectedSeries], IOType.IN_MEMORY) |
44 | 113 | @md.output("dicom_seg_instance", DataPath, IOType.DISK) |
@@ -75,7 +144,7 @@ def __init__(self, segment_descriptions: List[SegmentDescription], *args, **kwar |
75 | 144 | segmentation. |
76 | 145 | """ |
77 | 146 |
|
78 | | - self._seg_descs = segment_descriptions |
| 147 | + self._seg_descs = [sd.to_segment_description() for sd in segment_descriptions] |
79 | 148 |
|
80 | 149 | def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext): |
81 | 150 | """Performs computation for this operator and handles I/O. |
|
0 commit comments