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

import typing
import pathlib
from styxdefs import *

ANTSUSE_DEFORMATION_FIELD_TO_GET_AFFINE_TRANSFORM_METADATA = Metadata(
    id="33730edc7d31f068b95a970d8aa4ca5ece2c7a6f.boutiques",
    name="ANTSUseDeformationFieldToGetAffineTransform",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_AntsuseDeformationFieldToGetAffineTransformParamsDictNoTag = typing.TypedDict('_AntsuseDeformationFieldToGetAffineTransformParamsDictNoTag', {
    "deformation_field": InputPathType,
    "load_ratio": float,
    "transform_type": typing.Literal["rigid", "affine"],
    "output_affine": str,
    "mask": typing.NotRequired[InputPathType | None],
})
AntsuseDeformationFieldToGetAffineTransformParamsDictTagged = typing.TypedDict('AntsuseDeformationFieldToGetAffineTransformParamsDictTagged', {
    "@type": typing.Literal["ants/ANTSUseDeformationFieldToGetAffineTransform"],
    "deformation_field": InputPathType,
    "load_ratio": float,
    "transform_type": typing.Literal["rigid", "affine"],
    "output_affine": str,
    "mask": typing.NotRequired[InputPathType | None],
})
AntsuseDeformationFieldToGetAffineTransformParamsDict = _AntsuseDeformationFieldToGetAffineTransformParamsDictNoTag | AntsuseDeformationFieldToGetAffineTransformParamsDictTagged


class AntsuseDeformationFieldToGetAffineTransformOutputs(typing.NamedTuple):
    """
    Output object returned when calling `AntsuseDeformationFieldToGetAffineTransformParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    out_affine_txt: OutputPathType
    """The output is the affine transformation matrix file."""


def antsuse_deformation_field_to_get_affine_transform_params(
    deformation_field: InputPathType,
    load_ratio: float,
    transform_type: typing.Literal["rigid", "affine"],
    output_affine: str,
    mask: InputPathType | None = None,
) -> AntsuseDeformationFieldToGetAffineTransformParamsDictTagged:
    """
    Build parameters.
    
    Args:
        deformation_field: The input deformation field in NIfTI format (e.g.,\
            zzzWarp.nii.gz).
        load_ratio: Ratio of points to be loaded from deformation field to save\
            memory (ex: 0.01).
        transform_type: Type of transform to be extracted. Can be 'rigid' or\
            'affine'.
        output_affine: The output file where the affine transform will be saved\
            (e.g., OutAffine.txt).
        mask: Optional mask file defining the region from which points will be\
            selected.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/ANTSUseDeformationFieldToGetAffineTransform",
        "deformation_field": deformation_field,
        "load_ratio": load_ratio,
        "transform_type": transform_type,
        "output_affine": output_affine,
    }
    if mask is not None:
        params["mask"] = mask
    return params


def antsuse_deformation_field_to_get_affine_transform_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `AntsuseDeformationFieldToGetAffineTransformParamsDict` 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("deformation_field", None) is None:
        raise StyxValidationError("`deformation_field` must not be None")
    if not isinstance(params["deformation_field"], (pathlib.Path, str)):
        raise StyxValidationError(f'`deformation_field` has the wrong type: Received `{type(params.get("deformation_field", None))}` expected `InputPathType`')
    if params.get("load_ratio", None) is None:
        raise StyxValidationError("`load_ratio` must not be None")
    if not isinstance(params["load_ratio"], (float, int)):
        raise StyxValidationError(f'`load_ratio` has the wrong type: Received `{type(params.get("load_ratio", None))}` expected `float`')
    if params.get("transform_type", None) is None:
        raise StyxValidationError("`transform_type` must not be None")
    if not isinstance(params["transform_type"], str):
        raise StyxValidationError(f'`transform_type` has the wrong type: Received `{type(params.get("transform_type", None))}` expected `typing.Literal["rigid", "affine"]`')
    if params["transform_type"] not in ["rigid", "affine"]:
        raise StyxValidationError("Parameter `transform_type` must be one of [\"rigid\", \"affine\"]")
    if params.get("output_affine", None) is None:
        raise StyxValidationError("`output_affine` must not be None")
    if not isinstance(params["output_affine"], str):
        raise StyxValidationError(f'`output_affine` has the wrong type: Received `{type(params.get("output_affine", None))}` expected `str`')
    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`')


def antsuse_deformation_field_to_get_affine_transform_cargs(
    params: AntsuseDeformationFieldToGetAffineTransformParamsDict,
    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("ANTSUseDeformationFieldToGetAffineTransform")
    cargs.append(execution.input_file(params.get("deformation_field", None)))
    cargs.append(str(params.get("load_ratio", None)))
    cargs.append(params.get("transform_type", None))
    cargs.append(params.get("output_affine", None))
    if params.get("mask", None) is not None:
        cargs.append(execution.input_file(params.get("mask", None)))
    return cargs


def antsuse_deformation_field_to_get_affine_transform_outputs(
    params: AntsuseDeformationFieldToGetAffineTransformParamsDict,
    execution: Execution,
) -> AntsuseDeformationFieldToGetAffineTransformOutputs:
    """
    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 = AntsuseDeformationFieldToGetAffineTransformOutputs(
        root=execution.output_file("."),
        out_affine_txt=execution.output_file(params.get("output_affine", None)),
    )
    return ret


def antsuse_deformation_field_to_get_affine_transform_execute(
    params: AntsuseDeformationFieldToGetAffineTransformParamsDict,
    runner: Runner | None = None,
) -> AntsuseDeformationFieldToGetAffineTransformOutputs:
    """
    ANTSUseDeformationFieldToGetAffineTransform
    
    Extracts an affine transform from a deformation field. The input deformation
    field is expected to be in the same physical space as the images you want to
    transform.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsuseDeformationFieldToGetAffineTransformOutputs`).
    """
    antsuse_deformation_field_to_get_affine_transform_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(ANTSUSE_DEFORMATION_FIELD_TO_GET_AFFINE_TRANSFORM_METADATA)
    params = execution.params(params)
    cargs = antsuse_deformation_field_to_get_affine_transform_cargs(params, execution)
    ret = antsuse_deformation_field_to_get_affine_transform_outputs(params, execution)
    execution.run(cargs)
    return ret


def antsuse_deformation_field_to_get_affine_transform(
    deformation_field: InputPathType,
    load_ratio: float,
    transform_type: typing.Literal["rigid", "affine"],
    output_affine: str,
    mask: InputPathType | None = None,
    runner: Runner | None = None,
) -> AntsuseDeformationFieldToGetAffineTransformOutputs:
    """
    ANTSUseDeformationFieldToGetAffineTransform
    
    Extracts an affine transform from a deformation field. The input deformation
    field is expected to be in the same physical space as the images you want to
    transform.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        deformation_field: The input deformation field in NIfTI format (e.g.,\
            zzzWarp.nii.gz).
        load_ratio: Ratio of points to be loaded from deformation field to save\
            memory (ex: 0.01).
        transform_type: Type of transform to be extracted. Can be 'rigid' or\
            'affine'.
        output_affine: The output file where the affine transform will be saved\
            (e.g., OutAffine.txt).
        mask: Optional mask file defining the region from which points will be\
            selected.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsuseDeformationFieldToGetAffineTransformOutputs`).
    """
    params = antsuse_deformation_field_to_get_affine_transform_params(
        deformation_field=deformation_field,
        load_ratio=load_ratio,
        transform_type=transform_type,
        output_affine=output_affine,
        mask=mask,
    )
    return antsuse_deformation_field_to_get_affine_transform_execute(params, runner)


__all__ = [
    "ANTSUSE_DEFORMATION_FIELD_TO_GET_AFFINE_TRANSFORM_METADATA",
    "AntsuseDeformationFieldToGetAffineTransformOutputs",
    "AntsuseDeformationFieldToGetAffineTransformParamsDict",
    "AntsuseDeformationFieldToGetAffineTransformParamsDictTagged",
    "antsuse_deformation_field_to_get_affine_transform",
    "antsuse_deformation_field_to_get_affine_transform_execute",
    "antsuse_deformation_field_to_get_affine_transform_params",
]
