Project-MONAI/monai-deploy-app-sdk

[BUG]

reghbali opened this issue · 1 comments

Describe the bug
I am trying to run the simple app example in
https://docs.monai.io/projects/monai-deploy-app-sdk/en/stable/notebooks/tutorials/01_simple_app.html
and I get this error

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/monai/deploy/core/io_context.py", line 112, in set
    check_type("value", value, data_type)
TypeError: check_type() takes 2 positional arguments but 3 were given

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/lebgali/Repos/monai-deploy/app.py", line 114, in <module>
    app.run(input='./normal-brain-mri-4.png', output="output")
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/monai/deploy/core/application.py", line 429, in run
    executor_obj.run()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/monai/deploy/core/executors/single_process_executor.py", line 93, in run
    op_exec_context.input_context.set(DataPath(input_path, read_only=True), input_label)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/monai/deploy/core/io_context.py", line 114, in set
    raise IOMappingError(
monai.deploy.exceptions.IOMappingError: The data type of 'image' in the input of '<__main__.SobelOperator object at 0x136f42850>' is <class 'monai.deploy.core.domain.datapath.DataPath'>, but the value to set is the data type of <class 'monai.deploy.core.domain.datapath.DataPath'>.

Which does not make sense. It is complaining that monai.deploy.core.domain.datapath.DataPath is not the same as itself.

Steps/Code to reproduce bug

from skimage import io

import monai.deploy.core as md  # 'md' stands for MONAI Deploy (or can use 'core' instead)
from monai.deploy.core import (
    Application,
    DataPath,
    ExecutionContext,
    Image,
    InputContext,
    IOType,
    Operator,
    OutputContext,
)

@md.input("image", DataPath, IOType.DISK)
@md.output("image", Image, IOType.IN_MEMORY)
# If `pip_packages` is specified, the definition will be aggregated with the package dependency list of other
# operators and the application in packaging time.
# @md.env(pip_packages=["scikit-image >= 0.17.2"])
class SobelOperator(Operator):
    """This Operator implements a Sobel edge detector.

    It has a single input and single output.
    """

    def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):
        from skimage import filters, io

        input_path = op_input.get().path
        if input_path.is_dir():
            input_path = next(input_path.glob("*.*"))  # take the first file

        data_in = io.imread(input_path)[:, :, :3]  # discard alpha channel if exists
        data_out = filters.sobel(data_in)

        op_output.set(Image(data_out))

        
@md.input("image", Image, IOType.IN_MEMORY)
@md.output("image", Image, IOType.IN_MEMORY)
# If `pip_packages` is specified, the definition will be aggregated with the package dependency list of other
# operators and the application in packaging time.
# @md.env(pip_packages=["scikit-image >= 0.17.2"])
class MedianOperator(Operator):
    """This Operator implements a noise reduction.

    The algorithm is based on the median operator.
    It ingests a single input and provides a single output.
    """

    def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):
        from skimage.filters import median

        data_in = op_input.get().asnumpy()
        data_out = median(data_in)
        op_output.set(Image(data_out))
        
@md.input("image", Image, IOType.IN_MEMORY)
@md.output("image", DataPath, IOType.DISK)
# If `pip_packages` is specified, the definition will be aggregated with the package dependency list of other
# operators and the application in packaging time.
# @md.env(pip_packages=["scikit-image >= 0.17.2"])
class GaussianOperator(Operator):
    """This Operator implements a smoothening based on Gaussian.

    It ingests a single input and provides a single output.
    """

    def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):
        from skimage.filters import gaussian
        from skimage.io import imsave

        data_in = op_input.get().asnumpy()
        data_out = gaussian(data_in, sigma=0.2)

        output_folder = op_output.get().path
        output_path = output_folder / "final_output.png"
        imsave(output_path, data_out)
        
@md.resource(cpu=1)
# pip_packages can be a string that is a path(str) to requirements.txt file or a list of packages.
@md.env(pip_packages=["scikit-image >= 0.17.2"])
class App(Application):
    """This is a very basic application.

    This showcases the MONAI Deploy application framework.
    """

    # App's name. <class name>('App') if not specified.
    name = "simple_imaging_app"
    # App's description. <class docstring> if not specified.
    description = "This is a very simple application."
    # App's version. <git version tag> or '0.0.0' if not specified.
    version = "0.1.0"

    def compose(self):
        """This application has three operators.

        Each operator has a single input and a single output port.
        Each operator performs some kind of image processing function.
        """
        sobel_op = SobelOperator()
        median_op = MedianOperator()
        gaussian_op = GaussianOperator()

        self.add_flow(sobel_op, median_op)
        # self.add_flow(sobel_op, median_op, {"image": "image"})
        # self.add_flow(sobel_op, median_op, {"image": {"image"}})

        self.add_flow(median_op, gaussian_op)
        
if __name__ == '__main__':
    app = App()
    app.run(input='./normal-brain-mri-4.png', output="output")

Expected behavior
This is copy pasted tutorial code. I expect no error.

Environment details (please complete the following information)

  • OS/Platform: macOS 12.4
  • Python Version: 3.11.0
  • Method of MONAI Deploy App SDK install: pip
  • SDK Version: 0.5.0

Seems like typegaurd is the issue. #410