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

import typing
import pathlib
from styxdefs import *

METRIC_ERODE_METADATA = Metadata(
    id="778174a7101c157fd44e9efd68314bd69963400f.workbench",
    name="metric-erode",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_MetricErodeParamsDictNoTag = typing.TypedDict('_MetricErodeParamsDictNoTag', {
    "metric": InputPathType,
    "surface": InputPathType,
    "distance": float,
    "metric-out": str,
    "area-metric": typing.NotRequired[InputPathType | None],
    "column": typing.NotRequired[str | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
})
MetricErodeParamsDictTagged = typing.TypedDict('MetricErodeParamsDictTagged', {
    "@type": typing.Literal["workbench/metric-erode"],
    "metric": InputPathType,
    "surface": InputPathType,
    "distance": float,
    "metric-out": str,
    "area-metric": typing.NotRequired[InputPathType | None],
    "column": typing.NotRequired[str | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
})
MetricErodeParamsDict = _MetricErodeParamsDictNoTag | MetricErodeParamsDictTagged


class MetricErodeOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MetricErodeParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    metric_out: OutputPathType
    """the output metric"""


def metric_erode_params(
    metric: InputPathType,
    surface: InputPathType,
    distance: float,
    metric_out: str,
    area_metric: InputPathType | None = None,
    column: str | None = None,
    roi_metric: InputPathType | None = None,
) -> MetricErodeParamsDictTagged:
    """
    Build parameters.
    
    Args:
        metric: the metric file to erode.
        surface: the surface to compute on.
        distance: distance in mm to erode.
        metric_out: the output metric.
        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 erode\
            \
            the column number or name.
        roi_metric: assume values outside this roi are nonzero\
            \
            metric file, positive values denote vertices that have data.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/metric-erode",
        "metric": metric,
        "surface": surface,
        "distance": distance,
        "metric-out": metric_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 metric_erode_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MetricErodeParamsDict` 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("metric", None) is None:
        raise StyxValidationError("`metric` must not be None")
    if not isinstance(params["metric"], (pathlib.Path, str)):
        raise StyxValidationError(f'`metric` has the wrong type: Received `{type(params.get("metric", 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("distance", None) is None:
        raise StyxValidationError("`distance` must not be None")
    if not isinstance(params["distance"], (float, int)):
        raise StyxValidationError(f'`distance` has the wrong type: Received `{type(params.get("distance", None))}` expected `float`')
    if params.get("metric-out", None) is None:
        raise StyxValidationError("`metric-out` must not be None")
    if not isinstance(params["metric-out"], str):
        raise StyxValidationError(f'`metric-out` has the wrong type: Received `{type(params.get("metric-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 metric_erode_cargs(
    params: MetricErodeParamsDict,
    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",
        "-metric-erode"
    ])
    cargs.append(execution.input_file(params.get("metric", None)))
    cargs.append(execution.input_file(params.get("surface", None)))
    cargs.append(str(params.get("distance", None)))
    cargs.append(params.get("metric-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([
            "-roi",
            execution.input_file(params.get("roi-metric", None))
        ])
    return cargs


def metric_erode_outputs(
    params: MetricErodeParamsDict,
    execution: Execution,
) -> MetricErodeOutputs:
    """
    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 = MetricErodeOutputs(
        root=execution.output_file("."),
        metric_out=execution.output_file(params.get("metric-out", None)),
    )
    return ret


def metric_erode_execute(
    params: MetricErodeParamsDict,
    runner: Runner | None = None,
) -> MetricErodeOutputs:
    """
    ERODE A METRIC FILE.
    
    Around each vertex with a value of zero, set surrounding vertices to zero.
    The surrounding vertices are all immediate neighbors and all vertices within
    the specified distance.
    
    Note that the -corrected-areas option uses an approximate correction for
    distance along the surface.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricErodeOutputs`).
    """
    metric_erode_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(METRIC_ERODE_METADATA)
    params = execution.params(params)
    cargs = metric_erode_cargs(params, execution)
    ret = metric_erode_outputs(params, execution)
    execution.run(cargs)
    return ret


def metric_erode(
    metric: InputPathType,
    surface: InputPathType,
    distance: float,
    metric_out: str,
    area_metric: InputPathType | None = None,
    column: str | None = None,
    roi_metric: InputPathType | None = None,
    runner: Runner | None = None,
) -> MetricErodeOutputs:
    """
    ERODE A METRIC FILE.
    
    Around each vertex with a value of zero, set surrounding vertices to zero.
    The surrounding vertices are all immediate neighbors and all vertices within
    the specified distance.
    
    Note that the -corrected-areas option uses an approximate correction for
    distance along the surface.
    
    Args:
        metric: the metric file to erode.
        surface: the surface to compute on.
        distance: distance in mm to erode.
        metric_out: the output metric.
        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 erode\
            \
            the column number or name.
        roi_metric: assume values outside this roi are nonzero\
            \
            metric file, positive values denote vertices that have data.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricErodeOutputs`).
    """
    params = metric_erode_params(
        metric=metric,
        surface=surface,
        distance=distance,
        metric_out=metric_out,
        area_metric=area_metric,
        column=column,
        roi_metric=roi_metric,
    )
    return metric_erode_execute(params, runner)


__all__ = [
    "METRIC_ERODE_METADATA",
    "MetricErodeOutputs",
    "MetricErodeParamsDict",
    "MetricErodeParamsDictTagged",
    "metric_erode",
    "metric_erode_execute",
    "metric_erode_params",
]
