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

import typing
import pathlib
from styxdefs import *

CIFTI_AVERAGE_METADATA = Metadata(
    id="e097bdbc9855ceeaf5b5317f6d524a83688c3dbe.boutiques",
    name="cifti-average",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


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


CiftiAverageCiftiParameters = typing.TypedDict('CiftiAverageCiftiParameters', {
    "@type": typing.NotRequired[typing.Literal["cifti"]],
    "cifti_in": InputPathType,
    "opt_weight_weight": typing.NotRequired[float | None],
})
CiftiAverageCiftiParametersTagged = typing.TypedDict('CiftiAverageCiftiParametersTagged', {
    "@type": typing.Literal["cifti"],
    "cifti_in": InputPathType,
    "opt_weight_weight": typing.NotRequired[float | None],
})


CiftiAverageParameters = typing.TypedDict('CiftiAverageParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/cifti-average"]],
    "cifti_out": str,
    "exclude_outliers": typing.NotRequired[CiftiAverageExcludeOutliersParameters | None],
    "opt_mem_limit_limit_gb": typing.NotRequired[float | None],
    "cifti": typing.NotRequired[list[CiftiAverageCiftiParameters] | None],
})
CiftiAverageParametersTagged = typing.TypedDict('CiftiAverageParametersTagged', {
    "@type": typing.Literal["workbench/cifti-average"],
    "cifti_out": str,
    "exclude_outliers": typing.NotRequired[CiftiAverageExcludeOutliersParameters | None],
    "opt_mem_limit_limit_gb": typing.NotRequired[float | None],
    "cifti": typing.NotRequired[list[CiftiAverageCiftiParameters] | None],
})


def cifti_average_exclude_outliers_params(
    sigma_below: float,
    sigma_above: float,
) -> CiftiAverageExcludeOutliersParametersTagged:
    """
    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 cifti_average_exclude_outliers_cargs(
    params: CiftiAverageExcludeOutliersParameters,
    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


def cifti_average_cifti_params(
    cifti_in: InputPathType,
    opt_weight_weight: float | None = None,
) -> CiftiAverageCiftiParametersTagged:
    """
    Build parameters.
    
    Args:
        cifti_in: the input cifti file.
        opt_weight_weight: give a weight for this file: the weight to use.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "cifti",
        "cifti_in": cifti_in,
    }
    if opt_weight_weight is not None:
        params["opt_weight_weight"] = opt_weight_weight
    return params


def cifti_average_cifti_cargs(
    params: CiftiAverageCiftiParameters,
    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("-cifti")
    cargs.append(execution.input_file(params.get("cifti_in", None)))
    if params.get("opt_weight_weight", None) is not None:
        cargs.extend([
            "-weight",
            str(params.get("opt_weight_weight", None))
        ])
    return cargs


class CiftiAverageOutputs(typing.NamedTuple):
    """
    Output object returned when calling `CiftiAverageParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    cifti_out: OutputPathType
    """output cifti file"""


def cifti_average_params(
    cifti_out: str,
    exclude_outliers: CiftiAverageExcludeOutliersParameters | None = None,
    opt_mem_limit_limit_gb: float | None = None,
    cifti: list[CiftiAverageCiftiParameters] | None = None,
) -> CiftiAverageParametersTagged:
    """
    Build parameters.
    
    Args:
        cifti_out: output cifti file.
        exclude_outliers: exclude outliers by standard deviation of each\
            element across files.
        opt_mem_limit_limit_gb: restrict memory used for file reading\
            efficiency: memory limit in gigabytes.
        cifti: specify an input file.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-average",
        "cifti_out": cifti_out,
    }
    if exclude_outliers is not None:
        params["exclude_outliers"] = exclude_outliers
    if opt_mem_limit_limit_gb is not None:
        params["opt_mem_limit_limit_gb"] = opt_mem_limit_limit_gb
    if cifti is not None:
        params["cifti"] = cifti
    return params


def cifti_average_cargs(
    params: CiftiAverageParameters,
    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("-cifti-average")
    cargs.append(params.get("cifti_out", None))
    if params.get("exclude_outliers", None) is not None:
        cargs.extend(cifti_average_exclude_outliers_cargs(params.get("exclude_outliers", None), execution))
    if params.get("opt_mem_limit_limit_gb", None) is not None:
        cargs.extend([
            "-mem-limit",
            str(params.get("opt_mem_limit_limit_gb", None))
        ])
    if params.get("cifti", None) is not None:
        cargs.extend([a for c in [cifti_average_cifti_cargs(s, execution) for s in params.get("cifti", None)] for a in c])
    return cargs


def cifti_average_outputs(
    params: CiftiAverageParameters,
    execution: Execution,
) -> CiftiAverageOutputs:
    """
    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 = CiftiAverageOutputs(
        root=execution.output_file("."),
        cifti_out=execution.output_file(params.get("cifti_out", None)),
    )
    return ret


def cifti_average_execute(
    params: CiftiAverageParameters,
    runner: Runner | None = None,
) -> CiftiAverageOutputs:
    """
    cifti-average
    
    Average cifti files.
    
    Averages cifti files together. Files without -weight specified are given a
    weight of 1. If -exclude-outliers is specified, at each element, the data
    across all files is taken as a set, its unweighted mean and sample standard
    deviation are found, and values outside the specified number of standard
    deviations are excluded from the (potentially weighted) average at that
    element.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiAverageOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_AVERAGE_METADATA)
    params = execution.params(params)
    cargs = cifti_average_cargs(params, execution)
    ret = cifti_average_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_average(
    cifti_out: str,
    exclude_outliers: CiftiAverageExcludeOutliersParameters | None = None,
    opt_mem_limit_limit_gb: float | None = None,
    cifti: list[CiftiAverageCiftiParameters] | None = None,
    runner: Runner | None = None,
) -> CiftiAverageOutputs:
    """
    cifti-average
    
    Average cifti files.
    
    Averages cifti files together. Files without -weight specified are given a
    weight of 1. If -exclude-outliers is specified, at each element, the data
    across all files is taken as a set, its unweighted mean and sample standard
    deviation are found, and values outside the specified number of standard
    deviations are excluded from the (potentially weighted) average at that
    element.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        cifti_out: output cifti file.
        exclude_outliers: exclude outliers by standard deviation of each\
            element across files.
        opt_mem_limit_limit_gb: restrict memory used for file reading\
            efficiency: memory limit in gigabytes.
        cifti: specify an input file.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiAverageOutputs`).
    """
    params = cifti_average_params(
        cifti_out=cifti_out,
        exclude_outliers=exclude_outliers,
        opt_mem_limit_limit_gb=opt_mem_limit_limit_gb,
        cifti=cifti,
    )
    return cifti_average_execute(params, runner)


__all__ = [
    "CIFTI_AVERAGE_METADATA",
    "CiftiAverageOutputs",
    "cifti_average",
    "cifti_average_cifti_params",
    "cifti_average_exclude_outliers_params",
    "cifti_average_execute",
    "cifti_average_params",
]
