Skip to content

Commit e4fbd22

Browse files
committed
Why does Python think None is a type, and not treat it as null?
Signed-off-by: mmelqin <[email protected]>
1 parent efb1cb7 commit e4fbd22

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

monai/deploy/core/domain/dicom_series_selection.py

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

12-
from typing import Dict, List, Optional
12+
from typing import Dict, List, Optional, Union
1313

1414
from .dicom_series import DICOMSeries
1515
from .dicom_study import DICOMStudy
@@ -27,7 +27,7 @@ def __init__(
2727
self,
2828
selection_name: str,
2929
dicom_series: DICOMSeries,
30-
image: Image = None,
30+
image: Optional[Image] = None,
3131
metadata: Optional[Dict] = None,
3232
) -> None:
3333
"""Creates an instance of this class
@@ -45,7 +45,13 @@ def __init__(
4545
raise ValueError("Argument dicom_series must be a DICOMSeries object.")
4646
super().__init__(metadata)
4747
self._series = dicom_series
48-
self._image = image
48+
49+
if not image:
50+
self._image = None
51+
elif isinstance(image, Image):
52+
self._image = image
53+
else:
54+
raise ValueError("'image' must be an Image object if not None.")
4955

5056
if not selection_name or not selection_name.strip():
5157
self._name = str(dicom_series.SeriesInstanceUID)
@@ -61,12 +67,17 @@ def slection_name(self) -> str:
6167
return self._name
6268

6369
@property
64-
def image(self) -> Image:
70+
def image(self) -> Union[Image, None]:
6571
return self._image
6672

6773
@image.setter
68-
def image(self, val):
69-
self._image = val
74+
def image(self, val: Optional[Image]):
75+
if not val:
76+
self._image = None
77+
elif isinstance(val, Image):
78+
self._image = val
79+
else:
80+
raise ValueError("'val' must be an Image object if not None.")
7081

7182

7283
class StudySelectedSeries(Domain):

monai/deploy/operators/dicom_series_selector_operator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def filter(self, selection_rules, dicom_study_list, all_matched: bool = False) -
156156
series_list = self._select_series(conditions, study, all_matched)
157157
if series_list and len(series_list) > 0:
158158
for series in series_list:
159-
selected_series = SelectedSeries(selection_name, series)
159+
selected_series = SelectedSeries(selection_name, series, None) # No Image obj yet.
160160
study_selected_series.add_selected_series(selected_series)
161161

162162
if len(study_selected_series.selected_series) > 0:
@@ -182,7 +182,7 @@ def _select_all_series(self, dicom_study_list: List[DICOMStudy]) -> List[StudySe
182182
for series in study.get_all_series():
183183
logging.info(f"Working on series, instance UID: {str(series.SeriesInstanceUID)}")
184184
print(f"Working on series, instance UID: {str(series.SeriesInstanceUID)}")
185-
selected_series = SelectedSeries("", series) # No selection name is known or given
185+
selected_series = SelectedSeries("", series, None) # No selection name or Image obj.
186186
study_selected_series.add_selected_series(selected_series)
187187
study_selected_series_list.append(study_selected_series)
188188
return study_selected_series_list

monai/deploy/operators/dicom_series_to_volume_operator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import copy
1313
import math
14-
from typing import Dict, List
14+
from typing import Dict, List, Union
1515

1616
import numpy as np
1717

@@ -38,7 +38,7 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
3838
image = self.convert_to_image(study_selected_series_list)
3939
op_output.set(image, "image")
4040

41-
def convert_to_image(self, study_selected_series_list: List[StudySelectedSeries]) -> Image:
41+
def convert_to_image(self, study_selected_series_list: List[StudySelectedSeries]) -> Union[Image, None]:
4242
"""Extracts the pixel data from a DICOM Series and other attributes to create an Image object"""
4343
# For now, only supports the one and only one selected series.
4444
if not study_selected_series_list or len(study_selected_series_list) < 1:

0 commit comments

Comments
 (0)