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

import typing
import pathlib
from styxdefs import *

WARPCONVERT_METADATA = Metadata(
    id="7468f4768b6e008e092ad5508a8d2b4875bab30a.boutiques",
    name="warpconvert",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_WarpconvertConfigParamsDictNoTag = typing.TypedDict('_WarpconvertConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
WarpconvertConfigParamsDictTagged = typing.TypedDict('WarpconvertConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
WarpconvertConfigParamsDict = _WarpconvertConfigParamsDictNoTag | WarpconvertConfigParamsDictTagged


_WarpconvertParamsDictNoTag = typing.TypedDict('_WarpconvertParamsDictNoTag', {
    "template": typing.NotRequired[InputPathType | None],
    "midway_space": bool,
    "from": typing.NotRequired[int | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[WarpconvertConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "in": InputPathType,
    "type": str,
    "out": str,
})
WarpconvertParamsDictTagged = typing.TypedDict('WarpconvertParamsDictTagged', {
    "@type": typing.Literal["mrtrix/warpconvert"],
    "template": typing.NotRequired[InputPathType | None],
    "midway_space": bool,
    "from": typing.NotRequired[int | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[WarpconvertConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "in": InputPathType,
    "type": str,
    "out": str,
})
WarpconvertParamsDict = _WarpconvertParamsDictNoTag | WarpconvertParamsDictTagged


def warpconvert_config(
    key: str,
    value: str,
) -> WarpconvertConfigParamsDictTagged:
    """
    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 warpconvert_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `WarpconvertConfigParamsDict` 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 warpconvert_config_cargs(
    params: WarpconvertConfigParamsDict,
    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 WarpconvertOutputs(typing.NamedTuple):
    """
    Output object returned when calling `WarpconvertParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    out: OutputPathType
    """the output warp image."""


def warpconvert_params(
    in_: InputPathType,
    type_: str,
    out: str,
    template: InputPathType | None = None,
    midway_space: bool = False,
    from_: int | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[WarpconvertConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> WarpconvertParamsDictTagged:
    """
    Build parameters.
    
    Args:
        in_: the input warp image.
        type_: the conversion type required. Valid choices are:\
            deformation2displacement, displacement2deformation,\
            warpfull2deformation, warpfull2displacement.
        out: the output warp image.
        template: define a template image when converting a warpfull file\
            (which is defined on a grid in the midway space between image 1 & 2).\
            For example to generate the deformation field that maps image1 to\
            image2, then supply image2 as the template image.
        midway_space: to be used only with warpfull2deformation and\
            warpfull2displacement conversion types. The output will only contain\
            the non-linear warp to map an input image to the midway space (defined\
            by the warpfull grid). If a linear transform exists in the warpfull\
            file header then it will be composed and included in the output.
        from_: to be used only with warpfull2deformation and\
            warpfull2displacement conversion types. Used to define the direction of\
            the desired output field.Use -from 1 to obtain the image1->image2 field\
            and from 2 for image2->image1. Can be used in combination with the\
            -midway_space option to produce a field that only maps to midway space.
        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/warpconvert",
        "midway_space": midway_space,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "in": in_,
        "type": type_,
        "out": out,
    }
    if template is not None:
        params["template"] = template
    if from_ is not None:
        params["from"] = from_
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def warpconvert_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `WarpconvertParamsDict` 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("template", None) is not None:
        if not isinstance(params["template"], (pathlib.Path, str)):
            raise StyxValidationError(f'`template` has the wrong type: Received `{type(params.get("template", None))}` expected `InputPathType | None`')
    if params.get("midway_space", False) is None:
        raise StyxValidationError("`midway_space` must not be None")
    if not isinstance(params["midway_space"], bool):
        raise StyxValidationError(f'`midway_space` has the wrong type: Received `{type(params.get("midway_space", False))}` expected `bool`')
    if params.get("from", None) is not None:
        if not isinstance(params["from"], int):
            raise StyxValidationError(f'`from` has the wrong type: Received `{type(params.get("from", None))}` expected `int | None`')
    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[WarpconvertConfigParamsDict] | None`')
        for e in params["config"]:
            warpconvert_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("in", None) is None:
        raise StyxValidationError("`in` must not be None")
    if not isinstance(params["in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`in` has the wrong type: Received `{type(params.get("in", None))}` expected `InputPathType`')
    if params.get("type", None) is None:
        raise StyxValidationError("`type` must not be None")
    if not isinstance(params["type"], str):
        raise StyxValidationError(f'`type` has the wrong type: Received `{type(params.get("type", None))}` expected `str`')
    if params.get("out", None) is None:
        raise StyxValidationError("`out` must not be None")
    if not isinstance(params["out"], str):
        raise StyxValidationError(f'`out` has the wrong type: Received `{type(params.get("out", None))}` expected `str`')


def warpconvert_cargs(
    params: WarpconvertParamsDict,
    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("warpconvert")
    if params.get("template", None) is not None:
        cargs.extend([
            "-template",
            execution.input_file(params.get("template", None))
        ])
    if params.get("midway_space", False):
        cargs.append("-midway_space")
    if params.get("from", None) is not None:
        cargs.extend([
            "-from",
            str(params.get("from", None))
        ])
    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 [warpconvert_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("in", None)))
    cargs.append(params.get("type", None))
    cargs.append(params.get("out", None))
    return cargs


def warpconvert_outputs(
    params: WarpconvertParamsDict,
    execution: Execution,
) -> WarpconvertOutputs:
    """
    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 = WarpconvertOutputs(
        root=execution.output_file("."),
        out=execution.output_file(params.get("out", None)),
    )
    return ret


def warpconvert_execute(
    params: WarpconvertParamsDict,
    runner: Runner | None = None,
) -> WarpconvertOutputs:
    """
    warpconvert
    
    Convert between different representations of a non-linear warp.
    
    A deformation field is defined as an image where each voxel defines the
    corresponding position in the other image (in scanner space coordinates). A
    displacement field stores the displacements (in mm) to the other image from
    the each voxel's position (in scanner space). The warpfull file is the 5D
    format output from mrregister -nl_warp_full, which contains linear
    transforms, warps and their inverses that map each image to a midway space.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `WarpconvertOutputs`).
    """
    warpconvert_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(WARPCONVERT_METADATA)
    params = execution.params(params)
    cargs = warpconvert_cargs(params, execution)
    ret = warpconvert_outputs(params, execution)
    execution.run(cargs)
    return ret


def warpconvert(
    in_: InputPathType,
    type_: str,
    out: str,
    template: InputPathType | None = None,
    midway_space: bool = False,
    from_: int | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[WarpconvertConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> WarpconvertOutputs:
    """
    warpconvert
    
    Convert between different representations of a non-linear warp.
    
    A deformation field is defined as an image where each voxel defines the
    corresponding position in the other image (in scanner space coordinates). A
    displacement field stores the displacements (in mm) to the other image from
    the each voxel's position (in scanner space). The warpfull file is the 5D
    format output from mrregister -nl_warp_full, which contains linear
    transforms, warps and their inverses that map each image to a midway space.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        in_: the input warp image.
        type_: the conversion type required. Valid choices are:\
            deformation2displacement, displacement2deformation,\
            warpfull2deformation, warpfull2displacement.
        out: the output warp image.
        template: define a template image when converting a warpfull file\
            (which is defined on a grid in the midway space between image 1 & 2).\
            For example to generate the deformation field that maps image1 to\
            image2, then supply image2 as the template image.
        midway_space: to be used only with warpfull2deformation and\
            warpfull2displacement conversion types. The output will only contain\
            the non-linear warp to map an input image to the midway space (defined\
            by the warpfull grid). If a linear transform exists in the warpfull\
            file header then it will be composed and included in the output.
        from_: to be used only with warpfull2deformation and\
            warpfull2displacement conversion types. Used to define the direction of\
            the desired output field.Use -from 1 to obtain the image1->image2 field\
            and from 2 for image2->image1. Can be used in combination with the\
            -midway_space option to produce a field that only maps to midway space.
        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 `WarpconvertOutputs`).
    """
    params = warpconvert_params(
        template=template,
        midway_space=midway_space,
        from_=from_,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        in_=in_,
        type_=type_,
        out=out,
    )
    return warpconvert_execute(params, runner)


__all__ = [
    "WARPCONVERT_METADATA",
    "WarpconvertConfigParamsDict",
    "WarpconvertConfigParamsDictTagged",
    "WarpconvertOutputs",
    "WarpconvertParamsDict",
    "WarpconvertParamsDictTagged",
    "warpconvert",
    "warpconvert_config",
    "warpconvert_execute",
    "warpconvert_params",
]
