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

import typing
import pathlib
from styxdefs import *

CIFTI_CORRELATION_METADATA = Metadata(
    id="af245290fbe6593faa46f40cb576c436c697b361.boutiques",
    name="cifti-correlation",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


CiftiCorrelationRoiOverrideParameters = typing.TypedDict('CiftiCorrelationRoiOverrideParameters', {
    "@type": typing.NotRequired[typing.Literal["roi_override"]],
    "opt_left_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_right_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_vol_roi_roi_vol": typing.NotRequired[InputPathType | None],
    "opt_cifti_roi_roi_cifti": typing.NotRequired[InputPathType | None],
})
CiftiCorrelationRoiOverrideParametersTagged = typing.TypedDict('CiftiCorrelationRoiOverrideParametersTagged', {
    "@type": typing.Literal["roi_override"],
    "opt_left_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_right_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_cerebellum_roi_roi_metric": typing.NotRequired[InputPathType | None],
    "opt_vol_roi_roi_vol": typing.NotRequired[InputPathType | None],
    "opt_cifti_roi_roi_cifti": typing.NotRequired[InputPathType | None],
})


CiftiCorrelationParameters = typing.TypedDict('CiftiCorrelationParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/cifti-correlation"]],
    "cifti": InputPathType,
    "cifti_out": str,
    "roi_override": typing.NotRequired[CiftiCorrelationRoiOverrideParameters | None],
    "opt_weights_weight_file": typing.NotRequired[str | None],
    "opt_fisher_z": bool,
    "opt_no_demean": bool,
    "opt_covariance": bool,
    "opt_mem_limit_limit_gb": typing.NotRequired[float | None],
})
CiftiCorrelationParametersTagged = typing.TypedDict('CiftiCorrelationParametersTagged', {
    "@type": typing.Literal["workbench/cifti-correlation"],
    "cifti": InputPathType,
    "cifti_out": str,
    "roi_override": typing.NotRequired[CiftiCorrelationRoiOverrideParameters | None],
    "opt_weights_weight_file": typing.NotRequired[str | None],
    "opt_fisher_z": bool,
    "opt_no_demean": bool,
    "opt_covariance": bool,
    "opt_mem_limit_limit_gb": typing.NotRequired[float | None],
})


def cifti_correlation_roi_override_params(
    opt_left_roi_roi_metric: InputPathType | None = None,
    opt_right_roi_roi_metric: InputPathType | None = None,
    opt_cerebellum_roi_roi_metric: InputPathType | None = None,
    opt_vol_roi_roi_vol: InputPathType | None = None,
    opt_cifti_roi_roi_cifti: InputPathType | None = None,
) -> CiftiCorrelationRoiOverrideParametersTagged:
    """
    Build parameters.
    
    Args:
        opt_left_roi_roi_metric: use an roi for left hempsphere: the left roi\
            as a metric file.
        opt_right_roi_roi_metric: use an roi for right hempsphere: the right\
            roi as a metric file.
        opt_cerebellum_roi_roi_metric: use an roi for cerebellum: the\
            cerebellum roi as a metric file.
        opt_vol_roi_roi_vol: use an roi for volume: the volume roi file.
        opt_cifti_roi_roi_cifti: use a cifti file for combined rois: the cifti\
            roi file.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "roi_override",
    }
    if opt_left_roi_roi_metric is not None:
        params["opt_left_roi_roi_metric"] = opt_left_roi_roi_metric
    if opt_right_roi_roi_metric is not None:
        params["opt_right_roi_roi_metric"] = opt_right_roi_roi_metric
    if opt_cerebellum_roi_roi_metric is not None:
        params["opt_cerebellum_roi_roi_metric"] = opt_cerebellum_roi_roi_metric
    if opt_vol_roi_roi_vol is not None:
        params["opt_vol_roi_roi_vol"] = opt_vol_roi_roi_vol
    if opt_cifti_roi_roi_cifti is not None:
        params["opt_cifti_roi_roi_cifti"] = opt_cifti_roi_roi_cifti
    return params


def cifti_correlation_roi_override_cargs(
    params: CiftiCorrelationRoiOverrideParameters,
    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("-roi-override")
    if params.get("opt_left_roi_roi_metric", None) is not None:
        cargs.extend([
            "-left-roi",
            execution.input_file(params.get("opt_left_roi_roi_metric", None))
        ])
    if params.get("opt_right_roi_roi_metric", None) is not None:
        cargs.extend([
            "-right-roi",
            execution.input_file(params.get("opt_right_roi_roi_metric", None))
        ])
    if params.get("opt_cerebellum_roi_roi_metric", None) is not None:
        cargs.extend([
            "-cerebellum-roi",
            execution.input_file(params.get("opt_cerebellum_roi_roi_metric", None))
        ])
    if params.get("opt_vol_roi_roi_vol", None) is not None:
        cargs.extend([
            "-vol-roi",
            execution.input_file(params.get("opt_vol_roi_roi_vol", None))
        ])
    if params.get("opt_cifti_roi_roi_cifti", None) is not None:
        cargs.extend([
            "-cifti-roi",
            execution.input_file(params.get("opt_cifti_roi_roi_cifti", None))
        ])
    return cargs


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


def cifti_correlation_params(
    cifti: InputPathType,
    cifti_out: str,
    roi_override: CiftiCorrelationRoiOverrideParameters | None = None,
    opt_weights_weight_file: str | None = None,
    opt_fisher_z: bool = False,
    opt_no_demean: bool = False,
    opt_covariance: bool = False,
    opt_mem_limit_limit_gb: float | None = None,
) -> CiftiCorrelationParametersTagged:
    """
    Build parameters.
    
    Args:
        cifti: input cifti file.
        cifti_out: output cifti file.
        roi_override: perform correlation from a subset of rows to all rows.
        opt_weights_weight_file: specify column weights: text file containing\
            one weight per column.
        opt_fisher_z: apply fisher small z transform (ie, artanh) to\
            correlation.
        opt_no_demean: instead of correlation, do dot product of rows, then\
            normalize by diagonal.
        opt_covariance: compute covariance instead of correlation.
        opt_mem_limit_limit_gb: restrict memory usage: memory limit in\
            gigabytes.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-correlation",
        "cifti": cifti,
        "cifti_out": cifti_out,
        "opt_fisher_z": opt_fisher_z,
        "opt_no_demean": opt_no_demean,
        "opt_covariance": opt_covariance,
    }
    if roi_override is not None:
        params["roi_override"] = roi_override
    if opt_weights_weight_file is not None:
        params["opt_weights_weight_file"] = opt_weights_weight_file
    if opt_mem_limit_limit_gb is not None:
        params["opt_mem_limit_limit_gb"] = opt_mem_limit_limit_gb
    return params


def cifti_correlation_cargs(
    params: CiftiCorrelationParameters,
    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-correlation")
    cargs.append(execution.input_file(params.get("cifti", None)))
    cargs.append(params.get("cifti_out", None))
    if params.get("roi_override", None) is not None:
        cargs.extend(cifti_correlation_roi_override_cargs(params.get("roi_override", None), execution))
    if params.get("opt_weights_weight_file", None) is not None:
        cargs.extend([
            "-weights",
            params.get("opt_weights_weight_file", None)
        ])
    if params.get("opt_fisher_z", False):
        cargs.append("-fisher-z")
    if params.get("opt_no_demean", False):
        cargs.append("-no-demean")
    if params.get("opt_covariance", False):
        cargs.append("-covariance")
    if params.get("opt_mem_limit_limit_gb", None) is not None:
        cargs.extend([
            "-mem-limit",
            str(params.get("opt_mem_limit_limit_gb", None))
        ])
    return cargs


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


def cifti_correlation_execute(
    params: CiftiCorrelationParameters,
    runner: Runner | None = None,
) -> CiftiCorrelationOutputs:
    """
    cifti-correlation
    
    Generate correlation of rows in a cifti file.
    
    For each row (or each row inside an roi if -roi-override is specified),
    correlate to all other rows. The -cifti-roi suboption to -roi-override may
    not be specified with any other -*-roi suboption, but you may specify the
    other -*-roi suboptions together.
    
    When using the -fisher-z option, the output is NOT a Z-score, it is
    artanh(r), to do further math on this output, consider using -cifti-math.
    
    Restricting the memory usage will make it calculate the output in chunks,
    and if the input file size is more than 70% of the memory limit, it will
    also read through the input file as rows are required, resulting in several
    passes through the input file (once per chunk). Memory limit does not need
    to be an integer, you may also specify 0 to calculate a single output row at
    a time (this may be very slow).
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiCorrelationOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_CORRELATION_METADATA)
    params = execution.params(params)
    cargs = cifti_correlation_cargs(params, execution)
    ret = cifti_correlation_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_correlation(
    cifti: InputPathType,
    cifti_out: str,
    roi_override: CiftiCorrelationRoiOverrideParameters | None = None,
    opt_weights_weight_file: str | None = None,
    opt_fisher_z: bool = False,
    opt_no_demean: bool = False,
    opt_covariance: bool = False,
    opt_mem_limit_limit_gb: float | None = None,
    runner: Runner | None = None,
) -> CiftiCorrelationOutputs:
    """
    cifti-correlation
    
    Generate correlation of rows in a cifti file.
    
    For each row (or each row inside an roi if -roi-override is specified),
    correlate to all other rows. The -cifti-roi suboption to -roi-override may
    not be specified with any other -*-roi suboption, but you may specify the
    other -*-roi suboptions together.
    
    When using the -fisher-z option, the output is NOT a Z-score, it is
    artanh(r), to do further math on this output, consider using -cifti-math.
    
    Restricting the memory usage will make it calculate the output in chunks,
    and if the input file size is more than 70% of the memory limit, it will
    also read through the input file as rows are required, resulting in several
    passes through the input file (once per chunk). Memory limit does not need
    to be an integer, you may also specify 0 to calculate a single output row at
    a time (this may be very slow).
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        cifti: input cifti file.
        cifti_out: output cifti file.
        roi_override: perform correlation from a subset of rows to all rows.
        opt_weights_weight_file: specify column weights: text file containing\
            one weight per column.
        opt_fisher_z: apply fisher small z transform (ie, artanh) to\
            correlation.
        opt_no_demean: instead of correlation, do dot product of rows, then\
            normalize by diagonal.
        opt_covariance: compute covariance instead of correlation.
        opt_mem_limit_limit_gb: restrict memory usage: memory limit in\
            gigabytes.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiCorrelationOutputs`).
    """
    params = cifti_correlation_params(
        cifti=cifti,
        cifti_out=cifti_out,
        roi_override=roi_override,
        opt_weights_weight_file=opt_weights_weight_file,
        opt_fisher_z=opt_fisher_z,
        opt_no_demean=opt_no_demean,
        opt_covariance=opt_covariance,
        opt_mem_limit_limit_gb=opt_mem_limit_limit_gb,
    )
    return cifti_correlation_execute(params, runner)


__all__ = [
    "CIFTI_CORRELATION_METADATA",
    "CiftiCorrelationOutputs",
    "cifti_correlation",
    "cifti_correlation_execute",
    "cifti_correlation_params",
    "cifti_correlation_roi_override_params",
]
