Specify all CLI-accessible modules and their configurations, the pipeline to run by default, and define special functions for the config
and pipeline
CLI option trees.
ModuleConfig
Represents a module's configuration, containing the following attributes:
Attributes:
Name |
Type |
Description |
func |
|
A callable that executes the module's functionality.
|
add_args |
|
A callable that populates the module's sub-parser arguments.
|
description |
|
A description of the module's functionality.
|
help |
|
A help message for the module's command-line interface.
|
common_parsers |
|
A list of common parsers to add to the module's sub-parser, appending the 'dataset' and 'core' parsers to those passed.
|
Source code in src/nhssynth/cli/module_setup.py
| class ModuleConfig:
"""
Represents a module's configuration, containing the following attributes:
Attributes:
func: A callable that executes the module's functionality.
add_args: A callable that populates the module's sub-parser arguments.
description: A description of the module's functionality.
help: A help message for the module's command-line interface.
common_parsers: A list of common parsers to add to the module's sub-parser, appending the 'dataset' and 'core' parsers to those passed.
"""
def __init__(
self,
func: Callable[..., argparse.Namespace],
add_args: Callable[..., None],
description: str,
help: str,
common_parsers: Optional[list[str]] = None,
no_seed: bool = False,
) -> None:
self.func = func
self.add_args = add_args
self.description = description
self.help = help
self.common_parsers = ["core", "seed"] if not no_seed else ["core"]
if common_parsers:
assert set(common_parsers) <= COMMON_PARSERS.keys(), "Invalid common parser(s) specified."
# merge the below two assert statements
assert (
"core" not in common_parsers and "seed" not in common_parsers
), "The 'seed' and 'core' parser groups are automatically added to all modules, remove the from `ModuleConfig`s."
self.common_parsers += common_parsers
def __call__(self, args: argparse.Namespace) -> argparse.Namespace:
return self.func(args)
|
add_config_args(parser)
Adds arguments to parser
relating to configuration file handling and module-specific config overrides.
Source code in src/nhssynth/cli/module_setup.py
| def add_config_args(parser: argparse.ArgumentParser) -> None:
"""Adds arguments to `parser` relating to configuration file handling and module-specific config overrides."""
parser.add_argument(
"-c",
"--input-config",
required=True,
help="specify the config file name",
)
parser.add_argument(
"-cp",
"--custom-pipeline",
action="store_true",
help="infer a custom pipeline running order of modules from the config",
)
for module_name in PIPELINE:
MODULE_MAP[module_name].add_args(parser, f"{module_name} option overrides", overrides=True)
for module_name in VALID_MODULES - set(PIPELINE):
MODULE_MAP[module_name].add_args(parser, f"{module_name} options overrides", overrides=True)
|
add_pipeline_args(parser)
Adds arguments to parser
for each module in the pipeline.
Source code in src/nhssynth/cli/module_setup.py
| def add_pipeline_args(parser: argparse.ArgumentParser) -> None:
"""Adds arguments to `parser` for each module in the pipeline."""
for module_name in PIPELINE:
MODULE_MAP[module_name].add_args(parser, f"{module_name} options")
|
add_subparser(subparsers, name, module_config)
Add a subparser to an argparse argument parser.
Parameters:
Name |
Type |
Description |
Default |
subparsers |
_SubParsersAction
|
The subparsers action to which the subparser will be added.
|
required
|
name |
str
|
The name of the subparser.
|
required
|
module_config |
ModuleConfig
|
A ModuleConfig object containing information about the subparser, including a function to execute and a function to add arguments.
|
required
|
Returns:
Type |
Description |
ArgumentParser
|
The newly created subparser.
|
Source code in src/nhssynth/cli/module_setup.py
| def add_subparser(
subparsers: argparse._SubParsersAction,
name: str,
module_config: ModuleConfig,
) -> argparse.ArgumentParser:
"""
Add a subparser to an argparse argument parser.
Args:
subparsers: The subparsers action to which the subparser will be added.
name: The name of the subparser.
module_config: A [`ModuleConfig`][nhssynth.cli.module_setup.ModuleConfig] object containing information about the subparser, including a function to execute and a function to add arguments.
Returns:
The newly created subparser.
"""
parent_parsers = get_parent_parsers(name, module_config.common_parsers)
parser = subparsers.add_parser(
name=name,
description=module_config.description,
help=module_config.help,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
parents=parent_parsers,
)
if name not in {"pipeline", "config"}:
module_config.add_args(parser, f"{name} options")
else:
module_config.add_args(parser)
parser.set_defaults(func=module_config.func)
return parser
|
get_parent_parsers(name, module_parsers)
Get a list of parent parsers for a given module, based on the module's common_parsers
attribute.
Source code in src/nhssynth/cli/module_setup.py
| def get_parent_parsers(name: str, module_parsers: list[str]) -> list[argparse.ArgumentParser]:
"""Get a list of parent parsers for a given module, based on the module's `common_parsers` attribute."""
if name in {"pipeline", "config"}:
return [p(name == "config") for p in COMMON_PARSERS.values()]
elif name == "dashboard":
return [COMMON_PARSERS[pn](True) for pn in module_parsers]
else:
return [COMMON_PARSERS[pn]() for pn in module_parsers]
|
run_pipeline(args)
Runs the specified pipeline of modules with the passed configuration args
.
Source code in src/nhssynth/cli/module_setup.py
| def run_pipeline(args: argparse.Namespace) -> None:
"""Runs the specified pipeline of modules with the passed configuration `args`."""
print("Running full pipeline...")
args.modules_to_run = PIPELINE
for module_name in PIPELINE:
args = MODULE_MAP[module_name](args)
|