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

import typing
import pathlib
from styxdefs import *

LABEL_PROBABILITY_METADATA = Metadata(
    id="3036af31e9086ad81e72bfa02cf68fef734a77c9.workbench",
    name="label-probability",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_LabelProbabilityParamsDictNoTag = typing.TypedDict('_LabelProbabilityParamsDictNoTag', {
    "label-maps": InputPathType,
    "probability-metric-out": str,
    "exclude-unlabeled": bool,
})
LabelProbabilityParamsDictTagged = typing.TypedDict('LabelProbabilityParamsDictTagged', {
    "@type": typing.Literal["workbench/label-probability"],
    "label-maps": InputPathType,
    "probability-metric-out": str,
    "exclude-unlabeled": bool,
})
LabelProbabilityParamsDict = _LabelProbabilityParamsDictNoTag | LabelProbabilityParamsDictTagged


class LabelProbabilityOutputs(typing.NamedTuple):
    """
    Output object returned when calling `LabelProbabilityParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    probability_metric_out: OutputPathType
    """the relative frequencies of each label at each vertex"""


def label_probability_params(
    label_maps: InputPathType,
    probability_metric_out: str,
    exclude_unlabeled: bool = False,
) -> LabelProbabilityParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label_maps: label file containing individual label maps from many\
            subjects.
        probability_metric_out: the relative frequencies of each label at each\
            vertex.
        exclude_unlabeled: don't make a probability map of the unlabeled key.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/label-probability",
        "label-maps": label_maps,
        "probability-metric-out": probability_metric_out,
        "exclude-unlabeled": exclude_unlabeled,
    }
    return params


def label_probability_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelProbabilityParamsDict` 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-maps", None) is None:
        raise StyxValidationError("`label-maps` must not be None")
    if not isinstance(params["label-maps"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label-maps` has the wrong type: Received `{type(params.get("label-maps", None))}` expected `InputPathType`')
    if params.get("probability-metric-out", None) is None:
        raise StyxValidationError("`probability-metric-out` must not be None")
    if not isinstance(params["probability-metric-out"], str):
        raise StyxValidationError(f'`probability-metric-out` has the wrong type: Received `{type(params.get("probability-metric-out", None))}` expected `str`')
    if params.get("exclude-unlabeled", False) is None:
        raise StyxValidationError("`exclude-unlabeled` must not be None")
    if not isinstance(params["exclude-unlabeled"], bool):
        raise StyxValidationError(f'`exclude-unlabeled` has the wrong type: Received `{type(params.get("exclude-unlabeled", False))}` expected `bool`')


def label_probability_cargs(
    params: LabelProbabilityParamsDict,
    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-probability"
    ])
    cargs.append(execution.input_file(params.get("label-maps", None)))
    cargs.append(params.get("probability-metric-out", None))
    if params.get("exclude-unlabeled", False):
        cargs.append("-exclude-unlabeled")
    return cargs


def label_probability_outputs(
    params: LabelProbabilityParamsDict,
    execution: Execution,
) -> LabelProbabilityOutputs:
    """
    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 = LabelProbabilityOutputs(
        root=execution.output_file("."),
        probability_metric_out=execution.output_file(params.get("probability-metric-out", None)),
    )
    return ret


def label_probability_execute(
    params: LabelProbabilityParamsDict,
    runner: Runner | None = None,
) -> LabelProbabilityOutputs:
    """
    FIND FREQUENCY OF SURFACE LABELS.
    
    This command outputs a set of soft ROIs, one for each label in the input,
    where the value is how many of the input maps had that label at that vertex,
    divided by the number of input maps.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelProbabilityOutputs`).
    """
    label_probability_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(LABEL_PROBABILITY_METADATA)
    params = execution.params(params)
    cargs = label_probability_cargs(params, execution)
    ret = label_probability_outputs(params, execution)
    execution.run(cargs)
    return ret


def label_probability(
    label_maps: InputPathType,
    probability_metric_out: str,
    exclude_unlabeled: bool = False,
    runner: Runner | None = None,
) -> LabelProbabilityOutputs:
    """
    FIND FREQUENCY OF SURFACE LABELS.
    
    This command outputs a set of soft ROIs, one for each label in the input,
    where the value is how many of the input maps had that label at that vertex,
    divided by the number of input maps.
    
    Args:
        label_maps: label file containing individual label maps from many\
            subjects.
        probability_metric_out: the relative frequencies of each label at each\
            vertex.
        exclude_unlabeled: don't make a probability map of the unlabeled key.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelProbabilityOutputs`).
    """
    params = label_probability_params(
        label_maps=label_maps,
        probability_metric_out=probability_metric_out,
        exclude_unlabeled=exclude_unlabeled,
    )
    return label_probability_execute(params, runner)


__all__ = [
    "LABEL_PROBABILITY_METADATA",
    "LabelProbabilityOutputs",
    "LabelProbabilityParamsDict",
    "LabelProbabilityParamsDictTagged",
    "label_probability",
    "label_probability_execute",
    "label_probability_params",
]
