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

import typing
import pathlib
from styxdefs import *

CIFTI_AVERAGE_ROI_CORRELATION_METADATA = Metadata(
    id="8675ac82eeff102d67fcb3c8cd17fa1826495162.workbench",
    name="cifti-average-roi-correlation",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_CiftiAverageRoiCorrelationCiftiRoiParamsDictNoTag = typing.TypedDict('_CiftiAverageRoiCorrelationCiftiRoiParamsDictNoTag', {
    "roi-cifti": InputPathType,
    "in-memory": bool,
})
CiftiAverageRoiCorrelationCiftiRoiParamsDictTagged = typing.TypedDict('CiftiAverageRoiCorrelationCiftiRoiParamsDictTagged', {
    "@type": typing.Literal["cifti-roi"],
    "roi-cifti": InputPathType,
    "in-memory": bool,
})
CiftiAverageRoiCorrelationCiftiRoiParamsDict = _CiftiAverageRoiCorrelationCiftiRoiParamsDictNoTag | CiftiAverageRoiCorrelationCiftiRoiParamsDictTagged


_CiftiAverageRoiCorrelationCiftiParamsDictNoTag = typing.TypedDict('_CiftiAverageRoiCorrelationCiftiParamsDictNoTag', {
    "cifti-in": InputPathType,
})
CiftiAverageRoiCorrelationCiftiParamsDictTagged = typing.TypedDict('CiftiAverageRoiCorrelationCiftiParamsDictTagged', {
    "@type": typing.Literal["cifti"],
    "cifti-in": InputPathType,
})
CiftiAverageRoiCorrelationCiftiParamsDict = _CiftiAverageRoiCorrelationCiftiParamsDictNoTag | CiftiAverageRoiCorrelationCiftiParamsDictTagged


_CiftiAverageRoiCorrelationParamsDictNoTag = typing.TypedDict('_CiftiAverageRoiCorrelationParamsDictNoTag', {
    "cifti-out": str,
    "cifti-roi": typing.NotRequired[CiftiAverageRoiCorrelationCiftiRoiParamsDict | None],
    "cifti": typing.NotRequired[list[CiftiAverageRoiCorrelationCiftiParamsDict] | None],
    "cerebellum-surf": typing.NotRequired[InputPathType | None],
    "right-surf": typing.NotRequired[InputPathType | None],
    "left-surf": typing.NotRequired[InputPathType | None],
    "roi-vol": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
})
CiftiAverageRoiCorrelationParamsDictTagged = typing.TypedDict('CiftiAverageRoiCorrelationParamsDictTagged', {
    "@type": typing.Literal["workbench/cifti-average-roi-correlation"],
    "cifti-out": str,
    "cifti-roi": typing.NotRequired[CiftiAverageRoiCorrelationCiftiRoiParamsDict | None],
    "cifti": typing.NotRequired[list[CiftiAverageRoiCorrelationCiftiParamsDict] | None],
    "cerebellum-surf": typing.NotRequired[InputPathType | None],
    "right-surf": typing.NotRequired[InputPathType | None],
    "left-surf": typing.NotRequired[InputPathType | None],
    "roi-vol": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
})
CiftiAverageRoiCorrelationParamsDict = _CiftiAverageRoiCorrelationParamsDictNoTag | CiftiAverageRoiCorrelationParamsDictTagged


def cifti_average_roi_correlation_cifti_roi(
    roi_cifti: InputPathType,
    in_memory: bool = False,
) -> CiftiAverageRoiCorrelationCiftiRoiParamsDictTagged:
    """
    Build parameters.
    
    Args:
        roi_cifti: the roi cifti file.
        in_memory: cache the roi in memory so that it isn't re-read for each\
            input cifti.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "cifti-roi",
        "roi-cifti": roi_cifti,
        "in-memory": in_memory,
    }
    return params


def cifti_average_roi_correlation_cifti_roi_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiAverageRoiCorrelationCiftiRoiParamsDict` 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("in-memory", False) is None:
        raise StyxValidationError("`in-memory` must not be None")
    if not isinstance(params["in-memory"], bool):
        raise StyxValidationError(f'`in-memory` has the wrong type: Received `{type(params.get("in-memory", False))}` expected `bool`')


def cifti_average_roi_correlation_cifti_roi_cargs(
    params: CiftiAverageRoiCorrelationCiftiRoiParamsDict,
    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([
        "-cifti-roi",
        execution.input_file(params.get("roi-cifti", None))
    ])
    if params.get("in-memory", False):
        cargs.append("-in-memory")
    return cargs


def cifti_average_roi_correlation_cifti(
    cifti_in: InputPathType,
) -> CiftiAverageRoiCorrelationCiftiParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_in: a cifti file to average across.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "cifti",
        "cifti-in": cifti_in,
    }
    return params


def cifti_average_roi_correlation_cifti_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiAverageRoiCorrelationCiftiParamsDict` 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`')


def cifti_average_roi_correlation_cifti_cargs(
    params: CiftiAverageRoiCorrelationCiftiParamsDict,
    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([
        "-cifti",
        execution.input_file(params.get("cifti-in", None))
    ])
    return cargs


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


def cifti_average_roi_correlation_params(
    cifti_out: str,
    cifti_roi: CiftiAverageRoiCorrelationCiftiRoiParamsDict | None = None,
    cifti: list[CiftiAverageRoiCorrelationCiftiParamsDict] | None = None,
    cerebellum_surf: InputPathType | None = None,
    right_surf: InputPathType | None = None,
    left_surf: InputPathType | None = None,
    roi_vol: InputPathType | None = None,
    roi_metric: InputPathType | None = None,
    roi_metric_: InputPathType | None = None,
    roi_metric_2: InputPathType | None = None,
) -> CiftiAverageRoiCorrelationParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_out: output cifti file.
        cifti_roi: cifti file containing combined weights.
        cifti: specify an input cifti file.
        cerebellum_surf: specify the cerebellum surface for vertex area\
            correction\
            \
            the cerebellum surface file.
        right_surf: specify the right surface for vertex area correction\
            \
            the right surface file.
        left_surf: specify the left surface for vertex area correction\
            \
            the left surface file.
        roi_vol: voxel weights to use\
            \
            the roi volume file.
        roi_metric: weights to use for cerebellum surface\
            \
            the cerebellum roi as a metric file.
        roi_metric_: weights to use for right hempsphere\
            \
            the right roi as a metric file.
        roi_metric_2: weights to use for left hempsphere\
            \
            the left roi as a metric file.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-average-roi-correlation",
        "cifti-out": cifti_out,
    }
    if cifti_roi is not None:
        params["cifti-roi"] = cifti_roi
    if cifti is not None:
        params["cifti"] = cifti
    if cerebellum_surf is not None:
        params["cerebellum-surf"] = cerebellum_surf
    if right_surf is not None:
        params["right-surf"] = right_surf
    if left_surf is not None:
        params["left-surf"] = left_surf
    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
    return params


def cifti_average_roi_correlation_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiAverageRoiCorrelationParamsDict` 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-out", None) is None:
        raise StyxValidationError("`cifti-out` must not be None")
    if not isinstance(params["cifti-out"], str):
        raise StyxValidationError(f'`cifti-out` has the wrong type: Received `{type(params.get("cifti-out", None))}` expected `str`')
    if params.get("cifti-roi", None) is not None:
        cifti_average_roi_correlation_cifti_roi_validate(params["cifti-roi"])
    if params.get("cifti", None) is not None:
        if not isinstance(params["cifti"], list):
            raise StyxValidationError(f'`cifti` has the wrong type: Received `{type(params.get("cifti", None))}` expected `list[CiftiAverageRoiCorrelationCiftiParamsDict] | None`')
        for e in params["cifti"]:
            cifti_average_roi_correlation_cifti_validate(e)
    if params.get("cerebellum-surf", None) is not None:
        if not isinstance(params["cerebellum-surf"], (pathlib.Path, str)):
            raise StyxValidationError(f'`cerebellum-surf` has the wrong type: Received `{type(params.get("cerebellum-surf", None))}` expected `InputPathType | None`')
    if params.get("right-surf", None) is not None:
        if not isinstance(params["right-surf"], (pathlib.Path, str)):
            raise StyxValidationError(f'`right-surf` has the wrong type: Received `{type(params.get("right-surf", None))}` expected `InputPathType | None`')
    if params.get("left-surf", None) is not None:
        if not isinstance(params["left-surf"], (pathlib.Path, str)):
            raise StyxValidationError(f'`left-surf` has the wrong type: Received `{type(params.get("left-surf", None))}` expected `InputPathType | None`')
    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`')


def cifti_average_roi_correlation_cargs(
    params: CiftiAverageRoiCorrelationParamsDict,
    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-average-roi-correlation"
    ])
    cargs.append(params.get("cifti-out", None))
    if params.get("cifti-roi", None) is not None or params.get("cifti", None) is not None:
        cargs.extend([
            *(cifti_average_roi_correlation_cifti_roi_cargs(params.get("cifti-roi", None), execution) if (params.get("cifti-roi", None) is not None) else []),
            *([a for c in [cifti_average_roi_correlation_cifti_cargs(s, execution) for s in params.get("cifti", None)] for a in c] if (params.get("cifti", None) is not None) else [])
        ])
    if params.get("cerebellum-surf", None) is not None:
        cargs.extend([
            "-cerebellum-area-surf",
            execution.input_file(params.get("cerebellum-surf", None))
        ])
    if params.get("right-surf", None) is not None:
        cargs.extend([
            "-right-area-surf",
            execution.input_file(params.get("right-surf", None))
        ])
    if params.get("left-surf", None) is not None:
        cargs.extend([
            "-left-area-surf",
            execution.input_file(params.get("left-surf", 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))
        ])
    return cargs


def cifti_average_roi_correlation_outputs(
    params: CiftiAverageRoiCorrelationParamsDict,
    execution: Execution,
) -> CiftiAverageRoiCorrelationOutputs:
    """
    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 = CiftiAverageRoiCorrelationOutputs(
        root=execution.output_file("."),
        cifti_out=execution.output_file(params.get("cifti-out", None)),
    )
    return ret


def cifti_average_roi_correlation_execute(
    params: CiftiAverageRoiCorrelationParamsDict,
    runner: Runner | None = None,
) -> CiftiAverageRoiCorrelationOutputs:
    """
    CORRELATE ROI AVERAGE WITH ALL ROWS THEN AVERAGE ACROSS SUBJECTS.
    
    Averages rows for each map of the ROI(s), takes the correlation of each ROI
    average to the rest of the rows in the same file, applies the fisher small z
    transform, then averages the results across all files. ROIs are always
    treated as weighting functions, including negative values. For efficiency,
    ensure that everything that is not intended to be used is zero in the ROI
    map. If -cifti-roi is specified, -left-roi, -right-roi, -cerebellum-roi, and
    -vol-roi must not be specified. If multiple non-cifti ROI files are
    specified, they must have the same number of columns.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiAverageRoiCorrelationOutputs`).
    """
    cifti_average_roi_correlation_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_AVERAGE_ROI_CORRELATION_METADATA)
    params = execution.params(params)
    cargs = cifti_average_roi_correlation_cargs(params, execution)
    ret = cifti_average_roi_correlation_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_average_roi_correlation(
    cifti_out: str,
    cifti_roi: CiftiAverageRoiCorrelationCiftiRoiParamsDict | None = None,
    cifti: list[CiftiAverageRoiCorrelationCiftiParamsDict] | None = None,
    cerebellum_surf: InputPathType | None = None,
    right_surf: InputPathType | None = None,
    left_surf: InputPathType | None = None,
    roi_vol: InputPathType | None = None,
    roi_metric: InputPathType | None = None,
    roi_metric_: InputPathType | None = None,
    roi_metric_2: InputPathType | None = None,
    runner: Runner | None = None,
) -> CiftiAverageRoiCorrelationOutputs:
    """
    CORRELATE ROI AVERAGE WITH ALL ROWS THEN AVERAGE ACROSS SUBJECTS.
    
    Averages rows for each map of the ROI(s), takes the correlation of each ROI
    average to the rest of the rows in the same file, applies the fisher small z
    transform, then averages the results across all files. ROIs are always
    treated as weighting functions, including negative values. For efficiency,
    ensure that everything that is not intended to be used is zero in the ROI
    map. If -cifti-roi is specified, -left-roi, -right-roi, -cerebellum-roi, and
    -vol-roi must not be specified. If multiple non-cifti ROI files are
    specified, they must have the same number of columns.
    
    Args:
        cifti_out: output cifti file.
        cifti_roi: cifti file containing combined weights.
        cifti: specify an input cifti file.
        cerebellum_surf: specify the cerebellum surface for vertex area\
            correction\
            \
            the cerebellum surface file.
        right_surf: specify the right surface for vertex area correction\
            \
            the right surface file.
        left_surf: specify the left surface for vertex area correction\
            \
            the left surface file.
        roi_vol: voxel weights to use\
            \
            the roi volume file.
        roi_metric: weights to use for cerebellum surface\
            \
            the cerebellum roi as a metric file.
        roi_metric_: weights to use for right hempsphere\
            \
            the right roi as a metric file.
        roi_metric_2: weights to use for left hempsphere\
            \
            the left roi as a metric file.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiAverageRoiCorrelationOutputs`).
    """
    params = cifti_average_roi_correlation_params(
        cifti_out=cifti_out,
        cifti_roi=cifti_roi,
        cifti=cifti,
        cerebellum_surf=cerebellum_surf,
        right_surf=right_surf,
        left_surf=left_surf,
        roi_vol=roi_vol,
        roi_metric=roi_metric,
        roi_metric_=roi_metric_,
        roi_metric_2=roi_metric_2,
    )
    return cifti_average_roi_correlation_execute(params, runner)


__all__ = [
    "CIFTI_AVERAGE_ROI_CORRELATION_METADATA",
    "CiftiAverageRoiCorrelationCiftiParamsDict",
    "CiftiAverageRoiCorrelationCiftiParamsDictTagged",
    "CiftiAverageRoiCorrelationCiftiRoiParamsDict",
    "CiftiAverageRoiCorrelationCiftiRoiParamsDictTagged",
    "CiftiAverageRoiCorrelationOutputs",
    "CiftiAverageRoiCorrelationParamsDict",
    "CiftiAverageRoiCorrelationParamsDictTagged",
    "cifti_average_roi_correlation",
    "cifti_average_roi_correlation_cifti",
    "cifti_average_roi_correlation_cifti_roi",
    "cifti_average_roi_correlation_execute",
    "cifti_average_roi_correlation_params",
]
