Skip to content

Commit

Permalink
finish NH ensemble analysis example
Browse files Browse the repository at this point in the history
  • Loading branch information
formularin committed Feb 22, 2023
1 parent 095ee4b commit f2dc8b0
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 76 deletions.
252 changes: 190 additions & 62 deletions examples/ensemble_analysis.ipynb

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions rba/district_quantification.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def quantify_gerrymandering(state_graph, district_graphs, community_lifespan, ve
crossdistrict_edges[state_graph.nodes[edge[1]]["district"]].append((edge[1], edge[0]))
state_gerrymandering = 0
district_gerrymanderings = {}
num_crossedges = sum([len(edge_list) for edge_list in crossdistrict_edges.values()])
for district, district_graph in district_graphs.items():
district_gerrymandering = 0
# for edge in district_graph.edges():
Expand All @@ -82,12 +83,12 @@ def quantify_gerrymandering(state_graph, district_graphs, community_lifespan, ve
district_gerrymandering += (community_lifespan[crossedge])/total_crossedge_num
# district_gerrymandering -= (community_lifespan[crossedge])/2
# state_gerrymandering -= community_lifespan[crossedge]/2
state_gerrymandering += community_lifespan[crossedge]/(len(crossdistrict_edges)*2)
state_gerrymandering += community_lifespan[crossedge]/(num_crossedges)
except:
district_gerrymandering += (community_lifespan[(crossedge[1], crossedge[0])])/total_crossedge_num
# district_gerrymandering -= (community_lifespan[(crossedge[1], crossedge[0])])/2
# state_gerrymandering -= community_lifespan[(crossedge[1], crossedge[0])]/2
state_gerrymandering += community_lifespan[(crossedge[1], crossedge[0])]/(len(crossdistrict_edges)*2)
state_gerrymandering += community_lifespan[(crossedge[1], crossedge[0])]/(num_crossedges)
district_gerrymanderings[district] = district_gerrymandering
return district_gerrymanderings, state_gerrymandering

Expand Down
13 changes: 1 addition & 12 deletions rba/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,12 @@ def get_num_vra_districts(partition, label, threshold):
return num_vra_districts


def get_gerrymandering_score(partition, edge_lifetimes):
"""Returns the gerrymandering score of a partition.
"""
return quantify_gerrymandering(partition.graph, partition.subgraphs, edge_lifetimes)[1]


def get_district_gerrymandering_scores(partition, edge_lifetimes):
"""Returns the gerrymandering scores of the districts in a partition"""
return quantify_gerrymandering(partition.graph, partition.subgraphs, edge_lifetimes)[0]


def get_county_weighted_random_spanning_tree(graph):
"""Applies random edge weights to a graph, then multiplies those weights depending on whether or
not the edge crosses a county border. Then returns the maximum spanning tree for the graph."""
for u, v in graph.edges:
weight = random.random()
if graph[u]["COUNTYFP10"] == graph[v]["COUNTYFP10"]:
if graph.nodes[u]["COUNTYFP10"] == graph.nodes[v]["COUNTYFP10"]:
weight *= constants.SAME_COUNTY_PENALTY
graph[u][v]["random_weight"] = weight

Expand Down

0 comments on commit f2dc8b0

Please sign in to comment.