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

import typing
import pathlib
from styxdefs import *

FIXEL2VOXEL_METADATA = Metadata(
    id="0e517d0eb23dea1741bca5bac37fc1918bda4919.boutiques",
    name="fixel2voxel",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_Fixel2voxelConfigParamsDictNoTag = typing.TypedDict('_Fixel2voxelConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
Fixel2voxelConfigParamsDictTagged = typing.TypedDict('Fixel2voxelConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
Fixel2voxelConfigParamsDict = _Fixel2voxelConfigParamsDictNoTag | Fixel2voxelConfigParamsDictTagged


_Fixel2voxelParamsDictNoTag = typing.TypedDict('_Fixel2voxelParamsDictNoTag', {
    "number": typing.NotRequired[int | None],
    "fill": typing.NotRequired[float | None],
    "weighted": typing.NotRequired[InputPathType | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Fixel2voxelConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "fixel_in": InputPathType,
    "operation": str,
    "image_out": str,
})
Fixel2voxelParamsDictTagged = typing.TypedDict('Fixel2voxelParamsDictTagged', {
    "@type": typing.Literal["mrtrix/fixel2voxel"],
    "number": typing.NotRequired[int | None],
    "fill": typing.NotRequired[float | None],
    "weighted": typing.NotRequired[InputPathType | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Fixel2voxelConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "fixel_in": InputPathType,
    "operation": str,
    "image_out": str,
})
Fixel2voxelParamsDict = _Fixel2voxelParamsDictNoTag | Fixel2voxelParamsDictTagged


def fixel2voxel_config(
    key: str,
    value: str,
) -> Fixel2voxelConfigParamsDictTagged:
    """
    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 fixel2voxel_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Fixel2voxelConfigParamsDict` 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 fixel2voxel_config_cargs(
    params: Fixel2voxelConfigParamsDict,
    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 Fixel2voxelOutputs(typing.NamedTuple):
    """
    Output object returned when calling `Fixel2voxelParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    image_out: OutputPathType
    """the output scalar image."""


def fixel2voxel_params(
    fixel_in: InputPathType,
    operation: str,
    image_out: str,
    number: int | None = None,
    fill: float | None = None,
    weighted: InputPathType | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Fixel2voxelConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> Fixel2voxelParamsDictTagged:
    """
    Build parameters.
    
    Args:
        fixel_in: the input fixel data file.
        operation: the operation to apply, one of: mean, sum, product, min,\
            max, absmax, magmax, count, complexity, sf, dec_unit, dec_scaled, none.
        image_out: the output scalar image.
        number: use only the largest N fixels in calculation of the voxel-wise\
            statistic; in the case of operation "none", output only the largest N\
            fixels in each voxel.
        fill: for "none" operation, specify the value to fill when number of\
            fixels is fewer than the maximum (default: 0.0).
        weighted: weight the contribution of each fixel to the per-voxel result\
            according to its volume.
        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/fixel2voxel",
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "fixel_in": fixel_in,
        "operation": operation,
        "image_out": image_out,
    }
    if number is not None:
        params["number"] = number
    if fill is not None:
        params["fill"] = fill
    if weighted is not None:
        params["weighted"] = weighted
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def fixel2voxel_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Fixel2voxelParamsDict` 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("number", None) is not None:
        if not isinstance(params["number"], int):
            raise StyxValidationError(f'`number` has the wrong type: Received `{type(params.get("number", None))}` expected `int | None`')
    if params.get("fill", None) is not None:
        if not isinstance(params["fill"], (float, int)):
            raise StyxValidationError(f'`fill` has the wrong type: Received `{type(params.get("fill", None))}` expected `float | None`')
    if params.get("weighted", None) is not None:
        if not isinstance(params["weighted"], (pathlib.Path, str)):
            raise StyxValidationError(f'`weighted` has the wrong type: Received `{type(params.get("weighted", 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[Fixel2voxelConfigParamsDict] | None`')
        for e in params["config"]:
            fixel2voxel_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("fixel_in", None) is None:
        raise StyxValidationError("`fixel_in` must not be None")
    if not isinstance(params["fixel_in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`fixel_in` has the wrong type: Received `{type(params.get("fixel_in", None))}` expected `InputPathType`')
    if params.get("operation", None) is None:
        raise StyxValidationError("`operation` must not be None")
    if not isinstance(params["operation"], str):
        raise StyxValidationError(f'`operation` has the wrong type: Received `{type(params.get("operation", None))}` expected `str`')
    if params.get("image_out", None) is None:
        raise StyxValidationError("`image_out` must not be None")
    if not isinstance(params["image_out"], str):
        raise StyxValidationError(f'`image_out` has the wrong type: Received `{type(params.get("image_out", None))}` expected `str`')


def fixel2voxel_cargs(
    params: Fixel2voxelParamsDict,
    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("fixel2voxel")
    if params.get("number", None) is not None:
        cargs.extend([
            "-number",
            str(params.get("number", None))
        ])
    if params.get("fill", None) is not None:
        cargs.extend([
            "-fill",
            str(params.get("fill", None))
        ])
    if params.get("weighted", None) is not None:
        cargs.extend([
            "-weighted",
            execution.input_file(params.get("weighted", 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 [fixel2voxel_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("fixel_in", None)))
    cargs.append(params.get("operation", None))
    cargs.append(params.get("image_out", None))
    return cargs


def fixel2voxel_outputs(
    params: Fixel2voxelParamsDict,
    execution: Execution,
) -> Fixel2voxelOutputs:
    """
    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 = Fixel2voxelOutputs(
        root=execution.output_file("."),
        image_out=execution.output_file(params.get("image_out", None)),
    )
    return ret


def fixel2voxel_execute(
    params: Fixel2voxelParamsDict,
    runner: Runner | None = None,
) -> Fixel2voxelOutputs:
    """
    fixel2voxel
    
    Convert a fixel-based sparse-data image into some form of scalar image.
    
    Fixel data can be reduced to voxel data in a number of ways:
    
    - Some statistic computed across all fixel values within a voxel: mean, sum,
    product, min, max, absmax, magmax
    
    - The number of fixels in each voxel: count
    
    - Some measure of crossing-fibre organisation: complexity, sf
    ('single-fibre')
    
    - A 4D directionally-encoded colour image: dec_unit, dec_scaled
    
    - A 4D image containing all fixel data values in each voxel unmodified: none
    
    The -weighted option deals with the case where there is some per-fixel
    metric of interest that you wish to collapse into a single scalar measure
    per voxel, but each fixel possesses a different volume, and you wish for
    those fixels with greater volume to have a greater influence on the
    calculation than fixels with lesser volume. For instance, when estimating a
    voxel-based measure of mean axon diameter from per-fixel mean axon
    diameters, a fixel's mean axon diameter should be weigthed by its relative
    volume within the voxel in the calculation of that voxel mean.
    
    References:
    
    * Reference for 'complexity' operation:
    Riffert, T. W.; Schreiber, J.; Anwander, A. & Knosche, T. R. Beyond
    Fractional Anisotropy: Extraction of bundle-specific structural metrics from
    crossing fibre models. NeuroImage, 2014, 100, 176-191.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Fixel2voxelOutputs`).
    """
    fixel2voxel_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(FIXEL2VOXEL_METADATA)
    params = execution.params(params)
    cargs = fixel2voxel_cargs(params, execution)
    ret = fixel2voxel_outputs(params, execution)
    execution.run(cargs)
    return ret


def fixel2voxel(
    fixel_in: InputPathType,
    operation: str,
    image_out: str,
    number: int | None = None,
    fill: float | None = None,
    weighted: InputPathType | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Fixel2voxelConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> Fixel2voxelOutputs:
    """
    fixel2voxel
    
    Convert a fixel-based sparse-data image into some form of scalar image.
    
    Fixel data can be reduced to voxel data in a number of ways:
    
    - Some statistic computed across all fixel values within a voxel: mean, sum,
    product, min, max, absmax, magmax
    
    - The number of fixels in each voxel: count
    
    - Some measure of crossing-fibre organisation: complexity, sf
    ('single-fibre')
    
    - A 4D directionally-encoded colour image: dec_unit, dec_scaled
    
    - A 4D image containing all fixel data values in each voxel unmodified: none
    
    The -weighted option deals with the case where there is some per-fixel
    metric of interest that you wish to collapse into a single scalar measure
    per voxel, but each fixel possesses a different volume, and you wish for
    those fixels with greater volume to have a greater influence on the
    calculation than fixels with lesser volume. For instance, when estimating a
    voxel-based measure of mean axon diameter from per-fixel mean axon
    diameters, a fixel's mean axon diameter should be weigthed by its relative
    volume within the voxel in the calculation of that voxel mean.
    
    References:
    
    * Reference for 'complexity' operation:
    Riffert, T. W.; Schreiber, J.; Anwander, A. & Knosche, T. R. Beyond
    Fractional Anisotropy: Extraction of bundle-specific structural metrics from
    crossing fibre models. NeuroImage, 2014, 100, 176-191.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        fixel_in: the input fixel data file.
        operation: the operation to apply, one of: mean, sum, product, min,\
            max, absmax, magmax, count, complexity, sf, dec_unit, dec_scaled, none.
        image_out: the output scalar image.
        number: use only the largest N fixels in calculation of the voxel-wise\
            statistic; in the case of operation "none", output only the largest N\
            fixels in each voxel.
        fill: for "none" operation, specify the value to fill when number of\
            fixels is fewer than the maximum (default: 0.0).
        weighted: weight the contribution of each fixel to the per-voxel result\
            according to its volume.
        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 `Fixel2voxelOutputs`).
    """
    params = fixel2voxel_params(
        number=number,
        fill=fill,
        weighted=weighted,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        fixel_in=fixel_in,
        operation=operation,
        image_out=image_out,
    )
    return fixel2voxel_execute(params, runner)


__all__ = [
    "FIXEL2VOXEL_METADATA",
    "Fixel2voxelConfigParamsDict",
    "Fixel2voxelConfigParamsDictTagged",
    "Fixel2voxelOutputs",
    "Fixel2voxelParamsDict",
    "Fixel2voxelParamsDictTagged",
    "fixel2voxel",
    "fixel2voxel_config",
    "fixel2voxel_execute",
    "fixel2voxel_params",
]
