Skip to content

Commit 5418c07

Browse files
committed
Add tests
1 parent 01a8ad7 commit 5418c07

File tree

2 files changed

+61
-72
lines changed

2 files changed

+61
-72
lines changed

_delphi_utils_python/delphi_utils/data/jhu_uid_fips_table.csv

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ jhu_uid,fips,weight
130130
84000046,46000,1.0
131131
84000047,47000,1.0
132132
84000048,48000,1.0
133-
84000049,49000,1.0
134133
84000050,50000,1.0
135134
84000051,51000,1.0
136135
84000053,53000,1.0
@@ -320,6 +319,7 @@ jhu_uid,fips,weight
320319
84002122,02122,1.0
321320
84002130,02130,1.0
322321
84002150,02150,1.0
322+
84002158,02158,1.0
323323
84002164,02164,1.0
324324
84002170,02170,1.0
325325
84002180,02180,1.0
@@ -1458,13 +1458,11 @@ jhu_uid,fips,weight
14581458
84025001,25001,1.0
14591459
84025003,25003,1.0
14601460
84025005,25005,1.0
1461-
84025007,25007,1.0
14621461
84025009,25009,1.0
14631462
84025011,25011,1.0
14641463
84025013,25013,1.0
14651464
84025015,25015,1.0
14661465
84025017,25017,1.0
1467-
84025019,25019,1.0
14681466
84025021,25021,1.0
14691467
84025023,25023,1.0
14701468
84025025,25025,1.0
@@ -1739,12 +1737,10 @@ jhu_uid,fips,weight
17391737
84029031,29031,1.0
17401738
84029033,29033,1.0
17411739
84029035,29035,1.0
1742-
84029037,29037,1.0
17431740
84029039,29039,1.0
17441741
84029041,29041,1.0
17451742
84029043,29043,1.0
17461743
84029045,29045,1.0
1747-
84029047,29047,1.0
17481744
84029049,29049,1.0
17491745
84029051,29051,1.0
17501746
84029053,29053,1.0
@@ -1768,7 +1764,6 @@ jhu_uid,fips,weight
17681764
84029089,29089,1.0
17691765
84029091,29091,1.0
17701766
84029093,29093,1.0
1771-
84029095,29095,1.0
17721767
84029097,29097,1.0
17731768
84029099,29099,1.0
17741769
84029101,29101,1.0
@@ -1803,7 +1798,6 @@ jhu_uid,fips,weight
18031798
84029159,29159,1.0
18041799
84029161,29161,1.0
18051800
84029163,29163,1.0
1806-
84029165,29165,1.0
18071801
84029167,29167,1.0
18081802
84029169,29169,1.0
18091803
84029171,29171,1.0
@@ -2068,7 +2062,6 @@ jhu_uid,fips,weight
20682062
84035061,35061,1.0
20692063
84036001,36001,1.0
20702064
84036003,36003,1.0
2071-
84036005,36005,1.0
20722065
84036007,36007,1.0
20732066
84036009,36009,1.0
20742067
84036011,36011,1.0
@@ -2089,14 +2082,12 @@ jhu_uid,fips,weight
20892082
84036041,36041,1.0
20902083
84036043,36043,1.0
20912084
84036045,36045,1.0
2092-
84036047,36047,1.0
20932085
84036049,36049,1.0
20942086
84036051,36051,1.0
20952087
84036053,36053,1.0
20962088
84036055,36055,1.0
20972089
84036057,36057,1.0
20982090
84036059,36059,1.0
2099-
84036061,36061,1.0
21002091
84036063,36063,1.0
21012092
84036065,36065,1.0
21022093
84036067,36067,1.0
@@ -2106,9 +2097,7 @@ jhu_uid,fips,weight
21062097
84036075,36075,1.0
21072098
84036077,36077,1.0
21082099
84036079,36079,1.0
2109-
84036081,36081,1.0
21102100
84036083,36083,1.0
2111-
84036085,36085,1.0
21122101
84036087,36087,1.0
21132102
84036089,36089,1.0
21142103
84036091,36091,1.0
@@ -3380,13 +3369,19 @@ jhu_uid,fips,weight
33803369
84056041,56041,1.0
33813370
84056043,56043,1.0
33823371
84056045,56045,1.0
3383-
84070002,25007,0.5
3384-
84070002,25019,0.5
3385-
84070003,29095,0.25
3386-
84070003,29165,0.25
3387-
84070003,29037,0.25
3388-
84070003,29047,0.25
3372+
84070002,25007,0.6191260718163777
3373+
84070002,25019,0.38087392818362226
3374+
84070003,29095,0.6214027691108004
3375+
84070003,29165,0.08233223983474929
3376+
84070003,29037,0.09169349717069915
3377+
84070003,29047,0.20457149388375118
33893378
84002158,02270,1.0
3379+
84046102,46113,1.0
3380+
84036061,36005,0.17011372565812588
3381+
84036061,36047,0.307059996639005
3382+
84036061,36061,0.1953630504303981
3383+
84036061,36081,0.27034994290986597
3384+
84036061,36085,0.05711328436260506
33903385
84070015,49000,1.0
33913386
84070016,49000,1.0
33923387
84070017,49000,1.0

_delphi_utils_python/tests/test_geomap.py

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,31 @@
77

88

99
class TestGeoMapper:
10+
# For complex FIPS <-> ZIP transfers, use
11+
# zip 45147, 45140 -> fips 39025; fips 39025, 30961 -> 45147
12+
# For a non-FIPS, use 10999
13+
# For a non-ZIP, use 00500
1014
fips_data = pd.DataFrame(
1115
{
12-
"fips": ["01123", "02340", "98633", "18181"],
13-
"num": [2, 0, 20, 10021],
14-
"den": [4, 0, 400, 100001],
15-
}
16-
)
17-
fips_data_2 = pd.DataFrame(
18-
{
19-
"fips": ["01123", "02340", "02002", "18633", "18181"],
20-
"date": [pd.Timestamp("2018-01-01")] * 5,
21-
"num": [2, 1, 20, np.nan, 10021],
22-
"den": [4, 1, 400, np.nan, 100001],
16+
"fips": [
17+
"39025",
18+
"30961",
19+
"02002",
20+
"10999",
21+
],
22+
"date": pd.date_range("2018-01-01", periods=4),
23+
"num": [2, np.nan, 20, 10021],
24+
"den": [2, 20, 40, np.nan]
2325
}
2426
)
25-
fips_data_3 = pd.DataFrame(
27+
zip_data = pd.DataFrame(
2628
{
27-
"fips": ["48059", "48253", "48441", "72003", "72005", "10999"],
28-
"date": [pd.Timestamp("2018-01-01")] * 3 + [pd.Timestamp("2018-01-03")] * 3,
29-
"num": [1, 2, 3, 4, 8, 5],
30-
"den": [2, 4, 7, 11, 100, 10],
29+
"zip": ["45140", "45147", "00500", "95616", "95618"],
30+
"date": pd.date_range("2018-01-01", periods=5),
31+
"count": [2, np.nan, 20, 10021],
32+
"total": [2, 20, 40, np.nan]
3133
}
3234
)
33-
zip_data = pd.DataFrame(
34-
{
35-
"zip": ["45140", "95616", "95618", "00500"] * 2,
36-
"date": [pd.Timestamp("2018-01-01")] * 3 + [pd.Timestamp("2018-01-03")] * 5,
37-
"count": [99, 345, 456, 100, 344, 442, 450, 470],
38-
}
39-
)
40-
zip_data["total"] = zip_data["count"] * 2
4135
jan_month = pd.bdate_range("2018-01-01", "2018-02-01")
4236
mega_data = pd.concat(
4337
(
@@ -59,14 +53,6 @@ class TestGeoMapper:
5953
),
6054
)
6155
)
62-
jhu_data = pd.DataFrame(
63-
{
64-
"fips_jhu": ["48059", "48253", "72005", "10999", "90010", "70002"],
65-
"date": [pd.Timestamp("2018-01-01")] * 3 + [pd.Timestamp("2018-01-03")] * 3,
66-
"num": [1, 2, 3, 4, 8, 5],
67-
"den": [2, 4, 7, 11, 100, 10],
68-
}
69-
)
7056
jhu_uid_data = pd.DataFrame(
7157
{
7258
"jhu_uid": [
@@ -76,10 +62,11 @@ class TestGeoMapper:
7662
"84013299",
7763
"84070002",
7864
"84000013",
79-
"84090002"
65+
"84090002",
8066
],
8167
"date": [pd.Timestamp("2018-01-01")] * 3
82-
+ [pd.Timestamp("2018-01-03")] * 3 + [pd.Timestamp('2018-01-01')],
68+
+ [pd.Timestamp("2018-01-03")] * 3
69+
+ [pd.Timestamp("2018-01-01")],
8370
"num": [1, 2, 3, 4, 8, 5, 20],
8471
"den": [2, 4, 7, 11, 100, 10, 40],
8572
}
@@ -116,6 +103,24 @@ def test_load_zip_hrr_table(self):
116103
assert pd.api.types.is_string_dtype(zip_data["zip"])
117104
assert pd.api.types.is_string_dtype(zip_data["hrr"])
118105

106+
def test_add_new_code(self):
107+
gmpr = GeoMapper()
108+
109+
# fips -> zip
110+
new_data = gmpr.convert_fips_to_zip(self.fips_data)
111+
new_data2 = gmpr.add_new_code(self.fips_data, "fips", "zip")
112+
assert new_data.equals(new_data2)
113+
114+
# zip -> hrr
115+
new_data = gmpr.convert_to_new_code(self.fips_data, "fips", "zip")
116+
new_data2 = gmpr.add_new_code(new_data, "zip", "hrr")
117+
assert new_data.equals(new_data)
118+
119+
# fips -> hrr
120+
new_data = gmpr.convert_to_new_code(self.fips_data, "fips", "hrr")
121+
new_data2 = gmpr.add_new_code(self.fips_data, "fips", "hrr")
122+
assert new_data.shape == (2, 4)
123+
119124
def test_convert_fips_to_state_code(self):
120125
gmpr = GeoMapper()
121126
new_data = gmpr.convert_fips_to_state_code(self.fips_data)
@@ -137,22 +142,22 @@ def test_convert_fips_to_state_id(self):
137142

138143
def test_fips_to_state_id(self):
139144
gmpr = GeoMapper()
140-
new_data = gmpr.fips_to_state_id(self.fips_data_2)
145+
new_data = gmpr.fips_to_state_id(self.fips_data)
141146
assert new_data.shape[0] == 3
142147

143148
def test_convert_fips_to_msa(self):
144149
gmpr = GeoMapper()
145-
new_data = gmpr.convert_fips_to_msa(self.fips_data_3)
146-
assert new_data['msa'][2] == "10180"
147-
new_data = gmpr.convert_fips_to_msa(self.fips_data_3,create_mega=True)
148-
assert new_data['den'].sum() == self.fips_data_3['den'].sum()
150+
new_data = gmpr.convert_fips_to_msa(self.fips_data)
151+
assert new_data["msa"][2] == "10180"
152+
new_data = gmpr.convert_fips_to_msa(self.fips_data, create_mega=True)
153+
assert new_data["den"].sum() == self.fips_data["den"].sum()
149154

150155
def test_fips_to_msa(self):
151156
gmpr = GeoMapper()
152-
new_data = gmpr.fips_to_msa(self.fips_data_3)
157+
new_data = gmpr.fips_to_msa(self.fips_data)
153158
assert new_data.shape[0] == 2
154-
new_data = gmpr.fips_to_msa(self.fips_data_3, create_mega=True)
155-
assert new_data[["num"]].sum()[0] == self.fips_data_3["num"].sum()
159+
new_data = gmpr.fips_to_msa(self.fips_data, create_mega=True)
160+
assert new_data[["num"]].sum()[0] == self.fips_data["num"].sum()
156161

157162
def test_convert_zip_to_fips(self):
158163
gmpr = GeoMapper()
@@ -224,16 +229,5 @@ def test_jhu_uid_to_fips(self):
224229
assert not (new_data["fips"].astype(int) > 90000).any()
225230
assert new_data["den"].sum() == self.jhu_uid_data["den"].sum()
226231

227-
def test_convert_fips_to_zip(self):
228-
gmpr = GeoMapper()
229-
new_data = gmpr.convert_fips_to_zip(self.fips_data_3)
230-
# assert new_data.eval('weight * den').sum() == self.fips_data_3['den'].sum()
231-
232-
def test_fips_to_zip(self):
233-
gmpr = GeoMapper()
234-
new_data = gmpr.fips_to_zip(self.fips_data_3)
235232

236-
def test_fips_to_hrr(self):
237-
gmpr = GeoMapper()
238-
new_data = gmpr.fips_to_hrr(self.fips_data_3)
239-
assert new_data.shape == (2, 4)
233+
TestGeoMapper().test_add_new_code()

0 commit comments

Comments
 (0)