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

import typing
import pathlib
from styxdefs import *

CIFTI_STATS_METADATA = Metadata(
    id="f349676d3d092156442226a065fb86440603b2ac.workbench",
    name="cifti-stats",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_CiftiStatsRoiParamsDictNoTag = typing.TypedDict('_CiftiStatsRoiParamsDictNoTag', {
    "roi-cifti": InputPathType,
    "match-maps": bool,
})
CiftiStatsRoiParamsDictTagged = typing.TypedDict('CiftiStatsRoiParamsDictTagged', {
    "@type": typing.Literal["roi"],
    "roi-cifti": InputPathType,
    "match-maps": bool,
})
CiftiStatsRoiParamsDict = _CiftiStatsRoiParamsDictNoTag | CiftiStatsRoiParamsDictTagged


_CiftiStatsParamsDictNoTag = typing.TypedDict('_CiftiStatsParamsDictNoTag', {
    "cifti-in": InputPathType,
    "roi": typing.NotRequired[CiftiStatsRoiParamsDict | None],
    "column": typing.NotRequired[int | None],
    "percent": typing.NotRequired[float | None],
    "operation": typing.NotRequired[str | None],
    "show-map-name": bool,
})
CiftiStatsParamsDictTagged = typing.TypedDict('CiftiStatsParamsDictTagged', {
    "@type": typing.Literal["workbench/cifti-stats"],
    "cifti-in": InputPathType,
    "roi": typing.NotRequired[CiftiStatsRoiParamsDict | None],
    "column": typing.NotRequired[int | None],
    "percent": typing.NotRequired[float | None],
    "operation": typing.NotRequired[str | None],
    "show-map-name": bool,
})
CiftiStatsParamsDict = _CiftiStatsParamsDictNoTag | CiftiStatsParamsDictTagged


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


def cifti_stats_roi_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiStatsRoiParamsDict` 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-cifti", None) is None:
        raise StyxValidationError("`roi-cifti` must not be None")
    if not isinstance(params["roi-cifti"], (pathlib.Path, str)):
        raise StyxValidationError(f'`roi-cifti` has the wrong type: Received `{type(params.get("roi-cifti", 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 cifti_stats_roi_cargs(
    params: CiftiStatsRoiParamsDict,
    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-cifti", None))
    ])
    if params.get("match-maps", False):
        cargs.append("-match-maps")
    return cargs


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


def cifti_stats_params(
    cifti_in: InputPathType,
    roi: CiftiStatsRoiParamsDict | None = None,
    column: int | None = None,
    percent: float | None = None,
    operation: str | None = None,
    show_map_name: bool = False,
) -> CiftiStatsParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_in: the input cifti.
        roi: only consider data inside an roi.
        column: only display output for one column\
            \
            the column index (starting from 1).
        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 column index and name before each output.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-stats",
        "cifti-in": cifti_in,
        "show-map-name": show_map_name,
    }
    if roi is not None:
        params["roi"] = roi
    if column is not None:
        params["column"] = column
    if percent is not None:
        params["percent"] = percent
    if operation is not None:
        params["operation"] = operation
    return params


def cifti_stats_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiStatsParamsDict` 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("cifti-in", None) is None:
        raise StyxValidationError("`cifti-in` must not be None")
    if not isinstance(params["cifti-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`cifti-in` has the wrong type: Received `{type(params.get("cifti-in", None))}` expected `InputPathType`')
    if params.get("roi", None) is not None:
        cifti_stats_roi_validate(params["roi"])
    if params.get("column", None) is not None:
        if not isinstance(params["column"], int):
            raise StyxValidationError(f'`column` has the wrong type: Received `{type(params.get("column", None))}` expected `int | 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 cifti_stats_cargs(
    params: CiftiStatsParamsDict,
    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",
        "-cifti-stats"
    ])
    cargs.append(execution.input_file(params.get("cifti-in", None)))
    if params.get("roi", None) is not None:
        cargs.extend(cifti_stats_roi_cargs(params.get("roi", None), execution))
    if params.get("column", None) is not None:
        cargs.extend([
            "-column",
            str(params.get("column", 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 cifti_stats_outputs(
    params: CiftiStatsParamsDict,
    execution: Execution,
) -> CiftiStatsOutputs:
    """
    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 = CiftiStatsOutputs(
        root=execution.output_file("."),
    )
    return ret


def cifti_stats_execute(
    params: CiftiStatsParamsDict,
    runner: Runner | None = None,
) -> CiftiStatsOutputs:
    """
    STATISTICS ALONG CIFTI COLUMNS.
    
    For each column of the input, a line of text is printed, resulting from the
    specified reduction or percentile operation. If -roi is specified without
    -match-maps, then each line will contain as many numbers as there are maps
    in the ROI file, separated by tab characters. Use -column to only give
    output for a single data column. 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 `CiftiStatsOutputs`).
    """
    cifti_stats_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_STATS_METADATA)
    params = execution.params(params)
    cargs = cifti_stats_cargs(params, execution)
    ret = cifti_stats_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_stats(
    cifti_in: InputPathType,
    roi: CiftiStatsRoiParamsDict | None = None,
    column: int | None = None,
    percent: float | None = None,
    operation: str | None = None,
    show_map_name: bool = False,
    runner: Runner | None = None,
) -> CiftiStatsOutputs:
    """
    STATISTICS ALONG CIFTI COLUMNS.
    
    For each column of the input, a line of text is printed, resulting from the
    specified reduction or percentile operation. If -roi is specified without
    -match-maps, then each line will contain as many numbers as there are maps
    in the ROI file, separated by tab characters. Use -column to only give
    output for a single data column. 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:
        cifti_in: the input cifti.
        roi: only consider data inside an roi.
        column: only display output for one column\
            \
            the column index (starting from 1).
        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 column index and name before each output.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiStatsOutputs`).
    """
    params = cifti_stats_params(
        cifti_in=cifti_in,
        roi=roi,
        column=column,
        percent=percent,
        operation=operation,
        show_map_name=show_map_name,
    )
    return cifti_stats_execute(params, runner)


__all__ = [
    "CIFTI_STATS_METADATA",
    "CiftiStatsOutputs",
    "CiftiStatsParamsDict",
    "CiftiStatsParamsDictTagged",
    "CiftiStatsRoiParamsDict",
    "CiftiStatsRoiParamsDictTagged",
    "cifti_stats",
    "cifti_stats_execute",
    "cifti_stats_params",
    "cifti_stats_roi",
]
