monai.deploy.core.Application¶
- class monai.deploy.core.Application(runtime_env=None, do_run=False, path=None)[source]¶
Bases:
abc.ABC
This is the base application class.
All applications should be extended from this Class. The application class provides support for chaining up operators, as well as mechanism to execute the application.
Constructor for the application class.
It initializes the application’s graph, the runtime environment and the application context.
if do_run is True, it would accept user’s arguments from the application’s context and execute the application.
- Parameters
runtime_env (Optional[RuntimeEnv]) – The runtime environment to use.
do_run (bool) – Whether to run the application.
path (Optional[Union[str, Path]]) – The path to the application (Python file path). This path is used for launching the application to get the package information from monai.deploy.utils.importutil.get_application method.
Methods
__init__
([runtime_env, do_run, path])Constructor for the application class.
add_flow
(source_op, destination_op[, io_map])Adds a flow from source to destination.
add_operator
(operator)Adds an operator to the graph.
compose
()This is a method that needs to implemented by all subclasses.
get_package_info
([model_path])Returns the package information of this application.
run
([log_level, input, output, model, …])Runs the application.
Attributes
Returns the context of this application.
description
Gives access to the environment.
Gives access to the DAG.
name
version
- __init__(runtime_env=None, do_run=False, path=None)[source]¶
Constructor for the application class.
It initializes the application’s graph, the runtime environment and the application context.
if do_run is True, it would accept user’s arguments from the application’s context and execute the application.
- Parameters
runtime_env (Optional[RuntimeEnv]) – The runtime environment to use.
do_run (bool) – Whether to run the application.
path (Optional[Union[str, Path]]) – The path to the application (Python file path). This path is used for launching the application to get the package information from monai.deploy.utils.importutil.get_application method.
- add_flow(source_op, destination_op, io_map=None)[source]¶
Adds a flow from source to destination.
An output port of the source operator is connected to one of the input ports of a destination operators.
- Parameters
source_op (Operator) – An instance of the source operator of type Operator.
destination_op (Operator) – An instance of the destination operator of type Operator.
io_map (Optional[Dict[str, Union[str, Set[str]]]]) – A dictionary of mapping from the source operator’s label to the destination operator’s label(s).
- add_operator(operator)[source]¶
Adds an operator to the graph.
- Parameters
operator (Operator) – An instance of the operator of type Operator.
- abstract compose()[source]¶
This is a method that needs to implemented by all subclasses.
Derived appications will chain up the operators inside this compose method.
- property context: monai.deploy.core.app_context.AppContext¶
Returns the context of this application.
- Return type
AppContext
- property env¶
Gives access to the environment.
This sets a default value for the application’s environment if not set.
- Returns
An instance of ApplicationEnv.
- get_package_info(model_path='')[source]¶
Returns the package information of this application.
- Parameters
model_path (Union[str, Path]) – The path to the model directory.
- Return type
Dict
- Returns
A dictionary containing the package information of this application.
- property graph: monai.deploy.core.graphs.graph.Graph¶
Gives access to the DAG.
- Return type
- Returns
Instance of the DAG
- run(log_level=None, input=None, output=None, model=None, workdir=None, datastore=None, executor=None)[source]¶
Runs the application.
This method accepts log_level to set the log level of the application.
Other arguments are used to specify the input, output, model, workdir, datastore, and executor. (Cannot set graph because it is set and used by the constructor.)
If those arguments are not specified, values in the application context will be used.
This method is useful when you want to interactively run the application inside IPython (Jupyter Notebook).
For example, you can run the following code in a notebook:
>>> from pathlib import Path >>> import monai.deploy.core as md >>> from monai.deploy.core import ( >>> Application, >>> DataPath, >>> ExecutionContext, >>> InputContext, >>> IOType, >>> Operator, >>> OutputContext, >>> ) >>> >>> @md.input("path", DataPath, IOType.DISK) >>> @md.output("path", DataPath, IOType.DISK) >>> class FirstOperator(Operator): >>> def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext): >>> print(f"First Operator. input:{op_input.get().path}, model:{context.models.get().path}") >>> output_path = Path("output_first.txt") >>> output_path.write_text("first output\n") >>> output.set(DataPath(output_path)) >>> >>> @md.input("path", DataPath, IOType.DISK) >>> @md.output("path", DataPath, IOType.DISK) >>> class SecondOperator(Operator): >>> def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext): >>> print(f"First Operator. output:{op_output.get().path}, model:{context.models.get().path}") >>> # The leaf operators can only read output DataPath and should not set output DataPath. >>> output_path = op_output.get().path / "output_second.txt" >>> output_path.write_text("second output\n") >>> >>> class App(Application): >>> def compose(self): >>> first_op = FirstOperator() >>> second_op = SecondOperator() >>> >>> self.add_flow(first_op, second_op) >>> >>> if __name__ == "__main__": >>> App(do_run=True)
>>> app = App() >>> app.run(input="inp", output="out", model="model.pt")
>>> !ls out
- Parameters
log_level (Optional[str]) – A log level.
input (Optional[str]) – An input data path.
output (Optional[str]) – An output data path.
model (Optional[str]) – A model path.
workdir (Optional[str]) – A working directory path.
datastore (Optional[str]) – A datastore path.
executor (Optional[str]) – An executor name.
- Return type
None