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

import typing
import pathlib
from styxdefs import *

CIFTI_PARCELLATE_METADATA = Metadata(
    id="09eebc79465dbc1545219d2adbd3332cbcb71232.boutiques",
    name="cifti-parcellate",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


CiftiParcellateSpatialWeightsParameters = typing.TypedDict('CiftiParcellateSpatialWeightsParameters', {
    "@type": typing.NotRequired[typing.Literal["spatial_weights"]],
    "opt_left_area_surf_left_surf": typing.NotRequired[InputPathType | None],
    "opt_right_area_surf_right_surf": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_area_surf_cerebellum_surf": typing.NotRequired[InputPathType | None],
    "opt_left_area_metric_left_metric": typing.NotRequired[InputPathType | None],
    "opt_right_area_metric_right_metric": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_area_metric_cerebellum_metric": typing.NotRequired[InputPathType | None],
})
CiftiParcellateSpatialWeightsParametersTagged = typing.TypedDict('CiftiParcellateSpatialWeightsParametersTagged', {
    "@type": typing.Literal["spatial_weights"],
    "opt_left_area_surf_left_surf": typing.NotRequired[InputPathType | None],
    "opt_right_area_surf_right_surf": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_area_surf_cerebellum_surf": typing.NotRequired[InputPathType | None],
    "opt_left_area_metric_left_metric": typing.NotRequired[InputPathType | None],
    "opt_right_area_metric_right_metric": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_area_metric_cerebellum_metric": typing.NotRequired[InputPathType | None],
})


CiftiParcellateExcludeOutliersParameters = typing.TypedDict('CiftiParcellateExcludeOutliersParameters', {
    "@type": typing.NotRequired[typing.Literal["exclude_outliers"]],
    "sigma_below": float,
    "sigma_above": float,
})
CiftiParcellateExcludeOutliersParametersTagged = typing.TypedDict('CiftiParcellateExcludeOutliersParametersTagged', {
    "@type": typing.Literal["exclude_outliers"],
    "sigma_below": float,
    "sigma_above": float,
})


CiftiParcellateParameters = typing.TypedDict('CiftiParcellateParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/cifti-parcellate"]],
    "cifti_in": InputPathType,
    "cifti_label": InputPathType,
    "direction": str,
    "cifti_out": str,
    "spatial_weights": typing.NotRequired[CiftiParcellateSpatialWeightsParameters | None],
    "opt_cifti_weights_weight_cifti": typing.NotRequired[InputPathType | None],
    "opt_method_method": typing.NotRequired[str | None],
    "exclude_outliers": typing.NotRequired[CiftiParcellateExcludeOutliersParameters | None],
    "opt_only_numeric": bool,
    "opt_fill_value_value": typing.NotRequired[float | None],
    "opt_nonempty_mask_out_mask_out": typing.NotRequired[str | None],
    "opt_legacy_mode": bool,
    "opt_include_empty": bool,
})
CiftiParcellateParametersTagged = typing.TypedDict('CiftiParcellateParametersTagged', {
    "@type": typing.Literal["workbench/cifti-parcellate"],
    "cifti_in": InputPathType,
    "cifti_label": InputPathType,
    "direction": str,
    "cifti_out": str,
    "spatial_weights": typing.NotRequired[CiftiParcellateSpatialWeightsParameters | None],
    "opt_cifti_weights_weight_cifti": typing.NotRequired[InputPathType | None],
    "opt_method_method": typing.NotRequired[str | None],
    "exclude_outliers": typing.NotRequired[CiftiParcellateExcludeOutliersParameters | None],
    "opt_only_numeric": bool,
    "opt_fill_value_value": typing.NotRequired[float | None],
    "opt_nonempty_mask_out_mask_out": typing.NotRequired[str | None],
    "opt_legacy_mode": bool,
    "opt_include_empty": bool,
})


def cifti_parcellate_spatial_weights_params(
    opt_left_area_surf_left_surf: InputPathType | None = None,
    opt_right_area_surf_right_surf: InputPathType | None = None,
    opt_cerebellum_area_surf_cerebellum_surf: InputPathType | None = None,
    opt_left_area_metric_left_metric: InputPathType | None = None,
    opt_right_area_metric_right_metric: InputPathType | None = None,
    opt_cerebellum_area_metric_cerebellum_metric: InputPathType | None = None,
) -> CiftiParcellateSpatialWeightsParametersTagged:
    """
    Build parameters.
    
    Args:
        opt_left_area_surf_left_surf: use a surface for left vertex areas: the\
            left surface to use, areas are in mm^2.
        opt_right_area_surf_right_surf: use a surface for right vertex areas:\
            the right surface to use, areas are in mm^2.
        opt_cerebellum_area_surf_cerebellum_surf: use a surface for cerebellum\
            vertex areas: the cerebellum surface to use, areas are in mm^2.
        opt_left_area_metric_left_metric: use a metric file for left vertex\
            weights: metric file containing left vertex weights.
        opt_right_area_metric_right_metric: use a metric file for right vertex\
            weights: metric file containing right vertex weights.
        opt_cerebellum_area_metric_cerebellum_metric: use a metric file for\
            cerebellum vertex weights: metric file containing cerebellum vertex\
            weights.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "spatial_weights",
    }
    if opt_left_area_surf_left_surf is not None:
        params["opt_left_area_surf_left_surf"] = opt_left_area_surf_left_surf
    if opt_right_area_surf_right_surf is not None:
        params["opt_right_area_surf_right_surf"] = opt_right_area_surf_right_surf
    if opt_cerebellum_area_surf_cerebellum_surf is not None:
        params["opt_cerebellum_area_surf_cerebellum_surf"] = opt_cerebellum_area_surf_cerebellum_surf
    if opt_left_area_metric_left_metric is not None:
        params["opt_left_area_metric_left_metric"] = opt_left_area_metric_left_metric
    if opt_right_area_metric_right_metric is not None:
        params["opt_right_area_metric_right_metric"] = opt_right_area_metric_right_metric
    if opt_cerebellum_area_metric_cerebellum_metric is not None:
        params["opt_cerebellum_area_metric_cerebellum_metric"] = opt_cerebellum_area_metric_cerebellum_metric
    return params


def cifti_parcellate_spatial_weights_cargs(
    params: CiftiParcellateSpatialWeightsParameters,
    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("-spatial-weights")
    if params.get("opt_left_area_surf_left_surf", None) is not None:
        cargs.extend([
            "-left-area-surf",
            execution.input_file(params.get("opt_left_area_surf_left_surf", None))
        ])
    if params.get("opt_right_area_surf_right_surf", None) is not None:
        cargs.extend([
            "-right-area-surf",
            execution.input_file(params.get("opt_right_area_surf_right_surf", None))
        ])
    if params.get("opt_cerebellum_area_surf_cerebellum_surf", None) is not None:
        cargs.extend([
            "-cerebellum-area-surf",
            execution.input_file(params.get("opt_cerebellum_area_surf_cerebellum_surf", None))
        ])
    if params.get("opt_left_area_metric_left_metric", None) is not None:
        cargs.extend([
            "-left-area-metric",
            execution.input_file(params.get("opt_left_area_metric_left_metric", None))
        ])
    if params.get("opt_right_area_metric_right_metric", None) is not None:
        cargs.extend([
            "-right-area-metric",
            execution.input_file(params.get("opt_right_area_metric_right_metric", None))
        ])
    if params.get("opt_cerebellum_area_metric_cerebellum_metric", None) is not None:
        cargs.extend([
            "-cerebellum-area-metric",
            execution.input_file(params.get("opt_cerebellum_area_metric_cerebellum_metric", None))
        ])
    return cargs


def cifti_parcellate_exclude_outliers_params(
    sigma_below: float,
    sigma_above: float,
) -> CiftiParcellateExcludeOutliersParametersTagged:
    """
    Build parameters.
    
    Args:
        sigma_below: number of standard deviations below the mean to include.
        sigma_above: number of standard deviations above the mean to include.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "exclude_outliers",
        "sigma_below": sigma_below,
        "sigma_above": sigma_above,
    }
    return params


def cifti_parcellate_exclude_outliers_cargs(
    params: CiftiParcellateExcludeOutliersParameters,
    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("-exclude-outliers")
    cargs.append(str(params.get("sigma_below", None)))
    cargs.append(str(params.get("sigma_above", None)))
    return cargs


class CiftiParcellateOutputs(typing.NamedTuple):
    """
    Output object returned when calling `CiftiParcellateParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    cifti_out: OutputPathType
    """output cifti file"""
    opt_nonempty_mask_out_mask_out: OutputPathType | None
    """output a matching pscalar file that has 0s in empty parcels, and 1s
    elsewhere: the output mask file"""


def cifti_parcellate_params(
    cifti_in: InputPathType,
    cifti_label: InputPathType,
    direction: str,
    cifti_out: str,
    spatial_weights: CiftiParcellateSpatialWeightsParameters | None = None,
    opt_cifti_weights_weight_cifti: InputPathType | None = None,
    opt_method_method: str | None = None,
    exclude_outliers: CiftiParcellateExcludeOutliersParameters | None = None,
    opt_only_numeric: bool = False,
    opt_fill_value_value: float | None = None,
    opt_nonempty_mask_out_mask_out: str | None = None,
    opt_legacy_mode: bool = False,
    opt_include_empty: bool = False,
) -> CiftiParcellateParametersTagged:
    """
    Build parameters.
    
    Args:
        cifti_in: the cifti file to parcellate.
        cifti_label: a cifti label file to use for the parcellation.
        direction: which mapping to parcellate (integer, ROW, or COLUMN).
        cifti_out: output cifti file.
        spatial_weights: use voxel volume and either vertex areas or metric\
            files as weights.
        opt_cifti_weights_weight_cifti: use a cifti file containing weights:\
            the weights to use, as a cifti file.
        opt_method_method: specify method of parcellation (default MEAN, or\
            MODE if label data): the method to use to assign parcel values from the\
            values of member brainordinates.
        exclude_outliers: exclude non-numeric values and outliers from each\
            parcel by standard deviation.
        opt_only_numeric: exclude non-numeric values.
        opt_fill_value_value: specify value to use in empty parcels (default\
            0): the value to fill empty parcels with.
        opt_nonempty_mask_out_mask_out: output a matching pscalar file that has\
            0s in empty parcels, and 1s elsewhere: the output mask file.
        opt_legacy_mode: use the old behavior, parcels are defined by the\
            intersection between labels and valid data, and empty parcels are\
            discarded.
        opt_include_empty: deprecated: now the default behavior.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-parcellate",
        "cifti_in": cifti_in,
        "cifti_label": cifti_label,
        "direction": direction,
        "cifti_out": cifti_out,
        "opt_only_numeric": opt_only_numeric,
        "opt_legacy_mode": opt_legacy_mode,
        "opt_include_empty": opt_include_empty,
    }
    if spatial_weights is not None:
        params["spatial_weights"] = spatial_weights
    if opt_cifti_weights_weight_cifti is not None:
        params["opt_cifti_weights_weight_cifti"] = opt_cifti_weights_weight_cifti
    if opt_method_method is not None:
        params["opt_method_method"] = opt_method_method
    if exclude_outliers is not None:
        params["exclude_outliers"] = exclude_outliers
    if opt_fill_value_value is not None:
        params["opt_fill_value_value"] = opt_fill_value_value
    if opt_nonempty_mask_out_mask_out is not None:
        params["opt_nonempty_mask_out_mask_out"] = opt_nonempty_mask_out_mask_out
    return params


def cifti_parcellate_cargs(
    params: CiftiParcellateParameters,
    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-parcellate")
    cargs.append(execution.input_file(params.get("cifti_in", None)))
    cargs.append(execution.input_file(params.get("cifti_label", None)))
    cargs.append(params.get("direction", None))
    cargs.append(params.get("cifti_out", None))
    if params.get("spatial_weights", None) is not None:
        cargs.extend(cifti_parcellate_spatial_weights_cargs(params.get("spatial_weights", None), execution))
    if params.get("opt_cifti_weights_weight_cifti", None) is not None:
        cargs.extend([
            "-cifti-weights",
            execution.input_file(params.get("opt_cifti_weights_weight_cifti", None))
        ])
    if params.get("opt_method_method", None) is not None:
        cargs.extend([
            "-method",
            params.get("opt_method_method", None)
        ])
    if params.get("exclude_outliers", None) is not None:
        cargs.extend(cifti_parcellate_exclude_outliers_cargs(params.get("exclude_outliers", None), execution))
    if params.get("opt_only_numeric", False):
        cargs.append("-only-numeric")
    if params.get("opt_fill_value_value", None) is not None:
        cargs.extend([
            "-fill-value",
            str(params.get("opt_fill_value_value", None))
        ])
    if params.get("opt_nonempty_mask_out_mask_out", None) is not None:
        cargs.extend([
            "-nonempty-mask-out",
            params.get("opt_nonempty_mask_out_mask_out", None)
        ])
    if params.get("opt_legacy_mode", False):
        cargs.append("-legacy-mode")
    if params.get("opt_include_empty", False):
        cargs.append("-include-empty")
    return cargs


def cifti_parcellate_outputs(
    params: CiftiParcellateParameters,
    execution: Execution,
) -> CiftiParcellateOutputs:
    """
    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 = CiftiParcellateOutputs(
        root=execution.output_file("."),
        cifti_out=execution.output_file(params.get("cifti_out", None)),
        opt_nonempty_mask_out_mask_out=execution.output_file(params.get("opt_nonempty_mask_out_mask_out", None)) if (params.get("opt_nonempty_mask_out_mask_out") is not None) else None,
    )
    return ret


def cifti_parcellate_execute(
    params: CiftiParcellateParameters,
    runner: Runner | None = None,
) -> CiftiParcellateOutputs:
    """
    cifti-parcellate
    
    Parcellate a cifti file.
    
    Each label (other than the unlabeled key) in the cifti label file will be
    treated as a parcel, and all rows or columns of data within the parcel are
    averaged together to form the parcel's output row or column. If -legacy-mode
    is specified, parcels will be defined as the overlap between a label and the
    data, with no errors for missing data vertices or voxels, and empty parcels
    discarded. The direction can be either an integer starting from 1, or the
    strings 'ROW' or 'COLUMN'. For dtseries or dscalar, use COLUMN. If you are
    parcellating a dconn in both directions, parcellating by ROW first will use
    much less memory.
    
    NOTE: the parcels in the output file are sorted by the numeric label keys,
    in ascending order.
    
    The parameter to the -method option must be one of the following:
    
    MAX: the maximum value
    MIN: the minimum value
    INDEXMAX: the 1-based index of the maximum value
    INDEXMIN: the 1-based index of the minimum value
    SUM: add all values
    PRODUCT: multiply all values
    MEAN: the mean of the data
    STDEV: the standard deviation (N denominator)
    SAMPSTDEV: the sample standard deviation (N-1 denominator)
    VARIANCE: the variance of the data
    TSNR: mean divided by sample standard deviation (N-1 denominator)
    COV: sample standard deviation (N-1 denominator) divided by mean
    L2NORM: square root of sum of squares
    MEDIAN: the median of the data
    MODE: the mode of the data
    COUNT_NONZERO: the number of nonzero elements in the data
    
    The -*-weights options are mutually exclusive and may only be used with MEAN
    (default), SUM, STDEV, SAMPSTDEV, VARIANCE, MEDIAN, or MODE (default for
    label data).
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiParcellateOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_PARCELLATE_METADATA)
    params = execution.params(params)
    cargs = cifti_parcellate_cargs(params, execution)
    ret = cifti_parcellate_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_parcellate(
    cifti_in: InputPathType,
    cifti_label: InputPathType,
    direction: str,
    cifti_out: str,
    spatial_weights: CiftiParcellateSpatialWeightsParameters | None = None,
    opt_cifti_weights_weight_cifti: InputPathType | None = None,
    opt_method_method: str | None = None,
    exclude_outliers: CiftiParcellateExcludeOutliersParameters | None = None,
    opt_only_numeric: bool = False,
    opt_fill_value_value: float | None = None,
    opt_nonempty_mask_out_mask_out: str | None = None,
    opt_legacy_mode: bool = False,
    opt_include_empty: bool = False,
    runner: Runner | None = None,
) -> CiftiParcellateOutputs:
    """
    cifti-parcellate
    
    Parcellate a cifti file.
    
    Each label (other than the unlabeled key) in the cifti label file will be
    treated as a parcel, and all rows or columns of data within the parcel are
    averaged together to form the parcel's output row or column. If -legacy-mode
    is specified, parcels will be defined as the overlap between a label and the
    data, with no errors for missing data vertices or voxels, and empty parcels
    discarded. The direction can be either an integer starting from 1, or the
    strings 'ROW' or 'COLUMN'. For dtseries or dscalar, use COLUMN. If you are
    parcellating a dconn in both directions, parcellating by ROW first will use
    much less memory.
    
    NOTE: the parcels in the output file are sorted by the numeric label keys,
    in ascending order.
    
    The parameter to the -method option must be one of the following:
    
    MAX: the maximum value
    MIN: the minimum value
    INDEXMAX: the 1-based index of the maximum value
    INDEXMIN: the 1-based index of the minimum value
    SUM: add all values
    PRODUCT: multiply all values
    MEAN: the mean of the data
    STDEV: the standard deviation (N denominator)
    SAMPSTDEV: the sample standard deviation (N-1 denominator)
    VARIANCE: the variance of the data
    TSNR: mean divided by sample standard deviation (N-1 denominator)
    COV: sample standard deviation (N-1 denominator) divided by mean
    L2NORM: square root of sum of squares
    MEDIAN: the median of the data
    MODE: the mode of the data
    COUNT_NONZERO: the number of nonzero elements in the data
    
    The -*-weights options are mutually exclusive and may only be used with MEAN
    (default), SUM, STDEV, SAMPSTDEV, VARIANCE, MEDIAN, or MODE (default for
    label data).
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        cifti_in: the cifti file to parcellate.
        cifti_label: a cifti label file to use for the parcellation.
        direction: which mapping to parcellate (integer, ROW, or COLUMN).
        cifti_out: output cifti file.
        spatial_weights: use voxel volume and either vertex areas or metric\
            files as weights.
        opt_cifti_weights_weight_cifti: use a cifti file containing weights:\
            the weights to use, as a cifti file.
        opt_method_method: specify method of parcellation (default MEAN, or\
            MODE if label data): the method to use to assign parcel values from the\
            values of member brainordinates.
        exclude_outliers: exclude non-numeric values and outliers from each\
            parcel by standard deviation.
        opt_only_numeric: exclude non-numeric values.
        opt_fill_value_value: specify value to use in empty parcels (default\
            0): the value to fill empty parcels with.
        opt_nonempty_mask_out_mask_out: output a matching pscalar file that has\
            0s in empty parcels, and 1s elsewhere: the output mask file.
        opt_legacy_mode: use the old behavior, parcels are defined by the\
            intersection between labels and valid data, and empty parcels are\
            discarded.
        opt_include_empty: deprecated: now the default behavior.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiParcellateOutputs`).
    """
    params = cifti_parcellate_params(
        cifti_in=cifti_in,
        cifti_label=cifti_label,
        direction=direction,
        cifti_out=cifti_out,
        spatial_weights=spatial_weights,
        opt_cifti_weights_weight_cifti=opt_cifti_weights_weight_cifti,
        opt_method_method=opt_method_method,
        exclude_outliers=exclude_outliers,
        opt_only_numeric=opt_only_numeric,
        opt_fill_value_value=opt_fill_value_value,
        opt_nonempty_mask_out_mask_out=opt_nonempty_mask_out_mask_out,
        opt_legacy_mode=opt_legacy_mode,
        opt_include_empty=opt_include_empty,
    )
    return cifti_parcellate_execute(params, runner)


__all__ = [
    "CIFTI_PARCELLATE_METADATA",
    "CiftiParcellateOutputs",
    "cifti_parcellate",
    "cifti_parcellate_exclude_outliers_params",
    "cifti_parcellate_execute",
    "cifti_parcellate_params",
    "cifti_parcellate_spatial_weights_params",
]
