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

import typing
import pathlib
from styxdefs import *

SH2RESPONSE_METADATA = Metadata(
    id="b0158f6418a85b394a1f360188b67bbde8c8da29.boutiques",
    name="sh2response",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_Sh2responseConfigParamsDictNoTag = typing.TypedDict('_Sh2responseConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
Sh2responseConfigParamsDictTagged = typing.TypedDict('Sh2responseConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
Sh2responseConfigParamsDict = _Sh2responseConfigParamsDictNoTag | Sh2responseConfigParamsDictTagged


_Sh2responseParamsDictNoTag = typing.TypedDict('_Sh2responseParamsDictNoTag', {
    "lmax": typing.NotRequired[int | None],
    "dump": typing.NotRequired[str | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Sh2responseConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "SH": InputPathType,
    "mask": InputPathType,
    "directions": InputPathType,
    "response": str,
})
Sh2responseParamsDictTagged = typing.TypedDict('Sh2responseParamsDictTagged', {
    "@type": typing.Literal["mrtrix/sh2response"],
    "lmax": typing.NotRequired[int | None],
    "dump": typing.NotRequired[str | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Sh2responseConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "SH": InputPathType,
    "mask": InputPathType,
    "directions": InputPathType,
    "response": str,
})
Sh2responseParamsDict = _Sh2responseParamsDictNoTag | Sh2responseParamsDictTagged


def sh2response_config(
    key: str,
    value: str,
) -> Sh2responseConfigParamsDictTagged:
    """
    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 sh2response_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Sh2responseConfigParamsDict` 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 sh2response_config_cargs(
    params: Sh2responseConfigParamsDict,
    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 Sh2responseOutputs(typing.NamedTuple):
    """
    Output object returned when calling `Sh2responseParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    response: OutputPathType
    """the output axially-symmetric spherical harmonic coefficients"""
    dump: OutputPathType | None
    """dump the m=0 SH coefficients from all voxels in the mask to the output
    file, rather than their mean """


def sh2response_params(
    sh: InputPathType,
    mask: InputPathType,
    directions: InputPathType,
    response: str,
    lmax: int | None = None,
    dump: str | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Sh2responseConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> Sh2responseParamsDictTagged:
    """
    Build parameters.
    
    Args:
        sh: the spherical harmonic decomposition of the diffusion-weighted\
            images.
        mask: the mask containing the voxels from which to estimate the\
            response function.
        directions: a 4D image containing the direction vectors along which to\
            estimate the response function.
        response: the output axially-symmetric spherical harmonic coefficients.
        lmax: specify the maximum harmonic degree of the response function to\
            estimate.
        dump: dump the m=0 SH coefficients from all voxels in the mask to the\
            output file, rather than their mean.
        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/sh2response",
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "SH": sh,
        "mask": mask,
        "directions": directions,
        "response": response,
    }
    if lmax is not None:
        params["lmax"] = lmax
    if dump is not None:
        params["dump"] = dump
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def sh2response_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Sh2responseParamsDict` 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("lmax", None) is not None:
        if not isinstance(params["lmax"], int):
            raise StyxValidationError(f'`lmax` has the wrong type: Received `{type(params.get("lmax", None))}` expected `int | None`')
    if params.get("dump", None) is not None:
        if not isinstance(params["dump"], str):
            raise StyxValidationError(f'`dump` has the wrong type: Received `{type(params.get("dump", 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[Sh2responseConfigParamsDict] | None`')
        for e in params["config"]:
            sh2response_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("SH", None) is None:
        raise StyxValidationError("`SH` must not be None")
    if not isinstance(params["SH"], (pathlib.Path, str)):
        raise StyxValidationError(f'`SH` has the wrong type: Received `{type(params.get("SH", 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", None) is None:
        raise StyxValidationError("`directions` must not be 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`')
    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 sh2response_cargs(
    params: Sh2responseParamsDict,
    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("sh2response")
    if params.get("lmax", None) is not None:
        cargs.extend([
            "-lmax",
            str(params.get("lmax", None))
        ])
    if params.get("dump", None) is not None:
        cargs.extend([
            "-dump",
            params.get("dump", 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 [sh2response_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("SH", None)))
    cargs.append(execution.input_file(params.get("mask", None)))
    cargs.append(execution.input_file(params.get("directions", None)))
    cargs.append(params.get("response", None))
    return cargs


def sh2response_outputs(
    params: Sh2responseParamsDict,
    execution: Execution,
) -> Sh2responseOutputs:
    """
    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 = Sh2responseOutputs(
        root=execution.output_file("."),
        response=execution.output_file(params.get("response", None)),
        dump=execution.output_file(params.get("dump", None)) if (params.get("dump") is not None) else None,
    )
    return ret


def sh2response_execute(
    params: Sh2responseParamsDict,
    runner: Runner | None = None,
) -> Sh2responseOutputs:
    """
    sh2response
    
    Generate an appropriate response function from the image data for spherical
    deconvolution.
    
    The spherical harmonic coefficients are stored according the conventions
    described the main documentation, which can be found at the following link:
    https://mrtrix.readthedocs.io/en/3.0.4/concepts/spherical_harmonics.html
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Sh2responseOutputs`).
    """
    sh2response_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(SH2RESPONSE_METADATA)
    params = execution.params(params)
    cargs = sh2response_cargs(params, execution)
    ret = sh2response_outputs(params, execution)
    execution.run(cargs)
    return ret


def sh2response(
    sh: InputPathType,
    mask: InputPathType,
    directions: InputPathType,
    response: str,
    lmax: int | None = None,
    dump: str | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Sh2responseConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> Sh2responseOutputs:
    """
    sh2response
    
    Generate an appropriate response function from the image data for spherical
    deconvolution.
    
    The spherical harmonic coefficients are stored according the conventions
    described the main documentation, which can be found at the following link:
    https://mrtrix.readthedocs.io/en/3.0.4/concepts/spherical_harmonics.html
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        sh: the spherical harmonic decomposition of the diffusion-weighted\
            images.
        mask: the mask containing the voxels from which to estimate the\
            response function.
        directions: a 4D image containing the direction vectors along which to\
            estimate the response function.
        response: the output axially-symmetric spherical harmonic coefficients.
        lmax: specify the maximum harmonic degree of the response function to\
            estimate.
        dump: dump the m=0 SH coefficients from all voxels in the mask to the\
            output file, rather than their mean.
        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 `Sh2responseOutputs`).
    """
    params = sh2response_params(
        lmax=lmax,
        dump=dump,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        sh=sh,
        mask=mask,
        directions=directions,
        response=response,
    )
    return sh2response_execute(params, runner)


__all__ = [
    "SH2RESPONSE_METADATA",
    "Sh2responseConfigParamsDict",
    "Sh2responseConfigParamsDictTagged",
    "Sh2responseOutputs",
    "Sh2responseParamsDict",
    "Sh2responseParamsDictTagged",
    "sh2response",
    "sh2response_config",
    "sh2response_execute",
    "sh2response_params",
]
