Skip to content

Commit f00586e

Browse files
committed
Modularized run
1 parent b7f213f commit f00586e

File tree

1 file changed

+64
-33
lines changed
  • changehc/delphi_changehc

1 file changed

+64
-33
lines changed

changehc/delphi_changehc/run.py

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,73 @@
1818
from .load_data import load_combined_data, load_cli_data
1919
from .update_sensor import CHCSensorUpdator
2020

21+
def retrieve_files(params, filedate):
22+
"""Return filenames of relevant files, downloading them if necessary
23+
"""
24+
if params["input_denom_file"] is None:
2125

22-
def run_module(): # pylint: disable=too-many-branches,too-many-statements
26+
## download recent files from FTP server
27+
logging.info("downloading recent files through SFTP")
28+
if "covid" in params["types"]:
29+
download_covid(params["cache_dir"], params["ftp_conn"])
30+
if "cli" in params["types"]:
31+
download_cli(params["cache_dir"], params["ftp_conn"])
32+
33+
denom_file = "%s/%s_All_Outpatients_By_County.dat.gz" % (params["cache_dir"],filedate)
34+
covid_file = "%s/%s_Covid_Outpatients_By_County.dat.gz" % (params["cache_dir"],filedate)
35+
flu_file = "%s/%s_Flu_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
36+
mixed_file = "%s/%s_Mixed_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
37+
flu_like_file = "%s/%s_Flu_Like_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
38+
covid_like_file = "%s/%s_Covid_Like_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
39+
else:
40+
denom_file = params["input_denom_file"]
41+
covid_file = params["input_covid_file"]
42+
flu_file = params["input_flu_file"]
43+
mixed_file = params["input_mixed_file"]
44+
flu_like_file = params["input_flu_like_file"]
45+
covid_like_file = params["input_covid_like_file"]
46+
47+
file_dict = {"denom": denom_file}
48+
if "covid" in params["types"]:
49+
file_dict["covid"] = covid_file
50+
if "cli" in params["types"]:
51+
file_dict["flu"] = flu_file
52+
file_dict["mixed"] = mixed_file
53+
file_dict["flu_like"] = flu_like_file
54+
file_dict["covid_like"] = covid_like_file
55+
return file_dict
56+
57+
58+
def make_asserts(params):
59+
"""Assert that for each type, filenames are either all present or all absent
60+
"""
61+
if "covid" in params["types"]:
62+
assert (params["input_denom_file"] is None) == (params["input_covid_file"] is None), \
63+
"exactly one of denom and covid files are provided"
64+
if "cli" in params["types"]:
65+
if params["input_denom_file"] is None:
66+
assert params["input_flu_file"] is None and \
67+
params["input_mixed_file"] is None and \
68+
params["input_flu_like_file"] is None and \
69+
params["input_covid_like_file"] is None,\
70+
"files must be all present or all absent"
71+
else:
72+
assert params["input_flu_file"] is not None and \
73+
params["input_mixed_file"] is not None and \
74+
params["input_flu_like_file"] is not None and \
75+
params["input_covid_like_file"] is not None,\
76+
"files must be all present or all absent"
77+
78+
79+
def run_module():
2380
"""Run the delphi_changehc module.
2481
"""
2582

2683
params = read_params()
2784

2885
logging.basicConfig(level=logging.DEBUG)
2986

30-
# the filenames are expected to be in the format:
31-
# Denominator: "YYYYMMDD_All_Outpatients_By_County.dat.gz"
32-
# Numerator: "YYYYMMDD_Covid_Outpatients_By_County.dat.gz"
33-
34-
assert (params["input_denom_file"] is None) == (params["input_covid_file"] is None), \
35-
"exactly one of denom and covid files are provided"
87+
make_asserts(params)
3688

3789
if params["drop_date"] is None:
3890
# files are dropped about 8pm the day after the issue date
@@ -42,28 +94,7 @@ def run_module(): # pylint: disable=too-many-branches,too-many-statements
4294
dropdate_dt = datetime.strptime(params["drop_date"], "%Y-%m-%d")
4395
filedate = dropdate_dt.strftime("%Y%m%d")
4496

45-
if params["input_denom_file"] is None:
46-
47-
## download recent files from FTP server
48-
logging.info("downloading recent files through SFTP")
49-
if "covid" in params["types"]:
50-
download_covid(params["cache_dir"], params["ftp_conn"])
51-
if "cli" in params["types"]:
52-
download_cli(params["cache_dir"], params["ftp_conn"])
53-
54-
input_denom_file = "%s/%s_All_Outpatients_By_County.dat.gz" % (params["cache_dir"],filedate)
55-
input_covid_file = "%s/%s_Covid_Outpatients_By_County.dat.gz" % (params["cache_dir"],filedate)
56-
input_flu_file = "%s/%s_Flu_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
57-
input_mixed_file = "%s/%s_Mixed_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
58-
input_flu_like_file = "%s/%s_Flu_Like_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
59-
input_covid_like_file = "%s/%s_Covid_Like_Patient_Count_By_County.dat.gz" % (params["cache_dir"],filedate)
60-
else:
61-
input_denom_file = params["input_denom_file"]
62-
input_covid_file = params["input_covid_file"]
63-
input_flu_file = params["input_flu_file"]
64-
input_mixed_file = params["input_mixed_file"]
65-
input_flu_like_file = params["input_flu_like_file"]
66-
input_covid_like_file = params["input_covid_like_file"]
97+
file_dict = retrieve_files(params, filedate)
6798

6899
dropdate = str(dropdate_dt.date())
69100

@@ -114,11 +145,11 @@ def run_module(): # pylint: disable=too-many-branches,too-many-statements
114145
params["se"]
115146
)
116147
if numtype == "covid":
117-
data = load_combined_data(input_denom_file,
118-
input_covid_file,dropdate_dt,"fips")
148+
data = load_combined_data(file_dict["denom"],
149+
file_dict["covid"],dropdate_dt,"fips")
119150
elif numtype == "cli":
120-
data = load_cli_data(input_denom_file,input_flu_file,input_mixed_file,
121-
input_flu_like_file,input_covid_like_file,dropdate_dt,"fips")
151+
data = load_cli_data(file_dict["denom"],file_dict["flu"],file_dict["mixed"],
152+
file_dict["flu_like"],file_dict["covid_like"],dropdate_dt,"fips")
122153
su_inst.update_sensor(
123154
data,
124155
params["export_dir"]

0 commit comments

Comments
 (0)