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

import typing
import pathlib
from styxdefs import *

LABEL_RESAMPLE_METADATA = Metadata(
    id="27cac022f876a408a3e9c32a66575355a7783848.boutiques",
    name="label-resample",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


LabelResampleAreaSurfsParameters = typing.TypedDict('LabelResampleAreaSurfsParameters', {
    "@type": typing.NotRequired[typing.Literal["area_surfs"]],
    "current_area": InputPathType,
    "new_area": InputPathType,
})
LabelResampleAreaSurfsParametersTagged = typing.TypedDict('LabelResampleAreaSurfsParametersTagged', {
    "@type": typing.Literal["area_surfs"],
    "current_area": InputPathType,
    "new_area": InputPathType,
})


LabelResampleAreaMetricsParameters = typing.TypedDict('LabelResampleAreaMetricsParameters', {
    "@type": typing.NotRequired[typing.Literal["area_metrics"]],
    "current_area": InputPathType,
    "new_area": InputPathType,
})
LabelResampleAreaMetricsParametersTagged = typing.TypedDict('LabelResampleAreaMetricsParametersTagged', {
    "@type": typing.Literal["area_metrics"],
    "current_area": InputPathType,
    "new_area": InputPathType,
})


LabelResampleParameters = typing.TypedDict('LabelResampleParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/label-resample"]],
    "label_in": InputPathType,
    "current_sphere": InputPathType,
    "new_sphere": InputPathType,
    "method": str,
    "label_out": str,
    "area_surfs": typing.NotRequired[LabelResampleAreaSurfsParameters | None],
    "area_metrics": typing.NotRequired[LabelResampleAreaMetricsParameters | None],
    "opt_current_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_valid_roi_out_roi_out": typing.NotRequired[str | None],
    "opt_largest": bool,
    "opt_bypass_sphere_check": bool,
})
LabelResampleParametersTagged = typing.TypedDict('LabelResampleParametersTagged', {
    "@type": typing.Literal["workbench/label-resample"],
    "label_in": InputPathType,
    "current_sphere": InputPathType,
    "new_sphere": InputPathType,
    "method": str,
    "label_out": str,
    "area_surfs": typing.NotRequired[LabelResampleAreaSurfsParameters | None],
    "area_metrics": typing.NotRequired[LabelResampleAreaMetricsParameters | None],
    "opt_current_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_valid_roi_out_roi_out": typing.NotRequired[str | None],
    "opt_largest": bool,
    "opt_bypass_sphere_check": bool,
})


def label_resample_area_surfs_params(
    current_area: InputPathType,
    new_area: InputPathType,
) -> LabelResampleAreaSurfsParametersTagged:
    """
    Build parameters.
    
    Args:
        current_area: a relevant anatomical surface with <current-sphere> mesh.
        new_area: a relevant anatomical surface with <new-sphere> mesh.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "area_surfs",
        "current_area": current_area,
        "new_area": new_area,
    }
    return params


def label_resample_area_surfs_cargs(
    params: LabelResampleAreaSurfsParameters,
    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("-area-surfs")
    cargs.append(execution.input_file(params.get("current_area", None)))
    cargs.append(execution.input_file(params.get("new_area", None)))
    return cargs


def label_resample_area_metrics_params(
    current_area: InputPathType,
    new_area: InputPathType,
) -> LabelResampleAreaMetricsParametersTagged:
    """
    Build parameters.
    
    Args:
        current_area: a metric file with vertex areas for <current-sphere> mesh.
        new_area: a metric file with vertex areas for <new-sphere> mesh.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "area_metrics",
        "current_area": current_area,
        "new_area": new_area,
    }
    return params


def label_resample_area_metrics_cargs(
    params: LabelResampleAreaMetricsParameters,
    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("-area-metrics")
    cargs.append(execution.input_file(params.get("current_area", None)))
    cargs.append(execution.input_file(params.get("new_area", None)))
    return cargs


class LabelResampleOutputs(typing.NamedTuple):
    """
    Output object returned when calling `LabelResampleParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    label_out: OutputPathType
    """the output label file"""
    opt_valid_roi_out_roi_out: OutputPathType | None
    """output the ROI of vertices that got data from valid source vertices: the
    output roi as a metric"""


def label_resample_params(
    label_in: InputPathType,
    current_sphere: InputPathType,
    new_sphere: InputPathType,
    method: str,
    label_out: str,
    area_surfs: LabelResampleAreaSurfsParameters | None = None,
    area_metrics: LabelResampleAreaMetricsParameters | None = None,
    opt_current_roi_roi_metric: InputPathType | None = None,
    opt_valid_roi_out_roi_out: str | None = None,
    opt_largest: bool = False,
    opt_bypass_sphere_check: bool = False,
) -> LabelResampleParametersTagged:
    """
    Build parameters.
    
    Args:
        label_in: the label file to resample.
        current_sphere: a sphere surface with the mesh that the label file is\
            currently on.
        new_sphere: a sphere surface that is in register with <current-sphere>\
            and has the desired output mesh.
        method: the method name.
        label_out: the output label file.
        area_surfs: specify surfaces to do vertex area correction based on.
        area_metrics: specify vertex area metrics to do area correction based\
            on.
        opt_current_roi_roi_metric: use an input roi on the current mesh to\
            exclude non-data vertices: the roi, as a metric file.
        opt_valid_roi_out_roi_out: output the ROI of vertices that got data\
            from valid source vertices: the output roi as a metric.
        opt_largest: use only the label of the vertex with the largest weight.
        opt_bypass_sphere_check: ADVANCED: allow the current and new 'spheres'\
            to have arbitrary shape as long as they follow the same contour.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/label-resample",
        "label_in": label_in,
        "current_sphere": current_sphere,
        "new_sphere": new_sphere,
        "method": method,
        "label_out": label_out,
        "opt_largest": opt_largest,
        "opt_bypass_sphere_check": opt_bypass_sphere_check,
    }
    if area_surfs is not None:
        params["area_surfs"] = area_surfs
    if area_metrics is not None:
        params["area_metrics"] = area_metrics
    if opt_current_roi_roi_metric is not None:
        params["opt_current_roi_roi_metric"] = opt_current_roi_roi_metric
    if opt_valid_roi_out_roi_out is not None:
        params["opt_valid_roi_out_roi_out"] = opt_valid_roi_out_roi_out
    return params


def label_resample_cargs(
    params: LabelResampleParameters,
    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-resample")
    cargs.append(execution.input_file(params.get("label_in", None)))
    cargs.append(execution.input_file(params.get("current_sphere", None)))
    cargs.append(execution.input_file(params.get("new_sphere", None)))
    cargs.append(params.get("method", None))
    cargs.append(params.get("label_out", None))
    if params.get("area_surfs", None) is not None:
        cargs.extend(label_resample_area_surfs_cargs(params.get("area_surfs", None), execution))
    if params.get("area_metrics", None) is not None:
        cargs.extend(label_resample_area_metrics_cargs(params.get("area_metrics", None), execution))
    if params.get("opt_current_roi_roi_metric", None) is not None:
        cargs.extend([
            "-current-roi",
            execution.input_file(params.get("opt_current_roi_roi_metric", None))
        ])
    if params.get("opt_valid_roi_out_roi_out", None) is not None:
        cargs.extend([
            "-valid-roi-out",
            params.get("opt_valid_roi_out_roi_out", None)
        ])
    if params.get("opt_largest", False):
        cargs.append("-largest")
    if params.get("opt_bypass_sphere_check", False):
        cargs.append("-bypass-sphere-check")
    return cargs


def label_resample_outputs(
    params: LabelResampleParameters,
    execution: Execution,
) -> LabelResampleOutputs:
    """
    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 = LabelResampleOutputs(
        root=execution.output_file("."),
        label_out=execution.output_file(params.get("label_out", None)),
        opt_valid_roi_out_roi_out=execution.output_file(params.get("opt_valid_roi_out_roi_out", None)) if (params.get("opt_valid_roi_out_roi_out") is not None) else None,
    )
    return ret


def label_resample_execute(
    params: LabelResampleParameters,
    runner: Runner | None = None,
) -> LabelResampleOutputs:
    """
    label-resample
    
    Resample a label file to a different mesh.
    
    Resamples a label file, given two spherical surfaces that are in register.
    If ADAP_BARY_AREA is used, exactly one of -area-surfs or -area-metrics must
    be specified.
    
    The ADAP_BARY_AREA method is recommended for label data, because it should
    be better at resolving vertices that are near multiple labels, or in case of
    downsampling. Midthickness surfaces are recommended for the vertex areas for
    most data.
    
    The -largest option results in nearest vertex behavior when used with
    BARYCENTRIC, as it uses the value of the source vertex that has the largest
    weight.
    
    When -largest is not specified, the vertex weights are summed according to
    which label they correspond to, and the label with the largest sum is used.
    
    The <method> argument must be one of the following:
    
    ADAP_BARY_AREA
    BARYCENTRIC
    .
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelResampleOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(LABEL_RESAMPLE_METADATA)
    params = execution.params(params)
    cargs = label_resample_cargs(params, execution)
    ret = label_resample_outputs(params, execution)
    execution.run(cargs)
    return ret


def label_resample(
    label_in: InputPathType,
    current_sphere: InputPathType,
    new_sphere: InputPathType,
    method: str,
    label_out: str,
    area_surfs: LabelResampleAreaSurfsParameters | None = None,
    area_metrics: LabelResampleAreaMetricsParameters | None = None,
    opt_current_roi_roi_metric: InputPathType | None = None,
    opt_valid_roi_out_roi_out: str | None = None,
    opt_largest: bool = False,
    opt_bypass_sphere_check: bool = False,
    runner: Runner | None = None,
) -> LabelResampleOutputs:
    """
    label-resample
    
    Resample a label file to a different mesh.
    
    Resamples a label file, given two spherical surfaces that are in register.
    If ADAP_BARY_AREA is used, exactly one of -area-surfs or -area-metrics must
    be specified.
    
    The ADAP_BARY_AREA method is recommended for label data, because it should
    be better at resolving vertices that are near multiple labels, or in case of
    downsampling. Midthickness surfaces are recommended for the vertex areas for
    most data.
    
    The -largest option results in nearest vertex behavior when used with
    BARYCENTRIC, as it uses the value of the source vertex that has the largest
    weight.
    
    When -largest is not specified, the vertex weights are summed according to
    which label they correspond to, and the label with the largest sum is used.
    
    The <method> argument must be one of the following:
    
    ADAP_BARY_AREA
    BARYCENTRIC
    .
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        label_in: the label file to resample.
        current_sphere: a sphere surface with the mesh that the label file is\
            currently on.
        new_sphere: a sphere surface that is in register with <current-sphere>\
            and has the desired output mesh.
        method: the method name.
        label_out: the output label file.
        area_surfs: specify surfaces to do vertex area correction based on.
        area_metrics: specify vertex area metrics to do area correction based\
            on.
        opt_current_roi_roi_metric: use an input roi on the current mesh to\
            exclude non-data vertices: the roi, as a metric file.
        opt_valid_roi_out_roi_out: output the ROI of vertices that got data\
            from valid source vertices: the output roi as a metric.
        opt_largest: use only the label of the vertex with the largest weight.
        opt_bypass_sphere_check: ADVANCED: allow the current and new 'spheres'\
            to have arbitrary shape as long as they follow the same contour.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelResampleOutputs`).
    """
    params = label_resample_params(
        label_in=label_in,
        current_sphere=current_sphere,
        new_sphere=new_sphere,
        method=method,
        label_out=label_out,
        area_surfs=area_surfs,
        area_metrics=area_metrics,
        opt_current_roi_roi_metric=opt_current_roi_roi_metric,
        opt_valid_roi_out_roi_out=opt_valid_roi_out_roi_out,
        opt_largest=opt_largest,
        opt_bypass_sphere_check=opt_bypass_sphere_check,
    )
    return label_resample_execute(params, runner)


__all__ = [
    "LABEL_RESAMPLE_METADATA",
    "LabelResampleOutputs",
    "label_resample",
    "label_resample_area_metrics_params",
    "label_resample_area_surfs_params",
    "label_resample_execute",
    "label_resample_params",
]
