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

import typing
import pathlib
from styxdefs import *

VOLUME_LABEL_IMPORT_METADATA = Metadata(
    id="8ebfebc97768bf77ee4f3467a6f745021a8c3a59.workbench",
    name="volume-label-import",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


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


class VolumeLabelImportOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeLabelImportParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output: OutputPathType
    """the output workbench label volume"""


def volume_label_import_params(
    input_: InputPathType,
    label_list_file: str,
    output: str,
    file: str | None = None,
    subvol: str | None = None,
    value: int | None = None,
    drop_unused_labels: bool = False,
    discard_others: bool = False,
) -> VolumeLabelImportParamsDictTagged:
    """
    Build parameters.
    
    Args:
        input_: the input volume file.
        label_list_file: text file containing the values and names for labels.
        output: the output workbench label volume.
        file: read label name hierarchy from a json file\
            \
            the input json file.
        subvol: select a single subvolume to import\
            \
            the subvolume number or name.
        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 voxels with values not mentioned in the label\
            list to the ??? label.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-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 subvol is not None:
        params["subvol"] = subvol
    if value is not None:
        params["value"] = value
    return params


def volume_label_import_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeLabelImportParamsDict` 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("subvol", None) is not None:
        if not isinstance(params["subvol"], str):
            raise StyxValidationError(f'`subvol` has the wrong type: Received `{type(params.get("subvol", 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 volume_label_import_cargs(
    params: VolumeLabelImportParamsDict,
    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",
        "-volume-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("subvol", None) is not None:
        cargs.extend([
            "-subvolume",
            params.get("subvol", 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 volume_label_import_outputs(
    params: VolumeLabelImportParamsDict,
    execution: Execution,
) -> VolumeLabelImportOutputs:
    """
    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 = VolumeLabelImportOutputs(
        root=execution.output_file("."),
        output=execution.output_file(params.get("output", None)),
    )
    return ret


def volume_label_import_execute(
    params: VolumeLabelImportParamsDict,
    runner: Runner | None = None,
) -> VolumeLabelImportOutputs:
    """
    IMPORT A LABEL VOLUME TO WORKBENCH FORMAT.
    
    Creates a label volume from an integer-valued volume file. The label name
    and color information is stored in the volume header in a nifti extension,
    with a similar format as in caret5, see -volume-help. 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 `VolumeLabelImportOutputs`).
    """
    volume_label_import_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_LABEL_IMPORT_METADATA)
    params = execution.params(params)
    cargs = volume_label_import_cargs(params, execution)
    ret = volume_label_import_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_label_import(
    input_: InputPathType,
    label_list_file: str,
    output: str,
    file: str | None = None,
    subvol: str | None = None,
    value: int | None = None,
    drop_unused_labels: bool = False,
    discard_others: bool = False,
    runner: Runner | None = None,
) -> VolumeLabelImportOutputs:
    """
    IMPORT A LABEL VOLUME TO WORKBENCH FORMAT.
    
    Creates a label volume from an integer-valued volume file. The label name
    and color information is stored in the volume header in a nifti extension,
    with a similar format as in caret5, see -volume-help. 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 volume file.
        label_list_file: text file containing the values and names for labels.
        output: the output workbench label volume.
        file: read label name hierarchy from a json file\
            \
            the input json file.
        subvol: select a single subvolume to import\
            \
            the subvolume number or name.
        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 voxels with values not mentioned in the label\
            list to the ??? label.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeLabelImportOutputs`).
    """
    params = volume_label_import_params(
        input_=input_,
        label_list_file=label_list_file,
        output=output,
        file=file,
        subvol=subvol,
        value=value,
        drop_unused_labels=drop_unused_labels,
        discard_others=discard_others,
    )
    return volume_label_import_execute(params, runner)


__all__ = [
    "VOLUME_LABEL_IMPORT_METADATA",
    "VolumeLabelImportOutputs",
    "VolumeLabelImportParamsDict",
    "VolumeLabelImportParamsDictTagged",
    "volume_label_import",
    "volume_label_import_execute",
    "volume_label_import_params",
]
