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

import typing
import pathlib
from styxdefs import *
import dataclasses

V_3D_REMLFIT_METADATA = Metadata(
    id="9fb81de20c23d0c04b8b45344a5e91f20b4ba1b5.boutiques",
    name="3dREMLfit",
    package="afni",
    container_image_tag="afni/afni_make_build:AFNI_24.2.06",
)


class V3dRemlfitOutputs(typing.NamedTuple):
    """
    Output object returned when calling `v_3d_remlfit(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    outfile: OutputPathType | None
    """Main default output of 3dREMLfit"""
    rvar_file: OutputPathType | None
    """REML variance parameters"""
    rbeta_file: OutputPathType | None
    """REML beta weights"""
    rbuck_file: OutputPathType | None
    """REML estimates and statistics"""
    rfitts_file: OutputPathType | None
    """REML fitted model"""
    rerrts_file: OutputPathType | None
    """REML residuals"""


def v_3d_remlfit(
    input_file: InputPathType,
    regression_matrix: InputPathType,
    baseline_files: list[str] | None = None,
    sort_nods: bool = False,
    temp_storage: bool = False,
    mask: InputPathType | None = None,
    output_prefix: str | None = None,
    go_for_it: bool = False,
    max_b_param: float | None = None,
    grid_param: float | None = None,
    negative_corr: bool = False,
    runner: Runner | None = None,
) -> V3dRemlfitOutputs:
    """
    Generalized least squares time series fit, with REML estimation of the temporal
    auto-correlation structure.
    
    Author: AFNI Team
    
    URL: https://afni.nimh.nih.gov/pub/dist/doc/program_help/3dREMLfit.html
    
    Args:
        input_file: Read time series dataset.
        regression_matrix: Read the regression matrix, which should have been\
            output from 3dDeconvolve via the '-x1D' option.
        baseline_files: Add baseline model columns to the matrix. Each column\
            in the specified .1D file will be appended to the matrix.
        sort_nods: If '-dsort' is used, the output datasets reflect the impact\
            of the voxel-wise regressor(s). If you want to compare those results to\
            the case where you did NOT give the '-dsort' option, then also use\
            '-dsort_nods'.
        temp_storage: Write intermediate output to disk, to economize on RAM.
        mask: Read dataset as a mask for the input; voxels outside the mask\
            will not be fit by the regression model.
        output_prefix: Dataset prefix for saving REML variance parameters.
        go_for_it: Force the program to continue past a failed collinearity\
            check.
        max_b_param: Set max allowed MA b parameter.
        grid_param: Set the number of grid divisions in the (a,b) grid.
        negative_corr: Allows negative correlations to be used.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `V3dRemlfitOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(V_3D_REMLFIT_METADATA)
    cargs = []
    cargs.append("3dREMLfit")
    cargs.extend([
        "-input",
        execution.input_file(input_file)
    ])
    cargs.extend([
        "-matrix",
        execution.input_file(regression_matrix)
    ])
    if baseline_files is not None:
        cargs.extend([
            "-addbase",
            *baseline_files
        ])
    if sort_nods:
        cargs.append("-dsort_nods")
    if temp_storage:
        cargs.append("-usetemp")
    if mask is not None:
        cargs.extend([
            "-mask",
            execution.input_file(mask)
        ])
    if output_prefix is not None:
        cargs.extend([
            "-Rvar",
            output_prefix
        ])
    if go_for_it:
        cargs.append("-GOFORIT")
    if max_b_param is not None:
        cargs.extend([
            "-MAXb",
            str(max_b_param)
        ])
    if grid_param is not None:
        cargs.extend([
            "-Grid",
            str(grid_param)
        ])
    if negative_corr:
        cargs.append("-NEGcor")
    ret = V3dRemlfitOutputs(
        root=execution.output_file("."),
        outfile=execution.output_file(output_prefix + ".nii.gz") if (output_prefix is not None) else None,
        rvar_file=execution.output_file(output_prefix + "_Rvar.nii.gz") if (output_prefix is not None) else None,
        rbeta_file=execution.output_file(output_prefix + "_Rbeta.nii.gz") if (output_prefix is not None) else None,
        rbuck_file=execution.output_file(output_prefix + "_Rbuck.nii.gz") if (output_prefix is not None) else None,
        rfitts_file=execution.output_file(output_prefix + "_Rfitts.nii.gz") if (output_prefix is not None) else None,
        rerrts_file=execution.output_file(output_prefix + "_Rerrts.nii.gz") if (output_prefix is not None) else None,
    )
    execution.run(cargs)
    return ret


__all__ = [
    "V3dRemlfitOutputs",
    "V_3D_REMLFIT_METADATA",
    "v_3d_remlfit",
]
