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

import typing
import pathlib
from styxdefs import *

VOLUME_RESAMPLE_METADATA = Metadata(
    id="7182598b31901aea230333f6711cb7e4f51b6bd1.boutiques",
    name="volume-resample",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


VolumeResampleFlirtParameters = typing.TypedDict('VolumeResampleFlirtParameters', {
    "@type": typing.NotRequired[typing.Literal["flirt"]],
    "source_volume": str,
    "target_volume": str,
})
VolumeResampleFlirtParametersTagged = typing.TypedDict('VolumeResampleFlirtParametersTagged', {
    "@type": typing.Literal["flirt"],
    "source_volume": str,
    "target_volume": str,
})


VolumeResampleAffineParameters = typing.TypedDict('VolumeResampleAffineParameters', {
    "@type": typing.NotRequired[typing.Literal["affine"]],
    "affine": str,
    "flirt": typing.NotRequired[VolumeResampleFlirtParameters | None],
})
VolumeResampleAffineParametersTagged = typing.TypedDict('VolumeResampleAffineParametersTagged', {
    "@type": typing.Literal["affine"],
    "affine": str,
    "flirt": typing.NotRequired[VolumeResampleFlirtParameters | None],
})


VolumeResampleFlirt1Parameters = typing.TypedDict('VolumeResampleFlirt1Parameters', {
    "@type": typing.NotRequired[typing.Literal["flirt_1"]],
    "source_volume": str,
    "target_volume": str,
})
VolumeResampleFlirt1ParametersTagged = typing.TypedDict('VolumeResampleFlirt1ParametersTagged', {
    "@type": typing.Literal["flirt_1"],
    "source_volume": str,
    "target_volume": str,
})


VolumeResampleAffineSeriesParameters = typing.TypedDict('VolumeResampleAffineSeriesParameters', {
    "@type": typing.NotRequired[typing.Literal["affine_series"]],
    "affine_series": str,
    "flirt": typing.NotRequired[VolumeResampleFlirt1Parameters | None],
})
VolumeResampleAffineSeriesParametersTagged = typing.TypedDict('VolumeResampleAffineSeriesParametersTagged', {
    "@type": typing.Literal["affine_series"],
    "affine_series": str,
    "flirt": typing.NotRequired[VolumeResampleFlirt1Parameters | None],
})


VolumeResampleWarpParameters = typing.TypedDict('VolumeResampleWarpParameters', {
    "@type": typing.NotRequired[typing.Literal["warp"]],
    "warpfield": str,
    "opt_fnirt_source_volume": typing.NotRequired[str | None],
})
VolumeResampleWarpParametersTagged = typing.TypedDict('VolumeResampleWarpParametersTagged', {
    "@type": typing.Literal["warp"],
    "warpfield": str,
    "opt_fnirt_source_volume": typing.NotRequired[str | None],
})


VolumeResampleParameters = typing.TypedDict('VolumeResampleParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/volume-resample"]],
    "volume_in": InputPathType,
    "volume_space": str,
    "method": str,
    "volume_out": str,
    "affine": typing.NotRequired[list[VolumeResampleAffineParameters] | None],
    "affine_series": typing.NotRequired[list[VolumeResampleAffineSeriesParameters] | None],
    "warp": typing.NotRequired[list[VolumeResampleWarpParameters] | None],
})
VolumeResampleParametersTagged = typing.TypedDict('VolumeResampleParametersTagged', {
    "@type": typing.Literal["workbench/volume-resample"],
    "volume_in": InputPathType,
    "volume_space": str,
    "method": str,
    "volume_out": str,
    "affine": typing.NotRequired[list[VolumeResampleAffineParameters] | None],
    "affine_series": typing.NotRequired[list[VolumeResampleAffineSeriesParameters] | None],
    "warp": typing.NotRequired[list[VolumeResampleWarpParameters] | None],
})


def volume_resample_flirt_params(
    source_volume: str,
    target_volume: str,
) -> VolumeResampleFlirtParametersTagged:
    """
    Build parameters.
    
    Args:
        source_volume: the source volume used when generating the affine.
        target_volume: the target volume used when generating the affine.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "flirt",
        "source_volume": source_volume,
        "target_volume": target_volume,
    }
    return params


def volume_resample_flirt_cargs(
    params: VolumeResampleFlirtParameters,
    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")
    cargs.append(params.get("source_volume", None))
    cargs.append(params.get("target_volume", None))
    return cargs


def volume_resample_affine_params(
    affine: str,
    flirt: VolumeResampleFlirtParameters | None = None,
) -> VolumeResampleAffineParametersTagged:
    """
    Build parameters.
    
    Args:
        affine: the affine file to use.
        flirt: MUST be used if affine is a flirt affine.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "affine",
        "affine": affine,
    }
    if flirt is not None:
        params["flirt"] = flirt
    return params


def volume_resample_affine_cargs(
    params: VolumeResampleAffineParameters,
    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("-affine")
    cargs.append(params.get("affine", None))
    if params.get("flirt", None) is not None:
        cargs.extend(volume_resample_flirt_cargs(params.get("flirt", None), execution))
    return cargs


def volume_resample_flirt_1_params(
    source_volume: str,
    target_volume: str,
) -> VolumeResampleFlirt1ParametersTagged:
    """
    Build parameters.
    
    Args:
        source_volume: the source volume used when generating the affine.
        target_volume: the target volume used when generating the affine.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "flirt_1",
        "source_volume": source_volume,
        "target_volume": target_volume,
    }
    return params


def volume_resample_flirt_1_cargs(
    params: VolumeResampleFlirt1Parameters,
    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")
    cargs.append(params.get("source_volume", None))
    cargs.append(params.get("target_volume", None))
    return cargs


def volume_resample_affine_series_params(
    affine_series: str,
    flirt: VolumeResampleFlirt1Parameters | None = None,
) -> VolumeResampleAffineSeriesParametersTagged:
    """
    Build parameters.
    
    Args:
        affine_series: text file containing 12 or 16 numbers per line, each\
            being a row-major flattened affine.
        flirt: MUST be used if the affines are flirt affines.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "affine_series",
        "affine_series": affine_series,
    }
    if flirt is not None:
        params["flirt"] = flirt
    return params


def volume_resample_affine_series_cargs(
    params: VolumeResampleAffineSeriesParameters,
    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("-affine-series")
    cargs.append(params.get("affine_series", None))
    if params.get("flirt", None) is not None:
        cargs.extend(volume_resample_flirt_1_cargs(params.get("flirt", None), execution))
    return cargs


def volume_resample_warp_params(
    warpfield: str,
    opt_fnirt_source_volume: str | None = None,
) -> VolumeResampleWarpParametersTagged:
    """
    Build parameters.
    
    Args:
        warpfield: the warpfield file.
        opt_fnirt_source_volume: MUST be used if using a fnirt warpfield: the\
            source volume used when generating the warpfield.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "warp",
        "warpfield": warpfield,
    }
    if opt_fnirt_source_volume is not None:
        params["opt_fnirt_source_volume"] = opt_fnirt_source_volume
    return params


def volume_resample_warp_cargs(
    params: VolumeResampleWarpParameters,
    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("-warp")
    cargs.append(params.get("warpfield", None))
    if params.get("opt_fnirt_source_volume", None) is not None:
        cargs.extend([
            "-fnirt",
            params.get("opt_fnirt_source_volume", None)
        ])
    return cargs


class VolumeResampleOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeResampleParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    volume_out: OutputPathType
    """the output volume"""


def volume_resample_params(
    volume_in: InputPathType,
    volume_space: str,
    method: str,
    volume_out: str,
    affine: list[VolumeResampleAffineParameters] | None = None,
    affine_series: list[VolumeResampleAffineSeriesParameters] | None = None,
    warp: list[VolumeResampleWarpParameters] | None = None,
) -> VolumeResampleParametersTagged:
    """
    Build parameters.
    
    Args:
        volume_in: volume to resample.
        volume_space: a volume file in the volume space you want for the output.
        method: the resampling method.
        volume_out: the output volume.
        affine: add an affine transform.
        affine_series: add an independent affine per-frame.
        warp: add a nonlinear warpfield transform.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-resample",
        "volume_in": volume_in,
        "volume_space": volume_space,
        "method": method,
        "volume_out": volume_out,
    }
    if affine is not None:
        params["affine"] = affine
    if affine_series is not None:
        params["affine_series"] = affine_series
    if warp is not None:
        params["warp"] = warp
    return params


def volume_resample_cargs(
    params: VolumeResampleParameters,
    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-resample")
    cargs.append(execution.input_file(params.get("volume_in", None)))
    cargs.append(params.get("volume_space", None))
    cargs.append(params.get("method", None))
    cargs.append(params.get("volume_out", None))
    if params.get("affine", None) is not None:
        cargs.extend([a for c in [volume_resample_affine_cargs(s, execution) for s in params.get("affine", None)] for a in c])
    if params.get("affine_series", None) is not None:
        cargs.extend([a for c in [volume_resample_affine_series_cargs(s, execution) for s in params.get("affine_series", None)] for a in c])
    if params.get("warp", None) is not None:
        cargs.extend([a for c in [volume_resample_warp_cargs(s, execution) for s in params.get("warp", None)] for a in c])
    return cargs


def volume_resample_outputs(
    params: VolumeResampleParameters,
    execution: Execution,
) -> VolumeResampleOutputs:
    """
    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 = VolumeResampleOutputs(
        root=execution.output_file("."),
        volume_out=execution.output_file(params.get("volume_out", None)),
    )
    return ret


def volume_resample_execute(
    params: VolumeResampleParameters,
    runner: Runner | None = None,
) -> VolumeResampleOutputs:
    """
    volume-resample
    
    Transform and resample a volume file.
    
    Resample a volume file with an arbitrary list of transformations. You may
    specify -affine, -warp, and -affine-series multiple times each, and they
    will be used in the order specified. For instance, for rigid motion
    correction followed by nonlinear atlas registration, specify -affine-series
    first, then -warp. The recommended methods are CUBIC (cubic spline) for most
    data, and ENCLOSING_VOXEL for label data. The parameter <method> must be one
    of:
    
    CUBIC
    ENCLOSING_VOXEL
    TRILINEAR.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeResampleOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_RESAMPLE_METADATA)
    params = execution.params(params)
    cargs = volume_resample_cargs(params, execution)
    ret = volume_resample_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_resample(
    volume_in: InputPathType,
    volume_space: str,
    method: str,
    volume_out: str,
    affine: list[VolumeResampleAffineParameters] | None = None,
    affine_series: list[VolumeResampleAffineSeriesParameters] | None = None,
    warp: list[VolumeResampleWarpParameters] | None = None,
    runner: Runner | None = None,
) -> VolumeResampleOutputs:
    """
    volume-resample
    
    Transform and resample a volume file.
    
    Resample a volume file with an arbitrary list of transformations. You may
    specify -affine, -warp, and -affine-series multiple times each, and they
    will be used in the order specified. For instance, for rigid motion
    correction followed by nonlinear atlas registration, specify -affine-series
    first, then -warp. The recommended methods are CUBIC (cubic spline) for most
    data, and ENCLOSING_VOXEL for label data. The parameter <method> must be one
    of:
    
    CUBIC
    ENCLOSING_VOXEL
    TRILINEAR.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        volume_in: volume to resample.
        volume_space: a volume file in the volume space you want for the output.
        method: the resampling method.
        volume_out: the output volume.
        affine: add an affine transform.
        affine_series: add an independent affine per-frame.
        warp: add a nonlinear warpfield transform.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeResampleOutputs`).
    """
    params = volume_resample_params(
        volume_in=volume_in,
        volume_space=volume_space,
        method=method,
        volume_out=volume_out,
        affine=affine,
        affine_series=affine_series,
        warp=warp,
    )
    return volume_resample_execute(params, runner)


__all__ = [
    "VOLUME_RESAMPLE_METADATA",
    "VolumeResampleOutputs",
    "volume_resample",
    "volume_resample_affine_params",
    "volume_resample_affine_series_params",
    "volume_resample_execute",
    "volume_resample_flirt_1_params",
    "volume_resample_flirt_params",
    "volume_resample_params",
    "volume_resample_warp_params",
]
