Commit 7bfe1aa5 authored by Jonathan Mattingly's avatar Jonathan Mattingly
Browse files

working now

parent 2dbe187a
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ 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:
@@ -46,9 +45,7 @@ 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
+18 −15
Original line number Diff line number Diff line
@@ -18,15 +18,15 @@ pctData = json.load(pctDataFile)
dataElection = pctData['nodes']
hf.addTotalVotes(dataElection)

#atlas = Atlas.openAtlas("./truncated_nc_multiscale.jsonl")
atlas = Atlas.openAtlas("./atlas_measureID12.jsonl.gz")
atlas = Atlas.openAtlas("./truncated_nc_multiscale.jsonl")
#atlas = Atlas.openAtlas("./atlas_measureID12.jsonl.gz")
print(atlas)
map = []
print("\n")

swingElections=[]
swings=[.46+i/200 for i in range(4*2+1)]
electons=["G20_USS","G20_LG","G20_GV","G20_PR","G16_PR"]
swings=[.46+i/200 for i in range(8*2+1)]
electons=["G20_USS","G20_PR","G16_PR","G08_AG"]#,"G20_LG","G20_GV","G20_PR","G16_PR"]
for election in electons:
    hf.addUniformSwings(swings,electons,dataElection)
    for s in swings:
@@ -49,26 +49,29 @@ while map is not None: # This loops through all of the map in the atlas
        node_to_dist = hf.get_node_to_district(map.districting, 
                                               pctData["nodes"])
        
        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
        numDemsPrecinct=hf.demWinByPrecicts(swingElections,node_to_dist,dataElection)

        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

        # id = 100
        # if (numDemsPrecinct[id] in pctDemWinnersHist[id].keys()):
        #     pctDemWinnersHist[id][numDemsPrecinct[id]]+=1
        #     print(id, "dem winner", numDemsPrecinct[id], pctDemWinnersHist[id][numDemsPrecinct[id]])
        # else:
        #     pctDemWinnersHist[id][numDemsPrecinct[id]]=1
        #     print(id, "dem winner", numDemsPrecinct[id], pctDemWinnersHist[id][numDemsPrecinct[id]])
        
        if (count % 50) == 0:
            print(count," ; ",map.name)
            print(f"{count:,}"," ; ",map.name)
        count+=1
        if count>500:
            break
        # if count>10000:
        #     break
    except Exception:
        break



#  i=1200;plt.bar(pctDemWinnersHist[i].keys(),pctDemWinnersHist[i].values());plt.show()
 No newline at end of file
+70 −1
Original line number Diff line number Diff line
import math

def get_node_to_district(districting, nodes):
    # This function reconstucts for a multiscale districting description
    # the mapping from precinct IDs to Districts for a given map 
@@ -63,3 +65,70 @@ def addTotalVotes(dataElection):
            for p in dataElection:
                p[e+"_T"]=p[e+"_D"]+p[e+"_R"]
        
import numpy as np

def demWinByPrecicts(elections,node_to_dist,dataElection):
    numDemsPrecinct=np.zeros(len(node_to_dist.keys()))
    for e in elections:
        distVoteR, distVoteD, distVoteT = sumElection(e, node_to_dist,
                                                            dataElection)
        for id in node_to_dist.keys():
            # id = 100
            # print(e, id, count, distVoteD[node_to_dist[id]], distVoteR[node_to_dist[id]])
            if distVoteD[node_to_dist[id]] >distVoteR[node_to_dist[id]]:
                numDemsPrecinct[id]+=1
    return numDemsPrecinct

def histMode(hist):
    return max(hist, key=hist.get)

def histNormalize(hist):
    total=sum(hist.values())
    histNorm={}
    for k in hist.keys():
        histNorm[k]=hist[k]/total
    return histNorm

def histMean(hist):
    mn=0.0
    total=sum(hist.values())
    for k in hist.keys():
       mn+=k*hist[k]
    mn=mn/total
    return mn

def histStd(hist,scale=1.0):
    mn=histMean(hist)
    std=0.0
    total=sum(hist.values())
    for k in hist.keys():
       std+=((k-mn)**2)*hist[k]
    std=std/total
    return math.sqrt(std)/scale

def histMedean(hist):
    totalMass=sum(hist.values())
    keys=(list(hist.keys()))
    keys.sort()
    mass=0.0
    lastMass=0.0
    kLast=0.0
    for k in keys:
        mass+=hist[k]
        if mass >= totalMass/2.0:
            break
        kLast=k
        lastMass=mass
    if kLast>0.0:
        d=k-kLast
        a=(totalMass/2.0-lastMass)/(mass-lastMass)
        return  (a*kLast + (1.0-a)*k)
    else:
        d=k
        a=(totalMass/2.0)/mass
        return  (1.0-a)*k

def histSpread(hist,scale=1.0):
    l=list(hist.keys())
    return (max(l)-min(l))/scale