# This file was auto generated by Styx.
# Do not edit this file directly.

import typing
import pathlib
from styxdefs import *

IMAGE_MATH_METADATA = Metadata(
    id="fadbb78c295789002a291f08bcdf90f7f7e37437.boutiques",
    name="ImageMath",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_ImageMathParamsDictNoTag = typing.TypedDict('_ImageMathParamsDictNoTag', {
    "image_dimension": typing.Literal[2, 3, 4],
    "output_image": str,
    "operations_and_inputs": str,
    "image1": InputPathType,
    "image2": typing.NotRequired[InputPathType | None],
})
ImageMathParamsDictTagged = typing.TypedDict('ImageMathParamsDictTagged', {
    "@type": typing.Literal["ants/ImageMath"],
    "image_dimension": typing.Literal[2, 3, 4],
    "output_image": str,
    "operations_and_inputs": str,
    "image1": InputPathType,
    "image2": typing.NotRequired[InputPathType | None],
})
ImageMathParamsDict = _ImageMathParamsDictNoTag | ImageMathParamsDictTagged


class ImageMathOutputs(typing.NamedTuple):
    """
    Output object returned when calling `ImageMathParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output_image: OutputPathType
    """The resulting image after processing."""


def image_math_params(
    image_dimension: typing.Literal[2, 3, 4],
    output_image: str,
    operations_and_inputs: str,
    image1: InputPathType,
    image2: InputPathType | None = None,
) -> ImageMathParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image_dimension: The dimensionality of the image. Use 2 or 3 for\
            spatial images, and 4 for 4D images like time-series data.
        output_image: The output image file resulting from the operations.
        operations_and_inputs: Mathematical operations and inputs to be applied\
            on the images.
        image1: The first input image for the operation.
        image2: The second input image for the operation, if required.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/ImageMath",
        "image_dimension": image_dimension,
        "output_image": output_image,
        "operations_and_inputs": operations_and_inputs,
        "image1": image1,
    }
    if image2 is not None:
        params["image2"] = image2
    return params


def image_math_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ImageMathParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("image_dimension", None) is None:
        raise StyxValidationError("`image_dimension` must not be None")
    if not isinstance(params["image_dimension"], int):
        raise StyxValidationError(f'`image_dimension` has the wrong type: Received `{type(params.get("image_dimension", None))}` expected `typing.Literal[2, 3, 4]`')
    if params["image_dimension"] not in [2, 3, 4]:
        raise StyxValidationError("Parameter `image_dimension` must be one of [2, 3, 4]")
    if params.get("output_image", None) is None:
        raise StyxValidationError("`output_image` must not be None")
    if not isinstance(params["output_image"], str):
        raise StyxValidationError(f'`output_image` has the wrong type: Received `{type(params.get("output_image", None))}` expected `str`')
    if params.get("operations_and_inputs", None) is None:
        raise StyxValidationError("`operations_and_inputs` must not be None")
    if not isinstance(params["operations_and_inputs"], str):
        raise StyxValidationError(f'`operations_and_inputs` has the wrong type: Received `{type(params.get("operations_and_inputs", None))}` expected `str`')
    if params.get("image1", None) is None:
        raise StyxValidationError("`image1` must not be None")
    if not isinstance(params["image1"], (pathlib.Path, str)):
        raise StyxValidationError(f'`image1` has the wrong type: Received `{type(params.get("image1", None))}` expected `InputPathType`')
    if params.get("image2", None) is not None:
        if not isinstance(params["image2"], (pathlib.Path, str)):
            raise StyxValidationError(f'`image2` has the wrong type: Received `{type(params.get("image2", None))}` expected `InputPathType | None`')


def image_math_cargs(
    params: ImageMathParamsDict,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("ImageMath")
    cargs.append(str(params.get("image_dimension", None)))
    cargs.append(params.get("output_image", None))
    cargs.append(params.get("operations_and_inputs", None))
    cargs.append(execution.input_file(params.get("image1", None)))
    if params.get("image2", None) is not None:
        cargs.append(execution.input_file(params.get("image2", None)))
    return cargs


def image_math_outputs(
    params: ImageMathParamsDict,
    execution: Execution,
) -> ImageMathOutputs:
    """
    Build outputs object containing output file paths and possibly stdout/stderr.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Outputs object.
    """
    ret = ImageMathOutputs(
        root=execution.output_file("."),
        output_image=execution.output_file(params.get("output_image", None)),
    )
    return ret


def image_math_execute(
    params: ImageMathParamsDict,
    runner: Runner | None = None,
) -> ImageMathOutputs:
    """
    ImageMath
    
    A versatile tool for performing various mathematical and manipulation
    operations on images.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ImageMathOutputs`).
    """
    image_math_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(IMAGE_MATH_METADATA)
    params = execution.params(params)
    cargs = image_math_cargs(params, execution)
    ret = image_math_outputs(params, execution)
    execution.run(cargs)
    return ret


def image_math(
    image_dimension: typing.Literal[2, 3, 4],
    output_image: str,
    operations_and_inputs: str,
    image1: InputPathType,
    image2: InputPathType | None = None,
    runner: Runner | None = None,
) -> ImageMathOutputs:
    """
    ImageMath
    
    A versatile tool for performing various mathematical and manipulation
    operations on images.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: The dimensionality of the image. Use 2 or 3 for\
            spatial images, and 4 for 4D images like time-series data.
        output_image: The output image file resulting from the operations.
        operations_and_inputs: Mathematical operations and inputs to be applied\
            on the images.
        image1: The first input image for the operation.
        image2: The second input image for the operation, if required.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ImageMathOutputs`).
    """
    params = image_math_params(
        image_dimension=image_dimension,
        output_image=output_image,
        operations_and_inputs=operations_and_inputs,
        image1=image1,
        image2=image2,
    )
    return image_math_execute(params, runner)


__all__ = [
    "IMAGE_MATH_METADATA",
    "ImageMathOutputs",
    "ImageMathParamsDict",
    "ImageMathParamsDictTagged",
    "image_math",
    "image_math_execute",
    "image_math_params",
]
