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

import typing
import pathlib
from styxdefs import *

LABEL_TO_VOLUME_MAPPING_METADATA = Metadata(
    id="a8de8237605262064e9a5abfe0dd83ad86664dd4.boutiques",
    name="label-to-volume-mapping",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


LabelToVolumeMappingRibbonConstrainedParameters = typing.TypedDict('LabelToVolumeMappingRibbonConstrainedParameters', {
    "@type": typing.NotRequired[typing.Literal["ribbon_constrained"]],
    "inner_surf": InputPathType,
    "outer_surf": InputPathType,
    "opt_voxel_subdiv_subdiv_num": typing.NotRequired[int | None],
    "opt_greedy": bool,
    "opt_thick_columns": bool,
})
LabelToVolumeMappingRibbonConstrainedParametersTagged = typing.TypedDict('LabelToVolumeMappingRibbonConstrainedParametersTagged', {
    "@type": typing.Literal["ribbon_constrained"],
    "inner_surf": InputPathType,
    "outer_surf": InputPathType,
    "opt_voxel_subdiv_subdiv_num": typing.NotRequired[int | None],
    "opt_greedy": bool,
    "opt_thick_columns": bool,
})


LabelToVolumeMappingParameters = typing.TypedDict('LabelToVolumeMappingParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/label-to-volume-mapping"]],
    "label": InputPathType,
    "surface": InputPathType,
    "volume_space": InputPathType,
    "volume_out": str,
    "opt_nearest_vertex_distance": typing.NotRequired[float | None],
    "ribbon_constrained": typing.NotRequired[LabelToVolumeMappingRibbonConstrainedParameters | None],
})
LabelToVolumeMappingParametersTagged = typing.TypedDict('LabelToVolumeMappingParametersTagged', {
    "@type": typing.Literal["workbench/label-to-volume-mapping"],
    "label": InputPathType,
    "surface": InputPathType,
    "volume_space": InputPathType,
    "volume_out": str,
    "opt_nearest_vertex_distance": typing.NotRequired[float | None],
    "ribbon_constrained": typing.NotRequired[LabelToVolumeMappingRibbonConstrainedParameters | None],
})


def label_to_volume_mapping_ribbon_constrained_params(
    inner_surf: InputPathType,
    outer_surf: InputPathType,
    opt_voxel_subdiv_subdiv_num: int | None = None,
    opt_greedy: bool = False,
    opt_thick_columns: bool = False,
) -> LabelToVolumeMappingRibbonConstrainedParametersTagged:
    """
    Build parameters.
    
    Args:
        inner_surf: the inner surface of the ribbon.
        outer_surf: the outer surface of the ribbon.
        opt_voxel_subdiv_subdiv_num: voxel divisions while estimating voxel\
            weights: number of subdivisions, default 3.
        opt_greedy: also put labels in voxels with less than 50% partial volume\
            (legacy behavior).
        opt_thick_columns: use overlapping columns (legacy method).
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ribbon_constrained",
        "inner_surf": inner_surf,
        "outer_surf": outer_surf,
        "opt_greedy": opt_greedy,
        "opt_thick_columns": opt_thick_columns,
    }
    if opt_voxel_subdiv_subdiv_num is not None:
        params["opt_voxel_subdiv_subdiv_num"] = opt_voxel_subdiv_subdiv_num
    return params


def label_to_volume_mapping_ribbon_constrained_cargs(
    params: LabelToVolumeMappingRibbonConstrainedParameters,
    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.append("-ribbon-constrained")
    cargs.append(execution.input_file(params.get("inner_surf", None)))
    cargs.append(execution.input_file(params.get("outer_surf", None)))
    if params.get("opt_voxel_subdiv_subdiv_num", None) is not None:
        cargs.extend([
            "-voxel-subdiv",
            str(params.get("opt_voxel_subdiv_subdiv_num", None))
        ])
    if params.get("opt_greedy", False):
        cargs.append("-greedy")
    if params.get("opt_thick_columns", False):
        cargs.append("-thick-columns")
    return cargs


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


def label_to_volume_mapping_params(
    label: InputPathType,
    surface: InputPathType,
    volume_space: InputPathType,
    volume_out: str,
    opt_nearest_vertex_distance: float | None = None,
    ribbon_constrained: LabelToVolumeMappingRibbonConstrainedParameters | None = None,
) -> LabelToVolumeMappingParametersTagged:
    """
    Build parameters.
    
    Args:
        label: the input label 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.
        opt_nearest_vertex_distance: use the label from the vertex closest to\
            the voxel center: how far from the surface to map labels to voxels, in\
            mm.
        ribbon_constrained: use ribbon constrained mapping algorithm.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/label-to-volume-mapping",
        "label": label,
        "surface": surface,
        "volume_space": volume_space,
        "volume_out": volume_out,
    }
    if opt_nearest_vertex_distance is not None:
        params["opt_nearest_vertex_distance"] = opt_nearest_vertex_distance
    if ribbon_constrained is not None:
        params["ribbon_constrained"] = ribbon_constrained
    return params


def label_to_volume_mapping_cargs(
    params: LabelToVolumeMappingParameters,
    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.append("wb_command")
    cargs.append("-label-to-volume-mapping")
    cargs.append(execution.input_file(params.get("label", 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("opt_nearest_vertex_distance", None) is not None:
        cargs.extend([
            "-nearest-vertex",
            str(params.get("opt_nearest_vertex_distance", None))
        ])
    if params.get("ribbon_constrained", None) is not None:
        cargs.extend(label_to_volume_mapping_ribbon_constrained_cargs(params.get("ribbon_constrained", None), execution))
    return cargs


def label_to_volume_mapping_outputs(
    params: LabelToVolumeMappingParameters,
    execution: Execution,
) -> LabelToVolumeMappingOutputs:
    """
    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 = LabelToVolumeMappingOutputs(
        root=execution.output_file("."),
        volume_out=execution.output_file(params.get("volume_out", None)),
    )
    return ret


def label_to_volume_mapping_execute(
    params: LabelToVolumeMappingParameters,
    runner: Runner | None = None,
) -> LabelToVolumeMappingOutputs:
    """
    label-to-volume-mapping
    
    Map label file to volume.
    
    Maps labels from a gifti label file into a volume file. You must specify
    exactly one mapping method option. The -nearest-vertex method uses the label
    from the vertex closest to the voxel center. The -ribbon-constrained method
    uses the same method as in -volume-to-surface-mapping, then uses the weights
    in reverse, with popularity logic to decide on a label to use.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelToVolumeMappingOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(LABEL_TO_VOLUME_MAPPING_METADATA)
    params = execution.params(params)
    cargs = label_to_volume_mapping_cargs(params, execution)
    ret = label_to_volume_mapping_outputs(params, execution)
    execution.run(cargs)
    return ret


def label_to_volume_mapping(
    label: InputPathType,
    surface: InputPathType,
    volume_space: InputPathType,
    volume_out: str,
    opt_nearest_vertex_distance: float | None = None,
    ribbon_constrained: LabelToVolumeMappingRibbonConstrainedParameters | None = None,
    runner: Runner | None = None,
) -> LabelToVolumeMappingOutputs:
    """
    label-to-volume-mapping
    
    Map label file to volume.
    
    Maps labels from a gifti label file into a volume file. You must specify
    exactly one mapping method option. The -nearest-vertex method uses the label
    from the vertex closest to the voxel center. The -ribbon-constrained method
    uses the same method as in -volume-to-surface-mapping, then uses the weights
    in reverse, with popularity logic to decide on a label to use.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        label: the input label 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.
        opt_nearest_vertex_distance: use the label from the vertex closest to\
            the voxel center: how far from the surface to map labels to voxels, in\
            mm.
        ribbon_constrained: use ribbon constrained mapping algorithm.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelToVolumeMappingOutputs`).
    """
    params = label_to_volume_mapping_params(
        label=label,
        surface=surface,
        volume_space=volume_space,
        volume_out=volume_out,
        opt_nearest_vertex_distance=opt_nearest_vertex_distance,
        ribbon_constrained=ribbon_constrained,
    )
    return label_to_volume_mapping_execute(params, runner)


__all__ = [
    "LABEL_TO_VOLUME_MAPPING_METADATA",
    "LabelToVolumeMappingOutputs",
    "label_to_volume_mapping",
    "label_to_volume_mapping_execute",
    "label_to_volume_mapping_params",
    "label_to_volume_mapping_ribbon_constrained_params",
]
