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

import typing
import pathlib
from styxdefs import *

ANTS_JOINT_LABEL_FUSION_SH_METADATA = Metadata(
    id="c1267bf8b624408f46319d6d24c21b8a6d4c38ca.boutiques",
    name="antsJointLabelFusion.sh",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_AntsJointLabelFusionShParamsDictNoTag = typing.TypedDict('_AntsJointLabelFusionShParamsDictNoTag', {
    "dimensionality": typing.NotRequired[typing.Literal[2, 3] | None],
    "output": typing.NotRequired[str | None],
    "atlas_image_mrf": typing.NotRequired[str | None],
    "atlas_segmentation_mrf": typing.NotRequired[str | None],
    "rigid_transform": typing.NotRequired[str | None],
    "similarity_metric": typing.NotRequired[str | None],
    "other_options": typing.NotRequired[str | None],
    "verbose": typing.NotRequired[bool | None],
    "target_image": str,
    "mask_image": str,
    "rigid_transform_additional_options": typing.NotRequired[str | None],
    "similarity_metric_additional_options": typing.NotRequired[str | None],
})
AntsJointLabelFusionShParamsDictTagged = typing.TypedDict('AntsJointLabelFusionShParamsDictTagged', {
    "@type": typing.Literal["ants/antsJointLabelFusion.sh"],
    "dimensionality": typing.NotRequired[typing.Literal[2, 3] | None],
    "output": typing.NotRequired[str | None],
    "atlas_image_mrf": typing.NotRequired[str | None],
    "atlas_segmentation_mrf": typing.NotRequired[str | None],
    "rigid_transform": typing.NotRequired[str | None],
    "similarity_metric": typing.NotRequired[str | None],
    "other_options": typing.NotRequired[str | None],
    "verbose": typing.NotRequired[bool | None],
    "target_image": str,
    "mask_image": str,
    "rigid_transform_additional_options": typing.NotRequired[str | None],
    "similarity_metric_additional_options": typing.NotRequired[str | None],
})
AntsJointLabelFusionShParamsDict = _AntsJointLabelFusionShParamsDictNoTag | AntsJointLabelFusionShParamsDictTagged


class AntsJointLabelFusionShOutputs(typing.NamedTuple):
    """
    Output object returned when calling `AntsJointLabelFusionShParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    segmentation_output: OutputPathType | None
    """Output segmented image."""


def ants_joint_label_fusion_sh_params(
    target_image: str,
    mask_image: str,
    dimensionality: typing.Literal[2, 3] | None = None,
    output: str | None = None,
    atlas_image_mrf: str | None = None,
    atlas_segmentation_mrf: str | None = None,
    rigid_transform: str | None = None,
    similarity_metric: str | None = None,
    other_options: str | None = None,
    verbose: bool | None = None,
    rigid_transform_additional_options: str | None = None,
    similarity_metric_additional_options: str | None = None,
) -> AntsJointLabelFusionShParamsDictTagged:
    """
    Build parameters.
    
    Args:
        target_image: Image to segment.
        mask_image: Mask image.
        dimensionality: Image dimensionality (2 or 3).
        output: Root directory for the output segmentation.
        atlas_image_mrf: Atlas image(s) to be used for MRF initialization.
        atlas_segmentation_mrf: Atlas segmentation(s) to be used for MRF\
            initialization.
        rigid_transform: Rigid transform initialization.
        similarity_metric: Metric used for calculating similarity.
        other_options: Additional options for label fusion.
        verbose: Verbose output.
        rigid_transform_additional_options: Additional options for rigid\
            transform.
        similarity_metric_additional_options: Additional options for similarity\
            metric.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/antsJointLabelFusion.sh",
        "target_image": target_image,
        "mask_image": mask_image,
    }
    if dimensionality is not None:
        params["dimensionality"] = dimensionality
    if output is not None:
        params["output"] = output
    if atlas_image_mrf is not None:
        params["atlas_image_mrf"] = atlas_image_mrf
    if atlas_segmentation_mrf is not None:
        params["atlas_segmentation_mrf"] = atlas_segmentation_mrf
    if rigid_transform is not None:
        params["rigid_transform"] = rigid_transform
    if similarity_metric is not None:
        params["similarity_metric"] = similarity_metric
    if other_options is not None:
        params["other_options"] = other_options
    if verbose is not None:
        params["verbose"] = verbose
    if rigid_transform_additional_options is not None:
        params["rigid_transform_additional_options"] = rigid_transform_additional_options
    if similarity_metric_additional_options is not None:
        params["similarity_metric_additional_options"] = similarity_metric_additional_options
    return params


def ants_joint_label_fusion_sh_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `AntsJointLabelFusionShParamsDict` 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("dimensionality", None) is not None:
        if not isinstance(params["dimensionality"], int):
            raise StyxValidationError(f'`dimensionality` has the wrong type: Received `{type(params.get("dimensionality", None))}` expected `typing.Literal[2, 3] | None`')
        if params["dimensionality"] not in [2, 3]:
            raise StyxValidationError("Parameter `dimensionality` must be one of [2, 3]")
    if params.get("output", None) is not None:
        if not isinstance(params["output"], str):
            raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `str | None`')
    if params.get("atlas_image_mrf", None) is not None:
        if not isinstance(params["atlas_image_mrf"], str):
            raise StyxValidationError(f'`atlas_image_mrf` has the wrong type: Received `{type(params.get("atlas_image_mrf", None))}` expected `str | None`')
    if params.get("atlas_segmentation_mrf", None) is not None:
        if not isinstance(params["atlas_segmentation_mrf"], str):
            raise StyxValidationError(f'`atlas_segmentation_mrf` has the wrong type: Received `{type(params.get("atlas_segmentation_mrf", None))}` expected `str | None`')
    if params.get("rigid_transform", None) is not None:
        if not isinstance(params["rigid_transform"], str):
            raise StyxValidationError(f'`rigid_transform` has the wrong type: Received `{type(params.get("rigid_transform", None))}` expected `str | None`')
    if params.get("similarity_metric", None) is not None:
        if not isinstance(params["similarity_metric"], str):
            raise StyxValidationError(f'`similarity_metric` has the wrong type: Received `{type(params.get("similarity_metric", None))}` expected `str | None`')
    if params.get("other_options", None) is not None:
        if not isinstance(params["other_options"], str):
            raise StyxValidationError(f'`other_options` has the wrong type: Received `{type(params.get("other_options", None))}` expected `str | None`')
    if params.get("verbose", None) is not None:
        if not isinstance(params["verbose"], bool):
            raise StyxValidationError(f'`verbose` has the wrong type: Received `{type(params.get("verbose", None))}` expected `bool | None`')
    if params.get("target_image", None) is None:
        raise StyxValidationError("`target_image` must not be None")
    if not isinstance(params["target_image"], str):
        raise StyxValidationError(f'`target_image` has the wrong type: Received `{type(params.get("target_image", None))}` expected `str`')
    if params.get("mask_image", None) is None:
        raise StyxValidationError("`mask_image` must not be None")
    if not isinstance(params["mask_image"], str):
        raise StyxValidationError(f'`mask_image` has the wrong type: Received `{type(params.get("mask_image", None))}` expected `str`')
    if params.get("rigid_transform_additional_options", None) is not None:
        if not isinstance(params["rigid_transform_additional_options"], str):
            raise StyxValidationError(f'`rigid_transform_additional_options` has the wrong type: Received `{type(params.get("rigid_transform_additional_options", None))}` expected `str | None`')
    if params.get("similarity_metric_additional_options", None) is not None:
        if not isinstance(params["similarity_metric_additional_options"], str):
            raise StyxValidationError(f'`similarity_metric_additional_options` has the wrong type: Received `{type(params.get("similarity_metric_additional_options", None))}` expected `str | None`')


def ants_joint_label_fusion_sh_cargs(
    params: AntsJointLabelFusionShParamsDict,
    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("antsJointLabelFusion.sh")
    if params.get("dimensionality", None) is not None:
        cargs.extend([
            "-d",
            str(params.get("dimensionality", None))
        ])
    if params.get("output", None) is not None:
        cargs.extend([
            "-o",
            params.get("output", None)
        ])
    if params.get("atlas_image_mrf", None) is not None:
        cargs.extend([
            "-a",
            params.get("atlas_image_mrf", None)
        ])
    if params.get("atlas_segmentation_mrf", None) is not None:
        cargs.extend([
            "-l",
            params.get("atlas_segmentation_mrf", None)
        ])
    if params.get("rigid_transform", None) is not None:
        cargs.extend([
            "-g",
            params.get("rigid_transform", None)
        ])
    if params.get("similarity_metric", None) is not None:
        cargs.extend([
            "-s",
            params.get("similarity_metric", None)
        ])
    if params.get("other_options", None) is not None:
        cargs.extend([
            "-k",
            params.get("other_options", None)
        ])
    if params.get("verbose", None) is not None:
        cargs.extend([
            "-v",
            ("1" if params.get("verbose", None) else "0")
        ])
    cargs.extend([
        "-i",
        params.get("target_image", None)
    ])
    cargs.extend([
        "-m",
        params.get("mask_image", None)
    ])
    if params.get("rigid_transform_additional_options", None) is not None:
        cargs.extend([
            "-g",
            params.get("rigid_transform_additional_options", None)
        ])
    if params.get("similarity_metric_additional_options", None) is not None:
        cargs.extend([
            "-x",
            params.get("similarity_metric_additional_options", None)
        ])
    return cargs


def ants_joint_label_fusion_sh_outputs(
    params: AntsJointLabelFusionShParamsDict,
    execution: Execution,
) -> AntsJointLabelFusionShOutputs:
    """
    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 = AntsJointLabelFusionShOutputs(
        root=execution.output_file("."),
        segmentation_output=execution.output_file(params.get("output", None) + ".nii.gz") if (params.get("output") is not None) else None,
    )
    return ret


def ants_joint_label_fusion_sh_execute(
    params: AntsJointLabelFusionShParamsDict,
    runner: Runner | None = None,
) -> AntsJointLabelFusionShOutputs:
    """
    antsJointLabelFusion.sh
    
    The antsJointLabelFusion script is used for performing label fusion using
    multiple atlases to improve segmentation accuracy.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsJointLabelFusionShOutputs`).
    """
    ants_joint_label_fusion_sh_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(ANTS_JOINT_LABEL_FUSION_SH_METADATA)
    params = execution.params(params)
    cargs = ants_joint_label_fusion_sh_cargs(params, execution)
    ret = ants_joint_label_fusion_sh_outputs(params, execution)
    execution.run(cargs)
    return ret


def ants_joint_label_fusion_sh(
    target_image: str,
    mask_image: str,
    dimensionality: typing.Literal[2, 3] | None = None,
    output: str | None = None,
    atlas_image_mrf: str | None = None,
    atlas_segmentation_mrf: str | None = None,
    rigid_transform: str | None = None,
    similarity_metric: str | None = None,
    other_options: str | None = None,
    verbose: bool | None = None,
    rigid_transform_additional_options: str | None = None,
    similarity_metric_additional_options: str | None = None,
    runner: Runner | None = None,
) -> AntsJointLabelFusionShOutputs:
    """
    antsJointLabelFusion.sh
    
    The antsJointLabelFusion script is used for performing label fusion using
    multiple atlases to improve segmentation accuracy.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        target_image: Image to segment.
        mask_image: Mask image.
        dimensionality: Image dimensionality (2 or 3).
        output: Root directory for the output segmentation.
        atlas_image_mrf: Atlas image(s) to be used for MRF initialization.
        atlas_segmentation_mrf: Atlas segmentation(s) to be used for MRF\
            initialization.
        rigid_transform: Rigid transform initialization.
        similarity_metric: Metric used for calculating similarity.
        other_options: Additional options for label fusion.
        verbose: Verbose output.
        rigid_transform_additional_options: Additional options for rigid\
            transform.
        similarity_metric_additional_options: Additional options for similarity\
            metric.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsJointLabelFusionShOutputs`).
    """
    params = ants_joint_label_fusion_sh_params(
        dimensionality=dimensionality,
        output=output,
        atlas_image_mrf=atlas_image_mrf,
        atlas_segmentation_mrf=atlas_segmentation_mrf,
        rigid_transform=rigid_transform,
        similarity_metric=similarity_metric,
        other_options=other_options,
        verbose=verbose,
        target_image=target_image,
        mask_image=mask_image,
        rigid_transform_additional_options=rigid_transform_additional_options,
        similarity_metric_additional_options=similarity_metric_additional_options,
    )
    return ants_joint_label_fusion_sh_execute(params, runner)


__all__ = [
    "ANTS_JOINT_LABEL_FUSION_SH_METADATA",
    "AntsJointLabelFusionShOutputs",
    "AntsJointLabelFusionShParamsDict",
    "AntsJointLabelFusionShParamsDictTagged",
    "ants_joint_label_fusion_sh",
    "ants_joint_label_fusion_sh_execute",
    "ants_joint_label_fusion_sh_params",
]
