|
1 | 1 | """Contains geographic mapping tools."""
|
2 | 2 | from delphi_utils import GeoMapper
|
3 | 3 |
|
4 |
| -date_col = "timestamp" |
5 |
| -data_cols = ['totalTest', 'numUniqueDevices', 'positiveTest', "population"] |
6 |
| -gmpr = GeoMapper() # Use geo utils |
| 4 | +DATE_COL = "timestamp" |
| 5 | +DATA_COLS = ['totalTest', 'numUniqueDevices', 'positiveTest', "population"] |
| 6 | +GMPR = GeoMapper() # Use geo utils |
7 | 7 | GEO_KEY_DICT = {
|
8 | 8 | "county": "fips",
|
9 | 9 | "msa": "msa",
|
10 | 10 | "hrr": "hrr",
|
11 | 11 | "state": "state_id"
|
12 | 12 | }
|
| 13 | + |
| 14 | + |
13 | 15 | def geo_map(geo_res, df):
|
| 16 | + """ |
| 17 | + Map a geocode to a new value. |
| 18 | + """ |
14 | 19 | data = df.copy()
|
15 | 20 | geo_key = GEO_KEY_DICT[geo_res]
|
16 | 21 | # Add population for each zipcode
|
17 |
| - data = gmpr.add_population_column(data, "zip") |
| 22 | + data = GMPR.add_population_column(data, "zip") |
18 | 23 | # zip -> geo_res
|
19 |
| - data = gmpr.replace_geocode(data, "zip", geo_key, |
20 |
| - date_col=date_col, data_cols=data_cols) |
| 24 | + data = GMPR.replace_geocode(data, "zip", geo_key, |
| 25 | + date_col=DATE_COL, data_cols=DATA_COLS) |
21 | 26 | if geo_res == "state":
|
22 | 27 | return data
|
23 | 28 | # Add parent state
|
24 | 29 | data = add_parent_state(data, geo_res, geo_key)
|
25 | 30 | return data, geo_key
|
26 | 31 |
|
| 32 | + |
27 | 33 | def add_parent_state(data, geo_res, geo_key):
|
28 | 34 | """
|
29 | 35 | - map from msa/hrr to state, going by the state with the largest
|
30 | 36 | population (since a msa/hrr may span multiple states)
|
31 | 37 | - map from county to the corresponding state
|
32 | 38 | """
|
33 |
| - fips_to_state = gmpr._load_crosswalk(from_code="fips", to_code="state") |
| 39 | + fips_to_state = GMPR._load_crosswalk(from_code="fips", to_code="state") # pylint: disable=protected-access |
34 | 40 | if geo_res == "county":
|
35 |
| - mix_map = fips_to_state[["fips", "state_id"]] |
| 41 | + mix_map = fips_to_state[["fips", "state_id"]] # pylint: disable=unsubscriptable-object |
36 | 42 | else:
|
37 |
| - fips_to_geo_res = gmpr._load_crosswalk(from_code="fips", to_code=geo_res) |
| 43 | + fips_to_geo_res = GMPR._load_crosswalk(from_code="fips", to_code=geo_res) # pylint: disable=protected-access |
38 | 44 | mix_map = fips_to_geo_res[["fips", geo_res]].merge(
|
39 |
| - fips_to_state[["fips", "state_id"]], |
| 45 | + fips_to_state[["fips", "state_id"]], # pylint: disable=unsubscriptable-object |
40 | 46 | on="fips",
|
41 | 47 | how="inner")
|
42 |
| - mix_map = gmpr.add_population_column(mix_map, "fips").groupby( |
| 48 | + mix_map = GMPR.add_population_column(mix_map, "fips").groupby( |
43 | 49 | geo_res).max().reset_index().drop(
|
44 | 50 | ["fips", "population"], axis = 1)
|
45 | 51 | # Merge the info of parent state to the data
|
|
0 commit comments