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

import typing
import pathlib
from styxdefs import *

MRMETRIC_METADATA = Metadata(
    id="900f502c877db64aba337cc23f205ccc871706a3.boutiques",
    name="mrmetric",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_MrmetricConfigParamsDictNoTag = typing.TypedDict('_MrmetricConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
MrmetricConfigParamsDictTagged = typing.TypedDict('MrmetricConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
MrmetricConfigParamsDict = _MrmetricConfigParamsDictNoTag | MrmetricConfigParamsDictTagged


_MrmetricParamsDictNoTag = typing.TypedDict('_MrmetricParamsDictNoTag', {
    "space": typing.NotRequired[str | None],
    "interp": typing.NotRequired[str | None],
    "metric": typing.NotRequired[str | None],
    "mask1": typing.NotRequired[InputPathType | None],
    "mask2": typing.NotRequired[InputPathType | None],
    "nonormalisation": bool,
    "overlap": bool,
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrmetricConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "image1": InputPathType,
    "image2": InputPathType,
})
MrmetricParamsDictTagged = typing.TypedDict('MrmetricParamsDictTagged', {
    "@type": typing.Literal["mrtrix/mrmetric"],
    "space": typing.NotRequired[str | None],
    "interp": typing.NotRequired[str | None],
    "metric": typing.NotRequired[str | None],
    "mask1": typing.NotRequired[InputPathType | None],
    "mask2": typing.NotRequired[InputPathType | None],
    "nonormalisation": bool,
    "overlap": bool,
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrmetricConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "image1": InputPathType,
    "image2": InputPathType,
})
MrmetricParamsDict = _MrmetricParamsDictNoTag | MrmetricParamsDictTagged


def mrmetric_config(
    key: str,
    value: str,
) -> MrmetricConfigParamsDictTagged:
    """
    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 mrmetric_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrmetricConfigParamsDict` 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 mrmetric_config_cargs(
    params: MrmetricConfigParamsDict,
    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 MrmetricOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MrmetricParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def mrmetric_params(
    image1: InputPathType,
    image2: InputPathType,
    space: str | None = None,
    interp: str | None = None,
    metric: str | None = None,
    mask1: InputPathType | None = None,
    mask2: InputPathType | None = None,
    nonormalisation: bool = False,
    overlap: bool = False,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrmetricConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> MrmetricParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image1: the first input image.
        image2: the second input image.
        space: voxel (default): per voxel image1: scanner space of image 1\
            image2: scanner space of image 2 average: scanner space of the average\
            affine transformation of image 1 and 2.
        interp: set the interpolation method to use when reslicing (choices:\
            nearest, linear, cubic, sinc. Default: linear).
        metric: define the dissimilarity metric used to calculate the cost.\
            Choices: diff (squared differences), cc (non-normalised negative cross\
            correlation aka negative cross covariance). Default: diff). cc is only\
            implemented for -space average and -interp linear and cubic.
        mask1: mask for image 1.
        mask2: mask for image 2.
        nonormalisation: do not normalise the dissimilarity metric to the\
            number of voxels.
        overlap: output number of voxels that were used.
        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/mrmetric",
        "nonormalisation": nonormalisation,
        "overlap": overlap,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "image1": image1,
        "image2": image2,
    }
    if space is not None:
        params["space"] = space
    if interp is not None:
        params["interp"] = interp
    if metric is not None:
        params["metric"] = metric
    if mask1 is not None:
        params["mask1"] = mask1
    if mask2 is not None:
        params["mask2"] = mask2
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def mrmetric_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrmetricParamsDict` 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("space", None) is not None:
        if not isinstance(params["space"], str):
            raise StyxValidationError(f'`space` has the wrong type: Received `{type(params.get("space", None))}` expected `str | None`')
    if params.get("interp", None) is not None:
        if not isinstance(params["interp"], str):
            raise StyxValidationError(f'`interp` has the wrong type: Received `{type(params.get("interp", None))}` expected `str | None`')
    if params.get("metric", None) is not None:
        if not isinstance(params["metric"], str):
            raise StyxValidationError(f'`metric` has the wrong type: Received `{type(params.get("metric", None))}` expected `str | None`')
    if params.get("mask1", None) is not None:
        if not isinstance(params["mask1"], (pathlib.Path, str)):
            raise StyxValidationError(f'`mask1` has the wrong type: Received `{type(params.get("mask1", None))}` expected `InputPathType | None`')
    if params.get("mask2", None) is not None:
        if not isinstance(params["mask2"], (pathlib.Path, str)):
            raise StyxValidationError(f'`mask2` has the wrong type: Received `{type(params.get("mask2", None))}` expected `InputPathType | None`')
    if params.get("nonormalisation", False) is None:
        raise StyxValidationError("`nonormalisation` must not be None")
    if not isinstance(params["nonormalisation"], bool):
        raise StyxValidationError(f'`nonormalisation` has the wrong type: Received `{type(params.get("nonormalisation", False))}` expected `bool`')
    if params.get("overlap", False) is None:
        raise StyxValidationError("`overlap` must not be None")
    if not isinstance(params["overlap"], bool):
        raise StyxValidationError(f'`overlap` has the wrong type: Received `{type(params.get("overlap", False))}` expected `bool`')
    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[MrmetricConfigParamsDict] | None`')
        for e in params["config"]:
            mrmetric_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("image1", None) is None:
        raise StyxValidationError("`image1` must not be None")
    if not isinstance(params["image1"], (pathlib.Path, str)):
        raise StyxValidationError(f'`image1` has the wrong type: Received `{type(params.get("image1", None))}` expected `InputPathType`')
    if params.get("image2", None) is None:
        raise StyxValidationError("`image2` must not be None")
    if not isinstance(params["image2"], (pathlib.Path, str)):
        raise StyxValidationError(f'`image2` has the wrong type: Received `{type(params.get("image2", None))}` expected `InputPathType`')


def mrmetric_cargs(
    params: MrmetricParamsDict,
    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("mrmetric")
    if params.get("space", None) is not None:
        cargs.extend([
            "-space",
            params.get("space", None)
        ])
    if params.get("interp", None) is not None:
        cargs.extend([
            "-interp",
            params.get("interp", None)
        ])
    if params.get("metric", None) is not None:
        cargs.extend([
            "-metric",
            params.get("metric", None)
        ])
    if params.get("mask1", None) is not None:
        cargs.extend([
            "-mask1",
            execution.input_file(params.get("mask1", None))
        ])
    if params.get("mask2", None) is not None:
        cargs.extend([
            "-mask2",
            execution.input_file(params.get("mask2", None))
        ])
    if params.get("nonormalisation", False):
        cargs.append("-nonormalisation")
    if params.get("overlap", False):
        cargs.append("-overlap")
    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 [mrmetric_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("image1", None)))
    cargs.append(execution.input_file(params.get("image2", None)))
    return cargs


def mrmetric_outputs(
    params: MrmetricParamsDict,
    execution: Execution,
) -> MrmetricOutputs:
    """
    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 = MrmetricOutputs(
        root=execution.output_file("."),
    )
    return ret


def mrmetric_execute(
    params: MrmetricParamsDict,
    runner: Runner | None = None,
) -> MrmetricOutputs:
    """
    mrmetric
    
    Computes a dissimilarity metric between two images.
    
    Currently only the mean squared difference is fully implemented.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MrmetricOutputs`).
    """
    mrmetric_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(MRMETRIC_METADATA)
    params = execution.params(params)
    cargs = mrmetric_cargs(params, execution)
    ret = mrmetric_outputs(params, execution)
    execution.run(cargs)
    return ret


def mrmetric(
    image1: InputPathType,
    image2: InputPathType,
    space: str | None = None,
    interp: str | None = None,
    metric: str | None = None,
    mask1: InputPathType | None = None,
    mask2: InputPathType | None = None,
    nonormalisation: bool = False,
    overlap: bool = False,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrmetricConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> MrmetricOutputs:
    """
    mrmetric
    
    Computes a dissimilarity metric between two images.
    
    Currently only the mean squared difference is fully implemented.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        image1: the first input image.
        image2: the second input image.
        space: voxel (default): per voxel image1: scanner space of image 1\
            image2: scanner space of image 2 average: scanner space of the average\
            affine transformation of image 1 and 2.
        interp: set the interpolation method to use when reslicing (choices:\
            nearest, linear, cubic, sinc. Default: linear).
        metric: define the dissimilarity metric used to calculate the cost.\
            Choices: diff (squared differences), cc (non-normalised negative cross\
            correlation aka negative cross covariance). Default: diff). cc is only\
            implemented for -space average and -interp linear and cubic.
        mask1: mask for image 1.
        mask2: mask for image 2.
        nonormalisation: do not normalise the dissimilarity metric to the\
            number of voxels.
        overlap: output number of voxels that were used.
        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 `MrmetricOutputs`).
    """
    params = mrmetric_params(
        space=space,
        interp=interp,
        metric=metric,
        mask1=mask1,
        mask2=mask2,
        nonormalisation=nonormalisation,
        overlap=overlap,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        image1=image1,
        image2=image2,
    )
    return mrmetric_execute(params, runner)


__all__ = [
    "MRMETRIC_METADATA",
    "MrmetricConfigParamsDict",
    "MrmetricConfigParamsDictTagged",
    "MrmetricOutputs",
    "MrmetricParamsDict",
    "MrmetricParamsDictTagged",
    "mrmetric",
    "mrmetric_config",
    "mrmetric_execute",
    "mrmetric_params",
]
