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

import typing
import pathlib
from styxdefs import *
import dataclasses

SAMSEG_METADATA = Metadata(
    id="1947f646fba1c79e354f5efabbda10d0a11595ce.boutiques",
    name="samseg",
    package="freesurfer",
    container_image_tag="freesurfer/freesurfer:7.4.1",
)


class SamsegOutputs(typing.NamedTuple):
    """
    Output object returned when calling `samseg(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    segmentation_output: OutputPathType
    """Segmentation output file."""
    posteriors_output: OutputPathType
    """Posterior probabilities output file."""
    probabilities_output: OutputPathType
    """Probabilities output file, including post, prior, and likelihood."""
    mesh_output: OutputPathType
    """Mesh output file (if save-mesh is enabled)."""


def samseg(
    input_files: list[InputPathType],
    output_directory: str,
    t1w_files: list[InputPathType] | None = None,
    t2w_files: list[InputPathType] | None = None,
    flair_files: list[InputPathType] | None = None,
    other_modality_files: list[InputPathType] | None = None,
    options_file: InputPathType | None = None,
    dissection_photo_mode: str | None = None,
    save_history: bool = False,
    subject: str | None = None,
    save_posteriors: bool = False,
    save_probabilities: bool = False,
    no_save_warp: bool = False,
    mrf: bool = False,
    no_mrf: bool = False,
    threads: float | None = None,
    atlas_directory: str | None = None,
    gmm_file: InputPathType | None = None,
    no_block_coordinate_descent: bool = False,
    logdomain_costandgradient_calculator: bool = False,
    no_logdomain_costandgradient_calculator: bool = False,
    recon: bool = False,
    fill: bool = False,
    normalization2: bool = False,
    use_t2w: bool = False,
    use_flair: bool = False,
    hires: bool = False,
    subjects_directory: str | None = None,
    pallidum_separate: bool = False,
    stiffness: float | None = None,
    lesion: bool = False,
    lesion_mask_pattern: list[float] | None = None,
    bias_field_smoothing_kernel: float | None = None,
    registration_file: InputPathType | None = None,
    regmat_file: InputPathType | None = None,
    init_lta: InputPathType | None = None,
    reg_only: bool = False,
    save_mesh: bool = False,
    max_iters: float | None = None,
    dice_file: InputPathType | None = None,
    ignore_unknown: bool = False,
    smooth_wm_cortex: float | None = None,
    profile_file: InputPathType | None = None,
    runner: Runner | None = None,
) -> SamsegOutputs:
    """
    SAMSEG (Statistical and Algorithmic Methods for Segmentation) is a tool for
    segmenting neuroimaging data.
    
    Author: FreeSurfer Developers
    
    URL: https://github.com/freesurfer/freesurfer
    
    Args:
        input_files: Input image files, must be in registration with each\
            other.
        output_directory: Output directory where results will be saved.
        t1w_files: T1-weighted input file, causes input to be conformed unless\
            --hires is used.
        t2w_files: T2-weighted input file.
        flair_files: FLAIR-weighted input file.
        other_modality_files: Specific mode input files, each associated with a\
            custom modality name.
        options_file: JSON file for specifying advanced options.
        dissection_photo_mode: Mode for processing 3D reconstructed dissection\
            photos, specifying left/right/both.
        save_history: Turns on saving of processing history.
        subject: Subject identifier, sets output directory to\
            subject/mri/samseg. The first input must be the conformed T1 weighted\
            input.
        save_posteriors: Save posterior probabilities.
        save_probabilities: Save posterior, prior, and likelihood as 3-frame\
            volume for each tissue type.
        no_save_warp: Do not save m3z-style warp.
        mrf: Perform Markov Random Field processing.
        no_mrf: Do not perform Markov Random Field processing.
        threads: Set the number of CPUs to use.
        atlas_directory: Path to the SAMSEG atlas directory.
        gmm_file: Gaussian Mixture Model file.
        no_block_coordinate_descent: Do not use block coordinate descent.
        logdomain_costandgradient_calculator: Use log-domain cost and gradient\
            calculator.
        no_logdomain_costandgradient_calculator: Do not use log-domain cost and\
            gradient calculator.
        recon: Run recon-all.
        fill: Use samseg to create filled.mgz instead of recon-all.
        normalization2: Use samseg to create brain.mgz instead of recon-all\
            (with --recon).
        use_t2w: Use the T2-weighted input when running recon-all.
        use_flair: Use the FLAIR-weighted input when running recon-all.
        hires: Use -hires when running recon-all.
        subjects_directory: Path to the SUBJECTS_DIR.
        pallidum_separate: Move pallidum outside of global white matter class,\
            used for analyzing T2 or FLAIR.
        stiffness: Set mesh stiffness.
        lesion: Turn on lesion segmentation.
        lesion_mask_pattern: Pattern needed when using T2 for lesion mask,\
            e.g., 0 1.
        bias_field_smoothing_kernel: Width of bias field smoothness.
        registration_file: Registration file (LTA or MAT) as output by samseg.
        regmat_file: Same as --reg.
        init_lta: Initial registration LTA file.
        reg_only: Only perform registration.
        save_mesh: Save the mesh, useful for longitudinal analysis.
        max_iters: Maximum number of iterations.
        dice_file: DICE coefficient file for segmentation.
        ignore_unknown: Ignore unknown flags.
        smooth_wm_cortex: Smooth white matter and cortex priors using specified\
            sigma value.
        profile_file: Run using the cProfile Python profiler, with the\
            specified profile file.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `SamsegOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(SAMSEG_METADATA)
    cargs = []
    cargs.append("samseg")
    cargs.extend([
        "--i",
        *[execution.input_file(f) for f in input_files]
    ])
    if t1w_files is not None:
        cargs.extend([
            "--t1w",
            *[execution.input_file(f) for f in t1w_files]
        ])
    if t2w_files is not None:
        cargs.extend([
            "--t2w",
            *[execution.input_file(f) for f in t2w_files]
        ])
    if flair_files is not None:
        cargs.extend([
            "--flair",
            *[execution.input_file(f) for f in flair_files]
        ])
    if other_modality_files is not None:
        cargs.extend([
            "--mode",
            *[execution.input_file(f) for f in other_modality_files]
        ])
    cargs.extend([
        "--o",
        output_directory
    ])
    if options_file is not None:
        cargs.extend([
            "--options",
            execution.input_file(options_file)
        ])
    if dissection_photo_mode is not None:
        cargs.extend([
            "--dissection-photo",
            dissection_photo_mode
        ])
    if save_history:
        cargs.append("--history")
    if subject is not None:
        cargs.extend([
            "--s",
            subject
        ])
    if save_posteriors:
        cargs.append("--save-posteriors")
    if save_probabilities:
        cargs.append("--save-probabilities")
    if no_save_warp:
        cargs.append("--no-save-warp")
    if mrf:
        cargs.append("--mrf")
    if no_mrf:
        cargs.append("--no-mrf")
    if threads is not None:
        cargs.extend([
            "--threads",
            str(threads)
        ])
    if atlas_directory is not None:
        cargs.extend([
            "--atlas",
            atlas_directory
        ])
    if gmm_file is not None:
        cargs.extend([
            "--gmm",
            execution.input_file(gmm_file)
        ])
    if no_block_coordinate_descent:
        cargs.append("--no-block-coordinate-descent")
    if logdomain_costandgradient_calculator:
        cargs.append("--logdomain-costandgradient-calculator")
    if no_logdomain_costandgradient_calculator:
        cargs.append("--no-logdomain-costandgradient-calculator")
    if recon:
        cargs.append("--recon")
    if fill:
        cargs.append("--fill")
    if normalization2:
        cargs.append("--normalization2")
    if use_t2w:
        cargs.append("--use-t2w")
    if use_flair:
        cargs.append("--use-flair")
    if hires:
        cargs.append("--hires")
    if subjects_directory is not None:
        cargs.extend([
            "--sd",
            subjects_directory
        ])
    if pallidum_separate:
        cargs.append("--pallidum-separate")
    if stiffness is not None:
        cargs.extend([
            "--stiffness",
            str(stiffness)
        ])
    if lesion:
        cargs.append("--lesion")
    if lesion_mask_pattern is not None:
        cargs.extend([
            "--lesion-mask-pattern",
            *map(str, lesion_mask_pattern)
        ])
    if bias_field_smoothing_kernel is not None:
        cargs.extend([
            "--bias-field-smoothing-kernel",
            str(bias_field_smoothing_kernel)
        ])
    if registration_file is not None:
        cargs.extend([
            "--reg",
            execution.input_file(registration_file)
        ])
    if regmat_file is not None:
        cargs.extend([
            "--regmat",
            execution.input_file(regmat_file)
        ])
    if init_lta is not None:
        cargs.extend([
            "--initlta",
            execution.input_file(init_lta)
        ])
    if reg_only:
        cargs.append("--reg-only")
    if save_mesh:
        cargs.append("--save-mesh")
    if max_iters is not None:
        cargs.extend([
            "--max-iters",
            str(max_iters)
        ])
    if dice_file is not None:
        cargs.extend([
            "--dice",
            execution.input_file(dice_file)
        ])
    if ignore_unknown:
        cargs.append("--ignore-unknown")
    if smooth_wm_cortex is not None:
        cargs.extend([
            "--smooth-wm-cortex",
            str(smooth_wm_cortex)
        ])
    if profile_file is not None:
        cargs.extend([
            "--profile",
            execution.input_file(profile_file)
        ])
    ret = SamsegOutputs(
        root=execution.output_file("."),
        segmentation_output=execution.output_file(output_directory + "/seg.mgz"),
        posteriors_output=execution.output_file(output_directory + "/posteriors.mgz"),
        probabilities_output=execution.output_file(output_directory + "/probabilities.mgz"),
        mesh_output=execution.output_file(output_directory + "/mesh.stl"),
    )
    execution.run(cargs)
    return ret


__all__ = [
    "SAMSEG_METADATA",
    "SamsegOutputs",
    "samseg",
]
