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

import typing
import pathlib
from styxdefs import *

SET_SPACING_METADATA = Metadata(
    id="943a10a041bed9edc5b49fa9bf6fd81849eff416.boutiques",
    name="SetSpacing",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_SetSpacingParamsDictNoTag = typing.TypedDict('_SetSpacingParamsDictNoTag', {
    "dimension": int,
    "input_file": InputPathType,
    "output_file": str,
    "spacing": list[float],
})
SetSpacingParamsDictTagged = typing.TypedDict('SetSpacingParamsDictTagged', {
    "@type": typing.Literal["ants/SetSpacing"],
    "dimension": int,
    "input_file": InputPathType,
    "output_file": str,
    "spacing": list[float],
})
SetSpacingParamsDict = _SetSpacingParamsDictNoTag | SetSpacingParamsDictTagged


class SetSpacingOutputs(typing.NamedTuple):
    """
    Output object returned when calling `SetSpacingParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output_image: OutputPathType
    """The output image with the specified spacing."""


def set_spacing_params(
    dimension: int,
    input_file: InputPathType,
    output_file: str,
    spacing: list[float],
) -> SetSpacingParamsDictTagged:
    """
    Build parameters.
    
    Args:
        dimension: The dimensionality of the image (e.g., 2 or 3).
        input_file: The input image file in HDR format.
        output_file: The output image file in NII format.
        spacing: Spacing values for each dimension. Requires SpacingX,\
            SpacingY, and optionally SpacingZ.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/SetSpacing",
        "dimension": dimension,
        "input_file": input_file,
        "output_file": output_file,
        "spacing": spacing,
    }
    return params


def set_spacing_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `SetSpacingParamsDict` 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("dimension", None) is None:
        raise StyxValidationError("`dimension` must not be None")
    if not isinstance(params["dimension"], int):
        raise StyxValidationError(f'`dimension` has the wrong type: Received `{type(params.get("dimension", None))}` expected `int`')
    if params.get("input_file", None) is None:
        raise StyxValidationError("`input_file` must not be None")
    if not isinstance(params["input_file"], (pathlib.Path, str)):
        raise StyxValidationError(f'`input_file` has the wrong type: Received `{type(params.get("input_file", None))}` expected `InputPathType`')
    if params.get("output_file", None) is None:
        raise StyxValidationError("`output_file` must not be None")
    if not isinstance(params["output_file"], str):
        raise StyxValidationError(f'`output_file` has the wrong type: Received `{type(params.get("output_file", None))}` expected `str`')
    if params.get("spacing", None) is None:
        raise StyxValidationError("`spacing` must not be None")
    if not isinstance(params["spacing"], list):
        raise StyxValidationError(f'`spacing` has the wrong type: Received `{type(params.get("spacing", None))}` expected `list[float]`')
    if not (2 <= len(params["spacing"]) <= 3):
        raise StyxValidationError("Parameter `spacing` must contain between 2 and 3 elements (inclusive)")
    for e in params["spacing"]:
        if not isinstance(e, (float, int)):
            raise StyxValidationError(f'`spacing` has the wrong type: Received `{type(params.get("spacing", None))}` expected `list[float]`')


def set_spacing_cargs(
    params: SetSpacingParamsDict,
    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("SetSpacing")
    cargs.append(str(params.get("dimension", None)))
    cargs.append(execution.input_file(params.get("input_file", None)))
    cargs.append(params.get("output_file", None))
    cargs.extend(map(str, params.get("spacing", None)))
    return cargs


def set_spacing_outputs(
    params: SetSpacingParamsDict,
    execution: Execution,
) -> SetSpacingOutputs:
    """
    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 = SetSpacingOutputs(
        root=execution.output_file("."),
        output_image=execution.output_file(params.get("output_file", None)),
    )
    return ret


def set_spacing_execute(
    params: SetSpacingParamsDict,
    runner: Runner | None = None,
) -> SetSpacingOutputs:
    """
    SetSpacing
    
    A tool to set the spacing of an image in each dimension.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `SetSpacingOutputs`).
    """
    set_spacing_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(SET_SPACING_METADATA)
    params = execution.params(params)
    cargs = set_spacing_cargs(params, execution)
    ret = set_spacing_outputs(params, execution)
    execution.run(cargs)
    return ret


def set_spacing(
    dimension: int,
    input_file: InputPathType,
    output_file: str,
    spacing: list[float],
    runner: Runner | None = None,
) -> SetSpacingOutputs:
    """
    SetSpacing
    
    A tool to set the spacing of an image in each dimension.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        dimension: The dimensionality of the image (e.g., 2 or 3).
        input_file: The input image file in HDR format.
        output_file: The output image file in NII format.
        spacing: Spacing values for each dimension. Requires SpacingX,\
            SpacingY, and optionally SpacingZ.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `SetSpacingOutputs`).
    """
    params = set_spacing_params(
        dimension=dimension,
        input_file=input_file,
        output_file=output_file,
        spacing=spacing,
    )
    return set_spacing_execute(params, runner)


__all__ = [
    "SET_SPACING_METADATA",
    "SetSpacingOutputs",
    "SetSpacingParamsDict",
    "SetSpacingParamsDictTagged",
    "set_spacing",
    "set_spacing_execute",
    "set_spacing_params",
]
