Skip to content

Commit fa4df1d

Browse files
committed
Format code with isort
- Update license year if needed - Fix some typing issues in core module - Add `is_module_installed()` method in importutil.py
1 parent 567bd34 commit fa4df1d

37 files changed

+216
-161
lines changed

examples/apps/ai_spleen_seg_app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
34
_current_dir = os.path.abspath(os.path.dirname(__file__))
45
if sys.path and os.path.abspath(sys.path[0]) != _current_dir:
56
sys.path.insert(0, _current_dir)

examples/apps/ai_spleen_seg_app/app.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
# limitations under the License.
1111

1212
import logging
13+
14+
from spleen_seg_operator import SpleenSegOperator
15+
1316
from monai.deploy.core import Application, resource
1417
from monai.deploy.operators.dicom_data_loader_operator import DICOMDataLoaderOperator
18+
from monai.deploy.operators.dicom_seg_writer_operator import DICOMSegmentationWriterOperator
1519
from monai.deploy.operators.dicom_series_selector_operator import DICOMSeriesSelectorOperator
1620
from monai.deploy.operators.dicom_series_to_volume_operator import DICOMSeriesToVolumeOperator
17-
from monai.deploy.operators.dicom_seg_writer_operator import DICOMSegmentationWriterOperator
18-
from spleen_seg_operator import SpleenSegOperator
21+
1922

2023
@resource(cpu=1, gpu=1, memory="7Gi")
2124
# pip_packages can be a string that is a path(str) to requirements.txt file or a list of packages.

examples/apps/ai_spleen_seg_app/spleen_seg_operator.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,22 @@
1111

1212
import logging
1313

14+
from monai.deploy.core import ExecutionContext, Image, InputContext, IOType, Operator, OutputContext, env, input, output
15+
from monai.deploy.operators.monai_seg_inference_operator import InMemImageReader, MonaiSegInferenceOperator
1416
from monai.transforms import (
1517
Activationsd,
1618
AsDiscreted,
1719
Compose,
20+
CropForegroundd,
1821
EnsureChannelFirstd,
1922
Invertd,
2023
LoadImaged,
21-
Spacingd,
2224
SaveImaged,
2325
ScaleIntensityRanged,
24-
CropForegroundd,
25-
ToTensord
26-
)
27-
28-
from monai.deploy.core import (
29-
ExecutionContext,
30-
Image,
31-
InputContext,
32-
IOType,
33-
Operator,
34-
OutputContext,
35-
input,
36-
output,
37-
env
26+
Spacingd,
27+
ToTensord,
3828
)
3929

40-
from monai.deploy.operators.monai_seg_inference_operator import MonaiSegInferenceOperator, InMemImageReader
4130

4231
@input("image", Image, IOType.IN_MEMORY)
4332
@output("seg_image", Image, IOType.IN_MEMORY)

examples/apps/dicom_series_to_image_app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
34
_current_dir = os.path.abspath(os.path.dirname(__file__))
45
if sys.path and os.path.abspath(sys.path[0]) != _current_dir:
56
sys.path.insert(0, _current_dir)

examples/apps/simple_imaging_app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
34
_current_dir = os.path.abspath(os.path.dirname(__file__))
45
if sys.path and os.path.abspath(sys.path[0]) != _current_dir:
56
sys.path.insert(0, _current_dir)

examples/apps/simple_imaging_app/median_operator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def compute(self, input: InputContext, output: OutputContext, context: Execution
4949

5050
# `context.input.get().path` (Path) is the file/folder path of the input data from the application's context.
5151
# `context.output.get().path` (Path) is the file/folder path of the output data from the application's context.
52-
5352
# `context.models.get(model_name)` returns a model instance
5453
# (a null model would be returned if model is not available)
5554
# If model_name is not specified and only one model exists, it returns that model.

monai/deploy/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
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.
111

212
from . import _version
13+
314
__version__ = _version.get_versions()['version']

monai/deploy/cli/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
from .main import main
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+
import os
13+
import sys
14+
15+
_current_dir = os.path.abspath(os.path.dirname(__file__))
16+
if sys.path and os.path.abspath(sys.path[0]) != _current_dir:
17+
sys.path.insert(0, _current_dir)
18+
del _current_dir

monai/deploy/cli/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 - 2021 MONAI Consortium
1+
# Copyright 2021 MONAI Consortium
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -10,7 +10,7 @@
1010
# limitations under the License.
1111

1212

13-
from . import main
13+
from main import main
1414

1515
if __name__ == "__main__":
16-
main.main()
16+
main()

monai/deploy/core/application.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Application(ABC):
5353
# This is needed to identify Application class across different environments (e.g. by `runpy.run_path()`).
5454
_class_id: str = "monai.application"
5555

56-
_env: "ApplicationEnv" = None
56+
_env: Optional["ApplicationEnv"] = None
5757

5858
def __init__(
5959
self,
@@ -179,7 +179,7 @@ def add_operator(self, operator: Operator):
179179
self._graph.add_operator(operator)
180180

181181
def add_flow(
182-
self, upstream_op: Operator, downstream_op: Operator, io_map: Optional[Dict[str, Union[str, Set]]] = None
182+
self, upstream_op: Operator, downstream_op: Operator, io_map: Optional[Dict[str, Union[str, Set[str]]]] = None
183183
):
184184
"""Adds a flow from upstream to downstream.
185185
@@ -189,8 +189,8 @@ def add_flow(
189189
Args:
190190
upstream_op (Operator): An instance of the upstream operator of type Operator.
191191
downstream_op (Operator): An instance of the downstream operator of type Operator.
192-
io_map (Optional[Dict[str, str]]): A dictionary of mapping from the source operator's label to the
193-
destination operator's label.
192+
io_map (Optional[Dict[str, Union[str, Set[str]]]]): A dictionary of mapping from the source operator's label
193+
to the destination operator's label(s).
194194
"""
195195

196196
# Ensure that the upstream and downstream operators are valid
@@ -213,12 +213,13 @@ def add_flow(
213213
io_map = {"": {""}}
214214

215215
# Convert io_map's values to the set of strings.
216+
io_maps : Dict[str, Set[str]] = io_map # type: ignore
216217
for k, v in io_map.items():
217218
if isinstance(v, str):
218-
io_map[k] = {v}
219+
io_maps[k] = {v}
219220

220221
# Verify that the upstream & downstream operator have the input and output ports specified by the io_map
221-
output_labels = list(io_map.keys())
222+
output_labels = list(io_maps.keys())
222223

223224
if len(op_output_labels) == 1 and len(output_labels) != 1:
224225
raise IOMappingError(
@@ -231,17 +232,17 @@ def add_flow(
231232
if output_label not in op_output_labels:
232233
if len(op_output_labels) == 1 and len(output_labels) == 1 and output_label == "":
233234
# Set the default output port label.
234-
io_map[next(iter(op_output_labels))] = io_map[output_label]
235-
del io_map[output_label]
235+
io_maps[next(iter(op_output_labels))] = io_maps[output_label]
236+
del io_maps[output_label]
236237
break
237238
raise IOMappingError(
238239
f"The upstream operator({upstream_op.name}) has no output port with label '{output_label}'. "
239240
f"It should be one of ({', '.join(op_output_labels)})."
240241
)
241242

242-
output_labels = list(io_map.keys()) # re-evaluate output_labels
243+
output_labels = list(io_maps.keys()) # re-evaluate output_labels
243244
for output_label in output_labels:
244-
input_labels = io_map[output_label]
245+
input_labels = io_maps[output_label]
245246

246247
if len(op_input_labels) == 1 and len(input_labels) != 1:
247248
raise IOMappingError(
@@ -262,7 +263,7 @@ def add_flow(
262263
f"It should be one of ({', '.join(op_input_labels)})."
263264
)
264265

265-
self._graph.add_flow(upstream_op, downstream_op, io_map)
266+
self._graph.add_flow(upstream_op, downstream_op, io_maps)
266267

267268
def get_package_info(self, model_path: Union[str, Path] = "") -> Dict:
268269
"""Returns the package information of this application.

monai/deploy/core/datastores/memory.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
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+
112
from typing import Any, Dict, Hashable, KeysView, Optional
213

314
from .datastore import Datastore
415

516

617
class MemoryDatastore(Datastore):
7-
def __init__(self):
18+
def __init__(self, **kwargs: Dict):
819
self._storage: Dict = {}
920

1021
def get(self, key: Hashable, def_val: Optional[Any] = None) -> Any:

monai/deploy/core/domain/dicom_series.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 - 2021 MONAI Consortium
1+
# Copyright 2021 MONAI Consortium
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -9,8 +9,6 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
13-
import math
1412
from .dicom_sop_instance import DICOMSOPInstance
1513
from .domain import Domain
1614

monai/deploy/core/domain/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 - 2021 MONAI Consortium
1+
# Copyright 2021 MONAI Consortium
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -12,7 +12,7 @@
1212
from typing import Any, Dict, Optional, Union
1313

1414
try:
15-
from numpy.typing import ArrayLike
15+
from numpy.typing import ArrayLike # type: ignore
1616
except ImportError:
1717
ArrayLike = Any
1818

monai/deploy/core/execution_context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from .operator import Operator
1818

1919
from .datastores import Datastore, MemoryDatastore
20-
from .domain import NamedDataPath
2120
from .io_context import InputContext, OutputContext
2221
from .models import Model
2322

@@ -42,7 +41,7 @@ def __init__(
4241
models: Optional[Model] = None,
4342
):
4443
if datastore is None:
45-
self._storage = MemoryDatastore()
44+
self._storage: Datastore = MemoryDatastore()
4645
else:
4746
self._storage = datastore
4847

monai/deploy/core/executors/executor.py

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

1212
from abc import ABC, abstractmethod
13-
from typing import TYPE_CHECKING, Optional
13+
from typing import TYPE_CHECKING, Dict, Optional
1414

1515
if TYPE_CHECKING:
1616
from monai.deploy.core import Application
@@ -21,7 +21,7 @@
2121
class Executor(ABC):
2222
"""This is the base class that enables execution of an application."""
2323

24-
def __init__(self, app: "Application", datastore: Optional[Datastore] = None):
24+
def __init__(self, app: "Application", datastore: Optional[Datastore] = None, **kwargs: Dict):
2525
"""Constructor of the class.
2626
2727
Given an application it invokes the compose method on the app, which

monai/deploy/core/graphs/graph.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 - 2021 MONAI Consortium
1+
# Copyright 2021 MONAI Consortium
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -10,7 +10,7 @@
1010
# limitations under the License.
1111

1212
from abc import ABC, abstractmethod
13-
from typing import Dict, Generator, Optional
13+
from typing import Dict, Generator, Optional, Set
1414

1515
from monai.deploy.core.operator import Operator
1616

@@ -24,24 +24,26 @@ def add_operator(self, op: Operator):
2424
pass
2525

2626
@abstractmethod
27-
def add_flow(self, op_u: Operator, op_v: Operator, io_map: Dict[str, str]):
27+
def add_flow(self, op_u: Operator, op_v: Operator, io_map: Dict[str, Set[str]]):
2828
"""Add an edge to the graph.
2929
3030
Args:
3131
op_u (Operator): A source operator.
3232
op_v (Operator): A destination operator.
33-
io_map (Dict[str, str]): A dictionary of mapping from the source operator's label to the destination operator's label.
33+
io_map (Dict[str, Set[str]]): A dictionary of mapping from the source operator's label to the destination
34+
operator's label(s).
3435
"""
3536
pass
3637

3738
@abstractmethod
38-
def get_io_map(self, op_u: Operator, op_v) -> Dict[str, str]:
39+
def get_io_map(self, op_u: Operator, op_v) -> Dict[str, Set[str]]:
3940
"""Get a mapping from the source operator's output label to the destination operator's input label.
4041
Args:
4142
op_u (Operator): A source operator.
4243
op_v (Operator): A destination operator.
4344
Returns:
44-
A dictionary of mapping from the source operator's output label to the destination operator's input label.
45+
A dictionary of mapping from the source operator's output label to the destination operator's
46+
input label(s).
4547
"""
4648
pass
4749

0 commit comments

Comments
 (0)