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

import typing
import pathlib
from styxdefs import *

MRHISTMATCH_METADATA = Metadata(
    id="1798cd7ea1a79d9523cf1fd1a7ec35b94aa4a5ab.boutiques",
    name="mrhistmatch",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_MrhistmatchConfigParamsDictNoTag = typing.TypedDict('_MrhistmatchConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
MrhistmatchConfigParamsDictTagged = typing.TypedDict('MrhistmatchConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
MrhistmatchConfigParamsDict = _MrhistmatchConfigParamsDictNoTag | MrhistmatchConfigParamsDictTagged


_MrhistmatchParamsDictNoTag = typing.TypedDict('_MrhistmatchParamsDictNoTag', {
    "mask_input": typing.NotRequired[InputPathType | None],
    "mask_target": typing.NotRequired[InputPathType | None],
    "bins": typing.NotRequired[int | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrhistmatchConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "type": str,
    "input": InputPathType,
    "target": InputPathType,
    "output": str,
})
MrhistmatchParamsDictTagged = typing.TypedDict('MrhistmatchParamsDictTagged', {
    "@type": typing.Literal["mrtrix/mrhistmatch"],
    "mask_input": typing.NotRequired[InputPathType | None],
    "mask_target": typing.NotRequired[InputPathType | None],
    "bins": typing.NotRequired[int | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[MrhistmatchConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "type": str,
    "input": InputPathType,
    "target": InputPathType,
    "output": str,
})
MrhistmatchParamsDict = _MrhistmatchParamsDictNoTag | MrhistmatchParamsDictTagged


def mrhistmatch_config(
    key: str,
    value: str,
) -> MrhistmatchConfigParamsDictTagged:
    """
    Build parameters.
    
    Args:
        key: temporarily set the value of an MRtrix config file entry.
        value: temporarily set the value of an MRtrix config file entry.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "config",
        "key": key,
        "value": value,
    }
    return params


def mrhistmatch_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrhistmatchConfigParamsDict` 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("key", None) is None:
        raise StyxValidationError("`key` must not be None")
    if not isinstance(params["key"], str):
        raise StyxValidationError(f'`key` has the wrong type: Received `{type(params.get("key", None))}` expected `str`')
    if params.get("value", None) is None:
        raise StyxValidationError("`value` must not be None")
    if not isinstance(params["value"], str):
        raise StyxValidationError(f'`value` has the wrong type: Received `{type(params.get("value", None))}` expected `str`')


def mrhistmatch_config_cargs(
    params: MrhistmatchConfigParamsDict,
    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("-config")
    cargs.append(params.get("key", None))
    cargs.append(params.get("value", None))
    return cargs


class MrhistmatchOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MrhistmatchParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output: OutputPathType
    """the output image"""


def mrhistmatch_params(
    type_: str,
    input_: InputPathType,
    target: InputPathType,
    output: str,
    mask_input: InputPathType | None = None,
    mask_target: InputPathType | None = None,
    bins: int | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrhistmatchConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> MrhistmatchParamsDictTagged:
    """
    Build parameters.
    
    Args:
        type_: type of histogram matching to perform; options are:\
            scale,linear,nonlinear.
        input_: the input image to be modified.
        target: the input image from which to derive the target histogram.
        output: the output image.
        mask_input: only generate input histogram based on a specified binary\
            mask image.
        mask_target: only generate target histogram based on a specified binary\
            mask image.
        bins: the number of bins to use to generate the histograms.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "mrtrix/mrhistmatch",
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "type": type_,
        "input": input_,
        "target": target,
        "output": output,
    }
    if mask_input is not None:
        params["mask_input"] = mask_input
    if mask_target is not None:
        params["mask_target"] = mask_target
    if bins is not None:
        params["bins"] = bins
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def mrhistmatch_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MrhistmatchParamsDict` 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("mask_input", None) is not None:
        if not isinstance(params["mask_input"], (pathlib.Path, str)):
            raise StyxValidationError(f'`mask_input` has the wrong type: Received `{type(params.get("mask_input", None))}` expected `InputPathType | None`')
    if params.get("mask_target", None) is not None:
        if not isinstance(params["mask_target"], (pathlib.Path, str)):
            raise StyxValidationError(f'`mask_target` has the wrong type: Received `{type(params.get("mask_target", None))}` expected `InputPathType | None`')
    if params.get("bins", None) is not None:
        if not isinstance(params["bins"], int):
            raise StyxValidationError(f'`bins` has the wrong type: Received `{type(params.get("bins", None))}` expected `int | None`')
    if params.get("info", False) is None:
        raise StyxValidationError("`info` must not be None")
    if not isinstance(params["info"], bool):
        raise StyxValidationError(f'`info` has the wrong type: Received `{type(params.get("info", False))}` expected `bool`')
    if params.get("quiet", False) is None:
        raise StyxValidationError("`quiet` must not be None")
    if not isinstance(params["quiet"], bool):
        raise StyxValidationError(f'`quiet` has the wrong type: Received `{type(params.get("quiet", False))}` expected `bool`')
    if params.get("debug", False) is None:
        raise StyxValidationError("`debug` must not be None")
    if not isinstance(params["debug"], bool):
        raise StyxValidationError(f'`debug` has the wrong type: Received `{type(params.get("debug", False))}` expected `bool`')
    if params.get("force", False) is None:
        raise StyxValidationError("`force` must not be None")
    if not isinstance(params["force"], bool):
        raise StyxValidationError(f'`force` has the wrong type: Received `{type(params.get("force", False))}` expected `bool`')
    if params.get("nthreads", None) is not None:
        if not isinstance(params["nthreads"], int):
            raise StyxValidationError(f'`nthreads` has the wrong type: Received `{type(params.get("nthreads", None))}` expected `int | None`')
    if params.get("config", None) is not None:
        if not isinstance(params["config"], list):
            raise StyxValidationError(f'`config` has the wrong type: Received `{type(params.get("config", None))}` expected `list[MrhistmatchConfigParamsDict] | None`')
        for e in params["config"]:
            mrhistmatch_config_validate(e)
    if params.get("help", False) is None:
        raise StyxValidationError("`help` must not be None")
    if not isinstance(params["help"], bool):
        raise StyxValidationError(f'`help` has the wrong type: Received `{type(params.get("help", False))}` expected `bool`')
    if params.get("version", False) is None:
        raise StyxValidationError("`version` must not be None")
    if not isinstance(params["version"], bool):
        raise StyxValidationError(f'`version` has the wrong type: Received `{type(params.get("version", False))}` expected `bool`')
    if params.get("type", None) is None:
        raise StyxValidationError("`type` must not be None")
    if not isinstance(params["type"], str):
        raise StyxValidationError(f'`type` has the wrong type: Received `{type(params.get("type", None))}` expected `str`')
    if params.get("input", None) is None:
        raise StyxValidationError("`input` must not be None")
    if not isinstance(params["input"], (pathlib.Path, str)):
        raise StyxValidationError(f'`input` has the wrong type: Received `{type(params.get("input", None))}` expected `InputPathType`')
    if params.get("target", None) is None:
        raise StyxValidationError("`target` must not be None")
    if not isinstance(params["target"], (pathlib.Path, str)):
        raise StyxValidationError(f'`target` has the wrong type: Received `{type(params.get("target", None))}` expected `InputPathType`')
    if params.get("output", None) is None:
        raise StyxValidationError("`output` must not be None")
    if not isinstance(params["output"], str):
        raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `str`')


def mrhistmatch_cargs(
    params: MrhistmatchParamsDict,
    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("mrhistmatch")
    if params.get("mask_input", None) is not None:
        cargs.extend([
            "-mask_input",
            execution.input_file(params.get("mask_input", None))
        ])
    if params.get("mask_target", None) is not None:
        cargs.extend([
            "-mask_target",
            execution.input_file(params.get("mask_target", None))
        ])
    if params.get("bins", None) is not None:
        cargs.extend([
            "-bins",
            str(params.get("bins", None))
        ])
    if params.get("info", False):
        cargs.append("-info")
    if params.get("quiet", False):
        cargs.append("-quiet")
    if params.get("debug", False):
        cargs.append("-debug")
    if params.get("force", False):
        cargs.append("-force")
    if params.get("nthreads", None) is not None:
        cargs.extend([
            "-nthreads",
            str(params.get("nthreads", None))
        ])
    if params.get("config", None) is not None:
        cargs.extend([a for c in [mrhistmatch_config_cargs(s, execution) for s in params.get("config", None)] for a in c])
    if params.get("help", False):
        cargs.append("-help")
    if params.get("version", False):
        cargs.append("-version")
    cargs.append(params.get("type", None))
    cargs.append(execution.input_file(params.get("input", None)))
    cargs.append(execution.input_file(params.get("target", None)))
    cargs.append(params.get("output", None))
    return cargs


def mrhistmatch_outputs(
    params: MrhistmatchParamsDict,
    execution: Execution,
) -> MrhistmatchOutputs:
    """
    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 = MrhistmatchOutputs(
        root=execution.output_file("."),
        output=execution.output_file(params.get("output", None)),
    )
    return ret


def mrhistmatch_execute(
    params: MrhistmatchParamsDict,
    runner: Runner | None = None,
) -> MrhistmatchOutputs:
    """
    mrhistmatch
    
    Modify the intensities of one image to match the histogram of another.
    
    
    
    References:
    
    * If using inverse contrast normalization for inter-modal (DWI - T1)
    registration:
    Bhushan, C.; Haldar, J. P.; Choi, S.; Joshi, A. A.; Shattuck, D. W. & Leahy,
    R. M. Co-registration and distortion correction of diffusion and anatomical
    images based on inverse contrast normalization. NeuroImage, 2015, 115,
    269-280.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MrhistmatchOutputs`).
    """
    mrhistmatch_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(MRHISTMATCH_METADATA)
    params = execution.params(params)
    cargs = mrhistmatch_cargs(params, execution)
    ret = mrhistmatch_outputs(params, execution)
    execution.run(cargs)
    return ret


def mrhistmatch(
    type_: str,
    input_: InputPathType,
    target: InputPathType,
    output: str,
    mask_input: InputPathType | None = None,
    mask_target: InputPathType | None = None,
    bins: int | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[MrhistmatchConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> MrhistmatchOutputs:
    """
    mrhistmatch
    
    Modify the intensities of one image to match the histogram of another.
    
    
    
    References:
    
    * If using inverse contrast normalization for inter-modal (DWI - T1)
    registration:
    Bhushan, C.; Haldar, J. P.; Choi, S.; Joshi, A. A.; Shattuck, D. W. & Leahy,
    R. M. Co-registration and distortion correction of diffusion and anatomical
    images based on inverse contrast normalization. NeuroImage, 2015, 115,
    269-280.
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        type_: type of histogram matching to perform; options are:\
            scale,linear,nonlinear.
        input_: the input image to be modified.
        target: the input image from which to derive the target histogram.
        output: the output image.
        mask_input: only generate input histogram based on a specified binary\
            mask image.
        mask_target: only generate target histogram based on a specified binary\
            mask image.
        bins: the number of bins to use to generate the histograms.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MrhistmatchOutputs`).
    """
    params = mrhistmatch_params(
        mask_input=mask_input,
        mask_target=mask_target,
        bins=bins,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        type_=type_,
        input_=input_,
        target=target,
        output=output,
    )
    return mrhistmatch_execute(params, runner)


__all__ = [
    "MRHISTMATCH_METADATA",
    "MrhistmatchConfigParamsDict",
    "MrhistmatchConfigParamsDictTagged",
    "MrhistmatchOutputs",
    "MrhistmatchParamsDict",
    "MrhistmatchParamsDictTagged",
    "mrhistmatch",
    "mrhistmatch_config",
    "mrhistmatch_execute",
    "mrhistmatch_params",
]
