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

import typing
import pathlib
from styxdefs import *

CIFTI_CREATE_PARCELLATED_FROM_TEMPLATE_METADATA = Metadata(
    id="3f274d723b32862f2a3c97e709a1481f947e0288.workbench",
    name="cifti-create-parcellated-from-template",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_CiftiCreateParcellatedFromTemplateCiftiParamsDictNoTag = typing.TypedDict('_CiftiCreateParcellatedFromTemplateCiftiParamsDictNoTag', {
    "cifti-in": InputPathType,
})
CiftiCreateParcellatedFromTemplateCiftiParamsDictTagged = typing.TypedDict('CiftiCreateParcellatedFromTemplateCiftiParamsDictTagged', {
    "@type": typing.Literal["cifti"],
    "cifti-in": InputPathType,
})
CiftiCreateParcellatedFromTemplateCiftiParamsDict = _CiftiCreateParcellatedFromTemplateCiftiParamsDictNoTag | CiftiCreateParcellatedFromTemplateCiftiParamsDictTagged


_CiftiCreateParcellatedFromTemplateParamsDictNoTag = typing.TypedDict('_CiftiCreateParcellatedFromTemplateParamsDictNoTag', {
    "cifti-template": InputPathType,
    "modify-direction": str,
    "cifti-out": str,
    "cifti": typing.NotRequired[list[CiftiCreateParcellatedFromTemplateCiftiParamsDict] | None],
    "value": typing.NotRequired[float | None],
})
CiftiCreateParcellatedFromTemplateParamsDictTagged = typing.TypedDict('CiftiCreateParcellatedFromTemplateParamsDictTagged', {
    "@type": typing.Literal["workbench/cifti-create-parcellated-from-template"],
    "cifti-template": InputPathType,
    "modify-direction": str,
    "cifti-out": str,
    "cifti": typing.NotRequired[list[CiftiCreateParcellatedFromTemplateCiftiParamsDict] | None],
    "value": typing.NotRequired[float | None],
})
CiftiCreateParcellatedFromTemplateParamsDict = _CiftiCreateParcellatedFromTemplateParamsDictNoTag | CiftiCreateParcellatedFromTemplateParamsDictTagged


def cifti_create_parcellated_from_template_cifti(
    cifti_in: InputPathType,
) -> CiftiCreateParcellatedFromTemplateCiftiParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_in: the input parcellated cifti file.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "cifti",
        "cifti-in": cifti_in,
    }
    return params


def cifti_create_parcellated_from_template_cifti_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiCreateParcellatedFromTemplateCiftiParamsDict` 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_create_parcellated_from_template_cifti_cargs(
    params: CiftiCreateParcellatedFromTemplateCiftiParamsDict,
    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 CiftiCreateParcellatedFromTemplateOutputs(typing.NamedTuple):
    """
    Output object returned when calling `CiftiCreateParcellatedFromTemplateParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    cifti_out: OutputPathType
    """the output cifti file"""


def cifti_create_parcellated_from_template_params(
    cifti_template: InputPathType,
    modify_direction: str,
    cifti_out: str,
    cifti: list[CiftiCreateParcellatedFromTemplateCiftiParamsDict] | None = None,
    value: float | None = None,
) -> CiftiCreateParcellatedFromTemplateParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_template: a cifti file with the template parcel mapping along\
            column.
        modify_direction: which dimension of the output file should match the\
            template (integer, 'ROW', or 'COLUMN').
        cifti_out: the output cifti file.
        cifti: specify an input cifti file.
        value: specify value to be used in parcels that don't match\
            \
            value to use (default 0).
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-create-parcellated-from-template",
        "cifti-template": cifti_template,
        "modify-direction": modify_direction,
        "cifti-out": cifti_out,
    }
    if cifti is not None:
        params["cifti"] = cifti
    if value is not None:
        params["value"] = value
    return params


def cifti_create_parcellated_from_template_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiCreateParcellatedFromTemplateParamsDict` 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-template", None) is None:
        raise StyxValidationError("`cifti-template` must not be None")
    if not isinstance(params["cifti-template"], (pathlib.Path, str)):
        raise StyxValidationError(f'`cifti-template` has the wrong type: Received `{type(params.get("cifti-template", None))}` expected `InputPathType`')
    if params.get("modify-direction", None) is None:
        raise StyxValidationError("`modify-direction` must not be None")
    if not isinstance(params["modify-direction"], str):
        raise StyxValidationError(f'`modify-direction` has the wrong type: Received `{type(params.get("modify-direction", None))}` expected `str`')
    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", 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[CiftiCreateParcellatedFromTemplateCiftiParamsDict] | None`')
        for e in params["cifti"]:
            cifti_create_parcellated_from_template_cifti_validate(e)
    if params.get("value", None) is not None:
        if not isinstance(params["value"], (float, int)):
            raise StyxValidationError(f'`value` has the wrong type: Received `{type(params.get("value", None))}` expected `float | None`')


def cifti_create_parcellated_from_template_cargs(
    params: CiftiCreateParcellatedFromTemplateParamsDict,
    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-create-parcellated-from-template"
    ])
    cargs.append(execution.input_file(params.get("cifti-template", None)))
    cargs.append(params.get("modify-direction", None))
    cargs.append(params.get("cifti-out", None))
    if params.get("cifti", None) is not None:
        cargs.extend([a for c in [cifti_create_parcellated_from_template_cifti_cargs(s, execution) for s in params.get("cifti", None)] for a in c])
    if params.get("value", None) is not None:
        cargs.extend([
            "-fill-value",
            str(params.get("value", None))
        ])
    return cargs


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


def cifti_create_parcellated_from_template_execute(
    params: CiftiCreateParcellatedFromTemplateParamsDict,
    runner: Runner | None = None,
) -> CiftiCreateParcellatedFromTemplateOutputs:
    """
    MATCH PARCELS TO TEMPLATE BY NAME.
    
    For each parcel name in the template mapping, find that name in an input
    cifti file and use its data in the output file. All input cifti files must
    have a parcels mapping along <modify-direction> and matching mappings along
    other dimensions. The direction can be either an integer starting from 1, or
    the strings 'ROW' or 'COLUMN'.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiCreateParcellatedFromTemplateOutputs`).
    """
    cifti_create_parcellated_from_template_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_CREATE_PARCELLATED_FROM_TEMPLATE_METADATA)
    params = execution.params(params)
    cargs = cifti_create_parcellated_from_template_cargs(params, execution)
    ret = cifti_create_parcellated_from_template_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_create_parcellated_from_template(
    cifti_template: InputPathType,
    modify_direction: str,
    cifti_out: str,
    cifti: list[CiftiCreateParcellatedFromTemplateCiftiParamsDict] | None = None,
    value: float | None = None,
    runner: Runner | None = None,
) -> CiftiCreateParcellatedFromTemplateOutputs:
    """
    MATCH PARCELS TO TEMPLATE BY NAME.
    
    For each parcel name in the template mapping, find that name in an input
    cifti file and use its data in the output file. All input cifti files must
    have a parcels mapping along <modify-direction> and matching mappings along
    other dimensions. The direction can be either an integer starting from 1, or
    the strings 'ROW' or 'COLUMN'.
    
    Args:
        cifti_template: a cifti file with the template parcel mapping along\
            column.
        modify_direction: which dimension of the output file should match the\
            template (integer, 'ROW', or 'COLUMN').
        cifti_out: the output cifti file.
        cifti: specify an input cifti file.
        value: specify value to be used in parcels that don't match\
            \
            value to use (default 0).
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiCreateParcellatedFromTemplateOutputs`).
    """
    params = cifti_create_parcellated_from_template_params(
        cifti_template=cifti_template,
        modify_direction=modify_direction,
        cifti_out=cifti_out,
        cifti=cifti,
        value=value,
    )
    return cifti_create_parcellated_from_template_execute(params, runner)


__all__ = [
    "CIFTI_CREATE_PARCELLATED_FROM_TEMPLATE_METADATA",
    "CiftiCreateParcellatedFromTemplateCiftiParamsDict",
    "CiftiCreateParcellatedFromTemplateCiftiParamsDictTagged",
    "CiftiCreateParcellatedFromTemplateOutputs",
    "CiftiCreateParcellatedFromTemplateParamsDict",
    "CiftiCreateParcellatedFromTemplateParamsDictTagged",
    "cifti_create_parcellated_from_template",
    "cifti_create_parcellated_from_template_cifti",
    "cifti_create_parcellated_from_template_execute",
    "cifti_create_parcellated_from_template_params",
]
