def cleanAndDumpCountyData():
	FilePath = "DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_ann.csv"
	with open(FilePath) as file:
		reader = csv.reader(file)

		outputPath = "DEC_10_DP_DPDP1/clean_county_census_data.csv"
		with open(outputPath, mode = 'w') as output:
			writer = csv.writer(output, lineterminator="\n")
			row = next(reader, None)
			writer.writerow(constructWriteRow(row) + ["DemSplit", "RepSplit"])
			row = next(reader, None)
			writer.writerow(constructWriteRow(row) + ["DemSplit", "RepSplit"])

			state_codes = reverseStateCodes()

			currentStateID = None
			for row in reader:
				stateID = state_codes['{:02d}'.format(int(math.floor(int(row[1])/1000.0)))]
				if stateID != 'AK':
					countyID = int(row[1]) % 1000
					countyName = row[2]
					countyObject = County(countyID, countyName)
					if currentStateID == None or currentStateID != stateID:
						currentStateResults = getCountyVotes(stateID, "2016")
					#if countyObject not in currentStateResults.keys():
					#	print(countyObject)
					countyResults = currentStateResults.get(countyObject, VoteCount())
					writer.writerow(constructWriteRow(row) + [countyResults.getDemSplit(), countyResults.getRepSplit()])
Exemple #2
0
def appendElectionResultsToCountyData(electionDate="2016"):
    CountyCensusDataFilePath = "CensusDemographicProfileData2010/CountyLevel/DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_ann.csv"
    with open(CountyCensusDataFilePath) as file:
        reader = csv.reader(file)

        outputPath = "CensusDemographicProfileData2010/CountyLevel/DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_election_results.csv"
        with open(outputPath, mode='w') as output:
            writer = csv.writer(output, lineterminator="\n")
            row = next(reader, None)
            writer.writerow(row + ["DemVoteFraction", "RepVoteFraction"])
            row = next(reader, None)
            writer.writerow(row + ["DemVoteFraction", "RepVoteFraction"])

            currentStateID = None
            for row in reader:
                stateID = convertStateCodes('{:02d}'.format(
                    int(math.floor(int(row[1]) / 1000.0))))
                if stateID != 'AK':
                    countyID = int(row[1]) % 1000
                    countyName = row[2]
                    countyObject = County(countyID, countyName)
                    if currentStateID == None or currentStateID != stateID:
                        currentStateResults = getCountyVotes(
                            stateID, electionDate)
                    #if countyObject not in currentStateResults.keys():
                    #	print(countyObject)
                    countyResults = currentStateResults.get(
                        countyObject, VoteCount())
                    row[3] = int(row[3].split('(')[0])
                    writer.writerow(row + [
                        countyResults.getDemSplit(),
                        countyResults.getRepSplit()
                    ])
def get_counties(state, maptype):
    counties = dict()
    districtToCounty = {}

    #FilePath = "../QGIS/county_district_percentages.csv"
    FilePath = "../CensusData/CensusDemographicProfileData2010/advanced_county_splits_house_2016.csv"
    #FilePath = "../CensusData/CensusDemographicProfileData2010/advanced_county_splits.csv"
    #FilePath = "../CensusData/CensusDemographicProfileData2010/advanced_county_splits_distances_2016_house.csv"
    #FilePath = "../CensusData/CensusDemographicProfileData2010/advanced_county_splits_distances.csv"

    with open(FilePath) as file:
        reader = csv.reader(file)
        next(reader, None)
        for row in reader:
            if (state == row[1] and maptype == row[2]):
                countyID = int(row[3]) % 1000
                countyname = row[4]  #.lower().replace(" county", "")
                district = int(row[5])
                demSplit = float(row[6])
                repSplit = float(row[6])
                countyObject = County(countyID, countyname, demSplit, repSplit)
                if (district in districtToCounty):
                    districts_counties = districtToCounty[district]
                    districts_counties.append(countyObject)
                    districtToCounty[district] = districts_counties
                else:
                    districtToCounty.update({district: [countyObject]})
    return districtToCounty
def getHouseCountyVotes(state, electionDate):
    countyVotes = {}
    House2016ElectionResultsFile = "../ElectionData/NYTHouseElectionData/" + electionDate + "HouseResultsAggregated.csv"
    with open(House2016ElectionResultsFile) as file:
        reader = csv.reader(file)
        for row in reader:
            if row[0] == state:
                countyID = int(row[1]) % 1000
                countyName = row[2]
                countyObject = County(countyID, countyName)
                countyVoteCount = VoteCount(int(row[3]), int(row[4]))
                countyVotes.update({countyObject: countyVoteCount})
    return countyVotes
def constructExistingSet(state, year="2016", countyMap = None):
	countyVotes = {}
	ElectionResultsFile = "CQElectionData/President_CountyDetail_"+year+".csv"
	with open(ElectionResultsFile) as file:
		reader = csv.reader(file)
		for row in reader:
			if len(row) > 1 and row[1] == state:
				#if row[29] != '':
				if row[0] == "President":
					state = row[1]
					countyName = row[4]
					if (state, countyName) in countyMap.keys():
						#countyID = int(row[29])
						countyID = countyMap.get((state, countyName))[0]
						countyObject = County(countyID, countyName)
						if row[10] != 'N/A':
							countyVotes.update({countyObject: VoteCount(int(row[10].replace(',', '')), int(row[7].replace(',', '')))})

	return countyVotes
def testaksdbalkbsgalksdbflakjdsbf():
	#existingStuff = constructExistingSet()

	#print(existingStuff)

	CountyCensusDataFilePath = "CensusDemographicProfileData2010/CountyLevel/DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_ann.csv"
	with open(CountyCensusDataFilePath) as file:
		reader = csv.reader(file)

		state_codes = reverseStateCodes()

		outputPath = "CensusDemographicProfileData2010/CountyLevel/DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_election_results.csv"
		with open(outputPath, mode = 'w') as output:
			writer = csv.writer(output, lineterminator="\n")
			row = next(reader, None)
			writer.writerow(row + ["DemVoteFraction", "RepVoteFraction"])
			row = next(reader, None)
			writer.writerow(row)

			currentStateID = None
			for row in reader:
				stateID = state_codes['{:02d}'.format(int(math.floor(int(row[1])/1000.0)))]
				if stateID != 'AK' and stateID != 'DC':
					stateName = state_names[stateID]
					countyName = row[2].split(" County")[0].upper()
					countyID = int(row[1]) % 1000

					if currentStateID == None or currentStateID != stateID:
						currentStateResults = constructExistingSet(stateName)
						currentStateID = stateID
					#if County(countyID, countyName) not in existingStuff.keys():
					#print(countyName)
					countyObject = County(countyID, countyName)
					countyResults = currentStateResults.get(countyObject, VoteCount())
					newrow = []
					for cell in row:
						newcell = cell.split('(')[0]
						if newcell == ' ':
							newcell = '0'
						newrow.append(newcell)

					writer.writerow(newrow + [countyResults.getDemSplit(), countyResults.getRepSplit()])
def getCQPresidentialCountyVotes(state, electionDate):
    state = state_names[state]

    countyVotes = {}
    ElectionResultsFile = "CQElectionData/President_CountyDetail_2016.csv"
    with open(ElectionResultsFile) as file:
        reader = csv.reader(file)
        for row in reader:
            if row[1] == state:
                if row[29] != '':
                    countyID = int(row[29])
                    countyName = row[4]
                    countyObject = County(countyID, countyName)
                    if row[10] != 'N/A':
                        countyVotes.update({
                            countyObject:
                            VoteCount(int(row[10].replace(',', '')),
                                      int(row[7].replace(',', '')))
                        })
    return countyVotes
def getPresidentialCountyVotes(state, electionDate):
    countyVotes = {}

    FilePath = "../ElectionData/US_County_Level_Election_Results_08-16/US_County_Level_Presidential_Results_08-16.csv"
    with open(FilePath) as file:
        reader = csv.reader(file)
        next(reader, None)
        for row in reader:
            if math.floor(int(row[0]) / 1000.0) == int(state_codes[state]):
                countyID = int(row[0]) % 1000
                countyName = row[1]  #.lower().replace(" county", "")
                countyObject = County(countyID, countyName)
                if electionDate == "2008":
                    countyVoteCount = VoteCount(int(row[3]), int(row[4]))
                if electionDate == "2012":
                    countyVoteCount = VoteCount(int(row[7]), int(row[8]))
                if electionDate == "2016":
                    countyVoteCount = VoteCount(int(row[11]), int(row[12]))
                countyVotes.update({countyObject: countyVoteCount})

    return countyVotes
def checkCountyLinks():
	FilePath2016 = "CQElectionData/President_CountyDetail_2016.csv"
	BaseFilePath = "CQElectionData/President_CountyDetail_"
	Years = ["2012", "2008", "2004", "2000"]

	CountyMap = {}
	with open(FilePath2016) as file:
		reader = csv.reader(file)
		for row in reader:
			if row[0] == "President" and row[29] != '':
				state = row[1]
				county = row[4]
				countyID = int(row[29])
				countyName = row[30]
				CountyMap.update({(state, county): (countyID, countyName)})
	#print(CountyMap)

	#for year in Years:
	#	with open(BaseFilePath+year+".csv") as file:
	#		reader = csv.reader(file)
	#		for row in reader:
	#			if row[0] == "President":
	#				state = row[1]
	#				county = row[4]
	#				if (state, county) not in CountyMap.keys():
	#					print(year + str((state, county)))


	state_codes = reverseStateCodes()
	outputPath = "CensusDemographicProfileData2010/CountyLevel/DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_election_results.csv"
	with open(outputPath, mode = 'w') as output:
		writer = csv.writer(output, lineterminator="\n")

		CountyCensusDataFilePath = "CensusDemographicProfileData2010/CountyLevel/DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_ann.csv"
		with open(CountyCensusDataFilePath) as file:
			reader = csv.reader(file)
			row = next(reader, None)
			writer.writerow(row + ["DemVoteFraction", "RepVoteFraction"])
			row = next(reader, None)
			writer.writerow(row)

		for year in ["2016", "2012", "2008", "2004", "2000"]:
			print(year)
			CountyCensusDataFilePath = "CensusDemographicProfileData2010/CountyLevel/DEC_10_DP_DPDP1/DEC_10_DP_DPDP1_with_ann.csv"
			with open(CountyCensusDataFilePath) as file:
				reader = csv.reader(file)
				next(reader, None)
				next(reader, None)

				currentStateID = None
				for row in reader:
					stateID = state_codes['{:02d}'.format(int(math.floor(int(row[1])/1000.0)))]
					if stateID != 'AK' and stateID != 'DC':
						stateName = state_names[stateID]
						countyName = row[2].split(" County")[0].upper()
						countyID = int(row[1]) % 1000

						if currentStateID == None or currentStateID != stateID:
							currentStateResults = constructExistingSet(stateName, year, CountyMap)
							currentStateID = stateID
						#if County(countyID, countyName) not in existingStuff.keys():
						#print(countyName)
						countyObject = County(countyID, countyName)
						if countyObject not in currentStateResults.keys():
							print(countyObject)
						countyResults = currentStateResults.get(countyObject, VoteCount())
						newrow = []
						for cell in row:
							newcell = cell.split('(')[0]
							if newcell == ' ':
								newcell = '0'
							newrow.append(newcell)

						writer.writerow(newrow + [countyResults.getDemSplit(), countyResults.getRepSplit()])