-
Notifications
You must be signed in to change notification settings - Fork 1
/
timingstuff.py
325 lines (261 loc) · 14.2 KB
/
timingstuff.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
317
318
319
320
321
322
323
324
325
time1 = time.time()
for i in range(100):
dangle = neighbor(state)
time2 = time.time()
dungus = MH(state, 100, neighbor, goodness, switchDistrict)
time3 = time.time()
t2-t1
t3-t2
t4-t3
t5-t4
t6-t5
t7-t6
t8-t7
t8-t2
##################################################################################
#oldContNeighborhoodHigh = contiguousness( state.loc[neighborhood], temphighdist, previousVersion)
#statecopy = state.copy()
state = statecopy.loc[neighborhood]
district = temphighdist
subframe = previousVersion
#state = statecopy
def contiguousness(state, district, subframe = "DEFAULT"):
#This function is going to count the numbr of disjoint, connected regeions of the district.
#The arguments are a state of assignments of precincts to CDs, a district to evaluate, and
# a subframe, which is a subset of the adjacencies so we can check contiguousness on a relative
# topology.
t1 = time.time()
regions = 0
#start with 0
regionlist = list(state.key[state.value == district])
#We're going to keep track of the precincts that have been used already.
if len(regionlist) == 0:
pass
#If there's nothing in this district...
# return 1
# ... we say that it's in one piece.
#if type(subframe) == str:
#If the subframe passed is the default, then use anything in the adjacencyframe that's in the district.
subframe = adjacencyFrame.ix[(adjacencyFrame.lowdist == district) & (adjacencyFrame.highdist == district), :]
#else:
subedges = subframe[subframe.length != 0][['low','high']]
t1a = np.empty(20)
t2a = np.empty(20)
t3a = np.empty(20)
t4a = np.empty(20)
t2 = time.time()
while len(regionlist) > 0:
loops = 0
regions += 1
currentregion = set()
addons = {regionlist[0]}
while len(addons) > 0:
t1a[loops] = time.time()
currentregion = currentregion.union(addons)
t2a[loops] = time.time()
subsubedges = subedges.loc[subedges.low.isin(currentregion) | subedges.high.isin(currentregion)]
t3a[loops] = time.time()
if(not subsubedges.empty):
addons = set(subsubedges['low']).union(set(subsubedges['high'])) - currentregion
else:
addons = set()
t4a[loops] = time.time()
loops += 1
regionlist = [x for x in regionlist if x not in currentregion]
t1a = t1a[:loops]
t2a = t2a[:loops]
t3a = t3a[:loops]
t4a = t4a[:loops]
t3 = time.time()
return regions
t4a-t3a
t3a-t2a
t2a-t1a
sum(t4a-t1a)
sum(t4a-t3a) # 0.002
sum(t3a-t2a) # 0.009
sum(t2a-t1a) # 3e-05
def contiguousness(state, district, subframe = "DEFAULT"):
#This function is going to count the numbr of disjoint, connected regeions of the district.
#The arguments are a state of assignments of precincts to CDs, a district to evaluate, and
# a subframe, which is a subset of the adjacencies so we can check contiguousness on a relative
# topology.
#It is assumed that the precincts are uniquely identified by integers.
t1 = time.time()
regions = 0
#start with 0
regionlist = state.key[state.value == district].values
#We're going to keep track of the precincts that have been used already.
# ... we say that it's in one piece.
#if type(subframe) == str:
#If the subframe passed is the default, then use anything in the adjacencyframe that's in the district.
subframe = adjacencyFrame.ix[(adjacencyFrame.lowdist == district) & (adjacencyFrame.highdist == district), :]
subedges = subframe[subframe.length != 0][['low','high']]
t1a = np.empty(20)
t2a = np.empty(20)
t3a = np.empty(20)
t4a = np.empty(20)
t2 = time.time()
while len(regionlist) > 0:
loops = 0
regions += 1
currentregion = np.array([], dtype=int)
addons = np.array([regionlist[0]])
while len(addons) > 0:
t1a[loops] = time.time()
currentregion = np.concatenate((currentregion, addons))
t2a[loops] = time.time()
subsubedges = subedges.ix[subedges.low.isin(addons) | subedges.high.isin(addons),:]
t3a[loops] = time.time()
if(not subsubedges.empty):
addons = np.setdiff1d(np.concatenate((subsubedges['low'].values, subsubedges['high'].values)), currentregion)
else:
addons = np.array([], dtype=int)
t4a[loops] = time.time()
loops += 1
regionlist = np.setdiff1d(regionlist, currentregion, assume_unique=True)
t3 = time.time()
t1a = t1a[:loops]
t2a = t2a[:loops]
t3a = t3a[:loops]
t4a = t4a[:loops]
return regions
t4a-t3a # 0.002
t3a-t2a # 0.01
t2a-t1a # 3e-05
sum(t4a-t1a)
sum(t4a-t3a)
sum(t3a-t2a)
sum(t2a-t1a)
def neighbor(state):
#stConts = [contiguousness(runningState[0], i) for i in range(ndistricts)]
#stPops = [ population(runningState[0], i) for i in range(ndistricts)]
#stBiz = [ bizarreness(runningState[0], i) for i in range(ndistricts)]
#stPerim = [ perimeter(runningState[0], i) for i in range(ndistricts)]
#stArea = [ distArea(runningState[0], i) for i in range(ndistricts)]
global adjacencyFrame, metrics
t1 = time.time()
newstate = state.copy()
newmetrics = metrics.copy()
missingdist = set.difference(set(range(ndistricts)), set(newstate['value']))
#If we've blobbed out some districts, we wants to behave differently
t2 = time.time()
if len(missingdist) == 0:
switchedge = np.random.choice(adjacencyFrame.index[-(adjacencyFrame.isSame == 1)])
lownode = adjacencyFrame.low[switchedge]
highnode = adjacencyFrame.high[switchedge]
templowdist = adjacencyFrame.lowdist[switchedge]
temphighdist = adjacencyFrame.highdist[switchedge]
#Randomly choose an adjacency. Find the low node and high node for that adjacency.
t3 = time.time()
#switch low node stuff to high node's district
switchTo = temphighdist
previousVersion = adjacencyFrame[(adjacencyFrame.low == lownode) | (adjacencyFrame.high == lownode)]
proposedChanges = previousVersion.copy()
#want proposedChanges to be a slice of adjacencyFrame where the values could be changing.
newstate.ix[newstate.key == lownode, 'value'] = switchTo
proposedChanges.ix[proposedChanges.low == lownode, 'lowdist'] = switchTo
proposedChanges.ix[proposedChanges.high == lownode, 'highdist'] = switchTo
proposedChanges.isSame = proposedChanges.lowdist == proposedChanges.highdist
#change values in the state as well as the proposedChanges
t4 = time.time()
#change population
popchange = blockstats.population[lownode]
newmetrics.ix[templowdist, 'population'] -= popchange
newmetrics.ix[temphighdist, 'population'] += popchange
t5 = time.time()
#change bizarreness
newmetrics.ix[templowdist,'perimeter'] += \
(sum(previousVersion.length[ (previousVersion.isSame==1) & ((previousVersion.low == lownode) | (previousVersion.high == lownode))]) -\
sum(previousVersion.length[~(previousVersion.isSame==1) & ((previousVersion.low == lownode) | (previousVersion.high == lownode))]))
newmetrics.ix[temphighdist, 'perimeter'] += \
(sum(proposedChanges.length[~(proposedChanges.isSame==1) & ((proposedChanges.low == lownode) | (proposedChanges.high == lownode))]) -\
sum(proposedChanges.length[ (proposedChanges.isSame==1) & ((proposedChanges.low == lownode) | (proposedChanges.high == lownode))]))
areachange = blockstats.ALAND[lownode] + blockstats.AWATER[lownode]
newmetrics.ix[templowdist, 'area'] -= areachange
newmetrics.ix[temphighdist,'area'] += areachange
newmetrics.ix[templowdist, 'bizarreness'] = bizarreness(newmetrics['area'][templowdist], \
newmetrics['perimeter'][templowdist])
newmetrics.ix[temphighdist, 'bizarreness'] = bizarreness(newmetrics['area'][temphighdist], \
newmetrics['perimeter'][temphighdist])
t6 = time.time()
#update boundary information
newmetrics.ix[temphighdist,'sumAframDiff'] = newmetrics.ix[temphighdist,'sumAframDiff']\
+ np.sum((-proposedChanges.isSame)*proposedChanges.aframdiff)\
- np.sum((-previousVersion.isSame)*previousVersion.aframdiff)
newmetrics.ix[templowdist,'sumAframDiff'] = newmetrics.ix[templowdist,'sumAframDiff']\
- np.sum((-proposedChanges.isSame)*proposedChanges.aframdiff)\
+ np.sum((-previousVersion.isSame)*previousVersion.aframdiff)
newmetrics.ix[temphighdist,'sumHispDiff'] = newmetrics.ix[temphighdist,'sumHispDiff']\
+ np.sum((-proposedChanges.isSame)*proposedChanges.hispdiff)\
- np.sum((-previousVersion.isSame)*previousVersion.hispdiff)
newmetrics.ix[templowdist,'sumHispDiff'] = newmetrics.ix[templowdist,'sumHispDiff']\
- np.sum((-proposedChanges.isSame)*proposedChanges.hispdiff)\
+ np.sum((-previousVersion.isSame)*previousVersion.hispdiff)
newmetrics.ix[temphighdist,'numedges'] = newmetrics.ix[temphighdist,'numedges']\
+ np.sum(-(proposedChanges.isSame))\
- np.sum(-(previousVersion.isSame))
newmetrics.ix[templowdist,'numedges'] = newmetrics.ix[templowdist,'numedges']\
- np.sum(-(proposedChanges.isSame))\
+ np.sum(-(previousVersion.isSame))
t7 = time.time()
#update contiguousness
#t1 = time.time()
neighborhood = set(proposedChanges.low).union(set(proposedChanges.high))
oldContNeighborhoodLow = contiguousness( state.loc[neighborhood], templowdist, previousVersion)
oldContNeighborhoodHigh = contiguousness( state.loc[neighborhood], temphighdist, previousVersion)
newContNeighborhoodLow = contiguousness(newstate.loc[neighborhood], templowdist, proposedChanges)
newContNeighborhoodHigh = contiguousness(newstate.loc[neighborhood], temphighdist, proposedChanges)
#t2 = time.time()
n1dict = {i: (0,1-float(state.value[i])/ndistricts, float(state.value[i])/ndistricts) for i in list(neighborhood)}
n1dict[lownode] = tuple((x + 1.0)/2 for x in n1dict[lownode])
n1dict[highnode] = tuple((x + 1.0)/2 for x in n1dict[highnode])
color_by_rgb(g, n1dict, "tests/neighborhood.png", linewidth = 0.03, DPI = 1000)
n2dict = {i: (0,1-float(newstate.value[i])/ndistricts, float(newstate.value[i])/ndistricts) for i in list(neighborhood)}
n2dict[lownode] = tuple((x + 1.0)/2 for x in n2dict[lownode])
n2dict[highnode] = tuple((x + 1.0)/2 for x in n2dict[highnode])
color_by_rgb(g, n2dict, "tests/neighborhood2.png", linewidth = 0.03, DPI = 1000)
if ((oldContNeighborhoodLow != newContNeighborhoodLow)|(oldContNeighborhoodHigh != newContNeighborhoodHigh)):
tempframe = adjacencyFrame.copy()
tempframe.update(proposedChanges)
tempframe.lowdist = tempframe.lowdist.astype(int)
tempframe.highdist = tempframe.highdist.astype(int)
if (oldContNeighborhoodLow != newContNeighborhoodLow):
newmetrics.ix[templowdist, 'contiguousness'] = contiguousness(newstate, templowdist, tempframe)
else:
pass
if (oldContNeighborhoodHigh != newContNeighborhoodHigh):
newmetrics.ix[temphighdist, 'contiguousness'] = contiguousness(newstate, temphighdist, tempframe)
else:
pass
t8 = time.time()
else:
#If there are some districts missing,
changenode = newstate.key.sample(1)
olddist = newstate.value[changenode]
newdist = list(missingdist)[0]
newstate.value[newstate.key == changenode] = newdist
#We want to select one randomly, and make it one of the missing districts
proposedChanges = adjacencyFrame.loc[(adjacencyFrame.low == changenode) | \
(adjacencyFrame.high == changenode)]
proposedChanges.lowdist[proposedChanges.low == changenode] = newdist
proposedChanges.highdist[proposedChanges.high == changenode] = newdist
proposedChanges.isSame = False
# And none of its adjacencies match anymore.
#change contiguousness
newmetrics['contiguousness'][olddist] = contiguousness(newstate, olddist)
#change population
popchange = blockstats.population[changenode]
newmetrics['population'][olddist] -= popchange
newmetrics['population'][newdist] += popchange
#change bizarreness
newmetrics['perimeter'][olddist] = perimeter(newstate, olddist)
newmetrics['perimeter'][newdist] = perimeter(newstate, newdist)
areachange = blockstats.ALAND[changenode] + blockstats.AWATER[changenode]
newmetrics['area'][olddist] -= areachange
newmetrics['area'][newdist] += areachange
newmetrics['bizarreness'][olddist] = bizarreness(newmetrics['area'][olddist], \
newmetrics['perimeter'][olddist])
newmetrics['bizarreness'][newdist] = bizarreness(newmetrics['area'][newdist], \
newmetrics['perimeter'][newdist])
return (newstate, proposedChanges, newmetrics)