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

import typing
import pathlib
from styxdefs import *

ANTS_BRAIN_EXTRACTION_SH_METADATA = Metadata(
    id="72830e1ccd1043926fd7dcd995dc980ded440138.boutiques",
    name="antsBrainExtraction.sh",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_AntsBrainExtractionShParamsDictNoTag = typing.TypedDict('_AntsBrainExtractionShParamsDictNoTag', {
    "image_dimension": typing.Literal[2, 3],
    "anatomical_image": InputPathType,
    "template": InputPathType,
    "probability_mask": InputPathType,
    "tissue_classification": typing.NotRequired[str | None],
    "brain_extraction_registration_mask": typing.NotRequired[InputPathType | None],
    "keep_temporary_files": typing.NotRequired[bool | None],
    "single_floating_point_precision": typing.NotRequired[bool | None],
    "initial_moving_transform": typing.NotRequired[InputPathType | None],
    "rotation_search_params": typing.NotRequired[str | None],
    "image_file_suffix": typing.NotRequired[str | None],
    "translation_search_params": typing.NotRequired[str | None],
    "random_seeding": typing.NotRequired[bool | None],
    "debug_mode": typing.NotRequired[int | None],
    "output_prefix": typing.NotRequired[str | None],
})
AntsBrainExtractionShParamsDictTagged = typing.TypedDict('AntsBrainExtractionShParamsDictTagged', {
    "@type": typing.Literal["ants/antsBrainExtraction.sh"],
    "image_dimension": typing.Literal[2, 3],
    "anatomical_image": InputPathType,
    "template": InputPathType,
    "probability_mask": InputPathType,
    "tissue_classification": typing.NotRequired[str | None],
    "brain_extraction_registration_mask": typing.NotRequired[InputPathType | None],
    "keep_temporary_files": typing.NotRequired[bool | None],
    "single_floating_point_precision": typing.NotRequired[bool | None],
    "initial_moving_transform": typing.NotRequired[InputPathType | None],
    "rotation_search_params": typing.NotRequired[str | None],
    "image_file_suffix": typing.NotRequired[str | None],
    "translation_search_params": typing.NotRequired[str | None],
    "random_seeding": typing.NotRequired[bool | None],
    "debug_mode": typing.NotRequired[int | None],
    "output_prefix": typing.NotRequired[str | None],
})
AntsBrainExtractionShParamsDict = _AntsBrainExtractionShParamsDictNoTag | AntsBrainExtractionShParamsDictTagged


class AntsBrainExtractionShOutputs(typing.NamedTuple):
    """
    Output object returned when calling `AntsBrainExtractionShParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    brain_extracted_image: OutputPathType | None
    """Brain extracted image"""
    brain_mask: OutputPathType | None
    """Brain extraction mask"""
    brain_segmentation: OutputPathType | None
    """Brain segmentation (tissue classification)"""
    csf_segmentation: OutputPathType | None
    """Cerebrospinal fluid segmentation"""
    gm_segmentation: OutputPathType | None
    """Gray matter segmentation"""
    wm_segmentation: OutputPathType | None
    """White matter segmentation"""
    generic_affine: OutputPathType | None
    """Generic affine transform from registration"""
    warp_field: OutputPathType | None
    """Warp field from registration"""
    inverse_warp_field: OutputPathType | None
    """Inverse warp field from registration"""


def ants_brain_extraction_sh_params(
    image_dimension: typing.Literal[2, 3],
    anatomical_image: InputPathType,
    template: InputPathType,
    probability_mask: InputPathType,
    tissue_classification: str | None = None,
    brain_extraction_registration_mask: InputPathType | None = None,
    keep_temporary_files: bool | None = None,
    single_floating_point_precision: bool | None = None,
    initial_moving_transform: InputPathType | None = None,
    rotation_search_params: str | None = None,
    image_file_suffix: str | None = None,
    translation_search_params: str | None = None,
    random_seeding: bool | None = None,
    debug_mode: int | None = None,
    output_prefix: str | None = None,
) -> AntsBrainExtractionShParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image_dimension: Image dimension (2 or 3 for 2- or 3-dimensional image).
        anatomical_image: Anatomical image (Structural image, typically T1). If\
            more than one anatomical image is specified, subsequently specified\
            images are used during the segmentation process. However, only the\
            first image is used in the registration of priors.
        template: Brain extraction template (Anatomical template).
        probability_mask: Brain extraction probability mask (with intensity\
            range 1=definitely brain to 0=definitely background).
        tissue_classification: Tissue classification - A k-means segmentation\
            to find tissue classes. Format: KxcsfLabelxgmLabelxwmLabel. Examples:\
            '3x1x2x3' for T1 (K=3, CSF=1, GM=2, WM=3), '3x3x2x1' for T2, '3x1x3x2'\
            for FLAIR, '4x4x2x3' for K=4.
        brain_extraction_registration_mask: Brain extraction registration mask\
            (Mask used for registration to limit the metric computation to a\
            specific region).
        keep_temporary_files: Keep temporary files (keep brain\
            extraction/segmentation warps, etc). 0=delete, 1=keep.
        single_floating_point_precision: Use antsRegistration with single (1)\
            or double (0) floating point precision.
        initial_moving_transform: Initial moving transform (An ITK affine\
            transform, e.g., from antsAI or ITK-SNAP). Without this option, the\
            script calls antsAI to search for a good initial moving transform.
        rotation_search_params: Rotation search parameters for antsAI in format\
            'step,arcFraction'. Step is in degrees, arcFraction goes from 0 (no\
            search) to 1 (search -180 to 180 degrees).
        image_file_suffix: Image file suffix (any standard ITK IO format, e.g.,\
            nrrd, nii.gz, mhd).
        translation_search_params: Translation search parameters for antsAI in\
            format 'step,range'. Step is in mm, -range to range will be tested in\
            each dimension.
        random_seeding: Use random seeding: 1=use random seed from system\
            clock, 0=use fixed seed (ANTS_RANDOM_SEED=19650218 or value from\
            environment). For reproducibility, set to 0 and also set\
            ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1.
        debug_mode: Test/debug mode: If > 0, runs a faster version of the\
            script. Only for debugging, results will not be good.
        output_prefix: Output prefix (directory + file prefix).
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/antsBrainExtraction.sh",
        "image_dimension": image_dimension,
        "anatomical_image": anatomical_image,
        "template": template,
        "probability_mask": probability_mask,
    }
    if tissue_classification is not None:
        params["tissue_classification"] = tissue_classification
    if brain_extraction_registration_mask is not None:
        params["brain_extraction_registration_mask"] = brain_extraction_registration_mask
    if keep_temporary_files is not None:
        params["keep_temporary_files"] = keep_temporary_files
    if single_floating_point_precision is not None:
        params["single_floating_point_precision"] = single_floating_point_precision
    if initial_moving_transform is not None:
        params["initial_moving_transform"] = initial_moving_transform
    if rotation_search_params is not None:
        params["rotation_search_params"] = rotation_search_params
    if image_file_suffix is not None:
        params["image_file_suffix"] = image_file_suffix
    if translation_search_params is not None:
        params["translation_search_params"] = translation_search_params
    if random_seeding is not None:
        params["random_seeding"] = random_seeding
    if debug_mode is not None:
        params["debug_mode"] = debug_mode
    if output_prefix is not None:
        params["output_prefix"] = output_prefix
    return params


def ants_brain_extraction_sh_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `AntsBrainExtractionShParamsDict` 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("image_dimension", None) is None:
        raise StyxValidationError("`image_dimension` must not be None")
    if not isinstance(params["image_dimension"], int):
        raise StyxValidationError(f'`image_dimension` has the wrong type: Received `{type(params.get("image_dimension", None))}` expected `typing.Literal[2, 3]`')
    if params["image_dimension"] not in [2, 3]:
        raise StyxValidationError("Parameter `image_dimension` must be one of [2, 3]")
    if params.get("anatomical_image", None) is None:
        raise StyxValidationError("`anatomical_image` must not be None")
    if not isinstance(params["anatomical_image"], (pathlib.Path, str)):
        raise StyxValidationError(f'`anatomical_image` has the wrong type: Received `{type(params.get("anatomical_image", None))}` expected `InputPathType`')
    if params.get("template", None) is None:
        raise StyxValidationError("`template` must not be None")
    if not isinstance(params["template"], (pathlib.Path, str)):
        raise StyxValidationError(f'`template` has the wrong type: Received `{type(params.get("template", None))}` expected `InputPathType`')
    if params.get("probability_mask", None) is None:
        raise StyxValidationError("`probability_mask` must not be None")
    if not isinstance(params["probability_mask"], (pathlib.Path, str)):
        raise StyxValidationError(f'`probability_mask` has the wrong type: Received `{type(params.get("probability_mask", None))}` expected `InputPathType`')
    if params.get("tissue_classification", None) is not None:
        if not isinstance(params["tissue_classification"], str):
            raise StyxValidationError(f'`tissue_classification` has the wrong type: Received `{type(params.get("tissue_classification", None))}` expected `str | None`')
    if params.get("brain_extraction_registration_mask", None) is not None:
        if not isinstance(params["brain_extraction_registration_mask"], (pathlib.Path, str)):
            raise StyxValidationError(f'`brain_extraction_registration_mask` has the wrong type: Received `{type(params.get("brain_extraction_registration_mask", None))}` expected `InputPathType | None`')
    if params.get("keep_temporary_files", None) is not None:
        if not isinstance(params["keep_temporary_files"], bool):
            raise StyxValidationError(f'`keep_temporary_files` has the wrong type: Received `{type(params.get("keep_temporary_files", None))}` expected `bool | None`')
    if params.get("single_floating_point_precision", None) is not None:
        if not isinstance(params["single_floating_point_precision"], bool):
            raise StyxValidationError(f'`single_floating_point_precision` has the wrong type: Received `{type(params.get("single_floating_point_precision", None))}` expected `bool | None`')
    if params.get("initial_moving_transform", None) is not None:
        if not isinstance(params["initial_moving_transform"], (pathlib.Path, str)):
            raise StyxValidationError(f'`initial_moving_transform` has the wrong type: Received `{type(params.get("initial_moving_transform", None))}` expected `InputPathType | None`')
    if params.get("rotation_search_params", None) is not None:
        if not isinstance(params["rotation_search_params"], str):
            raise StyxValidationError(f'`rotation_search_params` has the wrong type: Received `{type(params.get("rotation_search_params", None))}` expected `str | None`')
    if params.get("image_file_suffix", None) is not None:
        if not isinstance(params["image_file_suffix"], str):
            raise StyxValidationError(f'`image_file_suffix` has the wrong type: Received `{type(params.get("image_file_suffix", None))}` expected `str | None`')
    if params.get("translation_search_params", None) is not None:
        if not isinstance(params["translation_search_params"], str):
            raise StyxValidationError(f'`translation_search_params` has the wrong type: Received `{type(params.get("translation_search_params", None))}` expected `str | None`')
    if params.get("random_seeding", None) is not None:
        if not isinstance(params["random_seeding"], bool):
            raise StyxValidationError(f'`random_seeding` has the wrong type: Received `{type(params.get("random_seeding", None))}` expected `bool | None`')
    if params.get("debug_mode", None) is not None:
        if not isinstance(params["debug_mode"], int):
            raise StyxValidationError(f'`debug_mode` has the wrong type: Received `{type(params.get("debug_mode", None))}` expected `int | None`')
        if params["debug_mode"] < 0:
            raise StyxValidationError("Parameter `debug_mode` must be at least 0")
    if params.get("output_prefix", None) is not None:
        if not isinstance(params["output_prefix"], str):
            raise StyxValidationError(f'`output_prefix` has the wrong type: Received `{type(params.get("output_prefix", None))}` expected `str | None`')


def ants_brain_extraction_sh_cargs(
    params: AntsBrainExtractionShParamsDict,
    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("antsBrainExtraction.sh")
    cargs.extend([
        "-d",
        str(params.get("image_dimension", None))
    ])
    cargs.extend([
        "-a",
        execution.input_file(params.get("anatomical_image", None))
    ])
    cargs.extend([
        "-e",
        execution.input_file(params.get("template", None))
    ])
    cargs.extend([
        "-m",
        execution.input_file(params.get("probability_mask", None))
    ])
    if params.get("tissue_classification", None) is not None:
        cargs.extend([
            "-c",
            params.get("tissue_classification", None)
        ])
    if params.get("brain_extraction_registration_mask", None) is not None:
        cargs.extend([
            "-f",
            execution.input_file(params.get("brain_extraction_registration_mask", None))
        ])
    if params.get("keep_temporary_files", None) is not None:
        cargs.extend([
            "-k",
            ("1" if params.get("keep_temporary_files", None) else "0")
        ])
    if params.get("single_floating_point_precision", None) is not None:
        cargs.extend([
            "-q",
            ("1" if params.get("single_floating_point_precision", None) else "0")
        ])
    if params.get("initial_moving_transform", None) is not None:
        cargs.extend([
            "-r",
            execution.input_file(params.get("initial_moving_transform", None))
        ])
    if params.get("rotation_search_params", None) is not None:
        cargs.extend([
            "-R",
            params.get("rotation_search_params", None)
        ])
    if params.get("image_file_suffix", None) is not None:
        cargs.extend([
            "-s",
            params.get("image_file_suffix", None)
        ])
    if params.get("translation_search_params", None) is not None:
        cargs.extend([
            "-T",
            params.get("translation_search_params", None)
        ])
    if params.get("random_seeding", None) is not None:
        cargs.extend([
            "-u",
            ("1" if params.get("random_seeding", None) else "0")
        ])
    if params.get("debug_mode", None) is not None:
        cargs.extend([
            "-z",
            str(params.get("debug_mode", None))
        ])
    if params.get("output_prefix", None) is not None:
        cargs.extend([
            "-o",
            params.get("output_prefix", None)
        ])
    return cargs


def ants_brain_extraction_sh_outputs(
    params: AntsBrainExtractionShParamsDict,
    execution: Execution,
) -> AntsBrainExtractionShOutputs:
    """
    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 = AntsBrainExtractionShOutputs(
        root=execution.output_file("."),
        brain_extracted_image=execution.output_file(params.get("output_prefix", None) + "BrainExtractionBrain.nii.gz") if (params.get("output_prefix") is not None) else None,
        brain_mask=execution.output_file(params.get("output_prefix", None) + "BrainExtractionMask.nii.gz") if (params.get("output_prefix") is not None) else None,
        brain_segmentation=execution.output_file(params.get("output_prefix", None) + "BrainExtractionSegmentation.nii.gz") if (params.get("output_prefix") is not None) else None,
        csf_segmentation=execution.output_file(params.get("output_prefix", None) + "BrainExtractionCSF.nii.gz") if (params.get("output_prefix") is not None) else None,
        gm_segmentation=execution.output_file(params.get("output_prefix", None) + "BrainExtractionGM.nii.gz") if (params.get("output_prefix") is not None) else None,
        wm_segmentation=execution.output_file(params.get("output_prefix", None) + "BrainExtractionWM.nii.gz") if (params.get("output_prefix") is not None) else None,
        generic_affine=execution.output_file(params.get("output_prefix", None) + "BrainExtractionPrior0GenericAffine.mat") if (params.get("output_prefix") is not None) else None,
        warp_field=execution.output_file(params.get("output_prefix", None) + "BrainExtractionPrior1Warp.nii.gz") if (params.get("output_prefix") is not None) else None,
        inverse_warp_field=execution.output_file(params.get("output_prefix", None) + "BrainExtractionPrior1InverseWarp.nii.gz") if (params.get("output_prefix") is not None) else None,
    )
    return ret


def ants_brain_extraction_sh_execute(
    params: AntsBrainExtractionShParamsDict,
    runner: Runner | None = None,
) -> AntsBrainExtractionShOutputs:
    """
    antsBrainExtraction.sh
    
    antsBrainExtraction.sh performs template-based brain extraction using N4
    bias correction, registration, and Atropos segmentation.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsBrainExtractionShOutputs`).
    """
    ants_brain_extraction_sh_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(ANTS_BRAIN_EXTRACTION_SH_METADATA)
    params = execution.params(params)
    cargs = ants_brain_extraction_sh_cargs(params, execution)
    ret = ants_brain_extraction_sh_outputs(params, execution)
    execution.run(cargs)
    return ret


def ants_brain_extraction_sh(
    image_dimension: typing.Literal[2, 3],
    anatomical_image: InputPathType,
    template: InputPathType,
    probability_mask: InputPathType,
    tissue_classification: str | None = None,
    brain_extraction_registration_mask: InputPathType | None = None,
    keep_temporary_files: bool | None = None,
    single_floating_point_precision: bool | None = None,
    initial_moving_transform: InputPathType | None = None,
    rotation_search_params: str | None = None,
    image_file_suffix: str | None = None,
    translation_search_params: str | None = None,
    random_seeding: bool | None = None,
    debug_mode: int | None = None,
    output_prefix: str | None = None,
    runner: Runner | None = None,
) -> AntsBrainExtractionShOutputs:
    """
    antsBrainExtraction.sh
    
    antsBrainExtraction.sh performs template-based brain extraction using N4
    bias correction, registration, and Atropos segmentation.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: Image dimension (2 or 3 for 2- or 3-dimensional image).
        anatomical_image: Anatomical image (Structural image, typically T1). If\
            more than one anatomical image is specified, subsequently specified\
            images are used during the segmentation process. However, only the\
            first image is used in the registration of priors.
        template: Brain extraction template (Anatomical template).
        probability_mask: Brain extraction probability mask (with intensity\
            range 1=definitely brain to 0=definitely background).
        tissue_classification: Tissue classification - A k-means segmentation\
            to find tissue classes. Format: KxcsfLabelxgmLabelxwmLabel. Examples:\
            '3x1x2x3' for T1 (K=3, CSF=1, GM=2, WM=3), '3x3x2x1' for T2, '3x1x3x2'\
            for FLAIR, '4x4x2x3' for K=4.
        brain_extraction_registration_mask: Brain extraction registration mask\
            (Mask used for registration to limit the metric computation to a\
            specific region).
        keep_temporary_files: Keep temporary files (keep brain\
            extraction/segmentation warps, etc). 0=delete, 1=keep.
        single_floating_point_precision: Use antsRegistration with single (1)\
            or double (0) floating point precision.
        initial_moving_transform: Initial moving transform (An ITK affine\
            transform, e.g., from antsAI or ITK-SNAP). Without this option, the\
            script calls antsAI to search for a good initial moving transform.
        rotation_search_params: Rotation search parameters for antsAI in format\
            'step,arcFraction'. Step is in degrees, arcFraction goes from 0 (no\
            search) to 1 (search -180 to 180 degrees).
        image_file_suffix: Image file suffix (any standard ITK IO format, e.g.,\
            nrrd, nii.gz, mhd).
        translation_search_params: Translation search parameters for antsAI in\
            format 'step,range'. Step is in mm, -range to range will be tested in\
            each dimension.
        random_seeding: Use random seeding: 1=use random seed from system\
            clock, 0=use fixed seed (ANTS_RANDOM_SEED=19650218 or value from\
            environment). For reproducibility, set to 0 and also set\
            ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1.
        debug_mode: Test/debug mode: If > 0, runs a faster version of the\
            script. Only for debugging, results will not be good.
        output_prefix: Output prefix (directory + file prefix).
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `AntsBrainExtractionShOutputs`).
    """
    params = ants_brain_extraction_sh_params(
        image_dimension=image_dimension,
        anatomical_image=anatomical_image,
        template=template,
        probability_mask=probability_mask,
        tissue_classification=tissue_classification,
        brain_extraction_registration_mask=brain_extraction_registration_mask,
        keep_temporary_files=keep_temporary_files,
        single_floating_point_precision=single_floating_point_precision,
        initial_moving_transform=initial_moving_transform,
        rotation_search_params=rotation_search_params,
        image_file_suffix=image_file_suffix,
        translation_search_params=translation_search_params,
        random_seeding=random_seeding,
        debug_mode=debug_mode,
        output_prefix=output_prefix,
    )
    return ants_brain_extraction_sh_execute(params, runner)


__all__ = [
    "ANTS_BRAIN_EXTRACTION_SH_METADATA",
    "AntsBrainExtractionShOutputs",
    "AntsBrainExtractionShParamsDict",
    "AntsBrainExtractionShParamsDictTagged",
    "ants_brain_extraction_sh",
    "ants_brain_extraction_sh_execute",
    "ants_brain_extraction_sh_params",
]
