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

import typing
import pathlib
from styxdefs import *

IMAGE_SET_STATISTICS_METADATA = Metadata(
    id="9329b484e2f67dba69c548b9b9a373ea2a072aa8.boutiques",
    name="ImageSetStatistics",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


ImageSetStatisticsParameters = typing.TypedDict('ImageSetStatisticsParameters', {
    "__STYX_TYPE__": typing.Literal["ImageSetStatistics"],
    "image_dimension": int,
    "controls_list": InputPathType,
    "output_image": str,
    "which_stat": typing.Literal[0, 1, 2, 3, 4, 5, 6, 7],
    "roi": typing.NotRequired[InputPathType | None],
    "imagelist2": typing.NotRequired[InputPathType | None],
})


def dyn_cargs(
    t: str,
) -> typing.Any:
    """
    Get build cargs function by command type.
    
    Args:
        t: Command type.
    Returns:
        Build cargs function.
    """
    return {
        "ImageSetStatistics": image_set_statistics_cargs,
    }.get(t)


def dyn_outputs(
    t: str,
) -> typing.Any:
    """
    Get build outputs function by command type.
    
    Args:
        t: Command type.
    Returns:
        Build outputs function.
    """
    return {
        "ImageSetStatistics": image_set_statistics_outputs,
    }.get(t)


class ImageSetStatisticsOutputs(typing.NamedTuple):
    """
    Output object returned when calling `image_set_statistics(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    computed_statistics_image: OutputPathType
    """The output image containing the computed statistics."""


def image_set_statistics_params(
    image_dimension: int,
    controls_list: InputPathType,
    output_image: str,
    which_stat: typing.Literal[0, 1, 2, 3, 4, 5, 6, 7],
    roi: InputPathType | None = None,
    imagelist2: InputPathType | None = None,
) -> ImageSetStatisticsParameters:
    """
    Build parameters.
    
    Args:
        image_dimension: The dimensionality of the images to be processed by\
            ImageSetStatistics.
        controls_list: Text file containing the list of control images.
        output_image: The output image file where the computed statistics are\
            stored.
        which_stat: Choice of statistic to compute: 0 for median, 1 for max\
            probability appearance, 2 for weighted mean appearance, 3 for trimmed\
            mean, 4 for max value, 5 for similarity-weighted (requires imagelist2),\
            6 for best local match label, 7 for max value from ROI.
        roi: Region of interest image file, optional depending on the whichstat\
            option.
        imagelist2: List of similarity images used for similarity-weighted\
            statistics. Required if whichstat equals 5 or 6.
    Returns:
        Parameter dictionary
    """
    params = {
        "__STYXTYPE__": "ImageSetStatistics",
        "image_dimension": image_dimension,
        "controls_list": controls_list,
        "output_image": output_image,
        "which_stat": which_stat,
    }
    if roi is not None:
        params["roi"] = roi
    if imagelist2 is not None:
        params["imagelist2"] = imagelist2
    return params


def image_set_statistics_cargs(
    params: ImageSetStatisticsParameters,
    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("ImageSetStatistics")
    cargs.append(str(params.get("image_dimension")))
    cargs.extend([
        "[CONTROLS_LIST]",
        execution.input_file(params.get("controls_list"))
    ])
    cargs.extend([
        "[OUTPUT_IMAGE]",
        params.get("output_image")
    ])
    cargs.extend([
        "[WHICH_STAT]",
        str(params.get("which_stat"))
    ])
    if params.get("roi") is not None:
        cargs.extend([
            "[ROI]",
            execution.input_file(params.get("roi"))
        ])
    if params.get("imagelist2") is not None:
        cargs.extend([
            "[IMAGELIST2]",
            execution.input_file(params.get("imagelist2"))
        ])
    return cargs


def image_set_statistics_outputs(
    params: ImageSetStatisticsParameters,
    execution: Execution,
) -> ImageSetStatisticsOutputs:
    """
    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 = ImageSetStatisticsOutputs(
        root=execution.output_file("."),
        computed_statistics_image=execution.output_file(params.get("output_image")),
    )
    return ret


def image_set_statistics_execute(
    params: ImageSetStatisticsParameters,
    execution: Execution,
) -> ImageSetStatisticsOutputs:
    """
    ImageSetStatistics computes statistics from a set of images. The whichstat
    option defines the type of statistic to compute, ranging from median to
    similarity-weighted metrics.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        execution: The execution object.
    Returns:
        NamedTuple of outputs (described in `ImageSetStatisticsOutputs`).
    """
    params = execution.params(params)
    cargs = image_set_statistics_cargs(params, execution)
    ret = image_set_statistics_outputs(params, execution)
    execution.run(cargs)
    return ret


def image_set_statistics(
    image_dimension: int,
    controls_list: InputPathType,
    output_image: str,
    which_stat: typing.Literal[0, 1, 2, 3, 4, 5, 6, 7],
    roi: InputPathType | None = None,
    imagelist2: InputPathType | None = None,
    runner: Runner | None = None,
) -> ImageSetStatisticsOutputs:
    """
    ImageSetStatistics computes statistics from a set of images. The whichstat
    option defines the type of statistic to compute, ranging from median to
    similarity-weighted metrics.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: The dimensionality of the images to be processed by\
            ImageSetStatistics.
        controls_list: Text file containing the list of control images.
        output_image: The output image file where the computed statistics are\
            stored.
        which_stat: Choice of statistic to compute: 0 for median, 1 for max\
            probability appearance, 2 for weighted mean appearance, 3 for trimmed\
            mean, 4 for max value, 5 for similarity-weighted (requires imagelist2),\
            6 for best local match label, 7 for max value from ROI.
        roi: Region of interest image file, optional depending on the whichstat\
            option.
        imagelist2: List of similarity images used for similarity-weighted\
            statistics. Required if whichstat equals 5 or 6.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ImageSetStatisticsOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(IMAGE_SET_STATISTICS_METADATA)
    params = image_set_statistics_params(
        image_dimension=image_dimension,
        controls_list=controls_list,
        output_image=output_image,
        which_stat=which_stat,
        roi=roi,
        imagelist2=imagelist2,
    )
    return image_set_statistics_execute(params, execution)


__all__ = [
    "IMAGE_SET_STATISTICS_METADATA",
    "ImageSetStatisticsOutputs",
    "ImageSetStatisticsParameters",
    "image_set_statistics",
    "image_set_statistics_params",
]
