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

import typing
import pathlib
from styxdefs import *

MASKFILTER_METADATA = Metadata(
    id="b76a6dd73084b9b3914644ade090d2c6bf379b1d.boutiques",
    name="maskfilter",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_MaskfilterVariousStringParamsDictNoTag = typing.TypedDict('_MaskfilterVariousStringParamsDictNoTag', {
    "obj": str,
})
MaskfilterVariousStringParamsDictTagged = typing.TypedDict('MaskfilterVariousStringParamsDictTagged', {
    "@type": typing.Literal["VariousString"],
    "obj": str,
})
MaskfilterVariousStringParamsDict = _MaskfilterVariousStringParamsDictNoTag | MaskfilterVariousStringParamsDictTagged


_MaskfilterVariousFileParamsDictNoTag = typing.TypedDict('_MaskfilterVariousFileParamsDictNoTag', {
    "obj": InputPathType,
})
MaskfilterVariousFileParamsDictTagged = typing.TypedDict('MaskfilterVariousFileParamsDictTagged', {
    "@type": typing.Literal["VariousFile"],
    "obj": InputPathType,
})
MaskfilterVariousFileParamsDict = _MaskfilterVariousFileParamsDictNoTag | MaskfilterVariousFileParamsDictTagged


_MaskfilterConfigParamsDictNoTag = typing.TypedDict('_MaskfilterConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
MaskfilterConfigParamsDictTagged = typing.TypedDict('MaskfilterConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
MaskfilterConfigParamsDict = _MaskfilterConfigParamsDictNoTag | MaskfilterConfigParamsDictTagged


_MaskfilterParamsDictNoTag = typing.TypedDict('_MaskfilterParamsDictNoTag', {
    "scale": typing.NotRequired[int | None],
    "axes": typing.NotRequired[list[int] | None],
    "largest": bool,
    "connectivity": bool,
    "npass": typing.NotRequired[int | None],
    "extent": typing.NotRequired[list[int] | None],
    "strides": typing.NotRequired[typing.Union[MaskfilterVariousStringParamsDictTagged, MaskfilterVariousFileParamsDictTagged] | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MaskfilterConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "input": InputPathType,
    "filter": str,
    "output": str,
})
MaskfilterParamsDictTagged = typing.TypedDict('MaskfilterParamsDictTagged', {
    "@type": typing.Literal["mrtrix/maskfilter"],
    "scale": typing.NotRequired[int | None],
    "axes": typing.NotRequired[list[int] | None],
    "largest": bool,
    "connectivity": bool,
    "npass": typing.NotRequired[int | None],
    "extent": typing.NotRequired[list[int] | None],
    "strides": typing.NotRequired[typing.Union[MaskfilterVariousStringParamsDictTagged, MaskfilterVariousFileParamsDictTagged] | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MaskfilterConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "input": InputPathType,
    "filter": str,
    "output": str,
})
MaskfilterParamsDict = _MaskfilterParamsDictNoTag | MaskfilterParamsDictTagged


def maskfilter_strides_cargs_dyn_fn(
    t: str,
) -> typing.Any:
    """
    Get build cargs function by command type.
    
    Args:
        t: Command type.
    Returns:
        Build cargs function.
    """
    return {
        "VariousString": maskfilter_various_string_cargs,
        "VariousFile": maskfilter_various_file_cargs,
    }.get(t)


def maskfilter_strides_validate_dyn_fn(
    t: str,
) -> typing.Any:
    """
    Get validate params function by command type.
    
    Args:
        t: Command type.
    Returns:
        Validate params function.
    """
    return {
        "VariousString": maskfilter_various_string_validate,
        "VariousFile": maskfilter_various_file_validate,
    }.get(t)


def maskfilter_various_string(
    obj: str,
) -> MaskfilterVariousStringParamsDictTagged:
    """
    Build parameters.
    
    Args:
        obj: String object.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "VariousString",
        "obj": obj,
    }
    return params


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


def maskfilter_various_string_cargs(
    params: MaskfilterVariousStringParamsDict,
    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.append(params.get("obj", None))
    return cargs


def maskfilter_various_file(
    obj: InputPathType,
) -> MaskfilterVariousFileParamsDictTagged:
    """
    Build parameters.
    
    Args:
        obj: File object.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "VariousFile",
        "obj": obj,
    }
    return params


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


def maskfilter_various_file_cargs(
    params: MaskfilterVariousFileParamsDict,
    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.append(execution.input_file(params.get("obj", None)))
    return cargs


def maskfilter_config(
    key: str,
    value: str,
) -> MaskfilterConfigParamsDictTagged:
    """
    Build parameters.
    
    Args:
        key: temporarily set the value of an MRtrix config file entry.
        value: temporarily set the value of an MRtrix config file entry.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "config",
        "key": key,
        "value": value,
    }
    return params


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


def maskfilter_config_cargs(
    params: MaskfilterConfigParamsDict,
    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.append("-config")
    cargs.append(params.get("key", None))
    cargs.append(params.get("value", None))
    return cargs


class MaskfilterOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MaskfilterParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output: OutputPathType
    """the output image."""


def maskfilter_params(
    input_: InputPathType,
    filter_: str,
    output: str,
    scale: int | None = None,
    axes: list[int] | None = None,
    largest: bool = False,
    connectivity: bool = False,
    npass: int | None = None,
    extent: list[int] | None = None,
    strides: typing.Union[MaskfilterVariousStringParamsDictTagged, MaskfilterVariousFileParamsDictTagged] | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MaskfilterConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> MaskfilterParamsDictTagged:
    """
    Build parameters.
    
    Args:
        input_: the input image.
        filter_: the type of filter to be applied (clean, connect, dilate,\
            erode, median).
        output: the output image.
        scale: the maximum scale used to cut bridges. A certain maximum scale\
            cuts bridges up to a width (in voxels) of 2x the provided scale.\
            (Default: 2).
        axes: specify which axes should be included in the connected\
            components. By default only the first 3 axes are included. The axes\
            should be provided as a comma-separated list of values.
        largest: only retain the largest connected component.
        connectivity: use 26-voxel-neighbourhood connectivity (Default: 6).
        npass: the number of times to repeatedly apply the filter.
        extent: specify the extent (width) of kernel size in voxels. This can\
            be specified either as a single value to be used for all axes, or as a\
            comma-separated list of the extent for each axis. The default is 3x3x3.
        strides: specify the strides of the output data in memory; either as a\
            comma-separated list of (signed) integers, or as a template image from\
            which the strides shall be extracted and used. The actual strides\
            produced will depend on whether the output image format can support it.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "mrtrix/maskfilter",
        "largest": largest,
        "connectivity": connectivity,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "input": input_,
        "filter": filter_,
        "output": output,
    }
    if scale is not None:
        params["scale"] = scale
    if axes is not None:
        params["axes"] = axes
    if npass is not None:
        params["npass"] = npass
    if extent is not None:
        params["extent"] = extent
    if strides is not None:
        params["strides"] = strides
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def maskfilter_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MaskfilterParamsDict` 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("scale", None) is not None:
        if not isinstance(params["scale"], int):
            raise StyxValidationError(f'`scale` has the wrong type: Received `{type(params.get("scale", None))}` expected `int | None`')
    if params.get("axes", None) is not None:
        if not isinstance(params["axes"], list):
            raise StyxValidationError(f'`axes` has the wrong type: Received `{type(params.get("axes", None))}` expected `list[int] | None`')
        for e in params["axes"]:
            if not isinstance(e, int):
                raise StyxValidationError(f'`axes` has the wrong type: Received `{type(params.get("axes", None))}` expected `list[int] | None`')
    if params.get("largest", False) is None:
        raise StyxValidationError("`largest` must not be None")
    if not isinstance(params["largest"], bool):
        raise StyxValidationError(f'`largest` has the wrong type: Received `{type(params.get("largest", False))}` expected `bool`')
    if params.get("connectivity", False) is None:
        raise StyxValidationError("`connectivity` must not be None")
    if not isinstance(params["connectivity"], bool):
        raise StyxValidationError(f'`connectivity` has the wrong type: Received `{type(params.get("connectivity", False))}` expected `bool`')
    if params.get("npass", None) is not None:
        if not isinstance(params["npass"], int):
            raise StyxValidationError(f'`npass` has the wrong type: Received `{type(params.get("npass", None))}` expected `int | None`')
    if params.get("extent", None) is not None:
        if not isinstance(params["extent"], list):
            raise StyxValidationError(f'`extent` has the wrong type: Received `{type(params.get("extent", None))}` expected `list[int] | None`')
        for e in params["extent"]:
            if not isinstance(e, int):
                raise StyxValidationError(f'`extent` has the wrong type: Received `{type(params.get("extent", None))}` expected `list[int] | None`')
    if params.get("strides", None) is not None:
        if not isinstance(params["strides"], dict):
            raise StyxValidationError(f'Params object has the wrong type \'{type(params["strides"])}\'')
        if "@type" not in params["strides"]:
            raise StyxValidationError("Params object is missing `@type`")
        if params["strides"]["@type"] not in ["VariousString", "VariousFile"]:
            raise StyxValidationError("Parameter `strides`s `@type` must be one of [\"VariousString\", \"VariousFile\"]")
        maskfilter_strides_validate_dyn_fn(params["strides"]["@type"])(params["strides"])
    if params.get("info", False) is None:
        raise StyxValidationError("`info` must not be None")
    if not isinstance(params["info"], bool):
        raise StyxValidationError(f'`info` has the wrong type: Received `{type(params.get("info", False))}` expected `bool`')
    if params.get("quiet", False) is None:
        raise StyxValidationError("`quiet` must not be None")
    if not isinstance(params["quiet"], bool):
        raise StyxValidationError(f'`quiet` has the wrong type: Received `{type(params.get("quiet", False))}` expected `bool`')
    if params.get("debug", False) is None:
        raise StyxValidationError("`debug` must not be None")
    if not isinstance(params["debug"], bool):
        raise StyxValidationError(f'`debug` has the wrong type: Received `{type(params.get("debug", False))}` expected `bool`')
    if params.get("force", False) is None:
        raise StyxValidationError("`force` must not be None")
    if not isinstance(params["force"], bool):
        raise StyxValidationError(f'`force` has the wrong type: Received `{type(params.get("force", False))}` expected `bool`')
    if params.get("nthreads", None) is not None:
        if not isinstance(params["nthreads"], int):
            raise StyxValidationError(f'`nthreads` has the wrong type: Received `{type(params.get("nthreads", None))}` expected `int | None`')
    if params.get("config", None) is not None:
        if not isinstance(params["config"], list):
            raise StyxValidationError(f'`config` has the wrong type: Received `{type(params.get("config", None))}` expected `list[MaskfilterConfigParamsDict] | None`')
        for e in params["config"]:
            maskfilter_config_validate(e)
    if params.get("help", False) is None:
        raise StyxValidationError("`help` must not be None")
    if not isinstance(params["help"], bool):
        raise StyxValidationError(f'`help` has the wrong type: Received `{type(params.get("help", False))}` expected `bool`')
    if params.get("version", False) is None:
        raise StyxValidationError("`version` must not be None")
    if not isinstance(params["version"], bool):
        raise StyxValidationError(f'`version` has the wrong type: Received `{type(params.get("version", False))}` expected `bool`')
    if params.get("input", None) is None:
        raise StyxValidationError("`input` must not be None")
    if not isinstance(params["input"], (pathlib.Path, str)):
        raise StyxValidationError(f'`input` has the wrong type: Received `{type(params.get("input", None))}` expected `InputPathType`')
    if params.get("filter", None) is None:
        raise StyxValidationError("`filter` must not be None")
    if not isinstance(params["filter"], str):
        raise StyxValidationError(f'`filter` has the wrong type: Received `{type(params.get("filter", None))}` expected `str`')
    if params.get("output", None) is None:
        raise StyxValidationError("`output` must not be None")
    if not isinstance(params["output"], str):
        raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `str`')


def maskfilter_cargs(
    params: MaskfilterParamsDict,
    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.append("maskfilter")
    if params.get("scale", None) is not None:
        cargs.extend([
            "-scale",
            str(params.get("scale", None))
        ])
    if params.get("axes", None) is not None:
        cargs.extend([
            "-axes",
            ",".join(map(str, params.get("axes", None)))
        ])
    if params.get("largest", False):
        cargs.append("-largest")
    if params.get("connectivity", False):
        cargs.append("-connectivity")
    if params.get("npass", None) is not None:
        cargs.extend([
            "-npass",
            str(params.get("npass", None))
        ])
    if params.get("extent", None) is not None:
        cargs.extend([
            "-extent",
            ",".join(map(str, params.get("extent", None)))
        ])
    if params.get("strides", None) is not None:
        cargs.extend([
            "-strides",
            *maskfilter_strides_cargs_dyn_fn(params.get("strides", None)["@type"])(params.get("strides", None), execution)
        ])
    if params.get("info", False):
        cargs.append("-info")
    if params.get("quiet", False):
        cargs.append("-quiet")
    if params.get("debug", False):
        cargs.append("-debug")
    if params.get("force", False):
        cargs.append("-force")
    if params.get("nthreads", None) is not None:
        cargs.extend([
            "-nthreads",
            str(params.get("nthreads", None))
        ])
    if params.get("config", None) is not None:
        cargs.extend([a for c in [maskfilter_config_cargs(s, execution) for s in params.get("config", None)] for a in c])
    if params.get("help", False):
        cargs.append("-help")
    if params.get("version", False):
        cargs.append("-version")
    cargs.append(execution.input_file(params.get("input", None)))
    cargs.append(params.get("filter", None))
    cargs.append(params.get("output", None))
    return cargs


def maskfilter_outputs(
    params: MaskfilterParamsDict,
    execution: Execution,
) -> MaskfilterOutputs:
    """
    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 = MaskfilterOutputs(
        root=execution.output_file("."),
        output=execution.output_file(params.get("output", None)),
    )
    return ret


def maskfilter_execute(
    params: MaskfilterParamsDict,
    runner: Runner | None = None,
) -> MaskfilterOutputs:
    """
    maskfilter
    
    Perform filtering operations on 3D / 4D mask images.
    
    The available filters are: clean, connect, dilate, erode, median.
    
    Each filter has its own unique set of optional parameters.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MaskfilterOutputs`).
    """
    maskfilter_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(MASKFILTER_METADATA)
    params = execution.params(params)
    cargs = maskfilter_cargs(params, execution)
    ret = maskfilter_outputs(params, execution)
    execution.run(cargs)
    return ret


def maskfilter(
    input_: InputPathType,
    filter_: str,
    output: str,
    scale: int | None = None,
    axes: list[int] | None = None,
    largest: bool = False,
    connectivity: bool = False,
    npass: int | None = None,
    extent: list[int] | None = None,
    strides: typing.Union[MaskfilterVariousStringParamsDictTagged, MaskfilterVariousFileParamsDictTagged] | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MaskfilterConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> MaskfilterOutputs:
    """
    maskfilter
    
    Perform filtering operations on 3D / 4D mask images.
    
    The available filters are: clean, connect, dilate, erode, median.
    
    Each filter has its own unique set of optional parameters.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        input_: the input image.
        filter_: the type of filter to be applied (clean, connect, dilate,\
            erode, median).
        output: the output image.
        scale: the maximum scale used to cut bridges. A certain maximum scale\
            cuts bridges up to a width (in voxels) of 2x the provided scale.\
            (Default: 2).
        axes: specify which axes should be included in the connected\
            components. By default only the first 3 axes are included. The axes\
            should be provided as a comma-separated list of values.
        largest: only retain the largest connected component.
        connectivity: use 26-voxel-neighbourhood connectivity (Default: 6).
        npass: the number of times to repeatedly apply the filter.
        extent: specify the extent (width) of kernel size in voxels. This can\
            be specified either as a single value to be used for all axes, or as a\
            comma-separated list of the extent for each axis. The default is 3x3x3.
        strides: specify the strides of the output data in memory; either as a\
            comma-separated list of (signed) integers, or as a template image from\
            which the strides shall be extracted and used. The actual strides\
            produced will depend on whether the output image format can support it.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MaskfilterOutputs`).
    """
    params = maskfilter_params(
        scale=scale,
        axes=axes,
        largest=largest,
        connectivity=connectivity,
        npass=npass,
        extent=extent,
        strides=strides,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        input_=input_,
        filter_=filter_,
        output=output,
    )
    return maskfilter_execute(params, runner)


__all__ = [
    "MASKFILTER_METADATA",
    "MaskfilterConfigParamsDict",
    "MaskfilterConfigParamsDictTagged",
    "MaskfilterOutputs",
    "MaskfilterParamsDict",
    "MaskfilterParamsDictTagged",
    "MaskfilterVariousFileParamsDict",
    "MaskfilterVariousFileParamsDictTagged",
    "MaskfilterVariousStringParamsDict",
    "MaskfilterVariousStringParamsDictTagged",
    "maskfilter",
    "maskfilter_config",
    "maskfilter_execute",
    "maskfilter_params",
    "maskfilter_various_file",
    "maskfilter_various_string",
]
