Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add app to generate target data for flusight #20

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies = [

[project.optional-dependencies]
dev = [
"freezegun",
"pytest",
"pip-tools"
]
Expand Down
6 changes: 5 additions & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ click==8.1.7
# via
# hub-dashboard-predtimechart (pyproject.toml)
# pip-tools
freezegun==1.5.1
# via hub-dashboard-predtimechart (pyproject.toml)
iniconfig==2.0.0
# via pytest
jsonschema==4.23.0
Expand Down Expand Up @@ -45,7 +47,9 @@ pyproject-hooks==1.1.0
pytest==8.3.2
# via hub-dashboard-predtimechart (pyproject.toml)
python-dateutil==2.9.0.post0
# via pandas
# via
# freezegun
# pandas
pytz==2024.1
# via pandas
pyyaml==6.0.2
Expand Down
78 changes: 78 additions & 0 deletions src/hub_predtimechart/app/generate_target_json_files_FluSight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from datetime import date, datetime, timedelta
import json
import re
from pathlib import Path

import click
import polars as pl
import structlog

from hub_predtimechart.generate_target_data import target_data_for_FluSight
from hub_predtimechart.app.generate_json_files import json_file_name
from hub_predtimechart.util.logs import setup_logging


setup_logging()
logger = structlog.get_logger()


@click.command()
@click.argument('hub_dir', type=click.Path(file_okay=False, exists=True))
@click.argument('target_out_dir', type=click.Path(file_okay=False, exists=True))
def main(hub_dir, target_out_dir):
'''
Generates the target data json files used by https://github.com/reichlab/predtimechart to
visualize a hub's forecasts.

HUB_DIR: (input) a directory Path of a https://hubverse.io hub to generate target data json files from

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 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)
target_out_dir = Path(target_out_dir)

# load the target data csv file from the hub repo
# for now, file path for target data is hard coded
target_data_df = pl.read_csv(hub_dir / 'target-data/target-hospital-admissions.csv')

# for each location,
# - generate target data file contents
# - save as json
json_files = []
for loc in target_data_df['location'].unique():
task_ids_tuple = (loc,)
location_data_dict = target_data_for_FluSight(target_data_df, task_ids_tuple)
file_name = json_file_name('wk inc flu hosp', task_ids_tuple, reference_date_from_today())
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)

logger.info(f'main(): done: {len(json_files)} JSON files generated: {[str(_) for _ in json_files]}. ')



#
# _generate_json_files() and helpers
#
def reference_date_from_today(now: datetime = date.today()) -> datetime:
# Calculate the days until the next Saturday
days_to_saturday = 5 - now.weekday()
if days_to_saturday < 0:
days_to_saturday += 7

# Add the calculated days to the given date
return now + timedelta(days=days_to_saturday)


#
# main()
#

if __name__ == '__main__':
main()
19 changes: 19 additions & 0 deletions tests/hub_predtimechart/test_generate_target_data.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json
from datetime import date
from pathlib import Path

from freezegun import freeze_time
import polars as pl

from hub_predtimechart.app.generate_json_files import json_file_name
from hub_predtimechart.generate_target_data import target_data_for_FluSight
from hub_predtimechart.app.generate_target_json_files_FluSight import reference_date_from_today


def test_generate_target_data_for_FluSight():
Expand All @@ -20,3 +23,19 @@ def test_generate_target_data_for_FluSight():
model_output_file = hub_dir / 'model-output/Flusight-baseline/2022-10-22-Flusight-baseline.csv'
act_data = target_data_for_FluSight(target_data_df, task_ids_tuple)
assert act_data == exp_data


@freeze_time("2024-10-24")
def test_reference_date_from_today():
# Test dates are Sunday, Thursday, and Saturday.
# For all of these, the expected reference date is the same: the Saturday
exp_reference_date = date.fromisoformat("2024-10-26")
for reference_date_str in ["2024-10-20", "2024-10-24", "2024-10-26"]:
act_reference_date = reference_date_from_today(date.fromisoformat(reference_date_str))
assert act_reference_date == exp_reference_date

# test that now is used if no parameter passed
# in this case, we expect that reference_date_from_today uses now,
# which is set to "2024-10-24" via the freeze_time decorator.
act_reference_date = reference_date_from_today()
assert act_reference_date == exp_reference_date