Skip to content

Commit 0081d49

Browse files
committed
Fix all type errors in existing code
Signed-off-by: Gigon Bae <[email protected]>
1 parent 74ba93c commit 0081d49

File tree

15 files changed

+219
-115
lines changed

15 files changed

+219
-115
lines changed

examples/apps/ai_spleen_seg_app/spleen_seg_operator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import logging
1313

1414
from monai.deploy.core import ExecutionContext, Image, InputContext, IOType, Operator, OutputContext, env, input, output
15+
from monai.deploy.core.domain.monai_types import ComposeInterface
1516
from monai.deploy.operators.monai_seg_inference_operator import InMemImageReader, MonaiSegInferenceOperator
1617
from monai.transforms import (
1718
Activationsd,
@@ -81,7 +82,7 @@ def compute(self, input: InputContext, output: OutputContext, context: Execution
8182
# Now let the built-in operator handles the work with the I/O spec and execution context.
8283
infer_operator.compute(input, output, context)
8384

84-
def pre_process(self, img_reader) -> Compose:
85+
def pre_process(self, img_reader) -> ComposeInterface:
8586
"""Composes transforms for preprocessing input before predicting on a model."""
8687

8788
my_key = self._input_dataset_key
@@ -96,7 +97,7 @@ def pre_process(self, img_reader) -> Compose:
9697
]
9798
)
9899

99-
def post_process(self, pre_transforms: Compose, out_dir: str = "./infer_output") -> Compose:
100+
def post_process(self, pre_transforms: ComposeInterface, out_dir: str = "./infer_output") -> ComposeInterface:
100101
"""Composes transforms for postprocessing the prediction results."""
101102

102103
pred_key = self._pred_dataset_key

monai/deploy/cli/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# limitations under the License.
1111

1212

13-
from main import main
13+
from main import main # type: ignore # for pytype
1414

1515
if __name__ == "__main__":
1616
main()

monai/deploy/core/domain/dicom_series.py

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12+
from typing import Any
13+
1214
from .dicom_sop_instance import DICOMSOPInstance
1315
from .domain import Domain
1416

@@ -21,6 +23,23 @@ def __init__(self, series_instance_uid):
2123
self._series_instance_uid = series_instance_uid
2224
self._sop_instances = []
2325

26+
self._series_date: Any = None
27+
self._series_time: Any = None
28+
self._modality: Any = None
29+
self._series_description: Any = None
30+
self._body_part_examined: Any = None
31+
self._patient_position: Any = None
32+
self._series_number: Any = None
33+
self._laterality: Any = None
34+
self._row_pixel_spacing: Any = None
35+
self._col_pixel_spacing: Any = None
36+
self._depth_pixel_spacing: Any = None
37+
self._row_direction_cosine: Any = None
38+
self._col_direction_cosine: Any = None
39+
self._depth_direction_cosine: Any = None
40+
self._dicom_affine_transform: Any = None
41+
self._nifti_affine_transform: Any = None
42+
2443
def get_series_instance_uid(self):
2544
return self._series_instance_uid
2645

@@ -33,131 +52,131 @@ def get_sop_instances(self):
3352

3453
@property
3554
def series_date(self):
36-
return self.__series_date
55+
return self._series_date
3756

3857
@series_date.setter
3958
def series_date(self, val):
40-
self.__series_date = val
59+
self._series_date = val
4160

4261
@property
4362
def series_time(self):
44-
return self.__series_time
63+
return self._series_time
4564

4665
@series_time.setter
4766
def series_time(self, val):
48-
self.__series_time = val
67+
self._series_time = val
4968

5069
@property
5170
def modality(self):
52-
return self.__modality
71+
return self._modality
5372

5473
@modality.setter
5574
def modality(self, val):
56-
self.__modality = val
75+
self._modality = val
5776

5877
@property
5978
def series_description(self):
60-
return self.__series_description
79+
return self._series_description
6180

6281
@series_description.setter
6382
def series_description(self, val):
64-
self.__series_description = val
83+
self._series_description = val
6584

6685
@property
6786
def body_part_examined(self):
68-
return self.__body_part_examined
87+
return self._body_part_examined
6988

7089
@body_part_examined.setter
7190
def body_part_examined(self, val):
72-
self.__body_part_examined = val
91+
self._body_part_examined = val
7392

7493
@property
7594
def patient_position(self):
76-
return self.__patient_position
95+
return self._patient_position
7796

7897
@patient_position.setter
7998
def patient_position(self, val):
80-
self.__patient_position = val
99+
self._patient_position = val
81100

82101
@property
83102
def series_number(self):
84-
return self.__series_number
103+
return self._series_number
85104

86105
@series_number.setter
87106
def series_number(self, val):
88-
self.__series_number = val
107+
self._series_number = val
89108

90109
@property
91110
def laterality(self):
92-
return self.__laterality
111+
return self._laterality
93112

94113
@laterality.setter
95114
def laterality(self, val):
96-
self.__laterality = val
115+
self._laterality = val
97116

98117
@property
99118
def row_pixel_spacing(self):
100-
return self.__row_pixel_spacing
119+
return self._row_pixel_spacing
101120

102121
@row_pixel_spacing.setter
103122
def row_pixel_spacing(self, val):
104-
self.__row_pixel_spacing = val
123+
self._row_pixel_spacing = val
105124

106125
@property
107126
def col_pixel_spacing(self):
108-
return self.__col_pixel_spacing
127+
return self._col_pixel_spacing
109128

110129
@col_pixel_spacing.setter
111130
def col_pixel_spacing(self, val):
112-
self.__col_pixel_spacing = val
131+
self._col_pixel_spacing = val
113132

114133
@property
115134
def depth_pixel_spacing(self):
116-
return self.__depth_pixel_spacing
135+
return self._depth_pixel_spacing
117136

118137
@depth_pixel_spacing.setter
119138
def depth_pixel_spacing(self, val):
120-
self.__depth_pixel_spacing = val
139+
self._depth_pixel_spacing = val
121140

122141
@property
123142
def row_direction_cosine(self):
124-
return self.__row_direction_cosine
143+
return self._row_direction_cosine
125144

126145
@row_direction_cosine.setter
127146
def row_direction_cosine(self, val):
128-
self.__row_direction_cosine = val
147+
self._row_direction_cosine = val
129148

130149
@property
131150
def col_direction_cosine(self):
132-
return self.__col_direction_cosine
151+
return self._col_direction_cosine
133152

134153
@col_direction_cosine.setter
135154
def col_direction_cosine(self, val):
136-
self.__col_direction_cosine = val
155+
self._col_direction_cosine = val
137156

138157
@property
139158
def depth_direction_cosine(self):
140-
return self.__depth_direction_cosine
159+
return self._depth_direction_cosine
141160

142161
@depth_direction_cosine.setter
143162
def depth_direction_cosine(self, val):
144-
self.__depth_direction_cosine = val
163+
self._depth_direction_cosine = val
145164

146165
@property
147166
def dicom_affine_transform(self):
148-
return self.__dicom_affine_transform
167+
return self._dicom_affine_transform
149168

150169
@dicom_affine_transform.setter
151170
def dicom_affine_transform(self, val):
152-
self.__dicom_affine_transform = val
171+
self._dicom_affine_transform = val
153172

154173
@property
155174
def nifti_affine_transform(self):
156-
return self.__nifti_affine_transform
175+
return self._nifti_affine_transform
157176

158177
@nifti_affine_transform.setter
159178
def nifti_affine_transform(self, val):
160-
self.__nifti_affine_transform = val
179+
self._nifti_affine_transform = val
161180

162181
def __str__(self):
163182
result = "---------------" + "\n"

monai/deploy/core/domain/dicom_sop_instance.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,33 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
from typing import Union
12+
from typing import Generic
1313

1414
from monai.deploy.utils.importutil import optional_import
1515

16+
from .dicom_types import SopInstance_KT, SopInstance_VT, SopInstanceInterface
1617
from .domain import Domain
1718

1819
DataElement, _ = optional_import("pydicom", name="DataElement")
1920
Dataset, _ = optional_import("pydicom", name="Dataset")
20-
Tag, _ = optional_import("pydicom.tag", name="Tag")
21-
BaseTag, _ = optional_import("pydicom.tag", name="BaseTag")
22-
tag_in_exception, _ = optional_import("pydicom.tag", name="tag_in_exception")
23-
TagType, _ = optional_import("pydicom.tag", name="TagType")
21+
TagType, _ = optional_import("pydicom.tag")
2422

2523

26-
class DICOMSOPInstance(Domain):
24+
# Ignore type for Generic: https://github.com/google/pytype/issues/704
25+
class DICOMSOPInstance(Domain, Generic[SopInstance_KT, SopInstance_VT]): # type: ignore
2726
"""This class representes a SOP Instance.
2827
2928
An attribute can be looked up with a slice ([group_number, element number]).
3029
"""
3130

3231
def __init__(self, native_sop):
3332
super().__init__(None)
34-
self._sop = native_sop
33+
self._sop: SopInstanceInterface = native_sop
3534

3635
def get_native_sop_instance(self):
3736
return self._sop
3837

39-
def __getitem__(self, key: Union[int, slice, "TagType"]) -> Union["Dataset", "DataElement"]:
38+
def __getitem__(self, key: SopInstance_KT) -> SopInstance_VT:
4039
return self._sop.__getitem__(key)
4140

4241
def get_pixel_array(self):

monai/deploy/core/domain/dicom_study.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# limitations under the License.
1111

1212

13+
from typing import Any
14+
1315
from .domain import Domain
1416

1517

@@ -24,6 +26,12 @@ def __init__(self, study_instance_uid):
2426
self._study_instance_uid = study_instance_uid
2527
self._series_dict = {}
2628

29+
self._study_id: Any = None
30+
self._study_date: Any = None
31+
self._study_time: Any = None
32+
self._study_description: Any = None
33+
self._accession_number: Any = None
34+
2735
def get_study_instance_uid(self):
2836
return self._study_instance_uid
2937

@@ -35,43 +43,43 @@ def get_all_series(self):
3543

3644
@property
3745
def study_id(self):
38-
return self.__study_id
46+
return self._study_id
3947

4048
@study_id.setter
4149
def study_id(self, val):
42-
self.__study_id = val
50+
self._study_id = val
4351

4452
@property
4553
def study_date(self):
46-
return self.__study_date
54+
return self._study_date
4755

4856
@study_date.setter
4957
def study_date(self, val):
50-
self.__study_date = val
58+
self._study_date = val
5159

5260
@property
5361
def study_time(self):
54-
return self.__study_time
62+
return self._study_time
5563

5664
@study_time.setter
5765
def study_time(self, val):
58-
self.__study_time = val
66+
self._study_time = val
5967

6068
@property
6169
def study_description(self):
62-
return self.__study_description
70+
return self._study_description
6371

6472
@study_description.setter
6573
def study_description(self, val):
66-
self.__study_description = val
74+
self._study_description = val
6775

6876
@property
6977
def accession_number(self):
70-
return self.__accession_number
78+
return self._accession_number
7179

7280
@accession_number.setter
7381
def accession_number(self, val):
74-
self.__accession_number = val
82+
self._accession_number = val
7583

7684
def __str__(self):
7785
result = "---------------" + "\n"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2021 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+
12+
from typing import Any, TypeVar
13+
14+
from typing_extensions import Protocol
15+
16+
17+
class TagTypeInterface(Protocol):
18+
pass
19+
20+
21+
class DatasetInterface(Protocol):
22+
pass
23+
24+
25+
class DataElementInterface(Protocol):
26+
pass
27+
28+
29+
SopInstance_KT = TypeVar("SopInstance_KT", int, slice, TagTypeInterface)
30+
SopInstance_VT = TypeVar("SopInstance_VT", DatasetInterface, DataElementInterface)
31+
32+
33+
class SopInstanceInterface(Protocol):
34+
pixel_array: Any
35+
36+
def __getitem__(self, key: SopInstance_KT) -> SopInstance_VT:
37+
...

monai/deploy/core/domain/domain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ def __init__(self, metadata: Optional[Dict] = None):
2929
else:
3030
self._metadata = {}
3131

32-
def metadata(self) -> Optional[Dict]:
32+
def metadata(self) -> Dict:
3333
return self._metadata

0 commit comments

Comments
 (0)