Skip to content

Commit c21b544

Browse files
chinandrewAnanya-Joshi
authored andcommitted
Refactor NCHS mortality to use delphi export util
1 parent 02f7080 commit c21b544

File tree

4 files changed

+17
-45
lines changed

4 files changed

+17
-45
lines changed

_delphi_utils_python/delphi_utils/export.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional
66
import logging
77

8+
from epiweeks import Week
89
import numpy as np
910
import pandas as pd
1011

@@ -39,7 +40,7 @@ def create_export_csv(
3940
end_date: Optional[datetime] = None,
4041
remove_null_samples: Optional[bool] = False,
4142
write_empty_days: Optional[bool] = False,
42-
logger: Optional[logging.Logger] = None
43+
weekly_dates = False,
4344
):
4445
"""Export data in the format expected by the Delphi API.
4546
@@ -90,10 +91,15 @@ def create_export_csv(
9091
dates = pd.date_range(start_date, end_date)
9192

9293
for date in dates:
94+
if weekly_dates:
95+
t = Week.fromdate(pd.to_datetime(str(date)))
96+
date_str = "weekly_" + str(t.year) + str(t.week).zfill(2)
97+
else:
98+
date_str = date.strftime('%Y%m%d')
9399
if metric is None:
94-
export_filename = f"{date.strftime('%Y%m%d')}_{geo_res}_{sensor}.csv"
100+
export_filename = f"{date_str}_{geo_res}_{sensor}.csv"
95101
else:
96-
export_filename = f"{date.strftime('%Y%m%d')}_{geo_res}_{metric}_{sensor}.csv"
102+
export_filename = f"{date_str}_{geo_res}_{metric}_{sensor}.csv"
97103
export_file = join(export_dir, export_filename)
98104
expected_columns = [
99105
"geo_id",

_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)))

0 commit comments

Comments
 (0)