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

import typing
import pathlib
from styxdefs import *
import dataclasses

V_3D_POLYFIT_METADATA = Metadata(
    id="71d806c929428c9ef4f179dbd4ad3511aa5ac59a.boutiques",
    name="3dPolyfit",
    package="afni",
    container_image_tag="afni/afni_make_build:AFNI_24.2.06",
)


class V3dPolyfitOutputs(typing.NamedTuple):
    """
    Output object returned when calling `v_3d_polyfit(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output_file: OutputPathType | None
    """Fitted output dataset"""
    resid_file: OutputPathType | None
    """Residual dataset"""
    coeff_file: OutputPathType | None
    """Coefficient output file"""


def v_3d_polyfit(
    input_dataset: InputPathType,
    poly_order: int | None = None,
    blur: float | None = None,
    median_radius: float | None = None,
    output_prefix: str | None = None,
    resid_prefix: str | None = None,
    coeff_output: str | None = None,
    automask: bool = False,
    mask_dataset: InputPathType | None = None,
    mean_scale: bool = False,
    clip_box: bool = False,
    fit_method: int | None = None,
    base_dataset: InputPathType | None = None,
    verbose: bool = False,
    runner: Runner | None = None,
) -> V3dPolyfitOutputs:
    """
    Fits a polynomial in space to the input dataset and outputs that fitted dataset.
    You can also add your own basis datasets to the fitting mix.
    
    Author: AFNI Developers
    
    URL: https://afni.nimh.nih.gov/
    
    Args:
        input_dataset: Input dataset (e.g., data.nii.gz).
        poly_order: Maximum polynomial order (0..9); [default=3]; [n=0 is the\
            constant 1]; [n=-1 means only use volumes from '-base'].
        blur: Gaussian blur input dataset (inside mask) with FWHM='f' (mm).
        median_radius: Radius (voxels) of preliminary median filter of input;\
            default is no blurring.
        output_prefix: Use 'pp' for prefix of output dataset (the fit); default\
            prefix is 'Polyfit'; use NULL to skip this output.
        resid_prefix: Use 'rr' for the prefix of the residual dataset; default\
            is not to output residuals.
        coeff_output: Save coefficients of fit into text file cc.1D; default is\
            not to save these coefficients.
        automask: Create a mask (a la 3dAutomask).
        mask_dataset: Create a mask from nonzero voxels in 'mset'; default is\
            not to use a mask.
        mean_scale: Scale the mean value of the fit (inside the mask) to 1;\
            probably this option is not useful for anything.
        clip_box: Clip fit values outside the rectilinear box containing the\
            mask to the edge of that box, to avoid weird artifacts.
        fit_method: Set 'mm' to 2 for least squares fit; set it to 1 for L1 fit\
            [default method=2]; [Note that L1 fitting is slower than L2 fitting].
        base_dataset: In addition to the polynomial fit, also use the volumes\
            in dataset 'bb' as extra basis functions.
        verbose: Print fun and useful progress reports.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `V3dPolyfitOutputs`).
    """
    if poly_order is not None and not (-1 <= poly_order <= 9): 
        raise ValueError(f"'poly_order' must be between -1 <= x <= 9 but was {poly_order}")
    if fit_method is not None and not (1 <= fit_method <= 2): 
        raise ValueError(f"'fit_method' must be between 1 <= x <= 2 but was {fit_method}")
    runner = runner or get_global_runner()
    execution = runner.start_execution(V_3D_POLYFIT_METADATA)
    cargs = []
    cargs.append("3dPolyfit")
    cargs.append(execution.input_file(input_dataset))
    if poly_order is not None:
        cargs.extend([
            "-nord",
            str(poly_order)
        ])
    if blur is not None:
        cargs.extend([
            "-blur",
            str(blur)
        ])
    if median_radius is not None:
        cargs.extend([
            "-mrad",
            str(median_radius)
        ])
    if output_prefix is not None:
        cargs.extend([
            "-prefix",
            output_prefix
        ])
    if resid_prefix is not None:
        cargs.extend([
            "-resid",
            resid_prefix
        ])
    if coeff_output is not None:
        cargs.extend([
            "-1Dcoef",
            coeff_output
        ])
    if automask:
        cargs.append("-automask")
    if mask_dataset is not None:
        cargs.extend([
            "-mask",
            execution.input_file(mask_dataset)
        ])
    if mean_scale:
        cargs.append("-mone")
    if clip_box:
        cargs.append("-mclip")
    if fit_method is not None:
        cargs.extend([
            "-meth",
            str(fit_method)
        ])
    if base_dataset is not None:
        cargs.extend([
            "-base",
            execution.input_file(base_dataset)
        ])
    if verbose:
        cargs.append("-verb")
    ret = V3dPolyfitOutputs(
        root=execution.output_file("."),
        output_file=execution.output_file(output_prefix + ".nii.gz") if (output_prefix is not None) else None,
        resid_file=execution.output_file(resid_prefix + ".nii.gz") if (resid_prefix is not None) else None,
        coeff_file=execution.output_file(coeff_output + ".1D") if (coeff_output is not None) else None,
    )
    execution.run(cargs)
    return ret


__all__ = [
    "V3dPolyfitOutputs",
    "V_3D_POLYFIT_METADATA",
    "v_3d_polyfit",
]
