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

import typing
import pathlib
from styxdefs import *

CIFTI_DILATE_METADATA = Metadata(
    id="224f2fa95412d16b7d3853836e3c088fb81a4158.workbench",
    name="cifti-dilate",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_CiftiDilateLeftSurfaceParamsDictNoTag = typing.TypedDict('_CiftiDilateLeftSurfaceParamsDictNoTag', {
    "surface": InputPathType,
    "area-metric": typing.NotRequired[InputPathType | None],
})
CiftiDilateLeftSurfaceParamsDictTagged = typing.TypedDict('CiftiDilateLeftSurfaceParamsDictTagged', {
    "@type": typing.Literal["left-surface"],
    "surface": InputPathType,
    "area-metric": typing.NotRequired[InputPathType | None],
})
CiftiDilateLeftSurfaceParamsDict = _CiftiDilateLeftSurfaceParamsDictNoTag | CiftiDilateLeftSurfaceParamsDictTagged


_CiftiDilateRightSurfaceParamsDictNoTag = typing.TypedDict('_CiftiDilateRightSurfaceParamsDictNoTag', {
    "surface": InputPathType,
    "area-metric": typing.NotRequired[InputPathType | None],
})
CiftiDilateRightSurfaceParamsDictTagged = typing.TypedDict('CiftiDilateRightSurfaceParamsDictTagged', {
    "@type": typing.Literal["right-surface"],
    "surface": InputPathType,
    "area-metric": typing.NotRequired[InputPathType | None],
})
CiftiDilateRightSurfaceParamsDict = _CiftiDilateRightSurfaceParamsDictNoTag | CiftiDilateRightSurfaceParamsDictTagged


_CiftiDilateCerebellumSurfaceParamsDictNoTag = typing.TypedDict('_CiftiDilateCerebellumSurfaceParamsDictNoTag', {
    "surface": InputPathType,
    "area-metric": typing.NotRequired[InputPathType | None],
})
CiftiDilateCerebellumSurfaceParamsDictTagged = typing.TypedDict('CiftiDilateCerebellumSurfaceParamsDictTagged', {
    "@type": typing.Literal["cerebellum-surface"],
    "surface": InputPathType,
    "area-metric": typing.NotRequired[InputPathType | None],
})
CiftiDilateCerebellumSurfaceParamsDict = _CiftiDilateCerebellumSurfaceParamsDictNoTag | CiftiDilateCerebellumSurfaceParamsDictTagged


_CiftiDilateParamsDictNoTag = typing.TypedDict('_CiftiDilateParamsDictNoTag', {
    "cifti-in": InputPathType,
    "direction": str,
    "surface-distance": float,
    "volume-distance": float,
    "cifti-out": str,
    "left-surface": typing.NotRequired[CiftiDilateLeftSurfaceParamsDict | None],
    "right-surface": typing.NotRequired[CiftiDilateRightSurfaceParamsDict | None],
    "cerebellum-surface": typing.NotRequired[CiftiDilateCerebellumSurfaceParamsDict | None],
    "roi-cifti": typing.NotRequired[InputPathType | None],
    "legacy-mode": bool,
    "merged-volume": bool,
    "nearest": bool,
})
CiftiDilateParamsDictTagged = typing.TypedDict('CiftiDilateParamsDictTagged', {
    "@type": typing.Literal["workbench/cifti-dilate"],
    "cifti-in": InputPathType,
    "direction": str,
    "surface-distance": float,
    "volume-distance": float,
    "cifti-out": str,
    "left-surface": typing.NotRequired[CiftiDilateLeftSurfaceParamsDict | None],
    "right-surface": typing.NotRequired[CiftiDilateRightSurfaceParamsDict | None],
    "cerebellum-surface": typing.NotRequired[CiftiDilateCerebellumSurfaceParamsDict | None],
    "roi-cifti": typing.NotRequired[InputPathType | None],
    "legacy-mode": bool,
    "merged-volume": bool,
    "nearest": bool,
})
CiftiDilateParamsDict = _CiftiDilateParamsDictNoTag | CiftiDilateParamsDictTagged


def cifti_dilate_left_surface(
    surface: InputPathType,
    area_metric: InputPathType | None = None,
) -> CiftiDilateLeftSurfaceParamsDictTagged:
    """
    Build parameters.
    
    Args:
        surface: the left surface file.
        area_metric: vertex areas to use instead of computing them from the\
            left surface\
            \
            the corrected vertex areas, as a metric.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "left-surface",
        "surface": surface,
    }
    if area_metric is not None:
        params["area-metric"] = area_metric
    return params


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


def cifti_dilate_left_surface_cargs(
    params: CiftiDilateLeftSurfaceParamsDict,
    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([
        "-left-surface",
        execution.input_file(params.get("surface", None))
    ])
    if params.get("area-metric", None) is not None:
        cargs.extend([
            "-left-corrected-areas",
            execution.input_file(params.get("area-metric", None))
        ])
    return cargs


def cifti_dilate_right_surface(
    surface: InputPathType,
    area_metric: InputPathType | None = None,
) -> CiftiDilateRightSurfaceParamsDictTagged:
    """
    Build parameters.
    
    Args:
        surface: the right surface file.
        area_metric: vertex areas to use instead of computing them from the\
            right surface\
            \
            the corrected vertex areas, as a metric.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "right-surface",
        "surface": surface,
    }
    if area_metric is not None:
        params["area-metric"] = area_metric
    return params


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


def cifti_dilate_right_surface_cargs(
    params: CiftiDilateRightSurfaceParamsDict,
    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([
        "-right-surface",
        execution.input_file(params.get("surface", None))
    ])
    if params.get("area-metric", None) is not None:
        cargs.extend([
            "-right-corrected-areas",
            execution.input_file(params.get("area-metric", None))
        ])
    return cargs


def cifti_dilate_cerebellum_surface(
    surface: InputPathType,
    area_metric: InputPathType | None = None,
) -> CiftiDilateCerebellumSurfaceParamsDictTagged:
    """
    Build parameters.
    
    Args:
        surface: the cerebellum surface file.
        area_metric: vertex areas to use instead of computing them from the\
            cerebellum surface\
            \
            the corrected vertex areas, as a metric.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "cerebellum-surface",
        "surface": surface,
    }
    if area_metric is not None:
        params["area-metric"] = area_metric
    return params


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


def cifti_dilate_cerebellum_surface_cargs(
    params: CiftiDilateCerebellumSurfaceParamsDict,
    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([
        "-cerebellum-surface",
        execution.input_file(params.get("surface", None))
    ])
    if params.get("area-metric", None) is not None:
        cargs.extend([
            "-cerebellum-corrected-areas",
            execution.input_file(params.get("area-metric", None))
        ])
    return cargs


class CiftiDilateOutputs(typing.NamedTuple):
    """
    Output object returned when calling `CiftiDilateParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    cifti_out: OutputPathType
    """the output cifti file"""


def cifti_dilate_params(
    cifti_in: InputPathType,
    direction: str,
    surface_distance: float,
    volume_distance: float,
    cifti_out: str,
    left_surface: CiftiDilateLeftSurfaceParamsDict | None = None,
    right_surface: CiftiDilateRightSurfaceParamsDict | None = None,
    cerebellum_surface: CiftiDilateCerebellumSurfaceParamsDict | None = None,
    roi_cifti: InputPathType | None = None,
    legacy_mode: bool = False,
    merged_volume: bool = False,
    nearest: bool = False,
) -> CiftiDilateParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_in: the input cifti file.
        direction: which dimension to dilate along, ROW or COLUMN.
        surface_distance: the distance to dilate on surfaces, in mm.
        volume_distance: the distance to dilate in the volume, in mm.
        cifti_out: the output cifti file.
        left_surface: specify the left surface to use.
        right_surface: specify the right surface to use.
        cerebellum_surface: specify the cerebellum surface to use.
        roi_cifti: specify an roi of brainordinates to overwrite, rather than\
            zeros\
            \
            cifti dscalar or dtseries file, positive values denote\
            brainordinates to have their values replaced.
        legacy_mode: use the math from v1.3.2 and earlier for weighted dilation.
        merged_volume: treat volume components as if they were a single\
            component.
        nearest: use nearest good value instead of a weighted average.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-dilate",
        "cifti-in": cifti_in,
        "direction": direction,
        "surface-distance": surface_distance,
        "volume-distance": volume_distance,
        "cifti-out": cifti_out,
        "legacy-mode": legacy_mode,
        "merged-volume": merged_volume,
        "nearest": nearest,
    }
    if left_surface is not None:
        params["left-surface"] = left_surface
    if right_surface is not None:
        params["right-surface"] = right_surface
    if cerebellum_surface is not None:
        params["cerebellum-surface"] = cerebellum_surface
    if roi_cifti is not None:
        params["roi-cifti"] = roi_cifti
    return params


def cifti_dilate_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiDilateParamsDict` 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("cifti-in", None) is None:
        raise StyxValidationError("`cifti-in` must not be None")
    if not isinstance(params["cifti-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`cifti-in` has the wrong type: Received `{type(params.get("cifti-in", None))}` expected `InputPathType`')
    if params.get("direction", None) is None:
        raise StyxValidationError("`direction` must not be None")
    if not isinstance(params["direction"], str):
        raise StyxValidationError(f'`direction` has the wrong type: Received `{type(params.get("direction", None))}` expected `str`')
    if params.get("surface-distance", None) is None:
        raise StyxValidationError("`surface-distance` must not be None")
    if not isinstance(params["surface-distance"], (float, int)):
        raise StyxValidationError(f'`surface-distance` has the wrong type: Received `{type(params.get("surface-distance", None))}` expected `float`')
    if params.get("volume-distance", None) is None:
        raise StyxValidationError("`volume-distance` must not be None")
    if not isinstance(params["volume-distance"], (float, int)):
        raise StyxValidationError(f'`volume-distance` has the wrong type: Received `{type(params.get("volume-distance", None))}` expected `float`')
    if params.get("cifti-out", None) is None:
        raise StyxValidationError("`cifti-out` must not be None")
    if not isinstance(params["cifti-out"], str):
        raise StyxValidationError(f'`cifti-out` has the wrong type: Received `{type(params.get("cifti-out", None))}` expected `str`')
    if params.get("left-surface", None) is not None:
        cifti_dilate_left_surface_validate(params["left-surface"])
    if params.get("right-surface", None) is not None:
        cifti_dilate_right_surface_validate(params["right-surface"])
    if params.get("cerebellum-surface", None) is not None:
        cifti_dilate_cerebellum_surface_validate(params["cerebellum-surface"])
    if params.get("roi-cifti", None) is not None:
        if not isinstance(params["roi-cifti"], (pathlib.Path, str)):
            raise StyxValidationError(f'`roi-cifti` has the wrong type: Received `{type(params.get("roi-cifti", None))}` expected `InputPathType | None`')
    if params.get("legacy-mode", False) is None:
        raise StyxValidationError("`legacy-mode` must not be None")
    if not isinstance(params["legacy-mode"], bool):
        raise StyxValidationError(f'`legacy-mode` has the wrong type: Received `{type(params.get("legacy-mode", False))}` expected `bool`')
    if params.get("merged-volume", False) is None:
        raise StyxValidationError("`merged-volume` must not be None")
    if not isinstance(params["merged-volume"], bool):
        raise StyxValidationError(f'`merged-volume` has the wrong type: Received `{type(params.get("merged-volume", False))}` expected `bool`')
    if params.get("nearest", False) is None:
        raise StyxValidationError("`nearest` must not be None")
    if not isinstance(params["nearest"], bool):
        raise StyxValidationError(f'`nearest` has the wrong type: Received `{type(params.get("nearest", False))}` expected `bool`')


def cifti_dilate_cargs(
    params: CiftiDilateParamsDict,
    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",
        "-cifti-dilate"
    ])
    cargs.append(execution.input_file(params.get("cifti-in", None)))
    cargs.append(params.get("direction", None))
    cargs.append(str(params.get("surface-distance", None)))
    cargs.append(str(params.get("volume-distance", None)))
    cargs.append(params.get("cifti-out", None))
    if params.get("left-surface", None) is not None or params.get("right-surface", None) is not None or params.get("cerebellum-surface", None) is not None:
        cargs.extend([
            *(cifti_dilate_left_surface_cargs(params.get("left-surface", None), execution) if (params.get("left-surface", None) is not None) else []),
            *(cifti_dilate_right_surface_cargs(params.get("right-surface", None), execution) if (params.get("right-surface", None) is not None) else []),
            *(cifti_dilate_cerebellum_surface_cargs(params.get("cerebellum-surface", None), execution) if (params.get("cerebellum-surface", None) is not None) else [])
        ])
    if params.get("roi-cifti", None) is not None:
        cargs.extend([
            "-bad-brainordinate-roi",
            execution.input_file(params.get("roi-cifti", None))
        ])
    if params.get("legacy-mode", False):
        cargs.append("-legacy-mode")
    if params.get("merged-volume", False):
        cargs.append("-merged-volume")
    if params.get("nearest", False):
        cargs.append("-nearest")
    return cargs


def cifti_dilate_outputs(
    params: CiftiDilateParamsDict,
    execution: Execution,
) -> CiftiDilateOutputs:
    """
    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 = CiftiDilateOutputs(
        root=execution.output_file("."),
        cifti_out=execution.output_file(params.get("cifti-out", None)),
    )
    return ret


def cifti_dilate_execute(
    params: CiftiDilateParamsDict,
    runner: Runner | None = None,
) -> CiftiDilateOutputs:
    """
    DILATE A CIFTI FILE.
    
    For all data values designated as bad, if they neighbor a good value or are
    within the specified distance of a good value in the same kind of model,
    replace the value with a distance weighted average of nearby good values,
    otherwise set the value to zero. If -nearest is specified, it will use the
    value from the closest good value within range instead of a weighted
    average. When the input file contains label data, nearest dilation is used
    on the surface, and weighted popularity is used in the volume.
    
    The -*-corrected-areas options are intended for dilating on group average
    surfaces, but it is only an approximate correction for the reduction of
    structure in a group average surface.
    
    If -bad-brainordinate-roi is specified, all values, including those with
    value zero, are good, except for locations with a positive value in the ROI.
    If it is not specified, only values equal to zero are bad.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiDilateOutputs`).
    """
    cifti_dilate_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_DILATE_METADATA)
    params = execution.params(params)
    cargs = cifti_dilate_cargs(params, execution)
    ret = cifti_dilate_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_dilate(
    cifti_in: InputPathType,
    direction: str,
    surface_distance: float,
    volume_distance: float,
    cifti_out: str,
    left_surface: CiftiDilateLeftSurfaceParamsDict | None = None,
    right_surface: CiftiDilateRightSurfaceParamsDict | None = None,
    cerebellum_surface: CiftiDilateCerebellumSurfaceParamsDict | None = None,
    roi_cifti: InputPathType | None = None,
    legacy_mode: bool = False,
    merged_volume: bool = False,
    nearest: bool = False,
    runner: Runner | None = None,
) -> CiftiDilateOutputs:
    """
    DILATE A CIFTI FILE.
    
    For all data values designated as bad, if they neighbor a good value or are
    within the specified distance of a good value in the same kind of model,
    replace the value with a distance weighted average of nearby good values,
    otherwise set the value to zero. If -nearest is specified, it will use the
    value from the closest good value within range instead of a weighted
    average. When the input file contains label data, nearest dilation is used
    on the surface, and weighted popularity is used in the volume.
    
    The -*-corrected-areas options are intended for dilating on group average
    surfaces, but it is only an approximate correction for the reduction of
    structure in a group average surface.
    
    If -bad-brainordinate-roi is specified, all values, including those with
    value zero, are good, except for locations with a positive value in the ROI.
    If it is not specified, only values equal to zero are bad.
    
    Args:
        cifti_in: the input cifti file.
        direction: which dimension to dilate along, ROW or COLUMN.
        surface_distance: the distance to dilate on surfaces, in mm.
        volume_distance: the distance to dilate in the volume, in mm.
        cifti_out: the output cifti file.
        left_surface: specify the left surface to use.
        right_surface: specify the right surface to use.
        cerebellum_surface: specify the cerebellum surface to use.
        roi_cifti: specify an roi of brainordinates to overwrite, rather than\
            zeros\
            \
            cifti dscalar or dtseries file, positive values denote\
            brainordinates to have their values replaced.
        legacy_mode: use the math from v1.3.2 and earlier for weighted dilation.
        merged_volume: treat volume components as if they were a single\
            component.
        nearest: use nearest good value instead of a weighted average.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiDilateOutputs`).
    """
    params = cifti_dilate_params(
        cifti_in=cifti_in,
        direction=direction,
        surface_distance=surface_distance,
        volume_distance=volume_distance,
        cifti_out=cifti_out,
        left_surface=left_surface,
        right_surface=right_surface,
        cerebellum_surface=cerebellum_surface,
        roi_cifti=roi_cifti,
        legacy_mode=legacy_mode,
        merged_volume=merged_volume,
        nearest=nearest,
    )
    return cifti_dilate_execute(params, runner)


__all__ = [
    "CIFTI_DILATE_METADATA",
    "CiftiDilateCerebellumSurfaceParamsDict",
    "CiftiDilateCerebellumSurfaceParamsDictTagged",
    "CiftiDilateLeftSurfaceParamsDict",
    "CiftiDilateLeftSurfaceParamsDictTagged",
    "CiftiDilateOutputs",
    "CiftiDilateParamsDict",
    "CiftiDilateParamsDictTagged",
    "CiftiDilateRightSurfaceParamsDict",
    "CiftiDilateRightSurfaceParamsDictTagged",
    "cifti_dilate",
    "cifti_dilate_cerebellum_surface",
    "cifti_dilate_execute",
    "cifti_dilate_left_surface",
    "cifti_dilate_params",
    "cifti_dilate_right_surface",
]
