Skip to content

Commit

Permalink
implement ensemble analysis as script
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjam-es committed Feb 23, 2023
1 parent b9ef667 commit 36e74dd
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 99 deletions.
7 changes: 7 additions & 0 deletions rba/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
draw_parser.add_argument("--partition_file", type=str, default=None)
draw_parser.set_defaults(func=rba.visualization.visualize)

ensemble_parser = subparsers.add_parser("ensemble")
ensemble_parser.add_argument("--graph_file", type=str, default=os.path.join(package_dir, "data/2010/new_hampshire_geodata_merged.json"))
ensemble_parser.add_argument("--community_file", type=str, default=os.path.join(package_dir, "data/2010/new_hampshire_communities.json"))
ensemble_parser.add_argument("--vra_config_file", type=str, default=os.path.join(package_dir, "data/2010/vra_nh.json"))
ensemble_parser.add_argument("--district_file", type=str, default=os.path.join(package_dir, "data/2010/new_hampshire_districts.json"))
ensemble_parser.set_defaults(func=rba.ensemble.ensemble_analysis)

optimize_parser = subparsers.add_parser("optimize")
optimize_parser.add_argument("--graph_file", type=str, default=os.path.join(package_dir, "data/2010/new_hampshire_geodata_merged.json"))
optimize_parser.add_argument("--communitygen_out_file", type=str, default=os.path.join(package_dir, "data/2010/new_hampshire_communities.json"))
Expand Down
7 changes: 5 additions & 2 deletions rba/community_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ def compute_precinct_similarities(graph, verbose=False):
pop_density_distance = abs(pop1 - pop2)

similarity = 1 - np.average(
[race_distance, votes_distance, pop_density_distance],
weights=[SIMILARITY_WEIGHTS["race"], SIMILARITY_WEIGHTS["votes"], SIMILARITY_WEIGHTS["pop_density"]])
[race_distance, pop_density_distance],
weights=[SIMILARITY_WEIGHTS["race"], SIMILARITY_WEIGHTS["pop_density"]])
# similarity = 1 - np.average(
# [race_distance, votes_distance, pop_density_distance],
# weights=[SIMILARITY_WEIGHTS["race"], SIMILARITY_WEIGHTS["votes"], SIMILARITY_WEIGHTS["pop_density"]])
# print(similarity, race_distance, votes_distance, pop_density_distance)
graph.edges[node1, node2]["similarity"] = similarity
race_distances.append(race_distance)
Expand Down
3 changes: 2 additions & 1 deletion rba/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

# Edges between nodes in the same county should be weighted less than those that cross, because
# the maximum spanning tree should be made more likely to choose an edge crossing county lines.
SAME_COUNTY_PENALTY = 0.1
# SAME_COUNTY_PENALTY = 0.1
SAME_COUNTY_PENALTY = 10

POP_EQUALITY_THRESHOLD = 0.005

Expand Down
Loading

0 comments on commit 36e74dd

Please sign in to comment.