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

import typing
import pathlib
from styxdefs import *

VOLUME_PARCEL_SMOOTHING_METADATA = Metadata(
    id="4531678f8f3118c9ec57f1efd95f5ab4ee4503d2.workbench",
    name="volume-parcel-smoothing",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeParcelSmoothingParamsDictNoTag = typing.TypedDict('_VolumeParcelSmoothingParamsDictNoTag', {
    "data-volume": InputPathType,
    "label-volume": InputPathType,
    "kernel": float,
    "volume-out": str,
    "subvol": typing.NotRequired[str | None],
    "fix-zeros": bool,
    "fwhm": bool,
})
VolumeParcelSmoothingParamsDictTagged = typing.TypedDict('VolumeParcelSmoothingParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-parcel-smoothing"],
    "data-volume": InputPathType,
    "label-volume": InputPathType,
    "kernel": float,
    "volume-out": str,
    "subvol": typing.NotRequired[str | None],
    "fix-zeros": bool,
    "fwhm": bool,
})
VolumeParcelSmoothingParamsDict = _VolumeParcelSmoothingParamsDictNoTag | VolumeParcelSmoothingParamsDictTagged


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


def volume_parcel_smoothing_params(
    data_volume: InputPathType,
    label_volume: InputPathType,
    kernel: float,
    volume_out: str,
    subvol: str | None = None,
    fix_zeros: bool = False,
    fwhm: bool = False,
) -> VolumeParcelSmoothingParamsDictTagged:
    """
    Build parameters.
    
    Args:
        data_volume: the volume to smooth.
        label_volume: a label volume containing the parcels to smooth.
        kernel: the size of the gaussian smoothing kernel in mm, as sigma by\
            default.
        volume_out: the output volume.
        subvol: select a single subvolume to smooth\
            \
            the subvolume number or name.
        fix_zeros: treat zero values as not being data.
        fwhm: smoothing kernel size is FWHM, not sigma.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-parcel-smoothing",
        "data-volume": data_volume,
        "label-volume": label_volume,
        "kernel": kernel,
        "volume-out": volume_out,
        "fix-zeros": fix_zeros,
        "fwhm": fwhm,
    }
    if subvol is not None:
        params["subvol"] = subvol
    return params


def volume_parcel_smoothing_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeParcelSmoothingParamsDict` 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("data-volume", None) is None:
        raise StyxValidationError("`data-volume` must not be None")
    if not isinstance(params["data-volume"], (pathlib.Path, str)):
        raise StyxValidationError(f'`data-volume` has the wrong type: Received `{type(params.get("data-volume", None))}` expected `InputPathType`')
    if params.get("label-volume", None) is None:
        raise StyxValidationError("`label-volume` must not be None")
    if not isinstance(params["label-volume"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label-volume` has the wrong type: Received `{type(params.get("label-volume", None))}` expected `InputPathType`')
    if params.get("kernel", None) is None:
        raise StyxValidationError("`kernel` must not be None")
    if not isinstance(params["kernel"], (float, int)):
        raise StyxValidationError(f'`kernel` has the wrong type: Received `{type(params.get("kernel", None))}` expected `float`')
    if params.get("volume-out", None) is None:
        raise StyxValidationError("`volume-out` must not be None")
    if not isinstance(params["volume-out"], str):
        raise StyxValidationError(f'`volume-out` has the wrong type: Received `{type(params.get("volume-out", None))}` expected `str`')
    if params.get("subvol", None) is not None:
        if not isinstance(params["subvol"], str):
            raise StyxValidationError(f'`subvol` has the wrong type: Received `{type(params.get("subvol", None))}` expected `str | None`')
    if params.get("fix-zeros", False) is None:
        raise StyxValidationError("`fix-zeros` must not be None")
    if not isinstance(params["fix-zeros"], bool):
        raise StyxValidationError(f'`fix-zeros` has the wrong type: Received `{type(params.get("fix-zeros", False))}` expected `bool`')
    if params.get("fwhm", False) is None:
        raise StyxValidationError("`fwhm` must not be None")
    if not isinstance(params["fwhm"], bool):
        raise StyxValidationError(f'`fwhm` has the wrong type: Received `{type(params.get("fwhm", False))}` expected `bool`')


def volume_parcel_smoothing_cargs(
    params: VolumeParcelSmoothingParamsDict,
    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-parcel-smoothing"
    ])
    cargs.append(execution.input_file(params.get("data-volume", None)))
    cargs.append(execution.input_file(params.get("label-volume", None)))
    cargs.append(str(params.get("kernel", None)))
    cargs.append(params.get("volume-out", None))
    if params.get("subvol", None) is not None:
        cargs.extend([
            "-subvolume",
            params.get("subvol", None)
        ])
    if params.get("fix-zeros", False):
        cargs.append("-fix-zeros")
    if params.get("fwhm", False):
        cargs.append("-fwhm")
    return cargs


def volume_parcel_smoothing_outputs(
    params: VolumeParcelSmoothingParamsDict,
    execution: Execution,
) -> VolumeParcelSmoothingOutputs:
    """
    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 = VolumeParcelSmoothingOutputs(
        root=execution.output_file("."),
        volume_out=execution.output_file(params.get("volume-out", None)),
    )
    return ret


def volume_parcel_smoothing_execute(
    params: VolumeParcelSmoothingParamsDict,
    runner: Runner | None = None,
) -> VolumeParcelSmoothingOutputs:
    """
    SMOOTH PARCELS IN A VOLUME SEPARATELY.
    
    The volume is smoothed within each label in the label volume using data only
    from within the label. Equivalent to running volume smoothing with ROIs
    matching each label separately, then adding the resulting volumes, but
    faster.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeParcelSmoothingOutputs`).
    """
    volume_parcel_smoothing_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_PARCEL_SMOOTHING_METADATA)
    params = execution.params(params)
    cargs = volume_parcel_smoothing_cargs(params, execution)
    ret = volume_parcel_smoothing_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_parcel_smoothing(
    data_volume: InputPathType,
    label_volume: InputPathType,
    kernel: float,
    volume_out: str,
    subvol: str | None = None,
    fix_zeros: bool = False,
    fwhm: bool = False,
    runner: Runner | None = None,
) -> VolumeParcelSmoothingOutputs:
    """
    SMOOTH PARCELS IN A VOLUME SEPARATELY.
    
    The volume is smoothed within each label in the label volume using data only
    from within the label. Equivalent to running volume smoothing with ROIs
    matching each label separately, then adding the resulting volumes, but
    faster.
    
    Args:
        data_volume: the volume to smooth.
        label_volume: a label volume containing the parcels to smooth.
        kernel: the size of the gaussian smoothing kernel in mm, as sigma by\
            default.
        volume_out: the output volume.
        subvol: select a single subvolume to smooth\
            \
            the subvolume number or name.
        fix_zeros: treat zero values as not being data.
        fwhm: smoothing kernel size is FWHM, not sigma.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeParcelSmoothingOutputs`).
    """
    params = volume_parcel_smoothing_params(
        data_volume=data_volume,
        label_volume=label_volume,
        kernel=kernel,
        volume_out=volume_out,
        subvol=subvol,
        fix_zeros=fix_zeros,
        fwhm=fwhm,
    )
    return volume_parcel_smoothing_execute(params, runner)


__all__ = [
    "VOLUME_PARCEL_SMOOTHING_METADATA",
    "VolumeParcelSmoothingOutputs",
    "VolumeParcelSmoothingParamsDict",
    "VolumeParcelSmoothingParamsDictTagged",
    "volume_parcel_smoothing",
    "volume_parcel_smoothing_execute",
    "volume_parcel_smoothing_params",
]
