|
7 | 7 |
|
8 | 8 | # first party |
9 | 9 | from delphi.utils.geo.locations import Locations |
10 | | -from delphi.epidata.acquisition.fluview.impute_missing_values import get_argument_parser, \ |
11 | | - get_lag_and_ili, impute_missing_values, StatespaceException |
| 10 | +from delphi.epidata.acquisition.fluview.impute_missing_values import ( |
| 11 | + get_argument_parser, |
| 12 | + get_lag_and_ili, |
| 13 | + impute_missing_values, |
| 14 | + StatespaceException, |
| 15 | +) |
12 | 16 |
|
13 | 17 | # py3tester coverage target |
14 | | -__test_target__ = 'delphi.epidata.acquisition.fluview.impute_missing_values' |
| 18 | +__test_target__ = "delphi.epidata.acquisition.fluview.impute_missing_values" |
15 | 19 |
|
16 | 20 |
|
17 | 21 | class FunctionTests(unittest.TestCase): |
18 | | - """Tests each function individually.""" |
19 | | - |
20 | | - def test_get_argument_parser(self): |
21 | | - """An ArgumentParser is returned.""" |
22 | | - self.assertIsInstance(get_argument_parser(), argparse.ArgumentParser) |
23 | | - |
24 | | - def test_get_lag_and_ili(self): |
25 | | - samples = ( |
26 | | - ((201740, 201730, 5, 10), (10, 50)), |
27 | | - ((201740, 201740, 1, 1), (0, 100)), |
28 | | - ((201501, 201452, 0, 0), (2, 0)), |
29 | | - ) |
30 | | - |
31 | | - for args, expected in samples: |
32 | | - with self.subTest(args=args): |
33 | | - actual = get_lag_and_ili(*args) |
34 | | - self.assertEqual(actual, expected) |
35 | | - |
36 | | - def test_impute_missing_values(self): |
37 | | - """Atoms are imputed and stored.""" |
38 | | - |
39 | | - unknown_set = set(['pa', 'tx']) |
40 | | - known_set = set(['nat', 'hhs6'] + Locations.atom_list) - unknown_set |
41 | | - known_data = {} |
42 | | - for loc in known_set: |
43 | | - n = len(Locations.region_map[loc]) |
44 | | - known_data[loc] = (n, n, n) |
45 | | - |
46 | | - db = MagicMock() |
47 | | - db.count_rows.return_value = 123 |
48 | | - db.find_missing_rows.return_value = [(201740, 201740)] |
49 | | - db.get_known_values.return_value = known_data |
50 | | - |
51 | | - impute_missing_values(db, test_mode=True) |
52 | | - |
53 | | - self.assertEqual(db.connect.call_count, 1) |
54 | | - self.assertTrue(db.count_rows.call_count >= 1) |
55 | | - self.assertTrue(db.find_missing_rows.call_count >= 1) |
56 | | - self.assertEqual(db.add_imputed_values.call_count, 1) |
57 | | - self.assertEqual(db.close.call_count, 1) |
58 | | - self.assertFalse(db.close.call_args[0][0]) |
59 | | - |
60 | | - imputed = db.add_imputed_values.call_args[0][-1] |
61 | | - self.assertTrue(unknown_set <= set(imputed.keys())) |
62 | | - for loc, (lag, n_ili, n_pat, n_prov, ili) in imputed.items(): |
63 | | - with self.subTest(loc=loc): |
64 | | - num = len(Locations.region_map[loc]) |
65 | | - self.assertEqual(lag, 0) |
66 | | - self.assertEqual(n_ili, num) |
67 | | - self.assertEqual(n_pat, num) |
68 | | - self.assertEqual(n_prov, num) |
69 | | - self.assertEqual(ili, 100) |
70 | | - |
71 | | - def test_impute_missing_values_vipr(self): |
72 | | - """PR and VI are imputed only when appropriate.""" |
73 | | - |
74 | | - unknown_set = set(['vi', 'pr']) |
75 | | - known_set = set(['nat'] + Locations.atom_list) - unknown_set |
76 | | - known_data = {} |
77 | | - for loc in known_set: |
78 | | - n = len(Locations.region_map[loc]) |
79 | | - known_data[loc] = (n, n, n) |
80 | | - |
81 | | - db = MagicMock() |
82 | | - db.count_rows.return_value = 123 |
83 | | - db.get_known_values.return_value = known_data |
84 | | - |
85 | | - db.find_missing_rows.return_value = [(201340, 201340)] |
86 | | - with self.assertRaises(Exception): |
87 | | - impute_missing_values(db, test_mode=True) |
88 | | - |
89 | | - db.find_missing_rows.return_value = [(201339, 201339)] |
90 | | - impute_missing_values(db, test_mode=True) |
91 | | - |
92 | | - imputed = db.add_imputed_values.call_args[0][-1] |
93 | | - self.assertIn('pr', set(imputed.keys())) |
94 | | - |
95 | | - def test_impute_missing_values_regions(self): |
96 | | - """Regions are imputed in addition to atoms.""" |
97 | | - |
98 | | - known_set = set(Locations.atom_list) |
99 | | - known_data = {} |
100 | | - for loc in known_set: |
101 | | - known_data[loc] = (1, 2, 3) |
102 | | - |
103 | | - db = MagicMock() |
104 | | - db.count_rows.return_value = 123 |
105 | | - db.find_missing_rows.return_value = [(201740, 201740)] |
106 | | - db.get_known_values.return_value = known_data |
107 | | - |
108 | | - impute_missing_values(db, test_mode=True) |
109 | | - imputed = db.add_imputed_values.call_args[0][-1] |
110 | | - self.assertIn('nat', set(imputed.keys())) |
111 | | - self.assertIn('hhs2', set(imputed.keys())) |
112 | | - self.assertIn('cen3', set(imputed.keys())) |
113 | | - self.assertIn('ny', set(imputed.keys())) |
114 | | - |
115 | | - def test_impute_missing_values_underdetermined(self): |
116 | | - """Fail when the system is underdetermined.""" |
117 | | - |
118 | | - unknown_set = set(['pa', 'tx']) |
119 | | - known_set = set(Locations.atom_list) - unknown_set |
120 | | - known_data = {} |
121 | | - for loc in known_set: |
122 | | - n = len(Locations.region_map[loc]) |
123 | | - known_data[loc] = (n, n, n) |
124 | | - |
125 | | - db = MagicMock() |
126 | | - db.count_rows.return_value = 123 |
127 | | - db.find_missing_rows.return_value = [(201740, 201740)] |
128 | | - db.get_known_values.return_value = known_data |
129 | | - |
130 | | - with self.assertRaises(StatespaceException): |
131 | | - impute_missing_values(db, test_mode=True) |
| 22 | + """Tests each function individually.""" |
| 23 | + |
| 24 | + def test_get_argument_parser(self): |
| 25 | + """An ArgumentParser is returned.""" |
| 26 | + self.assertIsInstance(get_argument_parser(), argparse.ArgumentParser) |
| 27 | + |
| 28 | + def test_get_lag_and_ili(self): |
| 29 | + samples = ( |
| 30 | + ((201740, 201730, 5, 10), (10, 50)), |
| 31 | + ((201740, 201740, 1, 1), (0, 100)), |
| 32 | + ((201501, 201452, 0, 0), (2, 0)), |
| 33 | + ) |
| 34 | + |
| 35 | + for args, expected in samples: |
| 36 | + with self.subTest(args=args): |
| 37 | + actual = get_lag_and_ili(*args) |
| 38 | + self.assertEqual(actual, expected) |
| 39 | + |
| 40 | + def test_impute_missing_values(self): |
| 41 | + """Atoms are imputed and stored.""" |
| 42 | + |
| 43 | + unknown_set = set(["pa", "tx"]) |
| 44 | + known_set = set(["nat", "hhs6"] + Locations.atom_list) - unknown_set |
| 45 | + known_data = {} |
| 46 | + for loc in known_set: |
| 47 | + n = len(Locations.region_map[loc]) |
| 48 | + known_data[loc] = (n, n, n) |
| 49 | + |
| 50 | + db = MagicMock() |
| 51 | + db.count_rows.return_value = 123 |
| 52 | + db.find_missing_rows.return_value = [(201740, 201740)] |
| 53 | + db.get_known_values.return_value = known_data |
| 54 | + |
| 55 | + impute_missing_values(db, test_mode=True) |
| 56 | + |
| 57 | + self.assertEqual(db.connect.call_count, 1) |
| 58 | + self.assertTrue(db.count_rows.call_count >= 1) |
| 59 | + self.assertTrue(db.find_missing_rows.call_count >= 1) |
| 60 | + self.assertEqual(db.add_imputed_values.call_count, 1) |
| 61 | + self.assertEqual(db.close.call_count, 1) |
| 62 | + self.assertFalse(db.close.call_args[0][0]) |
| 63 | + |
| 64 | + imputed = db.add_imputed_values.call_args[0][-1] |
| 65 | + self.assertTrue(unknown_set <= set(imputed.keys())) |
| 66 | + for loc, (lag, n_ili, n_pat, n_prov, ili) in imputed.items(): |
| 67 | + with self.subTest(loc=loc): |
| 68 | + num = len(Locations.region_map[loc]) |
| 69 | + self.assertEqual(lag, 0) |
| 70 | + self.assertEqual(n_ili, num) |
| 71 | + self.assertEqual(n_pat, num) |
| 72 | + self.assertEqual(n_prov, num) |
| 73 | + self.assertEqual(ili, 100) |
| 74 | + |
| 75 | + def test_impute_missing_values_vipr(self): |
| 76 | + """PR and VI are imputed only when appropriate.""" |
| 77 | + |
| 78 | + unknown_set = set(["vi", "pr"]) |
| 79 | + known_set = set(["nat"] + Locations.atom_list) - unknown_set |
| 80 | + known_data = {} |
| 81 | + for loc in known_set: |
| 82 | + n = len(Locations.region_map[loc]) |
| 83 | + known_data[loc] = (n, n, n) |
| 84 | + |
| 85 | + db = MagicMock() |
| 86 | + db.count_rows.return_value = 123 |
| 87 | + db.get_known_values.return_value = known_data |
| 88 | + |
| 89 | + db.find_missing_rows.return_value = [(201340, 201340)] |
| 90 | + with self.assertRaises(Exception): |
| 91 | + impute_missing_values(db, test_mode=True) |
| 92 | + |
| 93 | + db.find_missing_rows.return_value = [(201339, 201339)] |
| 94 | + impute_missing_values(db, test_mode=True) |
| 95 | + |
| 96 | + imputed = db.add_imputed_values.call_args[0][-1] |
| 97 | + self.assertIn("pr", set(imputed.keys())) |
| 98 | + |
| 99 | + def test_impute_missing_values_regions(self): |
| 100 | + """Regions are imputed in addition to atoms.""" |
| 101 | + |
| 102 | + known_set = set(Locations.atom_list) |
| 103 | + known_data = {} |
| 104 | + for loc in known_set: |
| 105 | + known_data[loc] = (1, 2, 3) |
| 106 | + |
| 107 | + db = MagicMock() |
| 108 | + db.count_rows.return_value = 123 |
| 109 | + db.find_missing_rows.return_value = [(201740, 201740)] |
| 110 | + db.get_known_values.return_value = known_data |
| 111 | + |
| 112 | + impute_missing_values(db, test_mode=True) |
| 113 | + imputed = db.add_imputed_values.call_args[0][-1] |
| 114 | + self.assertIn("nat", set(imputed.keys())) |
| 115 | + self.assertIn("hhs2", set(imputed.keys())) |
| 116 | + self.assertIn("cen3", set(imputed.keys())) |
| 117 | + self.assertIn("ny", set(imputed.keys())) |
| 118 | + |
| 119 | + def test_impute_missing_values_underdetermined(self): |
| 120 | + """Fail when the system is underdetermined.""" |
| 121 | + |
| 122 | + unknown_set = set(["pa", "tx"]) |
| 123 | + known_set = set(Locations.atom_list) - unknown_set |
| 124 | + known_data = {} |
| 125 | + for loc in known_set: |
| 126 | + n = len(Locations.region_map[loc]) |
| 127 | + known_data[loc] = (n, n, n) |
| 128 | + |
| 129 | + db = MagicMock() |
| 130 | + db.count_rows.return_value = 123 |
| 131 | + db.find_missing_rows.return_value = [(201740, 201740)] |
| 132 | + db.get_known_values.return_value = known_data |
| 133 | + |
| 134 | + with self.assertRaises(StatespaceException): |
| 135 | + impute_missing_values(db, test_mode=True) |
0 commit comments