From e85292bd3ff3f4286f735fdb3198f252f8218b19 Mon Sep 17 00:00:00 2001 From: Dobson Date: Thu, 9 May 2024 10:46:29 +0100 Subject: [PATCH] Update swmmanywhere.py enable argprase for main function --- swmmanywhere/swmmanywhere.py | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/swmmanywhere/swmmanywhere.py b/swmmanywhere/swmmanywhere.py index b99b5d00..de6cd83c 100644 --- a/swmmanywhere/swmmanywhere.py +++ b/swmmanywhere/swmmanywhere.py @@ -1,6 +1,7 @@ """The main SWMManywhere module to generate and run a synthetic network.""" from __future__ import annotations +import argparse import os from pathlib import Path @@ -12,8 +13,8 @@ import swmmanywhere.geospatial_utilities as go from swmmanywhere import parameters, preprocessing +from swmmanywhere.custom_logging import logger from swmmanywhere.graph_utilities import iterate_graphfcns, load_graph, save_graph -from swmmanywhere.logging import logger from swmmanywhere.metric_utilities import iterate_metrics from swmmanywhere.post_processing import synthetic_write @@ -64,7 +65,7 @@ def swmmanywhere(config: dict) -> tuple[Path, dict | None]: # Identify the starting graph logger.info("Iterating graphs.") - if config['starting_graph']: + if config.get('starting_graph', None): G = load_graph(config['starting_graph']) else: G = preprocessing.create_starting_graph(addresses) @@ -384,4 +385,33 @@ def run(model: Path, storevar['id'])}) logger.info("Model run complete.") - return pd.DataFrame(results) \ No newline at end of file + return pd.DataFrame(results) + +def parse_arguments(): + """Parse the command line arguments. + + Returns: + Path: The path to the configuration file. + """ + parser = argparse.ArgumentParser(description='Process command line arguments.') + parser.add_argument('--config_path', + type=Path, + default=Path(__file__).parent.parent.parent / 'tests' /\ + 'test_data' / 'demo_config.yml', + help='Configuration file path') + parser.add_argument('--verbose', + type=bool, + default=False, + help='Configuration verbosity') + args = parser.parse_args() + return args.config_path, args.verbose + +if __name__ == '__main__': + # Parse the arguments + config_path, verbose = parse_arguments() + os.environ["SWMMANYWHERE_VERBOSE"] = str(verbose).lower() + + config = load_config(config_path) + + # Run the model + inp, metrics = swmmanywhere(config)