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

import typing
import pathlib
from styxdefs import *
import dataclasses

FSL_DEFACE_METADATA = Metadata(
    id="df142ae527dbfb0cc6fa0cc6e7112d7cda842d70.boutiques",
    name="fsl_deface",
    package="fsl",
    container_image_tag="brainlife/fsl:6.0.4-patched2",
)


class FslDefaceOutputs(typing.NamedTuple):
    """
    Output object returned when calling `fsl_deface(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    outfile: OutputPathType
    """Defaced T1w image"""
    out_defacing_mask: OutputPathType | None
    """Defacing mask file"""
    out_cropped_struc: OutputPathType | None
    """Cropped structural image file"""
    out_orig_to_std_mat: OutputPathType | None
    """Affine matrix from original struct to std"""
    out_orig_to_cropped_mat: OutputPathType | None
    """Affine matrix from original struct to cropped struct"""
    out_cropped_to_std_mat: OutputPathType | None
    """Affine matrix from cropped struct to std"""
    qc_image_original: OutputPathType | None
    """QC image for defacing (original)"""
    qc_image_cropped: OutputPathType | None
    """QC image for defacing (cropped)"""


def fsl_deface(
    infile: InputPathType,
    outfile: str,
    cropped_defacing_flag: bool = False,
    defacing_mask: str | None = None,
    cropped_struc: str | None = None,
    orig_to_std_mat: str | None = None,
    orig_to_cropped_mat: str | None = None,
    cropped_to_std_mat: str | None = None,
    shift_nud: list[float] | None = None,
    fractional_intensity: float | None = None,
    bias_correct_flag: bool = False,
    center_of_gravity: list[float] | None = None,
    qc_images: str | None = None,
    runner: Runner | None = None,
) -> FslDefaceOutputs:
    """
    Tool to deface a structural T1w image.
    
    Author: FMRIB Analysis Group, University of Oxford
    
    URL: https://fsl.fmrib.ox.ac.uk/fsl/fslwiki
    
    Args:
        infile: Input T1w image.
        outfile: Output defaced T1w image.
        cropped_defacing_flag: Apply the defacing to the cropped image instead\
            of the original image.
        defacing_mask: Filename to save the defacing mask.
        cropped_struc: Filename to save the new cropped struct.
        orig_to_std_mat: Filename to save affine matrix from original struct to\
            std.
        orig_to_cropped_mat: Filename to save affine matrix from original\
            struct to cropped struct.
        cropped_to_std_mat: Filename to save affine matrix from cropped struct\
            to std.
        shift_nud: Shift, in mm, x-, y- and z-directions, to shift face mask by\
            (These matrices will only work if the input has been previously\
            reoriented to std).
        fractional_intensity: Fractional intensity for bet (0->1); default=0.5.
        bias_correct_flag: Bias-correct the input image (with fast).
        center_of_gravity: Center-of-gravity for bet (voxels, not mm).
        qc_images: Generate 2 pngs to show how the defacing worked for QC\
            purposes.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `FslDefaceOutputs`).
    """
    if fractional_intensity is not None and not (0 <= fractional_intensity <= 1): 
        raise ValueError(f"'fractional_intensity' must be between 0 <= x <= 1 but was {fractional_intensity}")
    runner = runner or get_global_runner()
    execution = runner.start_execution(FSL_DEFACE_METADATA)
    cargs = []
    cargs.append("fsl_deface")
    cargs.append(execution.input_file(infile))
    cargs.append(outfile)
    if cropped_defacing_flag:
        cargs.append("-k")
    if defacing_mask is not None:
        cargs.extend([
            "-d",
            defacing_mask
        ])
    if cropped_struc is not None:
        cargs.extend([
            "-n",
            cropped_struc
        ])
    if orig_to_std_mat is not None:
        cargs.extend([
            "-m13",
            orig_to_std_mat
        ])
    if orig_to_cropped_mat is not None:
        cargs.extend([
            "-m12",
            orig_to_cropped_mat
        ])
    if cropped_to_std_mat is not None:
        cargs.extend([
            "-m23",
            cropped_to_std_mat
        ])
    if shift_nud is not None:
        cargs.extend([
            "-nud",
            *map(str, shift_nud)
        ])
    if fractional_intensity is not None:
        cargs.extend([
            "-f",
            str(fractional_intensity)
        ])
    if bias_correct_flag:
        cargs.append("-B")
    if center_of_gravity is not None:
        cargs.extend([
            "-c",
            *map(str, center_of_gravity)
        ])
    if qc_images is not None:
        cargs.extend([
            "-p",
            qc_images
        ])
    ret = FslDefaceOutputs(
        root=execution.output_file("."),
        outfile=execution.output_file(outfile + ".nii.gz"),
        out_defacing_mask=execution.output_file(defacing_mask + ".nii.gz") if (defacing_mask is not None) else None,
        out_cropped_struc=execution.output_file(cropped_struc + ".nii.gz") if (cropped_struc is not None) else None,
        out_orig_to_std_mat=execution.output_file(orig_to_std_mat + ".mat") if (orig_to_std_mat is not None) else None,
        out_orig_to_cropped_mat=execution.output_file(orig_to_cropped_mat + ".mat") if (orig_to_cropped_mat is not None) else None,
        out_cropped_to_std_mat=execution.output_file(cropped_to_std_mat + ".mat") if (cropped_to_std_mat is not None) else None,
        qc_image_original=execution.output_file(qc_images + "_defaced_original.png") if (qc_images is not None) else None,
        qc_image_cropped=execution.output_file(qc_images + "_defaced_cropped.png") if (qc_images is not None) else None,
    )
    execution.run(cargs)
    return ret


__all__ = [
    "FSL_DEFACE_METADATA",
    "FslDefaceOutputs",
    "fsl_deface",
]
