Skip to content

Commit a5628ac

Browse files
committed
lint and cleanup
1 parent c941982 commit a5628ac

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

_delphi_utils_python/delphi_utils/covidcast_wrapper.py

+47-24
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
"""module for covidcast api call wrapper."""
2+
13
from datetime import date, timedelta
2-
from typing import List, Tuple, Union, Iterable
4+
from typing import Iterable, Union
35

4-
import numpy as np
56
import pandas as pd
6-
77
from delphi_epidata import Epidata
88
from epiweeks import Week
99

10-
def date_generator(startdate, enddate):
11-
while startdate <= enddate:
12-
yield startdate.strftime('%Y-%m-%d')
13-
startdate = startdate + timedelta(days=1)
10+
11+
def date_generator(startdate: date, enddate: date) -> Iterable[date]:
12+
"""
13+
Take start date and end date and generates date string.
14+
15+
Parameters
16+
----------
17+
startdate: date
18+
enddate: date
19+
20+
Returns
21+
-------
22+
generator of str
23+
"""
24+
while startdate <= enddate:
25+
yield startdate.strftime("%Y-%m-%d")
26+
startdate = startdate + timedelta(days=1)
1427

1528

16-
def _parse_datetimes(date_int: int,
17-
time_type: str,
18-
date_format: str = "%Y%m%d") -> Union[pd.Timestamp]: # annotating nan errors
29+
def _parse_datetimes(date_int: int, time_type: str, date_format: str = "%Y%m%d") -> Union[pd.Timestamp, None]:
1930
"""Convert a date or epiweeks string into timestamp objects.
2031
2132
Datetimes (length 8) are converted to their corresponding date, while epiweeks (length 6)
@@ -33,15 +44,23 @@ def _parse_datetimes(date_int: int,
3344
if time_type == "week":
3445
epiwk = Week(int(date_str[:4]), int(date_str[-2:]))
3546
return pd.to_datetime(epiwk.startdate())
36-
return np.nan
47+
return None
48+
49+
50+
def metadata() -> Union[pd.DataFrame, None]:
51+
"""
52+
Make covidcast metadata api call.
3753
38-
def metadata():
54+
Returns
55+
-------
56+
pd.DataFrame of covidcast metadata.
57+
"""
58+
# pylint: disable=W0212
3959
response = Epidata._request("covidcast_meta")
4060

4161
if response["result"] != 1:
4262
# Something failed in the API and we did not get real metadata
43-
raise RuntimeError("Error when fetching metadata from the API",
44-
response["message"])
63+
raise RuntimeError("Error when fetching metadata from the API", response["message"])
4564

4665
df = pd.DataFrame.from_dict(response["epidata"])
4766
df["min_time"] = df.apply(lambda x: _parse_datetimes(x.min_time, x.time_type), axis=1)
@@ -58,7 +77,6 @@ def signal(
5877
geo_type: str = "county",
5978
geo_values: Union[str, Iterable[str]] = "*",
6079
as_of: date = None,
61-
issues: Union[date, Tuple[date], List[date]] = None,
6280
lag: int = None,
6381
time_type: str = "day",
6482
) -> Union[pd.DataFrame, None]:
@@ -208,19 +226,24 @@ def signal(
208226
)
209227

210228
time_values = list(date_generator(start_day, end_day))
211-
issues = list(date_generator(start_day, end_day)) #TODO placesholder need to see how the issues params are coming in
212-
response = Epidata.covidcast(data_source, signal, time_type=time_type,
213-
geo_type=geo_type, time_values=time_values,
214-
geo_value=geo_values, as_of=as_of,
215-
issues=issues, lag=lag)
229+
230+
response = Epidata.covidcast(
231+
data_source,
232+
signal,
233+
time_type=time_type,
234+
geo_type=geo_type,
235+
time_values=time_values,
236+
geo_value=geo_values,
237+
as_of=as_of,
238+
lag=lag,
239+
)
216240
if response["result"] != 1:
217241
# Something failed in the API and we did not get real metadata
218-
raise RuntimeError("Error when fetching metadata from the API",
219-
response["message"])
242+
raise RuntimeError("Error when fetching metadata from the API", response["message"])
220243

221244
api_df = pd.DataFrame.from_dict(response["epidata"])
222-
api_df["issue"] = pd.to_datetime(api_df["issue"], format='%Y%m%d')
223-
api_df["time_value"] = pd.to_datetime(api_df["time_value"], format='%Y%m%d')
245+
api_df["issue"] = pd.to_datetime(api_df["issue"], format="%Y%m%d")
246+
api_df["time_value"] = pd.to_datetime(api_df["time_value"], format="%Y%m%d")
224247
api_df.drop("direction", axis=1, inplace=True)
225248
api_df["data_source"] = data_source
226249
api_df["signal"] = signal

_delphi_utils_python/delphi_utils/validator/dynamic.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""Dynamic file checks."""
2+
3+
import re
24
from dataclasses import dataclass
35
from datetime import date, timedelta
46
from typing import Dict, Set
5-
import re
6-
import pandas as pd
7+
78
import numpy as np
8-
import covidcast
9-
from delphi_epidata import Epidata
10-
from .errors import ValidationFailure
9+
import pandas as pd
10+
1111
from .datafetcher import get_geo_signal_combos, threaded_api_calls
12-
from .utils import relative_difference_by_min, TimeWindow, lag_converter
12+
from .errors import ValidationFailure
13+
from .utils import TimeWindow, lag_converter, relative_difference_by_min
1314

1415

1516
class DynamicValidator:

_delphi_utils_python/delphi_utils/validator/run.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
when the module is run with `python -m delphi_utils.validator`.
66
"""
77
import argparse as ap
8+
89
from delphi_epidata import Epidata
9-
from .. import read_params, get_structured_logger
10+
11+
from .. import get_structured_logger, read_params
1012
from .validate import Validator
1113

1214

0 commit comments

Comments
 (0)