def validate_adjusted_votes(adjusted_votes, valid_votes): sum_votes = 0 for party in adjusted_votes: if adjusted_votes[party] is None: continue sum_votes += float(adjusted_votes[party]) if not isclose(sum_votes, int(valid_votes)): raise Exception("New vote shares are not normalised for constituency. Error!")
def validate_constituency_power_dict(constituency_power_dict): sum_powers = 0 for constituency in constituency_power_dict: sum_powers += constituency_power_dict[constituency] if not isclose(sum_powers, 1): raise Exception("Bad averaging. Constutuency power dict not verified. Average power found to be " + str(sum_powers / len(constituency_power_dict)) + " but it should be 1") logging.info("Constituency power dictionary validated.")
def validate_adjusted_vote_shares(adjusted_vote_shares): for constituency in adjusted_vote_shares: sum_constituency_shares = 0 for party in adjusted_vote_shares[constituency]: if adjusted_vote_shares[constituency][party] is None: continue sum_constituency_shares += adjusted_vote_shares[constituency][party] if not isclose(sum_constituency_shares, 1): raise Exception("New vote shares are not normalised for constituency: " + constituency + ". Error!")
def validate_party_power_dict(constituency_party_power_dict): sum_constituencies = 0 party_count_dict = dict.fromkeys(party for party in Party) for constituency in constituency_party_power_dict: sum_constituencies += 1 for party in constituency_party_power_dict[constituency]: if party_count_dict[party] is None: party_count_dict[party] = constituency_party_power_dict[constituency][party] else: party_count_dict[party] += constituency_party_power_dict[constituency][party] for party in party_count_dict: if party == Party.OTHER or party == Party.UNKNOWN or party == Party.DK: continue if not isclose((party_count_dict[party] / sum_constituencies), 1): if party == Party.CON or party == party.LAB or party == Party.LD: #JOSH remove me! raise Exception("Bad averaging. Party power dict not verified. Party power for party " + party.name + " is " + str(party_count_dict[party] / sum_constituencies) + " but it should be 1") logging.info("Party power dictionary validated.")
base_projection_davey = open(config['Swing_Input']['ElectionProjectionRaw']) csv_base_projection_davey = csv.DictReader(base_projection_davey) fieldnames = csv_base_projection_davey.fieldnames targetdata_2024 = open(config["Swing_Output"]["ElectionProjection"], 'w', newline='') csv_targetdata_2024 = csv.DictWriter(targetdata_2024, fieldnames=fieldnames) csv_targetdata_2024.writeheader() for row in csv_base_projection_davey: for party in Party: if party == Party.UNKNOWN or party == Party.DK: continue if adjusted_votes[row["constituency_name"]][party] is None or isclose( adjusted_votes[row["constituency_name"]][party], 0): row[party.name.lower() + "_share_2024"] = 0 else: row[party.name.lower() + "_share_2024"] = adjusted_votes[ row["constituency_name"]][party] row["valid_votes_2024"] = new_electorate_sizes[row["constituency_name"]] row["ld_swing_2024"] = calculate_swing_2024(row, Party.LD) #print(adjusted_votes[row["constituency_name"]].get) list_of_results_tuples = list( adjusted_votes[row["constituency_name"]].items()) list_of_results_tuples = [(sub[1], sub[0]) for sub in list_of_results_tuples] #print(list_of_results_tuples)