From c099ad1c235b9c51aaca70fa2132fd18929f673c Mon Sep 17 00:00:00 2001 From: matthewcornell Date: Tue, 3 Dec 2024 13:31:53 -0500 Subject: [PATCH] generalized target data generation to both flu and covid --- .../app/generate_target_json_files.py | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/hub_predtimechart/app/generate_target_json_files.py b/src/hub_predtimechart/app/generate_target_json_files.py index c71628e..169dd23 100644 --- a/src/hub_predtimechart/app/generate_target_json_files.py +++ b/src/hub_predtimechart/app/generate_target_json_files.py @@ -20,33 +20,46 @@ @click.argument('ptc_config_file', type=click.Path(file_okay=True, exists=False)) @click.argument('target_out_dir', type=click.Path(file_okay=False, exists=True)) def main(hub_dir, ptc_config_file, target_out_dir): - ''' + """ Generates the target data json files used by https://github.com/reichlab/predtimechart to - visualize a hub's forecasts. + visualize a hub's forecasts. Handles missing input target data in two ways, depending on the error. 1) If the + `target_data_file_name` entry in the hub config file is missing, then the program will exit with no messages. + 2) If the entry is present but the file it points to does not exist, then the program will exit with an error + message, but won't actually raise a Python exception. HUB_DIR: (input) a directory Path of a https://hubverse.io hub to generate target data json files from + PTC_CONFIG_FILE: (input) a file Path to a `predtimechart-config.yaml` file that specifies how to process `hub_dir` + to get predtimechart output + TARGET_OUT_DIR: (output) a directory Path to output the viz target data json files to \f :param hub_dir: (input) a directory Path of a https://hubverse.io hub to generate target data json files from + :param ptc_config_file: (input) a file Path to a `predtimechart-config.yaml` file that specifies how to process + `hub_dir` to get predtimechart output :param target_out_dir: (output) a directory Path to output the viz target data json files to - - ''' + """ logger.info(f'main({hub_dir=}, {target_out_dir=}): entered') hub_dir = Path(hub_dir) hub_config = HubConfig(hub_dir, Path(ptc_config_file)) - target_out_dir = Path(target_out_dir) - target_data_df = get_target_data_df(hub_dir, hub_config.target_data_file_name) + if hub_config.target_data_file_name is None: + logger.info('No `target_data_file_name` entry found in hub config file. exiting') + return - # for each location, - # - generate target data file contents - # - save as json + # for each location, generate target data file contents and then save as json json_files = [] + try: + target_data_df = get_target_data_df(hub_dir, hub_config.target_data_file_name) + except FileNotFoundError as error: + logger.error(f"target data file not found. {hub_config.target_data_file_name=}, {error=}") + return + for loc in target_data_df['location'].unique(): task_ids_tuple = (loc,) + target_out_dir = Path(target_out_dir) + file_name = json_file_name(hub_config.fetch_target_id, task_ids_tuple, reference_date_from_today().isoformat()) location_data_dict = ptc_target_data(target_data_df, task_ids_tuple) - file_name = json_file_name('wk inc flu hosp', task_ids_tuple, reference_date_from_today().isoformat()) json_files.append(target_out_dir / file_name) with open(target_out_dir / file_name, 'w') as fp: json.dump(location_data_dict, fp, indent=4)