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

import typing
import pathlib
from styxdefs import *

METRIC_TO_VOLUME_MAPPING_METADATA = Metadata(
    id="59e64c901ea699276bc6abbb818ee42c40042ca0.workbench",
    name="metric-to-volume-mapping",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_MetricToVolumeMappingRibbonConstrainedParamsDictNoTag = typing.TypedDict('_MetricToVolumeMappingRibbonConstrainedParamsDictNoTag', {
    "inner-surf": InputPathType,
    "outer-surf": InputPathType,
    "subdiv-num": typing.NotRequired[int | None],
    "thick-columns": bool,
    "greedy": bool,
})
MetricToVolumeMappingRibbonConstrainedParamsDictTagged = typing.TypedDict('MetricToVolumeMappingRibbonConstrainedParamsDictTagged', {
    "@type": typing.Literal["ribbon-constrained"],
    "inner-surf": InputPathType,
    "outer-surf": InputPathType,
    "subdiv-num": typing.NotRequired[int | None],
    "thick-columns": bool,
    "greedy": bool,
})
MetricToVolumeMappingRibbonConstrainedParamsDict = _MetricToVolumeMappingRibbonConstrainedParamsDictNoTag | MetricToVolumeMappingRibbonConstrainedParamsDictTagged


_MetricToVolumeMappingParamsDictNoTag = typing.TypedDict('_MetricToVolumeMappingParamsDictNoTag', {
    "metric": InputPathType,
    "surface": InputPathType,
    "volume-space": InputPathType,
    "volume-out": str,
    "ribbon-constrained": typing.NotRequired[MetricToVolumeMappingRibbonConstrainedParamsDict | None],
    "distance": typing.NotRequired[float | None],
})
MetricToVolumeMappingParamsDictTagged = typing.TypedDict('MetricToVolumeMappingParamsDictTagged', {
    "@type": typing.Literal["workbench/metric-to-volume-mapping"],
    "metric": InputPathType,
    "surface": InputPathType,
    "volume-space": InputPathType,
    "volume-out": str,
    "ribbon-constrained": typing.NotRequired[MetricToVolumeMappingRibbonConstrainedParamsDict | None],
    "distance": typing.NotRequired[float | None],
})
MetricToVolumeMappingParamsDict = _MetricToVolumeMappingParamsDictNoTag | MetricToVolumeMappingParamsDictTagged


def metric_to_volume_mapping_ribbon_constrained(
    inner_surf: InputPathType,
    outer_surf: InputPathType,
    subdiv_num: int | None = None,
    thick_columns: bool = False,
    greedy: bool = False,
) -> MetricToVolumeMappingRibbonConstrainedParamsDictTagged:
    """
    Build parameters.
    
    Args:
        inner_surf: the inner surface of the ribbon.
        outer_surf: the outer surface of the ribbon.
        subdiv_num: voxel divisions while estimating voxel weights\
            \
            number of subdivisions, default 3.
        thick_columns: use overlapping columns (legacy method).
        greedy: instead of antialiasing partial-volumed voxels, put full metric\
            values (legacy behavior).
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ribbon-constrained",
        "inner-surf": inner_surf,
        "outer-surf": outer_surf,
        "thick-columns": thick_columns,
        "greedy": greedy,
    }
    if subdiv_num is not None:
        params["subdiv-num"] = subdiv_num
    return params


def metric_to_volume_mapping_ribbon_constrained_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MetricToVolumeMappingRibbonConstrainedParamsDict` 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("inner-surf", None) is None:
        raise StyxValidationError("`inner-surf` must not be None")
    if not isinstance(params["inner-surf"], (pathlib.Path, str)):
        raise StyxValidationError(f'`inner-surf` has the wrong type: Received `{type(params.get("inner-surf", None))}` expected `InputPathType`')
    if params.get("outer-surf", None) is None:
        raise StyxValidationError("`outer-surf` must not be None")
    if not isinstance(params["outer-surf"], (pathlib.Path, str)):
        raise StyxValidationError(f'`outer-surf` has the wrong type: Received `{type(params.get("outer-surf", None))}` expected `InputPathType`')
    if params.get("subdiv-num", None) is not None:
        if not isinstance(params["subdiv-num"], int):
            raise StyxValidationError(f'`subdiv-num` has the wrong type: Received `{type(params.get("subdiv-num", None))}` expected `int | None`')
    if params.get("thick-columns", False) is None:
        raise StyxValidationError("`thick-columns` must not be None")
    if not isinstance(params["thick-columns"], bool):
        raise StyxValidationError(f'`thick-columns` has the wrong type: Received `{type(params.get("thick-columns", False))}` expected `bool`')
    if params.get("greedy", False) is None:
        raise StyxValidationError("`greedy` must not be None")
    if not isinstance(params["greedy"], bool):
        raise StyxValidationError(f'`greedy` has the wrong type: Received `{type(params.get("greedy", False))}` expected `bool`')


def metric_to_volume_mapping_ribbon_constrained_cargs(
    params: MetricToVolumeMappingRibbonConstrainedParamsDict,
    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([
        "-ribbon-constrained",
        execution.input_file(params.get("inner-surf", None)),
        execution.input_file(params.get("outer-surf", None))
    ])
    if params.get("subdiv-num", None) is not None:
        cargs.extend([
            "-voxel-subdiv",
            str(params.get("subdiv-num", None))
        ])
    if params.get("thick-columns", False):
        cargs.append("-thick-columns")
    if params.get("greedy", False):
        cargs.append("-greedy")
    return cargs


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


def metric_to_volume_mapping_params(
    metric: InputPathType,
    surface: InputPathType,
    volume_space: InputPathType,
    volume_out: str,
    ribbon_constrained: MetricToVolumeMappingRibbonConstrainedParamsDict | None = None,
    distance: float | None = None,
) -> MetricToVolumeMappingParamsDictTagged:
    """
    Build parameters.
    
    Args:
        metric: the input metric file.
        surface: the surface to use coordinates from.
        volume_space: a volume file in the desired output volume space.
        volume_out: the output volume file.
        ribbon_constrained: use ribbon constrained mapping algorithm.
        distance: use the value from the vertex closest to the voxel center\
            \
            how far from the surface to map values to voxels, in mm.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/metric-to-volume-mapping",
        "metric": metric,
        "surface": surface,
        "volume-space": volume_space,
        "volume-out": volume_out,
    }
    if ribbon_constrained is not None:
        params["ribbon-constrained"] = ribbon_constrained
    if distance is not None:
        params["distance"] = distance
    return params


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


def metric_to_volume_mapping_cargs(
    params: MetricToVolumeMappingParamsDict,
    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-to-volume-mapping"
    ])
    cargs.append(execution.input_file(params.get("metric", None)))
    cargs.append(execution.input_file(params.get("surface", None)))
    cargs.append(execution.input_file(params.get("volume-space", None)))
    cargs.append(params.get("volume-out", None))
    if params.get("ribbon-constrained", None) is not None:
        cargs.extend(metric_to_volume_mapping_ribbon_constrained_cargs(params.get("ribbon-constrained", None), execution))
    if params.get("distance", None) is not None:
        cargs.extend([
            "-nearest-vertex",
            str(params.get("distance", None))
        ])
    return cargs


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


def metric_to_volume_mapping_execute(
    params: MetricToVolumeMappingParamsDict,
    runner: Runner | None = None,
) -> MetricToVolumeMappingOutputs:
    """
    MAP METRIC FILE TO VOLUME.
    
    Maps values from a metric file into a volume file. You must specify exactly
    one mapping method option. The -nearest-vertex method uses the value from
    the vertex closest to the voxel center (useful for integer values). The
    -ribbon-constrained method uses the same method as in
    -volume-to-surface-mapping, then uses the weights in reverse. Mapping to
    lower resolutions than the mesh may require a larger -voxel-subdiv value in
    order to have all of the surface data participate.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricToVolumeMappingOutputs`).
    """
    metric_to_volume_mapping_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(METRIC_TO_VOLUME_MAPPING_METADATA)
    params = execution.params(params)
    cargs = metric_to_volume_mapping_cargs(params, execution)
    ret = metric_to_volume_mapping_outputs(params, execution)
    execution.run(cargs)
    return ret


def metric_to_volume_mapping(
    metric: InputPathType,
    surface: InputPathType,
    volume_space: InputPathType,
    volume_out: str,
    ribbon_constrained: MetricToVolumeMappingRibbonConstrainedParamsDict | None = None,
    distance: float | None = None,
    runner: Runner | None = None,
) -> MetricToVolumeMappingOutputs:
    """
    MAP METRIC FILE TO VOLUME.
    
    Maps values from a metric file into a volume file. You must specify exactly
    one mapping method option. The -nearest-vertex method uses the value from
    the vertex closest to the voxel center (useful for integer values). The
    -ribbon-constrained method uses the same method as in
    -volume-to-surface-mapping, then uses the weights in reverse. Mapping to
    lower resolutions than the mesh may require a larger -voxel-subdiv value in
    order to have all of the surface data participate.
    
    Args:
        metric: the input metric file.
        surface: the surface to use coordinates from.
        volume_space: a volume file in the desired output volume space.
        volume_out: the output volume file.
        ribbon_constrained: use ribbon constrained mapping algorithm.
        distance: use the value from the vertex closest to the voxel center\
            \
            how far from the surface to map values to voxels, in mm.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricToVolumeMappingOutputs`).
    """
    params = metric_to_volume_mapping_params(
        metric=metric,
        surface=surface,
        volume_space=volume_space,
        volume_out=volume_out,
        ribbon_constrained=ribbon_constrained,
        distance=distance,
    )
    return metric_to_volume_mapping_execute(params, runner)


__all__ = [
    "METRIC_TO_VOLUME_MAPPING_METADATA",
    "MetricToVolumeMappingOutputs",
    "MetricToVolumeMappingParamsDict",
    "MetricToVolumeMappingParamsDictTagged",
    "MetricToVolumeMappingRibbonConstrainedParamsDict",
    "MetricToVolumeMappingRibbonConstrainedParamsDictTagged",
    "metric_to_volume_mapping",
    "metric_to_volume_mapping_execute",
    "metric_to_volume_mapping_params",
    "metric_to_volume_mapping_ribbon_constrained",
]
