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

import typing
import pathlib
from styxdefs import *

VOLUME_LABEL_PROBABILITY_METADATA = Metadata(
    id="433915b90ed4936671e31ff5e9d775b5a9ffb4e9.workbench",
    name="volume-label-probability",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeLabelProbabilityParamsDictNoTag = typing.TypedDict('_VolumeLabelProbabilityParamsDictNoTag', {
    "label-maps": InputPathType,
    "probability-out": str,
    "exclude-unlabeled": bool,
})
VolumeLabelProbabilityParamsDictTagged = typing.TypedDict('VolumeLabelProbabilityParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-label-probability"],
    "label-maps": InputPathType,
    "probability-out": str,
    "exclude-unlabeled": bool,
})
VolumeLabelProbabilityParamsDict = _VolumeLabelProbabilityParamsDictNoTag | VolumeLabelProbabilityParamsDictTagged


class VolumeLabelProbabilityOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeLabelProbabilityParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    probability_out: OutputPathType
    """the relative frequencies of each label at each voxel"""


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


def volume_label_probability_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeLabelProbabilityParamsDict` 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-out", None) is None:
        raise StyxValidationError("`probability-out` must not be None")
    if not isinstance(params["probability-out"], str):
        raise StyxValidationError(f'`probability-out` has the wrong type: Received `{type(params.get("probability-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 volume_label_probability_cargs(
    params: VolumeLabelProbabilityParamsDict,
    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",
        "-volume-label-probability"
    ])
    cargs.append(execution.input_file(params.get("label-maps", None)))
    cargs.append(params.get("probability-out", None))
    if params.get("exclude-unlabeled", False):
        cargs.append("-exclude-unlabeled")
    return cargs


def volume_label_probability_outputs(
    params: VolumeLabelProbabilityParamsDict,
    execution: Execution,
) -> VolumeLabelProbabilityOutputs:
    """
    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 = VolumeLabelProbabilityOutputs(
        root=execution.output_file("."),
        probability_out=execution.output_file(params.get("probability-out", None)),
    )
    return ret


def volume_label_probability_execute(
    params: VolumeLabelProbabilityParamsDict,
    runner: Runner | None = None,
) -> VolumeLabelProbabilityOutputs:
    """
    FIND FREQUENCY OF VOLUME 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 voxel,
    divided by the number of input maps.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeLabelProbabilityOutputs`).
    """
    volume_label_probability_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_LABEL_PROBABILITY_METADATA)
    params = execution.params(params)
    cargs = volume_label_probability_cargs(params, execution)
    ret = volume_label_probability_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_label_probability(
    label_maps: InputPathType,
    probability_out: str,
    exclude_unlabeled: bool = False,
    runner: Runner | None = None,
) -> VolumeLabelProbabilityOutputs:
    """
    FIND FREQUENCY OF VOLUME 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 voxel,
    divided by the number of input maps.
    
    Args:
        label_maps: volume label file containing individual label maps from\
            many subjects.
        probability_out: the relative frequencies of each label at each voxel.
        exclude_unlabeled: don't make a probability map of the unlabeled key.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeLabelProbabilityOutputs`).
    """
    params = volume_label_probability_params(
        label_maps=label_maps,
        probability_out=probability_out,
        exclude_unlabeled=exclude_unlabeled,
    )
    return volume_label_probability_execute(params, runner)


__all__ = [
    "VOLUME_LABEL_PROBABILITY_METADATA",
    "VolumeLabelProbabilityOutputs",
    "VolumeLabelProbabilityParamsDict",
    "VolumeLabelProbabilityParamsDictTagged",
    "volume_label_probability",
    "volume_label_probability_execute",
    "volume_label_probability_params",
]
