Skip to content

Commit a8d4232

Browse files
committed
Added flag to select first one or all matched series.
1 parent f79454e commit a8d4232

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

monai/deploy/operators/dicom_series_selector_operator.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,35 @@ class DICOMSeriesSelectorOperator(Operator):
4949
}
5050
"""
5151

52-
def __init__(self, rules: Text = None, *args, **kwargs):
52+
def __init__(self, rules: Text = None, all_matched: bool = False, *args, **kwargs) -> None:
5353
super().__init__(*args, **kwargs)
54+
"""Instantiate an instance.
55+
56+
Args:
57+
rules (Text): Selection rules in JSON string.
58+
all_matched (bool): Gets all matched series in a study. Defaults to False for first one only.
59+
"""
5460

5561
# Delay loading the rules as json string till compute time.
5662
self._rules_json_str = rules if rules else None
63+
self._all_matched = all_matched
5764

5865
def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):
5966
"""Performs computation for this operator."""
6067
try:
6168
dicom_study_list = op_input.get("dicom_study_list")
6269
selection_rules = self._load_rules() if self._rules_json_str else None
63-
dicom_series_list, study_selected_series = self.filter(selection_rules, dicom_study_list)
70+
dicom_series_list, study_selected_series = self.filter(selection_rules, dicom_study_list, self._all_matched)
6471
op_output.set(dicom_series_list, "dicom_series")
65-
op_output.set(study_selected_series, )
72+
op_output.set(
73+
study_selected_series,
74+
)
6675
except ItemNotExistsError:
6776
pass
6877

69-
def filter(self, selection_rules, dicom_study_list) -> Tuple[List[SelectedSeries], List[StudySelectedSeries]]:
78+
def filter(
79+
self, selection_rules, dicom_study_list, all_matched: bool = False
80+
) -> Tuple[List[SelectedSeries], List[StudySelectedSeries]]:
7081
"""Selects the series with the given matching rules.
7182
7283
If rules object is None, all series will be returned with series instance UID
@@ -78,7 +89,9 @@ def filter(self, selection_rules, dicom_study_list) -> Tuple[List[SelectedSeries
7889
String array matches as subset, case insensitive
7990
8091
Args:
81-
rules_json (object): JSON object containing the matching rules
92+
selection_rules (object): JSON object containing the matching rules.
93+
dicom_study_list (list): A list of DICOMStudiy objects.
94+
all_matched (bool): Gets all matched series in a study. Defaults to False for first one only.
8295
8396
Returns:
8497
list: A list of all selected series of type SelectedSeries.
@@ -114,7 +127,7 @@ def filter(self, selection_rules, dicom_study_list) -> Tuple[List[SelectedSeries
114127
continue
115128

116129
# Select only the first series that matches the conditions, list of one
117-
series_list = self._select_series(conditions, study, False)
130+
series_list = self._select_series(conditions, study, all_matched)
118131
if series_list and len(series_list) > 0:
119132
selected_series_list.extend(x for x in series_list) # Add each single one
120133
for series in series_list:
@@ -152,15 +165,15 @@ def _select_all_series(self, dicom_study_list: List[DICOMStudy]):
152165
study_selected_series_list.append(study_selected_series)
153166
return series_list, study_selected_series_list
154167

155-
def _select_series(self, attributes: dict, study: DICOMStudy, find_all=False):
168+
def _select_series(self, attributes: dict, study: DICOMStudy, all_matched=False):
156169
"""Finds series whose attributes match the given attributes.
157170
158171
Args:
159172
attributes (dict): Dictionary of attributes for matching
160-
find_all (bool): Find all series that match; default is False
173+
all_matched (bool): Gets all matched series in a study. Defaults to False for first one only.
161174
162175
Returns:
163-
List of DICOMSeries. At most 1 if find_all is False.
176+
List of DICOMSeries. At most one element if all_matched is False.
164177
"""
165178
assert isinstance(attributes, dict), '"attributes" must be a dict.'
166179

@@ -215,7 +228,7 @@ def _select_series(self, attributes: dict, study: DICOMStudy, find_all=False):
215228
logging.info(f"Selected Series, UID: {series.SeriesInstanceUID}")
216229
found_series.append(series)
217230

218-
if not find_all:
231+
if not all_matched:
219232
return found_series
220233

221234
return found_series
@@ -230,6 +243,7 @@ def _get_instance_properties(obj: object) -> Dict:
230243
prop_dict[attribute] = getattr(obj, attribute, None)
231244
return prop_dict
232245

246+
233247
# Module functions
234248
def _print_instance_properties(obj: object, pre_fix: str = None, print_val=True):
235249
print(f"{pre_fix}Instance of {type(obj)}")

monai/deploy/operators/dicom_series_to_volume_operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
from typing import List
1312
import copy
1413
import math
14+
from typing import List
1515

1616
import numpy as np
1717

0 commit comments

Comments
 (0)