-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_ppartonly.py
311 lines (263 loc) · 13.4 KB
/
chain_ppartonly.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 9 16:47:51 2020
@author: dinos
This does NOT perform a Markov chain simulation but instead recreates EACH instance of a simulated district partition from scratch
using recursive_tree_part to create random districts. While this is slow,
Some nice stuff added to DataFrame structure to add congressional district labels in order of actual increasing congressional district No.
dependencies include stopit - install with pip (conda install didn't work for me)
It prevents recursive_tree_part from getting hung-up indefinitely, uses time_out to define maximum limit before timing out
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Created on Tue Mar 24 16:55:12 2020
uses recom proposal
@author: dpg
"""
import backup_chain as bc
from multiprocessing import set_start_method, freeze_support
#from multiprocessing import Pool
from multiprocessing import get_context
import backup_chain as bc
import matplotlib.pyplot as plt
import time
import stopit
from gerrychain import (GeographicPartition, Partition, Graph, MarkovChain,
proposals, updaters, constraints, accept, Election)
from gerrychain.proposals import recom
from gerrychain.tree import recursive_tree_part
from functools import partial
from strcmp_matlab import strfilter
import pandas
import numpy as np
import pandas as pd
from gerrychain.metrics import mean_median, efficiency_gap, polsby_popper
from get_districtlabels import get_labels
from norm_50 import norm_data
from get_electioninfo import get_elections
import random
import os
def multichain_run(i1, graph, chainlength, my_apportionment, poptol, my_electionproxy, my_electionproxy_alternate, rsw, rmm, reg, rpp, datastruct, state):
# poptol = 0.06 #min % population deviation per district
totsteps = 2
elections, composite = get_elections(state)
time_out = 400 #modify depending on how fast a machine usually performs recursive_tree_part
if "TOTPOP" in graph._node[0]:
popkey = "TOTPOP"
elif "PERSONS" in graph._node[0]:
popkey = "PERSONS"
else:
popkey = []
print("woops no popkey in file, look @ graph._node[0] to figure out what the keyword for population is\n")
#CONFIGURE UPDATERS
#We want to set up updaters for everything we want to compute for each plan in the ensemble.
# Population updater, for computing how close to equality the district
# populations are. "TOTPOP" is the population column from our shapefile.
my_updaters = {"population": updaters.Tally(popkey, alias="population")}
# Election updaters, for computing election results using the vote totals
# from our shapefile.
election_updaters = {election.name: election for election in elections}
my_updaters.update(election_updaters)
#INITIAL PARTITION (useful for CD labels)
initial_partition = GeographicPartition(graph, assignment=my_apportionment, updaters=my_updaters)
#this block obtains the Congressional District Labels and converts to string labels, cds
cds = get_labels(initial_partition, my_electionproxy) #get congressional district labels
nparts = len(initial_partition)
ideal_population = sum(list(initial_partition["population"].values())) / len(initial_partition)
random.seed(os.urandom(10)*i1)
pop_constraint = constraints.within_percent_of_ideal_population(initial_partition, poptol)
proposal = partial(recom,
pop_col=popkey,
pop_target=ideal_population,
epsilon=poptol,
node_repeats=2
)
itno = 0
for zz in range(chainlength):
with stopit.ThreadingTimeout(time_out) as to_ctx_mgr:
assert to_ctx_mgr.state == to_ctx_mgr.EXECUTING
ranpart = recursive_tree_part(graph, range(nparts), ideal_population, popkey,poptol-0.02,node_repeats=1)
randpartition = GeographicPartition(graph,assignment = ranpart, updaters = my_updaters)
if to_ctx_mgr.state == to_ctx_mgr.EXECUTED:
compactness_bound = constraints.UpperBound(
lambda p: len(p["cut_edges"]),
2*len(initial_partition["cut_edges"])
)
chain = MarkovChain(
proposal=proposal,
constraints=[
pop_constraint,
compactness_bound],
accept=accept.always_accept,
initial_state=randpartition,
total_steps = totsteps
)
print(i1, " got here\n")
for part in chain:
rsw.append(part[my_electionproxy].wins("Democratic"))
rmm.append(mean_median(part[my_electionproxy]))
reg.append(efficiency_gap(part[my_electionproxy]))
rpp.append(np.mean(pd.Series(polsby_popper(part)))) #depends on geometry of the partition only not on vote outcomes
datax = pandas.DataFrame(sorted(part[my_electionproxy].percents("Democratic" )), index=cds)
datax = datax.transpose()
# data1 = pandas.concat([data1, pandas.DataFrame(part["SEN12"].percents("Democratic" ))],axis=1)
datastruct = pandas.concat([datastruct, datax])
if itno % 1 == 0:
print("worker ", i1, " iteration = ", itno, "chain = ", zz ,"\n")
itno+=1
elif to_ctx_mgr.state == to_ctx_mgr.TIMED_OUT:
print("time out, worker ", i1, "\n")
for kk in range(totsteps):
rsw.append(-1 )
rmm.append(-100*mean_median(initial_partition[my_electionproxy]))
reg.append(-100*efficiency_gap(initial_partition[my_electionproxy]))
rpp.append(-1 )
datax = pandas.DataFrame(sorted(initial_partition[my_electionproxy].percents("Democratic" )), index=cds)
datax = datax.transpose()
# data1 = pandas.concat([data1, pandas.DataFrame(part["SEN12"].percents("Democratic" ))],axis=1)
datastruct = pandas.concat([datastruct, datax])
# Eeek the 100 seconds timeout occurred while executing the block
return i1, rsw, rmm, reg, rpp, datastruct
#MAIN PROGRAM HERE:
#few key lines for making parallel pool not mess up (freeze_support() and __spec__ definition)
if __name__ == '__main__':
freeze_support()
__spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
dontfeedin = 0 #if set=0, feeds in data, otherwise skip
poolsize=40
chainlength=2
totsteps = 2
normalize=' normalized'
postfix='1'
countysp=''
#DEFINE CONSTANTS:
dontfeedin = 0 #if set=0, feeds in data, otherwise skip
# exec(open("input_templates/MI_SENDIST_PRES16.py").read())
exec(open("input_templates/PA_HDIST_SEN12.py").read())
# my_electionproxy_alternate = my_electionproxy
#for PA data:
elections, composite = get_elections(state)
if 'dontfeedin' in globals():
if dontfeedin == 0 or not( 'graph' in globals()):
if ".json" in my_electiondatafile:
graph = Graph.from_json(my_electiondatafile)
else:
graph = Graph.from_file(my_electiondatafile)
else:
if ".json" in my_electiondatafile:
graph = Graph.from_json(my_electiondatafile)
else:
graph = Graph.from_file(my_electiondatafile)
if 'poptol' not in globals():
poptol = 0.03
if "TOTPOP" in graph._node[0]:
popkey = "TOTPOP"
elif "PERSONS" in graph._node[0]:
popkey = "PERSONS"
else:
popkey = []
print("woops no popkey in file, look @ graph._node[0] to figure out what the keyword for population is\n")
#CONFIGURE UPDATERS
#We want to set up updaters for everything we want to compute for each plan in the ensemble.
# Population updater, for computing how close to equality the district
# populations are. "TOTPOP" is the population column from our shapefile.
my_updaters = {"population": updaters.Tally(popkey, alias="population")}
election_updaters = {election.name: election for election in elections}
my_updaters.update(election_updaters)
#run chain ONCE to clean up graph and use primary election assignment name...
#INITIAL PARTITION
initial_partition = GeographicPartition(graph, assignment=my_apportionment, updaters=my_updaters)
# initial_partition, graph, my_updaters = norm_data(graph, my_updaters, my_apportionment, my_electionproxy, my_electionproxy_alternate)
# cds = get_labels(initial_partition, my_electionproxy) #get congressional district labels
#RUNNING THE CHAIN
ideal_population = sum(list(initial_partition["population"].values())) / len(initial_partition)
# We use functools.partial to bind the extra parameters (pop_col, pop_target, epsilon, node_repeats)
# of the recom proposal.
t0=time.time()
#now can do initial_partition and know my_electionproxy will be OK, won't need alternate
cds = get_labels(initial_partition, my_electionproxy) #get congressional district labels
# This will take about 10 minutes.
#setup variables
rsw = [[0 for x in range(1)] for x in range(poolsize)] # np.zeros([poolsize, chainlength])
rmm = [[0 for x in range(1)] for x in range(poolsize)] # np.zeros([poolsize, chainlength])
reg = [[0 for x in range(1)] for x in range(poolsize)] # np.zeros([poolsize, chainlength])
rpp = [[0 for x in range(1)] for x in range(poolsize)] # np.zeros([poolsize, chainlength])
data1 = pandas.DataFrame(sorted(initial_partition[my_electionproxy ].percents("Democratic") ), index=cds)
data1 = data1.transpose()
datastruct = []
#setup parallel list of DataFrames
for nn in range(poolsize):
datastruct.append(data1)
#key defs for setting up parallel pool HERE:
ctx = get_context("spawn")
p = ctx.Pool(poolsize)
updated_vals = p.starmap(multichain_run, [(i1, graph, chainlength, my_apportionment, poptol, my_electionproxy, my_electionproxy_alternate,
rsw[i1], rmm[i1], reg[i1], rpp[i1], datastruct[i1], state) for i1 in range(poolsize)])
for i1, rsw_updated, rmm_updated, reg_updated, rpp_updated, datastruct_updated in updated_vals:
rsw[i1] = rsw_updated
rmm[i1] = rmm_updated
reg[i1] = reg_updated
rpp[i1] = rpp_updated
datastruct[i1] = datastruct_updated
#clean up data
rsw_bak= rsw.copy() #just to be on the safe side
reg_bak = reg.copy()
rmm_bak = rmm.copy()
datastruct_bak = datastruct.copy()
for nn in range(poolsize): #clean up since 1st value in each list is a junk '0'
junk = rsw[nn].pop(0)
junk = reg[nn].pop(0)
junk = rmm[nn].pop(0)
junk = rpp[nn].pop(0)
iter1 = range(chainlength * totsteps) #since the correlation length is 200, only collect every 200th point
reg_clean = []
rmm_clean = []
rsw_clean = []
rpp_clean = []
for nn in range(poolsize):
for kk in iter1:
if rsw[nn][kk] > -1 : #skip over workers that failed timeout, with -1 in 'won districts'
reg_clean.append(reg[nn][kk])
rmm_clean.append(rmm[nn][kk])
rsw_clean.append(rsw[nn][kk])
# rpp_clean.append(rpp[nn][kk])
#data1 = data1.transpose()
#data1 = pandas.DataFrame((initial_partition["SEN12"].percents("Democratic") ))
t1=time.time()
#exec(open("condense_datastruct_minimal.py").read()) #run condense_datastruct.py as a script using this namespace
# RUN condense_datastruct.py after this to unpack the data structure and plot it
data_condensed = pandas.DataFrame([]) #null dataframe to start
threadcount = len(datastruct) #depth of datastruct list object
skipno = 1 # basically, don't skip b/c
for ii in range(threadcount):
data_x = datastruct[ii]
data_x.columns = cds
sx = data_x.shape
sx0 = sx[0] #this is the # of iterations per dataframe... loop thru these skipping every 100- 200
data_x.index = range(sx0)
# indexer = range(skipno-1, sx0+ skipno-1, skipno) #collect data from these rows
indexer = range(sx0-1)
for kk in indexer:
if rsw[ii][kk] > -1: #skip over workers that timed out
data_condensed= pandas.concat([data_condensed,data_x[kk:kk+1]])
outname = "redist_data/" + state + "_" + my_apportionment + "_" + my_electionproxy + "x" + \
str(chainlength)+ "x" + str(poolsize) + normalize + postfix
bc.save1(outname,data_condensed, reg_clean, rmm_clean, rpp_clean, rsw_clean, reg, rmm, rsw, rpp)
print(t1-t0, "seconds\n")
plt.figure()
fig, ax = plt.subplots(figsize=(8, 6))
# Draw 50% line
ax.axhline(0.5, color="#cccccc")
# Draw boxplot
#data1.boxplot(ax=ax, positions=range(len(data1.columns)))
data_condensed.boxplot(positions=range(len(data_condensed.columns)))
# Draw initial plan's Democratic vote %s (.iloc[0] gives the first row)
plt.plot(sorted(data1.iloc[0]), "ro")
# Annotate
titlestr = state + " " + my_apportionment + " x" + str(chainlength) + " x" + str(poolsize) + normalize + postfix
ax.set_title(titlestr)
ax.set_ylabel("Democratic vote % " + my_electionproxy)
ax.set_xlabel("Sorted districts")
ax.set_ylim(0, 1)
ax.set_yticks([0, 0.25, 0.5, 0.75, 1])
plt.show()