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

import typing
import pathlib
from styxdefs import *

SMOOTH_IMAGE_METADATA = Metadata(
    id="0b9f787af4c1a0fcf3db27f547e0fd544c29f18f.boutiques",
    name="SmoothImage",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_SmoothImageParamsDictNoTag = typing.TypedDict('_SmoothImageParamsDictNoTag', {
    "image_dimension": int,
    "image_ext": InputPathType,
    "smoothing_sigma": str,
    "out_image_ext": str,
    "sigma_units": typing.NotRequired[bool | None],
    "median_filter": typing.NotRequired[bool | None],
})
SmoothImageParamsDictTagged = typing.TypedDict('SmoothImageParamsDictTagged', {
    "@type": typing.Literal["ants/SmoothImage"],
    "image_dimension": int,
    "image_ext": InputPathType,
    "smoothing_sigma": str,
    "out_image_ext": str,
    "sigma_units": typing.NotRequired[bool | None],
    "median_filter": typing.NotRequired[bool | None],
})
SmoothImageParamsDict = _SmoothImageParamsDictNoTag | SmoothImageParamsDictTagged


class SmoothImageOutputs(typing.NamedTuple):
    """
    Output object returned when calling `SmoothImageParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    smoothed_image: OutputPathType
    """The output smoothed image file."""


def smooth_image_params(
    image_dimension: int,
    image_ext: InputPathType,
    smoothing_sigma: str,
    out_image_ext: str,
    sigma_units: bool | None = None,
    median_filter: bool | None = None,
) -> SmoothImageParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image_dimension: Specifies the dimensionality of the image.
        image_ext: The input image file to be smoothed.
        smoothing_sigma: The sigma value for smoothing. A separate sigma may be\
            specified for each dimension, e.g., '1.5x1x2'.
        out_image_ext: The output smoothed image file.
        sigma_units: Determines if sigma is in spacing units (1) or not (0).\
            Default is 0.
        median_filter: Whether to use median filter. Default is 0. If using\
            median filter, sigma represents the radius in voxels.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/SmoothImage",
        "image_dimension": image_dimension,
        "image_ext": image_ext,
        "smoothing_sigma": smoothing_sigma,
        "out_image_ext": out_image_ext,
    }
    if sigma_units is not None:
        params["sigma_units"] = sigma_units
    if median_filter is not None:
        params["median_filter"] = median_filter
    return params


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


def smooth_image_cargs(
    params: SmoothImageParamsDict,
    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("SmoothImage")
    cargs.append(str(params.get("image_dimension", None)))
    cargs.append(execution.input_file(params.get("image_ext", None)))
    cargs.append(params.get("smoothing_sigma", None))
    cargs.append(params.get("out_image_ext", None))
    if params.get("sigma_units", None) is not None:
        cargs.append(("1" if params.get("sigma_units", None) else "0"))
    if params.get("median_filter", None) is not None:
        cargs.append(("1" if params.get("median_filter", None) else "0"))
    return cargs


def smooth_image_outputs(
    params: SmoothImageParamsDict,
    execution: Execution,
) -> SmoothImageOutputs:
    """
    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 = SmoothImageOutputs(
        root=execution.output_file("."),
        smoothed_image=execution.output_file(params.get("out_image_ext", None)),
    )
    return ret


def smooth_image_execute(
    params: SmoothImageParamsDict,
    runner: Runner | None = None,
) -> SmoothImageOutputs:
    """
    SmoothImage
    
    SmoothImage allows smoothing of images with adjustable sigma values,
    offering optional median filtering functionality.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `SmoothImageOutputs`).
    """
    smooth_image_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(SMOOTH_IMAGE_METADATA)
    params = execution.params(params)
    cargs = smooth_image_cargs(params, execution)
    ret = smooth_image_outputs(params, execution)
    execution.run(cargs)
    return ret


def smooth_image(
    image_dimension: int,
    image_ext: InputPathType,
    smoothing_sigma: str,
    out_image_ext: str,
    sigma_units: bool | None = None,
    median_filter: bool | None = None,
    runner: Runner | None = None,
) -> SmoothImageOutputs:
    """
    SmoothImage
    
    SmoothImage allows smoothing of images with adjustable sigma values,
    offering optional median filtering functionality.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: Specifies the dimensionality of the image.
        image_ext: The input image file to be smoothed.
        smoothing_sigma: The sigma value for smoothing. A separate sigma may be\
            specified for each dimension, e.g., '1.5x1x2'.
        out_image_ext: The output smoothed image file.
        sigma_units: Determines if sigma is in spacing units (1) or not (0).\
            Default is 0.
        median_filter: Whether to use median filter. Default is 0. If using\
            median filter, sigma represents the radius in voxels.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `SmoothImageOutputs`).
    """
    params = smooth_image_params(
        image_dimension=image_dimension,
        image_ext=image_ext,
        smoothing_sigma=smoothing_sigma,
        out_image_ext=out_image_ext,
        sigma_units=sigma_units,
        median_filter=median_filter,
    )
    return smooth_image_execute(params, runner)


__all__ = [
    "SMOOTH_IMAGE_METADATA",
    "SmoothImageOutputs",
    "SmoothImageParamsDict",
    "SmoothImageParamsDictTagged",
    "smooth_image",
    "smooth_image_execute",
    "smooth_image_params",
]
