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

import typing
import pathlib
from styxdefs import *

MRMATH_METADATA = Metadata(
    id="53ebb217f19a2e0a5175c77890d64115b390d7bd.boutiques",
    name="mrmath",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_MrmathConfigParamsDictNoTag = typing.TypedDict('_MrmathConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
MrmathConfigParamsDictTagged = typing.TypedDict('MrmathConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
MrmathConfigParamsDict = _MrmathConfigParamsDictNoTag | MrmathConfigParamsDictTagged


_MrmathParamsDictNoTag = typing.TypedDict('_MrmathParamsDictNoTag', {
    "axis": typing.NotRequired[int | None],
    "keep_unary_axes": bool,
    "datatype": typing.NotRequired[str | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrmathConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "input": list[InputPathType],
    "operation": str,
    "output": str,
})
MrmathParamsDictTagged = typing.TypedDict('MrmathParamsDictTagged', {
    "@type": typing.Literal["mrtrix/mrmath"],
    "axis": typing.NotRequired[int | None],
    "keep_unary_axes": bool,
    "datatype": typing.NotRequired[str | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrmathConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "input": list[InputPathType],
    "operation": str,
    "output": str,
})
MrmathParamsDict = _MrmathParamsDictNoTag | MrmathParamsDictTagged


def mrmath_config(
    key: str,
    value: str,
) -> MrmathConfigParamsDictTagged:
    """
    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 mrmath_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrmathConfigParamsDict` 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 mrmath_config_cargs(
    params: MrmathConfigParamsDict,
    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 MrmathOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MrmathParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output: OutputPathType
    """the output image."""


def mrmath_params(
    input_: list[InputPathType],
    operation: str,
    output: str,
    axis: int | None = None,
    keep_unary_axes: bool = False,
    datatype: str | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrmathConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> MrmathParamsDictTagged:
    """
    Build parameters.
    
    Args:
        input_: the input image(s).
        operation: the operation to apply, one of: mean, median, sum, product,\
            rms, norm, var, std, min, max, absmax, magmax.
        output: the output image.
        axis: perform operation along a specified axis of a single input image.
        keep_unary_axes: Keep unary axes in input images prior to calculating\
            the stats. The default is to wipe axes with single elements.
        datatype: specify output image data type. Valid choices are: float32,\
            float32le, float32be, float64, float64le, float64be, int64, uint64,\
            int64le, uint64le, int64be, uint64be, int32, uint32, int32le, uint32le,\
            int32be, uint32be, int16, uint16, int16le, uint16le, int16be, uint16be,\
            cfloat32, cfloat32le, cfloat32be, cfloat64, cfloat64le, cfloat64be,\
            int8, uint8, bit.
        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/mrmath",
        "keep_unary_axes": keep_unary_axes,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "input": input_,
        "operation": operation,
        "output": output,
    }
    if axis is not None:
        params["axis"] = axis
    if datatype is not None:
        params["datatype"] = datatype
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def mrmath_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrmathParamsDict` 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("axis", None) is not None:
        if not isinstance(params["axis"], int):
            raise StyxValidationError(f'`axis` has the wrong type: Received `{type(params.get("axis", None))}` expected `int | None`')
    if params.get("keep_unary_axes", False) is None:
        raise StyxValidationError("`keep_unary_axes` must not be None")
    if not isinstance(params["keep_unary_axes"], bool):
        raise StyxValidationError(f'`keep_unary_axes` has the wrong type: Received `{type(params.get("keep_unary_axes", False))}` expected `bool`')
    if params.get("datatype", None) is not None:
        if not isinstance(params["datatype"], str):
            raise StyxValidationError(f'`datatype` has the wrong type: Received `{type(params.get("datatype", None))}` expected `str | 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[MrmathConfigParamsDict] | None`')
        for e in params["config"]:
            mrmath_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"], list):
        raise StyxValidationError(f'`input` has the wrong type: Received `{type(params.get("input", None))}` expected `list[InputPathType]`')
    for e in params["input"]:
        if not isinstance(e, (pathlib.Path, str)):
            raise StyxValidationError(f'`input` has the wrong type: Received `{type(params.get("input", None))}` expected `list[InputPathType]`')
    if params.get("operation", None) is None:
        raise StyxValidationError("`operation` must not be None")
    if not isinstance(params["operation"], str):
        raise StyxValidationError(f'`operation` has the wrong type: Received `{type(params.get("operation", 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 mrmath_cargs(
    params: MrmathParamsDict,
    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("mrmath")
    if params.get("axis", None) is not None:
        cargs.extend([
            "-axis",
            str(params.get("axis", None))
        ])
    if params.get("keep_unary_axes", False):
        cargs.append("-keep_unary_axes")
    if params.get("datatype", None) is not None:
        cargs.extend([
            "-datatype",
            params.get("datatype", 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 [mrmath_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.extend([execution.input_file(f) for f in params.get("input", None)])
    cargs.append(params.get("operation", None))
    cargs.append(params.get("output", None))
    return cargs


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


def mrmath_execute(
    params: MrmathParamsDict,
    runner: Runner | None = None,
) -> MrmathOutputs:
    """
    mrmath
    
    Compute summary statistic on image intensities either across images, or
    along a specified axis of a single image.
    
    Supported operations are:
    
    mean, median, sum, product, rms (root-mean-square value), norm (vector
    2-norm), var (unbiased variance), std (unbiased standard deviation), min,
    max, absmax (maximum absolute value), magmax (value with maximum absolute
    value, preserving its sign).
    
    This command is used to traverse either along an image axis, or across a set
    of input images, calculating some statistic from the values along each
    traversal. If you are seeking to instead perform mathematical calculations
    that are done independently for each voxel, pleaase see the 'mrcalc'
    command.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MrmathOutputs`).
    """
    mrmath_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(MRMATH_METADATA)
    params = execution.params(params)
    cargs = mrmath_cargs(params, execution)
    ret = mrmath_outputs(params, execution)
    execution.run(cargs)
    return ret


def mrmath(
    input_: list[InputPathType],
    operation: str,
    output: str,
    axis: int | None = None,
    keep_unary_axes: bool = False,
    datatype: str | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrmathConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> MrmathOutputs:
    """
    mrmath
    
    Compute summary statistic on image intensities either across images, or
    along a specified axis of a single image.
    
    Supported operations are:
    
    mean, median, sum, product, rms (root-mean-square value), norm (vector
    2-norm), var (unbiased variance), std (unbiased standard deviation), min,
    max, absmax (maximum absolute value), magmax (value with maximum absolute
    value, preserving its sign).
    
    This command is used to traverse either along an image axis, or across a set
    of input images, calculating some statistic from the values along each
    traversal. If you are seeking to instead perform mathematical calculations
    that are done independently for each voxel, pleaase see the 'mrcalc'
    command.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        input_: the input image(s).
        operation: the operation to apply, one of: mean, median, sum, product,\
            rms, norm, var, std, min, max, absmax, magmax.
        output: the output image.
        axis: perform operation along a specified axis of a single input image.
        keep_unary_axes: Keep unary axes in input images prior to calculating\
            the stats. The default is to wipe axes with single elements.
        datatype: specify output image data type. Valid choices are: float32,\
            float32le, float32be, float64, float64le, float64be, int64, uint64,\
            int64le, uint64le, int64be, uint64be, int32, uint32, int32le, uint32le,\
            int32be, uint32be, int16, uint16, int16le, uint16le, int16be, uint16be,\
            cfloat32, cfloat32le, cfloat32be, cfloat64, cfloat64le, cfloat64be,\
            int8, uint8, bit.
        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 `MrmathOutputs`).
    """
    params = mrmath_params(
        axis=axis,
        keep_unary_axes=keep_unary_axes,
        datatype=datatype,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        input_=input_,
        operation=operation,
        output=output,
    )
    return mrmath_execute(params, runner)


__all__ = [
    "MRMATH_METADATA",
    "MrmathConfigParamsDict",
    "MrmathConfigParamsDictTagged",
    "MrmathOutputs",
    "MrmathParamsDict",
    "MrmathParamsDictTagged",
    "mrmath",
    "mrmath_config",
    "mrmath_execute",
    "mrmath_params",
]
