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

import typing
import pathlib
from styxdefs import *

TENSOR2METRIC_METADATA = Metadata(
    id="27f4e9c1379c830bcec96cfb3c8b321a7c99c245.boutiques",
    name="tensor2metric",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_Tensor2metricConfigParamsDictNoTag = typing.TypedDict('_Tensor2metricConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
Tensor2metricConfigParamsDictTagged = typing.TypedDict('Tensor2metricConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
Tensor2metricConfigParamsDict = _Tensor2metricConfigParamsDictNoTag | Tensor2metricConfigParamsDictTagged


_Tensor2metricParamsDictNoTag = typing.TypedDict('_Tensor2metricParamsDictNoTag', {
    "adc": typing.NotRequired[str | None],
    "fa": typing.NotRequired[str | None],
    "ad": typing.NotRequired[str | None],
    "rd": typing.NotRequired[str | None],
    "cl": typing.NotRequired[str | None],
    "cp": typing.NotRequired[str | None],
    "cs": typing.NotRequired[str | None],
    "value": typing.NotRequired[str | None],
    "vector": typing.NotRequired[str | None],
    "num": typing.NotRequired[list[int] | None],
    "modulate": typing.NotRequired[str | None],
    "mask": typing.NotRequired[InputPathType | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Tensor2metricConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "tensor": InputPathType,
})
Tensor2metricParamsDictTagged = typing.TypedDict('Tensor2metricParamsDictTagged', {
    "@type": typing.Literal["mrtrix/tensor2metric"],
    "adc": typing.NotRequired[str | None],
    "fa": typing.NotRequired[str | None],
    "ad": typing.NotRequired[str | None],
    "rd": typing.NotRequired[str | None],
    "cl": typing.NotRequired[str | None],
    "cp": typing.NotRequired[str | None],
    "cs": typing.NotRequired[str | None],
    "value": typing.NotRequired[str | None],
    "vector": typing.NotRequired[str | None],
    "num": typing.NotRequired[list[int] | None],
    "modulate": typing.NotRequired[str | None],
    "mask": typing.NotRequired[InputPathType | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Tensor2metricConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "tensor": InputPathType,
})
Tensor2metricParamsDict = _Tensor2metricParamsDictNoTag | Tensor2metricParamsDictTagged


def tensor2metric_config(
    key: str,
    value: str,
) -> Tensor2metricConfigParamsDictTagged:
    """
    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 tensor2metric_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Tensor2metricConfigParamsDict` 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 tensor2metric_config_cargs(
    params: Tensor2metricConfigParamsDict,
    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 Tensor2metricOutputs(typing.NamedTuple):
    """
    Output object returned when calling `Tensor2metricParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    adc: OutputPathType | None
    """compute the mean apparent diffusion coefficient (ADC) of the diffusion
    tensor. (sometimes also referred to as the mean diffusivity (MD)) """
    fa: OutputPathType | None
    """compute the fractional anisotropy (FA) of the diffusion tensor. """
    ad: OutputPathType | None
    """compute the axial diffusivity (AD) of the diffusion tensor. (equivalent
    to the principal eigenvalue) """
    rd: OutputPathType | None
    """compute the radial diffusivity (RD) of the diffusion tensor. (equivalent
    to the mean of the two non-principal eigenvalues) """
    cl: OutputPathType | None
    """compute the linearity metric of the diffusion tensor. (one of the three
    Westin shape metrics) """
    cp: OutputPathType | None
    """compute the planarity metric of the diffusion tensor. (one of the three
    Westin shape metrics) """
    cs: OutputPathType | None
    """compute the sphericity metric of the diffusion tensor. (one of the three
    Westin shape metrics) """
    value: OutputPathType | None
    """compute the selected eigenvalue(s) of the diffusion tensor. """
    vector: OutputPathType | None
    """compute the selected eigenvector(s) of the diffusion tensor. """


def tensor2metric_params(
    tensor: InputPathType,
    adc: str | None = None,
    fa: str | None = None,
    ad: str | None = None,
    rd: str | None = None,
    cl: str | None = None,
    cp: str | None = None,
    cs: str | None = None,
    value: str | None = None,
    vector: str | None = None,
    num: list[int] | None = None,
    modulate: str | None = None,
    mask: InputPathType | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Tensor2metricConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> Tensor2metricParamsDictTagged:
    """
    Build parameters.
    
    Args:
        tensor: the input tensor image.
        adc: compute the mean apparent diffusion coefficient (ADC) of the\
            diffusion tensor. (sometimes also referred to as the mean diffusivity\
            (MD)).
        fa: compute the fractional anisotropy (FA) of the diffusion tensor.
        ad: compute the axial diffusivity (AD) of the diffusion tensor.\
            (equivalent to the principal eigenvalue).
        rd: compute the radial diffusivity (RD) of the diffusion tensor.\
            (equivalent to the mean of the two non-principal eigenvalues).
        cl: compute the linearity metric of the diffusion tensor. (one of the\
            three Westin shape metrics).
        cp: compute the planarity metric of the diffusion tensor. (one of the\
            three Westin shape metrics).
        cs: compute the sphericity metric of the diffusion tensor. (one of the\
            three Westin shape metrics).
        value: compute the selected eigenvalue(s) of the diffusion tensor.
        vector: compute the selected eigenvector(s) of the diffusion tensor.
        num: specify the desired eigenvalue/eigenvector(s). Note that several\
            eigenvalues can be specified as a number sequence. For example, '1,3'\
            specifies the principal (1) and minor (3) eigenvalues/eigenvectors\
            (default = 1).
        modulate: specify how to modulate the magnitude of the eigenvectors.\
            Valid choices are: none, FA, eigval (default = FA).
        mask: only perform computation within the specified binary brain mask\
            image.
        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/tensor2metric",
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "tensor": tensor,
    }
    if adc is not None:
        params["adc"] = adc
    if fa is not None:
        params["fa"] = fa
    if ad is not None:
        params["ad"] = ad
    if rd is not None:
        params["rd"] = rd
    if cl is not None:
        params["cl"] = cl
    if cp is not None:
        params["cp"] = cp
    if cs is not None:
        params["cs"] = cs
    if value is not None:
        params["value"] = value
    if vector is not None:
        params["vector"] = vector
    if num is not None:
        params["num"] = num
    if modulate is not None:
        params["modulate"] = modulate
    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 tensor2metric_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Tensor2metricParamsDict` 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("adc", None) is not None:
        if not isinstance(params["adc"], str):
            raise StyxValidationError(f'`adc` has the wrong type: Received `{type(params.get("adc", None))}` expected `str | None`')
    if params.get("fa", None) is not None:
        if not isinstance(params["fa"], str):
            raise StyxValidationError(f'`fa` has the wrong type: Received `{type(params.get("fa", None))}` expected `str | None`')
    if params.get("ad", None) is not None:
        if not isinstance(params["ad"], str):
            raise StyxValidationError(f'`ad` has the wrong type: Received `{type(params.get("ad", None))}` expected `str | None`')
    if params.get("rd", None) is not None:
        if not isinstance(params["rd"], str):
            raise StyxValidationError(f'`rd` has the wrong type: Received `{type(params.get("rd", None))}` expected `str | None`')
    if params.get("cl", None) is not None:
        if not isinstance(params["cl"], str):
            raise StyxValidationError(f'`cl` has the wrong type: Received `{type(params.get("cl", None))}` expected `str | None`')
    if params.get("cp", None) is not None:
        if not isinstance(params["cp"], str):
            raise StyxValidationError(f'`cp` has the wrong type: Received `{type(params.get("cp", None))}` expected `str | None`')
    if params.get("cs", None) is not None:
        if not isinstance(params["cs"], str):
            raise StyxValidationError(f'`cs` has the wrong type: Received `{type(params.get("cs", None))}` expected `str | None`')
    if params.get("value", None) is not None:
        if not isinstance(params["value"], str):
            raise StyxValidationError(f'`value` has the wrong type: Received `{type(params.get("value", None))}` expected `str | None`')
    if params.get("vector", None) is not None:
        if not isinstance(params["vector"], str):
            raise StyxValidationError(f'`vector` has the wrong type: Received `{type(params.get("vector", None))}` expected `str | None`')
    if params.get("num", None) is not None:
        if not isinstance(params["num"], list):
            raise StyxValidationError(f'`num` has the wrong type: Received `{type(params.get("num", None))}` expected `list[int] | None`')
        for e in params["num"]:
            if not isinstance(e, int):
                raise StyxValidationError(f'`num` has the wrong type: Received `{type(params.get("num", None))}` expected `list[int] | None`')
    if params.get("modulate", None) is not None:
        if not isinstance(params["modulate"], str):
            raise StyxValidationError(f'`modulate` has the wrong type: Received `{type(params.get("modulate", None))}` expected `str | None`')
    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("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[Tensor2metricConfigParamsDict] | None`')
        for e in params["config"]:
            tensor2metric_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("tensor", None) is None:
        raise StyxValidationError("`tensor` must not be None")
    if not isinstance(params["tensor"], (pathlib.Path, str)):
        raise StyxValidationError(f'`tensor` has the wrong type: Received `{type(params.get("tensor", None))}` expected `InputPathType`')


def tensor2metric_cargs(
    params: Tensor2metricParamsDict,
    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("tensor2metric")
    if params.get("adc", None) is not None:
        cargs.extend([
            "-adc",
            params.get("adc", None)
        ])
    if params.get("fa", None) is not None:
        cargs.extend([
            "-fa",
            params.get("fa", None)
        ])
    if params.get("ad", None) is not None:
        cargs.extend([
            "-ad",
            params.get("ad", None)
        ])
    if params.get("rd", None) is not None:
        cargs.extend([
            "-rd",
            params.get("rd", None)
        ])
    if params.get("cl", None) is not None:
        cargs.extend([
            "-cl",
            params.get("cl", None)
        ])
    if params.get("cp", None) is not None:
        cargs.extend([
            "-cp",
            params.get("cp", None)
        ])
    if params.get("cs", None) is not None:
        cargs.extend([
            "-cs",
            params.get("cs", None)
        ])
    if params.get("value", None) is not None:
        cargs.extend([
            "-value",
            params.get("value", None)
        ])
    if params.get("vector", None) is not None:
        cargs.extend([
            "-vector",
            params.get("vector", None)
        ])
    if params.get("num", None) is not None:
        cargs.extend([
            "-num",
            *map(str, params.get("num", None))
        ])
    if params.get("modulate", None) is not None:
        cargs.extend([
            "-modulate",
            params.get("modulate", None)
        ])
    if params.get("mask", None) is not None:
        cargs.extend([
            "-mask",
            execution.input_file(params.get("mask", 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 [tensor2metric_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("tensor", None)))
    return cargs


def tensor2metric_outputs(
    params: Tensor2metricParamsDict,
    execution: Execution,
) -> Tensor2metricOutputs:
    """
    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 = Tensor2metricOutputs(
        root=execution.output_file("."),
        adc=execution.output_file(params.get("adc", None)) if (params.get("adc") is not None) else None,
        fa=execution.output_file(params.get("fa", None)) if (params.get("fa") is not None) else None,
        ad=execution.output_file(params.get("ad", None)) if (params.get("ad") is not None) else None,
        rd=execution.output_file(params.get("rd", None)) if (params.get("rd") is not None) else None,
        cl=execution.output_file(params.get("cl", None)) if (params.get("cl") is not None) else None,
        cp=execution.output_file(params.get("cp", None)) if (params.get("cp") is not None) else None,
        cs=execution.output_file(params.get("cs", None)) if (params.get("cs") is not None) else None,
        value=execution.output_file(params.get("value", None)) if (params.get("value") is not None) else None,
        vector=execution.output_file(params.get("vector", None)) if (params.get("vector") is not None) else None,
    )
    return ret


def tensor2metric_execute(
    params: Tensor2metricParamsDict,
    runner: Runner | None = None,
) -> Tensor2metricOutputs:
    """
    tensor2metric
    
    Generate maps of tensor-derived parameters.
    
    
    
    References:
    
    Basser, P. J.; Mattiello, J. & Lebihan, D. MR diffusion tensor spectroscopy
    and imaging. Biophysical Journal, 1994, 66, 259-267
    
    Westin, C. F.; Peled, S.; Gudbjartsson, H.; Kikinis, R. & Jolesz, F. A.
    Geometrical diffusion measures for MRI from tensor basis analysis. Proc Intl
    Soc Mag Reson Med, 1997, 5, 1742.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Tensor2metricOutputs`).
    """
    tensor2metric_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(TENSOR2METRIC_METADATA)
    params = execution.params(params)
    cargs = tensor2metric_cargs(params, execution)
    ret = tensor2metric_outputs(params, execution)
    execution.run(cargs)
    return ret


def tensor2metric(
    tensor: InputPathType,
    adc: str | None = None,
    fa: str | None = None,
    ad: str | None = None,
    rd: str | None = None,
    cl: str | None = None,
    cp: str | None = None,
    cs: str | None = None,
    value: str | None = None,
    vector: str | None = None,
    num: list[int] | None = None,
    modulate: str | None = None,
    mask: InputPathType | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Tensor2metricConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> Tensor2metricOutputs:
    """
    tensor2metric
    
    Generate maps of tensor-derived parameters.
    
    
    
    References:
    
    Basser, P. J.; Mattiello, J. & Lebihan, D. MR diffusion tensor spectroscopy
    and imaging. Biophysical Journal, 1994, 66, 259-267
    
    Westin, C. F.; Peled, S.; Gudbjartsson, H.; Kikinis, R. & Jolesz, F. A.
    Geometrical diffusion measures for MRI from tensor basis analysis. Proc Intl
    Soc Mag Reson Med, 1997, 5, 1742.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        tensor: the input tensor image.
        adc: compute the mean apparent diffusion coefficient (ADC) of the\
            diffusion tensor. (sometimes also referred to as the mean diffusivity\
            (MD)).
        fa: compute the fractional anisotropy (FA) of the diffusion tensor.
        ad: compute the axial diffusivity (AD) of the diffusion tensor.\
            (equivalent to the principal eigenvalue).
        rd: compute the radial diffusivity (RD) of the diffusion tensor.\
            (equivalent to the mean of the two non-principal eigenvalues).
        cl: compute the linearity metric of the diffusion tensor. (one of the\
            three Westin shape metrics).
        cp: compute the planarity metric of the diffusion tensor. (one of the\
            three Westin shape metrics).
        cs: compute the sphericity metric of the diffusion tensor. (one of the\
            three Westin shape metrics).
        value: compute the selected eigenvalue(s) of the diffusion tensor.
        vector: compute the selected eigenvector(s) of the diffusion tensor.
        num: specify the desired eigenvalue/eigenvector(s). Note that several\
            eigenvalues can be specified as a number sequence. For example, '1,3'\
            specifies the principal (1) and minor (3) eigenvalues/eigenvectors\
            (default = 1).
        modulate: specify how to modulate the magnitude of the eigenvectors.\
            Valid choices are: none, FA, eigval (default = FA).
        mask: only perform computation within the specified binary brain mask\
            image.
        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 `Tensor2metricOutputs`).
    """
    params = tensor2metric_params(
        adc=adc,
        fa=fa,
        ad=ad,
        rd=rd,
        cl=cl,
        cp=cp,
        cs=cs,
        value=value,
        vector=vector,
        num=num,
        modulate=modulate,
        mask=mask,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        tensor=tensor,
    )
    return tensor2metric_execute(params, runner)


__all__ = [
    "TENSOR2METRIC_METADATA",
    "Tensor2metricConfigParamsDict",
    "Tensor2metricConfigParamsDictTagged",
    "Tensor2metricOutputs",
    "Tensor2metricParamsDict",
    "Tensor2metricParamsDictTagged",
    "tensor2metric",
    "tensor2metric_config",
    "tensor2metric_execute",
    "tensor2metric_params",
]
