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

import typing
import pathlib
from styxdefs import *

AMP2RESPONSE_METADATA = Metadata(
    id="f0088602c41a6ff20ec7c430ad21aee73ff1f3e4.boutiques",
    name="amp2response",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_Amp2responseConfigParamsDictNoTag = typing.TypedDict('_Amp2responseConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
Amp2responseConfigParamsDictTagged = typing.TypedDict('Amp2responseConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
Amp2responseConfigParamsDict = _Amp2responseConfigParamsDictNoTag | Amp2responseConfigParamsDictTagged


_Amp2responseParamsDictNoTag = typing.TypedDict('_Amp2responseParamsDictNoTag', {
    "isotropic": bool,
    "noconstraint": bool,
    "directions": typing.NotRequired[InputPathType | None],
    "shells": typing.NotRequired[list[float] | None],
    "lmax": typing.NotRequired[list[int] | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Amp2responseConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "amps": InputPathType,
    "mask": InputPathType,
    "directions_1": InputPathType,
    "response": str,
})
Amp2responseParamsDictTagged = typing.TypedDict('Amp2responseParamsDictTagged', {
    "@type": typing.Literal["mrtrix/amp2response"],
    "isotropic": bool,
    "noconstraint": bool,
    "directions": typing.NotRequired[InputPathType | None],
    "shells": typing.NotRequired[list[float] | None],
    "lmax": typing.NotRequired[list[int] | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Amp2responseConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "amps": InputPathType,
    "mask": InputPathType,
    "directions_1": InputPathType,
    "response": str,
})
Amp2responseParamsDict = _Amp2responseParamsDictNoTag | Amp2responseParamsDictTagged


def amp2response_config(
    key: str,
    value: str,
) -> Amp2responseConfigParamsDictTagged:
    """
    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 amp2response_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Amp2responseConfigParamsDict` 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 amp2response_config_cargs(
    params: Amp2responseConfigParamsDict,
    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 Amp2responseOutputs(typing.NamedTuple):
    """
    Output object returned when calling `Amp2responseParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    response: OutputPathType
    """the output zonal spherical harmonic coefficients"""


def amp2response_params(
    amps: InputPathType,
    mask: InputPathType,
    directions_1: InputPathType,
    response: str,
    isotropic: bool = False,
    noconstraint: bool = False,
    directions: InputPathType | None = None,
    shells: list[float] | None = None,
    lmax: list[int] | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Amp2responseConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> Amp2responseParamsDictTagged:
    """
    Build parameters.
    
    Args:
        amps: the amplitudes image.
        mask: the mask containing the voxels from which to estimate the\
            response function.
        directions_1: a 4D image containing the estimated fibre directions.
        response: the output zonal spherical harmonic coefficients.
        isotropic: estimate an isotropic response function (lmax=0 for all\
            shells).
        noconstraint: disable the non-negativity and monotonicity constraints.
        directions: provide an external text file containing the directions\
            along which the amplitudes are sampled.
        shells: specify one or more b-values to use during processing, as a\
            comma-separated list of the desired approximate b-values (b-values are\
            clustered to allow for small deviations). Note that some commands are\
            incompatible with multiple b-values, and will report an error if more\
            than one b-value is provided.\
            WARNING: note that, even though the b=0 volumes are never referred\
            to as shells in the literature, they still have to be explicitly\
            included in the list of b-values as provided to the -shell option!\
            Several algorithms which include the b=0 volumes in their\
            computations may otherwise return an undesired result.
        lmax: specify the maximum harmonic degree of the response function to\
            estimate (can be a comma-separated list for multi-shell data).
        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/amp2response",
        "isotropic": isotropic,
        "noconstraint": noconstraint,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "amps": amps,
        "mask": mask,
        "directions_1": directions_1,
        "response": response,
    }
    if directions is not None:
        params["directions"] = directions
    if shells is not None:
        params["shells"] = shells
    if lmax is not None:
        params["lmax"] = lmax
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def amp2response_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Amp2responseParamsDict` 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("isotropic", False) is None:
        raise StyxValidationError("`isotropic` must not be None")
    if not isinstance(params["isotropic"], bool):
        raise StyxValidationError(f'`isotropic` has the wrong type: Received `{type(params.get("isotropic", False))}` expected `bool`')
    if params.get("noconstraint", False) is None:
        raise StyxValidationError("`noconstraint` must not be None")
    if not isinstance(params["noconstraint"], bool):
        raise StyxValidationError(f'`noconstraint` has the wrong type: Received `{type(params.get("noconstraint", False))}` expected `bool`')
    if params.get("directions", None) is not None:
        if not isinstance(params["directions"], (pathlib.Path, str)):
            raise StyxValidationError(f'`directions` has the wrong type: Received `{type(params.get("directions", None))}` expected `InputPathType | None`')
    if params.get("shells", None) is not None:
        if not isinstance(params["shells"], list):
            raise StyxValidationError(f'`shells` has the wrong type: Received `{type(params.get("shells", None))}` expected `list[float] | None`')
        for e in params["shells"]:
            if not isinstance(e, (float, int)):
                raise StyxValidationError(f'`shells` has the wrong type: Received `{type(params.get("shells", None))}` expected `list[float] | None`')
    if params.get("lmax", None) is not None:
        if not isinstance(params["lmax"], list):
            raise StyxValidationError(f'`lmax` has the wrong type: Received `{type(params.get("lmax", None))}` expected `list[int] | None`')
        for e in params["lmax"]:
            if not isinstance(e, int):
                raise StyxValidationError(f'`lmax` has the wrong type: Received `{type(params.get("lmax", None))}` expected `list[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[Amp2responseConfigParamsDict] | None`')
        for e in params["config"]:
            amp2response_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("amps", None) is None:
        raise StyxValidationError("`amps` must not be None")
    if not isinstance(params["amps"], (pathlib.Path, str)):
        raise StyxValidationError(f'`amps` has the wrong type: Received `{type(params.get("amps", None))}` expected `InputPathType`')
    if params.get("mask", None) is None:
        raise StyxValidationError("`mask` must not be 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`')
    if params.get("directions_1", None) is None:
        raise StyxValidationError("`directions_1` must not be None")
    if not isinstance(params["directions_1"], (pathlib.Path, str)):
        raise StyxValidationError(f'`directions_1` has the wrong type: Received `{type(params.get("directions_1", None))}` expected `InputPathType`')
    if params.get("response", None) is None:
        raise StyxValidationError("`response` must not be None")
    if not isinstance(params["response"], str):
        raise StyxValidationError(f'`response` has the wrong type: Received `{type(params.get("response", None))}` expected `str`')


def amp2response_cargs(
    params: Amp2responseParamsDict,
    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("amp2response")
    if params.get("isotropic", False):
        cargs.append("-isotropic")
    if params.get("noconstraint", False):
        cargs.append("-noconstraint")
    if params.get("directions", None) is not None:
        cargs.extend([
            "-directions",
            execution.input_file(params.get("directions", None))
        ])
    if params.get("shells", None) is not None:
        cargs.extend([
            "-shells",
            ",".join(map(str, params.get("shells", None)))
        ])
    if params.get("lmax", None) is not None:
        cargs.extend([
            "-lmax",
            ",".join(map(str, params.get("lmax", 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 [amp2response_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("amps", None)))
    cargs.append(execution.input_file(params.get("mask", None)))
    cargs.append(execution.input_file(params.get("directions_1", None)))
    cargs.append(params.get("response", None))
    return cargs


def amp2response_outputs(
    params: Amp2responseParamsDict,
    execution: Execution,
) -> Amp2responseOutputs:
    """
    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 = Amp2responseOutputs(
        root=execution.output_file("."),
        response=execution.output_file(params.get("response", None)),
    )
    return ret


def amp2response_execute(
    params: Amp2responseParamsDict,
    runner: Runner | None = None,
) -> Amp2responseOutputs:
    """
    amp2response
    
    Estimate response function coefficients based on the DWI signal in
    single-fibre voxels.
    
    This command uses the image data from all selected single-fibre voxels
    concurrently, rather than simply averaging their individual spherical
    harmonic coefficients. It also ensures that the response function is
    non-negative, and monotonic (i.e. its amplitude must increase from the fibre
    direction out to the orthogonal plane).
    
    If multi-shell data are provided, and one or more b-value shells are not
    explicitly requested, the command will generate a response function for
    every b-value shell (including b=0 if present).
    
    References:
    
    Smith, R. E.; Dhollander, T. & Connelly, A. Constrained linear least squares
    estimation of anisotropic response function for spherical deconvolution.
    ISMRM Workshop on Breaking the Barriers of Diffusion MRI, 23.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Amp2responseOutputs`).
    """
    amp2response_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(AMP2RESPONSE_METADATA)
    params = execution.params(params)
    cargs = amp2response_cargs(params, execution)
    ret = amp2response_outputs(params, execution)
    execution.run(cargs)
    return ret


def amp2response(
    amps: InputPathType,
    mask: InputPathType,
    directions_1: InputPathType,
    response: str,
    isotropic: bool = False,
    noconstraint: bool = False,
    directions: InputPathType | None = None,
    shells: list[float] | None = None,
    lmax: list[int] | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Amp2responseConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> Amp2responseOutputs:
    """
    amp2response
    
    Estimate response function coefficients based on the DWI signal in
    single-fibre voxels.
    
    This command uses the image data from all selected single-fibre voxels
    concurrently, rather than simply averaging their individual spherical
    harmonic coefficients. It also ensures that the response function is
    non-negative, and monotonic (i.e. its amplitude must increase from the fibre
    direction out to the orthogonal plane).
    
    If multi-shell data are provided, and one or more b-value shells are not
    explicitly requested, the command will generate a response function for
    every b-value shell (including b=0 if present).
    
    References:
    
    Smith, R. E.; Dhollander, T. & Connelly, A. Constrained linear least squares
    estimation of anisotropic response function for spherical deconvolution.
    ISMRM Workshop on Breaking the Barriers of Diffusion MRI, 23.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        amps: the amplitudes image.
        mask: the mask containing the voxels from which to estimate the\
            response function.
        directions_1: a 4D image containing the estimated fibre directions.
        response: the output zonal spherical harmonic coefficients.
        isotropic: estimate an isotropic response function (lmax=0 for all\
            shells).
        noconstraint: disable the non-negativity and monotonicity constraints.
        directions: provide an external text file containing the directions\
            along which the amplitudes are sampled.
        shells: specify one or more b-values to use during processing, as a\
            comma-separated list of the desired approximate b-values (b-values are\
            clustered to allow for small deviations). Note that some commands are\
            incompatible with multiple b-values, and will report an error if more\
            than one b-value is provided.\
            WARNING: note that, even though the b=0 volumes are never referred\
            to as shells in the literature, they still have to be explicitly\
            included in the list of b-values as provided to the -shell option!\
            Several algorithms which include the b=0 volumes in their\
            computations may otherwise return an undesired result.
        lmax: specify the maximum harmonic degree of the response function to\
            estimate (can be a comma-separated list for multi-shell data).
        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 `Amp2responseOutputs`).
    """
    params = amp2response_params(
        isotropic=isotropic,
        noconstraint=noconstraint,
        directions=directions,
        shells=shells,
        lmax=lmax,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        amps=amps,
        mask=mask,
        directions_1=directions_1,
        response=response,
    )
    return amp2response_execute(params, runner)


__all__ = [
    "AMP2RESPONSE_METADATA",
    "Amp2responseConfigParamsDict",
    "Amp2responseConfigParamsDictTagged",
    "Amp2responseOutputs",
    "Amp2responseParamsDict",
    "Amp2responseParamsDictTagged",
    "amp2response",
    "amp2response_config",
    "amp2response_execute",
    "amp2response_params",
]
