generated from com-480-data-visualization/com-480-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeoretical_results.py
58 lines (44 loc) · 2.7 KB
/
theoretical_results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import codecs
import json
import os
refsPath = "../website/resources/results"
federalElectionResultsPath = os.path.join(refsPath, "federal_elections.json")
votesInfosPath = "../website/resources/json_swiss_data_2.json"
votesInfos = json.load(codecs.open(votesInfosPath, "r", "utf-8-sig"))
federalElectionResults = json.load(open(federalElectionResultsPath))
#refractor federal election results for convenience
federalElectionResults = {year: {x["partei_bezeichnung_de"]: x["partei_staerke"] for x in data} for year, data in federalElectionResults.items()}
recommsYes = {1, 9}
recommsNo = {2, 8}
for i, (refId, voteInfo) in enumerate(votesInfos.items()):
#extract party recommendations
print(refId)
partiesRecomms = {fieldName[len("p-"):]: int(voteInfo[fieldName]) for fieldName in filter(lambda fieldName: fieldName.startswith("p-") and voteInfo[fieldName] is not None, voteInfo.keys())}
electionYear = voteInfo["annee_legislatur"].split("-")[0]
federal = federalElectionResults.get(electionYear) #can be None if we do not have federal election data -> ignore
if federal:
yes = dict()
no = dict()
for party, recomm in partiesRecomms.items():
if party in federal:
for cantonCode, percentage in federal[party].items():
turnoutVote = voteInfo[cantonCode+"-berecht"] if cantonCode != "ch" else sum(voteInfo[x] for x in voteInfo if x.endswith("-berecht"))
weight = turnoutVote * 0.01 * percentage
if cantonCode not in yes:
yes[cantonCode] = 0
no[cantonCode] = 0
if recomm in recommsYes:
yes[cantonCode] += weight
elif recomm in recommsNo:
no[cantonCode] += weight
res = {cantonCode: (100*a)/(a+b) for (cantonCode, a), b in zip(yes.items(), no.values()) if a+b != 0}
res2 = {cantonCode: {"per":(voteInfo[f"{cantonCode}-japroz"] if cantonCode != "ch" else voteInfo["volkja-proz"])-simRes} for cantonCode, simRes in res.items()}
for cantonCode, scoreYes in list(res.items()):
res[f"{cantonCode}-annahme" if cantonCode != "ch" else "annahme"] = int(scoreYes >= 50) + 8*(voteInfo["forme"] == 5)
res[cantonCode] = {"per": scoreYes}
res["forme"] = voteInfo["forme"]
with open(os.path.join(os.path.dirname(refsPath), f"sims/{refId}.json"), "w") as f:
json.dump(res, f)
print(f"Saved {refId}, {i+1}/{len(votesInfos)}")
with open(os.path.join(os.path.dirname(refsPath), f"compsims/{refId}.json"), "w") as f:
json.dump(res2, f)