# 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="aee8e35b38c06ea18eeb42aa6c5076216e3ebc0c.boutiques",
    name="volume-stats",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


VolumeStatsRoiParameters = typing.TypedDict('VolumeStatsRoiParameters', {
    "@type": typing.NotRequired[typing.Literal["roi"]],
    "roi_volume": InputPathType,
    "opt_match_maps": bool,
})
VolumeStatsRoiParametersTagged = typing.TypedDict('VolumeStatsRoiParametersTagged', {
    "@type": typing.Literal["roi"],
    "roi_volume": InputPathType,
    "opt_match_maps": bool,
})


VolumeStatsParameters = typing.TypedDict('VolumeStatsParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/volume-stats"]],
    "volume_in": InputPathType,
    "opt_reduce_operation": typing.NotRequired[str | None],
    "opt_percentile_percent": typing.NotRequired[float | None],
    "opt_subvolume_subvolume": typing.NotRequired[str | None],
    "roi": typing.NotRequired[VolumeStatsRoiParameters | None],
    "opt_show_map_name": bool,
})
VolumeStatsParametersTagged = typing.TypedDict('VolumeStatsParametersTagged', {
    "@type": typing.Literal["workbench/volume-stats"],
    "volume_in": InputPathType,
    "opt_reduce_operation": typing.NotRequired[str | None],
    "opt_percentile_percent": typing.NotRequired[float | None],
    "opt_subvolume_subvolume": typing.NotRequired[str | None],
    "roi": typing.NotRequired[VolumeStatsRoiParameters | None],
    "opt_show_map_name": bool,
})


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


def volume_stats_roi_cargs(
    params: VolumeStatsRoiParameters,
    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("-roi")
    cargs.append(execution.input_file(params.get("roi_volume", None)))
    if params.get("opt_match_maps", False):
        cargs.append("-match-maps")
    return cargs


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


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


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


def volume_stats_outputs(
    params: VolumeStatsParameters,
    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: VolumeStatsParameters,
    runner: Runner | None = None,
) -> VolumeStatsOutputs:
    """
    volume-stats
    
    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
    .
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeStatsOutputs`).
    """
    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,
    opt_reduce_operation: str | None = None,
    opt_percentile_percent: float | None = None,
    opt_subvolume_subvolume: str | None = None,
    roi: VolumeStatsRoiParameters | None = None,
    opt_show_map_name: bool = False,
    runner: Runner | None = None,
) -> VolumeStatsOutputs:
    """
    volume-stats
    
    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
    .
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        volume_in: the input volume.
        opt_reduce_operation: use a reduction operation: the reduction\
            operation.
        opt_percentile_percent: give the value at a percentile: the percentile\
            to find, must be between 0 and 100.
        opt_subvolume_subvolume: only display output for one subvolume: the\
            subvolume number or name.
        roi: only consider data inside an roi.
        opt_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,
        opt_reduce_operation=opt_reduce_operation,
        opt_percentile_percent=opt_percentile_percent,
        opt_subvolume_subvolume=opt_subvolume_subvolume,
        roi=roi,
        opt_show_map_name=opt_show_map_name,
    )
    return volume_stats_execute(params, runner)


__all__ = [
    "VOLUME_STATS_METADATA",
    "VolumeStatsOutputs",
    "volume_stats",
    "volume_stats_execute",
    "volume_stats_params",
    "volume_stats_roi_params",
]
