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

import typing
import pathlib
from styxdefs import *

ANTS_SLICE_REGULARIZED_REGISTRATION_METADATA = Metadata(
    id="f5c4948fa2560385a828a453a54af7e3a4ab4be7.boutiques",
    name="antsSliceRegularizedRegistration",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


AntsSliceRegularizedRegistrationParameters = typing.TypedDict('AntsSliceRegularizedRegistrationParameters', {
    "__STYX_TYPE__": typing.Literal["antsSliceRegularizedRegistration"],
    "polydegree": int,
    "output": str,
    "metric": str,
    "transform": str,
    "iterations": str,
    "shrink_factors": str,
    "smoothing_sigmas": str,
    "mask": typing.NotRequired[InputPathType | None],
    "interpolation": typing.NotRequired[typing.Literal["Linear", "NearestNeighbor", "MultiLabel", "Gaussian", "BSpline", "CosineWindowedSinc", "WelchWindowedSinc", "HammingWindowedSinc", "LanczosWindowedSinc", "GenericLabel"] | None],
    "verbose": typing.NotRequired[typing.Literal[0] | None],
})


def dyn_cargs(
    t: str,
) -> typing.Any:
    """
    Get build cargs function by command type.
    
    Args:
        t: Command type.
    Returns:
        Build cargs function.
    """
    return {
        "antsSliceRegularizedRegistration": ants_slice_regularized_registration_cargs,
    }.get(t)


def dyn_outputs(
    t: str,
) -> typing.Any:
    """
    Get build outputs function by command type.
    
    Args:
        t: Command type.
    Returns:
        Build outputs function.
    """
    return {
        "antsSliceRegularizedRegistration": ants_slice_regularized_registration_outputs,
    }.get(t)


class AntsSliceRegularizedRegistrationOutputs(typing.NamedTuple):
    """
    Output object returned when calling `ants_slice_regularized_registration(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    polynomial_fit: OutputPathType
    """Output is the polynomial fit to Tx & Ty."""
    transformed_image: OutputPathType
    """Output is the transformed image."""


def ants_slice_regularized_registration_params(
    polydegree: int,
    output: str,
    metric: str,
    transform: str,
    iterations: str,
    shrink_factors: str,
    smoothing_sigmas: str,
    mask: InputPathType | None = None,
    interpolation: typing.Literal["Linear", "NearestNeighbor", "MultiLabel", "Gaussian", "BSpline", "CosineWindowedSinc", "WelchWindowedSinc", "HammingWindowedSinc", "LanczosWindowedSinc", "GenericLabel"] | None = None,
    verbose: typing.Literal[0] | None = None,
) -> AntsSliceRegularizedRegistrationParameters:
    """
    Build parameters.
    
    Args:
        polydegree: Degree of polynomial up to zDimension-2. Controls the\
            polynomial degree. 0 means no regularization.
        output: Specify the output transform prefix (output format is .nii.gz).\
            Optionally, one can choose to warp the moving image to the fixed space,\
            and if the inverse transform exists, one can also output the warped\
            fixed image.
        metric: Four image metrics are available: GC: global correlation, CC:\
            ANTS neighborhood cross correlation, MI: Mutual information, and\
            MeanSquares: mean-squares intensity difference.
        transform: Several transform options are available. The gradientStep or\
            learningRate characterizes the gradient descent optimization.
        iterations: Specify the number of iterations at each level.
        shrink_factors: Specify the shrink factor for the virtual domain\
            (typically the fixed image) at each level.
        smoothing_sigmas: Specify the amount of smoothing at each level.
        mask: Fixed image mask to limit voxels considered by the metric.
        interpolation: Several interpolation options are available in ITK.
        verbose: Verbose option.
    Returns:
        Parameter dictionary
    """
    params = {
        "__STYXTYPE__": "antsSliceRegularizedRegistration",
        "polydegree": polydegree,
        "output": output,
        "metric": metric,
        "transform": transform,
        "iterations": iterations,
        "shrink_factors": shrink_factors,
        "smoothing_sigmas": smoothing_sigmas,
    }
    if mask is not None:
        params["mask"] = mask
    if interpolation is not None:
        params["interpolation"] = interpolation
    if verbose is not None:
        params["verbose"] = verbose
    return params


def ants_slice_regularized_registration_cargs(
    params: AntsSliceRegularizedRegistrationParameters,
    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("antsSliceRegularizedRegistration")
    cargs.extend([
        "-p",
        str(params.get("polydegree"))
    ])
    cargs.extend([
        "-o",
        params.get("output")
    ])
    cargs.extend([
        "-m",
        params.get("metric")
    ])
    cargs.extend([
        "-t",
        params.get("transform")
    ])
    cargs.extend([
        "-i",
        params.get("iterations")
    ])
    cargs.extend([
        "-f",
        params.get("shrink_factors")
    ])
    cargs.extend([
        "-s",
        params.get("smoothing_sigmas")
    ])
    if params.get("mask") is not None:
        cargs.extend([
            "-x",
            execution.input_file(params.get("mask"))
        ])
    if params.get("interpolation") is not None:
        cargs.extend([
            "-n",
            params.get("interpolation")
        ])
    if params.get("verbose") is not None:
        cargs.extend([
            "-v",
            str(params.get("verbose"))
        ])
    return cargs


def ants_slice_regularized_registration_outputs(
    params: AntsSliceRegularizedRegistrationParameters,
    execution: Execution,
) -> AntsSliceRegularizedRegistrationOutputs:
    """
    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 = AntsSliceRegularizedRegistrationOutputs(
        root=execution.output_file("."),
        polynomial_fit=execution.output_file("[OUTPUT_PREFIX]TxTy_poly.csv"),
        transformed_image=execution.output_file("[OUTPUT_PREFIX].nii.gz"),
    )
    return ret


def ants_slice_regularized_registration_execute(
    params: AntsSliceRegularizedRegistrationParameters,
    execution: Execution,
) -> AntsSliceRegularizedRegistrationOutputs:
    """
    This program is a user-level application for slice-by-slice translation
    registration. Results are regularized in z using polynomial regression. The
    program is targeted at spinal cord MRI. Only one stage is supported where a
    stage consists of a transform; an image metric; and iterations, shrink factors,
    and smoothing sigmas for each level. Specialized for 3D data: fixed image is 3D,
    moving image is 3D. Registration is performed slice-by-slice then regularized in
    z. The parameter -p controls the polynomial degree. -p 0 means no
    regularization.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        execution: The execution object.
    Returns:
        NamedTuple of outputs (described in `AntsSliceRegularizedRegistrationOutputs`).
    """
    params = execution.params(params)
    cargs = ants_slice_regularized_registration_cargs(params, execution)
    ret = ants_slice_regularized_registration_outputs(params, execution)
    execution.run(cargs)
    return ret


def ants_slice_regularized_registration(
    polydegree: int,
    output: str,
    metric: str,
    transform: str,
    iterations: str,
    shrink_factors: str,
    smoothing_sigmas: str,
    mask: InputPathType | None = None,
    interpolation: typing.Literal["Linear", "NearestNeighbor", "MultiLabel", "Gaussian", "BSpline", "CosineWindowedSinc", "WelchWindowedSinc", "HammingWindowedSinc", "LanczosWindowedSinc", "GenericLabel"] | None = None,
    verbose: typing.Literal[0] | None = None,
    runner: Runner | None = None,
) -> AntsSliceRegularizedRegistrationOutputs:
    """
    This program is a user-level application for slice-by-slice translation
    registration. Results are regularized in z using polynomial regression. The
    program is targeted at spinal cord MRI. Only one stage is supported where a
    stage consists of a transform; an image metric; and iterations, shrink factors,
    and smoothing sigmas for each level. Specialized for 3D data: fixed image is 3D,
    moving image is 3D. Registration is performed slice-by-slice then regularized in
    z. The parameter -p controls the polynomial degree. -p 0 means no
    regularization.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        polydegree: Degree of polynomial up to zDimension-2. Controls the\
            polynomial degree. 0 means no regularization.
        output: Specify the output transform prefix (output format is .nii.gz).\
            Optionally, one can choose to warp the moving image to the fixed space,\
            and if the inverse transform exists, one can also output the warped\
            fixed image.
        metric: Four image metrics are available: GC: global correlation, CC:\
            ANTS neighborhood cross correlation, MI: Mutual information, and\
            MeanSquares: mean-squares intensity difference.
        transform: Several transform options are available. The gradientStep or\
            learningRate characterizes the gradient descent optimization.
        iterations: Specify the number of iterations at each level.
        shrink_factors: Specify the shrink factor for the virtual domain\
            (typically the fixed image) at each level.
        smoothing_sigmas: Specify the amount of smoothing at each level.
        mask: Fixed image mask to limit voxels considered by the metric.
        interpolation: Several interpolation options are available in ITK.
        verbose: Verbose option.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsSliceRegularizedRegistrationOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(ANTS_SLICE_REGULARIZED_REGISTRATION_METADATA)
    params = ants_slice_regularized_registration_params(
        polydegree=polydegree,
        output=output,
        metric=metric,
        transform=transform,
        iterations=iterations,
        shrink_factors=shrink_factors,
        smoothing_sigmas=smoothing_sigmas,
        mask=mask,
        interpolation=interpolation,
        verbose=verbose,
    )
    return ants_slice_regularized_registration_execute(params, execution)


__all__ = [
    "ANTS_SLICE_REGULARIZED_REGISTRATION_METADATA",
    "AntsSliceRegularizedRegistrationOutputs",
    "AntsSliceRegularizedRegistrationParameters",
    "ants_slice_regularized_registration",
    "ants_slice_regularized_registration_params",
]
