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

import typing
import pathlib
from styxdefs import *

METRIC_REMOVE_ISLANDS_METADATA = Metadata(
    id="e1ca1127c9380dc9d8ce9364a1b74e3cb9676753.workbench",
    name="metric-remove-islands",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_MetricRemoveIslandsParamsDictNoTag = typing.TypedDict('_MetricRemoveIslandsParamsDictNoTag', {
    "surface": InputPathType,
    "metric-in": InputPathType,
    "metric-out": str,
    "area-metric": typing.NotRequired[InputPathType | None],
})
MetricRemoveIslandsParamsDictTagged = typing.TypedDict('MetricRemoveIslandsParamsDictTagged', {
    "@type": typing.Literal["workbench/metric-remove-islands"],
    "surface": InputPathType,
    "metric-in": InputPathType,
    "metric-out": str,
    "area-metric": typing.NotRequired[InputPathType | None],
})
MetricRemoveIslandsParamsDict = _MetricRemoveIslandsParamsDictNoTag | MetricRemoveIslandsParamsDictTagged


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


def metric_remove_islands_params(
    surface: InputPathType,
    metric_in: InputPathType,
    metric_out: str,
    area_metric: InputPathType | None = None,
) -> MetricRemoveIslandsParamsDictTagged:
    """
    Build parameters.
    
    Args:
        surface: the surface to use for neighbor information.
        metric_in: the input ROI metric.
        metric_out: the output ROI metric.
        area_metric: vertex areas to use instead of computing them from the\
            surface\
            \
            the corrected vertex areas, as a metric.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/metric-remove-islands",
        "surface": surface,
        "metric-in": metric_in,
        "metric-out": metric_out,
    }
    if area_metric is not None:
        params["area-metric"] = area_metric
    return params


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


def metric_remove_islands_cargs(
    params: MetricRemoveIslandsParamsDict,
    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-remove-islands"
    ])
    cargs.append(execution.input_file(params.get("surface", None)))
    cargs.append(execution.input_file(params.get("metric-in", 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))
        ])
    return cargs


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


def metric_remove_islands_execute(
    params: MetricRemoveIslandsParamsDict,
    runner: Runner | None = None,
) -> MetricRemoveIslandsOutputs:
    """
    REMOVE ISLANDS FROM AN ROI METRIC.
    
    Finds all connected areas in the ROI, and zeros out all but the largest one,
    in terms of surface area.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricRemoveIslandsOutputs`).
    """
    metric_remove_islands_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(METRIC_REMOVE_ISLANDS_METADATA)
    params = execution.params(params)
    cargs = metric_remove_islands_cargs(params, execution)
    ret = metric_remove_islands_outputs(params, execution)
    execution.run(cargs)
    return ret


def metric_remove_islands(
    surface: InputPathType,
    metric_in: InputPathType,
    metric_out: str,
    area_metric: InputPathType | None = None,
    runner: Runner | None = None,
) -> MetricRemoveIslandsOutputs:
    """
    REMOVE ISLANDS FROM AN ROI METRIC.
    
    Finds all connected areas in the ROI, and zeros out all but the largest one,
    in terms of surface area.
    
    Args:
        surface: the surface to use for neighbor information.
        metric_in: the input ROI metric.
        metric_out: the output ROI metric.
        area_metric: vertex areas to use instead of computing them from the\
            surface\
            \
            the corrected vertex areas, as a metric.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricRemoveIslandsOutputs`).
    """
    params = metric_remove_islands_params(
        surface=surface,
        metric_in=metric_in,
        metric_out=metric_out,
        area_metric=area_metric,
    )
    return metric_remove_islands_execute(params, runner)


__all__ = [
    "METRIC_REMOVE_ISLANDS_METADATA",
    "MetricRemoveIslandsOutputs",
    "MetricRemoveIslandsParamsDict",
    "MetricRemoveIslandsParamsDictTagged",
    "metric_remove_islands",
    "metric_remove_islands_execute",
    "metric_remove_islands_params",
]
