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

import typing
import pathlib
from styxdefs import *

CIFTI_EXTREMA_METADATA = Metadata(
    id="30b679ce9711d1251e93a3a78a855b94ac05e5d6.boutiques",
    name="cifti-extrema",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


CiftiExtremaThresholdParameters = typing.TypedDict('CiftiExtremaThresholdParameters', {
    "@type": typing.NotRequired[typing.Literal["threshold"]],
    "low": float,
    "high": float,
})
CiftiExtremaThresholdParametersTagged = typing.TypedDict('CiftiExtremaThresholdParametersTagged', {
    "@type": typing.Literal["threshold"],
    "low": float,
    "high": float,
})


CiftiExtremaParameters = typing.TypedDict('CiftiExtremaParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/cifti-extrema"]],
    "cifti": InputPathType,
    "surface_distance": float,
    "volume_distance": float,
    "direction": str,
    "cifti_out": str,
    "opt_left_surface_surface": typing.NotRequired[InputPathType | None],
    "opt_right_surface_surface": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_surface_surface": typing.NotRequired[InputPathType | None],
    "opt_surface_presmooth_surface_kernel": typing.NotRequired[float | None],
    "opt_volume_presmooth_volume_kernel": typing.NotRequired[float | None],
    "opt_presmooth_fwhm": bool,
    "threshold": typing.NotRequired[CiftiExtremaThresholdParameters | None],
    "opt_merged_volume": bool,
    "opt_sum_maps": bool,
    "opt_consolidate_mode": bool,
    "opt_only_maxima": bool,
    "opt_only_minima": bool,
})
CiftiExtremaParametersTagged = typing.TypedDict('CiftiExtremaParametersTagged', {
    "@type": typing.Literal["workbench/cifti-extrema"],
    "cifti": InputPathType,
    "surface_distance": float,
    "volume_distance": float,
    "direction": str,
    "cifti_out": str,
    "opt_left_surface_surface": typing.NotRequired[InputPathType | None],
    "opt_right_surface_surface": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_surface_surface": typing.NotRequired[InputPathType | None],
    "opt_surface_presmooth_surface_kernel": typing.NotRequired[float | None],
    "opt_volume_presmooth_volume_kernel": typing.NotRequired[float | None],
    "opt_presmooth_fwhm": bool,
    "threshold": typing.NotRequired[CiftiExtremaThresholdParameters | None],
    "opt_merged_volume": bool,
    "opt_sum_maps": bool,
    "opt_consolidate_mode": bool,
    "opt_only_maxima": bool,
    "opt_only_minima": bool,
})


def cifti_extrema_threshold_params(
    low: float,
    high: float,
) -> CiftiExtremaThresholdParametersTagged:
    """
    Build parameters.
    
    Args:
        low: the largest value to consider for being a minimum.
        high: the smallest value to consider for being a maximum.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "threshold",
        "low": low,
        "high": high,
    }
    return params


def cifti_extrema_threshold_cargs(
    params: CiftiExtremaThresholdParameters,
    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("-threshold")
    cargs.append(str(params.get("low", None)))
    cargs.append(str(params.get("high", None)))
    return cargs


class CiftiExtremaOutputs(typing.NamedTuple):
    """
    Output object returned when calling `CiftiExtremaParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    cifti_out: OutputPathType
    """the output cifti"""


def cifti_extrema_params(
    cifti: InputPathType,
    surface_distance: float,
    volume_distance: float,
    direction: str,
    cifti_out: str,
    opt_left_surface_surface: InputPathType | None = None,
    opt_right_surface_surface: InputPathType | None = None,
    opt_cerebellum_surface_surface: InputPathType | None = None,
    opt_surface_presmooth_surface_kernel: float | None = None,
    opt_volume_presmooth_volume_kernel: float | None = None,
    opt_presmooth_fwhm: bool = False,
    threshold: CiftiExtremaThresholdParameters | None = None,
    opt_merged_volume: bool = False,
    opt_sum_maps: bool = False,
    opt_consolidate_mode: bool = False,
    opt_only_maxima: bool = False,
    opt_only_minima: bool = False,
) -> CiftiExtremaParametersTagged:
    """
    Build parameters.
    
    Args:
        cifti: the input cifti.
        surface_distance: the minimum distance between extrema of the same\
            type, for surface components.
        volume_distance: the minimum distance between extrema of the same type,\
            for volume components.
        direction: which dimension to find extrema along, ROW or COLUMN.
        cifti_out: the output cifti.
        opt_left_surface_surface: specify the left surface to use: the left\
            surface file.
        opt_right_surface_surface: specify the right surface to use: the right\
            surface file.
        opt_cerebellum_surface_surface: specify the cerebellum surface to use:\
            the cerebellum surface file.
        opt_surface_presmooth_surface_kernel: smooth on the surface before\
            finding extrema: the size of the gaussian surface smoothing kernel in\
            mm, as sigma by default.
        opt_volume_presmooth_volume_kernel: smooth volume components before\
            finding extrema: the size of the gaussian volume smoothing kernel in\
            mm, as sigma by default.
        opt_presmooth_fwhm: smoothing kernel distances are FWHM, not sigma.
        threshold: ignore small extrema.
        opt_merged_volume: treat volume components as if they were a single\
            component.
        opt_sum_maps: output the sum of the extrema maps instead of each map\
            separately.
        opt_consolidate_mode: use consolidation of local minima instead of a\
            large neighborhood.
        opt_only_maxima: only find the maxima.
        opt_only_minima: only find the minima.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-extrema",
        "cifti": cifti,
        "surface_distance": surface_distance,
        "volume_distance": volume_distance,
        "direction": direction,
        "cifti_out": cifti_out,
        "opt_presmooth_fwhm": opt_presmooth_fwhm,
        "opt_merged_volume": opt_merged_volume,
        "opt_sum_maps": opt_sum_maps,
        "opt_consolidate_mode": opt_consolidate_mode,
        "opt_only_maxima": opt_only_maxima,
        "opt_only_minima": opt_only_minima,
    }
    if opt_left_surface_surface is not None:
        params["opt_left_surface_surface"] = opt_left_surface_surface
    if opt_right_surface_surface is not None:
        params["opt_right_surface_surface"] = opt_right_surface_surface
    if opt_cerebellum_surface_surface is not None:
        params["opt_cerebellum_surface_surface"] = opt_cerebellum_surface_surface
    if opt_surface_presmooth_surface_kernel is not None:
        params["opt_surface_presmooth_surface_kernel"] = opt_surface_presmooth_surface_kernel
    if opt_volume_presmooth_volume_kernel is not None:
        params["opt_volume_presmooth_volume_kernel"] = opt_volume_presmooth_volume_kernel
    if threshold is not None:
        params["threshold"] = threshold
    return params


def cifti_extrema_cargs(
    params: CiftiExtremaParameters,
    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("-cifti-extrema")
    cargs.append(execution.input_file(params.get("cifti", None)))
    cargs.append(str(params.get("surface_distance", None)))
    cargs.append(str(params.get("volume_distance", None)))
    cargs.append(params.get("direction", None))
    cargs.append(params.get("cifti_out", None))
    if params.get("opt_left_surface_surface", None) is not None:
        cargs.extend([
            "-left-surface",
            execution.input_file(params.get("opt_left_surface_surface", None))
        ])
    if params.get("opt_right_surface_surface", None) is not None:
        cargs.extend([
            "-right-surface",
            execution.input_file(params.get("opt_right_surface_surface", None))
        ])
    if params.get("opt_cerebellum_surface_surface", None) is not None:
        cargs.extend([
            "-cerebellum-surface",
            execution.input_file(params.get("opt_cerebellum_surface_surface", None))
        ])
    if params.get("opt_surface_presmooth_surface_kernel", None) is not None:
        cargs.extend([
            "-surface-presmooth",
            str(params.get("opt_surface_presmooth_surface_kernel", None))
        ])
    if params.get("opt_volume_presmooth_volume_kernel", None) is not None:
        cargs.extend([
            "-volume-presmooth",
            str(params.get("opt_volume_presmooth_volume_kernel", None))
        ])
    if params.get("opt_presmooth_fwhm", False):
        cargs.append("-presmooth-fwhm")
    if params.get("threshold", None) is not None:
        cargs.extend(cifti_extrema_threshold_cargs(params.get("threshold", None), execution))
    if params.get("opt_merged_volume", False):
        cargs.append("-merged-volume")
    if params.get("opt_sum_maps", False):
        cargs.append("-sum-maps")
    if params.get("opt_consolidate_mode", False):
        cargs.append("-consolidate-mode")
    if params.get("opt_only_maxima", False):
        cargs.append("-only-maxima")
    if params.get("opt_only_minima", False):
        cargs.append("-only-minima")
    return cargs


def cifti_extrema_outputs(
    params: CiftiExtremaParameters,
    execution: Execution,
) -> CiftiExtremaOutputs:
    """
    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 = CiftiExtremaOutputs(
        root=execution.output_file("."),
        cifti_out=execution.output_file(params.get("cifti_out", None)),
    )
    return ret


def cifti_extrema_execute(
    params: CiftiExtremaParameters,
    runner: Runner | None = None,
) -> CiftiExtremaOutputs:
    """
    cifti-extrema
    
    Find extrema in a cifti file.
    
    Finds spatial locations in a cifti file that have more extreme values than
    all nearby locations in the same component (surface or volume structure).
    The input cifti file must have a brain models mapping along the specified
    direction. COLUMN is the direction that works on dtseries and dscalar. For
    dconn, if it is symmetric use COLUMN, otherwise use ROW.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiExtremaOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_EXTREMA_METADATA)
    params = execution.params(params)
    cargs = cifti_extrema_cargs(params, execution)
    ret = cifti_extrema_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_extrema(
    cifti: InputPathType,
    surface_distance: float,
    volume_distance: float,
    direction: str,
    cifti_out: str,
    opt_left_surface_surface: InputPathType | None = None,
    opt_right_surface_surface: InputPathType | None = None,
    opt_cerebellum_surface_surface: InputPathType | None = None,
    opt_surface_presmooth_surface_kernel: float | None = None,
    opt_volume_presmooth_volume_kernel: float | None = None,
    opt_presmooth_fwhm: bool = False,
    threshold: CiftiExtremaThresholdParameters | None = None,
    opt_merged_volume: bool = False,
    opt_sum_maps: bool = False,
    opt_consolidate_mode: bool = False,
    opt_only_maxima: bool = False,
    opt_only_minima: bool = False,
    runner: Runner | None = None,
) -> CiftiExtremaOutputs:
    """
    cifti-extrema
    
    Find extrema in a cifti file.
    
    Finds spatial locations in a cifti file that have more extreme values than
    all nearby locations in the same component (surface or volume structure).
    The input cifti file must have a brain models mapping along the specified
    direction. COLUMN is the direction that works on dtseries and dscalar. For
    dconn, if it is symmetric use COLUMN, otherwise use ROW.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        cifti: the input cifti.
        surface_distance: the minimum distance between extrema of the same\
            type, for surface components.
        volume_distance: the minimum distance between extrema of the same type,\
            for volume components.
        direction: which dimension to find extrema along, ROW or COLUMN.
        cifti_out: the output cifti.
        opt_left_surface_surface: specify the left surface to use: the left\
            surface file.
        opt_right_surface_surface: specify the right surface to use: the right\
            surface file.
        opt_cerebellum_surface_surface: specify the cerebellum surface to use:\
            the cerebellum surface file.
        opt_surface_presmooth_surface_kernel: smooth on the surface before\
            finding extrema: the size of the gaussian surface smoothing kernel in\
            mm, as sigma by default.
        opt_volume_presmooth_volume_kernel: smooth volume components before\
            finding extrema: the size of the gaussian volume smoothing kernel in\
            mm, as sigma by default.
        opt_presmooth_fwhm: smoothing kernel distances are FWHM, not sigma.
        threshold: ignore small extrema.
        opt_merged_volume: treat volume components as if they were a single\
            component.
        opt_sum_maps: output the sum of the extrema maps instead of each map\
            separately.
        opt_consolidate_mode: use consolidation of local minima instead of a\
            large neighborhood.
        opt_only_maxima: only find the maxima.
        opt_only_minima: only find the minima.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiExtremaOutputs`).
    """
    params = cifti_extrema_params(
        cifti=cifti,
        surface_distance=surface_distance,
        volume_distance=volume_distance,
        direction=direction,
        cifti_out=cifti_out,
        opt_left_surface_surface=opt_left_surface_surface,
        opt_right_surface_surface=opt_right_surface_surface,
        opt_cerebellum_surface_surface=opt_cerebellum_surface_surface,
        opt_surface_presmooth_surface_kernel=opt_surface_presmooth_surface_kernel,
        opt_volume_presmooth_volume_kernel=opt_volume_presmooth_volume_kernel,
        opt_presmooth_fwhm=opt_presmooth_fwhm,
        threshold=threshold,
        opt_merged_volume=opt_merged_volume,
        opt_sum_maps=opt_sum_maps,
        opt_consolidate_mode=opt_consolidate_mode,
        opt_only_maxima=opt_only_maxima,
        opt_only_minima=opt_only_minima,
    )
    return cifti_extrema_execute(params, runner)


__all__ = [
    "CIFTI_EXTREMA_METADATA",
    "CiftiExtremaOutputs",
    "cifti_extrema",
    "cifti_extrema_execute",
    "cifti_extrema_params",
    "cifti_extrema_threshold_params",
]
