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

import typing
import pathlib
from styxdefs import *

ADD_NOISE_TO_IMAGE_METADATA = Metadata(
    id="5673a36dafb026b933cb72c1d9581ff34f85a2ae.boutiques",
    name="AddNoiseToImage",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_AddNoiseToImageParamsDictNoTag = typing.TypedDict('_AddNoiseToImageParamsDictNoTag', {
    "image_dimensionality": typing.NotRequired[typing.Literal[2, 3, 4] | None],
    "input_image": InputPathType,
    "noise_model": typing.Literal["AdditiveGaussian", "SaltAndPepper", "Shot", "Speckle"],
    "output": str,
    "verbose": typing.NotRequired[bool | None],
})
AddNoiseToImageParamsDictTagged = typing.TypedDict('AddNoiseToImageParamsDictTagged', {
    "@type": typing.Literal["ants/AddNoiseToImage"],
    "image_dimensionality": typing.NotRequired[typing.Literal[2, 3, 4] | None],
    "input_image": InputPathType,
    "noise_model": typing.Literal["AdditiveGaussian", "SaltAndPepper", "Shot", "Speckle"],
    "output": str,
    "verbose": typing.NotRequired[bool | None],
})
AddNoiseToImageParamsDict = _AddNoiseToImageParamsDictNoTag | AddNoiseToImageParamsDictTagged


class AddNoiseToImageOutputs(typing.NamedTuple):
    """
    Output object returned when calling `AddNoiseToImageParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    noise_corrupted_image: OutputPathType
    """The output is the noise corrupted version of the input image."""


def add_noise_to_image_params(
    input_image: InputPathType,
    noise_model: typing.Literal["AdditiveGaussian", "SaltAndPepper", "Shot", "Speckle"],
    output: str,
    image_dimensionality: typing.Literal[2, 3, 4] | None = None,
    verbose: bool | None = None,
) -> AddNoiseToImageParamsDictTagged:
    """
    Build parameters.
    
    Args:
        input_image: A scalar image is expected as input for noise correction.
        noise_model: Use different noise models each with its own (default)\
            parameters.
        output: The output consists of the noise corrupted version of the input\
            image.
        image_dimensionality: This option forces the image to be treated as a\
            specified-dimensional image. If not specified, the program tries to\
            infer the dimensionality from the input image.
        verbose: Verbose output.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/AddNoiseToImage",
        "input_image": input_image,
        "noise_model": noise_model,
        "output": output,
    }
    if image_dimensionality is not None:
        params["image_dimensionality"] = image_dimensionality
    if verbose is not None:
        params["verbose"] = verbose
    return params


def add_noise_to_image_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `AddNoiseToImageParamsDict` 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_dimensionality", None) is not None:
        if not isinstance(params["image_dimensionality"], int):
            raise StyxValidationError(f'`image_dimensionality` has the wrong type: Received `{type(params.get("image_dimensionality", None))}` expected `typing.Literal[2, 3, 4] | None`')
        if params["image_dimensionality"] not in [2, 3, 4]:
            raise StyxValidationError("Parameter `image_dimensionality` must be one of [2, 3, 4]")
    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("noise_model", None) is None:
        raise StyxValidationError("`noise_model` must not be None")
    if not isinstance(params["noise_model"], str):
        raise StyxValidationError(f'`noise_model` has the wrong type: Received `{type(params.get("noise_model", None))}` expected `typing.Literal["AdditiveGaussian", "SaltAndPepper", "Shot", "Speckle"]`')
    if params["noise_model"] not in ["AdditiveGaussian", "SaltAndPepper", "Shot", "Speckle"]:
        raise StyxValidationError("Parameter `noise_model` must be one of [\"AdditiveGaussian\", \"SaltAndPepper\", \"Shot\", \"Speckle\"]")
    if params.get("output", None) is None:
        raise StyxValidationError("`output` must not be None")
    if not isinstance(params["output"], str):
        raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `str`')
    if params.get("verbose", None) is not None:
        if not isinstance(params["verbose"], bool):
            raise StyxValidationError(f'`verbose` has the wrong type: Received `{type(params.get("verbose", None))}` expected `bool | None`')


def add_noise_to_image_cargs(
    params: AddNoiseToImageParamsDict,
    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("AddNoiseToImage")
    if params.get("image_dimensionality", None) is not None:
        cargs.extend([
            "--image-dimensionality",
            str(params.get("image_dimensionality", None))
        ])
    cargs.extend([
        "--input-image",
        execution.input_file(params.get("input_image", None))
    ])
    cargs.extend([
        "--noise-model",
        params.get("noise_model", None)
    ])
    cargs.extend([
        "--output",
        params.get("output", None)
    ])
    if params.get("verbose", None) is not None:
        cargs.extend([
            "--verbose",
            ("1" if params.get("verbose", None) else "0")
        ])
    return cargs


def add_noise_to_image_outputs(
    params: AddNoiseToImageParamsDict,
    execution: Execution,
) -> AddNoiseToImageOutputs:
    """
    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 = AddNoiseToImageOutputs(
        root=execution.output_file("."),
        noise_corrupted_image=execution.output_file(params.get("output", None)),
    )
    return ret


def add_noise_to_image_execute(
    params: AddNoiseToImageParamsDict,
    runner: Runner | None = None,
) -> AddNoiseToImageOutputs:
    """
    AddNoiseToImage
    
    Add various types of noise to an image.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AddNoiseToImageOutputs`).
    """
    add_noise_to_image_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(ADD_NOISE_TO_IMAGE_METADATA)
    params = execution.params(params)
    cargs = add_noise_to_image_cargs(params, execution)
    ret = add_noise_to_image_outputs(params, execution)
    execution.run(cargs)
    return ret


def add_noise_to_image(
    input_image: InputPathType,
    noise_model: typing.Literal["AdditiveGaussian", "SaltAndPepper", "Shot", "Speckle"],
    output: str,
    image_dimensionality: typing.Literal[2, 3, 4] | None = None,
    verbose: bool | None = None,
    runner: Runner | None = None,
) -> AddNoiseToImageOutputs:
    """
    AddNoiseToImage
    
    Add various types of noise to an image.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        input_image: A scalar image is expected as input for noise correction.
        noise_model: Use different noise models each with its own (default)\
            parameters.
        output: The output consists of the noise corrupted version of the input\
            image.
        image_dimensionality: This option forces the image to be treated as a\
            specified-dimensional image. If not specified, the program tries to\
            infer the dimensionality from the input image.
        verbose: Verbose output.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AddNoiseToImageOutputs`).
    """
    params = add_noise_to_image_params(
        image_dimensionality=image_dimensionality,
        input_image=input_image,
        noise_model=noise_model,
        output=output,
        verbose=verbose,
    )
    return add_noise_to_image_execute(params, runner)


__all__ = [
    "ADD_NOISE_TO_IMAGE_METADATA",
    "AddNoiseToImageOutputs",
    "AddNoiseToImageParamsDict",
    "AddNoiseToImageParamsDictTagged",
    "add_noise_to_image",
    "add_noise_to_image_execute",
    "add_noise_to_image_params",
]
