Skip to content

common_arguments

Functions to define the CLI's "common" arguments, i.e. those that can be applied to either: - All module argument lists, e.g. --dataset, --seed, etc. - A subset of module(s) argument lists, e.g. --synthetic, --typed, etc.

get_core_parser(overrides=False)

Create the core common parser group applied to all modules (and the pipeline and config options). Note that we leverage common titling of the argument group to ensure arguments appear together even if declared separately.

Parameters:

Name Type Description Default
overrides

whether the arguments declared within are required or not.

False

Returns:

Type Description
ArgumentParser

The parser with the group containing the core arguments attached.

Source code in src/nhssynth/cli/common_arguments.py
def get_core_parser(overrides=False) -> argparse.ArgumentParser:
    """
    Create the core common parser group applied to all modules (and the `pipeline` and `config` options).
    Note that we leverage common titling of the argument group to ensure arguments appear together even if declared separately.

    Args:
        overrides: whether the arguments declared within are required or not.

    Returns:
        The parser with the group containing the core arguments attached.
    """
    """"""
    core = argparse.ArgumentParser(add_help=False)
    core_grp = core.add_argument_group(title="options")
    core_grp.add_argument(
        "-d",
        "--dataset",
        required=(not overrides),
        type=str,
        help="the name of the dataset to experiment with, should be present in `<DATA_DIR>`",
    )
    core_grp.add_argument(
        "-e",
        "--experiment-name",
        type=str,
        default=TIME,
        help="name the experiment run to affect logging, config, and default-behaviour i/o",
    )
    core_grp.add_argument(
        "--save-config",
        action="store_true",
        help="save the config provided via the cli, this is a recommended option for reproducibility",
    )
    return core

get_seed_parser(overrides=False)

Create the common parser group for the seed. NB This is separate to the rest of the core arguments as it does not apply to the dashboard module.

Parameters:

Name Type Description Default
overrides

whether the arguments declared within are required or not.

False

Returns:

Type Description
ArgumentParser

The parser with the group containing the seed argument attached.

Source code in src/nhssynth/cli/common_arguments.py
def get_seed_parser(overrides=False) -> argparse.ArgumentParser:
    """
    Create the common parser group for the seed.
    NB This is separate to the rest of the core arguments as it does not apply to the dashboard module.

    Args:
        overrides: whether the arguments declared within are required or not.

    Returns:
        The parser with the group containing the seed argument attached.
    """
    parser = argparse.ArgumentParser(add_help=False)
    parser_grp = parser.add_argument_group(title="options")
    parser_grp.add_argument(
        "-s",
        "--seed",
        type=int,
        help="specify a seed for reproducibility, this is a recommended option for reproducibility",
    )
    return parser

suffix_parser_generator(name, help, required=False)

Generator function for creating parsers following a common template. These parsers are all suffixes to the --dataset / -d / DATASET argument, see COMMON_TITLE.

Parameters:

Name Type Description Default
name str

the name / label of the argument to add to the CLI options.

required
help str

the help message when the CLI is run with --help / -h.

required
required bool

whether the argument must be provided or not.

False
Source code in src/nhssynth/cli/common_arguments.py
def suffix_parser_generator(name: str, help: str, required: bool = False) -> argparse.ArgumentParser:
    """Generator function for creating parsers following a common template.
    These parsers are all suffixes to the --dataset / -d / DATASET argument, see `COMMON_TITLE`.

    Args:
        name: the name / label of the argument to add to the CLI options.
        help: the help message when the CLI is run with --help / -h.
        required: whether the argument must be provided or not.
    """

    def get_parser(overrides: bool = False) -> argparse.ArgumentParser:
        parser = argparse.ArgumentParser(add_help=False)
        parser_grp = parser.add_argument_group(title=COMMON_TITLE)
        parser_grp.add_argument(
            f"--{name.replace('_', '-')}",
            required=required and not overrides,
            type=str,
            default=f"_{name}",
            help=help,
        )
        return parser

    return get_parser