Esempio n. 1
0
def get_stat(df_projections,d, val, scope):
    if d is None:
        return None
    if isinstance(d, str):
        d = datetime.datetime.strptime(d, '%Y-%m-%d').date()
    if scope == 'US':
        df_projections_sub = df_projections.loc[(df_projections.Country == scope) & (df_projections.Province == 'None')]
    elif scope =='World':
        df_projections_sub = df_projections.loc[df_projections.Continent == 'None']
    else:
        df_projections_sub = df_projections.loc[(df_projections.Continent == scope) & (df_projections.Country == 'None')]

    df_projections_sub = df_projections_sub.loc[df_projections_sub['Day']==d].reset_index()

    if df_projections_sub.empty:
        return None

    card_content = [
        dbc.CardHeader(
            f'{df_projections_sub.iloc[0][val]:,}',
            style={"textAlign":"center","fontSize":30,"fontWeight": "bold","color":'#1E74F0'}
        ),
        dbc.CardBody(
            [
                html.H5(add_cases(val),id='us-stats-cards'),
            ]
        ),
    ]
    return card_content
Esempio n. 2
0
def build_continent_map(df_continent,PopInfo,map_date,val='Active', continent = 'World', pop = 1):
    if continent !='World':
        df_continent = df_continent.loc[df_continent.Continent == continent] #Filter by continent

    if map_date is None:
        return None

    if isinstance(map_date, str):
        map_date = datetime.datetime.strptime(map_date, '%Y-%m-%d').date()

    df_map = df_continent.loc[df_continent['Day'] == map_date]
    df_map = df_map.loc[df_map['Province'] == 'None'] #exclude province data
    df_map = df_map.loc[df_map['Country'] != 'None'] #exclude global world data

    population = np.array([])
    for i in df_map['Country']:
        ind1 = np.logical_and(PopInfo['Country']==i, PopInfo['Province']=='None')
        pop_val = PopInfo.loc[ind1,'pop'].values
        population = np.concatenate((population, pop_val),0)

    df_map['Population'] =population

    df_map['Active Per Million'] = (np.round(1000000*df_map['Active']/df_map['Population'], decimals = 2))
    df_map['Total Detected Per Million'] = (np.round(1000000*df_map['Total Detected']/df_map['Population'], decimals = 2))
    df_map['Active Hospitalized Per Million'] = (np.round(1000000*df_map['Active Hospitalized']/df_map['Population'], decimals = 2))
    df_map['Cumulative Hospitalized Per Million'] = (np.round(1000000*df_map['Cumulative Hospitalized']/df_map['Population'], decimals = 2))
    df_map['Total Detected Deaths Per Million'] = (np.round(1000000*df_map['Total Detected Deaths']/df_map['Population'], decimals = 2))
    df_map = df_map.applymap(str)

    cols = get_cols()
    if (val is not None) and (val in cols) and  pop == 1:

        df_map.loc[:,'text'] = df_map['Country'] + '<br>' + \
                    'Total Detected ' + df_map['Total Detected'] + '<br>' + \
                    'Active ' + df_map['Active'] + '<br>' + \
                    'Active Hospitalized ' + df_map['Active Hospitalized'] + '<br>' + \
                    'Cumulative Hospitalized ' + df_map['Cumulative Hospitalized'] + '<br>' + \
                    'Total Detected Deaths ' + df_map['Total Detected Deaths']
        zval = df_map[val].astype(float)

    if (val is not None) and (val in cols) and  pop != 1:

        df_map.loc[:,'text'] = df_map['Country'] + '<br>' + \
                    'Total Detected Per Million ' + df_map['Total Detected Per Million'] + '<br>' + \
                    'Active Per Million ' + df_map['Active Per Million'] + '<br>' + \
                    'Active Hospitalized Per Million ' + df_map['Active Hospitalized Per Million'] + '<br>' + \
                    'Cumulative Hospitalized Per Million ' + df_map['Cumulative Hospitalized Per Million'] + '<br>' + \
                    'Total Detected Deaths Per Million ' + df_map['Total Detected Deaths Per Million']


        zval = df_map[val+ " Per Million"].astype(float)

    if (val is not None) and (val in cols):
        fig = go.Figure(data=go.Choropleth(
            locations=df_map['Country'],
            z= zval,
            locationmode="country names",
            autocolorscale=False,
            colorscale='inferno_r',
            text=df_map['text'], # hover text
            marker_line_color='black', # line markers between states
            colorbar_title='<br>'.join(wrap(''.join(['{}'.format(add_cases(val))]), width=10))
        ))

        fig.update_layout(
                margin=dict(l=10, r=10, t=50, b=50),
                title_text=add_cases('{} Predicted {} {}'.format(map_date.strftime('%b %d,%Y'), continent, val)),
                geo = dict(
                    scope= continent.lower() if continent is not None else None,
                    projection=go.layout.geo.Projection(type = 'natural earth'),
                    showlakes=True, # lakes
                    lakecolor='rgb(255, 255, 255)',
                    countrycolor='lightgray',
                    landcolor='whitesmoke',
                    showland=True,
                    showframe = False,
                    showcoastlines = True,
                    showcountries=True,
                    visible = False,
                ),
                modebar={
                    'orientation': 'v',
                    'bgcolor': 'rgba(0,0,0,0)',
                    'color': 'lightgray',
                    'activecolor': 'gray'
                }
            )

        graph = dcc.Graph(
            id='continent-projection-map',
            figure=fig,
        )

        return graph
    return
Esempio n. 3
0
def build_us_map(df_projections,PopInfo,map_date,val='Active', pop = 1):
    if map_date is None:
        return None

    if isinstance(map_date, str):
        map_date = datetime.datetime.strptime(map_date, '%Y-%m-%d').date()

    df_us = df_projections.loc[(df_projections.Country == "US") & (df_projections.Province != 'None')]
    df_map = df_us.loc[df_us['Day']==map_date]
    df_map = df_map.loc[df_us['Province']!='US']

    states = get_states()
    df_map.loc[:,'code'] = df_map.Province.apply(lambda x: states[x])
    population = np.array([])

    for i in df_map['Province']:
        pop_val = PopInfo.loc[PopInfo['Province']==i,'pop'].values
        population = np.concatenate((population, pop_val),0)

    df_map['Population'] =population
    df_map['Active Per Million'] = (np.round(1000000*df_map['Active']/df_map['Population'], decimals = 2))
    df_map['Total Detected Per Million'] = (np.round(1000000*df_map['Total Detected']/df_map['Population'], decimals = 2))
    df_map['Active Hospitalized Per Million'] = (np.round(1000000*df_map['Active Hospitalized']/df_map['Population'], decimals = 2))
    df_map['Cumulative Hospitalized Per Million'] = (np.round(1000000*df_map['Cumulative Hospitalized']/df_map['Population'], decimals = 2))
    df_map['Total Detected Deaths Per Million'] = (np.round(1000000*df_map['Total Detected Deaths']/df_map['Population'], decimals = 2))
    df_map = df_map.applymap(str)

    cols = get_cols()
    if (val is not None) and (val in cols) and pop == 1:

        df_map.loc[:,'text'] = df_map['Province'] + '<br>' + \
                    'Total Detected ' + df_map['Total Detected'] + '<br>' + \
                    'Active ' + df_map['Active'] + '<br>' + \
                    'Active Hospitalized ' + df_map['Active Hospitalized'] + '<br>' + \
                    'Cumulative Hospitalized ' + df_map['Cumulative Hospitalized'] + '<br>' + \
                    'Total Detected Deaths ' + df_map['Total Detected Deaths']

        z_val = df_map[val].astype(float)


    if (val is not None) and (val in cols) and pop != 1:

        df_map.loc[:,'text'] = df_map['Province'] + '<br>' + \
            'Total Detected Per Million ' + df_map['Total Detected Per Million'] + '<br>' + \
            'Active Per Million ' + df_map['Active Per Million'] + '<br>' + \
            'Active Hospitalized Per Million ' + df_map['Active Hospitalized Per Million'] + '<br>' + \
            'Cumulative Hospitalized Per Million ' + df_map['Cumulative Hospitalized Per Million'] + '<br>' + \
            'Total Detected Deaths Per Million ' + df_map['Total Detected Deaths Per Million']
        z_val =df_map[val+ " Per Million"].astype(float)

    if (val is not None) and (val in cols):
        fig = go.Figure(data=go.Choropleth(
            locations=df_map['code'],
            z=z_val,
            locationmode='USA-states',
            colorscale='inferno_r',
            autocolorscale=False,
            text=df_map['text'], # hover text
            marker_line_color='white' , # line markers between states
            colorbar_title='<br>'.join(wrap(''.join(['{}'.format(add_cases(val))]), width=10))
        ))


        fig.update_layout(
                margin=dict(l=10, r=10, t=50, b=50),
                title_text=add_cases('{} Predicted US {}'.format(map_date.strftime('%b %d,%Y'), val)),
                geo = dict(
                    scope='usa',
                    projection=go.layout.geo.Projection(type = 'albers usa'),
                    showlakes=True, # lakes
                    lakecolor='rgb(255, 255, 255)'
                ),
                modebar={
                    'orientation': 'v',
                    'bgcolor': 'rgba(0,0,0,0)',
                    'color': 'lightgray',
                    'activecolor': 'gray'
                }
            )

        graph = dcc.Graph(
            id='us-projection-map',
            figure=fig
        )
        return graph
    return
Esempio n. 4
0
def get_bottom_visual():
    df_projections = get_df_projections()
    cols = get_cols()

    bottom_visual = \
        [
            dbc.Row(
                [
                    dbc.Col(
                    [
                        html.H5('Use the tool below to explore our predictions for different locations.'),
                    ]
                    ),
                ],
            )
        ] + \
        [
            dbc.Row(
            [
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    dcc.Markdown("What value would you like to plot?"),
                                    dbc.Row(
                                        [
                                            dbc.Col(dcc.Markdown("**Predicted  \n Value:**"),width="auto"),
                                            dbc.Col(
                                                html.Div(
                                                    dcc.Dropdown(
                                                        id = 'predicted_timeline',
                                                        options = [{'label': add_cases(x), 'value': x} for x in cols.keys()],
                                                        value = ['Active'],
                                                        multi=True,
                                                    ),
                                                    id = "p2-transfer-dropdown-wrapper",
                                                ),
                                            width=True
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100",
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    html.Div(
                                        id = 'province-card-title',
                                        style={'paddingBottom':10}
                                    ),
                                    dbc.Row(
                                        [
                                            dbc.Col([dcc.Markdown("**Country:**"),dcc.Markdown("**Province / State:**")]),
                                            dbc.Col(
                                                html.Div(
                                                    [
                                                    dcc.Dropdown(
                                                            id = 'country_dropdown',
                                                            options = [{'label': x, 'value': x} for x in df_projections[df_projections['Continent'] == 'North America'].Country.unique()],
                                                    ),

                                                    dcc.Dropdown(
                                                            id = 'province_dropdown',
                                                    ),
                                                    html.Div(id = "p2-transfer-dropdown-wrapper"),

                                                ]),
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100",
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
            ],
            style={'marginBottom':20,'marginTop':20},
            )
        ] + \
        [
             dbc.Row(
             [
                   dbc.Col(
                   [
                         html.Div(
                             id = 'state_projection_graph',
                             children = [],
                             style={
                                 'width': '100%',
                                 'display': 'inline-block',
                                 'paddingTop': 20,
                                 }
                         ),
                    ]
                    )
              ],
              )
        ] + \
        [
             dbc.Row([
                dbc.Col(
                    html.Div(
                        html.A(
                            "Download the Data",
                            id="download-link",
                            href="https://raw.githubusercontent.com/COVIDAnalytics/website/master/data/predicted/Global.csv",
                        ),
                        style={'textAlign':"center"}
                    )
                ),
                ]
            ),
        ]
    return bottom_visual
Esempio n. 5
0
def get_top_visual():
    map_locations = [
        'US', "Europe", "Asia", "North America", "South America", "Africa",
        'World'
    ]
    cols = get_cols()
    today = pd.Timestamp('today')
    oneWeekFromNow = datetime.date.today() + datetime.timedelta(days=7)
    df_projections = pd.read_csv('data/predicted/Global.csv',
                                 sep=",",
                                 parse_dates=['Day'])
    df_projections.loc[:, 'Day'] = pd.to_datetime(df_projections['Day'],
                                                  format='y%m%d').dt.date
    df_projections = df_projections.loc[df_projections['Day'] >= today]

    top_visual = [
            dbc.Row(
            [
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    dcc.Markdown("For what date do you want to see projections?"),
                                    dbc.Row(
                                        [
                                            dbc.Col(
                                                html.Div(
                                                    dcc.DatePickerSingle(
                                                        id='us-map-date-picker-range',
                                                        min_date_allowed=today,
                                                        max_date_allowed=max(df_projections.Day.values),
                                                        date=oneWeekFromNow,
                                                        initial_visible_month=oneWeekFromNow,
                                                    ),
                                                    id="date-projections-picker-div"
                                                ),
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100"
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    dcc.Markdown("And for which area?"),
                                    dbc.Row(
                                        [
                                            dbc.Col(
                                                html.Div(
                                                    [
                                                    dcc.Dropdown(
                                                            id = 'location_map_dropdown',
                                                            options = [{'label': x, 'value': x} for x in map_locations],
                                                            value = 'US',
                                                            clearable=False
                                                    ),

                                                ]),
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100"
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
            ],
            style={'marginBottom':20,'marginTop':20},
            )
        ] + \
        [
            dbc.Row(
            [
                html.Div(
                    id='us-stats-title',
                    style={
                        'width': '100%',
                        'color': 'black',
                        'textAlign': 'center',
                        'fontSize': 30,
                        'fontWeight':'bold'
                        }
                ),
            ],
            )
        ] + \
        [
            dbc.Row(
            [
                build_card('us_tot_det'),
                build_card('us_tot_death'),
                build_card('us_active'),
                build_card('us_active_hosp')
            ],
            align="center"
            )
        ] + \
        [
            dbc.Row(
            [
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    dcc.Markdown('Predicted Value:'),
                                    dbc.Row(
                                        [
                                            dbc.Col(
                                                html.Div(
                                                    dcc.Dropdown(
                                                        id = 'us_map_dropdown',
                                                        options = [{'label': add_cases(x), 'value': x} for x in cols.keys()],
                                                        value = 'Total Detected',
                                                    ),

                                                    id="predicted-value-projections-picker-div",
                                                ),
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100"
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    dcc.Markdown("And for which population type?"),
                                    dbc.Row(
                                        [
                                            dbc.Col(
                                                html.Div(
                                                    [
                                                    dcc.RadioItems(
                                                    options=[
                                                        {'label': '  Full population', 'value': 1},
                                                        {'label': '  Value per million', 'value': 2}
                                                    ],
                                                    value=1,
                                                    id = 'radio_botton',
                                                    labelStyle={'display': 'inline-block',
                                                    'margin-right': '20px'
                                                                   },
                                                    style={'textAlign': 'center'}
                                                    ),

                                                ]),
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100"
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
            ],
            style={'marginBottom':20,'marginTop':20},
            )
        ]  + \
        [
            dbc.Row(
            [
                dbc.Col(
                [
                    html.Div(
                        id = 'map_projections',
                        children = [],
                    ),
                    html.P(
                            children = [],
                            style={'color':'gray'},
                            id='grey-countries-text'
                    ),
                ]
                ),

            ],
            )
        ]
    return top_visual
Esempio n. 6
0
def get_bottom_visual():
    north_america_countries = ['Canada', 'Costa Rica', 'Cuba', 'Dominican Republic',
       'El Salvador', 'Guatemala', 'Honduras', 'Mexico', 'Panama',
       'US']
    df_projections = get_df_projections()
    data_csv_string = df_projections.to_csv(index=False, encoding='utf-8')
    data_csv_string = "data:text/csv;charset=utf-8," + urllib.parse.quote(data_csv_string)
    cols = get_cols()

    bottom_visual = \
        [
            dbc.Row(
                [
                    dbc.Col(
                    [
                        html.H5('Use the tool below to explore our predictions for different locations.'),
                    ]
                    ),
                ],
            )
        ] + \
        [
            dbc.Row(
            [
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    dcc.Markdown("What value would you like to plot?"),
                                    dbc.Row(
                                        [
                                            dbc.Col(dcc.Markdown("**Predicted  \n Value:**"),width="auto"),
                                            dbc.Col(
                                                html.Div(
                                                    dcc.Dropdown(
                                                        id = 'predicted_timeline',
                                                        options = [{'label': add_cases(x), 'value': x} for x in cols.keys()],
                                                        value = ['Total Detected'],
                                                        multi=True,
                                                    ),
                                                    id = "p2-transfer-dropdown-wrapper",
                                                ),
                                            width=True
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100",
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
                dbc.Col(
                [
                    dbc.Card(
                        [
                            dbc.CardBody(
                                [
                                    html.Div(
                                        id = 'province-card-title',
                                        style={'paddingBottom':10}
                                    ),
                                    dbc.Row(
                                        [
                                            dbc.Col([dcc.Markdown("**Country:**"),dcc.Markdown("**Province / State:**")]),
                                            dbc.Col(
                                                html.Div(
                                                    [
                                                    dcc.Dropdown(
                                                            id = 'country_dropdown',
                                                            options = [{'label': x, 'value': x} for x in north_america_countries],
                                                    ),

                                                    dcc.Dropdown(
                                                            id = 'province_dropdown',
                                                    ),
                                                    html.Div(id = "p2-transfer-dropdown-wrapper"),

                                                ]),
                                            ),
                                        ]
                                    ),
                                ],
                            ),
                        ],
                        className="projections-general-card h-100",
                    ),
                ],
                xs=12,
                sm=12,
                md=6,
                lg=6,
                ),
            ],
            style={'marginBottom':20,'marginTop':20},
            )
        ] + \
        [
             dbc.Row(
             [
                   dbc.Col(
                   [
                         html.Div(
                             id = 'state_projection_graph',
                             children = [],
                             style={
                                 'width': '100%',
                                 'display': 'inline-block',
                                 'paddingTop': 20,
                                 }
                         ),
                    ]
                    )
              ],
              )
        ] + \
        [
             dbc.Row([
                dbc.Col(
                    html.Div(
                        html.A(
                            "Download Most Recent Predictions",
                            id="download-link",
                            download="covid_analytics_projections.csv",
                            href=data_csv_string,
                            target="_blank"
                        ),
                        style={'textAlign':"center"}
                    )
                ),
                ]
            ),
        ]
    return bottom_visual
Esempio n. 7
0
        dbc.Row(
        [
            dbc.Col(
            [
                dbc.Card(
                    [
                        dbc.CardBody(
                            [
                                dcc.Markdown('Predicted Value:'),
                                dbc.Row(
                                    [
                                        dbc.Col(
                                            html.Div(
                                                dcc.Dropdown(
                                                    id = 'us_map_dropdown',
                                                    options = [{'label': add_cases(x), 'value': x} for x in cols.keys()],
                                                    value = 'Active',
                                                ),

                                                id="predicted-value-projections-picker-div",
                                            ),
                                        ),
                                    ]
                                ),
                            ],
                        ),
                    ],
                    className="projections-general-card h-100"
                ),
            ],
            xs=12,