-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvra_and_eg_table.py
316 lines (265 loc) · 14.8 KB
/
vra_and_eg_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import pandas as pd
import os
import json
import matplotlib.pyplot as plt
import gzip
# make smaller for faster runs, but not getting full ensemble
TESTING_TRUNCATOR = 100000
biden_thresh = .48
bvap_thresh = .44
map_type_relabeling = {"state_house": "HD", "state_senate": "SD", "house_1": "HD 1", "house_2": "HD 2", "senate": "SD 1", "congress": "CD"}
map_name_relabeling = {"Water Lily_vtd": "Water Lily VTD",
"Court Map_vtd": "Remedial Map (Motown Sound) VTD",
"Daisy 2_vtd": "Daisy 2 VTD",
"MI-HD-2022_vtd": "Invalidated Map VTD",
"Trillium_vtd": "Trillium VTD",
"Palm_vtd": "Palm VTD",
"MI-SD-2022_vtd": "Invalidated Map VTD",
"Linden_vtd": "Linden VTD",
"Cherry v2_vtd": "Cherry v2 VTD",
"MI-CD-2022_vtd": "Enacted Map VTD",
"Apple v2_vtd": "Apple v2 VTD",
"Birch v2_vtd": "Birch v2 VTD",
"Final Chestnut_vtd": "Chestnut VTD"
}
number_fixed_districts = {"house_1": 97, "house_2": 95, "senate":30 }
invalidated_maps = {"state_house": "MI-HD-2022_vtd",
"state_senate": "MI-SD-2022_vtd",
"congress": "MI-CD-2022_vtd"}
def sort_elections(elec_list):
"""
Helper function to sort elections chronologically for plotting. Assumes the last two characters
in the election name are the year, e.g. "SEN18"
"""
tuplified_elecs = list(map(lambda x: (x[:-2], x[-2:]), sorted(elec_list)))
sorted_tuples = sorted(tuplified_elecs, key=lambda x: x[1])
return [tup[0] + tup[1] for tup in sorted_tuples]
# for DF
data = {}
print("invalidated maps")
# invalidated map stats
for map_type in ["state_house", "state_senate", "congress"]:
proposed_plans = f"Michigan/plan_stats/{map_type}_proposed_plans.jsonl"
proposed_list = []
if os.path.exists(proposed_plans):
with open(proposed_plans, "rb") as fp:
proposed_list = list(fp)
invalidated_map = [json.loads(j) for j in proposed_list if json.loads(j)["type"] == "proposed_plan" and json.loads(j)["name"] ==invalidated_maps[map_type]][0]
invalidated_name = map_name_relabeling[invalidated_maps[map_type]]
# init plan data
plan_data = {"# Districts Scrambled": 0,
"# Districts Fixed": len(invalidated_map["TOTPOP"]),
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While OEG Serial Beats State": "0",
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |OEG Serial| < .04": "0" if abs(invalidated_map["avg_efficiency_gap"]) >= .04 else f"{invalidated_map[f'num_vra_effective_bvap_{bvap_thresh}_biden_{biden_thresh}']}", #compute
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While SEG Serial Beats State": "0",
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |SEG Serial| < .04": "0" if abs(invalidated_map["avg_s_efficiency_gap"]) >= .04 else f"{invalidated_map[f'num_vra_effective_bvap_{bvap_thresh}_biden_{biden_thresh}']}", #compute
}
# election indices
for i in [0,1,3]:
plan_data.update({
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While OEG Index {i} Beats State": "0",
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |OEG Index {i}| < .04": "0" if abs(invalidated_map["efficiency_gap"][f"Index_{i}"]) >= .04 else f"{invalidated_map[f'num_vra_effective_bvap_{bvap_thresh}_biden_{biden_thresh}']}",
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While SEG Index {i} Beats State": "0",
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |SEG Index {i}| < .04": "0" if abs(invalidated_map["s_efficiency_gap"][f"Index_{i}"]) >= .04 else f"{invalidated_map[f'num_vra_effective_bvap_{bvap_thresh}_biden_{biden_thresh}']}",
})
# print("plan data",plan_data)
data[f"{map_type_relabeling[map_type]} {invalidated_name}"] = plan_data
print("full scrambles")
# ensembles of full scrambles
for map_type in ["state_house", "state_senate", "congress"]:
if "house" in map_type:
region_aware_str = f"county_and_sub_aware_w{.33}_{.33}"
epsilon = .05
elif "senate" in map_type:
region_aware_str = f"county_and_sub_aware_w{.33}_{.66}"
epsilon = .05
else:
region_aware_str = f"county_and_sub_aware_w{.66}_{.66}"
epsilon = .01
method = f"{region_aware_str}_vra_neutral_theta_2.0_bvap_{bvap_thresh}_biden_{biden_thresh}"
with gzip.open(f"Michigan/ensemble_stats/michigan_{map_type}_{epsilon}_bal_100000_steps_{method}.jsonl.gz", "rb") as fe:
ensemble_list = list(fe)[:TESTING_TRUNCATOR]
ensemble_plans = [json.loads(j) for j in ensemble_list if json.loads(j)["type"] == "ensemble_plan"]
proposed_plans = f"Michigan/plan_stats/{map_type}_proposed_plans.jsonl"
proposed_list = []
if os.path.exists(proposed_plans):
with open(proposed_plans, "rb") as fp:
proposed_list = list(fp)
invalidated_map = [json.loads(j) for j in proposed_list if json.loads(j)["type"] == "proposed_plan" and json.loads(j)["name"] ==invalidated_maps[map_type]][0]
# print("OEG invaid", invalidated_map['avg_efficiency_gap'])
# print("SEG invalid", invalidated_map['avg_s_efficiency_gap'])
try:
vra_eg_state = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['avg_efficiency_gap']) < abs(invalidated_map['avg_efficiency_gap']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_state = "0"
else:
raise e
try:
vra_eg_04 = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['avg_efficiency_gap']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_04 = "0"
else:
raise e
try:
vra_seg_state = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['avg_s_efficiency_gap']) < abs(invalidated_map['avg_s_efficiency_gap']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_state = "0"
else:
raise e
try:
vra_seg_04 = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['avg_s_efficiency_gap']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_04 = "0"
else:
raise e
plan_data = {"# Districts Scrambled": len(ensemble_plans[0]["TOTPOP"]),
"# Districts Fixed": "0",
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While OEG Serial Beats State": vra_eg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |OEG Serial| < .04": vra_eg_04,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While SEG Serial Beats State": vra_seg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |SEG Serial| < .04": vra_seg_04,
}
# election indices
for i in [0,1,3]:
try:
vra_eg_state = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['efficiency_gap'][f'Index_{i}']) < abs(invalidated_map['efficiency_gap'][f'Index_{i}']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_state = "0"
else:
raise e
try:
vra_eg_04 = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['efficiency_gap'][f'Index_{i}']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_04 = "0"
else:
raise e
try:
vra_seg_state = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['s_efficiency_gap'][f'Index_{i}']) < abs(invalidated_map['s_efficiency_gap'][f'Index_{i}']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_state = "0"
else:
raise e
try:
vra_seg_04 = f"{max([plan['num_vra_effective'] for plan in ensemble_plans if abs(plan['s_efficiency_gap'][f'Index_{i}']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_04 = "0"
else:
raise e
plan_data.update({
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While OEG Index {i} Beats State": vra_eg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |OEG Index {i}| < .04": vra_eg_04,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While SEG Index {i} Beats State": vra_seg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |SEG Index {i}| < .04": vra_seg_04,
})
# print("plan data",plan_data)
data[f"{map_type_relabeling[map_type]} Full Run"] = plan_data
# ensembles of partial scrambles
print("partial scrambles")
for map_type in ["house_1", "house_2", "senate"]:
region_aware_str = f"county_sub_aware_w{.66}"
method = f"{region_aware_str}_vra_neutral_theta_2.0_bvap_{bvap_thresh}_biden_{biden_thresh}"
with gzip.open(f"Michigan_restricted/ensemble_stats/michigan_restricted_{map_type}_0.05_bal_100000_steps_{method}.jsonl.gz", "rb") as fe:
partial_ensemble_list = list(fe)[:TESTING_TRUNCATOR]
with gzip.open(f"Michigan_embed/ensemble_stats/michigan_embed_{map_type}_0.05_bal_100000_steps_{method}.jsonl.gz", "rb") as fe:
embed_ensemble_list = list(fe)[:TESTING_TRUNCATOR]
partial_ensemble_plans = [json.loads(j) for j in partial_ensemble_list if json.loads(j)["type"] == "ensemble_plan"]
embed_ensemble_plans = [json.loads(j) for j in embed_ensemble_list if json.loads(j)["type"] == "ensemble_plan"]
if "house" in map_type:
full_map = "state_house"
else:
full_map = "state_senate"
proposed_plans = f"Michigan/plan_stats/{full_map}_proposed_plans.jsonl"
proposed_list = []
if os.path.exists(proposed_plans):
with open(proposed_plans, "rb") as fp:
proposed_list = list(fp)
invalidated_map = [json.loads(j) for j in proposed_list if json.loads(j)["type"] == "proposed_plan" and json.loads(j)["name"] ==invalidated_maps[full_map]][0]
try:
vra_eg_state = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['avg_efficiency_gap']) < abs(invalidated_map['avg_efficiency_gap']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_state = "0"
else:
raise e
try:
vra_eg_04 = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['avg_efficiency_gap']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_04 = "0"
else:
raise e
try:
vra_seg_state = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['avg_s_efficiency_gap']) < abs(invalidated_map['avg_s_efficiency_gap']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_state = "0"
else:
raise e
try:
vra_seg_04 = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['avg_s_efficiency_gap']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_04 = "0"
else:
raise e
plan_data = {"# Districts Scrambled": len(partial_ensemble_plans[0]["TOTPOP"]),
"# Districts Fixed": len(embed_ensemble_plans[0]["TOTPOP"])-len(partial_ensemble_plans[0]["TOTPOP"]),
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While OEG Serial Beats State": vra_eg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |OEG Serial| < .04": vra_eg_04,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While SEG Serial Beats State": vra_seg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |SEG Serial| < .04": vra_seg_04,
}
# election indices
for i in [0,1,3]:
try:
vra_eg_state = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['efficiency_gap'][f'Index_{i}']) < abs(invalidated_map['efficiency_gap'][f'Index_{i}']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_state = "0"
else:
raise e
try:
vra_eg_04 = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['efficiency_gap'][f'Index_{i}']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_eg_04 = "0"
else:
raise e
try:
vra_seg_state = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['s_efficiency_gap'][f'Index_{i}']) < abs(invalidated_map['s_efficiency_gap'][f'Index_{i}']) ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_state = "0"
else:
raise e
try:
vra_seg_04 = f"{max([plan['num_vra_effective'] for plan in embed_ensemble_plans if abs(plan['s_efficiency_gap'][f'Index_{i}']) < .04 ])}"
except ValueError as e:
if "arg is an empty sequence" in str(e):
vra_seg_04 = "0"
else:
raise e
plan_data.update({
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While OEG Index {i} Beats State": vra_eg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |OEG Index {i}| < .04": vra_eg_04,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While SEG Index {i} Beats State": vra_seg_state,
f"Max # of VRA ({bvap_thresh}, {biden_thresh}) While |SEG Index {i}| < .04": vra_seg_04,
})
# print("plan data",plan_data)
data[f"{map_type_relabeling[map_type]} Scramble Run"] = plan_data
# keys of dictionary act as columns
df = pd.DataFrame.from_dict(data, orient="columns")
if TESTING_TRUNCATOR == 100000:
df.to_csv(f"Tables/vra_and_eg_table_biden_{biden_thresh}_bvap_{bvap_thresh}.csv")
else:
print("used truncator")
df.to_csv(f"Tables/TEST_{TESTING_TRUNCATOR}_vra_and_eg_table_biden_{biden_thresh}_bvap_{bvap_thresh}.csv")
print("done")