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

import typing
import pathlib
from styxdefs import *

VOLUME_PARCEL_RESAMPLING_METADATA = Metadata(
    id="b8666fd0e3fda0690696a4034402cad85826cbf3.workbench",
    name="volume-parcel-resampling",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeParcelResamplingParamsDictNoTag = typing.TypedDict('_VolumeParcelResamplingParamsDictNoTag', {
    "volume-in": InputPathType,
    "cur-parcels": InputPathType,
    "new-parcels": InputPathType,
    "kernel": float,
    "volume-out": str,
    "subvol": typing.NotRequired[str | None],
    "fwhm": bool,
    "fix-zeros": bool,
})
VolumeParcelResamplingParamsDictTagged = typing.TypedDict('VolumeParcelResamplingParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-parcel-resampling"],
    "volume-in": InputPathType,
    "cur-parcels": InputPathType,
    "new-parcels": InputPathType,
    "kernel": float,
    "volume-out": str,
    "subvol": typing.NotRequired[str | None],
    "fwhm": bool,
    "fix-zeros": bool,
})
VolumeParcelResamplingParamsDict = _VolumeParcelResamplingParamsDictNoTag | VolumeParcelResamplingParamsDictTagged


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


def volume_parcel_resampling_params(
    volume_in: InputPathType,
    cur_parcels: InputPathType,
    new_parcels: InputPathType,
    kernel: float,
    volume_out: str,
    subvol: str | None = None,
    fwhm: bool = False,
    fix_zeros: bool = False,
) -> VolumeParcelResamplingParamsDictTagged:
    """
    Build parameters.
    
    Args:
        volume_in: the input data volume.
        cur_parcels: label volume of where the parcels currently are.
        new_parcels: label volume of where the parcels should be.
        kernel: gaussian kernel size in mm to smooth by during resampling, as\
            sigma by default.
        volume_out: output volume.
        subvol: select a single subvolume as input\
            \
            the subvolume number or name.
        fwhm: smoothing kernel size is FWHM, not sigma.
        fix_zeros: treat zero values as not being data.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-parcel-resampling",
        "volume-in": volume_in,
        "cur-parcels": cur_parcels,
        "new-parcels": new_parcels,
        "kernel": kernel,
        "volume-out": volume_out,
        "fwhm": fwhm,
        "fix-zeros": fix_zeros,
    }
    if subvol is not None:
        params["subvol"] = subvol
    return params


def volume_parcel_resampling_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeParcelResamplingParamsDict` 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("volume-in", None) is None:
        raise StyxValidationError("`volume-in` must not be None")
    if not isinstance(params["volume-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`volume-in` has the wrong type: Received `{type(params.get("volume-in", None))}` expected `InputPathType`')
    if params.get("cur-parcels", None) is None:
        raise StyxValidationError("`cur-parcels` must not be None")
    if not isinstance(params["cur-parcels"], (pathlib.Path, str)):
        raise StyxValidationError(f'`cur-parcels` has the wrong type: Received `{type(params.get("cur-parcels", None))}` expected `InputPathType`')
    if params.get("new-parcels", None) is None:
        raise StyxValidationError("`new-parcels` must not be None")
    if not isinstance(params["new-parcels"], (pathlib.Path, str)):
        raise StyxValidationError(f'`new-parcels` has the wrong type: Received `{type(params.get("new-parcels", 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("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`')
    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`')


def volume_parcel_resampling_cargs(
    params: VolumeParcelResamplingParamsDict,
    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-resampling"
    ])
    cargs.append(execution.input_file(params.get("volume-in", None)))
    cargs.append(execution.input_file(params.get("cur-parcels", None)))
    cargs.append(execution.input_file(params.get("new-parcels", 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("fwhm", False):
        cargs.append("-fwhm")
    if params.get("fix-zeros", False):
        cargs.append("-fix-zeros")
    return cargs


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


def volume_parcel_resampling_execute(
    params: VolumeParcelResamplingParamsDict,
    runner: Runner | None = None,
) -> VolumeParcelResamplingOutputs:
    """
    SMOOTH AND RESAMPLE VOLUME PARCELS.
    
    Smooths and resamples the region inside each label in cur-parcels to the
    region of the same label name in new-parcels. Any voxels in the output label
    region but outside the input label region will be extrapolated from nearby
    data. The -fix-zeros option causes the smoothing to not use an input value
    if it is zero, but still write a smoothed value to the voxel, and after
    smoothing is complete, it will check for any remaining values of zero, and
    fill them in with extrapolated values.
    
    Note: all volumes must have the same dimensions and spacing. To use a
    different output space, see -volume-parcel-resampling-generic.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeParcelResamplingOutputs`).
    """
    volume_parcel_resampling_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_PARCEL_RESAMPLING_METADATA)
    params = execution.params(params)
    cargs = volume_parcel_resampling_cargs(params, execution)
    ret = volume_parcel_resampling_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_parcel_resampling(
    volume_in: InputPathType,
    cur_parcels: InputPathType,
    new_parcels: InputPathType,
    kernel: float,
    volume_out: str,
    subvol: str | None = None,
    fwhm: bool = False,
    fix_zeros: bool = False,
    runner: Runner | None = None,
) -> VolumeParcelResamplingOutputs:
    """
    SMOOTH AND RESAMPLE VOLUME PARCELS.
    
    Smooths and resamples the region inside each label in cur-parcels to the
    region of the same label name in new-parcels. Any voxels in the output label
    region but outside the input label region will be extrapolated from nearby
    data. The -fix-zeros option causes the smoothing to not use an input value
    if it is zero, but still write a smoothed value to the voxel, and after
    smoothing is complete, it will check for any remaining values of zero, and
    fill them in with extrapolated values.
    
    Note: all volumes must have the same dimensions and spacing. To use a
    different output space, see -volume-parcel-resampling-generic.
    
    Args:
        volume_in: the input data volume.
        cur_parcels: label volume of where the parcels currently are.
        new_parcels: label volume of where the parcels should be.
        kernel: gaussian kernel size in mm to smooth by during resampling, as\
            sigma by default.
        volume_out: output volume.
        subvol: select a single subvolume as input\
            \
            the subvolume number or name.
        fwhm: smoothing kernel size is FWHM, not sigma.
        fix_zeros: treat zero values as not being data.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeParcelResamplingOutputs`).
    """
    params = volume_parcel_resampling_params(
        volume_in=volume_in,
        cur_parcels=cur_parcels,
        new_parcels=new_parcels,
        kernel=kernel,
        volume_out=volume_out,
        subvol=subvol,
        fwhm=fwhm,
        fix_zeros=fix_zeros,
    )
    return volume_parcel_resampling_execute(params, runner)


__all__ = [
    "VOLUME_PARCEL_RESAMPLING_METADATA",
    "VolumeParcelResamplingOutputs",
    "VolumeParcelResamplingParamsDict",
    "VolumeParcelResamplingParamsDictTagged",
    "volume_parcel_resampling",
    "volume_parcel_resampling_execute",
    "volume_parcel_resampling_params",
]
