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

import typing
import pathlib
from styxdefs import *

CIFTI_ROI_AVERAGE_METADATA = Metadata(
    id="1c54604ccdd07eb726448c23d9769ff3ee506272.workbench",
    name="cifti-roi-average",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_CiftiRoiAverageParamsDictNoTag = typing.TypedDict('_CiftiRoiAverageParamsDictNoTag', {
    "cifti-in": InputPathType,
    "text-out": str,
    "roi-vol": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-cifti": typing.NotRequired[InputPathType | None],
})
CiftiRoiAverageParamsDictTagged = typing.TypedDict('CiftiRoiAverageParamsDictTagged', {
    "@type": typing.Literal["workbench/cifti-roi-average"],
    "cifti-in": InputPathType,
    "text-out": str,
    "roi-vol": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-cifti": typing.NotRequired[InputPathType | None],
})
CiftiRoiAverageParamsDict = _CiftiRoiAverageParamsDictNoTag | CiftiRoiAverageParamsDictTagged


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


def cifti_roi_average_params(
    cifti_in: InputPathType,
    text_out: str,
    roi_vol: InputPathType | None = None,
    roi_metric: InputPathType | None = None,
    roi_metric_: InputPathType | None = None,
    roi_metric_2: InputPathType | None = None,
    roi_cifti: InputPathType | None = None,
) -> CiftiRoiAverageParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_in: the cifti file to average rows from.
        text_out: output text file of the average values.
        roi_vol: voxels to use\
            \
            the roi volume file.
        roi_metric: vertices to use from cerebellum\
            \
            the cerebellum roi as a metric file.
        roi_metric_: vertices to use from right hemisphere\
            \
            the right roi as a metric file.
        roi_metric_2: vertices to use from left hemisphere\
            \
            the left roi as a metric file.
        roi_cifti: cifti file containing combined rois\
            \
            the rois as a cifti file.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-roi-average",
        "cifti-in": cifti_in,
        "text-out": text_out,
    }
    if roi_vol is not None:
        params["roi-vol"] = roi_vol
    if roi_metric is not None:
        params["roi-metric"] = roi_metric
    if roi_metric_ is not None:
        params["roi-metric"] = roi_metric_
    if roi_metric_2 is not None:
        params["roi-metric"] = roi_metric_2
    if roi_cifti is not None:
        params["roi-cifti"] = roi_cifti
    return params


def cifti_roi_average_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiRoiAverageParamsDict` 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("text-out", None) is None:
        raise StyxValidationError("`text-out` must not be None")
    if not isinstance(params["text-out"], str):
        raise StyxValidationError(f'`text-out` has the wrong type: Received `{type(params.get("text-out", None))}` expected `str`')
    if params.get("roi-vol", None) is not None:
        if not isinstance(params["roi-vol"], (pathlib.Path, str)):
            raise StyxValidationError(f'`roi-vol` has the wrong type: Received `{type(params.get("roi-vol", None))}` expected `InputPathType | None`')
    if params.get("roi-metric", None) is not None:
        if not isinstance(params["roi-metric"], (pathlib.Path, str)):
            raise StyxValidationError(f'`roi-metric` has the wrong type: Received `{type(params.get("roi-metric", None))}` expected `InputPathType | None`')
    if params.get("roi-metric", None) is not None:
        if not isinstance(params["roi-metric"], (pathlib.Path, str)):
            raise StyxValidationError(f'`roi-metric` has the wrong type: Received `{type(params.get("roi-metric", None))}` expected `InputPathType | None`')
    if params.get("roi-metric", None) is not None:
        if not isinstance(params["roi-metric"], (pathlib.Path, str)):
            raise StyxValidationError(f'`roi-metric` has the wrong type: Received `{type(params.get("roi-metric", None))}` expected `InputPathType | None`')
    if params.get("roi-cifti", None) is not 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 | None`')


def cifti_roi_average_cargs(
    params: CiftiRoiAverageParamsDict,
    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-roi-average"
    ])
    cargs.append(execution.input_file(params.get("cifti-in", None)))
    cargs.append(params.get("text-out", None))
    if params.get("roi-vol", None) is not None:
        cargs.extend([
            "-vol-roi",
            execution.input_file(params.get("roi-vol", None))
        ])
    if params.get("roi-metric", None) is not None:
        cargs.extend([
            "-cerebellum-roi",
            execution.input_file(params.get("roi-metric", None))
        ])
    if params.get("roi-metric", None) is not None:
        cargs.extend([
            "-right-roi",
            execution.input_file(params.get("roi-metric", None))
        ])
    if params.get("roi-metric", None) is not None:
        cargs.extend([
            "-left-roi",
            execution.input_file(params.get("roi-metric", None))
        ])
    if params.get("roi-cifti", None) is not None:
        cargs.extend([
            "-cifti-roi",
            execution.input_file(params.get("roi-cifti", None))
        ])
    return cargs


def cifti_roi_average_outputs(
    params: CiftiRoiAverageParamsDict,
    execution: Execution,
) -> CiftiRoiAverageOutputs:
    """
    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 = CiftiRoiAverageOutputs(
        root=execution.output_file("."),
    )
    return ret


def cifti_roi_average_execute(
    params: CiftiRoiAverageParamsDict,
    runner: Runner | None = None,
) -> CiftiRoiAverageOutputs:
    """
    AVERAGE ROWS IN A SINGLE CIFTI FILE.
    
    Average the rows that are within the specified ROIs, and write the resulting
    average row to a text file, separated by newlines. If -cifti-roi is
    specified, -left-roi, -right-roi, -cerebellum-roi, and -vol-roi must not be
    specified.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiRoiAverageOutputs`).
    """
    cifti_roi_average_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_ROI_AVERAGE_METADATA)
    params = execution.params(params)
    cargs = cifti_roi_average_cargs(params, execution)
    ret = cifti_roi_average_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_roi_average(
    cifti_in: InputPathType,
    text_out: str,
    roi_vol: InputPathType | None = None,
    roi_metric: InputPathType | None = None,
    roi_metric_: InputPathType | None = None,
    roi_metric_2: InputPathType | None = None,
    roi_cifti: InputPathType | None = None,
    runner: Runner | None = None,
) -> CiftiRoiAverageOutputs:
    """
    AVERAGE ROWS IN A SINGLE CIFTI FILE.
    
    Average the rows that are within the specified ROIs, and write the resulting
    average row to a text file, separated by newlines. If -cifti-roi is
    specified, -left-roi, -right-roi, -cerebellum-roi, and -vol-roi must not be
    specified.
    
    Args:
        cifti_in: the cifti file to average rows from.
        text_out: output text file of the average values.
        roi_vol: voxels to use\
            \
            the roi volume file.
        roi_metric: vertices to use from cerebellum\
            \
            the cerebellum roi as a metric file.
        roi_metric_: vertices to use from right hemisphere\
            \
            the right roi as a metric file.
        roi_metric_2: vertices to use from left hemisphere\
            \
            the left roi as a metric file.
        roi_cifti: cifti file containing combined rois\
            \
            the rois as a cifti file.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiRoiAverageOutputs`).
    """
    params = cifti_roi_average_params(
        cifti_in=cifti_in,
        text_out=text_out,
        roi_vol=roi_vol,
        roi_metric=roi_metric,
        roi_metric_=roi_metric_,
        roi_metric_2=roi_metric_2,
        roi_cifti=roi_cifti,
    )
    return cifti_roi_average_execute(params, runner)


__all__ = [
    "CIFTI_ROI_AVERAGE_METADATA",
    "CiftiRoiAverageOutputs",
    "CiftiRoiAverageParamsDict",
    "CiftiRoiAverageParamsDictTagged",
    "cifti_roi_average",
    "cifti_roi_average_execute",
    "cifti_roi_average_params",
]
