Skip to content

Commit

Permalink
generalized target data generation to both flu and covid
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewcornell committed Dec 3, 2024
1 parent f3157ec commit c099ad1
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/hub_predtimechart/app/generate_target_json_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c099ad1

Please sign in to comment.