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

import typing
import pathlib
from styxdefs import *

ANTS_MOTION_CORR_STATS_METADATA = Metadata(
    id="51fc61f7cc00b9eb9222ce3cabc8dc3386c917fa.boutiques",
    name="antsMotionCorrStats",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_AntsMotionCorrStatsParamsDictNoTag = typing.TypedDict('_AntsMotionCorrStatsParamsDictNoTag', {
    "mask": InputPathType,
    "moco_params": InputPathType,
    "output": str,
    "transform_index": typing.NotRequired[int | None],
    "framewise": typing.NotRequired[bool | None],
    "spatial_map": bool,
    "timeseries_displacement": bool,
    "help": typing.NotRequired[bool | None],
})
AntsMotionCorrStatsParamsDictTagged = typing.TypedDict('AntsMotionCorrStatsParamsDictTagged', {
    "@type": typing.Literal["ants/antsMotionCorrStats"],
    "mask": InputPathType,
    "moco_params": InputPathType,
    "output": str,
    "transform_index": typing.NotRequired[int | None],
    "framewise": typing.NotRequired[bool | None],
    "spatial_map": bool,
    "timeseries_displacement": bool,
    "help": typing.NotRequired[bool | None],
})
AntsMotionCorrStatsParamsDict = _AntsMotionCorrStatsParamsDictNoTag | AntsMotionCorrStatsParamsDictTagged


class AntsMotionCorrStatsOutputs(typing.NamedTuple):
    """
    Output object returned when calling `AntsMotionCorrStatsParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    corrected_csv: OutputPathType
    """The corrected motion parameters.csv file."""


def ants_motion_corr_stats_params(
    mask: InputPathType,
    moco_params: InputPathType,
    output: str,
    transform_index: int | None = None,
    framewise: bool | None = None,
    spatial_map: bool = False,
    timeseries_displacement: bool = False,
    help_: bool | None = None,
) -> AntsMotionCorrStatsParamsDictTagged:
    """
    Build parameters.
    
    Args:
        mask: Mask image - compute displacements within mask.
        moco_params: Motion correction parameters from antsMotionCorr.
        output: Specify the output file.
        transform_index: Specify the index for a 3D transform to output.
        framewise: Do framewise summary stats.
        spatial_map: Output image of displacement magnitude.
        timeseries_displacement: Output 4d time-series image of displacement\
            magnitude.
        help_: Print the help menu. Short version with -h.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/antsMotionCorrStats",
        "mask": mask,
        "moco_params": moco_params,
        "output": output,
        "spatial_map": spatial_map,
        "timeseries_displacement": timeseries_displacement,
    }
    if transform_index is not None:
        params["transform_index"] = transform_index
    if framewise is not None:
        params["framewise"] = framewise
    if help_ is not None:
        params["help"] = help_
    return params


def ants_motion_corr_stats_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `AntsMotionCorrStatsParamsDict` 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("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("moco_params", None) is None:
        raise StyxValidationError("`moco_params` must not be None")
    if not isinstance(params["moco_params"], (pathlib.Path, str)):
        raise StyxValidationError(f'`moco_params` has the wrong type: Received `{type(params.get("moco_params", 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`')
    if params.get("transform_index", None) is not None:
        if not isinstance(params["transform_index"], int):
            raise StyxValidationError(f'`transform_index` has the wrong type: Received `{type(params.get("transform_index", None))}` expected `int | None`')
    if params.get("framewise", None) is not None:
        if not isinstance(params["framewise"], bool):
            raise StyxValidationError(f'`framewise` has the wrong type: Received `{type(params.get("framewise", None))}` expected `bool | None`')
    if params.get("spatial_map", False) is None:
        raise StyxValidationError("`spatial_map` must not be None")
    if not isinstance(params["spatial_map"], bool):
        raise StyxValidationError(f'`spatial_map` has the wrong type: Received `{type(params.get("spatial_map", False))}` expected `bool`')
    if params.get("timeseries_displacement", False) is None:
        raise StyxValidationError("`timeseries_displacement` must not be None")
    if not isinstance(params["timeseries_displacement"], bool):
        raise StyxValidationError(f'`timeseries_displacement` has the wrong type: Received `{type(params.get("timeseries_displacement", False))}` expected `bool`')
    if params.get("help", None) is not None:
        if not isinstance(params["help"], bool):
            raise StyxValidationError(f'`help` has the wrong type: Received `{type(params.get("help", None))}` expected `bool | None`')


def ants_motion_corr_stats_cargs(
    params: AntsMotionCorrStatsParamsDict,
    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("antsMotionCorrStats")
    cargs.extend([
        "-x",
        execution.input_file(params.get("mask", None))
    ])
    cargs.extend([
        "-m",
        execution.input_file(params.get("moco_params", None))
    ])
    cargs.extend([
        "-o",
        params.get("output", None)
    ])
    if params.get("transform_index", None) is not None:
        cargs.extend([
            "-t",
            str(params.get("transform_index", None))
        ])
    if params.get("framewise", None) is not None:
        cargs.extend([
            "-f",
            ("1" if params.get("framewise", None) else "0")
        ])
    if params.get("spatial_map", False):
        cargs.append("-s")
    if params.get("timeseries_displacement", False):
        cargs.append("-d")
    if params.get("help", None) is not None:
        cargs.extend([
            "--help",
            ("1" if params.get("help", None) else "0")
        ])
    return cargs


def ants_motion_corr_stats_outputs(
    params: AntsMotionCorrStatsParamsDict,
    execution: Execution,
) -> AntsMotionCorrStatsOutputs:
    """
    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 = AntsMotionCorrStatsOutputs(
        root=execution.output_file("."),
        corrected_csv=execution.output_file(params.get("output", None)),
    )
    return ret


def ants_motion_corr_stats_execute(
    params: AntsMotionCorrStatsParamsDict,
    runner: Runner | None = None,
) -> AntsMotionCorrStatsOutputs:
    """
    antsMotionCorrStats
    
    Create summary measures of the parameters that are output by antsMotionCorr.
    Currently only works for linear transforms. Outputs the mean and max
    displacements for the voxels within a provided mask, at each time point. By
    default the displacements are relative to the reference space, but the
    framewise option may be used to provide displacements between consecutive
    time points.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsMotionCorrStatsOutputs`).
    """
    ants_motion_corr_stats_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(ANTS_MOTION_CORR_STATS_METADATA)
    params = execution.params(params)
    cargs = ants_motion_corr_stats_cargs(params, execution)
    ret = ants_motion_corr_stats_outputs(params, execution)
    execution.run(cargs)
    return ret


def ants_motion_corr_stats(
    mask: InputPathType,
    moco_params: InputPathType,
    output: str,
    transform_index: int | None = None,
    framewise: bool | None = None,
    spatial_map: bool = False,
    timeseries_displacement: bool = False,
    help_: bool | None = None,
    runner: Runner | None = None,
) -> AntsMotionCorrStatsOutputs:
    """
    antsMotionCorrStats
    
    Create summary measures of the parameters that are output by antsMotionCorr.
    Currently only works for linear transforms. Outputs the mean and max
    displacements for the voxels within a provided mask, at each time point. By
    default the displacements are relative to the reference space, but the
    framewise option may be used to provide displacements between consecutive
    time points.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        mask: Mask image - compute displacements within mask.
        moco_params: Motion correction parameters from antsMotionCorr.
        output: Specify the output file.
        transform_index: Specify the index for a 3D transform to output.
        framewise: Do framewise summary stats.
        spatial_map: Output image of displacement magnitude.
        timeseries_displacement: Output 4d time-series image of displacement\
            magnitude.
        help_: Print the help menu. Short version with -h.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsMotionCorrStatsOutputs`).
    """
    params = ants_motion_corr_stats_params(
        mask=mask,
        moco_params=moco_params,
        output=output,
        transform_index=transform_index,
        framewise=framewise,
        spatial_map=spatial_map,
        timeseries_displacement=timeseries_displacement,
        help_=help_,
    )
    return ants_motion_corr_stats_execute(params, runner)


__all__ = [
    "ANTS_MOTION_CORR_STATS_METADATA",
    "AntsMotionCorrStatsOutputs",
    "AntsMotionCorrStatsParamsDict",
    "AntsMotionCorrStatsParamsDictTagged",
    "ants_motion_corr_stats",
    "ants_motion_corr_stats_execute",
    "ants_motion_corr_stats_params",
]
