# 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="c5578cc16cfdc8f6e4a75e75e6c10f719016d2e0.workbench",
    name="label-resample",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_LabelResampleAreaSurfsParamsDictNoTag = typing.TypedDict('_LabelResampleAreaSurfsParamsDictNoTag', {
    "current-area": InputPathType,
    "new-area": InputPathType,
})
LabelResampleAreaSurfsParamsDictTagged = typing.TypedDict('LabelResampleAreaSurfsParamsDictTagged', {
    "@type": typing.Literal["area-surfs"],
    "current-area": InputPathType,
    "new-area": InputPathType,
})
LabelResampleAreaSurfsParamsDict = _LabelResampleAreaSurfsParamsDictNoTag | LabelResampleAreaSurfsParamsDictTagged


_LabelResampleAreaMetricsParamsDictNoTag = typing.TypedDict('_LabelResampleAreaMetricsParamsDictNoTag', {
    "current-area": InputPathType,
    "new-area": InputPathType,
})
LabelResampleAreaMetricsParamsDictTagged = typing.TypedDict('LabelResampleAreaMetricsParamsDictTagged', {
    "@type": typing.Literal["area-metrics"],
    "current-area": InputPathType,
    "new-area": InputPathType,
})
LabelResampleAreaMetricsParamsDict = _LabelResampleAreaMetricsParamsDictNoTag | LabelResampleAreaMetricsParamsDictTagged


_LabelResampleValidRoiOutParamsDictNoTag = typing.TypedDict('_LabelResampleValidRoiOutParamsDictNoTag', {
    "roi-out": str,
})
LabelResampleValidRoiOutParamsDictTagged = typing.TypedDict('LabelResampleValidRoiOutParamsDictTagged', {
    "@type": typing.Literal["valid-roi-out"],
    "roi-out": str,
})
LabelResampleValidRoiOutParamsDict = _LabelResampleValidRoiOutParamsDictNoTag | LabelResampleValidRoiOutParamsDictTagged


_LabelResampleParamsDictNoTag = typing.TypedDict('_LabelResampleParamsDictNoTag', {
    "label-in": InputPathType,
    "current-sphere": InputPathType,
    "new-sphere": InputPathType,
    "method": str,
    "label-out": str,
    "area-surfs": typing.NotRequired[LabelResampleAreaSurfsParamsDict | None],
    "area-metrics": typing.NotRequired[LabelResampleAreaMetricsParamsDict | None],
    "valid-roi-out": typing.NotRequired[LabelResampleValidRoiOutParamsDict | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "bypass-sphere-check": bool,
    "largest": bool,
})
LabelResampleParamsDictTagged = typing.TypedDict('LabelResampleParamsDictTagged', {
    "@type": typing.Literal["workbench/label-resample"],
    "label-in": InputPathType,
    "current-sphere": InputPathType,
    "new-sphere": InputPathType,
    "method": str,
    "label-out": str,
    "area-surfs": typing.NotRequired[LabelResampleAreaSurfsParamsDict | None],
    "area-metrics": typing.NotRequired[LabelResampleAreaMetricsParamsDict | None],
    "valid-roi-out": typing.NotRequired[LabelResampleValidRoiOutParamsDict | None],
    "roi-metric": typing.NotRequired[InputPathType | None],
    "bypass-sphere-check": bool,
    "largest": bool,
})
LabelResampleParamsDict = _LabelResampleParamsDictNoTag | LabelResampleParamsDictTagged


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


def label_resample_area_surfs_cargs(
    params: LabelResampleAreaSurfsParamsDict,
    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([
        "-area-surfs",
        execution.input_file(params.get("current-area", None)),
        execution.input_file(params.get("new-area", None))
    ])
    return cargs


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


def label_resample_area_metrics_cargs(
    params: LabelResampleAreaMetricsParamsDict,
    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([
        "-area-metrics",
        execution.input_file(params.get("current-area", None)),
        execution.input_file(params.get("new-area", None))
    ])
    return cargs


class LabelResampleValidRoiOutOutputs(typing.NamedTuple):
    """
    Output object returned when calling `LabelResampleValidRoiOutParamsDict | None(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    roi_out: OutputPathType
    """the output roi as a metric"""


def label_resample_valid_roi_out(
    roi_out: str,
) -> LabelResampleValidRoiOutParamsDictTagged:
    """
    Build parameters.
    
    Args:
        roi_out: the output roi as a metric.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "valid-roi-out",
        "roi-out": roi_out,
    }
    return params


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


def label_resample_valid_roi_out_cargs(
    params: LabelResampleValidRoiOutParamsDict,
    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([
        "-valid-roi-out",
        params.get("roi-out", None)
    ])
    return cargs


def label_resample_valid_roi_out_outputs(
    params: LabelResampleValidRoiOutParamsDict,
    execution: Execution,
) -> LabelResampleValidRoiOutOutputs:
    """
    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 = LabelResampleValidRoiOutOutputs(
        root=execution.output_file("."),
        roi_out=execution.output_file(params.get("roi-out", None)),
    )
    return ret


class LabelResampleOutputs(typing.NamedTuple):
    """
    Output object returned when calling `LabelResampleParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    label_out: OutputPathType
    """the output label file"""
    valid_roi_out: LabelResampleValidRoiOutOutputs | None
    """Outputs from `label_resample_valid_roi_out_outputs`."""


def label_resample_params(
    label_in: InputPathType,
    current_sphere: InputPathType,
    new_sphere: InputPathType,
    method: str,
    label_out: str,
    area_surfs: LabelResampleAreaSurfsParamsDict | None = None,
    area_metrics: LabelResampleAreaMetricsParamsDict | None = None,
    valid_roi_out: LabelResampleValidRoiOutParamsDict | None = None,
    roi_metric: InputPathType | None = None,
    bypass_sphere_check: bool = False,
    largest: bool = False,
) -> LabelResampleParamsDictTagged:
    """
    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.
        valid_roi_out: output the ROI of vertices that got data from valid\
            source vertices.
        roi_metric: use an input roi on the current mesh to exclude non-data\
            vertices\
            \
            the roi, as a metric file.
        bypass_sphere_check: ADVANCED: allow the current and new 'spheres' to\
            have arbitrary shape as long as they follow the same contour.
        largest: use only the label of the vertex with the largest weight.
    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,
        "bypass-sphere-check": bypass_sphere_check,
        "largest": largest,
    }
    if area_surfs is not None:
        params["area-surfs"] = area_surfs
    if area_metrics is not None:
        params["area-metrics"] = area_metrics
    if valid_roi_out is not None:
        params["valid-roi-out"] = valid_roi_out
    if roi_metric is not None:
        params["roi-metric"] = roi_metric
    return params


def label_resample_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelResampleParamsDict` 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("label-in", None) is None:
        raise StyxValidationError("`label-in` must not be None")
    if not isinstance(params["label-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label-in` has the wrong type: Received `{type(params.get("label-in", None))}` expected `InputPathType`')
    if params.get("current-sphere", None) is None:
        raise StyxValidationError("`current-sphere` must not be None")
    if not isinstance(params["current-sphere"], (pathlib.Path, str)):
        raise StyxValidationError(f'`current-sphere` has the wrong type: Received `{type(params.get("current-sphere", None))}` expected `InputPathType`')
    if params.get("new-sphere", None) is None:
        raise StyxValidationError("`new-sphere` must not be None")
    if not isinstance(params["new-sphere"], (pathlib.Path, str)):
        raise StyxValidationError(f'`new-sphere` has the wrong type: Received `{type(params.get("new-sphere", None))}` expected `InputPathType`')
    if params.get("method", None) is None:
        raise StyxValidationError("`method` must not be None")
    if not isinstance(params["method"], str):
        raise StyxValidationError(f'`method` has the wrong type: Received `{type(params.get("method", None))}` expected `str`')
    if params.get("label-out", None) is None:
        raise StyxValidationError("`label-out` must not be None")
    if not isinstance(params["label-out"], str):
        raise StyxValidationError(f'`label-out` has the wrong type: Received `{type(params.get("label-out", None))}` expected `str`')
    if params.get("area-surfs", None) is not None:
        label_resample_area_surfs_validate(params["area-surfs"])
    if params.get("area-metrics", None) is not None:
        label_resample_area_metrics_validate(params["area-metrics"])
    if params.get("valid-roi-out", None) is not None:
        label_resample_valid_roi_out_validate(params["valid-roi-out"])
    if params.get("roi-metric", None) is not None:
        if not isinstance(params["roi-metric"], (pathlib.Path, str)):
            raise StyxValidationError(f'`roi-metric` has the wrong type: Received `{type(params.get("roi-metric", None))}` expected `InputPathType | None`')
    if params.get("bypass-sphere-check", False) is None:
        raise StyxValidationError("`bypass-sphere-check` must not be None")
    if not isinstance(params["bypass-sphere-check"], bool):
        raise StyxValidationError(f'`bypass-sphere-check` has the wrong type: Received `{type(params.get("bypass-sphere-check", False))}` expected `bool`')
    if params.get("largest", False) is None:
        raise StyxValidationError("`largest` must not be None")
    if not isinstance(params["largest"], bool):
        raise StyxValidationError(f'`largest` has the wrong type: Received `{type(params.get("largest", False))}` expected `bool`')


def label_resample_cargs(
    params: LabelResampleParamsDict,
    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",
        "-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 or params.get("area-metrics", None) is not None or params.get("valid-roi-out", None) is not None:
        cargs.extend([
            *(label_resample_area_surfs_cargs(params.get("area-surfs", None), execution) if (params.get("area-surfs", None) is not None) else []),
            *(label_resample_area_metrics_cargs(params.get("area-metrics", None), execution) if (params.get("area-metrics", None) is not None) else []),
            *(label_resample_valid_roi_out_cargs(params.get("valid-roi-out", None), execution) if (params.get("valid-roi-out", None) is not None) else [])
        ])
    if params.get("roi-metric", None) is not None:
        cargs.extend([
            "-current-roi",
            execution.input_file(params.get("roi-metric", None))
        ])
    if params.get("bypass-sphere-check", False):
        cargs.append("-bypass-sphere-check")
    if params.get("largest", False):
        cargs.append("-largest")
    return cargs


def label_resample_outputs(
    params: LabelResampleParamsDict,
    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)),
        valid_roi_out=label_resample_valid_roi_out_outputs(params.get("valid-roi-out"), execution) if params.get("valid-roi-out") else None,
    )
    return ret


def label_resample_execute(
    params: LabelResampleParamsDict,
    runner: Runner | None = None,
) -> LabelResampleOutputs:
    """
    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
    .
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelResampleOutputs`).
    """
    label_resample_validate(params)
    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: LabelResampleAreaSurfsParamsDict | None = None,
    area_metrics: LabelResampleAreaMetricsParamsDict | None = None,
    valid_roi_out: LabelResampleValidRoiOutParamsDict | None = None,
    roi_metric: InputPathType | None = None,
    bypass_sphere_check: bool = False,
    largest: bool = False,
    runner: Runner | None = None,
) -> LabelResampleOutputs:
    """
    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
    .
    
    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.
        valid_roi_out: output the ROI of vertices that got data from valid\
            source vertices.
        roi_metric: use an input roi on the current mesh to exclude non-data\
            vertices\
            \
            the roi, as a metric file.
        bypass_sphere_check: ADVANCED: allow the current and new 'spheres' to\
            have arbitrary shape as long as they follow the same contour.
        largest: use only the label of the vertex with the largest weight.
        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,
        valid_roi_out=valid_roi_out,
        roi_metric=roi_metric,
        bypass_sphere_check=bypass_sphere_check,
        largest=largest,
    )
    return label_resample_execute(params, runner)


__all__ = [
    "LABEL_RESAMPLE_METADATA",
    "LabelResampleAreaMetricsParamsDict",
    "LabelResampleAreaMetricsParamsDictTagged",
    "LabelResampleAreaSurfsParamsDict",
    "LabelResampleAreaSurfsParamsDictTagged",
    "LabelResampleOutputs",
    "LabelResampleParamsDict",
    "LabelResampleParamsDictTagged",
    "LabelResampleValidRoiOutOutputs",
    "LabelResampleValidRoiOutParamsDict",
    "LabelResampleValidRoiOutParamsDictTagged",
    "label_resample",
    "label_resample_area_metrics",
    "label_resample_area_surfs",
    "label_resample_execute",
    "label_resample_params",
    "label_resample_valid_roi_out",
]
