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

import typing
import pathlib
from styxdefs import *

ANTSJACOBIAN_METADATA = Metadata(
    id="d3e1548784785ff25ca205e88e434be38d8b149c.boutiques",
    name="ANTSJacobian",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_AntsjacobianParamsDictNoTag = typing.TypedDict('_AntsjacobianParamsDictNoTag', {
    "imagedim": int,
    "gwarp": InputPathType,
    "outfile": str,
    "uselog": int,
    "maskfn": InputPathType,
    "normbytotalbool": int,
    "projectionvector": typing.NotRequired[str | None],
})
AntsjacobianParamsDictTagged = typing.TypedDict('AntsjacobianParamsDictTagged', {
    "@type": typing.Literal["ants/ANTSJacobian"],
    "imagedim": int,
    "gwarp": InputPathType,
    "outfile": str,
    "uselog": int,
    "maskfn": InputPathType,
    "normbytotalbool": int,
    "projectionvector": typing.NotRequired[str | None],
})
AntsjacobianParamsDict = _AntsjacobianParamsDictNoTag | AntsjacobianParamsDictTagged


class AntsjacobianOutputs(typing.NamedTuple):
    """
    Output object returned when calling `AntsjacobianParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    jacobian_output: OutputPathType
    """Output file containing the Jacobian determinant information."""


def antsjacobian_params(
    imagedim: int,
    gwarp: InputPathType,
    outfile: str,
    uselog: int,
    maskfn: InputPathType,
    normbytotalbool: int,
    projectionvector: str | None = None,
) -> AntsjacobianParamsDictTagged:
    """
    Build parameters.
    
    Args:
        imagedim: The dimensionality of the input image.
        gwarp: The input warp image.
        outfile: The prefix for the output files.
        uselog: Whether to use logarithm in computation.
        maskfn: Mask file used in the computation.
        normbytotalbool: Normalize the Jacobian by the total in the mask. Use\
            this to adjust for head size.
        projectionvector: Projects the warp along the specified direction. Do\
            not add this option if no projection is desired.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/ANTSJacobian",
        "imagedim": imagedim,
        "gwarp": gwarp,
        "outfile": outfile,
        "uselog": uselog,
        "maskfn": maskfn,
        "normbytotalbool": normbytotalbool,
    }
    if projectionvector is not None:
        params["projectionvector"] = projectionvector
    return params


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


def antsjacobian_cargs(
    params: AntsjacobianParamsDict,
    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("ANTSJacobian")
    cargs.append(str(params.get("imagedim", None)))
    cargs.append(execution.input_file(params.get("gwarp", None)))
    cargs.append(params.get("outfile", None))
    cargs.append(str(params.get("uselog", None)))
    cargs.append(execution.input_file(params.get("maskfn", None)))
    cargs.append(str(params.get("normbytotalbool", None)))
    if params.get("projectionvector", None) is not None:
        cargs.append(params.get("projectionvector", None))
    return cargs


def antsjacobian_outputs(
    params: AntsjacobianParamsDict,
    execution: Execution,
) -> AntsjacobianOutputs:
    """
    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 = AntsjacobianOutputs(
        root=execution.output_file("."),
        jacobian_output=execution.output_file(params.get("outfile", None) + "Jacobian.nii.gz"),
    )
    return ret


def antsjacobian_execute(
    params: AntsjacobianParamsDict,
    runner: Runner | None = None,
) -> AntsjacobianOutputs:
    """
    ANTSJacobian
    
    Calculate the Jacobian determinant of a transformation using ANTs. WARNING:
    ANTSJacobian may not be working correctly; see
    CreateJacobianDeterminantImage for an alternative method.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsjacobianOutputs`).
    """
    antsjacobian_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(ANTSJACOBIAN_METADATA)
    params = execution.params(params)
    cargs = antsjacobian_cargs(params, execution)
    ret = antsjacobian_outputs(params, execution)
    execution.run(cargs)
    return ret


def antsjacobian(
    imagedim: int,
    gwarp: InputPathType,
    outfile: str,
    uselog: int,
    maskfn: InputPathType,
    normbytotalbool: int,
    projectionvector: str | None = None,
    runner: Runner | None = None,
) -> AntsjacobianOutputs:
    """
    ANTSJacobian
    
    Calculate the Jacobian determinant of a transformation using ANTs. WARNING:
    ANTSJacobian may not be working correctly; see
    CreateJacobianDeterminantImage for an alternative method.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        imagedim: The dimensionality of the input image.
        gwarp: The input warp image.
        outfile: The prefix for the output files.
        uselog: Whether to use logarithm in computation.
        maskfn: Mask file used in the computation.
        normbytotalbool: Normalize the Jacobian by the total in the mask. Use\
            this to adjust for head size.
        projectionvector: Projects the warp along the specified direction. Do\
            not add this option if no projection is desired.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsjacobianOutputs`).
    """
    params = antsjacobian_params(
        imagedim=imagedim,
        gwarp=gwarp,
        outfile=outfile,
        uselog=uselog,
        maskfn=maskfn,
        normbytotalbool=normbytotalbool,
        projectionvector=projectionvector,
    )
    return antsjacobian_execute(params, runner)


__all__ = [
    "ANTSJACOBIAN_METADATA",
    "AntsjacobianOutputs",
    "AntsjacobianParamsDict",
    "AntsjacobianParamsDictTagged",
    "antsjacobian",
    "antsjacobian_execute",
    "antsjacobian_params",
]
