-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·208 lines (156 loc) · 7.45 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
import pymongo
import math
import pandas as pd
import json
import datetime
import time
import numpy as np
mytime = 1478635200
start = time.time()
dicoStates = {"Hawai":"HI","Alaska":"AK","Floride":"FL","New_Hampshire":"NH","Michigan":"MI","Vermont":"VT","Maine":"ME","Rhode_Island":"RI","New_York":"NY","Pennsylvanie":"PA","New_Jersey":"NJ","Delaware":"DE","Maryland":"MD","Virginie":"VA","Virginie_Occidentale":"WV","Ohio":"OH","Indiana":"IN","Illinois":"IL","Connecticut":"CT","Wisconsin":"WI","Caroline_du_Nord":"NC","District_de_Columbia":"DC","Massachusetts":"MA","Tennessee":"TN","Arkansas":"AR","Missouri":"MO","Georgie":"GA","Caroline_du_Sud":"SC","Kentucky":"KY","Alabama":"AL","Louisiane":"LS","Mississippi":"MS","Iowa":"IA","Minnesota":"MN","Oklahoma":"OK","Texas":"TX","Nouveau_Mexique":"NM","Kansas":"KS","Nebraska":"NE","Dakota_du_Sud":"SD","Dakota_du_Nord":"ND","Wyoming":"WY","Montana":"MT","Colorado":"CO","Idaho":"ID","Utah":"UT","Arizona":"AZ","Nevada":"NV","Oregon":"OR","Washington":"WA","Californie":"CA"}
c = pymongo.MongoClient('mongodb://172.31.31.100:27017,172.31.31.101:27017,172.31.31.102:27017/?replicaSet=rs0')
print("Cluster Mongo de départ")
time.sleep(0.2)
print(c.nodes)
candidats = ["Trump", "Clinton", "Blanc"]
idx_section = 0
def load_data( p=.6): # proba de garder un état de sa couleur politique
df = pd.read_csv("state.csv")
probas_dem = []
for gouv in df['gouv']:
if gouv == 'Rép.':
probas_dem.append(1 - p)
elif gouv == 'Dém.':
probas_dem.append(p)
else:
probas_dem.append(.5)
df['proba_dem'] = probas_dem
return df
def update_simulation(df, etat=None, gagnant=1):
# a chaque résultat d'un état qui tombe on met à jour la grille des probas et on relance la simulation
if etat != None:
df.loc[np.argmax(df['State']==etat), 'proba_dem'] = gagnant # 1 si les démocrates ont pris l'état, 0 sinon
# simulation monte carlo pour estimer la proba de victoire des démocrates
B = 100
dem = 0
for i in range(0, B):
if np.sum(df['nb_elector'][np.random.rand(51) < df['proba_dem']]) >= 270:
dem += 1
result = dem /float(B)
return result # proba que les démocrates gagnent les élections
def reconnection():
while(1):
try:
print("Wait 0.5s Reconnect Mongo cluster")
time.sleep(0.5)
c = pymongo.MongoClient('mongodb://172.31.31.100:27017,172.31.31.101:27017,172.31.31.102:27017/?replicaSet=rs0')
break
except pymongo.errors.AutoReconnect:
continue
return c
df_proba = pd.read_csv("state.csv", sep=";")
list_proba_win = list()
list_timestamp = list()
while(1) :
elapsed_time = time.time() - start
#print(datetime.datetime.fromtimestamp(mytime+10*elapsed_time))
try:
result = list(c.elections.votes_2.find({"timestamp":{"$lte":datetime.datetime.fromtimestamp(mytime+30*elapsed_time)}}))
except pymongo.errors.AutoReconnect:
c = reconnection()
time.sleep(0.2)
print(c.nodes)
result = list(c.elections.votes_2.find({"timestamp":{"$lte":datetime.datetime.fromtimestamp(mytime+30*elapsed_time)}}))
list_timestamp.append(result[0]["timestamp"])
df = pd.DataFrame(result)
df["candidate"] = df["candidate"].map(lambda x: "Autre" if x not in candidats else x)
df1 = df.groupby(["candidate", "state"]).sum()
df1 = df1.reset_index()
df1 = df1.pivot(index = "state", columns="candidate", values="voix")
df1 = df1.reset_index()
if "Blanc" not in list(df1.columns):
df1["Blanc"] = 0
df1 = df1.fillna(0)
df1["state"] = df1["state"].map(lambda x : dicoStates[x])
df1["color"] = "#000000"
df1["color"] = (np.argmax(df1[["Trump","Clinton"]].values, axis=1))
df1["color"] = df1["color"].map(lambda x : "#FF0000" if x ==0 else "#3399FF")
#df1 = df1.set_index("state")
dfEmpty = pd.DataFrame(columns = ['state', "Trump", "Blanc", "Clinton", "Autre", "color"])
dfEmpty["state"] = [elem for elem in dicoStates.values() if elem not in list(df1.state)]
dfEmpty["Trump"] = 0
dfEmpty["Blanc"] = 0
dfEmpty["Clinton"] = 0
dfEmpty["Autre"] = 0
dfEmpty["color"] = "#FBF8EF"
df1 = pd.concat([df1,dfEmpty])
print(df1.head(5))
df2 = df1.copy()
df2["sumVotes"] = df2["Trump"]+df2["Clinton"]+df2["Autre"]+df2["Blanc"]
for candidat in ["Trump", "Clinton", "Autre", "Blanc"]:
df2[candidat] = np.round(100*df2[candidat]/df2["sumVotes"], decimals=1)
df2[candidat] = df2[candidat].map(lambda x : str(x)+"%" if math.isnan(x) == False else "0%")
df2 = df2.drop("sumVotes", axis=1)
#Création du json pour l'estimation
dico = {}
for col in df1.state.unique():
dico2 = {}
for item in ["Autre", "Blanc", "Clinton", "Trump", "color"]:
dico2[item] = df1[df1["state"] == col][item].values[0]
dico[col] = dico2
file = open("/var/www/html/donneesVotes3.json", "w")
json.dump(dico,file)
file.close()
#Création du Json pour la map
dico = {}
for col in df2.state.unique():
dico2 = {}
for item in ["Autre", "Blanc", "Clinton", "Trump", "color"]:
dico2[item] = df2[df2["state"] == col][item].values[0]
dico[col] = dico2
file = open("/var/www/html/donneesVotes2.json", "w")
json.dump(dico,file)
file.close()
print(df2.head(5))
# Section sur l'import des données aggrégés
data = json.load(open("/var/www/html/donneesVotes3.json"))
base_elec = pd.read_csv("state.csv")
result = pd.DataFrame(columns=["name","vote"])
result = result.append({"name":"Clinton","vote":0}, ignore_index=True)
result = result.append({"name":"Trump","vote":0}, ignore_index=True)
proba = pd.DataFrame(columns=["name","percent"])
proba = proba.append({"name":"Clinton","percent":0}, ignore_index=True)
proba = proba.append({"name":"Trump","percent":0}, ignore_index=True)
for state in dicoStates.values():
# Les etats sont tous initialisés dans le json on doit boucler seulement sur les états dont les résultats sont >0
try :
vote_clinton = data[state]["Clinton"]
vote_trump = data[state]["Trump"]
vote_electeur = base_elec.loc[base_elec["State"]==state,"nb_elector"].values
if vote_clinton > 0 and vote_trump > 0:
if vote_clinton > vote_trump :
result.loc[result["name"]=="Clinton", "vote"] += vote_electeur[0]
list_proba_win.append( update_simulation(df_proba, state, gagnant=1) )
else :
result.loc[result["name"]=="Trump", "vote"] += vote_electeur[0]
list_proba_win.append( update_simulation(df_proba, state, gagnant=0) )
except KeyError :
continue
print(result)
idx_section +=1
result.to_json("/var/www/html/jsonOne.json", orient="index")
print("Liste des probabilitées finales")
print(list_proba_win)
proba.loc[proba["name"]=="Clinton", "percent"] = list_proba_win[-1]
proba.loc[proba["name"]=="Trump", "percent"] = (1 - list_proba_win[-1])
print(proba)
proba.to_json("/var/www/html/jsonTwo.json", orient="index")
print(list_timestamp)
dict_timestamp = {"timestamp": list_timestamp[-1].strftime("%Y-%m-%d %H:%M:%S")}
file_t = open("/var/www/html/timestamp.json", "w")
json.dump(dict_timestamp,file_t)
file_t.close()
print("Extraction numéro %s" % idx_section)
if elapsed_time >= 3700 : break
time.sleep(1)