Spaces:
Configuration error
Configuration error
Update apps/Covid19.py
Browse files- apps/Covid19.py +1 -368
apps/Covid19.py
CHANGED
@@ -1,369 +1,2 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
import pandas as pd
|
4 |
-
from pandas.core.groupby.groupby import DataError
|
5 |
-
from pytrends.request import TrendReq
|
6 |
-
from datetime import datetime, timedelta, date
|
7 |
-
import numpy as np
|
8 |
-
from plotly.subplots import make_subplots
|
9 |
-
from metodos import colores_corporativos
|
10 |
-
import pybase64 as base64
|
11 |
-
import io
|
12 |
-
from logs_portal import log
|
13 |
-
from Scheduler import Scheduler_Covid as sc
|
14 |
-
import os
|
15 |
-
|
16 |
-
def button_style():
|
17 |
-
style_button = """
|
18 |
-
<style>
|
19 |
-
button {
|
20 |
-
margin-top:-100px;
|
21 |
-
display: inline-block;
|
22 |
-
background-color: #e8e8e8;
|
23 |
-
border-radius: 15px;
|
24 |
-
border: 4px #cccccc;
|
25 |
-
color: #4a4a4a;
|
26 |
-
text-align: center;
|
27 |
-
font-size: 15px;
|
28 |
-
padding: 2px;
|
29 |
-
width: 260px;
|
30 |
-
transition: all 0.5s;
|
31 |
-
cursor: pointer;
|
32 |
-
margin: 5px;
|
33 |
-
}
|
34 |
-
button span {
|
35 |
-
cursor: pointer;
|
36 |
-
display: inline-block;
|
37 |
-
position: relative;
|
38 |
-
transition: 0.5s;
|
39 |
-
}
|
40 |
-
button span:after {
|
41 |
-
content: '\00bb';
|
42 |
-
position: absolute;
|
43 |
-
opacity: 0;
|
44 |
-
top: 0;
|
45 |
-
right: -20px;
|
46 |
-
transition: 0.5s;
|
47 |
-
}
|
48 |
-
button:hover {
|
49 |
-
background-color: #bb1114;
|
50 |
-
color:#e8e8e8;
|
51 |
-
}
|
52 |
-
button:hover span {
|
53 |
-
padding-right: 25px;
|
54 |
-
}
|
55 |
-
button:hover span:after {
|
56 |
-
opacity: 1;
|
57 |
-
right: 0;
|
58 |
-
}
|
59 |
-
</style>
|
60 |
-
"""
|
61 |
-
st.markdown(style_button, unsafe_allow_html=True)
|
62 |
-
|
63 |
-
|
64 |
-
def get_table_download_link(df):
|
65 |
-
"""Generates a link allowing the data in a given panda dataframe to be
|
66 |
-
downloaded
|
67 |
-
in: dataframe
|
68 |
-
out: href string
|
69 |
-
"""
|
70 |
-
csv = df.to_csv(index=False)
|
71 |
-
b64 = base64.b64encode(csv.encode()).decode()
|
72 |
-
name_arch = "Scoring_filtrado.csv"
|
73 |
-
name_mark = "Descargar .csv "
|
74 |
-
style = '"color:black;text-decoration: none;font-size:18px;"'
|
75 |
-
href = f'<center><a href="data:file/csv;base64,{b64}" style=' + style+' download="'+name_arch+'" ><button>'+name_mark+'</button></a></center>'
|
76 |
-
return href
|
77 |
-
|
78 |
-
|
79 |
-
def get_table_excel_link(df, name_arch):
|
80 |
-
towrite = io.BytesIO()
|
81 |
-
downloaded_file = df.to_excel(towrite, encoding='utf-8', index=False,
|
82 |
-
header=True)
|
83 |
-
towrite.seek(0) # reset pointer
|
84 |
-
file_name = name_arch
|
85 |
-
style = 'style="color:black;text-decoration: none; font-size:18px;" '
|
86 |
-
name_mark = "Descargar "+name_arch
|
87 |
-
b64 = base64.b64encode(towrite.read()).decode() # some strings
|
88 |
-
linko= f'<center><a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}" '+style+'download="'+file_name+'"><button>'+name_mark+'</button></a></center>'
|
89 |
-
return linko
|
90 |
-
|
91 |
-
|
92 |
-
@st.cache(show_spinner=True)
|
93 |
-
def charged_data():
|
94 |
-
regiones = {}
|
95 |
-
regiones['Latam'] = ['Argentina', 'Brazil', 'Chile', 'Colombia',
|
96 |
-
'Mexico', 'Peru']
|
97 |
-
regiones['Europa'] = ['Italy', 'Spain', 'Germany', 'United Kingdom',
|
98 |
-
'France']
|
99 |
-
regiones['Asia Emergente'] = ['South Korea', 'Taiwan', 'Hong Kong',
|
100 |
-
'India', 'Thailand', 'Indonesia']
|
101 |
-
regiones['USA'] = ['United States']
|
102 |
-
data_dict = np.load('Scheduler/dict_movilidad.npy',
|
103 |
-
allow_pickle='TRUE').item()
|
104 |
-
return data_dict, regiones
|
105 |
-
|
106 |
-
|
107 |
-
@st.cache(show_spinner=True)
|
108 |
-
def charged_data2():
|
109 |
-
covid_data = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv')
|
110 |
-
paises = {'CL': 'Chile', 'AR': 'Argentina', 'BR': 'Brazil',
|
111 |
-
'MX': 'Mexico'}
|
112 |
-
covid_data = covid_data.loc[covid_data['location'].isin(paises.values())]
|
113 |
-
covid_data['date'] = pd.to_datetime(covid_data['date'])
|
114 |
-
covid_data.set_index(['date', 'location'], inplace=True)
|
115 |
-
# Creamos diccionario con cada una de las variables para distintos países
|
116 |
-
data_dict = {}
|
117 |
-
for col in covid_data.columns:
|
118 |
-
try:
|
119 |
-
data_dict[col] = covid_data[col].unstack().fillna(0).rolling(1).mean()
|
120 |
-
except DataError:
|
121 |
-
pass
|
122 |
-
# Descargamos la data de google trends
|
123 |
-
pytrends = TrendReq(retries=5, backoff_factor=0.2,
|
124 |
-
requests_args={'verify': False})
|
125 |
-
start = (datetime.today() - timedelta(180)).strftime("%Y-%m-%d")
|
126 |
-
start = datetime(2020, 2, 1).strftime("%Y-%m-%d")
|
127 |
-
end = datetime.today().strftime("%Y-%m-%d")
|
128 |
-
tf = f'{start} {end}'
|
129 |
-
kw_lists = {
|
130 |
-
'CL': ['PCR', 'sintomas covid', 'examen covid',
|
131 |
-
'covid positivo'],
|
132 |
-
'AR': ['PCR', 'olfato', 'sintomas covid', 'perdida gusto',
|
133 |
-
'covid positivo'],
|
134 |
-
'MX': ['PCR', 'olfato', 'sintomas covid', 'covid positivo',
|
135 |
-
'perdida gusto'],
|
136 |
-
'BR': ['PCR', 'sintomas covid', 'exame covid', 'covid positivo']
|
137 |
-
}
|
138 |
-
gt_data = {}
|
139 |
-
for p, kw in kw_lists.items():
|
140 |
-
pytrends.build_payload(kw, timeframe=tf, geo=p)
|
141 |
-
df = pytrends.interest_over_time().drop(columns='isPartial')
|
142 |
-
gt_data[paises[p]] = df.div(df.mean(0).values)
|
143 |
-
data_dict['GT Index'] = pd.DataFrame({p: gt_data[p].mean(1).rolling(1).mean()
|
144 |
-
for p in gt_data.keys()})
|
145 |
-
return data_dict, paises
|
146 |
-
|
147 |
-
|
148 |
-
@log
|
149 |
-
def Movilidad():
|
150 |
-
largo = 400
|
151 |
-
ancho = 550
|
152 |
-
button_style()
|
153 |
-
placebar = st.empty()
|
154 |
-
percent_complete = 0
|
155 |
-
my_bar = placebar.progress(percent_complete)
|
156 |
-
data_cargada = charged_data()
|
157 |
-
data_dict = data_cargada[0]
|
158 |
-
regiones = data_cargada[1]
|
159 |
-
europa = data_dict['Mobility Index'][regiones.keys()]["Europa"]
|
160 |
-
latam = data_dict['Mobility Index'][regiones.keys()]["Latam"]
|
161 |
-
asia = data_dict['Mobility Index'][regiones.keys()]["Asia Emergente"]
|
162 |
-
USA = data_dict['Mobility Index'][regiones.keys()]["USA"]
|
163 |
-
mov_region = data_dict['Mobility Index'][regiones.keys()][["USA", "Europa","Asia Emergente", "Latam"]]
|
164 |
-
percent_complete = percent_complete+33
|
165 |
-
placebar.progress(percent_complete)
|
166 |
-
colores = list(colores_corporativos().values())
|
167 |
-
colores2 = []
|
168 |
-
for i in range(len(colores)):
|
169 |
-
colores2.append("rgb"+str(colores[i]))
|
170 |
-
def plot_raw_data():
|
171 |
-
fig = go.Figure()
|
172 |
-
europa_ = go.Scatter(x=europa.index, y=europa.values, name="Europa",
|
173 |
-
line=dict(color=colores2[0]))
|
174 |
-
latam_ = go.Scatter(x=latam.index, y=latam.values, name="Latam",
|
175 |
-
line=dict(color=colores2[1]))
|
176 |
-
USA_ = go.Scatter(x=USA.index, y=USA.values, name="USA",
|
177 |
-
line=dict(color=colores2[2]))
|
178 |
-
asia_ = go.Scatter(x=asia.index, y=asia.values, name="Asia Emergente",
|
179 |
-
line=dict(color=colores2[3]))
|
180 |
-
fig.add_trace(europa_)
|
181 |
-
fig.add_trace(latam_)
|
182 |
-
fig.add_trace(USA_)
|
183 |
-
fig.add_trace(asia_)
|
184 |
-
fig.layout.update(title_text="Evolución por region",
|
185 |
-
xaxis_rangeslider_visible=True,
|
186 |
-
margin_b=20,
|
187 |
-
margin_r=20,
|
188 |
-
margin_l=20,
|
189 |
-
width=ancho,
|
190 |
-
height=largo,
|
191 |
-
legend=dict(orientation="h",
|
192 |
-
yanchor="bottom",
|
193 |
-
y=1.02,
|
194 |
-
xanchor="right",
|
195 |
-
x=1))
|
196 |
-
fig2 = go.Figure()
|
197 |
-
i = 0
|
198 |
-
for pais in regiones["Latam"]:
|
199 |
-
data_pais = data_dict['Mobility Index'][regiones['Latam']][pais]
|
200 |
-
pais_gr = go.Scatter(x=data_pais.index,
|
201 |
-
y=data_pais.values, name=pais,
|
202 |
-
line=dict(color=colores2[i]))
|
203 |
-
fig2.add_trace(pais_gr)
|
204 |
-
i = i+1
|
205 |
-
fig2.layout.update(title_text="Evolución LATAM",
|
206 |
-
xaxis_rangeslider_visible=True, margin_b=20,
|
207 |
-
margin_r=20,margin_l=20,
|
208 |
-
width=ancho, height=largo,
|
209 |
-
legend=dict(orientation="h",
|
210 |
-
yanchor="bottom",
|
211 |
-
y=1.0,
|
212 |
-
xanchor="right",
|
213 |
-
x=1))
|
214 |
-
col1, col2 = st.columns(2)
|
215 |
-
col1.plotly_chart(fig, use_container_width=True)
|
216 |
-
col2.plotly_chart(fig2, use_container_width=True)
|
217 |
-
link_excel_1 = get_table_excel_link(data_dict['Mobility Index'][regiones['Latam']], "Movilidad Latam.xlsx")
|
218 |
-
link_excel_2 = get_table_excel_link(mov_region, "Movilidad por region.xlsx")
|
219 |
-
col1.markdown(link_excel_1, unsafe_allow_html=True)
|
220 |
-
col2.markdown(link_excel_2, unsafe_allow_html=True)
|
221 |
-
percent_complete = percent_complete + 33
|
222 |
-
placebar.progress(percent_complete)
|
223 |
-
placebar.empty()
|
224 |
-
plot_raw_data()
|
225 |
-
percent_complete = percent_complete + 34
|
226 |
-
my_bar.progress(percent_complete)
|
227 |
-
my_bar.empty()
|
228 |
-
data_desag = pd.read_excel("Scheduler/Movilidad_desagrada.xlsx",
|
229 |
-
engine="openpyxl")
|
230 |
-
st.markdown(get_table_excel_link(data_desag, "Movilidad desagregada.xlsx"),
|
231 |
-
unsafe_allow_html=True)
|
232 |
-
try:
|
233 |
-
user = os.getlogin()
|
234 |
-
if user == 'bullm':
|
235 |
-
act = st.button('Actualizar')
|
236 |
-
if act:
|
237 |
-
sc.run_data_covid()
|
238 |
-
ud = pd.read_excel('Data/update_data.xlsx')
|
239 |
-
ud = ud[ud['View'] != 'Covid19']
|
240 |
-
today = date.today().strftime('%d-%m-%Y')
|
241 |
-
ud = ud.append({"View": "Covid19",
|
242 |
-
"Last_Update": today}, ignore_index=True)
|
243 |
-
ud.to_excel('Data/update_data.xlsx', index=False)
|
244 |
-
except Exception:
|
245 |
-
pass
|
246 |
-
|
247 |
-
|
248 |
-
@log
|
249 |
-
def Correlacion_GT():
|
250 |
-
largo = 400
|
251 |
-
ancho = 550
|
252 |
-
button_style()
|
253 |
-
# Cargamos la data relevante
|
254 |
-
percent_complete = 0
|
255 |
-
my_bar = st.progress(percent_complete)
|
256 |
-
percent_complete = percent_complete + 33
|
257 |
-
my_bar.progress(percent_complete)
|
258 |
-
data_cargada = charged_data2()
|
259 |
-
data_dict = data_cargada[0]
|
260 |
-
paises = data_cargada[1]
|
261 |
-
corr_df = pd.DataFrame(index=paises.values(), columns=np.arange(-3, 1))
|
262 |
-
percent_complete = percent_complete + 33
|
263 |
-
my_bar.progress(percent_complete)
|
264 |
-
i = 0
|
265 |
-
cols = st.columns(2)
|
266 |
-
col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
|
267 |
-
for p in corr_df.index:
|
268 |
-
df = pd.concat([data_dict['GT Index'][p],
|
269 |
-
data_dict['new_cases_per_million'][p]],
|
270 |
-
axis=1).dropna()
|
271 |
-
df.columns = ['GT Index', 'Nuevos Casos Confirmados']
|
272 |
-
fig = make_subplots(specs=[[{"secondary_y": True}]])
|
273 |
-
CC = go.Scatter(x=df['GT Index'].index,
|
274 |
-
y=df['GT Index'].values, name='GT index',
|
275 |
-
line=dict(color='dimgrey'))
|
276 |
-
GT = go.Scatter(x=df['Nuevos Casos Confirmados'].index,
|
277 |
-
y=df['Nuevos Casos Confirmados'].values,
|
278 |
-
name='Casos confirmados', line=dict(color='darkred'))
|
279 |
-
fig.add_trace(CC, secondary_y=False,)
|
280 |
-
fig.add_trace(GT, secondary_y=True,)
|
281 |
-
fig.layout.update(title_text="Evolución {}".format(p),
|
282 |
-
xaxis_rangeslider_visible=True, margin_b=20,
|
283 |
-
margin_r=20, margin_l=20,
|
284 |
-
width=ancho, height=largo,
|
285 |
-
legend=dict(orientation="h",
|
286 |
-
yanchor="bottom",
|
287 |
-
y=1.02,
|
288 |
-
xanchor="right",
|
289 |
-
x=1))
|
290 |
-
link_excel = get_table_excel_link(df, "Correlacion GT.xlsx")
|
291 |
-
if i % 2 == 0:
|
292 |
-
cols[0].plotly_chart(fig, use_container_width=True)
|
293 |
-
cols[0].markdown(link_excel, unsafe_allow_html=True)
|
294 |
-
else:
|
295 |
-
cols[1].plotly_chart(fig, use_container_width=True)
|
296 |
-
cols[1].markdown(link_excel, unsafe_allow_html=True)
|
297 |
-
cols = st.columns(2)
|
298 |
-
col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
|
299 |
-
i = i + 1
|
300 |
-
percent_complete = percent_complete + 34
|
301 |
-
my_bar.progress(percent_complete)
|
302 |
-
my_bar.empty()
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
@log
|
307 |
-
def vacunas():
|
308 |
-
largo = 400
|
309 |
-
ancho = 550
|
310 |
-
button_style()
|
311 |
-
vac_data = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv').set_index(['date','location'])
|
312 |
-
country_pop = (vac_data['population'].reset_index().set_index('location')
|
313 |
-
.drop(columns='date').squeeze().drop_duplicates())
|
314 |
-
min_pop = 1000000
|
315 |
-
idx = country_pop[country_pop > min_pop].index
|
316 |
-
vac_data = vac_data['total_vaccinations_per_hundred'].unstack().ffill().fillna(0)
|
317 |
-
vac_data.index = pd.to_datetime(vac_data.index)
|
318 |
-
N = 15
|
319 |
-
top_vac = vac_data[idx].iloc[-1].nlargest(N).sort_values()
|
320 |
-
regiones = {}
|
321 |
-
regiones['Latam'] = ['Argentina', 'Brazil', 'Chile', 'Colombia',
|
322 |
-
'Mexico', 'Peru']
|
323 |
-
regiones['Europa'] = ['Italy', 'Spain', 'Germany', 'United Kingdom',
|
324 |
-
'France', 'Russia']
|
325 |
-
regiones['Asia Emergente'] = ['South Korea', 'Taiwan', 'Hong Kong',
|
326 |
-
'China', 'Japan']
|
327 |
-
regiones['Norteamérica'] = ['United States', 'Canada']
|
328 |
-
inicio = datetime(2020, 11, 15)
|
329 |
-
vac_data = vac_data.loc[vac_data.index > inicio].resample('W').last()
|
330 |
-
vac_data.index.name = ''
|
331 |
-
colores = colores_corporativos().values()
|
332 |
-
colores = list(colores_corporativos().values())
|
333 |
-
colores2 = []
|
334 |
-
for i in range(len(colores)):
|
335 |
-
colores2.append("rgb"+str(colores[i]))
|
336 |
-
|
337 |
-
def plot_raw_data():
|
338 |
-
i = 0
|
339 |
-
cols = st.columns(2)
|
340 |
-
col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
|
341 |
-
for region in list(regiones.keys()):
|
342 |
-
fig = go.Figure()
|
343 |
-
j = 0
|
344 |
-
for pais in regiones[region]:
|
345 |
-
data_pais = vac_data[regiones[region]][pais]
|
346 |
-
pais_gr = go.Scatter(x=data_pais.index,
|
347 |
-
y=data_pais.values, name=pais,
|
348 |
-
line=dict(color=colores2[j]))
|
349 |
-
fig.add_trace(pais_gr)
|
350 |
-
j = j+1
|
351 |
-
fig.layout.update(title_text="Evolución "+region,
|
352 |
-
xaxis_rangeslider_visible=True, height=largo,
|
353 |
-
width=ancho, margin_b=20,
|
354 |
-
legend=dict(orientation="h",
|
355 |
-
yanchor="bottom",
|
356 |
-
y=1.0,
|
357 |
-
xanchor="right",
|
358 |
-
x=1))
|
359 |
-
link_excel = get_table_excel_link(data_pais, "Vacunacion.xlsx")
|
360 |
-
if i % 2 == 0:
|
361 |
-
cols[0].plotly_chart(fig, use_column_width=True)
|
362 |
-
cols[0].markdown(link_excel, unsafe_allow_html=True)
|
363 |
-
else:
|
364 |
-
cols[1].plotly_chart(fig, use_column_width=True)
|
365 |
-
cols[1].markdown(link_excel, unsafe_allow_html=True)
|
366 |
-
cols = st.columns(2)
|
367 |
-
col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
|
368 |
-
i = i+1
|
369 |
-
plot_raw_data()
|
|
|
1 |
import streamlit as st
|
2 |
+
st.write("Covid View Turn Off")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|