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

import typing
import pathlib
from styxdefs import *

METRIC_REDUCE_METADATA = Metadata(
    id="1d37ac901662d6962e04b6b12face19d11c30fc5.boutiques",
    name="metric-reduce",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


MetricReduceExcludeOutliersParameters = typing.TypedDict('MetricReduceExcludeOutliersParameters', {
    "@type": typing.NotRequired[typing.Literal["exclude_outliers"]],
    "sigma_below": float,
    "sigma_above": float,
})
MetricReduceExcludeOutliersParametersTagged = typing.TypedDict('MetricReduceExcludeOutliersParametersTagged', {
    "@type": typing.Literal["exclude_outliers"],
    "sigma_below": float,
    "sigma_above": float,
})


MetricReduceParameters = typing.TypedDict('MetricReduceParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/metric-reduce"]],
    "metric_in": InputPathType,
    "operation": str,
    "metric_out": str,
    "exclude_outliers": typing.NotRequired[MetricReduceExcludeOutliersParameters | None],
    "opt_only_numeric": bool,
})
MetricReduceParametersTagged = typing.TypedDict('MetricReduceParametersTagged', {
    "@type": typing.Literal["workbench/metric-reduce"],
    "metric_in": InputPathType,
    "operation": str,
    "metric_out": str,
    "exclude_outliers": typing.NotRequired[MetricReduceExcludeOutliersParameters | None],
    "opt_only_numeric": bool,
})


def metric_reduce_exclude_outliers_params(
    sigma_below: float,
    sigma_above: float,
) -> MetricReduceExcludeOutliersParametersTagged:
    """
    Build parameters.
    
    Args:
        sigma_below: number of standard deviations below the mean to include.
        sigma_above: number of standard deviations above the mean to include.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "exclude_outliers",
        "sigma_below": sigma_below,
        "sigma_above": sigma_above,
    }
    return params


def metric_reduce_exclude_outliers_cargs(
    params: MetricReduceExcludeOutliersParameters,
    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("-exclude-outliers")
    cargs.append(str(params.get("sigma_below", None)))
    cargs.append(str(params.get("sigma_above", None)))
    return cargs


class MetricReduceOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MetricReduceParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    metric_out: OutputPathType
    """the output metric"""


def metric_reduce_params(
    metric_in: InputPathType,
    operation: str,
    metric_out: str,
    exclude_outliers: MetricReduceExcludeOutliersParameters | None = None,
    opt_only_numeric: bool = False,
) -> MetricReduceParametersTagged:
    """
    Build parameters.
    
    Args:
        metric_in: the metric to reduce.
        operation: the reduction operator to use.
        metric_out: the output metric.
        exclude_outliers: exclude non-numeric values and outliers by standard\
            deviation.
        opt_only_numeric: exclude non-numeric values.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/metric-reduce",
        "metric_in": metric_in,
        "operation": operation,
        "metric_out": metric_out,
        "opt_only_numeric": opt_only_numeric,
    }
    if exclude_outliers is not None:
        params["exclude_outliers"] = exclude_outliers
    return params


def metric_reduce_cargs(
    params: MetricReduceParameters,
    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("-metric-reduce")
    cargs.append(execution.input_file(params.get("metric_in", None)))
    cargs.append(params.get("operation", None))
    cargs.append(params.get("metric_out", None))
    if params.get("exclude_outliers", None) is not None:
        cargs.extend(metric_reduce_exclude_outliers_cargs(params.get("exclude_outliers", None), execution))
    if params.get("opt_only_numeric", False):
        cargs.append("-only-numeric")
    return cargs


def metric_reduce_outputs(
    params: MetricReduceParameters,
    execution: Execution,
) -> MetricReduceOutputs:
    """
    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 = MetricReduceOutputs(
        root=execution.output_file("."),
        metric_out=execution.output_file(params.get("metric_out", None)),
    )
    return ret


def metric_reduce_execute(
    params: MetricReduceParameters,
    runner: Runner | None = None,
) -> MetricReduceOutputs:
    """
    metric-reduce
    
    Perform reduction operation across metric columns.
    
    For each surface vertex, takes the data across columns as a vector, and
    performs the specified reduction on it, putting the result into the single
    output column at that vertex. The reduction operators are as follows:
    
    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 `MetricReduceOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(METRIC_REDUCE_METADATA)
    params = execution.params(params)
    cargs = metric_reduce_cargs(params, execution)
    ret = metric_reduce_outputs(params, execution)
    execution.run(cargs)
    return ret


def metric_reduce(
    metric_in: InputPathType,
    operation: str,
    metric_out: str,
    exclude_outliers: MetricReduceExcludeOutliersParameters | None = None,
    opt_only_numeric: bool = False,
    runner: Runner | None = None,
) -> MetricReduceOutputs:
    """
    metric-reduce
    
    Perform reduction operation across metric columns.
    
    For each surface vertex, takes the data across columns as a vector, and
    performs the specified reduction on it, putting the result into the single
    output column at that vertex. The reduction operators are as follows:
    
    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:
        metric_in: the metric to reduce.
        operation: the reduction operator to use.
        metric_out: the output metric.
        exclude_outliers: exclude non-numeric values and outliers by standard\
            deviation.
        opt_only_numeric: exclude non-numeric values.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricReduceOutputs`).
    """
    params = metric_reduce_params(
        metric_in=metric_in,
        operation=operation,
        metric_out=metric_out,
        exclude_outliers=exclude_outliers,
        opt_only_numeric=opt_only_numeric,
    )
    return metric_reduce_execute(params, runner)


__all__ = [
    "METRIC_REDUCE_METADATA",
    "MetricReduceOutputs",
    "metric_reduce",
    "metric_reduce_exclude_outliers_params",
    "metric_reduce_execute",
    "metric_reduce_params",
]
