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

import typing
import pathlib
from styxdefs import *

MRSTATS_METADATA = Metadata(
    id="28fb063299fa07970bfa62d606e13e87fd3bc94e.boutiques",
    name="mrstats",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_MrstatsOutputParamsDictNoTag = typing.TypedDict('_MrstatsOutputParamsDictNoTag', {
    "field": str,
})
MrstatsOutputParamsDictTagged = typing.TypedDict('MrstatsOutputParamsDictTagged', {
    "@type": typing.Literal["output"],
    "field": str,
})
MrstatsOutputParamsDict = _MrstatsOutputParamsDictNoTag | MrstatsOutputParamsDictTagged


_MrstatsConfigParamsDictNoTag = typing.TypedDict('_MrstatsConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
MrstatsConfigParamsDictTagged = typing.TypedDict('MrstatsConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
MrstatsConfigParamsDict = _MrstatsConfigParamsDictNoTag | MrstatsConfigParamsDictTagged


_MrstatsParamsDictNoTag = typing.TypedDict('_MrstatsParamsDictNoTag', {
    "output": typing.NotRequired[list[MrstatsOutputParamsDict] | None],
    "mask": typing.NotRequired[InputPathType | None],
    "ignorezero": bool,
    "allvolumes": bool,
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrstatsConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "image": InputPathType,
})
MrstatsParamsDictTagged = typing.TypedDict('MrstatsParamsDictTagged', {
    "@type": typing.Literal["mrtrix/mrstats"],
    "output": typing.NotRequired[list[MrstatsOutputParamsDict] | None],
    "mask": typing.NotRequired[InputPathType | None],
    "ignorezero": bool,
    "allvolumes": bool,
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrstatsConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "image": InputPathType,
})
MrstatsParamsDict = _MrstatsParamsDictNoTag | MrstatsParamsDictTagged


def mrstats_output(
    field: str,
) -> MrstatsOutputParamsDictTagged:
    """
    Build parameters.
    
    Args:
        field: output only the field specified. Multiple such options can be\
            supplied if required. Choices are: mean, median, std, std_rv, min, max,\
            count. Useful for use in scripts. Both std options refer to the\
            unbiased (sample) standard deviation. For complex data, min, max and\
            std are calculated separately for real and imaginary parts, std_rv is\
            based on the real valued variance (equals sqrt of sum of variances of\
            imaginary and real parts).
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "output",
        "field": field,
    }
    return params


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


def mrstats_output_cargs(
    params: MrstatsOutputParamsDict,
    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("-output")
    cargs.append(params.get("field", None))
    return cargs


def mrstats_config(
    key: str,
    value: str,
) -> MrstatsConfigParamsDictTagged:
    """
    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 mrstats_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrstatsConfigParamsDict` 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 mrstats_config_cargs(
    params: MrstatsConfigParamsDict,
    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 MrstatsOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MrstatsParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def mrstats_params(
    image: InputPathType,
    output: list[MrstatsOutputParamsDict] | None = None,
    mask: InputPathType | None = None,
    ignorezero: bool = False,
    allvolumes: bool = False,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrstatsConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> MrstatsParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image: the input image from which statistics will be computed.
        output: output only the field specified. Multiple such options can be\
            supplied if required. Choices are: mean, median, std, std_rv, min, max,\
            count. Useful for use in scripts. Both std options refer to the\
            unbiased (sample) standard deviation. For complex data, min, max and\
            std are calculated separately for real and imaginary parts, std_rv is\
            based on the real valued variance (equals sqrt of sum of variances of\
            imaginary and real parts).
        mask: only perform computation within the specified binary mask image.
        ignorezero: ignore zero values during statistics calculation.
        allvolumes: generate statistics across all image volumes, rather than\
            one set of statistics per image volume.
        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/mrstats",
        "ignorezero": ignorezero,
        "allvolumes": allvolumes,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "image": image,
    }
    if output is not None:
        params["output"] = output
    if mask is not None:
        params["mask"] = mask
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def mrstats_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrstatsParamsDict` 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("output", None) is not None:
        if not isinstance(params["output"], list):
            raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `list[MrstatsOutputParamsDict] | None`')
        for e in params["output"]:
            mrstats_output_validate(e)
    if params.get("mask", None) is not None:
        if not isinstance(params["mask"], (pathlib.Path, str)):
            raise StyxValidationError(f'`mask` has the wrong type: Received `{type(params.get("mask", None))}` expected `InputPathType | None`')
    if params.get("ignorezero", False) is None:
        raise StyxValidationError("`ignorezero` must not be None")
    if not isinstance(params["ignorezero"], bool):
        raise StyxValidationError(f'`ignorezero` has the wrong type: Received `{type(params.get("ignorezero", False))}` expected `bool`')
    if params.get("allvolumes", False) is None:
        raise StyxValidationError("`allvolumes` must not be None")
    if not isinstance(params["allvolumes"], bool):
        raise StyxValidationError(f'`allvolumes` has the wrong type: Received `{type(params.get("allvolumes", 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[MrstatsConfigParamsDict] | None`')
        for e in params["config"]:
            mrstats_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("image", None) is None:
        raise StyxValidationError("`image` must not be None")
    if not isinstance(params["image"], (pathlib.Path, str)):
        raise StyxValidationError(f'`image` has the wrong type: Received `{type(params.get("image", None))}` expected `InputPathType`')


def mrstats_cargs(
    params: MrstatsParamsDict,
    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("mrstats")
    if params.get("output", None) is not None:
        cargs.extend([a for c in [mrstats_output_cargs(s, execution) for s in params.get("output", None)] for a in c])
    if params.get("mask", None) is not None:
        cargs.extend([
            "-mask",
            execution.input_file(params.get("mask", None))
        ])
    if params.get("ignorezero", False):
        cargs.append("-ignorezero")
    if params.get("allvolumes", False):
        cargs.append("-allvolumes")
    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 [mrstats_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("image", None)))
    return cargs


def mrstats_outputs(
    params: MrstatsParamsDict,
    execution: Execution,
) -> MrstatsOutputs:
    """
    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 = MrstatsOutputs(
        root=execution.output_file("."),
    )
    return ret


def mrstats_execute(
    params: MrstatsParamsDict,
    runner: Runner | None = None,
) -> MrstatsOutputs:
    """
    mrstats
    
    Compute images statistics.
    
    
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MrstatsOutputs`).
    """
    mrstats_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(MRSTATS_METADATA)
    params = execution.params(params)
    cargs = mrstats_cargs(params, execution)
    ret = mrstats_outputs(params, execution)
    execution.run(cargs)
    return ret


def mrstats(
    image: InputPathType,
    output: list[MrstatsOutputParamsDict] | None = None,
    mask: InputPathType | None = None,
    ignorezero: bool = False,
    allvolumes: bool = False,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrstatsConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> MrstatsOutputs:
    """
    mrstats
    
    Compute images statistics.
    
    
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        image: the input image from which statistics will be computed.
        output: output only the field specified. Multiple such options can be\
            supplied if required. Choices are: mean, median, std, std_rv, min, max,\
            count. Useful for use in scripts. Both std options refer to the\
            unbiased (sample) standard deviation. For complex data, min, max and\
            std are calculated separately for real and imaginary parts, std_rv is\
            based on the real valued variance (equals sqrt of sum of variances of\
            imaginary and real parts).
        mask: only perform computation within the specified binary mask image.
        ignorezero: ignore zero values during statistics calculation.
        allvolumes: generate statistics across all image volumes, rather than\
            one set of statistics per image volume.
        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 `MrstatsOutputs`).
    """
    params = mrstats_params(
        output=output,
        mask=mask,
        ignorezero=ignorezero,
        allvolumes=allvolumes,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        image=image,
    )
    return mrstats_execute(params, runner)


__all__ = [
    "MRSTATS_METADATA",
    "MrstatsConfigParamsDict",
    "MrstatsConfigParamsDictTagged",
    "MrstatsOutputParamsDict",
    "MrstatsOutputParamsDictTagged",
    "MrstatsOutputs",
    "MrstatsParamsDict",
    "MrstatsParamsDictTagged",
    "mrstats",
    "mrstats_config",
    "mrstats_execute",
    "mrstats_output",
    "mrstats_params",
]
