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

import typing
import pathlib
from styxdefs import *

WARP_TENSOR_IMAGE_MULTI_TRANSFORM_METADATA = Metadata(
    id="013f15045b6bdcf60fcdbe3fce339b08b8439b03.boutiques",
    name="WarpTensorImageMultiTransform",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_WarpTensorImageMultiTransformParamsDictNoTag = typing.TypedDict('_WarpTensorImageMultiTransformParamsDictNoTag', {
    "image_dimension": int,
    "moving_image": InputPathType,
    "output_image": str,
    "reference_image": typing.NotRequired[InputPathType | None],
    "tightest_bounding_box": bool,
    "reslice_by_header": bool,
    "use_nearest_neighbor": bool,
    "transforms": list[str],
    "ants_prefix": typing.NotRequired[str | None],
    "ants_prefix_invert": typing.NotRequired[str | None],
})
WarpTensorImageMultiTransformParamsDictTagged = typing.TypedDict('WarpTensorImageMultiTransformParamsDictTagged', {
    "@type": typing.Literal["ants/WarpTensorImageMultiTransform"],
    "image_dimension": int,
    "moving_image": InputPathType,
    "output_image": str,
    "reference_image": typing.NotRequired[InputPathType | None],
    "tightest_bounding_box": bool,
    "reslice_by_header": bool,
    "use_nearest_neighbor": bool,
    "transforms": list[str],
    "ants_prefix": typing.NotRequired[str | None],
    "ants_prefix_invert": typing.NotRequired[str | None],
})
WarpTensorImageMultiTransformParamsDict = _WarpTensorImageMultiTransformParamsDictNoTag | WarpTensorImageMultiTransformParamsDictTagged


class WarpTensorImageMultiTransformOutputs(typing.NamedTuple):
    """
    Output object returned when calling `WarpTensorImageMultiTransformParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output_image_file: OutputPathType
    """The resultant transformed output image."""


def warp_tensor_image_multi_transform_params(
    image_dimension: int,
    moving_image: InputPathType,
    output_image: str,
    transforms: list[str],
    reference_image: InputPathType | None = None,
    tightest_bounding_box: bool = False,
    reslice_by_header: bool = False,
    use_nearest_neighbor: bool = False,
    ants_prefix: str | None = None,
    ants_prefix_invert: str | None = None,
) -> WarpTensorImageMultiTransformParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image_dimension: Dimensionality of the image (e.g., 2D or 3D).
        moving_image: The moving image that will be transformed.
        output_image: Path for saving the transformed output image.
        transforms: List of transformations to apply, which can include\
            deformation fields or affine transforms.
        reference_image: Reference image for reslicing or defining the\
            transformation domain.
        tightest_bounding_box: Compute the tightest bounding box using all\
            affine transformations.
        reslice_by_header: Use the orientation matrix and origin encoded in the\
            image file header for reslicing.
        use_nearest_neighbor: Use Nearest Neighbor Interpolator for the\
            transformation.
        ants_prefix: Prefix for ANTS-generated deformation and affine\
            transformation files.
        ants_prefix_invert: Prefix for inverting ANTS-generated affine and\
            deformation transformations.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/WarpTensorImageMultiTransform",
        "image_dimension": image_dimension,
        "moving_image": moving_image,
        "output_image": output_image,
        "tightest_bounding_box": tightest_bounding_box,
        "reslice_by_header": reslice_by_header,
        "use_nearest_neighbor": use_nearest_neighbor,
        "transforms": transforms,
    }
    if reference_image is not None:
        params["reference_image"] = reference_image
    if ants_prefix is not None:
        params["ants_prefix"] = ants_prefix
    if ants_prefix_invert is not None:
        params["ants_prefix_invert"] = ants_prefix_invert
    return params


def warp_tensor_image_multi_transform_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `WarpTensorImageMultiTransformParamsDict` 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 `int`')
    if params.get("moving_image", None) is None:
        raise StyxValidationError("`moving_image` must not be None")
    if not isinstance(params["moving_image"], (pathlib.Path, str)):
        raise StyxValidationError(f'`moving_image` has the wrong type: Received `{type(params.get("moving_image", None))}` expected `InputPathType`')
    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("reference_image", None) is not None:
        if not isinstance(params["reference_image"], (pathlib.Path, str)):
            raise StyxValidationError(f'`reference_image` has the wrong type: Received `{type(params.get("reference_image", None))}` expected `InputPathType | None`')
    if params.get("tightest_bounding_box", False) is None:
        raise StyxValidationError("`tightest_bounding_box` must not be None")
    if not isinstance(params["tightest_bounding_box"], bool):
        raise StyxValidationError(f'`tightest_bounding_box` has the wrong type: Received `{type(params.get("tightest_bounding_box", False))}` expected `bool`')
    if params.get("reslice_by_header", False) is None:
        raise StyxValidationError("`reslice_by_header` must not be None")
    if not isinstance(params["reslice_by_header"], bool):
        raise StyxValidationError(f'`reslice_by_header` has the wrong type: Received `{type(params.get("reslice_by_header", False))}` expected `bool`')
    if params.get("use_nearest_neighbor", False) is None:
        raise StyxValidationError("`use_nearest_neighbor` must not be None")
    if not isinstance(params["use_nearest_neighbor"], bool):
        raise StyxValidationError(f'`use_nearest_neighbor` has the wrong type: Received `{type(params.get("use_nearest_neighbor", False))}` expected `bool`')
    if params.get("transforms", None) is None:
        raise StyxValidationError("`transforms` must not be None")
    if not isinstance(params["transforms"], list):
        raise StyxValidationError(f'`transforms` has the wrong type: Received `{type(params.get("transforms", None))}` expected `list[str]`')
    for e in params["transforms"]:
        if not isinstance(e, str):
            raise StyxValidationError(f'`transforms` has the wrong type: Received `{type(params.get("transforms", None))}` expected `list[str]`')
    if params.get("ants_prefix", None) is not None:
        if not isinstance(params["ants_prefix"], str):
            raise StyxValidationError(f'`ants_prefix` has the wrong type: Received `{type(params.get("ants_prefix", None))}` expected `str | None`')
    if params.get("ants_prefix_invert", None) is not None:
        if not isinstance(params["ants_prefix_invert"], str):
            raise StyxValidationError(f'`ants_prefix_invert` has the wrong type: Received `{type(params.get("ants_prefix_invert", None))}` expected `str | None`')


def warp_tensor_image_multi_transform_cargs(
    params: WarpTensorImageMultiTransformParamsDict,
    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("WarpTensorImageMultiTransform")
    cargs.append(str(params.get("image_dimension", None)))
    cargs.append(execution.input_file(params.get("moving_image", None)))
    cargs.append(params.get("output_image", None))
    if params.get("reference_image", None) is not None:
        cargs.extend([
            "-R",
            execution.input_file(params.get("reference_image", None))
        ])
    if params.get("tightest_bounding_box", False):
        cargs.append("--tightest-bounding-box")
    if params.get("reslice_by_header", False):
        cargs.append("--reslice-by-header")
    if params.get("use_nearest_neighbor", False):
        cargs.append("--use-NN")
    cargs.extend(params.get("transforms", None))
    if params.get("ants_prefix", None) is not None:
        cargs.extend([
            "--ANTS-prefix",
            params.get("ants_prefix", None)
        ])
    if params.get("ants_prefix_invert", None) is not None:
        cargs.extend([
            "--ANTS-prefix-invert",
            params.get("ants_prefix_invert", None)
        ])
    return cargs


def warp_tensor_image_multi_transform_outputs(
    params: WarpTensorImageMultiTransformParamsDict,
    execution: Execution,
) -> WarpTensorImageMultiTransformOutputs:
    """
    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 = WarpTensorImageMultiTransformOutputs(
        root=execution.output_file("."),
        output_image_file=execution.output_file(params.get("output_image", None)),
    )
    return ret


def warp_tensor_image_multi_transform_execute(
    params: WarpTensorImageMultiTransformParamsDict,
    runner: Runner | None = None,
) -> WarpTensorImageMultiTransformOutputs:
    """
    WarpTensorImageMultiTransform
    
    WarpTensorImageMultiTransform is used to apply transformations including
    affine and deformation fields to an image, supporting various interpolation
    techniques, image header reslicing, and compatibility with ANTS-generated
    transformations.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `WarpTensorImageMultiTransformOutputs`).
    """
    warp_tensor_image_multi_transform_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(WARP_TENSOR_IMAGE_MULTI_TRANSFORM_METADATA)
    params = execution.params(params)
    cargs = warp_tensor_image_multi_transform_cargs(params, execution)
    ret = warp_tensor_image_multi_transform_outputs(params, execution)
    execution.run(cargs)
    return ret


def warp_tensor_image_multi_transform(
    image_dimension: int,
    moving_image: InputPathType,
    output_image: str,
    transforms: list[str],
    reference_image: InputPathType | None = None,
    tightest_bounding_box: bool = False,
    reslice_by_header: bool = False,
    use_nearest_neighbor: bool = False,
    ants_prefix: str | None = None,
    ants_prefix_invert: str | None = None,
    runner: Runner | None = None,
) -> WarpTensorImageMultiTransformOutputs:
    """
    WarpTensorImageMultiTransform
    
    WarpTensorImageMultiTransform is used to apply transformations including
    affine and deformation fields to an image, supporting various interpolation
    techniques, image header reslicing, and compatibility with ANTS-generated
    transformations.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: Dimensionality of the image (e.g., 2D or 3D).
        moving_image: The moving image that will be transformed.
        output_image: Path for saving the transformed output image.
        transforms: List of transformations to apply, which can include\
            deformation fields or affine transforms.
        reference_image: Reference image for reslicing or defining the\
            transformation domain.
        tightest_bounding_box: Compute the tightest bounding box using all\
            affine transformations.
        reslice_by_header: Use the orientation matrix and origin encoded in the\
            image file header for reslicing.
        use_nearest_neighbor: Use Nearest Neighbor Interpolator for the\
            transformation.
        ants_prefix: Prefix for ANTS-generated deformation and affine\
            transformation files.
        ants_prefix_invert: Prefix for inverting ANTS-generated affine and\
            deformation transformations.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `WarpTensorImageMultiTransformOutputs`).
    """
    params = warp_tensor_image_multi_transform_params(
        image_dimension=image_dimension,
        moving_image=moving_image,
        output_image=output_image,
        reference_image=reference_image,
        tightest_bounding_box=tightest_bounding_box,
        reslice_by_header=reslice_by_header,
        use_nearest_neighbor=use_nearest_neighbor,
        transforms=transforms,
        ants_prefix=ants_prefix,
        ants_prefix_invert=ants_prefix_invert,
    )
    return warp_tensor_image_multi_transform_execute(params, runner)


__all__ = [
    "WARP_TENSOR_IMAGE_MULTI_TRANSFORM_METADATA",
    "WarpTensorImageMultiTransformOutputs",
    "WarpTensorImageMultiTransformParamsDict",
    "WarpTensorImageMultiTransformParamsDictTagged",
    "warp_tensor_image_multi_transform",
    "warp_tensor_image_multi_transform_execute",
    "warp_tensor_image_multi_transform_params",
]
