Loading __pycache__/helper_functions.cpython-311.pyc +460 B (4.68 KiB) File changed.No diff preview for this file type. View original file View changed file example_atlas_multilevel_precinctHistogram.py +6 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,8 @@ pctToDistVotes = {} for node in pctData["nodes"]: pctToDistVotes[node["id"]] = {} count=0 while map is not None: # This loops through all of the map in the atlas try: map = Atlas.nextMap(atlas) # Get the next map in the atlas Loading @@ -44,6 +46,10 @@ while map is not None: # This loops through all of the map in the atlas votes["Total"] = distVoteT[node_to_dist[id]] pctToDistVotes[id][map.name] = votes count+=1 if count>500: break except Exception: break Loading example_atlas_multilevel_precinctOutcomes.py +46 −45 Original line number Diff line number Diff line import Atlas import helper_functions as hf import numpy as np import json from time import sleep from tqdm import tqdm import matplotlib.pyplot as plt import pandas as pd import seaborn as sns Loading @@ -8,66 +13,62 @@ import seaborn as sns pctDataF = open("pct21_20cen_wMCD.json") pctData = json.load(pctDataF) pctDataFile = open("pct21_20cen_wMCD_updated.json") pctData = json.load(pctDataFile) dataElection = pctData['nodes'] hf.addTotalVotes(dataElection) atlas = Atlas.openAtlas("./truncated_nc_multiscale.jsonl") #atlas = Atlas.openAtlas("./truncated_nc_multiscale.jsonl") atlas = Atlas.openAtlas("./atlas_measureID12.jsonl.gz") print(atlas) map = [] print("\n") electionName = "G16_USS" # This is the name of the eleection we will consider. pctToDistVotes = {} swingElections=[] swings=[.47+i/200 for i in range(13)] electons=["G20_USS","G20_LG","G20_GV","G20_PR","G16_PR"] for election in electons: hf.addUniformSwings(swings,electons,dataElection) for s in swings: swingElections.append(election+"_USF"+str(s)) pctDemWinnersHist = {} for node in pctData["nodes"]: pctToDistVotes[node["id"]] = {} pctDemWinnersHist[node["id"]] = {} count=0 while map is not None: # This loops through all of the map in the atlas try: map = Atlas.nextMap(atlas) # Get the next map in the atlas print(map.name) # The maps are multi scale in the sense that if a county is kept whole # the following fuction makes a map from precicts to districts out of # the multiscale assignement node_to_dist = hf.get_node_to_district(map.districting, pctData["nodes"]) # The next fuction sums up the election for this districting (defined # by the map) distVoteR, distVoteD, distVoteT = hf.sumElection(electionName, node_to_dist, dataElection) for id in pctToDistVotes.keys(): votes = {} votes["Dem"] = distVoteD[node_to_dist[id]] votes["Rep"] = distVoteD[node_to_dist[id]] votes["Total"] = distVoteT[node_to_dist[id]] pctToDistVotes[id][map.name] = votes except Exception: break # This secton adds the uniform swings by the amounts listed in swings for the lections # listed in ellections to the dataEllections swings=[.47+i/200 for i in range(13)] electons=["G16_AG","G12_PR","G08_USS"] hf.addUniformSwings(swings,electons,dataElection) numDemsPrecinct=np.zeros(len(node_to_dist.keys())) for e in swingElections: distVoteR, distVoteD, distVoteT = hf.sumElection(e, node_to_dist, dataElection) for id in node_to_dist.keys(): if distVoteD[node_to_dist[id]] >distVoteR[node_to_dist[id]]: numDemsPrecinct[id]+=1 # now do somthing with data id = 100 # Choose precinct print(dataElection[id]["county"], dataElection[id]["prec_id"]) for id in node_to_dist.keys(): if (numDemsPrecinct[id] in pctDemWinnersHist[id].keys()): pctDemWinnersHist[id][numDemsPrecinct[id]]+=1 else: pctDemWinnersHist[id][numDemsPrecinct[id]]=1 # or county = "BEAUFORT" prec_id = "BLCK" id = [ii for ii in range(len(dataElection)) if dataElection[ii]["county"] == county and dataElection[ii]["prec_id"] == prec_id][0] print(id) df = pd.DataFrame(pctToDistVotes[id]).T df["Dem %"] = df["Dem"]/df["Total"] print(df) if (count % 50) == 0: print(count," ; ",map.name) count+=1 if count>500: break except Exception: break sns.displot(data=df, x="Dem %", bins=10, stat="density") plt.show() helper_functions.py +9 −1 Original line number Diff line number Diff line Loading @@ -54,4 +54,12 @@ def addUniformSwings(targetDemFractions,electionNames,dataElection): def listElections(dataElection, prefix="G", exluded={'id','prec_id','pop2020cen','MCD','area','border_length'},idx=0): return [ e[0:-2] for e in dataElection[idx].keys() if e not in {'id','prec_id','pop2020cen','MCD','area','border_length'} and e[-1] == 'T'] return [ e[0:-2] for e in dataElection[idx].keys() if e not in {'id','prec_id','pop2020cen','MCD','area','border_length'} and e[-1] == 'D'] def addTotalVotes(dataElection): elections=listElections(dataElection) for e in elections: if (e+"_T" not in dataElection[0].keys()): for p in dataElection: p[e+"_T"]=p[e+"_D"]+p[e+"_R"] No newline at end of file Loading
__pycache__/helper_functions.cpython-311.pyc +460 B (4.68 KiB) File changed.No diff preview for this file type. View original file View changed file
example_atlas_multilevel_precinctHistogram.py +6 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,8 @@ pctToDistVotes = {} for node in pctData["nodes"]: pctToDistVotes[node["id"]] = {} count=0 while map is not None: # This loops through all of the map in the atlas try: map = Atlas.nextMap(atlas) # Get the next map in the atlas Loading @@ -44,6 +46,10 @@ while map is not None: # This loops through all of the map in the atlas votes["Total"] = distVoteT[node_to_dist[id]] pctToDistVotes[id][map.name] = votes count+=1 if count>500: break except Exception: break Loading
example_atlas_multilevel_precinctOutcomes.py +46 −45 Original line number Diff line number Diff line import Atlas import helper_functions as hf import numpy as np import json from time import sleep from tqdm import tqdm import matplotlib.pyplot as plt import pandas as pd import seaborn as sns Loading @@ -8,66 +13,62 @@ import seaborn as sns pctDataF = open("pct21_20cen_wMCD.json") pctData = json.load(pctDataF) pctDataFile = open("pct21_20cen_wMCD_updated.json") pctData = json.load(pctDataFile) dataElection = pctData['nodes'] hf.addTotalVotes(dataElection) atlas = Atlas.openAtlas("./truncated_nc_multiscale.jsonl") #atlas = Atlas.openAtlas("./truncated_nc_multiscale.jsonl") atlas = Atlas.openAtlas("./atlas_measureID12.jsonl.gz") print(atlas) map = [] print("\n") electionName = "G16_USS" # This is the name of the eleection we will consider. pctToDistVotes = {} swingElections=[] swings=[.47+i/200 for i in range(13)] electons=["G20_USS","G20_LG","G20_GV","G20_PR","G16_PR"] for election in electons: hf.addUniformSwings(swings,electons,dataElection) for s in swings: swingElections.append(election+"_USF"+str(s)) pctDemWinnersHist = {} for node in pctData["nodes"]: pctToDistVotes[node["id"]] = {} pctDemWinnersHist[node["id"]] = {} count=0 while map is not None: # This loops through all of the map in the atlas try: map = Atlas.nextMap(atlas) # Get the next map in the atlas print(map.name) # The maps are multi scale in the sense that if a county is kept whole # the following fuction makes a map from precicts to districts out of # the multiscale assignement node_to_dist = hf.get_node_to_district(map.districting, pctData["nodes"]) # The next fuction sums up the election for this districting (defined # by the map) distVoteR, distVoteD, distVoteT = hf.sumElection(electionName, node_to_dist, dataElection) for id in pctToDistVotes.keys(): votes = {} votes["Dem"] = distVoteD[node_to_dist[id]] votes["Rep"] = distVoteD[node_to_dist[id]] votes["Total"] = distVoteT[node_to_dist[id]] pctToDistVotes[id][map.name] = votes except Exception: break # This secton adds the uniform swings by the amounts listed in swings for the lections # listed in ellections to the dataEllections swings=[.47+i/200 for i in range(13)] electons=["G16_AG","G12_PR","G08_USS"] hf.addUniformSwings(swings,electons,dataElection) numDemsPrecinct=np.zeros(len(node_to_dist.keys())) for e in swingElections: distVoteR, distVoteD, distVoteT = hf.sumElection(e, node_to_dist, dataElection) for id in node_to_dist.keys(): if distVoteD[node_to_dist[id]] >distVoteR[node_to_dist[id]]: numDemsPrecinct[id]+=1 # now do somthing with data id = 100 # Choose precinct print(dataElection[id]["county"], dataElection[id]["prec_id"]) for id in node_to_dist.keys(): if (numDemsPrecinct[id] in pctDemWinnersHist[id].keys()): pctDemWinnersHist[id][numDemsPrecinct[id]]+=1 else: pctDemWinnersHist[id][numDemsPrecinct[id]]=1 # or county = "BEAUFORT" prec_id = "BLCK" id = [ii for ii in range(len(dataElection)) if dataElection[ii]["county"] == county and dataElection[ii]["prec_id"] == prec_id][0] print(id) df = pd.DataFrame(pctToDistVotes[id]).T df["Dem %"] = df["Dem"]/df["Total"] print(df) if (count % 50) == 0: print(count," ; ",map.name) count+=1 if count>500: break except Exception: break sns.displot(data=df, x="Dem %", bins=10, stat="density") plt.show()
helper_functions.py +9 −1 Original line number Diff line number Diff line Loading @@ -54,4 +54,12 @@ def addUniformSwings(targetDemFractions,electionNames,dataElection): def listElections(dataElection, prefix="G", exluded={'id','prec_id','pop2020cen','MCD','area','border_length'},idx=0): return [ e[0:-2] for e in dataElection[idx].keys() if e not in {'id','prec_id','pop2020cen','MCD','area','border_length'} and e[-1] == 'T'] return [ e[0:-2] for e in dataElection[idx].keys() if e not in {'id','prec_id','pop2020cen','MCD','area','border_length'} and e[-1] == 'D'] def addTotalVotes(dataElection): elections=listElections(dataElection) for e in elections: if (e+"_T" not in dataElection[0].keys()): for p in dataElection: p[e+"_T"]=p[e+"_D"]+p[e+"_R"] No newline at end of file