def save_graph(place, filename): print("Creating rook graph") rook = Graph.from_file(filename, adjacency="rook", columns=[col.key for col in place.columns]) rook.to_json("./graphs/{}_rook.json".format(place.id)) print("Creating queen graph") queen = Graph.from_file(filename, adjacency="queen", columns=[col.key for col in place.columns]) queen.to_json("./graphs/{}_queen.json".format(place.id))
def load_graph_from_shp(file_path, write_path, id_key, adjacency): global graph print("Loading graph...") start = time.perf_counter() graph = Graph.from_file(file_path, adjacency=adjacency) end = time.perf_counter() print("Took " + str((end - start)) + " seconds to create the graph.") print("This state has " + str(len(graph.nodes)) + " blocks") # write matrix adj_mat = np.zeros((len(graph.nodes), len(graph.nodes))) if id_key not in graph.nodes[0].keys(): print(id_key, "not found in node attributes.") print("Attributes: ", graph.nodes[0].keys()) ids = [graph.nodes[i][id_key] for i in range(len(graph.nodes))] for e in graph.edges: adj_mat[e[0], e[1]] = 1 adj_mat[e[1], e[0]] = 1 print("Writing adjacency matrix and IDs to file...") with open(write_path, 'w') as outfile: json.dump({"order": ids, "adj": adj_mat.tolist()}, outfile) print("Done! JSON written to " + write_path)
total += 1 return total ## ## ## ## ## ## ## ## ## ## ## ## creating an initial partition ## ## ## ## ## ## ## ## ## ## ## print("Reading in Data/Graph") dat_path = "/Users/hopecj/projects/gerryspam/MO/dat/final_prec/prec_labeled.shp" dat = gpd.read_file(dat_path) list(dat.columns) dat['SLDUST'].nunique() dat['SLDLST'].nunique() graph = Graph.from_file(dat_path) mo_updaters = {"population" : Tally(POP_COL, alias="population"), "cut_edges": cut_edges, "county_splits": county_splits("county_splits", "COUNTYFP"), "num_vra_districts": num_vra_districts, "polsby_popper": polsby_popper} elections = [Election("USSEN16", {"Dem": "G16USSDKAN", "Rep": "G16USSRBLU"}), Election("PRES16", {"Dem": "G16PREDCLI", "Rep": "G16PRERTRU"})] election_updaters = {election.name: election for election in elections} mo_updaters.update(election_updaters) ## ## ## ## ## ## ## ## ## ## ## ## Initial partition
def generate_graph(path): return Graph.from_file(path)
import pickle from gerrychain import Graph # graph = Graph.from_file("data/GA_blockgroups_2018/GA_blockgroups_all_pops.shp") # with open("graphs/GA_blockgroup_graph.p", "wb") as fout: # pickle.dump(graph, fout) graph = Graph.from_file("data/GA_precincts_all_pops/GA_precincts_2016.shp") with open("graphs/GA_precincts_2016_graph.p", "wb") as fout: pickle.dump(graph, fout) # graph = Graph.from_file("data/GA_precincts_all_pops/GA_precincts_2018.shp") # with open("graphs/GA_precincts_2018_graph.p", "wb") as fout: # pickle.dump(graph, fout)
"population": updaters.Tally("POPULATION", alias="population"), "cut_edges": cut_edges, } election_updaters = {election.name: election for election in elections} my_updaters.update(election_updaters) # Construct Dual Graphs # Tightest Alaska print("Building Tight Graph") G_tight = Graph.from_file("./AK_precincts_ns/AK_precincts_ns/AK_precincts_ns.shp") G_tight.join(df, columns=["nAMIN"]) idict = {} for index, row in df.iterrows(): idict[int(row["ID"])] = index # Connect Islands to_add = [ (426, 444), (437, 438), (437, 442), (411, 420), (411, 414),
BK = gpd.read_file(shp_path + 'VA_precincts.shp') BK.columns BK['pct'] = BK['G16RPRS'].astype(float).div( np.sum(BK[['G16DPRS', 'G16RPRS']].astype(float), axis=1)) BK.plot(column='pct', cmap='seismic', alpha=0.8) plt.savefig("VA_PR16_gen_election.png", bbox_inches='tight', dpi=600) shp_path1 = 'directory for precincts of Virginia/VA_precincts/' va_precincts = gpd.read_file(shp_path1 + 'VA_precincts.shp') #va_precincts['G16DPRS'] = va_precincts['G16DPRS'].astype(float) #va_precincts['G16RPRS'] = va_precincts['G16RPRS'].astype(float) #centroids = va_precincts.centroid #va_precincts["C_X"] = centroids.x #va_precincts["C_Y"] = centroids.y #pos = {index:(row['C_X'],row['C_Y']) for index,row in va_precincts.iterrows()} graph = Graph.from_file(shp_path1 + 'VA_precincts.shp') #change strings to floats graph.to_json(shp_path1 + 'VA_precincts.json') for node in graph.nodes(): graph.nodes[node]['G16DPRS'] = float(graph.nodes[node]['G16DPRS']) graph.nodes[node]['G16RPRS'] = float(graph.nodes[node]['G16RPRS']) graph.nodes[node]['G18DSEN'] = float(graph.nodes[node]['G18DSEN']) graph.nodes[node]['G18RSEN'] = float(graph.nodes[node]['G18RSEN']) #Create dataframe of the data on each node. g_df = pd.DataFrame([graph.nodes[node] for node in graph.nodes()]) cd_verts = [] #Set list of colors. colors = [ 'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan', 'darkblue',
locality_splits_dict, num_parts, coincident_boundaries, vtds_per_district, shannon_entropy, power_entropy, invert_dict, num_split_localities, pieces_allowed, pennsylvania_fouls, vtds_to_localities, dictionary_to_score, symmetric_entropy, num_pieces) outdir = "./pa_score_outputs/" try: # Create target Directory os.mkdir(outdir) print("Directory ", outdir, " created ") except FileExistsError: print("Directory ", outdir, " already exists") print() #load the dual graph graph = Graph.from_file("./notebooks/PA_VTD/PA_VTD.shp") gdf = gpd.read_file("./notebooks/PA_VTD/PA_VTD.shp") #elections and updaters pop_col = "TOT_POP" election_names = [ "PRES12", "PRES16", "SENW101216", ] election_columns = [ ["PRES12D", "PRES12R"], ["T16PRESD", "T16PRESR"], ["W101216D", "W101216R"], ] updaters1 = {
num_plans (int): number of desired diverse plans to be returned """ # Gets distribution of plans from chain, and sort in descending order according # to the determined election statistic distribution = getDistribution(chain, electionName, electionStatistic, party, constraintFunction) distribution.sort(reverse=True, key=lambda x: x[0]) # The gerrymandered plans for the given party from the distribution gerryPlans = [ planPair[1] for planPair in distribution[:int(len(distribution) * 100)] ] return diversePlans(gerryPlans, num_plans) if __name__ == "__main__": graph = Graph.from_file("./Data/Wisconsin/WI_ltsb_corrected_final.shp") islands = graph.islands components = list(connected_components(graph)) df = gpd.read_file("./Data/Wisconsin/WI_ltsb_corrected_final.shp") df.to_crs({"init": "epsg:26986"}, inplace=True) biggest_component_size = max(len(c) for c in components) problem_components = [ c for c in components if len(c) != biggest_component_size ] problem_nodes = [ node for component in problem_components for node in component ] problem_geoids = [graph.nodes[node]["GEOID10"] for node in problem_nodes]
state_abbr = "MS" housen = "HOU" state_fip = "28" num_districts = 4 newdir = "./Outputs/"+state_abbr+housen+"_BG/" os.makedirs(os.path.dirname(newdir + "init.txt"), exist_ok=True) with open(newdir + "init.txt", "w") as f: f.write("Created Folder") tract_graph = Graph.from_file("./BG"+state_fip+".shp",reproject=False) tract_graph.to_json("./"+state_abbr+"_BG.json") print("built graph") graph_path = "./"+state_abbr+"_BG.json" plot_path = "./BG"+state_fip+".shp" df = gpd.read_file(plot_path) unique_label = "GEOID10" pop_col = "TOTPOP"
jsonFileSuffix = "PA_VTD.json" #### Import Data #### The Graph.from_file() classmethod creates a Graph of the precincts in our #### shapefile. By default, this method copies all of the data columns from the #### shapefile’s attribute table to the graph object as node attributes. #### The contents of this particular shapefile’s attribute table are summarized #### in the mggg-states/PA-shapefiles GitHub repo. #### Depending on the size of the state, the process of generating an adjacency #### graph can take a bit of time. To avoid having to repeat this process in #### the future, we call graph.to_json() to save the graph in the NetworkX #### json_graph format under the name "PA_VTD.json. graph = Graph.from_file(pennDataPathPrefix + shpFileSuffix) graph.to_json(pennDataPathPrefix + jsonFileSuffix) ## Simple Example #### In order to run a Markov chain, we need an adjacency Graph of our VTD #### geometries and Partition of our adjacency graph into districts. #### This Partition will be the initial state of our Markov chain. #### We configure an Election object representing the 2012 Senate #### election, using the USS12D and USS12R vote total columns from our #### shapefile. The first argument is a name for the election ("SEN12"), and #### the second argument is a dictionary matching political parties to their #### vote total columns in our shapefile. This will let us compute hypothetical #### election results for each districting plan in the ensemble.
#imports from gerrychain import Graph, Partition, Election from gerrychain.updaters import Tally, cut_edges import networkx as nx import geopandas as gpd import pandas as pd import matplotlib.pyplot as plt ##2016 Census Tract- TigerLine File for Georgia, follow link to download geo = gpd.read_file("https://www2.census.gov/geo/tiger/TIGER2010/BG/2010/tl_2010_13_bg10/tl_2010_13_bg10.shp") graph = Graph.from_file("""YOUR FILE PATH""/tl_2010_13_bg10/tl_2010_13_bg10.shp") #if graph is successfully generated, we should be able to run chain nx.is_connected(graph) # if returns true, graph is connected
import geopandas as gpd import pickle from gerrychain import Graph, Partition graph = Graph.from_file("data/OR_blocks/OR_blocks.shp") with open("data/OR_blocks/OR_block_graph.p", "wb") as f_out: pickle.dump(graph, f_out)
for x in sys.argv: file.write("{} \n".format(x)) file.close() #State and district level parameters -- change these for a new state num_districts = int(sys.argv[4]) election_names = [sys.argv[5]] election_columns = [ [sys.argv[6], sys.argv[7]], ] #DEM, REP pop_tol = 0.02 pop_col = sys.argv[8] pre_file_shape = sys.argv[9] if pre_file_shape.endswith(".shp"): graph = Graph.from_file(pre_file_shape) else: graph = Graph.from_json(pre_file_shape) #Set up and start the chain print("Shapefile loaded, graph created and islands fixed...") print("Connected components: {}, graph size: {}".format(nx.number_connected_components(graph), len(graph))) print("Sample node from graph:\n", graph.nodes[np.random.choice(graph.nodes)]) total_population = sum([graph.nodes[n][pop_col] for n in graph.nodes()]) print("Shapefiles loaded and ready to run ReCom...") for x in graph.nodes: #fix NaN for index, e in enumerate(election_names): for k in election_columns[index]: if isinstance(graph.nodes[x][k], str): graph.nodes[x][k] = float(graph.nodes[x][k].replace(',',''))
count_splits = 0 for name, data in dictionary.items(): if data[0].value != 0: count_splits = count_splits + 1 return count_splits def subset_list(percentage_list, lower_threshold, upper_threshold): percentage_intermediate = percentage_list[ percentage_list >= lower_threshold] return len( percentage_intermediate[percentage_intermediate < upper_threshold]) # ---- Load in graph ------\ graph = Graph.from_file(path_to_shapefile) #graph = Graph.from_json(path_to_shapefile) #graph = gpd.read_file(path_to_shapefile) # ---- Updaters & Elections ----- updaters = { "population": updaters.Tally(pop_col, alias="population"), "cut_edges": cut_edges, "BVAP": updaters.Tally(BVAP_col, alias="BVAP"), "HVAP": updaters.Tally(HVAP_col, alias="HVAP"), "AVAP": updaters.Tally(AVAP_col, alias="AVAP"), "NVAP": updaters.Tally(Native_col, alias="NVAP"), "PVAP": updaters.Tally(Pacific_col, alias="PVAP"), "VAP": updaters.Tally(VAP_col, alias="VAP"), "county_splits": county_splits("county_splits", county_assignment_col)
INTERVAL = int(sys.argv[2]) #sampling interval outputfolder = sys.argv[3] #outputfolder num_districts = int(sys.argv[4]) city = sys.argv[5] os.makedirs(outputfolder, exist_ok=True) print("Arguments: ", sys.argv[1:]) ''' State and district level parameters ''' pop_tol = 0.02 pop_col = "TOTPOP" if city == "Chicago": print("Loading Chicago...") graph = Graph.from_file("Chicago_Precincts/Chicago_Precincts.shp") elif city == "Phillyblocks": print("Loading Philly blocks...") graph = Graph.from_file("Philly_blocks/Philly_blocks_with_dems.shp") else: print("Loading Philly..") graph = Graph.from_file("Philly_Divisions/Philly_divisions_with_demos.shp") total_population = sum([graph.nodes[n][pop_col] for n in graph.nodes()]) print("Shapefiles loaded and ready to run ReCom...") ''' Run the chain ''' for k in [num_districts]:
test_05["polsbypopper"] plt.hist(test_05["eg"], bins=50) # load actual gerrychain results sen_05 = np.load( "/Users/hopecj/projects/gerryspam/MO/res_0817/MO_state_senate_300000_0.05.p" ) sen_01 = np.load( "/Users/hopecj/projects/gerryspam/MO/res_0817/MO_state_senate_100000_0.01.p" ) sen_05.keys() # shows "columns" available print(sen_05["mean_median_ussen16"]) # results from chain # open enacted map graph = Graph.from_file( "/Users/hopecj/projects/gerryspam/MO/dat/final_prec/prec_labeled.shp") elections = [ Election("USSEN16", { "Dem": "G16USSDKAN", "Rep": "G16USSRBLU" }), Election("PRES16", { "Dem": "G16PREDCLI", "Rep": "G16PRERTRU" }) ] mo_updaters = { "population": Tally("POP10", alias="population"), "cut_edges": cut_edges }
dictionary_to_score, symmetric_entropy, num_pieces ) #make new directory outdir = "./nc_score_outputs/" try: os.mkdir(outdir) print("Directory " , outdir , " created ") except FileExistsError: print("Directory " , outdir , " already exists") print() #load the dual graph graph = Graph.from_file("./notebooks/NC_VTD/NC_VTD.shp", ignore_errors=True) gdf = gpd.read_file("./notebooks/NC_VTD/NC_VTD.shp") pop_col = "TOTPOP" ccol = "County" #updaters updaters1 = { "population": updaters.Tally("TOTPOP", alias="population"), "cut_edges": cut_edges, } #real partitions judge_plan = Partition(graph, "judge", updaters1) old_plan = Partition(graph, "oldplan", updaters1) new_plan = Partition(graph, "newplan", updaters1)