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

import typing
import pathlib
from styxdefs import *
import dataclasses

TOPUP_METADATA = Metadata(
    id="d2018874d8c39fcdb4d692dff1529e2ea871e2a5.boutiques",
    name="topup",
    package="fsl",
    container_image_tag="mcin/fsl:6.0.5",
)


class TopupOutputs(typing.NamedTuple):
    """
    Output object returned when calling `topup(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    fieldcoef: OutputPathType | None
    """Spline coefficient field estimates (Hz)"""
    movpar: OutputPathType | None
    """Movement parameters"""
    fout: OutputPathType | None
    """Image file with field (Hz)"""
    iout: OutputPathType | None
    """4D image file with unwarped images"""
    logout: OutputPathType | None
    """Log file"""


def topup(
    imain: InputPathType,
    datain: InputPathType,
    out: str | None = None,
    fout: str | None = None,
    iout: str | None = None,
    logout: str | None = None,
    warpres: float | None = None,
    subsamp: float | None = None,
    fwhm: float | None = None,
    config: InputPathType | None = None,
    miter: float | None = None,
    lambda_: float | None = None,
    ssqlambda: bool = False,
    regmod: typing.Literal["membrane_energy", "bending_energy"] | None = None,
    estmov: bool = False,
    minmet: typing.Literal[0, 1] | None = None,
    splineorder: typing.Literal[2, 3] | None = None,
    numprec: typing.Literal["double", "float"] | None = None,
    interp: typing.Literal["linear", "spline"] | None = None,
    scale: bool = False,
    regrid: bool = False,
    nthr: float | None = None,
    verbose: bool = False,
    runner: Runner | None = None,
) -> TopupOutputs:
    """
    topup is part of FSL and is used to estimate and correct for
    susceptibility-induced distortions in echo planar imaging (EPI) data.
    
    Author: University of Oxford
    
    Args:
        imain: Name of 4D file with images.
        datain: Name of text file with PE directions/times.
        out: Base-name of output files (spline coefficients (Hz) and movement\
            parameters).
        fout: Name of image file with field (Hz).
        iout: Name of 4D image file with unwarped images.
        logout: Name of log-file.
        warpres: (Approximate) resolution (in mm) of warp basis for the\
            different sub-sampling levels, default 10.
        subsamp: Sub-sampling scheme, default 1.
        fwhm: FWHM (in mm) of gaussian smoothing kernel, default 8.
        config: Name of config file specifying command line arguments.
        miter: Max # of non-linear iterations, default 5.
        lambda_: Weight of regularisation, default depending on --ssqlambda and\
            --regmod switches. See user documentation.
        ssqlambda: If set (=1), lambda is weighted by current ssq, default 1.
        regmod: Model for regularisation of warp-field [membrane_energy\
            bending_energy], default bending_energy.
        estmov: Estimate movements if set, default 1 (true).
        minmet: Minimisation method 0=Levenberg-Marquardt, 1=Scaled Conjugate\
            Gradient, default 0 (LM).
        splineorder: Order of spline, 2=Quadratic spline, 3=Cubic spline.\
            Default=3.
        numprec: Precision for representing Hessian, double or float. Default\
            double.
        interp: Image interpolation model, linear or spline. Default spline.
        scale: If set (=1), the images are individually scaled to a common\
            mean, default 0 (false).
        regrid: If set (=1), the calculations are done in a different grid,\
            default 1 (true).
        nthr: Number of threads to use (cannot be greater than numbers of\
            hardware cores), default 1.
        verbose: Print diagnostic information while running.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `TopupOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(TOPUP_METADATA)
    cargs = []
    cargs.append("topup")
    cargs.append("--imain=" + execution.input_file(imain))
    cargs.append("--datain=" + execution.input_file(datain))
    if out is not None:
        cargs.append("--out=" + out)
    if fout is not None:
        cargs.append("--fout=" + fout)
    if iout is not None:
        cargs.append("--iout=" + iout)
    if logout is not None:
        cargs.append("--logout=" + logout)
    if warpres is not None:
        cargs.append("--warpres=" + str(warpres))
    if subsamp is not None:
        cargs.append("--subsamp=" + str(subsamp))
    if fwhm is not None:
        cargs.append("--fwhm=" + str(fwhm))
    if config is not None:
        cargs.append("--config=" + execution.input_file(config))
    if miter is not None:
        cargs.append("--miter=" + str(miter))
    if lambda_ is not None:
        cargs.append("--lambda=" + str(lambda_))
    if ssqlambda:
        cargs.append("--ssqlambda")
    if regmod is not None:
        cargs.append("--regmod=" + regmod)
    if estmov:
        cargs.append("--estmov")
    if minmet is not None:
        cargs.append("--minmet=" + str(minmet))
    if splineorder is not None:
        cargs.append("--splineorder=" + str(splineorder))
    if numprec is not None:
        cargs.append("--numprec=" + numprec)
    if interp is not None:
        cargs.append("--interp=" + interp)
    if scale:
        cargs.append("--scale")
    if regrid:
        cargs.append("--regrid")
    if nthr is not None:
        cargs.append("--nthr=" + str(nthr))
    if verbose:
        cargs.append("--verbose")
    ret = TopupOutputs(
        root=execution.output_file("."),
        fieldcoef=execution.output_file(out + "_fieldcoef.nii.gz") if (out is not None) else None,
        movpar=execution.output_file(out + "_movpar.txt") if (out is not None) else None,
        fout=execution.output_file(fout) if (fout is not None) else None,
        iout=execution.output_file(iout) if (iout is not None) else None,
        logout=execution.output_file(logout) if (logout is not None) else None,
    )
    execution.run(cargs)
    return ret


__all__ = [
    "TOPUP_METADATA",
    "TopupOutputs",
    "topup",
]
