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

import typing
import pathlib
from styxdefs import *

EXTRACT_REGION_FROM_IMAGE_BY_MASK_METADATA = Metadata(
    id="a771c6b15a68298a66132d0c551dee738e184a1d.boutiques",
    name="ExtractRegionFromImageByMask",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_ExtractRegionFromImageByMaskParamsDictNoTag = typing.TypedDict('_ExtractRegionFromImageByMaskParamsDictNoTag', {
    "image_dimension": int,
    "input_image": InputPathType,
    "output_image": InputPathType,
    "label_mask_image": InputPathType,
    "label": typing.NotRequired[int | None],
    "pad_radius": typing.NotRequired[int | None],
})
ExtractRegionFromImageByMaskParamsDictTagged = typing.TypedDict('ExtractRegionFromImageByMaskParamsDictTagged', {
    "@type": typing.Literal["ants/ExtractRegionFromImageByMask"],
    "image_dimension": int,
    "input_image": InputPathType,
    "output_image": InputPathType,
    "label_mask_image": InputPathType,
    "label": typing.NotRequired[int | None],
    "pad_radius": typing.NotRequired[int | None],
})
ExtractRegionFromImageByMaskParamsDict = _ExtractRegionFromImageByMaskParamsDictNoTag | ExtractRegionFromImageByMaskParamsDictTagged


class ExtractRegionFromImageByMaskOutputs(typing.NamedTuple):
    """
    Output object returned when calling `ExtractRegionFromImageByMaskParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def extract_region_from_image_by_mask_params(
    image_dimension: int,
    input_image: InputPathType,
    output_image: InputPathType,
    label_mask_image: InputPathType,
    label: int | None = None,
    pad_radius: int | None = None,
) -> ExtractRegionFromImageByMaskParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image_dimension: Dimension of the input image.
        input_image: The input image from which a region will be extracted.
        output_image: The output image containing the extracted region.
        label_mask_image: The label mask image used to extract the bounding\
            box.
        label: The label value used to extract the sub-region.
        pad_radius: Optional padding radius to be added around the bounding\
            box.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/ExtractRegionFromImageByMask",
        "image_dimension": image_dimension,
        "input_image": input_image,
        "output_image": output_image,
        "label_mask_image": label_mask_image,
    }
    if label is not None:
        params["label"] = label
    if pad_radius is not None:
        params["pad_radius"] = pad_radius
    return params


def extract_region_from_image_by_mask_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ExtractRegionFromImageByMaskParamsDict` 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("input_image", None) is None:
        raise StyxValidationError("`input_image` must not be None")
    if not isinstance(params["input_image"], (pathlib.Path, str)):
        raise StyxValidationError(f'`input_image` has the wrong type: Received `{type(params.get("input_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"], (pathlib.Path, str)):
        raise StyxValidationError(f'`output_image` has the wrong type: Received `{type(params.get("output_image", None))}` expected `InputPathType`')
    if params.get("label_mask_image", None) is None:
        raise StyxValidationError("`label_mask_image` must not be None")
    if not isinstance(params["label_mask_image"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label_mask_image` has the wrong type: Received `{type(params.get("label_mask_image", None))}` expected `InputPathType`')
    if params.get("label", None) is not None:
        if not isinstance(params["label"], int):
            raise StyxValidationError(f'`label` has the wrong type: Received `{type(params.get("label", None))}` expected `int | None`')
    if params.get("pad_radius", None) is not None:
        if not isinstance(params["pad_radius"], int):
            raise StyxValidationError(f'`pad_radius` has the wrong type: Received `{type(params.get("pad_radius", None))}` expected `int | None`')


def extract_region_from_image_by_mask_cargs(
    params: ExtractRegionFromImageByMaskParamsDict,
    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("ExtractRegionFromImageByMask")
    cargs.append(str(params.get("image_dimension", None)))
    cargs.append(execution.input_file(params.get("input_image", None)))
    cargs.append(execution.input_file(params.get("output_image", None)))
    cargs.append(execution.input_file(params.get("label_mask_image", None)))
    if params.get("label", None) is not None:
        cargs.append(str(params.get("label", None)))
    if params.get("pad_radius", None) is not None:
        cargs.append(str(params.get("pad_radius", None)))
    return cargs


def extract_region_from_image_by_mask_outputs(
    params: ExtractRegionFromImageByMaskParamsDict,
    execution: Execution,
) -> ExtractRegionFromImageByMaskOutputs:
    """
    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 = ExtractRegionFromImageByMaskOutputs(
        root=execution.output_file("."),
    )
    return ret


def extract_region_from_image_by_mask_execute(
    params: ExtractRegionFromImageByMaskParamsDict,
    runner: Runner | None = None,
) -> ExtractRegionFromImageByMaskOutputs:
    """
    ExtractRegionFromImageByMask
    
    Extract a sub-region from an image using the bounding box from a label
    image, with an optional padding radius.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ExtractRegionFromImageByMaskOutputs`).
    """
    extract_region_from_image_by_mask_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(EXTRACT_REGION_FROM_IMAGE_BY_MASK_METADATA)
    params = execution.params(params)
    cargs = extract_region_from_image_by_mask_cargs(params, execution)
    ret = extract_region_from_image_by_mask_outputs(params, execution)
    execution.run(cargs)
    return ret


def extract_region_from_image_by_mask(
    image_dimension: int,
    input_image: InputPathType,
    output_image: InputPathType,
    label_mask_image: InputPathType,
    label: int | None = None,
    pad_radius: int | None = None,
    runner: Runner | None = None,
) -> ExtractRegionFromImageByMaskOutputs:
    """
    ExtractRegionFromImageByMask
    
    Extract a sub-region from an image using the bounding box from a label
    image, with an optional padding radius.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: Dimension of the input image.
        input_image: The input image from which a region will be extracted.
        output_image: The output image containing the extracted region.
        label_mask_image: The label mask image used to extract the bounding\
            box.
        label: The label value used to extract the sub-region.
        pad_radius: Optional padding radius to be added around the bounding\
            box.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ExtractRegionFromImageByMaskOutputs`).
    """
    params = extract_region_from_image_by_mask_params(
        image_dimension=image_dimension,
        input_image=input_image,
        output_image=output_image,
        label_mask_image=label_mask_image,
        label=label,
        pad_radius=pad_radius,
    )
    return extract_region_from_image_by_mask_execute(params, runner)


__all__ = [
    "EXTRACT_REGION_FROM_IMAGE_BY_MASK_METADATA",
    "ExtractRegionFromImageByMaskOutputs",
    "ExtractRegionFromImageByMaskParamsDict",
    "ExtractRegionFromImageByMaskParamsDictTagged",
    "extract_region_from_image_by_mask",
    "extract_region_from_image_by_mask_execute",
    "extract_region_from_image_by_mask_params",
]
