monai.deploy.operators.PNGConverterOperator

class monai.deploy.operators.PNGConverterOperator(*args, **kwargs)[source]

Bases: monai.deploy.core.operator.Operator

This operator writes out a 3D Volumtric Image to disk in a slice by slice manner

Constructor of the base operator.

It creates an instance of Data Store which holds on to all inputs and outputs relavant for this operator.

Parameters
  • args – Arguments.

  • kwargs – Keyword arguments.

Methods

__init__(*args, **kwargs)

Constructor of the base operator.

add_input(label, data_type, storage_type)

add_output(label, data_type, storage_type)

compute(op_input, op_output, context)

An abstract method that needs to be implemented by the user.

convert_and_save(image, path)

extracts the slices in originally acquired direction (often axial) and saves then in PNG format slice by slice in the specified directory

ensure_valid()

Ensures that the operator is valid.

post_compute()

This method gets executed after “compute()” of an operator is called.

pre_compute()

This method gets executed before compute() of an operator is called.

Attributes

env

Gives access to the environment.

name

Returns the name of this operator.

op_info

Retrieves the operator info.

uid

Gives access to the UID of the operator.

__init__(*args, **kwargs)

Constructor of the base operator.

It creates an instance of Data Store which holds on to all inputs and outputs relavant for this operator.

Parameters
  • args – Arguments.

  • kwargs – Keyword arguments.

compute(op_input, op_output, context)[source]

An abstract method that needs to be implemented by the user.

The original input and output paths from the CLI are available through context.input.get() and context.output.get(). The type of the returned value is DataPath.

>>> context.input.get().path   # (Path) - The input path from the application's context
>>> context.output.get().path  # (Path) - The output path from the application's context

Both “input” and “output” are arguments passed to the application when the app is set to execute. The input and output paths are in the context of the whole application, and are read only once the application starts executing. Any operators in the application workflow graph can access the context.input and access the input to the application.

context.models.get(“<model_name>”) returns a model instance and a null model would be returned if model is not available.

If <model_name> is not specified and only one model exists, it returns that model.

>>> model = context.models.get()  # a model object that inherits Model class
>>> # Get a model instance if exists
>>> if model:  # if model is not a null model
>>>     print(model.items())
>>>     # model.path for accessing the model's path
>>>     # model.name for accessing the model's name
>>>     # result = model(op_input.get().asnumpy())

Similar way, op_input.get(“<input_label>”) returns the input data of the specified label for the operator.

If <input_label> is not specified and only one input exists, it returns that input.

>>> op_input.get()  # an input object that inherits a type specified by @input decorator of the operator.

If this operator is a leaf operator in the workflow graph, then the output path of the operator (the output path in local machine that is specified by CLI) is available through op_output.get().path. Otherwise, it is expected that the output of the operator is set through op_output.set() method.

For example, if the input and output data type are both Image objects, then the following logic is expected:

>>> data_in = op_input.get().asnumpy()  # get the input data as numpy array
>>> data_out = process(data_in)         # process the input data
>>> op_output.set(Image(data_out))      # set the output data

If the operator is a leaf operator in the workflow graph and the operator output’s (<data type>, <storage type>) == (DataPath, DISK), you cannot call op_output.set() method. Instead, you can use the destination path available by op_output.get().path to store output data and the following logic is expected:

>>> output_folder = op_output.get().path                 # get the output folder path
>>> output_path = output_folder / "final_output.png"     # get the output file path
>>> imsave(output_path, data_out)                        # save the output data
Parameters
  • op_input (InputContext) – An input context for the operator.

  • op_output (OutputContext) – An output context for the operator.

  • context (ExecutionContext) – An execution context for the operator.

convert_and_save(image, path)[source]

extracts the slices in originally acquired direction (often axial) and saves then in PNG format slice by slice in the specified directory

ensure_valid()

Ensures that the operator is valid.

This method needs to be executed by add_operator() and add_flow() methods in the compose() method of the application. This sets default values for the operator in the graph if necessary. (e.g., set default value for the operator’s input port, set default value for the operator’s output port, etc.)

property env: monai.deploy.core.operator.OperatorEnv

Gives access to the environment.

This sets a default value for the operator’s environment if not set.

Return type

OperatorEnv

Returns

An instance of OperatorEnv.

property name: str

Returns the name of this operator.

Return type

str

property op_info: monai.deploy.core.operator_info.OperatorInfo

Retrieves the operator info.

Args:

Return type

OperatorInfo

Returns

An instance of OperatorInfo.

post_compute()

This method gets executed after “compute()” of an operator is called.

This is a post-execution step before the operator is done doing its main action. This needs to be overridden by a base class for any meaningful action.

pre_compute()

This method gets executed before compute() of an operator is called.

This is a preperatory step before the operator executes its main job. This needs to be overridden by a base class for any meaningful action.

property uid: uuid.UUID

Gives access to the UID of the operator.

Return type

UUID

Returns

UID of the operator.