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

import typing
import pathlib
from styxdefs import *

VOLUME_ERODE_METADATA = Metadata(
    id="de54c7f54c6368999f264ce5975a56da07dfce18.workbench",
    name="volume-erode",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeErodeParamsDictNoTag = typing.TypedDict('_VolumeErodeParamsDictNoTag', {
    "volume": InputPathType,
    "distance": float,
    "volume-out": str,
    "subvol": typing.NotRequired[str | None],
    "roi-volume": typing.NotRequired[InputPathType | None],
})
VolumeErodeParamsDictTagged = typing.TypedDict('VolumeErodeParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-erode"],
    "volume": InputPathType,
    "distance": float,
    "volume-out": str,
    "subvol": typing.NotRequired[str | None],
    "roi-volume": typing.NotRequired[InputPathType | None],
})
VolumeErodeParamsDict = _VolumeErodeParamsDictNoTag | VolumeErodeParamsDictTagged


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


def volume_erode_params(
    volume: InputPathType,
    distance: float,
    volume_out: str,
    subvol: str | None = None,
    roi_volume: InputPathType | None = None,
) -> VolumeErodeParamsDictTagged:
    """
    Build parameters.
    
    Args:
        volume: the volume to erode.
        distance: distance in mm to erode.
        volume_out: the output volume.
        subvol: select a single subvolume to dilate\
            \
            the subvolume number or name.
        roi_volume: assume voxels outside this roi are nonzero\
            \
            volume file, positive values denote voxels that have data.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-erode",
        "volume": volume,
        "distance": distance,
        "volume-out": volume_out,
    }
    if subvol is not None:
        params["subvol"] = subvol
    if roi_volume is not None:
        params["roi-volume"] = roi_volume
    return params


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


def volume_erode_cargs(
    params: VolumeErodeParamsDict,
    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-erode"
    ])
    cargs.append(execution.input_file(params.get("volume", None)))
    cargs.append(str(params.get("distance", None)))
    cargs.append(params.get("volume-out", None))
    if params.get("subvol", None) is not None:
        cargs.extend([
            "-subvolume",
            params.get("subvol", None)
        ])
    if params.get("roi-volume", None) is not None:
        cargs.extend([
            "-roi",
            execution.input_file(params.get("roi-volume", None))
        ])
    return cargs


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


def volume_erode_execute(
    params: VolumeErodeParamsDict,
    runner: Runner | None = None,
) -> VolumeErodeOutputs:
    """
    ERODE A VOLUME FILE.
    
    Around each voxel with a value of zero, set surrounding voxels to zero. The
    surrounding voxels are all face neighbors and all voxels within the
    specified distance (center to center).
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeErodeOutputs`).
    """
    volume_erode_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_ERODE_METADATA)
    params = execution.params(params)
    cargs = volume_erode_cargs(params, execution)
    ret = volume_erode_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_erode(
    volume: InputPathType,
    distance: float,
    volume_out: str,
    subvol: str | None = None,
    roi_volume: InputPathType | None = None,
    runner: Runner | None = None,
) -> VolumeErodeOutputs:
    """
    ERODE A VOLUME FILE.
    
    Around each voxel with a value of zero, set surrounding voxels to zero. The
    surrounding voxels are all face neighbors and all voxels within the
    specified distance (center to center).
    
    Args:
        volume: the volume to erode.
        distance: distance in mm to erode.
        volume_out: the output volume.
        subvol: select a single subvolume to dilate\
            \
            the subvolume number or name.
        roi_volume: assume voxels outside this roi are nonzero\
            \
            volume file, positive values denote voxels that have data.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeErodeOutputs`).
    """
    params = volume_erode_params(
        volume=volume,
        distance=distance,
        volume_out=volume_out,
        subvol=subvol,
        roi_volume=roi_volume,
    )
    return volume_erode_execute(params, runner)


__all__ = [
    "VOLUME_ERODE_METADATA",
    "VolumeErodeOutputs",
    "VolumeErodeParamsDict",
    "VolumeErodeParamsDictTagged",
    "volume_erode",
    "volume_erode_execute",
    "volume_erode_params",
]
