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

import typing
import pathlib
from styxdefs import *

VOLUME_WARPFIELD_AFFINE_REGRESSION_METADATA = Metadata(
    id="e61fbc4ce12b2c7a4b5f716606949064603e9c48.boutiques",
    name="volume-warpfield-affine-regression",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


VolumeWarpfieldAffineRegressionFlirtOutParameters = typing.TypedDict('VolumeWarpfieldAffineRegressionFlirtOutParameters', {
    "@type": typing.NotRequired[typing.Literal["flirt_out"]],
    "source_volume": str,
    "target_volume": str,
})
VolumeWarpfieldAffineRegressionFlirtOutParametersTagged = typing.TypedDict('VolumeWarpfieldAffineRegressionFlirtOutParametersTagged', {
    "@type": typing.Literal["flirt_out"],
    "source_volume": str,
    "target_volume": str,
})


VolumeWarpfieldAffineRegressionParameters = typing.TypedDict('VolumeWarpfieldAffineRegressionParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/volume-warpfield-affine-regression"]],
    "warpfield": str,
    "affine_out": str,
    "opt_roi_roi_vol": typing.NotRequired[InputPathType | None],
    "opt_fnirt_source_volume": typing.NotRequired[str | None],
    "flirt_out": typing.NotRequired[VolumeWarpfieldAffineRegressionFlirtOutParameters | None],
})
VolumeWarpfieldAffineRegressionParametersTagged = typing.TypedDict('VolumeWarpfieldAffineRegressionParametersTagged', {
    "@type": typing.Literal["workbench/volume-warpfield-affine-regression"],
    "warpfield": str,
    "affine_out": str,
    "opt_roi_roi_vol": typing.NotRequired[InputPathType | None],
    "opt_fnirt_source_volume": typing.NotRequired[str | None],
    "flirt_out": typing.NotRequired[VolumeWarpfieldAffineRegressionFlirtOutParameters | None],
})


def volume_warpfield_affine_regression_flirt_out_params(
    source_volume: str,
    target_volume: str,
) -> VolumeWarpfieldAffineRegressionFlirtOutParametersTagged:
    """
    Build parameters.
    
    Args:
        source_volume: the volume you want to apply the transform to.
        target_volume: the target space you want the transformed volume to\
            match.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "flirt_out",
        "source_volume": source_volume,
        "target_volume": target_volume,
    }
    return params


def volume_warpfield_affine_regression_flirt_out_cargs(
    params: VolumeWarpfieldAffineRegressionFlirtOutParameters,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("-flirt-out")
    cargs.append(params.get("source_volume", None))
    cargs.append(params.get("target_volume", None))
    return cargs


class VolumeWarpfieldAffineRegressionOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeWarpfieldAffineRegressionParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def volume_warpfield_affine_regression_params(
    warpfield: str,
    affine_out: str,
    opt_roi_roi_vol: InputPathType | None = None,
    opt_fnirt_source_volume: str | None = None,
    flirt_out: VolumeWarpfieldAffineRegressionFlirtOutParameters | None = None,
) -> VolumeWarpfieldAffineRegressionParametersTagged:
    """
    Build parameters.
    
    Args:
        warpfield: the input warpfield.
        affine_out: output - the output affine file.
        opt_roi_roi_vol: only consider voxels within a mask (e.g., a brain\
            mask): the mask volume.
        opt_fnirt_source_volume: input is a fnirt warpfield: the source volume\
            used when generating the fnirt warpfield.
        flirt_out: write output as a flirt matrix rather than a world\
            coordinate transform.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-warpfield-affine-regression",
        "warpfield": warpfield,
        "affine_out": affine_out,
    }
    if opt_roi_roi_vol is not None:
        params["opt_roi_roi_vol"] = opt_roi_roi_vol
    if opt_fnirt_source_volume is not None:
        params["opt_fnirt_source_volume"] = opt_fnirt_source_volume
    if flirt_out is not None:
        params["flirt_out"] = flirt_out
    return params


def volume_warpfield_affine_regression_cargs(
    params: VolumeWarpfieldAffineRegressionParameters,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("wb_command")
    cargs.append("-volume-warpfield-affine-regression")
    cargs.append(params.get("warpfield", None))
    cargs.append(params.get("affine_out", None))
    if params.get("opt_roi_roi_vol", None) is not None:
        cargs.extend([
            "-roi",
            execution.input_file(params.get("opt_roi_roi_vol", None))
        ])
    if params.get("opt_fnirt_source_volume", None) is not None:
        cargs.extend([
            "-fnirt",
            params.get("opt_fnirt_source_volume", None)
        ])
    if params.get("flirt_out", None) is not None:
        cargs.extend(volume_warpfield_affine_regression_flirt_out_cargs(params.get("flirt_out", None), execution))
    return cargs


def volume_warpfield_affine_regression_outputs(
    params: VolumeWarpfieldAffineRegressionParameters,
    execution: Execution,
) -> VolumeWarpfieldAffineRegressionOutputs:
    """
    Build outputs object containing output file paths and possibly stdout/stderr.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Outputs object.
    """
    ret = VolumeWarpfieldAffineRegressionOutputs(
        root=execution.output_file("."),
    )
    return ret


def volume_warpfield_affine_regression_execute(
    params: VolumeWarpfieldAffineRegressionParameters,
    runner: Runner | None = None,
) -> VolumeWarpfieldAffineRegressionOutputs:
    """
    volume-warpfield-affine-regression
    
    Regress affine from warpfield.
    
    For all voxels in the warpfield, do a regression that predicts the post-warp
    coordinate from the source coordinate. When -roi is specified, only consider
    voxels with a value greater than 0 in <roi-vol>.
    
    The default is to expect the warpfield to be in relative world coordinates
    (mm space), and to write the output as a world affine (mm space to mm
    space). If you are using FSL-created files and utilities, specify -fnirt and
    -flirt as needed, as their coordinate conventions are different.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeWarpfieldAffineRegressionOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_WARPFIELD_AFFINE_REGRESSION_METADATA)
    params = execution.params(params)
    cargs = volume_warpfield_affine_regression_cargs(params, execution)
    ret = volume_warpfield_affine_regression_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_warpfield_affine_regression(
    warpfield: str,
    affine_out: str,
    opt_roi_roi_vol: InputPathType | None = None,
    opt_fnirt_source_volume: str | None = None,
    flirt_out: VolumeWarpfieldAffineRegressionFlirtOutParameters | None = None,
    runner: Runner | None = None,
) -> VolumeWarpfieldAffineRegressionOutputs:
    """
    volume-warpfield-affine-regression
    
    Regress affine from warpfield.
    
    For all voxels in the warpfield, do a regression that predicts the post-warp
    coordinate from the source coordinate. When -roi is specified, only consider
    voxels with a value greater than 0 in <roi-vol>.
    
    The default is to expect the warpfield to be in relative world coordinates
    (mm space), and to write the output as a world affine (mm space to mm
    space). If you are using FSL-created files and utilities, specify -fnirt and
    -flirt as needed, as their coordinate conventions are different.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        warpfield: the input warpfield.
        affine_out: output - the output affine file.
        opt_roi_roi_vol: only consider voxels within a mask (e.g., a brain\
            mask): the mask volume.
        opt_fnirt_source_volume: input is a fnirt warpfield: the source volume\
            used when generating the fnirt warpfield.
        flirt_out: write output as a flirt matrix rather than a world\
            coordinate transform.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeWarpfieldAffineRegressionOutputs`).
    """
    params = volume_warpfield_affine_regression_params(
        warpfield=warpfield,
        affine_out=affine_out,
        opt_roi_roi_vol=opt_roi_roi_vol,
        opt_fnirt_source_volume=opt_fnirt_source_volume,
        flirt_out=flirt_out,
    )
    return volume_warpfield_affine_regression_execute(params, runner)


__all__ = [
    "VOLUME_WARPFIELD_AFFINE_REGRESSION_METADATA",
    "VolumeWarpfieldAffineRegressionOutputs",
    "volume_warpfield_affine_regression",
    "volume_warpfield_affine_regression_execute",
    "volume_warpfield_affine_regression_flirt_out_params",
    "volume_warpfield_affine_regression_params",
]
