Skip to content

Commit 8a2aa60

Browse files
authored
Fix all style/type errors in existing code (#111)
* Fix all type errors in existing code Signed-off-by: Gigon Bae <[email protected]> * Fix DICOM attribute handling Existing implementation did not set attributes in advance, and their availability are checked by handing AttributeError. For the reason, initializing those attributes with None caused spleen app execution failure. (All metadata dictionary in Domain class is filled with None even for non-available attributes) With this patch, the attribute's availability is checked by checking if its value is None and getters return None if attributes are not set (using getattr() method). Signed-off-by: Gigon Bae <[email protected]>
1 parent 74ba93c commit 8a2aa60

13 files changed

+201
-211
lines changed

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: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ def __init__(self, series_instance_uid):
2121
self._series_instance_uid = series_instance_uid
2222
self._sop_instances = []
2323

24+
# Do not set attributes in advance to save memory
25+
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,215 +52,188 @@ def get_sop_instances(self):
3352

3453
@property
3554
def series_date(self):
36-
return self.__series_date
55+
return getattr(self, "_series_date", None)
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 getattr(self, "_series_time", None)
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 getattr(self, "_modality", None)
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 getattr(self, "_series_description", None)
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 getattr(self, "_body_part_examined", None)
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 getattr(self, "_patient_position", None)
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 getattr(self, "_series_number", None)
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 getattr(self, "_laterality", None)
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 getattr(self, "_row_pixel_spacing", None)
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 getattr(self, "_col_pixel_spacing", None)
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 getattr(self, "_depth_pixel_spacing", None)
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 getattr(self, "_row_direction_cosine", None)
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 getattr(self, "_col_direction_cosine", None)
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 getattr(self, "_depth_direction_cosine", None)
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 getattr(self, "_dicom_affine_transform", None)
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 getattr(self, "_nifti_affine_transform", None)
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"
164183

165184
series_instance_uid_attr = "Series Instance UID: " + self._series_instance_uid + "\n"
166185
result += series_instance_uid_attr
167186

168-
try:
169-
num_sop_instances = "Num SOP Instances: " + str(len(self._sop_instances)) + "\n"
170-
result += num_sop_instances
171-
except AttributeError:
172-
pass
187+
num_sop_instances = "Num SOP Instances: " + str(len(self._sop_instances)) + "\n"
188+
result += num_sop_instances
173189

174-
try:
190+
if self.series_date is not None:
175191
series_date_attr = "Series Date: " + self.series_date + "\n"
176192
result += series_date_attr
177-
except AttributeError:
178-
pass
179193

180-
try:
194+
if self.series_time is not None:
181195
series_time_attr = "Series Time: " + self.series_time + "\n"
182196
result += series_time_attr
183-
except AttributeError:
184-
pass
185197

186-
try:
198+
if self.modality is not None:
187199
modality_attr = "Modality: " + self.modality + "\n"
188200
result += modality_attr
189-
except AttributeError:
190-
pass
191201

192-
try:
202+
if self.series_description is not None:
193203
series_desc_attr = "Series Description: " + self.series_description + "\n"
194204
result += series_desc_attr
195-
except AttributeError:
196-
pass
197205

198-
try:
206+
if self.row_pixel_spacing is not None:
199207
row_pixel_spacing_attr = "Row Pixel Spacing: " + str(self.row_pixel_spacing) + "\n"
200208
result += row_pixel_spacing_attr
201-
except AttributeError:
202-
pass
203209

204-
try:
210+
if self.col_pixel_spacing is not None:
205211
col_pixel_spacing_attr = "Column Pixel Spacing: " + str(self.col_pixel_spacing) + "\n"
206212
result += col_pixel_spacing_attr
207-
except AttributeError:
208-
pass
209213

210-
try:
214+
if self.depth_pixel_spacing is not None:
211215
depth_pixel_spacing_attr = "Depth Pixel Spacing: " + str(self.depth_pixel_spacing) + "\n"
212216
result += depth_pixel_spacing_attr
213-
except AttributeError:
214-
pass
215217

216-
try:
218+
if self.row_direction_cosine is not None:
217219
row_direction_cosine_attr = "Row Direction Cosine: " + str(self.row_direction_cosine) + "\n"
218220
result += row_direction_cosine_attr
219-
except AttributeError:
220-
pass
221221

222-
try:
222+
if self.col_direction_cosine is not None:
223223
col_direction_cosine_attr = "Column Direction Cosine: " + str(self.col_direction_cosine) + "\n"
224224
result += col_direction_cosine_attr
225-
except AttributeError:
226-
pass
227225

228-
try:
226+
if self.depth_direction_cosine is not None:
229227
depth_direction_cosine_attr = "Depth Direction Cosine: " + str(self.depth_direction_cosine) + "\n"
230228
result += depth_direction_cosine_attr
231-
except AttributeError:
232-
pass
233229

234-
try:
230+
if self.dicom_affine_transform is not None:
235231
dicom_affine_transform_attr = "DICOM affine transform: " + "\n" + str(self.dicom_affine_transform) + "\n"
236232
result += dicom_affine_transform_attr
237-
except AttributeError:
238-
pass
239233

240-
try:
234+
if self.nifti_affine_transform is not None:
241235
nifti_affine_transform_attr = "NIFTI affine transform: " + "\n" + str(self.nifti_affine_transform) + "\n"
242236
result += nifti_affine_transform_attr
243-
except AttributeError:
244-
pass
245237

246238
result += "---------------" + "\n"
247239

monai/deploy/core/domain/dicom_sop_instance.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
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 Any, Union
1313

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

1616
from .domain import Domain
1717

18-
DataElement, _ = optional_import("pydicom", name="DataElement")
19-
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")
18+
DataElement_, _ = optional_import("pydicom", name="DataElement")
19+
# Dynamic class is not handled so make it Any for now: https://github.com/python/mypy/issues/2477
20+
DataElement: Any = DataElement_
21+
Dataset_, _ = optional_import("pydicom", name="Dataset")
22+
# Dynamic class is not handled so make it Any for now: https://github.com/python/mypy/issues/2477
23+
Dataset: Any = Dataset_
24+
TagType_, _ = optional_import("pydicom.tag", name="TagType")
25+
# Dynamic class is not handled so make it Any for now: https://github.com/python/mypy/issues/2477
26+
TagType: Any = TagType_
2427

2528

2629
class DICOMSOPInstance(Domain):
@@ -31,12 +34,12 @@ class DICOMSOPInstance(Domain):
3134

3235
def __init__(self, native_sop):
3336
super().__init__(None)
34-
self._sop = native_sop
37+
self._sop: Any = native_sop
3538

3639
def get_native_sop_instance(self):
3740
return self._sop
3841

39-
def __getitem__(self, key: Union[int, slice, "TagType"]) -> Union["Dataset", "DataElement"]:
42+
def __getitem__(self, key: Union[int, slice, TagType]) -> Union[Dataset, DataElement]:
4043
return self._sop.__getitem__(key)
4144

4245
def get_pixel_array(self):

0 commit comments

Comments
 (0)