File size: 15,184 Bytes
a8b660a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import streamlit as st
from plotly import graph_objs as go
import pandas as pd
from pandas.core.groupby.groupby import DataError
from pytrends.request import TrendReq
from datetime import datetime, timedelta, date
import numpy as np
from plotly.subplots import make_subplots
from metodos import colores_corporativos
import pybase64 as base64
import io
from logs_portal import log
from Scheduler import Scheduler_Covid as sc
import os

def button_style():
    style_button = """
        <style>
      button {
        margin-top:-100px;
        display: inline-block;
        background-color: #e8e8e8;
        border-radius: 15px;
        border: 4px  #cccccc;
        color: #4a4a4a;
        text-align: center;
        font-size: 15px;
        padding: 2px;
        width: 260px;
        transition: all 0.5s;
        cursor: pointer;
        margin: 5px;
      }
      button span {
        cursor: pointer;
        display: inline-block;
        position: relative;
        transition: 0.5s;
      }
      button span:after {
        content: '\00bb';
        position: absolute;
        opacity: 0;
        top: 0;
        right: -20px;
        transition: 0.5s;
      }
      button:hover {
        background-color: #bb1114;
        color:#e8e8e8;
      }
      button:hover span {
        padding-right: 25px;
      }
      button:hover span:after {
        opacity: 1;
        right: 0;
      }
    </style>
    """
    st.markdown(style_button, unsafe_allow_html=True)


def get_table_download_link(df):
    """Generates a link allowing the data in a given panda dataframe to be
    downloaded
    in:  dataframe
    out: href string
    """
    csv = df.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode()
    name_arch = "Scoring_filtrado.csv"
    name_mark = "Descargar .csv "
    style = '"color:black;text-decoration: none;font-size:18px;"'
    href = f'<center><a href="data:file/csv;base64,{b64}" style='  + style+' download="'+name_arch+'" ><button>'+name_mark+'</button></a></center>'
    return href


def get_table_excel_link(df, name_arch):
    towrite = io.BytesIO()
    downloaded_file = df.to_excel(towrite, encoding='utf-8', index=False,
                                  header=True)
    towrite.seek(0)  # reset pointer
    file_name = name_arch
    style = 'style="color:black;text-decoration: none; font-size:18px;" '
    name_mark = "Descargar "+name_arch
    b64 = base64.b64encode(towrite.read()).decode()  # some strings
    linko= f'<center><a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}" '+style+'download="'+file_name+'"><button>'+name_mark+'</button></a></center>'
    return linko


@st.cache(show_spinner=True)
def charged_data():
    regiones = {}
    regiones['Latam'] = ['Argentina', 'Brazil', 'Chile', 'Colombia',
                         'Mexico', 'Peru']
    regiones['Europa'] = ['Italy', 'Spain', 'Germany', 'United Kingdom',
                          'France']
    regiones['Asia Emergente'] = ['South Korea', 'Taiwan', 'Hong Kong',
                                  'India', 'Thailand', 'Indonesia']
    regiones['USA'] = ['United States']
    data_dict = np.load('Scheduler/dict_movilidad.npy',
                        allow_pickle='TRUE').item()
    return data_dict, regiones


@st.cache(show_spinner=True)
def charged_data2():
    covid_data = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv')
    paises = {'CL': 'Chile', 'AR': 'Argentina', 'BR': 'Brazil',
              'MX': 'Mexico'}
    covid_data = covid_data.loc[covid_data['location'].isin(paises.values())]
    covid_data['date'] = pd.to_datetime(covid_data['date'])
    covid_data.set_index(['date', 'location'], inplace=True)
    # Creamos diccionario con cada una de las variables para distintos pa铆ses
    data_dict = {}
    for col in covid_data.columns:
        try:
            data_dict[col] = covid_data[col].unstack().fillna(0).rolling(1).mean()
        except DataError:
            pass
    # Descargamos la data de google trends
    pytrends = TrendReq(retries=5, backoff_factor=0.2,
                        requests_args={'verify': False})
    start = (datetime.today() - timedelta(180)).strftime("%Y-%m-%d")
    start = datetime(2020, 2, 1).strftime("%Y-%m-%d")
    end = datetime.today().strftime("%Y-%m-%d")
    tf = f'{start} {end}'
    kw_lists = {
                'CL': ['PCR', 'sintomas covid', 'examen covid',
                       'covid positivo'],
                'AR': ['PCR', 'olfato', 'sintomas covid', 'perdida gusto',
                       'covid positivo'],
                'MX': ['PCR', 'olfato', 'sintomas covid', 'covid positivo',
                       'perdida gusto'],
                'BR': ['PCR', 'sintomas covid', 'exame covid', 'covid positivo']
                }
    gt_data = {}
    for p, kw in kw_lists.items():
        pytrends.build_payload(kw, timeframe=tf, geo=p)
        df = pytrends.interest_over_time().drop(columns='isPartial')
        gt_data[paises[p]] = df.div(df.mean(0).values)
    data_dict['GT Index'] = pd.DataFrame({p: gt_data[p].mean(1).rolling(1).mean()
                                          for p in gt_data.keys()})
    return data_dict, paises


@log
def Movilidad():
    largo = 400
    ancho = 550
    button_style()
    placebar = st.empty()
    percent_complete = 0
    my_bar = placebar.progress(percent_complete)
    data_cargada = charged_data()
    data_dict = data_cargada[0]
    regiones = data_cargada[1]
    europa = data_dict['Mobility Index'][regiones.keys()]["Europa"]
    latam = data_dict['Mobility Index'][regiones.keys()]["Latam"]
    asia = data_dict['Mobility Index'][regiones.keys()]["Asia Emergente"]
    USA = data_dict['Mobility Index'][regiones.keys()]["USA"]
    mov_region = data_dict['Mobility Index'][regiones.keys()][["USA", "Europa","Asia Emergente", "Latam"]]
    percent_complete = percent_complete+33
    placebar.progress(percent_complete)
    colores = list(colores_corporativos().values())
    colores2 = []
    for i in range(len(colores)):
        colores2.append("rgb"+str(colores[i]))
    def plot_raw_data():
        fig = go.Figure()
        europa_ = go.Scatter(x=europa.index, y=europa.values, name="Europa",
                             line=dict(color=colores2[0]))
        latam_ = go.Scatter(x=latam.index, y=latam.values, name="Latam",
                            line=dict(color=colores2[1]))
        USA_ = go.Scatter(x=USA.index, y=USA.values, name="USA",
                          line=dict(color=colores2[2]))
        asia_ = go.Scatter(x=asia.index, y=asia.values, name="Asia Emergente",
                           line=dict(color=colores2[3]))
        fig.add_trace(europa_)
        fig.add_trace(latam_)
        fig.add_trace(USA_)
        fig.add_trace(asia_)
        fig.layout.update(title_text="Evoluci贸n por region",
                          xaxis_rangeslider_visible=True,
                          margin_b=20,
                          margin_r=20,
                          margin_l=20,
                          width=ancho,
                          height=largo,
                          legend=dict(orientation="h",
                                      yanchor="bottom",
                                      y=1.02,
                                      xanchor="right",
                                      x=1))
        fig2 = go.Figure()
        i = 0
        for pais in regiones["Latam"]:
            data_pais = data_dict['Mobility Index'][regiones['Latam']][pais]
            pais_gr = go.Scatter(x=data_pais.index,
                                 y=data_pais.values, name=pais,
                                 line=dict(color=colores2[i]))
            fig2.add_trace(pais_gr)
            i = i+1
        fig2.layout.update(title_text="Evoluci贸n LATAM",
                           xaxis_rangeslider_visible=True, margin_b=20,
                           margin_r=20,margin_l=20,
                           width=ancho, height=largo,
                           legend=dict(orientation="h",
                                       yanchor="bottom",
                                       y=1.0,
                                       xanchor="right",
                                       x=1))
        col1, col2 = st.columns(2)
        col1.plotly_chart(fig, use_container_width=True)
        col2.plotly_chart(fig2, use_container_width=True)
        link_excel_1 = get_table_excel_link(data_dict['Mobility Index'][regiones['Latam']], "Movilidad Latam.xlsx")
        link_excel_2 = get_table_excel_link(mov_region, "Movilidad por region.xlsx")
        col1.markdown(link_excel_1, unsafe_allow_html=True)
        col2.markdown(link_excel_2, unsafe_allow_html=True)
    percent_complete = percent_complete + 33
    placebar.progress(percent_complete)
    placebar.empty()
    plot_raw_data()
    percent_complete = percent_complete + 34
    my_bar.progress(percent_complete)
    my_bar.empty()
    data_desag = pd.read_excel("Scheduler/Movilidad_desagrada.xlsx",
                               engine="openpyxl")
    st.markdown(get_table_excel_link(data_desag, "Movilidad desagregada.xlsx"),
                unsafe_allow_html=True)
    try:
        user = os.getlogin()
        if user == 'bullm':
            act = st.button('Actualizar')
            if act:
                sc.run_data_covid()
                ud = pd.read_excel('Data/update_data.xlsx')
                ud = ud[ud['View'] != 'Covid19']
                today = date.today().strftime('%d-%m-%Y')
                ud = ud.append({"View": "Covid19",
                                "Last_Update": today}, ignore_index=True)
                ud.to_excel('Data/update_data.xlsx', index=False)
    except Exception:
        pass


@log
def Correlacion_GT():
    largo = 400
    ancho = 550
    button_style()
    # Cargamos la data relevante
    percent_complete = 0
    my_bar = st.progress(percent_complete)
    percent_complete = percent_complete + 33
    my_bar.progress(percent_complete)
    data_cargada = charged_data2()
    data_dict = data_cargada[0]
    paises = data_cargada[1]
    corr_df = pd.DataFrame(index=paises.values(), columns=np.arange(-3, 1))
    percent_complete = percent_complete + 33
    my_bar.progress(percent_complete)
    i = 0
    cols = st.columns(2)
    col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
    for p in corr_df.index:
        df = pd.concat([data_dict['GT Index'][p],
                        data_dict['new_cases_per_million'][p]],
                       axis=1).dropna()
        df.columns = ['GT Index', 'Nuevos Casos Confirmados']
        fig = make_subplots(specs=[[{"secondary_y": True}]])
        CC = go.Scatter(x=df['GT Index'].index,
                        y=df['GT Index'].values, name='GT index',
                        line=dict(color='dimgrey'))
        GT = go.Scatter(x=df['Nuevos Casos Confirmados'].index,
                        y=df['Nuevos Casos Confirmados'].values,
                        name='Casos confirmados', line=dict(color='darkred'))
        fig.add_trace(CC, secondary_y=False,)
        fig.add_trace(GT, secondary_y=True,)
        fig.layout.update(title_text="Evoluci贸n {}".format(p),
                          xaxis_rangeslider_visible=True, margin_b=20,
                          margin_r=20, margin_l=20,
                          width=ancho, height=largo,
                          legend=dict(orientation="h",
                                      yanchor="bottom",
                                      y=1.02,
                                      xanchor="right",
                                      x=1))
        link_excel = get_table_excel_link(df, "Correlacion GT.xlsx")
        if i % 2 == 0:
            cols[0].plotly_chart(fig, use_container_width=True)
            cols[0].markdown(link_excel, unsafe_allow_html=True)
        else:
            cols[1].plotly_chart(fig, use_container_width=True)
            cols[1].markdown(link_excel, unsafe_allow_html=True)
            cols = st.columns(2)
            col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
        i = i + 1
    percent_complete = percent_complete + 34
    my_bar.progress(percent_complete)
    my_bar.empty()



@log
def vacunas():
    largo = 400
    ancho = 550
    button_style()
    vac_data = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv').set_index(['date','location'])
    country_pop = (vac_data['population'].reset_index().set_index('location')
                   .drop(columns='date').squeeze().drop_duplicates())
    min_pop = 1000000
    idx = country_pop[country_pop > min_pop].index
    vac_data = vac_data['total_vaccinations_per_hundred'].unstack().ffill().fillna(0)
    vac_data.index = pd.to_datetime(vac_data.index)
    N = 15
    top_vac = vac_data[idx].iloc[-1].nlargest(N).sort_values()
    regiones = {}
    regiones['Latam'] = ['Argentina', 'Brazil', 'Chile', 'Colombia',
                         'Mexico', 'Peru']
    regiones['Europa'] = ['Italy', 'Spain', 'Germany', 'United Kingdom',
                          'France', 'Russia']
    regiones['Asia Emergente'] = ['South Korea', 'Taiwan', 'Hong Kong',
                                  'China', 'Japan']
    regiones['Norteam茅rica'] = ['United States', 'Canada']
    inicio = datetime(2020, 11, 15)
    vac_data = vac_data.loc[vac_data.index > inicio].resample('W').last()
    vac_data.index.name = ''
    colores = colores_corporativos().values()
    colores = list(colores_corporativos().values())
    colores2 = []
    for i in range(len(colores)):
        colores2.append("rgb"+str(colores[i]))

    def plot_raw_data():
        i = 0
        cols = st.columns(2)
        col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
        for region in list(regiones.keys()):
            fig = go.Figure()
            j = 0
            for pais in regiones[region]:
                data_pais = vac_data[regiones[region]][pais]
                pais_gr = go.Scatter(x=data_pais.index,
                                     y=data_pais.values, name=pais,
                                     line=dict(color=colores2[j]))
                fig.add_trace(pais_gr)
                j = j+1
            fig.layout.update(title_text="Evoluci贸n "+region,
                              xaxis_rangeslider_visible=True, height=largo,
                              width=ancho, margin_b=20,
                              legend=dict(orientation="h",
                                          yanchor="bottom",
                                          y=1.0,
                                          xanchor="right",
                                          x=1))
            link_excel = get_table_excel_link(data_pais, "Vacunacion.xlsx")
            if i % 2 == 0:
                cols[0].plotly_chart(fig, use_column_width=True)
                cols[0].markdown(link_excel, unsafe_allow_html=True)
            else:
                cols[1].plotly_chart(fig, use_column_width=True)
                cols[1].markdown(link_excel, unsafe_allow_html=True)
                cols = st.columns(2)
                col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7))
            i = i+1
    plot_raw_data()