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

import typing
import pathlib
from styxdefs import *

EXTRACT_REGION_FROM_IMAGE_METADATA = Metadata(
    id="10d27f71688084d6c7f941dd2b6d185bedce38a8.boutiques",
    name="ExtractRegionFromImage",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_ExtractRegionFromImageRegionMinMaxIndexParamsDictNoTag = typing.TypedDict('_ExtractRegionFromImageRegionMinMaxIndexParamsDictNoTag', {
    "min_index": str,
    "max_index": str,
})
ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged = typing.TypedDict('ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged', {
    "@type": typing.Literal["region_min_max_index"],
    "min_index": str,
    "max_index": str,
})
ExtractRegionFromImageRegionMinMaxIndexParamsDict = _ExtractRegionFromImageRegionMinMaxIndexParamsDictNoTag | ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged


_ExtractRegionFromImageRegionLabelParamsDictNoTag = typing.TypedDict('_ExtractRegionFromImageRegionLabelParamsDictNoTag', {
    "label": str,
})
ExtractRegionFromImageRegionLabelParamsDictTagged = typing.TypedDict('ExtractRegionFromImageRegionLabelParamsDictTagged', {
    "@type": typing.Literal["region_label"],
    "label": str,
})
ExtractRegionFromImageRegionLabelParamsDict = _ExtractRegionFromImageRegionLabelParamsDictNoTag | ExtractRegionFromImageRegionLabelParamsDictTagged


_ExtractRegionFromImageRegionDomainImageParamsDictNoTag = typing.TypedDict('_ExtractRegionFromImageRegionDomainImageParamsDictNoTag', {
    "domain_image": InputPathType,
})
ExtractRegionFromImageRegionDomainImageParamsDictTagged = typing.TypedDict('ExtractRegionFromImageRegionDomainImageParamsDictTagged', {
    "@type": typing.Literal["region_domain_image"],
    "domain_image": InputPathType,
})
ExtractRegionFromImageRegionDomainImageParamsDict = _ExtractRegionFromImageRegionDomainImageParamsDictNoTag | ExtractRegionFromImageRegionDomainImageParamsDictTagged


_ExtractRegionFromImageRegionLabelWithImageParamsDictNoTag = typing.TypedDict('_ExtractRegionFromImageRegionLabelWithImageParamsDictNoTag', {
    "label": str,
    "label_image": InputPathType,
})
ExtractRegionFromImageRegionLabelWithImageParamsDictTagged = typing.TypedDict('ExtractRegionFromImageRegionLabelWithImageParamsDictTagged', {
    "@type": typing.Literal["region_label_with_image"],
    "label": str,
    "label_image": InputPathType,
})
ExtractRegionFromImageRegionLabelWithImageParamsDict = _ExtractRegionFromImageRegionLabelWithImageParamsDictNoTag | ExtractRegionFromImageRegionLabelWithImageParamsDictTagged


_ExtractRegionFromImageParamsDictNoTag = typing.TypedDict('_ExtractRegionFromImageParamsDictNoTag', {
    "image_dimension": int,
    "input_image": InputPathType,
    "output_image": str,
    "region_specification": typing.Union[ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged, ExtractRegionFromImageRegionLabelParamsDictTagged, ExtractRegionFromImageRegionDomainImageParamsDictTagged, ExtractRegionFromImageRegionLabelWithImageParamsDictTagged],
})
ExtractRegionFromImageParamsDictTagged = typing.TypedDict('ExtractRegionFromImageParamsDictTagged', {
    "@type": typing.Literal["ants/ExtractRegionFromImage"],
    "image_dimension": int,
    "input_image": InputPathType,
    "output_image": str,
    "region_specification": typing.Union[ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged, ExtractRegionFromImageRegionLabelParamsDictTagged, ExtractRegionFromImageRegionDomainImageParamsDictTagged, ExtractRegionFromImageRegionLabelWithImageParamsDictTagged],
})
ExtractRegionFromImageParamsDict = _ExtractRegionFromImageParamsDictNoTag | ExtractRegionFromImageParamsDictTagged


def extract_region_from_image_region_specification_cargs_dyn_fn(
    t: str,
) -> typing.Any:
    """
    Get build cargs function by command type.
    
    Args:
        t: Command type.
    Returns:
        Build cargs function.
    """
    return {
        "region_min_max_index": extract_region_from_image_region_min_max_index_cargs,
        "region_label": extract_region_from_image_region_label_cargs,
        "region_domain_image": extract_region_from_image_region_domain_image_cargs,
        "region_label_with_image": extract_region_from_image_region_label_with_image_cargs,
    }.get(t)


def extract_region_from_image_region_specification_validate_dyn_fn(
    t: str,
) -> typing.Any:
    """
    Get validate params function by command type.
    
    Args:
        t: Command type.
    Returns:
        Validate params function.
    """
    return {
        "region_min_max_index": extract_region_from_image_region_min_max_index_validate,
        "region_label": extract_region_from_image_region_label_validate,
        "region_domain_image": extract_region_from_image_region_domain_image_validate,
        "region_label_with_image": extract_region_from_image_region_label_with_image_validate,
    }.get(t)


def extract_region_from_image_region_min_max_index(
    min_index: str,
    max_index: str,
) -> ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged:
    """
    Build parameters.
    
    Args:
        min_index: Minimum index to define the starting point of the region.
        max_index: Maximum index to define the endpoint of the region.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "region_min_max_index",
        "min_index": min_index,
        "max_index": max_index,
    }
    return params


def extract_region_from_image_region_min_max_index_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ExtractRegionFromImageRegionMinMaxIndexParamsDict` 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("min_index", None) is None:
        raise StyxValidationError("`min_index` must not be None")
    if not isinstance(params["min_index"], str):
        raise StyxValidationError(f'`min_index` has the wrong type: Received `{type(params.get("min_index", None))}` expected `str`')
    if params.get("max_index", None) is None:
        raise StyxValidationError("`max_index` must not be None")
    if not isinstance(params["max_index"], str):
        raise StyxValidationError(f'`max_index` has the wrong type: Received `{type(params.get("max_index", None))}` expected `str`')


def extract_region_from_image_region_min_max_index_cargs(
    params: ExtractRegionFromImageRegionMinMaxIndexParamsDict,
    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(params.get("min_index", None))
    cargs.append(params.get("max_index", None))
    return cargs


def extract_region_from_image_region_label(
    label: str,
) -> ExtractRegionFromImageRegionLabelParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label: Label value to extract the region corresponding to the specified\
            label.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "region_label",
        "label": label,
    }
    return params


def extract_region_from_image_region_label_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ExtractRegionFromImageRegionLabelParamsDict` 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("label", None) is None:
        raise StyxValidationError("`label` must not be None")
    if not isinstance(params["label"], str):
        raise StyxValidationError(f'`label` has the wrong type: Received `{type(params.get("label", None))}` expected `str`')


def extract_region_from_image_region_label_cargs(
    params: ExtractRegionFromImageRegionLabelParamsDict,
    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(params.get("label", None))
    return cargs


def extract_region_from_image_region_domain_image(
    domain_image: InputPathType,
) -> ExtractRegionFromImageRegionDomainImageParamsDictTagged:
    """
    Build parameters.
    
    Args:
        domain_image: Image defining the domain from which to extract the\
            region.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "region_domain_image",
        "domain_image": domain_image,
    }
    return params


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


def extract_region_from_image_region_domain_image_cargs(
    params: ExtractRegionFromImageRegionDomainImageParamsDict,
    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(execution.input_file(params.get("domain_image", None)))
    return cargs


def extract_region_from_image_region_label_with_image(
    label: str,
    label_image: InputPathType,
) -> ExtractRegionFromImageRegionLabelWithImageParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label: Label value used for the region extraction.
        label_image: Image containing label information.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "region_label_with_image",
        "label": label,
        "label_image": label_image,
    }
    return params


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


def extract_region_from_image_region_label_with_image_cargs(
    params: ExtractRegionFromImageRegionLabelWithImageParamsDict,
    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(params.get("label", None))
    cargs.append(execution.input_file(params.get("label_image", None)))
    cargs.append("1")
    return cargs


class ExtractRegionFromImageOutputs(typing.NamedTuple):
    """
    Output object returned when calling `ExtractRegionFromImageParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output_image_file: OutputPathType
    """File containing the extracted region."""


def extract_region_from_image_params(
    image_dimension: int,
    input_image: InputPathType,
    output_image: str,
    region_specification: typing.Union[ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged, ExtractRegionFromImageRegionLabelParamsDictTagged, ExtractRegionFromImageRegionDomainImageParamsDictTagged, ExtractRegionFromImageRegionLabelWithImageParamsDictTagged],
) -> ExtractRegionFromImageParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image_dimension: The dimensionality of the image.
        input_image: Path to the input image from which the region will be\
            extracted.
        output_image: Path to the output image where the extracted region will\
            be saved.
        region_specification: Specify the region to extract using indices,\
            label, domain image, or label with label image.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/ExtractRegionFromImage",
        "image_dimension": image_dimension,
        "input_image": input_image,
        "output_image": output_image,
        "region_specification": region_specification,
    }
    return params


def extract_region_from_image_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ExtractRegionFromImageParamsDict` 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"], str):
        raise StyxValidationError(f'`output_image` has the wrong type: Received `{type(params.get("output_image", None))}` expected `str`')
    if params.get("region_specification", None) is None:
        raise StyxValidationError("`region_specification` must not be None")
    if not isinstance(params["region_specification"], dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params["region_specification"])}\'')
    if "@type" not in params["region_specification"]:
        raise StyxValidationError("Params object is missing `@type`")
    if params["region_specification"]["@type"] not in ["region_min_max_index", "region_label", "region_domain_image", "region_label_with_image"]:
        raise StyxValidationError("Parameter `region_specification`s `@type` must be one of [\"region_min_max_index\", \"region_label\", \"region_domain_image\", \"region_label_with_image\"]")
    extract_region_from_image_region_specification_validate_dyn_fn(params["region_specification"]["@type"])(params["region_specification"])


def extract_region_from_image_cargs(
    params: ExtractRegionFromImageParamsDict,
    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("ExtractRegionFromImage")
    cargs.append(str(params.get("image_dimension", None)))
    cargs.append(execution.input_file(params.get("input_image", None)))
    cargs.append(params.get("output_image", None))
    cargs.extend(extract_region_from_image_region_specification_cargs_dyn_fn(params.get("region_specification", None)["@type"])(params.get("region_specification", None), execution))
    return cargs


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


def extract_region_from_image_execute(
    params: ExtractRegionFromImageParamsDict,
    runner: Runner | None = None,
) -> ExtractRegionFromImageOutputs:
    """
    ExtractRegionFromImage
    
    ExtractRegionFromImage can be used to extract a specific region from a given
    image. The region can be specified via indices, label, or another domain
    image.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ExtractRegionFromImageOutputs`).
    """
    extract_region_from_image_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(EXTRACT_REGION_FROM_IMAGE_METADATA)
    params = execution.params(params)
    cargs = extract_region_from_image_cargs(params, execution)
    ret = extract_region_from_image_outputs(params, execution)
    execution.run(cargs)
    return ret


def extract_region_from_image(
    image_dimension: int,
    input_image: InputPathType,
    output_image: str,
    region_specification: typing.Union[ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged, ExtractRegionFromImageRegionLabelParamsDictTagged, ExtractRegionFromImageRegionDomainImageParamsDictTagged, ExtractRegionFromImageRegionLabelWithImageParamsDictTagged],
    runner: Runner | None = None,
) -> ExtractRegionFromImageOutputs:
    """
    ExtractRegionFromImage
    
    ExtractRegionFromImage can be used to extract a specific region from a given
    image. The region can be specified via indices, label, or another domain
    image.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: The dimensionality of the image.
        input_image: Path to the input image from which the region will be\
            extracted.
        output_image: Path to the output image where the extracted region will\
            be saved.
        region_specification: Specify the region to extract using indices,\
            label, domain image, or label with label image.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ExtractRegionFromImageOutputs`).
    """
    params = extract_region_from_image_params(
        image_dimension=image_dimension,
        input_image=input_image,
        output_image=output_image,
        region_specification=region_specification,
    )
    return extract_region_from_image_execute(params, runner)


__all__ = [
    "EXTRACT_REGION_FROM_IMAGE_METADATA",
    "ExtractRegionFromImageOutputs",
    "ExtractRegionFromImageParamsDict",
    "ExtractRegionFromImageParamsDictTagged",
    "ExtractRegionFromImageRegionDomainImageParamsDict",
    "ExtractRegionFromImageRegionDomainImageParamsDictTagged",
    "ExtractRegionFromImageRegionLabelParamsDict",
    "ExtractRegionFromImageRegionLabelParamsDictTagged",
    "ExtractRegionFromImageRegionLabelWithImageParamsDict",
    "ExtractRegionFromImageRegionLabelWithImageParamsDictTagged",
    "ExtractRegionFromImageRegionMinMaxIndexParamsDict",
    "ExtractRegionFromImageRegionMinMaxIndexParamsDictTagged",
    "extract_region_from_image",
    "extract_region_from_image_execute",
    "extract_region_from_image_params",
    "extract_region_from_image_region_domain_image",
    "extract_region_from_image_region_label",
    "extract_region_from_image_region_label_with_image",
    "extract_region_from_image_region_min_max_index",
]
