Skip to content

Commit 4afe0f0

Browse files
committed
in progress for replacing covidcast
1 parent 25ff20b commit 4afe0f0

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

_delphi_utils_python/delphi_utils/validator/datafetcher.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pandas as pd
1111
import numpy as np
1212
import covidcast
13+
from delphi_epidata import Epidata
1314
from .errors import APIDataFetchError, ValidationFailure
1415

1516
FILENAME_REGEX = re.compile(
@@ -115,7 +116,16 @@ def get_geo_signal_combos(data_source, api_key):
115116
meta_response.raise_for_status()
116117
source_signal_mappings = {i['source']:i['db_source'] for i in
117118
meta_response.json()}
118-
meta = covidcast.metadata()
119+
120+
response = Epidata._request("covidcast_meta")
121+
122+
if response["result"] != 1:
123+
# Something failed in the API and we did not get real metadata
124+
raise RuntimeError("Error when fetching metadata from the API",
125+
response["message"])
126+
127+
meta = pd.DataFrame.from_dict(response["epidata"])
128+
119129
source_meta = meta[meta['data_source'] == data_source]
120130
# Need to convert np.records to tuples so they are hashable and can be used in sets and dicts.
121131
geo_signal_combos = list(map(tuple,
@@ -160,8 +170,38 @@ def fetch_api_reference(data_source, start_date, end_date, geo_type, signal_type
160170
"""
161171
with warnings.catch_warnings():
162172
warnings.simplefilter("ignore")
163-
api_df = covidcast.signal(
164-
data_source, signal_type, start_date, end_date, geo_type)
173+
# api_df = covidcast.signal(
174+
# data_source, signal_type, start_date, end_date, geo_type)
175+
176+
response = Epidata.covidcast(data_source, signal_type, time_type="day",
177+
geo_type=geo_type, time_values=[start_date, end_date],
178+
geo_value="*", as_of=None,
179+
issues=None, lag=None)
180+
181+
if response["result"] != 1:
182+
# Something failed in the API and we did not get real metadata
183+
raise RuntimeError("Error when fetching metadata from the API",
184+
response["message"])
185+
186+
api_df = pd.DataFrame.from_dict(response["epidata"])
187+
188+
# # Two possible error conditions: no data or too much data.
189+
# if day_data["message"] == "no results":
190+
# warnings.warn(f"No {data_source} {signal} data found on {day_str} "
191+
# f"for geography '{geo_type}'",
192+
# NoDataWarning)
193+
# if day_data["message"] not in {"success", "no results"}:
194+
# warnings.warn(f"Problem obtaining {data_source} {signal} data on {day_str} "
195+
# f"for geography '{geo_type}': {day_data['message']}",
196+
# RuntimeWarning)
197+
#
198+
# # In the too-much-data case, we continue to try putting the truncated
199+
# # data in our results. In the no-data case, skip this day entirely,
200+
# # since there is no "epidata" in the response.
201+
# if day_data.get("epidata"):
202+
# dfs.append(pd.DataFrame.from_dict(day_data["epidata"]))
203+
# cur_day += timedelta(1) if time_type == "day" else timedelta(7)
204+
#
165205

166206
error_context = f"when fetching reference data from {start_date} to {end_date} " +\
167207
f"for data source: {data_source}, signal type: {signal_type}, geo type: {geo_type}"

_delphi_utils_python/delphi_utils/validator/dynamic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
import numpy as np
88
import covidcast
9+
from delphi_epidata import Epidata
910
from .errors import ValidationFailure
1011
from .datafetcher import get_geo_signal_combos, threaded_api_calls
1112
from .utils import relative_difference_by_min, TimeWindow, lag_converter
@@ -79,7 +80,7 @@ def validate(self, all_frames, report):
7980
outlier_lookbehind = timedelta(days=14)
8081

8182
# Authenticate API
82-
covidcast.use_api_key(self.params.api_key)
83+
# Epidata.auth = ("epidata", api)
8384

8485
# Get all expected combinations of geo_type and signal.
8586
geo_signal_combos = get_geo_signal_combos(self.params.data_source,

_delphi_utils_python/delphi_utils/validator/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
when the module is run with `python -m delphi_utils.validator`.
66
"""
77
import argparse as ap
8-
import covidcast
8+
from delphi_epidata import Epidata
99
from .. import read_params, get_structured_logger
1010
from .validate import Validator
1111

@@ -18,7 +18,7 @@ def run_module():
1818
args = parser.parse_args()
1919
params = read_params()
2020
assert "validation" in params
21-
covidcast.use_api_key(params["validation"]["common"]["api_credentials"])
21+
Epidata.auth = ("epidata", params["validation"]["common"]["api_credentials"])
2222
dry_run_param = params["validation"]["common"].get("dry_run", False)
2323
params["validation"]["common"]["dry_run"] = args.dry_run or dry_run_param
2424
validator = Validator(params)

0 commit comments

Comments
 (0)