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

import typing
import pathlib
from styxdefs import *

VOLUME_TFCE_METADATA = Metadata(
    id="88157dd1b5611c70b241d14e8a78bfe6302ab544.workbench",
    name="volume-tfce",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeTfcePresmoothParamsDictNoTag = typing.TypedDict('_VolumeTfcePresmoothParamsDictNoTag', {
    "kernel": float,
    "fwhm": bool,
})
VolumeTfcePresmoothParamsDictTagged = typing.TypedDict('VolumeTfcePresmoothParamsDictTagged', {
    "@type": typing.Literal["presmooth"],
    "kernel": float,
    "fwhm": bool,
})
VolumeTfcePresmoothParamsDict = _VolumeTfcePresmoothParamsDictNoTag | VolumeTfcePresmoothParamsDictTagged


_VolumeTfceParametersParamsDictNoTag = typing.TypedDict('_VolumeTfceParametersParamsDictNoTag', {
    "E": float,
    "H": float,
})
VolumeTfceParametersParamsDictTagged = typing.TypedDict('VolumeTfceParametersParamsDictTagged', {
    "@type": typing.Literal["parameters"],
    "E": float,
    "H": float,
})
VolumeTfceParametersParamsDict = _VolumeTfceParametersParamsDictNoTag | VolumeTfceParametersParamsDictTagged


_VolumeTfceParamsDictNoTag = typing.TypedDict('_VolumeTfceParamsDictNoTag', {
    "volume-in": InputPathType,
    "volume-out": str,
    "presmooth": typing.NotRequired[VolumeTfcePresmoothParamsDict | None],
    "parameters": typing.NotRequired[VolumeTfceParametersParamsDict | None],
    "subvolume": typing.NotRequired[str | None],
    "roi-volume": typing.NotRequired[InputPathType | None],
})
VolumeTfceParamsDictTagged = typing.TypedDict('VolumeTfceParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-tfce"],
    "volume-in": InputPathType,
    "volume-out": str,
    "presmooth": typing.NotRequired[VolumeTfcePresmoothParamsDict | None],
    "parameters": typing.NotRequired[VolumeTfceParametersParamsDict | None],
    "subvolume": typing.NotRequired[str | None],
    "roi-volume": typing.NotRequired[InputPathType | None],
})
VolumeTfceParamsDict = _VolumeTfceParamsDictNoTag | VolumeTfceParamsDictTagged


def volume_tfce_presmooth(
    kernel: float,
    fwhm: bool = False,
) -> VolumeTfcePresmoothParamsDictTagged:
    """
    Build parameters.
    
    Args:
        kernel: the size of the gaussian smoothing kernel in mm, as sigma by\
            default.
        fwhm: smoothing kernel size is FWHM, not sigma.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "presmooth",
        "kernel": kernel,
        "fwhm": fwhm,
    }
    return params


def volume_tfce_presmooth_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeTfcePresmoothParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("kernel", None) is None:
        raise StyxValidationError("`kernel` must not be None")
    if not isinstance(params["kernel"], (float, int)):
        raise StyxValidationError(f'`kernel` has the wrong type: Received `{type(params.get("kernel", None))}` expected `float`')
    if params.get("fwhm", False) is None:
        raise StyxValidationError("`fwhm` must not be None")
    if not isinstance(params["fwhm"], bool):
        raise StyxValidationError(f'`fwhm` has the wrong type: Received `{type(params.get("fwhm", False))}` expected `bool`')


def volume_tfce_presmooth_cargs(
    params: VolumeTfcePresmoothParamsDict,
    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.extend([
        "-presmooth",
        str(params.get("kernel", None))
    ])
    if params.get("fwhm", False):
        cargs.append("-fwhm")
    return cargs


def volume_tfce_parameters(
    e: float,
    h: float,
) -> VolumeTfceParametersParamsDictTagged:
    """
    Build parameters.
    
    Args:
        e: exponent for cluster volume (default 0.5).
        h: exponent for threshold value (default 2.0).
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "parameters",
        "E": e,
        "H": h,
    }
    return params


def volume_tfce_parameters_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeTfceParametersParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("E", None) is None:
        raise StyxValidationError("`E` must not be None")
    if not isinstance(params["E"], (float, int)):
        raise StyxValidationError(f'`E` has the wrong type: Received `{type(params.get("E", None))}` expected `float`')
    if params.get("H", None) is None:
        raise StyxValidationError("`H` must not be None")
    if not isinstance(params["H"], (float, int)):
        raise StyxValidationError(f'`H` has the wrong type: Received `{type(params.get("H", None))}` expected `float`')


def volume_tfce_parameters_cargs(
    params: VolumeTfceParametersParamsDict,
    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.extend([
        "-parameters",
        str(params.get("E", None)),
        str(params.get("H", None))
    ])
    return cargs


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


def volume_tfce_params(
    volume_in: InputPathType,
    volume_out: str,
    presmooth: VolumeTfcePresmoothParamsDict | None = None,
    parameters: VolumeTfceParametersParamsDict | None = None,
    subvolume: str | None = None,
    roi_volume: InputPathType | None = None,
) -> VolumeTfceParamsDictTagged:
    """
    Build parameters.
    
    Args:
        volume_in: the volume to run TFCE on.
        volume_out: the output volume.
        presmooth: smooth the volume before running TFCE.
        parameters: set parameters for TFCE integral.
        subvolume: select a single subvolume\
            \
            the subvolume number or name.
        roi_volume: select a region of interest to run TFCE on\
            \
            the area to run TFCE on, as a volume.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-tfce",
        "volume-in": volume_in,
        "volume-out": volume_out,
    }
    if presmooth is not None:
        params["presmooth"] = presmooth
    if parameters is not None:
        params["parameters"] = parameters
    if subvolume is not None:
        params["subvolume"] = subvolume
    if roi_volume is not None:
        params["roi-volume"] = roi_volume
    return params


def volume_tfce_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeTfceParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("volume-in", None) is None:
        raise StyxValidationError("`volume-in` must not be None")
    if not isinstance(params["volume-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`volume-in` has the wrong type: Received `{type(params.get("volume-in", None))}` expected `InputPathType`')
    if params.get("volume-out", None) is None:
        raise StyxValidationError("`volume-out` must not be None")
    if not isinstance(params["volume-out"], str):
        raise StyxValidationError(f'`volume-out` has the wrong type: Received `{type(params.get("volume-out", None))}` expected `str`')
    if params.get("presmooth", None) is not None:
        volume_tfce_presmooth_validate(params["presmooth"])
    if params.get("parameters", None) is not None:
        volume_tfce_parameters_validate(params["parameters"])
    if params.get("subvolume", None) is not None:
        if not isinstance(params["subvolume"], str):
            raise StyxValidationError(f'`subvolume` has the wrong type: Received `{type(params.get("subvolume", None))}` expected `str | None`')
    if params.get("roi-volume", None) is not None:
        if not isinstance(params["roi-volume"], (pathlib.Path, str)):
            raise StyxValidationError(f'`roi-volume` has the wrong type: Received `{type(params.get("roi-volume", None))}` expected `InputPathType | None`')


def volume_tfce_cargs(
    params: VolumeTfceParamsDict,
    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.extend([
        "wb_command",
        "-volume-tfce"
    ])
    cargs.append(execution.input_file(params.get("volume-in", None)))
    cargs.append(params.get("volume-out", None))
    if params.get("presmooth", None) is not None or params.get("parameters", None) is not None:
        cargs.extend([
            *(volume_tfce_presmooth_cargs(params.get("presmooth", None), execution) if (params.get("presmooth", None) is not None) else []),
            *(volume_tfce_parameters_cargs(params.get("parameters", None), execution) if (params.get("parameters", None) is not None) else [])
        ])
    if params.get("subvolume", None) is not None:
        cargs.extend([
            "-subvolume",
            params.get("subvolume", None)
        ])
    if params.get("roi-volume", None) is not None:
        cargs.extend([
            "-roi",
            execution.input_file(params.get("roi-volume", None))
        ])
    return cargs


def volume_tfce_outputs(
    params: VolumeTfceParamsDict,
    execution: Execution,
) -> VolumeTfceOutputs:
    """
    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 = VolumeTfceOutputs(
        root=execution.output_file("."),
        volume_out=execution.output_file(params.get("volume-out", None)),
    )
    return ret


def volume_tfce_execute(
    params: VolumeTfceParamsDict,
    runner: Runner | None = None,
) -> VolumeTfceOutputs:
    """
    DO TFCE ON A VOLUME FILE.
    
    This command does not do any statistical analysis. Please use something like
    PALM if you are just trying to do statistics on your data.
    
    Threshold-free cluster enhancement is a method to increase the relative
    value of regions that would form clusters in a standard thresholding test.
    This is accomplished by evaluating the integral of:
    
    e(h, p)^E * h^H * dh
    
    at each voxel p, where h ranges from 0 to the maximum value in the data, and
    e(h, p) is the extent of the cluster containing voxel p at threshold h.
    Negative values are similarly enhanced by negating the data, running the
    same process, and negating the result.
    
    This method is explained in: Smith SM, Nichols TE., "Threshold-free cluster
    enhancement: addressing problems of smoothing, threshold dependence and
    localisation in cluster inference." Neuroimage. 2009 Jan 1;44(1):83-98.
    PMID: 18501637.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeTfceOutputs`).
    """
    volume_tfce_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_TFCE_METADATA)
    params = execution.params(params)
    cargs = volume_tfce_cargs(params, execution)
    ret = volume_tfce_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_tfce(
    volume_in: InputPathType,
    volume_out: str,
    presmooth: VolumeTfcePresmoothParamsDict | None = None,
    parameters: VolumeTfceParametersParamsDict | None = None,
    subvolume: str | None = None,
    roi_volume: InputPathType | None = None,
    runner: Runner | None = None,
) -> VolumeTfceOutputs:
    """
    DO TFCE ON A VOLUME FILE.
    
    This command does not do any statistical analysis. Please use something like
    PALM if you are just trying to do statistics on your data.
    
    Threshold-free cluster enhancement is a method to increase the relative
    value of regions that would form clusters in a standard thresholding test.
    This is accomplished by evaluating the integral of:
    
    e(h, p)^E * h^H * dh
    
    at each voxel p, where h ranges from 0 to the maximum value in the data, and
    e(h, p) is the extent of the cluster containing voxel p at threshold h.
    Negative values are similarly enhanced by negating the data, running the
    same process, and negating the result.
    
    This method is explained in: Smith SM, Nichols TE., "Threshold-free cluster
    enhancement: addressing problems of smoothing, threshold dependence and
    localisation in cluster inference." Neuroimage. 2009 Jan 1;44(1):83-98.
    PMID: 18501637.
    
    Args:
        volume_in: the volume to run TFCE on.
        volume_out: the output volume.
        presmooth: smooth the volume before running TFCE.
        parameters: set parameters for TFCE integral.
        subvolume: select a single subvolume\
            \
            the subvolume number or name.
        roi_volume: select a region of interest to run TFCE on\
            \
            the area to run TFCE on, as a volume.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeTfceOutputs`).
    """
    params = volume_tfce_params(
        volume_in=volume_in,
        volume_out=volume_out,
        presmooth=presmooth,
        parameters=parameters,
        subvolume=subvolume,
        roi_volume=roi_volume,
    )
    return volume_tfce_execute(params, runner)


__all__ = [
    "VOLUME_TFCE_METADATA",
    "VolumeTfceOutputs",
    "VolumeTfceParametersParamsDict",
    "VolumeTfceParametersParamsDictTagged",
    "VolumeTfceParamsDict",
    "VolumeTfceParamsDictTagged",
    "VolumeTfcePresmoothParamsDict",
    "VolumeTfcePresmoothParamsDictTagged",
    "volume_tfce",
    "volume_tfce_execute",
    "volume_tfce_parameters",
    "volume_tfce_params",
    "volume_tfce_presmooth",
]
