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

import typing
import pathlib
from styxdefs import *

SHCONV_METADATA = Metadata(
    id="b17c6209ec80f04a818c3feb05bf3e7580772b75.boutiques",
    name="shconv",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_ShconvVariousStringParamsDictNoTag = typing.TypedDict('_ShconvVariousStringParamsDictNoTag', {
    "obj": str,
})
ShconvVariousStringParamsDictTagged = typing.TypedDict('ShconvVariousStringParamsDictTagged', {
    "@type": typing.Literal["VariousString"],
    "obj": str,
})
ShconvVariousStringParamsDict = _ShconvVariousStringParamsDictNoTag | ShconvVariousStringParamsDictTagged


_ShconvVariousFileParamsDictNoTag = typing.TypedDict('_ShconvVariousFileParamsDictNoTag', {
    "obj": InputPathType,
})
ShconvVariousFileParamsDictTagged = typing.TypedDict('ShconvVariousFileParamsDictTagged', {
    "@type": typing.Literal["VariousFile"],
    "obj": InputPathType,
})
ShconvVariousFileParamsDict = _ShconvVariousFileParamsDictNoTag | ShconvVariousFileParamsDictTagged


_ShconvConfigParamsDictNoTag = typing.TypedDict('_ShconvConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
ShconvConfigParamsDictTagged = typing.TypedDict('ShconvConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
ShconvConfigParamsDict = _ShconvConfigParamsDictNoTag | ShconvConfigParamsDictTagged


_ShconvParamsDictNoTag = typing.TypedDict('_ShconvParamsDictNoTag', {
    "datatype": typing.NotRequired[str | None],
    "strides": typing.NotRequired[typing.Union[ShconvVariousStringParamsDictTagged, ShconvVariousFileParamsDictTagged] | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[ShconvConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "odf_response": list[str],
    "SH_out": str,
})
ShconvParamsDictTagged = typing.TypedDict('ShconvParamsDictTagged', {
    "@type": typing.Literal["mrtrix/shconv"],
    "datatype": typing.NotRequired[str | None],
    "strides": typing.NotRequired[typing.Union[ShconvVariousStringParamsDictTagged, ShconvVariousFileParamsDictTagged] | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[ShconvConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "odf_response": list[str],
    "SH_out": str,
})
ShconvParamsDict = _ShconvParamsDictNoTag | ShconvParamsDictTagged


def shconv_strides_cargs_dyn_fn(
    t: str,
) -> typing.Any:
    """
    Get build cargs function by command type.
    
    Args:
        t: Command type.
    Returns:
        Build cargs function.
    """
    return {
        "VariousString": shconv_various_string_cargs,
        "VariousFile": shconv_various_file_cargs,
    }.get(t)


def shconv_strides_validate_dyn_fn(
    t: str,
) -> typing.Any:
    """
    Get validate params function by command type.
    
    Args:
        t: Command type.
    Returns:
        Validate params function.
    """
    return {
        "VariousString": shconv_various_string_validate,
        "VariousFile": shconv_various_file_validate,
    }.get(t)


def shconv_various_string(
    obj: str,
) -> ShconvVariousStringParamsDictTagged:
    """
    Build parameters.
    
    Args:
        obj: String object.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "VariousString",
        "obj": obj,
    }
    return params


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


def shconv_various_string_cargs(
    params: ShconvVariousStringParamsDict,
    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(params.get("obj", None))
    return cargs


def shconv_various_file(
    obj: InputPathType,
) -> ShconvVariousFileParamsDictTagged:
    """
    Build parameters.
    
    Args:
        obj: File object.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "VariousFile",
        "obj": obj,
    }
    return params


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


def shconv_various_file_cargs(
    params: ShconvVariousFileParamsDict,
    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(execution.input_file(params.get("obj", None)))
    return cargs


def shconv_config(
    key: str,
    value: str,
) -> ShconvConfigParamsDictTagged:
    """
    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 shconv_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ShconvConfigParamsDict` 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 shconv_config_cargs(
    params: ShconvConfigParamsDict,
    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 ShconvOutputs(typing.NamedTuple):
    """
    Output object returned when calling `ShconvParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    sh_out: OutputPathType
    """the output spherical harmonics coefficients image."""


def shconv_params(
    odf_response: list[str],
    sh_out: str,
    datatype: str | None = None,
    strides: typing.Union[ShconvVariousStringParamsDictTagged, ShconvVariousFileParamsDictTagged] | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[ShconvConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> ShconvParamsDictTagged:
    """
    Build parameters.
    
    Args:
        odf_response: pairs of input ODF image and corresponding responses.
        sh_out: the output spherical harmonics coefficients image.
        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.
        strides: specify the strides of the output data in memory; either as a\
            comma-separated list of (signed) integers, or as a template image from\
            which the strides shall be extracted and used. The actual strides\
            produced will depend on whether the output image format can support it.
        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/shconv",
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "odf_response": odf_response,
        "SH_out": sh_out,
    }
    if datatype is not None:
        params["datatype"] = datatype
    if strides is not None:
        params["strides"] = strides
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def shconv_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ShconvParamsDict` 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("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("strides", None) is not None:
        if not isinstance(params["strides"], dict):
            raise StyxValidationError(f'Params object has the wrong type \'{type(params["strides"])}\'')
        if "@type" not in params["strides"]:
            raise StyxValidationError("Params object is missing `@type`")
        if params["strides"]["@type"] not in ["VariousString", "VariousFile"]:
            raise StyxValidationError("Parameter `strides`s `@type` must be one of [\"VariousString\", \"VariousFile\"]")
        shconv_strides_validate_dyn_fn(params["strides"]["@type"])(params["strides"])
    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[ShconvConfigParamsDict] | None`')
        for e in params["config"]:
            shconv_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("odf_response", None) is None:
        raise StyxValidationError("`odf_response` must not be None")
    if not isinstance(params["odf_response"], list):
        raise StyxValidationError(f'`odf_response` has the wrong type: Received `{type(params.get("odf_response", None))}` expected `list[str]`')
    for e in params["odf_response"]:
        if not isinstance(e, str):
            raise StyxValidationError(f'`odf_response` has the wrong type: Received `{type(params.get("odf_response", None))}` expected `list[str]`')
    if params.get("SH_out", None) is None:
        raise StyxValidationError("`SH_out` must not be None")
    if not isinstance(params["SH_out"], str):
        raise StyxValidationError(f'`SH_out` has the wrong type: Received `{type(params.get("SH_out", None))}` expected `str`')


def shconv_cargs(
    params: ShconvParamsDict,
    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("shconv")
    if params.get("datatype", None) is not None:
        cargs.extend([
            "-datatype",
            params.get("datatype", None)
        ])
    if params.get("strides", None) is not None:
        cargs.extend([
            "-strides",
            *shconv_strides_cargs_dyn_fn(params.get("strides", None)["@type"])(params.get("strides", None), execution)
        ])
    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 [shconv_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(params.get("odf_response", None))
    cargs.append(params.get("SH_out", None))
    return cargs


def shconv_outputs(
    params: ShconvParamsDict,
    execution: Execution,
) -> ShconvOutputs:
    """
    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 = ShconvOutputs(
        root=execution.output_file("."),
        sh_out=execution.output_file(params.get("SH_out", None)),
    )
    return ret


def shconv_execute(
    params: ShconvParamsDict,
    runner: Runner | None = None,
) -> ShconvOutputs:
    """
    shconv
    
    Perform spherical convolution.
    
    Provided with matching pairs of response function and ODF images (containing
    SH coefficients), perform spherical convolution to provide the corresponding
    SH coefficients of the signal.
    
    If multiple pairs of inputs are provided, their contributions will be summed
    into a single output.
    
    If the responses are multi-shell (with one line of coefficients per shell),
    the output will be a 5-dimensional image, with the SH coefficients of the
    signal in each shell stored at different indices along the 5th dimension.
    
    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
    
    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 `ShconvOutputs`).
    """
    shconv_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(SHCONV_METADATA)
    params = execution.params(params)
    cargs = shconv_cargs(params, execution)
    ret = shconv_outputs(params, execution)
    execution.run(cargs)
    return ret


def shconv(
    odf_response: list[str],
    sh_out: str,
    datatype: str | None = None,
    strides: typing.Union[ShconvVariousStringParamsDictTagged, ShconvVariousFileParamsDictTagged] | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[ShconvConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> ShconvOutputs:
    """
    shconv
    
    Perform spherical convolution.
    
    Provided with matching pairs of response function and ODF images (containing
    SH coefficients), perform spherical convolution to provide the corresponding
    SH coefficients of the signal.
    
    If multiple pairs of inputs are provided, their contributions will be summed
    into a single output.
    
    If the responses are multi-shell (with one line of coefficients per shell),
    the output will be a 5-dimensional image, with the SH coefficients of the
    signal in each shell stored at different indices along the 5th dimension.
    
    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
    
    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:
        odf_response: pairs of input ODF image and corresponding responses.
        sh_out: the output spherical harmonics coefficients image.
        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.
        strides: specify the strides of the output data in memory; either as a\
            comma-separated list of (signed) integers, or as a template image from\
            which the strides shall be extracted and used. The actual strides\
            produced will depend on whether the output image format can support it.
        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 `ShconvOutputs`).
    """
    params = shconv_params(
        datatype=datatype,
        strides=strides,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        odf_response=odf_response,
        sh_out=sh_out,
    )
    return shconv_execute(params, runner)


__all__ = [
    "SHCONV_METADATA",
    "ShconvConfigParamsDict",
    "ShconvConfigParamsDictTagged",
    "ShconvOutputs",
    "ShconvParamsDict",
    "ShconvParamsDictTagged",
    "ShconvVariousFileParamsDict",
    "ShconvVariousFileParamsDictTagged",
    "ShconvVariousStringParamsDict",
    "ShconvVariousStringParamsDictTagged",
    "shconv",
    "shconv_config",
    "shconv_execute",
    "shconv_params",
    "shconv_various_file",
    "shconv_various_string",
]
