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

import typing
import pathlib
from styxdefs import *

VOLUME_STATS_METADATA = Metadata(
    id="ec1af7e3dc9990ebe00878d35008f857657ae3d6.workbench",
    name="volume-stats",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeStatsRoiParamsDictNoTag = typing.TypedDict('_VolumeStatsRoiParamsDictNoTag', {
    "roi-volume": InputPathType,
    "match-maps": bool,
})
VolumeStatsRoiParamsDictTagged = typing.TypedDict('VolumeStatsRoiParamsDictTagged', {
    "@type": typing.Literal["roi"],
    "roi-volume": InputPathType,
    "match-maps": bool,
})
VolumeStatsRoiParamsDict = _VolumeStatsRoiParamsDictNoTag | VolumeStatsRoiParamsDictTagged


_VolumeStatsParamsDictNoTag = typing.TypedDict('_VolumeStatsParamsDictNoTag', {
    "volume-in": InputPathType,
    "roi": typing.NotRequired[VolumeStatsRoiParamsDict | None],
    "subvolume": typing.NotRequired[str | None],
    "percent": typing.NotRequired[float | None],
    "operation": typing.NotRequired[str | None],
    "show-map-name": bool,
})
VolumeStatsParamsDictTagged = typing.TypedDict('VolumeStatsParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-stats"],
    "volume-in": InputPathType,
    "roi": typing.NotRequired[VolumeStatsRoiParamsDict | None],
    "subvolume": typing.NotRequired[str | None],
    "percent": typing.NotRequired[float | None],
    "operation": typing.NotRequired[str | None],
    "show-map-name": bool,
})
VolumeStatsParamsDict = _VolumeStatsParamsDictNoTag | VolumeStatsParamsDictTagged


def volume_stats_roi(
    roi_volume: InputPathType,
    match_maps: bool = False,
) -> VolumeStatsRoiParamsDictTagged:
    """
    Build parameters.
    
    Args:
        roi_volume: the roi, as a volume file.
        match_maps: each subvolume of input uses the corresponding subvolume\
            from the roi file.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "roi",
        "roi-volume": roi_volume,
        "match-maps": match_maps,
    }
    return params


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


def volume_stats_roi_cargs(
    params: VolumeStatsRoiParamsDict,
    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.extend([
        "-roi",
        execution.input_file(params.get("roi-volume", None))
    ])
    if params.get("match-maps", False):
        cargs.append("-match-maps")
    return cargs


class VolumeStatsOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeStatsParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def volume_stats_params(
    volume_in: InputPathType,
    roi: VolumeStatsRoiParamsDict | None = None,
    subvolume: str | None = None,
    percent: float | None = None,
    operation: str | None = None,
    show_map_name: bool = False,
) -> VolumeStatsParamsDictTagged:
    """
    Build parameters.
    
    Args:
        volume_in: the input volume.
        roi: only consider data inside an roi.
        subvolume: only display output for one subvolume\
            \
            the subvolume number or name.
        percent: give the value at a percentile\
            \
            the percentile to find, must be between 0 and 100.
        operation: use a reduction operation\
            \
            the reduction operation.
        show_map_name: print map index and name before each output.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-stats",
        "volume-in": volume_in,
        "show-map-name": show_map_name,
    }
    if roi is not None:
        params["roi"] = roi
    if subvolume is not None:
        params["subvolume"] = subvolume
    if percent is not None:
        params["percent"] = percent
    if operation is not None:
        params["operation"] = operation
    return params


def volume_stats_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeStatsParamsDict` 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("volume-in", None) is None:
        raise StyxValidationError("`volume-in` must not be None")
    if not isinstance(params["volume-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`volume-in` has the wrong type: Received `{type(params.get("volume-in", None))}` expected `InputPathType`')
    if params.get("roi", None) is not None:
        volume_stats_roi_validate(params["roi"])
    if params.get("subvolume", None) is not None:
        if not isinstance(params["subvolume"], str):
            raise StyxValidationError(f'`subvolume` has the wrong type: Received `{type(params.get("subvolume", None))}` expected `str | None`')
    if params.get("percent", None) is not None:
        if not isinstance(params["percent"], (float, int)):
            raise StyxValidationError(f'`percent` has the wrong type: Received `{type(params.get("percent", None))}` expected `float | None`')
    if params.get("operation", None) is not None:
        if not isinstance(params["operation"], str):
            raise StyxValidationError(f'`operation` has the wrong type: Received `{type(params.get("operation", None))}` expected `str | None`')
    if params.get("show-map-name", False) is None:
        raise StyxValidationError("`show-map-name` must not be None")
    if not isinstance(params["show-map-name"], bool):
        raise StyxValidationError(f'`show-map-name` has the wrong type: Received `{type(params.get("show-map-name", False))}` expected `bool`')


def volume_stats_cargs(
    params: VolumeStatsParamsDict,
    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.extend([
        "wb_command",
        "-volume-stats"
    ])
    cargs.append(execution.input_file(params.get("volume-in", None)))
    if params.get("roi", None) is not None:
        cargs.extend(volume_stats_roi_cargs(params.get("roi", None), execution))
    if params.get("subvolume", None) is not None:
        cargs.extend([
            "-subvolume",
            params.get("subvolume", None)
        ])
    if params.get("percent", None) is not None:
        cargs.extend([
            "-percentile",
            str(params.get("percent", None))
        ])
    if params.get("operation", None) is not None:
        cargs.extend([
            "-reduce",
            params.get("operation", None)
        ])
    if params.get("show-map-name", False):
        cargs.append("-show-map-name")
    return cargs


def volume_stats_outputs(
    params: VolumeStatsParamsDict,
    execution: Execution,
) -> VolumeStatsOutputs:
    """
    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 = VolumeStatsOutputs(
        root=execution.output_file("."),
    )
    return ret


def volume_stats_execute(
    params: VolumeStatsParamsDict,
    runner: Runner | None = None,
) -> VolumeStatsOutputs:
    """
    SPATIAL STATISTICS ON A VOLUME FILE.
    
    For each subvolume of the input, a line of text is printed, resulting from
    the specified reduction or percentile operation. Use -subvolume to only give
    output for a single subvolume. If the -roi option is used without
    -match-maps, then each line will contain as many numbers as there are maps
    in the ROI file, separated by tab characters. Exactly one of -reduce or
    -percentile must be specified.
    
    The argument to the -reduce option must be one of the following:
    
    MAX: the maximum value
    MIN: the minimum value
    INDEXMAX: the 1-based index of the maximum value
    INDEXMIN: the 1-based index of the minimum value
    SUM: add all values
    PRODUCT: multiply all values
    MEAN: the mean of the data
    STDEV: the standard deviation (N denominator)
    SAMPSTDEV: the sample standard deviation (N-1 denominator)
    VARIANCE: the variance of the data
    TSNR: mean divided by sample standard deviation (N-1 denominator)
    COV: sample standard deviation (N-1 denominator) divided by mean
    L2NORM: square root of sum of squares
    MEDIAN: the median of the data
    MODE: the mode of the data
    COUNT_NONZERO: the number of nonzero elements in the data
    .
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeStatsOutputs`).
    """
    volume_stats_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_STATS_METADATA)
    params = execution.params(params)
    cargs = volume_stats_cargs(params, execution)
    ret = volume_stats_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_stats(
    volume_in: InputPathType,
    roi: VolumeStatsRoiParamsDict | None = None,
    subvolume: str | None = None,
    percent: float | None = None,
    operation: str | None = None,
    show_map_name: bool = False,
    runner: Runner | None = None,
) -> VolumeStatsOutputs:
    """
    SPATIAL STATISTICS ON A VOLUME FILE.
    
    For each subvolume of the input, a line of text is printed, resulting from
    the specified reduction or percentile operation. Use -subvolume to only give
    output for a single subvolume. If the -roi option is used without
    -match-maps, then each line will contain as many numbers as there are maps
    in the ROI file, separated by tab characters. Exactly one of -reduce or
    -percentile must be specified.
    
    The argument to the -reduce option must be one of the following:
    
    MAX: the maximum value
    MIN: the minimum value
    INDEXMAX: the 1-based index of the maximum value
    INDEXMIN: the 1-based index of the minimum value
    SUM: add all values
    PRODUCT: multiply all values
    MEAN: the mean of the data
    STDEV: the standard deviation (N denominator)
    SAMPSTDEV: the sample standard deviation (N-1 denominator)
    VARIANCE: the variance of the data
    TSNR: mean divided by sample standard deviation (N-1 denominator)
    COV: sample standard deviation (N-1 denominator) divided by mean
    L2NORM: square root of sum of squares
    MEDIAN: the median of the data
    MODE: the mode of the data
    COUNT_NONZERO: the number of nonzero elements in the data
    .
    
    Args:
        volume_in: the input volume.
        roi: only consider data inside an roi.
        subvolume: only display output for one subvolume\
            \
            the subvolume number or name.
        percent: give the value at a percentile\
            \
            the percentile to find, must be between 0 and 100.
        operation: use a reduction operation\
            \
            the reduction operation.
        show_map_name: print map index and name before each output.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeStatsOutputs`).
    """
    params = volume_stats_params(
        volume_in=volume_in,
        roi=roi,
        subvolume=subvolume,
        percent=percent,
        operation=operation,
        show_map_name=show_map_name,
    )
    return volume_stats_execute(params, runner)


__all__ = [
    "VOLUME_STATS_METADATA",
    "VolumeStatsOutputs",
    "VolumeStatsParamsDict",
    "VolumeStatsParamsDictTagged",
    "VolumeStatsRoiParamsDict",
    "VolumeStatsRoiParamsDictTagged",
    "volume_stats",
    "volume_stats_execute",
    "volume_stats_params",
    "volume_stats_roi",
]
