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

import typing
import pathlib
from styxdefs import *

VOLUME_LABEL_TO_ROI_METADATA = Metadata(
    id="f849408f8ec3fabb1a2cc5fda20d4180749ba643.workbench",
    name="volume-label-to-roi",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeLabelToRoiParamsDictNoTag = typing.TypedDict('_VolumeLabelToRoiParamsDictNoTag', {
    "label-in": InputPathType,
    "volume-out": str,
    "map": typing.NotRequired[str | None],
    "label-key": typing.NotRequired[int | None],
    "label-name": typing.NotRequired[str | None],
})
VolumeLabelToRoiParamsDictTagged = typing.TypedDict('VolumeLabelToRoiParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-label-to-roi"],
    "label-in": InputPathType,
    "volume-out": str,
    "map": typing.NotRequired[str | None],
    "label-key": typing.NotRequired[int | None],
    "label-name": typing.NotRequired[str | None],
})
VolumeLabelToRoiParamsDict = _VolumeLabelToRoiParamsDictNoTag | VolumeLabelToRoiParamsDictTagged


class VolumeLabelToRoiOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeLabelToRoiParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    volume_out: OutputPathType
    """the output volume file"""


def volume_label_to_roi_params(
    label_in: InputPathType,
    volume_out: str,
    map_: str | None = None,
    label_key: int | None = None,
    label_name: str | None = None,
) -> VolumeLabelToRoiParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label_in: the input volume label file.
        volume_out: the output volume file.
        map_: select a single label map to use\
            \
            the map number or name.
        label_key: select label by key\
            \
            the label key that you want an roi of.
        label_name: select label by name\
            \
            the label name that you want an roi of.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-label-to-roi",
        "label-in": label_in,
        "volume-out": volume_out,
    }
    if map_ is not None:
        params["map"] = map_
    if label_key is not None:
        params["label-key"] = label_key
    if label_name is not None:
        params["label-name"] = label_name
    return params


def volume_label_to_roi_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeLabelToRoiParamsDict` 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("label-in", None) is None:
        raise StyxValidationError("`label-in` must not be None")
    if not isinstance(params["label-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label-in` has the wrong type: Received `{type(params.get("label-in", None))}` expected `InputPathType`')
    if params.get("volume-out", None) is None:
        raise StyxValidationError("`volume-out` must not be None")
    if not isinstance(params["volume-out"], str):
        raise StyxValidationError(f'`volume-out` has the wrong type: Received `{type(params.get("volume-out", None))}` expected `str`')
    if params.get("map", None) is not None:
        if not isinstance(params["map"], str):
            raise StyxValidationError(f'`map` has the wrong type: Received `{type(params.get("map", None))}` expected `str | None`')
    if params.get("label-key", None) is not None:
        if not isinstance(params["label-key"], int):
            raise StyxValidationError(f'`label-key` has the wrong type: Received `{type(params.get("label-key", None))}` expected `int | None`')
    if params.get("label-name", None) is not None:
        if not isinstance(params["label-name"], str):
            raise StyxValidationError(f'`label-name` has the wrong type: Received `{type(params.get("label-name", None))}` expected `str | None`')


def volume_label_to_roi_cargs(
    params: VolumeLabelToRoiParamsDict,
    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-to-roi"
    ])
    cargs.append(execution.input_file(params.get("label-in", None)))
    cargs.append(params.get("volume-out", None))
    if params.get("map", None) is not None:
        cargs.extend([
            "-map",
            params.get("map", None)
        ])
    if params.get("label-key", None) is not None:
        cargs.extend([
            "-key",
            str(params.get("label-key", None))
        ])
    if params.get("label-name", None) is not None:
        cargs.extend([
            "-name",
            params.get("label-name", None)
        ])
    return cargs


def volume_label_to_roi_outputs(
    params: VolumeLabelToRoiParamsDict,
    execution: Execution,
) -> VolumeLabelToRoiOutputs:
    """
    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 = VolumeLabelToRoiOutputs(
        root=execution.output_file("."),
        volume_out=execution.output_file(params.get("volume-out", None)),
    )
    return ret


def volume_label_to_roi_execute(
    params: VolumeLabelToRoiParamsDict,
    runner: Runner | None = None,
) -> VolumeLabelToRoiOutputs:
    """
    MAKE A VOLUME LABEL INTO AN ROI VOLUME.
    
    For each map in <label-in>, a map is created in <volume-out> where all
    locations labeled with <label-name> or with a key of <label-key> are given a
    value of 1, and all other locations are given 0. Exactly one of -name and
    -key must be specified. Specify -map to use only one map from <label-in>.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeLabelToRoiOutputs`).
    """
    volume_label_to_roi_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_LABEL_TO_ROI_METADATA)
    params = execution.params(params)
    cargs = volume_label_to_roi_cargs(params, execution)
    ret = volume_label_to_roi_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_label_to_roi(
    label_in: InputPathType,
    volume_out: str,
    map_: str | None = None,
    label_key: int | None = None,
    label_name: str | None = None,
    runner: Runner | None = None,
) -> VolumeLabelToRoiOutputs:
    """
    MAKE A VOLUME LABEL INTO AN ROI VOLUME.
    
    For each map in <label-in>, a map is created in <volume-out> where all
    locations labeled with <label-name> or with a key of <label-key> are given a
    value of 1, and all other locations are given 0. Exactly one of -name and
    -key must be specified. Specify -map to use only one map from <label-in>.
    
    Args:
        label_in: the input volume label file.
        volume_out: the output volume file.
        map_: select a single label map to use\
            \
            the map number or name.
        label_key: select label by key\
            \
            the label key that you want an roi of.
        label_name: select label by name\
            \
            the label name that you want an roi of.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeLabelToRoiOutputs`).
    """
    params = volume_label_to_roi_params(
        label_in=label_in,
        volume_out=volume_out,
        map_=map_,
        label_key=label_key,
        label_name=label_name,
    )
    return volume_label_to_roi_execute(params, runner)


__all__ = [
    "VOLUME_LABEL_TO_ROI_METADATA",
    "VolumeLabelToRoiOutputs",
    "VolumeLabelToRoiParamsDict",
    "VolumeLabelToRoiParamsDictTagged",
    "volume_label_to_roi",
    "volume_label_to_roi_execute",
    "volume_label_to_roi_params",
]
