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

import typing
import pathlib
from styxdefs import *

CIFTI_LABEL_IMPORT_METADATA = Metadata(
    id="a6ac49efad7e33730c569a6154b9c1ecb184a663.workbench",
    name="cifti-label-import",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_CiftiLabelImportParamsDictNoTag = typing.TypedDict('_CiftiLabelImportParamsDictNoTag', {
    "input": InputPathType,
    "label-list-file": str,
    "output": str,
    "file": typing.NotRequired[str | None],
    "value": typing.NotRequired[int | None],
    "drop-unused-labels": bool,
    "discard-others": bool,
})
CiftiLabelImportParamsDictTagged = typing.TypedDict('CiftiLabelImportParamsDictTagged', {
    "@type": typing.Literal["workbench/cifti-label-import"],
    "input": InputPathType,
    "label-list-file": str,
    "output": str,
    "file": typing.NotRequired[str | None],
    "value": typing.NotRequired[int | None],
    "drop-unused-labels": bool,
    "discard-others": bool,
})
CiftiLabelImportParamsDict = _CiftiLabelImportParamsDictNoTag | CiftiLabelImportParamsDictTagged


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


def cifti_label_import_params(
    input_: InputPathType,
    label_list_file: str,
    output: str,
    file: str | None = None,
    value: int | None = None,
    drop_unused_labels: bool = False,
    discard_others: bool = False,
) -> CiftiLabelImportParamsDictTagged:
    """
    Build parameters.
    
    Args:
        input_: the input cifti file.
        label_list_file: text file containing the values and names for labels.
        output: the output cifti label file.
        file: read label name hierarchy from a json file\
            \
            the input json file.
        value: set the value that will be interpreted as unlabeled\
            \
            the numeric value for unlabeled (default 0).
        drop_unused_labels: remove any unused label values from the label table.
        discard_others: set any values not mentioned in the label list to the\
            ??? label.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-label-import",
        "input": input_,
        "label-list-file": label_list_file,
        "output": output,
        "drop-unused-labels": drop_unused_labels,
        "discard-others": discard_others,
    }
    if file is not None:
        params["file"] = file
    if value is not None:
        params["value"] = value
    return params


def cifti_label_import_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiLabelImportParamsDict` 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("input", None) is None:
        raise StyxValidationError("`input` must not be None")
    if not isinstance(params["input"], (pathlib.Path, str)):
        raise StyxValidationError(f'`input` has the wrong type: Received `{type(params.get("input", None))}` expected `InputPathType`')
    if params.get("label-list-file", None) is None:
        raise StyxValidationError("`label-list-file` must not be None")
    if not isinstance(params["label-list-file"], str):
        raise StyxValidationError(f'`label-list-file` has the wrong type: Received `{type(params.get("label-list-file", None))}` expected `str`')
    if params.get("output", None) is None:
        raise StyxValidationError("`output` must not be None")
    if not isinstance(params["output"], str):
        raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `str`')
    if params.get("file", None) is not None:
        if not isinstance(params["file"], str):
            raise StyxValidationError(f'`file` has the wrong type: Received `{type(params.get("file", None))}` expected `str | None`')
    if params.get("value", None) is not None:
        if not isinstance(params["value"], int):
            raise StyxValidationError(f'`value` has the wrong type: Received `{type(params.get("value", None))}` expected `int | None`')
    if params.get("drop-unused-labels", False) is None:
        raise StyxValidationError("`drop-unused-labels` must not be None")
    if not isinstance(params["drop-unused-labels"], bool):
        raise StyxValidationError(f'`drop-unused-labels` has the wrong type: Received `{type(params.get("drop-unused-labels", False))}` expected `bool`')
    if params.get("discard-others", False) is None:
        raise StyxValidationError("`discard-others` must not be None")
    if not isinstance(params["discard-others"], bool):
        raise StyxValidationError(f'`discard-others` has the wrong type: Received `{type(params.get("discard-others", False))}` expected `bool`')


def cifti_label_import_cargs(
    params: CiftiLabelImportParamsDict,
    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-label-import"
    ])
    cargs.append(execution.input_file(params.get("input", None)))
    cargs.append(params.get("label-list-file", None))
    cargs.append(params.get("output", None))
    if params.get("file", None) is not None:
        cargs.extend([
            "-hierarchy",
            params.get("file", None)
        ])
    if params.get("value", None) is not None:
        cargs.extend([
            "-unlabeled-value",
            str(params.get("value", None))
        ])
    if params.get("drop-unused-labels", False):
        cargs.append("-drop-unused-labels")
    if params.get("discard-others", False):
        cargs.append("-discard-others")
    return cargs


def cifti_label_import_outputs(
    params: CiftiLabelImportParamsDict,
    execution: Execution,
) -> CiftiLabelImportOutputs:
    """
    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 = CiftiLabelImportOutputs(
        root=execution.output_file("."),
        output=execution.output_file(params.get("output", None)),
    )
    return ret


def cifti_label_import_execute(
    params: CiftiLabelImportParamsDict,
    runner: Runner | None = None,
) -> CiftiLabelImportOutputs:
    """
    MAKE A CIFTI LABEL FILE FROM A CIFTI FILE.
    
    Creates a cifti label file from a cifti file with label-like values. You may
    specify the empty string (use "") for <label-list-file>, which will be
    treated as if it is an empty file. The label list file must have the
    following format (2 lines per label):
    
    <labelname>
    <key> <red> <green> <blue> <alpha>
    ...
    
    Label names are specified on a separate line from their value and color, in
    order to let label names contain spaces. Whitespace is trimmed from both
    ends of the label name, but is kept if it is in the middle of a label. Do
    not specify the "unlabeled" key in the file, it is assumed that 0 means not
    labeled unless -unlabeled-value is specified. The value of <key> specifies
    what value in the imported file should be used as this label (these same key
    values are also used in the output file). The values of <red>, <green>,
    <blue> and <alpha> must be integers from 0 to 255, and will specify the
    color the label is drawn as (alpha of 255 means fully opaque, which is
    probably what you want).
    
    By default, it will create new label names with names like LABEL_5 for any
    values encountered that are not mentioned in the list file, specify
    -discard-others to instead set these values to the "unlabeled" key.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiLabelImportOutputs`).
    """
    cifti_label_import_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_LABEL_IMPORT_METADATA)
    params = execution.params(params)
    cargs = cifti_label_import_cargs(params, execution)
    ret = cifti_label_import_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_label_import(
    input_: InputPathType,
    label_list_file: str,
    output: str,
    file: str | None = None,
    value: int | None = None,
    drop_unused_labels: bool = False,
    discard_others: bool = False,
    runner: Runner | None = None,
) -> CiftiLabelImportOutputs:
    """
    MAKE A CIFTI LABEL FILE FROM A CIFTI FILE.
    
    Creates a cifti label file from a cifti file with label-like values. You may
    specify the empty string (use "") for <label-list-file>, which will be
    treated as if it is an empty file. The label list file must have the
    following format (2 lines per label):
    
    <labelname>
    <key> <red> <green> <blue> <alpha>
    ...
    
    Label names are specified on a separate line from their value and color, in
    order to let label names contain spaces. Whitespace is trimmed from both
    ends of the label name, but is kept if it is in the middle of a label. Do
    not specify the "unlabeled" key in the file, it is assumed that 0 means not
    labeled unless -unlabeled-value is specified. The value of <key> specifies
    what value in the imported file should be used as this label (these same key
    values are also used in the output file). The values of <red>, <green>,
    <blue> and <alpha> must be integers from 0 to 255, and will specify the
    color the label is drawn as (alpha of 255 means fully opaque, which is
    probably what you want).
    
    By default, it will create new label names with names like LABEL_5 for any
    values encountered that are not mentioned in the list file, specify
    -discard-others to instead set these values to the "unlabeled" key.
    
    Args:
        input_: the input cifti file.
        label_list_file: text file containing the values and names for labels.
        output: the output cifti label file.
        file: read label name hierarchy from a json file\
            \
            the input json file.
        value: set the value that will be interpreted as unlabeled\
            \
            the numeric value for unlabeled (default 0).
        drop_unused_labels: remove any unused label values from the label table.
        discard_others: set any values not mentioned in the label list to the\
            ??? label.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiLabelImportOutputs`).
    """
    params = cifti_label_import_params(
        input_=input_,
        label_list_file=label_list_file,
        output=output,
        file=file,
        value=value,
        drop_unused_labels=drop_unused_labels,
        discard_others=discard_others,
    )
    return cifti_label_import_execute(params, runner)


__all__ = [
    "CIFTI_LABEL_IMPORT_METADATA",
    "CiftiLabelImportOutputs",
    "CiftiLabelImportParamsDict",
    "CiftiLabelImportParamsDictTagged",
    "cifti_label_import",
    "cifti_label_import_execute",
    "cifti_label_import_params",
]
