Skip to content

Commit 0e76a43

Browse files
authored
Merge pull request #1264 from cmu-delphi/nchs-export
Refactor NCHS mortality to use delphi export util
2 parents cdd12ff + 5fa6314 commit 0e76a43

File tree

5 files changed

+18
-96
lines changed

5 files changed

+18
-96
lines changed

_delphi_utils_python/delphi_utils/export.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from os.path import join
55
from typing import Optional
66

7+
from epiweeks import Week
78
import numpy as np
89
import pandas as pd
910

@@ -16,7 +17,8 @@ def create_export_csv(
1617
start_date: Optional[datetime] = None,
1718
end_date: Optional[datetime] = None,
1819
remove_null_samples: Optional[bool] = False,
19-
write_empty_days: Optional[bool] = False
20+
write_empty_days: Optional[bool] = False,
21+
weekly_dates = False,
2022
):
2123
"""Export data in the format expected by the Delphi API.
2224
@@ -65,10 +67,15 @@ def create_export_csv(
6567
dates = pd.date_range(start_date, end_date)
6668

6769
for date in dates:
70+
if weekly_dates:
71+
t = Week.fromdate(pd.to_datetime(str(date)))
72+
date_str = "weekly_" + str(t.year) + str(t.week).zfill(2)
73+
else:
74+
date_str = date.strftime('%Y%m%d')
6875
if metric is None:
69-
export_filename = f"{date.strftime('%Y%m%d')}_{geo_res}_{sensor}.csv"
76+
export_filename = f"{date_str}_{geo_res}_{sensor}.csv"
7077
else:
71-
export_filename = f"{date.strftime('%Y%m%d')}_{geo_res}_{metric}_{sensor}.csv"
78+
export_filename = f"{date_str}_{geo_res}_{metric}_{sensor}.csv"
7279
export_file = join(export_dir, export_filename)
7380
export_df = df[df["timestamp"] == date][["geo_id", "val", "se", "sample_size",]]
7481
if remove_null_samples:

_delphi_utils_python/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
required = [
88
"boto3",
99
"covidcast",
10+
"epiweeks",
1011
"freezegun",
1112
"gitpython",
1213
"mock",

nchs_mortality/delphi_nchs_mortality/export.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

nchs_mortality/delphi_nchs_mortality/run.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
from typing import Dict, Any
1010

1111
import numpy as np
12-
from delphi_utils import S3ArchiveDiffer, get_structured_logger
12+
from delphi_utils import S3ArchiveDiffer, get_structured_logger, create_export_csv
1313

1414
from .archive_diffs import arch_diffs
1515
from .constants import (METRICS, SENSOR_NAME_MAP,
1616
SENSORS, INCIDENCE_BASE, GEO_RES)
17-
from .export import export_csv
1817
from .pull import pull_nchs_mortality_data
1918

2019

@@ -70,12 +69,13 @@ def run_module(params: Dict[str, Any]):
7069
df["sample_size"] = np.nan
7170
df = df[~df["val"].isnull()]
7271
sensor_name = "_".join([SENSOR_NAME_MAP[metric]])
73-
dates = export_csv(
72+
dates = create_export_csv(
7473
df,
75-
geo_name=GEO_RES,
74+
geo_res=GEO_RES,
7675
export_dir=daily_export_dir,
7776
start_date=datetime.strptime(export_start_date, "%Y-%m-%d"),
7877
sensor=sensor_name,
78+
weekly_dates=True
7979
)
8080
if len(dates) > 0:
8181
stats.append((max(dates), len(dates)))
@@ -93,12 +93,13 @@ def run_module(params: Dict[str, Any]):
9393
df["sample_size"] = np.nan
9494
df = df[~df["val"].isnull()]
9595
sensor_name = "_".join([SENSOR_NAME_MAP[metric], sensor])
96-
dates = export_csv(
96+
dates = create_export_csv(
9797
df,
98-
geo_name=GEO_RES,
98+
geo_res=GEO_RES,
9999
export_dir=daily_export_dir,
100100
start_date=datetime.strptime(export_start_date, "%Y-%m-%d"),
101101
sensor=sensor_name,
102+
weekly_dates=True
102103
)
103104
if len(dates) > 0:
104105
stats.append((max(dates), len(dates)))

nchs_mortality/tests/test_export.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)