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

import typing
import pathlib
from styxdefs import *

SH2PEAKS_METADATA = Metadata(
    id="1cf460ab2a2a452d9f75103cade5ef23706312a1.boutiques",
    name="sh2peaks",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_Sh2peaksDirectionParamsDictNoTag = typing.TypedDict('_Sh2peaksDirectionParamsDictNoTag', {
    "phi": float,
    "theta": float,
})
Sh2peaksDirectionParamsDictTagged = typing.TypedDict('Sh2peaksDirectionParamsDictTagged', {
    "@type": typing.Literal["direction"],
    "phi": float,
    "theta": float,
})
Sh2peaksDirectionParamsDict = _Sh2peaksDirectionParamsDictNoTag | Sh2peaksDirectionParamsDictTagged


_Sh2peaksConfigParamsDictNoTag = typing.TypedDict('_Sh2peaksConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
Sh2peaksConfigParamsDictTagged = typing.TypedDict('Sh2peaksConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
Sh2peaksConfigParamsDict = _Sh2peaksConfigParamsDictNoTag | Sh2peaksConfigParamsDictTagged


_Sh2peaksParamsDictNoTag = typing.TypedDict('_Sh2peaksParamsDictNoTag', {
    "num": typing.NotRequired[int | None],
    "direction": typing.NotRequired[list[Sh2peaksDirectionParamsDict] | None],
    "peaks": typing.NotRequired[InputPathType | None],
    "threshold": typing.NotRequired[float | None],
    "seeds": typing.NotRequired[InputPathType | None],
    "mask": typing.NotRequired[InputPathType | None],
    "fast": bool,
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Sh2peaksConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "SH": InputPathType,
    "output": str,
})
Sh2peaksParamsDictTagged = typing.TypedDict('Sh2peaksParamsDictTagged', {
    "@type": typing.Literal["mrtrix/sh2peaks"],
    "num": typing.NotRequired[int | None],
    "direction": typing.NotRequired[list[Sh2peaksDirectionParamsDict] | None],
    "peaks": typing.NotRequired[InputPathType | None],
    "threshold": typing.NotRequired[float | None],
    "seeds": typing.NotRequired[InputPathType | None],
    "mask": typing.NotRequired[InputPathType | None],
    "fast": bool,
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Sh2peaksConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "SH": InputPathType,
    "output": str,
})
Sh2peaksParamsDict = _Sh2peaksParamsDictNoTag | Sh2peaksParamsDictTagged


def sh2peaks_direction(
    phi: float,
    theta: float,
) -> Sh2peaksDirectionParamsDictTagged:
    """
    Build parameters.
    
    Args:
        phi: the direction of a peak to estimate. The algorithm will attempt to\
            find the same number of peaks as have been specified using this option.
        theta: the direction of a peak to estimate. The algorithm will attempt\
            to find the same number of peaks as have been specified using this\
            option.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "direction",
        "phi": phi,
        "theta": theta,
    }
    return params


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


def sh2peaks_direction_cargs(
    params: Sh2peaksDirectionParamsDict,
    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("-direction")
    cargs.append(str(params.get("phi", None)))
    cargs.append(str(params.get("theta", None)))
    return cargs


def sh2peaks_config(
    key: str,
    value: str,
) -> Sh2peaksConfigParamsDictTagged:
    """
    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 sh2peaks_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Sh2peaksConfigParamsDict` 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 sh2peaks_config_cargs(
    params: Sh2peaksConfigParamsDict,
    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 Sh2peaksOutputs(typing.NamedTuple):
    """
    Output object returned when calling `Sh2peaksParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output: OutputPathType
    """the output image. Each volume corresponds to the x, y & z component of
    each peak direction vector in turn."""


def sh2peaks_params(
    sh: InputPathType,
    output: str,
    num: int | None = None,
    direction: list[Sh2peaksDirectionParamsDict] | None = None,
    peaks: InputPathType | None = None,
    threshold: float | None = None,
    seeds: InputPathType | None = None,
    mask: InputPathType | None = None,
    fast: bool = False,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Sh2peaksConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> Sh2peaksParamsDictTagged:
    """
    Build parameters.
    
    Args:
        sh: the input image of SH coefficients.
        output: the output image. Each volume corresponds to the x, y & z\
            component of each peak direction vector in turn.
        num: the number of peaks to extract (default: 3).
        direction: the direction of a peak to estimate. The algorithm will\
            attempt to find the same number of peaks as have been specified using\
            this option.
        peaks: the program will try to find the peaks that most closely match\
            those in the image provided.
        threshold: only peak amplitudes greater than the threshold will be\
            considered.
        seeds: specify a set of directions from which to start the multiple\
            restarts of the optimisation (by default, the built-in 60 direction set\
            is used).
        mask: only perform computation within the specified binary brain mask\
            image.
        fast: use lookup table to compute associated Legendre polynomials\
            (faster, but approximate).
        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/sh2peaks",
        "fast": fast,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "SH": sh,
        "output": output,
    }
    if num is not None:
        params["num"] = num
    if direction is not None:
        params["direction"] = direction
    if peaks is not None:
        params["peaks"] = peaks
    if threshold is not None:
        params["threshold"] = threshold
    if seeds is not None:
        params["seeds"] = seeds
    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 sh2peaks_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Sh2peaksParamsDict` 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("num", None) is not None:
        if not isinstance(params["num"], int):
            raise StyxValidationError(f'`num` has the wrong type: Received `{type(params.get("num", None))}` expected `int | None`')
    if params.get("direction", None) is not None:
        if not isinstance(params["direction"], list):
            raise StyxValidationError(f'`direction` has the wrong type: Received `{type(params.get("direction", None))}` expected `list[Sh2peaksDirectionParamsDict] | None`')
        for e in params["direction"]:
            sh2peaks_direction_validate(e)
    if params.get("peaks", None) is not None:
        if not isinstance(params["peaks"], (pathlib.Path, str)):
            raise StyxValidationError(f'`peaks` has the wrong type: Received `{type(params.get("peaks", None))}` expected `InputPathType | None`')
    if params.get("threshold", None) is not None:
        if not isinstance(params["threshold"], (float, int)):
            raise StyxValidationError(f'`threshold` has the wrong type: Received `{type(params.get("threshold", None))}` expected `float | None`')
    if params.get("seeds", None) is not None:
        if not isinstance(params["seeds"], (pathlib.Path, str)):
            raise StyxValidationError(f'`seeds` has the wrong type: Received `{type(params.get("seeds", None))}` expected `InputPathType | 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("fast", False) is None:
        raise StyxValidationError("`fast` must not be None")
    if not isinstance(params["fast"], bool):
        raise StyxValidationError(f'`fast` has the wrong type: Received `{type(params.get("fast", 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[Sh2peaksConfigParamsDict] | None`')
        for e in params["config"]:
            sh2peaks_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("output", None) is None:
        raise StyxValidationError("`output` must not be None")
    if not isinstance(params["output"], str):
        raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `str`')


def sh2peaks_cargs(
    params: Sh2peaksParamsDict,
    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("sh2peaks")
    if params.get("num", None) is not None:
        cargs.extend([
            "-num",
            str(params.get("num", None))
        ])
    if params.get("direction", None) is not None:
        cargs.extend([a for c in [sh2peaks_direction_cargs(s, execution) for s in params.get("direction", None)] for a in c])
    if params.get("peaks", None) is not None:
        cargs.extend([
            "-peaks",
            execution.input_file(params.get("peaks", None))
        ])
    if params.get("threshold", None) is not None:
        cargs.extend([
            "-threshold",
            str(params.get("threshold", None))
        ])
    if params.get("seeds", None) is not None:
        cargs.extend([
            "-seeds",
            execution.input_file(params.get("seeds", None))
        ])
    if params.get("mask", None) is not None:
        cargs.extend([
            "-mask",
            execution.input_file(params.get("mask", None))
        ])
    if params.get("fast", False):
        cargs.append("-fast")
    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 [sh2peaks_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(params.get("output", None))
    return cargs


def sh2peaks_outputs(
    params: Sh2peaksParamsDict,
    execution: Execution,
) -> Sh2peaksOutputs:
    """
    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 = Sh2peaksOutputs(
        root=execution.output_file("."),
        output=execution.output_file(params.get("output", None)),
    )
    return ret


def sh2peaks_execute(
    params: Sh2peaksParamsDict,
    runner: Runner | None = None,
) -> Sh2peaksOutputs:
    """
    sh2peaks
    
    Extract the peaks of a spherical harmonic function in each voxel.
    
    Peaks of the spherical harmonic function in each voxel are located by
    commencing a Newton search along each of a set of pre-specified directions
    
    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:
    
    Jeurissen, B.; Leemans, A.; Tournier, J.-D.; Jones, D.K.; Sijbers, J.
    Investigating the prevalence of complex fiber configurations in white matter
    tissue with diffusion magnetic resonance imaging. Human Brain Mapping, 2013,
    34(11), 2747-2766.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Sh2peaksOutputs`).
    """
    sh2peaks_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(SH2PEAKS_METADATA)
    params = execution.params(params)
    cargs = sh2peaks_cargs(params, execution)
    ret = sh2peaks_outputs(params, execution)
    execution.run(cargs)
    return ret


def sh2peaks(
    sh: InputPathType,
    output: str,
    num: int | None = None,
    direction: list[Sh2peaksDirectionParamsDict] | None = None,
    peaks: InputPathType | None = None,
    threshold: float | None = None,
    seeds: InputPathType | None = None,
    mask: InputPathType | None = None,
    fast: bool = False,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Sh2peaksConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> Sh2peaksOutputs:
    """
    sh2peaks
    
    Extract the peaks of a spherical harmonic function in each voxel.
    
    Peaks of the spherical harmonic function in each voxel are located by
    commencing a Newton search along each of a set of pre-specified directions
    
    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:
    
    Jeurissen, B.; Leemans, A.; Tournier, J.-D.; Jones, D.K.; Sijbers, J.
    Investigating the prevalence of complex fiber configurations in white matter
    tissue with diffusion magnetic resonance imaging. Human Brain Mapping, 2013,
    34(11), 2747-2766.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        sh: the input image of SH coefficients.
        output: the output image. Each volume corresponds to the x, y & z\
            component of each peak direction vector in turn.
        num: the number of peaks to extract (default: 3).
        direction: the direction of a peak to estimate. The algorithm will\
            attempt to find the same number of peaks as have been specified using\
            this option.
        peaks: the program will try to find the peaks that most closely match\
            those in the image provided.
        threshold: only peak amplitudes greater than the threshold will be\
            considered.
        seeds: specify a set of directions from which to start the multiple\
            restarts of the optimisation (by default, the built-in 60 direction set\
            is used).
        mask: only perform computation within the specified binary brain mask\
            image.
        fast: use lookup table to compute associated Legendre polynomials\
            (faster, but approximate).
        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 `Sh2peaksOutputs`).
    """
    params = sh2peaks_params(
        num=num,
        direction=direction,
        peaks=peaks,
        threshold=threshold,
        seeds=seeds,
        mask=mask,
        fast=fast,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        sh=sh,
        output=output,
    )
    return sh2peaks_execute(params, runner)


__all__ = [
    "SH2PEAKS_METADATA",
    "Sh2peaksConfigParamsDict",
    "Sh2peaksConfigParamsDictTagged",
    "Sh2peaksDirectionParamsDict",
    "Sh2peaksDirectionParamsDictTagged",
    "Sh2peaksOutputs",
    "Sh2peaksParamsDict",
    "Sh2peaksParamsDictTagged",
    "sh2peaks",
    "sh2peaks_config",
    "sh2peaks_direction",
    "sh2peaks_execute",
    "sh2peaks_params",
]
