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

import typing
import pathlib
from styxdefs import *

LABEL_GEOMETRY_MEASURES_METADATA = Metadata(
    id="e086bc064897b80e13ee956cedd1472309a2db39.boutiques",
    name="LabelGeometryMeasures",
    package="ants",
    container_image_tag="antsx/ants:v2.5.3",
)


_LabelGeometryMeasuresParamsDictNoTag = typing.TypedDict('_LabelGeometryMeasuresParamsDictNoTag', {
    "image_dimension": int,
    "label_image": InputPathType,
    "intensity_image": typing.NotRequired[str | None],
    "csv_file": typing.NotRequired[InputPathType | None],
})
LabelGeometryMeasuresParamsDictTagged = typing.TypedDict('LabelGeometryMeasuresParamsDictTagged', {
    "@type": typing.Literal["ants/LabelGeometryMeasures"],
    "image_dimension": int,
    "label_image": InputPathType,
    "intensity_image": typing.NotRequired[str | None],
    "csv_file": typing.NotRequired[InputPathType | None],
})
LabelGeometryMeasuresParamsDict = _LabelGeometryMeasuresParamsDictNoTag | LabelGeometryMeasuresParamsDictTagged


class LabelGeometryMeasuresOutputs(typing.NamedTuple):
    """
    Output object returned when calling `LabelGeometryMeasuresParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output_csv: OutputPathType | None
    """The CSV file containing the geometry measures."""


def label_geometry_measures_params(
    image_dimension: int,
    label_image: InputPathType,
    intensity_image: str | None = None,
    csv_file: InputPathType | None = None,
) -> LabelGeometryMeasuresParamsDictTagged:
    """
    Build parameters.
    
    Args:
        image_dimension: The dimensionality of the input images (e.g., 2 for\
            2D, 3 for 3D).
        label_image: The label image on which geometry measures are computed.
        intensity_image: An optional intensity image for computing\
            intensity-weighted measures. Use "none" or "na" if not provided.
        csv_file: The output file where the geometry measures are written in\
            CSV format.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "ants/LabelGeometryMeasures",
        "image_dimension": image_dimension,
        "label_image": label_image,
    }
    if intensity_image is not None:
        params["intensity_image"] = intensity_image
    if csv_file is not None:
        params["csv_file"] = csv_file
    return params


def label_geometry_measures_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelGeometryMeasuresParamsDict` 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("image_dimension", None) is None:
        raise StyxValidationError("`image_dimension` must not be None")
    if not isinstance(params["image_dimension"], int):
        raise StyxValidationError(f'`image_dimension` has the wrong type: Received `{type(params.get("image_dimension", None))}` expected `int`')
    if params.get("label_image", None) is None:
        raise StyxValidationError("`label_image` must not be None")
    if not isinstance(params["label_image"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label_image` has the wrong type: Received `{type(params.get("label_image", None))}` expected `InputPathType`')
    if params.get("intensity_image", None) is not None:
        if not isinstance(params["intensity_image"], str):
            raise StyxValidationError(f'`intensity_image` has the wrong type: Received `{type(params.get("intensity_image", None))}` expected `str | None`')
    if params.get("csv_file", None) is not None:
        if not isinstance(params["csv_file"], (pathlib.Path, str)):
            raise StyxValidationError(f'`csv_file` has the wrong type: Received `{type(params.get("csv_file", None))}` expected `InputPathType | None`')


def label_geometry_measures_cargs(
    params: LabelGeometryMeasuresParamsDict,
    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("LabelGeometryMeasures")
    cargs.append(str(params.get("image_dimension", None)))
    cargs.append(execution.input_file(params.get("label_image", None)))
    if params.get("intensity_image", None) is not None:
        cargs.append(params.get("intensity_image", None))
    if params.get("csv_file", None) is not None:
        cargs.append(execution.input_file(params.get("csv_file", None)))
    return cargs


def label_geometry_measures_outputs(
    params: LabelGeometryMeasuresParamsDict,
    execution: Execution,
) -> LabelGeometryMeasuresOutputs:
    """
    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 = LabelGeometryMeasuresOutputs(
        root=execution.output_file("."),
        output_csv=execution.output_file(pathlib.Path(params.get("csv_file", None)).name) if (params.get("csv_file") is not None) else None,
    )
    return ret


def label_geometry_measures_execute(
    params: LabelGeometryMeasuresParamsDict,
    runner: Runner | None = None,
) -> LabelGeometryMeasuresOutputs:
    """
    LabelGeometryMeasures
    
    This tool computes various geometry measures on a label image, optionally
    using an intensity image, and outputs the results to a CSV file.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelGeometryMeasuresOutputs`).
    """
    label_geometry_measures_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(LABEL_GEOMETRY_MEASURES_METADATA)
    params = execution.params(params)
    cargs = label_geometry_measures_cargs(params, execution)
    ret = label_geometry_measures_outputs(params, execution)
    execution.run(cargs)
    return ret


def label_geometry_measures(
    image_dimension: int,
    label_image: InputPathType,
    intensity_image: str | None = None,
    csv_file: InputPathType | None = None,
    runner: Runner | None = None,
) -> LabelGeometryMeasuresOutputs:
    """
    LabelGeometryMeasures
    
    This tool computes various geometry measures on a label image, optionally
    using an intensity image, and outputs the results to a CSV file.
    
    Author: ANTs Developers
    
    URL: https://github.com/ANTsX/ANTs
    
    Args:
        image_dimension: The dimensionality of the input images (e.g., 2 for\
            2D, 3 for 3D).
        label_image: The label image on which geometry measures are computed.
        intensity_image: An optional intensity image for computing\
            intensity-weighted measures. Use "none" or "na" if not provided.
        csv_file: The output file where the geometry measures are written in\
            CSV format.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelGeometryMeasuresOutputs`).
    """
    params = label_geometry_measures_params(
        image_dimension=image_dimension,
        label_image=label_image,
        intensity_image=intensity_image,
        csv_file=csv_file,
    )
    return label_geometry_measures_execute(params, runner)


__all__ = [
    "LABEL_GEOMETRY_MEASURES_METADATA",
    "LabelGeometryMeasuresOutputs",
    "LabelGeometryMeasuresParamsDict",
    "LabelGeometryMeasuresParamsDictTagged",
    "label_geometry_measures",
    "label_geometry_measures_execute",
    "label_geometry_measures_params",
]
