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

import typing
import pathlib
from styxdefs import *
import dataclasses

GENERATE_BRAIN_METADATA = Metadata(
    id="8e55c9d531ee7cf50058bbf211e40f0bf566dddc.boutiques",
    name="generate_brain",
    package="fsl",
    container_image_tag="mcin/fsl:6.0.5",
)


class GenerateBrainOutputs(typing.NamedTuple):
    """
    Output object returned when calling `generate_brain(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    segmented_image: OutputPathType | None
    """Segmented output image"""
    bias_field: OutputPathType | None
    """Estimated bias field"""
    corrected_image: OutputPathType | None
    """Bias-corrected image"""
    pve_maps: OutputPathType | None
    """Partial volume estimation maps"""
    probability_maps: OutputPathType | None
    """Individual probability maps for each class"""


def generate_brain(
    infile: InputPathType,
    class_number: float | None = 3,
    iter_number: float | None = 4,
    lowpass_extent: float | None = 20,
    image_type: float | None = 1,
    init_seg_smoothness: float | None = 0.02,
    segments_flag: bool = False,
    init_priors: InputPathType | None = None,
    alt_priors: list[InputPathType] | None = None,
    nopve_flag: bool = False,
    bias_field_flag: bool = False,
    bias_corrected_flag: bool = False,
    no_bias_flag: bool = False,
    channels_number: float | None = 1,
    output_basename: str | None = None,
    use_priors_flag: bool = False,
    seg_init_iters: float | None = 15,
    mixel_smoothness: float | None = 0.3,
    fixed_iters: float | None = 4,
    seg_smoothness: float | None = 0.1,
    verbose_flag: bool = False,
    help_flag: bool = False,
    manual_seg: InputPathType | None = None,
    prob_maps_flag: bool = False,
    runner: Runner | None = None,
) -> GenerateBrainOutputs:
    """
    FMRIB's Automated Segmentation Tool.
    
    Author: University of Oxford
    
    URL: https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FAST
    
    Args:
        infile: Input image(s) for segmentation.
        class_number: Number of tissue-type classes; default=3.
        iter_number: Number of main-loop iterations during bias-field removal;\
            default=4.
        lowpass_extent: Bias field smoothing extent (FWHM) in mm; default=20.
        image_type: Type of image 1=T1, 2=T2, 3=PD; default=T1.
        init_seg_smoothness: Initial segmentation spatial smoothness (during\
            bias field estimation); default=0.02.
        segments_flag: Outputs a separate binary image for each tissue type.
        init_priors: Initialise using priors; you must supply a FLIRT transform.
        alt_priors: Alternative prior images (must specify three images).
        nopve_flag: Turn off PVE (partial volume estimation).
        bias_field_flag: Output estimated bias field.
        bias_corrected_flag: Output bias-corrected image.
        no_bias_flag: Do not remove bias field.
        channels_number: Number of input images (channels); default 1.
        output_basename: Output basename.
        use_priors_flag: Use priors throughout; you must also set the -a option.
        seg_init_iters: Number of segmentation-initialisation iterations;\
            default=15.
        mixel_smoothness: Spatial smoothness for mixeltype; default=0.3.
        fixed_iters: Number of main-loop iterations after bias-field removal;\
            default=4.
        seg_smoothness: Segmentation spatial smoothness; default=0.1.
        verbose_flag: Switch on diagnostic messages.
        help_flag: Display help message.
        manual_seg: Filename containing intensities.
        prob_maps_flag: Outputs individual probability maps.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `GenerateBrainOutputs`).
    """
    if alt_priors is not None and (len(alt_priors) != 3): 
        raise ValueError(f"Length of 'alt_priors' must be 3 but was {len(alt_priors)}")
    runner = runner or get_global_runner()
    execution = runner.start_execution(GENERATE_BRAIN_METADATA)
    cargs = []
    cargs.append("fast")
    if class_number is not None:
        cargs.extend([
            "-n",
            str(class_number)
        ])
    if iter_number is not None:
        cargs.extend([
            "-I",
            str(iter_number)
        ])
    if lowpass_extent is not None:
        cargs.extend([
            "-l",
            str(lowpass_extent)
        ])
    if image_type is not None:
        cargs.extend([
            "-t",
            str(image_type)
        ])
    if init_seg_smoothness is not None:
        cargs.extend([
            "-f",
            str(init_seg_smoothness)
        ])
    if segments_flag:
        cargs.append("-g")
    if init_priors is not None:
        cargs.extend([
            "-a",
            execution.input_file(init_priors)
        ])
    if alt_priors is not None:
        cargs.extend([
            "-A",
            *[execution.input_file(f) for f in alt_priors]
        ])
    if nopve_flag:
        cargs.append("--nopve")
    if bias_field_flag:
        cargs.append("-b")
    if bias_corrected_flag:
        cargs.append("-B")
    if no_bias_flag:
        cargs.append("-N")
    if channels_number is not None:
        cargs.extend([
            "-S",
            str(channels_number)
        ])
    if output_basename is not None:
        cargs.extend([
            "-o",
            output_basename
        ])
    if use_priors_flag:
        cargs.append("-P")
    if seg_init_iters is not None:
        cargs.extend([
            "-W",
            str(seg_init_iters)
        ])
    if mixel_smoothness is not None:
        cargs.extend([
            "-R",
            str(mixel_smoothness)
        ])
    if fixed_iters is not None:
        cargs.extend([
            "-O",
            str(fixed_iters)
        ])
    if seg_smoothness is not None:
        cargs.extend([
            "-H",
            str(seg_smoothness)
        ])
    if verbose_flag:
        cargs.append("-v")
    if help_flag:
        cargs.append("-h")
    if manual_seg is not None:
        cargs.extend([
            "-s",
            execution.input_file(manual_seg)
        ])
    if prob_maps_flag:
        cargs.append("-p")
    cargs.append(execution.input_file(infile))
    ret = GenerateBrainOutputs(
        root=execution.output_file("."),
        segmented_image=execution.output_file(output_basename + "_seg.nii.gz") if (output_basename is not None) else None,
        bias_field=execution.output_file(output_basename + "_bias.nii.gz") if (output_basename is not None) else None,
        corrected_image=execution.output_file(output_basename + "_restore.nii.gz") if (output_basename is not None) else None,
        pve_maps=execution.output_file(output_basename + "_pve*.nii.gz") if (output_basename is not None) else None,
        probability_maps=execution.output_file(output_basename + "_prob*.nii.gz") if (output_basename is not None) else None,
    )
    execution.run(cargs)
    return ret


__all__ = [
    "GENERATE_BRAIN_METADATA",
    "GenerateBrainOutputs",
    "generate_brain",
]
