Skip to content

Commit aba2c85

Browse files
committed
lint compliance for usafacts
1 parent dcaf97d commit aba2c85

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

usafacts/delphi_usafacts/geo.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
"""Functions for converting geocodes."""
23
import pandas as pd
34

45

@@ -78,6 +79,11 @@
7879

7980
FIPS_TO_STATE = {v: k.lower() for k, v in STATE_TO_FIPS.items()}
8081

82+
# Valid geographical resolutions output by this indicator.
83+
VALID_GEO_RES = ("county", "state", "msa", "hrr")
84+
# Sensors that report proportions. For geo resolutions with unallocated cases
85+
# or deaths, we avoid reporting these sensors.
86+
PROP_SENSORS = ("incidence", "cumulative_prop")
8187

8288
def fips_to_state(fips: str) -> str:
8389
"""Wrapper that handles exceptions to the FIPS scheme in the USAFacts data.
@@ -121,9 +127,9 @@ def disburse(df: pd.DataFrame, pooled_fips: str, fips_list: list):
121127
pd.DataFrame
122128
Dataframe with same schema as df, with the counts disbursed.
123129
"""
124-
COLS = ["new_counts", "cumulative_counts"]
130+
cols = ["new_counts", "cumulative_counts"]
125131
df = df.copy().sort_values(["fips", "timestamp"])
126-
for col in COLS:
132+
for col in cols:
127133
# Get values from the aggregated county:
128134
vals = df.loc[df["fips"] == pooled_fips, col].values / len(fips_list)
129135
for fips in fips_list:
@@ -155,9 +161,6 @@ def geo_map(df: pd.DataFrame, geo_res: str, map_df: pd.DataFrame, sensor: str):
155161
pd.DataFrame
156162
Columns: geo_id, timestamp, ...
157163
"""
158-
VALID_GEO_RES = ("county", "state", "msa", "hrr")
159-
#It is not clear how to calculate the proportion for unallocated cases/deaths
160-
PROP_SENSORS = ("incidence", "cumulative_prop")
161164
if geo_res not in VALID_GEO_RES:
162165
raise ValueError(f"geo_res must be one of {VALID_GEO_RES}")
163166

usafacts/delphi_usafacts/pull.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
# -*- coding: utf-8 -*-
2+
"""Functions for pulling data from the USAFacts website."""
23
import numpy as np
34
import pandas as pd
45

6+
# Columns to drop the the data frame.
7+
DROP_COLUMNS = [
8+
"FIPS",
9+
"County Name",
10+
"State",
11+
"stateFIPS"
12+
]
13+
514

615
def pull_usafacts_data(base_url: str, metric: str, pop_df: pd.DataFrame) -> pd.DataFrame:
716
"""Pulls the latest USA Facts data, and conforms it into a dataset
@@ -43,24 +52,15 @@ def pull_usafacts_data(base_url: str, metric: str, pop_df: pd.DataFrame) -> pd.D
4352
pd.DataFrame
4453
Dataframe as described above.
4554
"""
46-
# Constants
47-
DROP_COLUMNS = [
48-
"FIPS",
49-
"County Name",
50-
"State",
51-
"stateFIPS"
52-
]
53-
# MIN_FIPS = 1000
54-
# MAX_FIPS = 57000
5555

5656
# Read data
5757
df = pd.read_csv(base_url.format(metric=metric)).rename({"countyFIPS":"FIPS"}, axis=1)
5858
# Check missing FIPS
5959
null_mask = pd.isnull(df["FIPS"])
6060
assert null_mask.sum() == 0
6161

62-
UNEXPECTED_COLUMNS = [x for x in df.columns if "Unnamed" in x]
63-
DROP_COLUMNS.extend(UNEXPECTED_COLUMNS)
62+
unexpected_columns = [x for x in df.columns if "Unnamed" in x]
63+
unexpected_columns.extend(DROP_COLUMNS)
6464

6565
# Assign Grand Princess Cruise Ship a special FIPS 90000
6666
# df.loc[df["FIPS"] == 6000, "FIPS"] = 90000
@@ -90,7 +90,7 @@ def pull_usafacts_data(base_url: str, metric: str, pop_df: pd.DataFrame) -> pd.D
9090
"Tried to drop non-existent columns. The dataset "
9191
"schema may have changed. Please investigate and "
9292
"amend DROP_COLUMNS."
93-
)
93+
) from e
9494
# Check that columns are either FIPS or dates
9595
try:
9696
columns = list(df.columns)
@@ -99,13 +99,12 @@ def pull_usafacts_data(base_url: str, metric: str, pop_df: pd.DataFrame) -> pd.D
9999
# Detects whether there is a non-date string column -- not perfect
100100
_ = [int(x.replace("/", "")) for x in columns]
101101
except ValueError as e:
102-
print(e)
103102
raise ValueError(
104103
"Detected unexpected column(s) "
105104
"after dropping DROP_COLUMNS. The dataset "
106105
"schema may have changed. Please investigate and "
107106
"amend DROP_COLUMNS."
108-
)
107+
) from e
109108
# Reshape dataframe
110109
df = df.melt(
111110
id_vars=["fips", "population"],

usafacts/delphi_usafacts/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868

6969
def run_module():
70-
70+
"""Run the usafacts indicator."""
7171
params = read_params()
7272
export_start_date = params["export_start_date"]
7373
if export_start_date == "latest":

usafacts/delphi_usafacts/smooth.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'''Functions for smoothing signals.'''
12
# -*- coding: utf-8 -*-
23
import numpy as np
34

0 commit comments

Comments
 (0)