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

import typing
import pathlib
from styxdefs import *

LABEL_DILATE_METADATA = Metadata(
    id="e02283fd8307f04426742148eee8e3e920bc3fac.workbench",
    name="label-dilate",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_LabelDilateParamsDictNoTag = typing.TypedDict('_LabelDilateParamsDictNoTag', {
    "label": InputPathType,
    "surface": InputPathType,
    "dilate-dist": float,
    "label-out": str,
    "area-metric": typing.NotRequired[InputPathType | None],
    "column": typing.NotRequired[str | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
})
LabelDilateParamsDictTagged = typing.TypedDict('LabelDilateParamsDictTagged', {
    "@type": typing.Literal["workbench/label-dilate"],
    "label": InputPathType,
    "surface": InputPathType,
    "dilate-dist": float,
    "label-out": str,
    "area-metric": typing.NotRequired[InputPathType | None],
    "column": typing.NotRequired[str | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
})
LabelDilateParamsDict = _LabelDilateParamsDictNoTag | LabelDilateParamsDictTagged


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


def label_dilate_params(
    label: InputPathType,
    surface: InputPathType,
    dilate_dist: float,
    label_out: str,
    area_metric: InputPathType | None = None,
    column: str | None = None,
    roi_metric: InputPathType | None = None,
) -> LabelDilateParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label: the input label.
        surface: the surface to dilate on.
        dilate_dist: distance in mm to dilate the labels.
        label_out: the output label file.
        area_metric: vertex areas to use instead of computing them from the\
            surface\
            \
            the corrected vertex areas, as a metric.
        column: select a single column to dilate\
            \
            the column number or name.
        roi_metric: specify an roi of vertices to overwrite, rather than\
            vertices with the unlabeled key\
            \
            metric file, positive values denote vertices to have their values\
            replaced.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/label-dilate",
        "label": label,
        "surface": surface,
        "dilate-dist": dilate_dist,
        "label-out": label_out,
    }
    if area_metric is not None:
        params["area-metric"] = area_metric
    if column is not None:
        params["column"] = column
    if roi_metric is not None:
        params["roi-metric"] = roi_metric
    return params


def label_dilate_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelDilateParamsDict` 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", None) is None:
        raise StyxValidationError("`label` must not be None")
    if not isinstance(params["label"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label` has the wrong type: Received `{type(params.get("label", None))}` expected `InputPathType`')
    if params.get("surface", None) is None:
        raise StyxValidationError("`surface` must not be None")
    if not isinstance(params["surface"], (pathlib.Path, str)):
        raise StyxValidationError(f'`surface` has the wrong type: Received `{type(params.get("surface", None))}` expected `InputPathType`')
    if params.get("dilate-dist", None) is None:
        raise StyxValidationError("`dilate-dist` must not be None")
    if not isinstance(params["dilate-dist"], (float, int)):
        raise StyxValidationError(f'`dilate-dist` has the wrong type: Received `{type(params.get("dilate-dist", None))}` expected `float`')
    if params.get("label-out", None) is None:
        raise StyxValidationError("`label-out` must not be None")
    if not isinstance(params["label-out"], str):
        raise StyxValidationError(f'`label-out` has the wrong type: Received `{type(params.get("label-out", None))}` expected `str`')
    if params.get("area-metric", None) is not None:
        if not isinstance(params["area-metric"], (pathlib.Path, str)):
            raise StyxValidationError(f'`area-metric` has the wrong type: Received `{type(params.get("area-metric", None))}` expected `InputPathType | None`')
    if params.get("column", None) is not None:
        if not isinstance(params["column"], str):
            raise StyxValidationError(f'`column` has the wrong type: Received `{type(params.get("column", None))}` expected `str | 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 label_dilate_cargs(
    params: LabelDilateParamsDict,
    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",
        "-label-dilate"
    ])
    cargs.append(execution.input_file(params.get("label", None)))
    cargs.append(execution.input_file(params.get("surface", None)))
    cargs.append(str(params.get("dilate-dist", None)))
    cargs.append(params.get("label-out", None))
    if params.get("area-metric", None) is not None:
        cargs.extend([
            "-corrected-areas",
            execution.input_file(params.get("area-metric", None))
        ])
    if params.get("column", None) is not None:
        cargs.extend([
            "-column",
            params.get("column", None)
        ])
    if params.get("roi-metric", None) is not None:
        cargs.extend([
            "-bad-vertex-roi",
            execution.input_file(params.get("roi-metric", None))
        ])
    return cargs


def label_dilate_outputs(
    params: LabelDilateParamsDict,
    execution: Execution,
) -> LabelDilateOutputs:
    """
    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 = LabelDilateOutputs(
        root=execution.output_file("."),
        label_out=execution.output_file(params.get("label-out", None)),
    )
    return ret


def label_dilate_execute(
    params: LabelDilateParamsDict,
    runner: Runner | None = None,
) -> LabelDilateOutputs:
    """
    DILATE A LABEL FILE.
    
    Fills in label information for all vertices designated as bad, up to the
    specified distance away from other labels. If -bad-vertex-roi is specified,
    all vertices, including those with the unlabeled key, are good, except for
    vertices with a positive value in the ROI. If it is not specified, only
    vertices with the unlabeled key are bad.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelDilateOutputs`).
    """
    label_dilate_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(LABEL_DILATE_METADATA)
    params = execution.params(params)
    cargs = label_dilate_cargs(params, execution)
    ret = label_dilate_outputs(params, execution)
    execution.run(cargs)
    return ret


def label_dilate(
    label: InputPathType,
    surface: InputPathType,
    dilate_dist: float,
    label_out: str,
    area_metric: InputPathType | None = None,
    column: str | None = None,
    roi_metric: InputPathType | None = None,
    runner: Runner | None = None,
) -> LabelDilateOutputs:
    """
    DILATE A LABEL FILE.
    
    Fills in label information for all vertices designated as bad, up to the
    specified distance away from other labels. If -bad-vertex-roi is specified,
    all vertices, including those with the unlabeled key, are good, except for
    vertices with a positive value in the ROI. If it is not specified, only
    vertices with the unlabeled key are bad.
    
    Args:
        label: the input label.
        surface: the surface to dilate on.
        dilate_dist: distance in mm to dilate the labels.
        label_out: the output label file.
        area_metric: vertex areas to use instead of computing them from the\
            surface\
            \
            the corrected vertex areas, as a metric.
        column: select a single column to dilate\
            \
            the column number or name.
        roi_metric: specify an roi of vertices to overwrite, rather than\
            vertices with the unlabeled key\
            \
            metric file, positive values denote vertices to have their values\
            replaced.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelDilateOutputs`).
    """
    params = label_dilate_params(
        label=label,
        surface=surface,
        dilate_dist=dilate_dist,
        label_out=label_out,
        area_metric=area_metric,
        column=column,
        roi_metric=roi_metric,
    )
    return label_dilate_execute(params, runner)


__all__ = [
    "LABEL_DILATE_METADATA",
    "LabelDilateOutputs",
    "LabelDilateParamsDict",
    "LabelDilateParamsDictTagged",
    "label_dilate",
    "label_dilate_execute",
    "label_dilate_params",
]
