Beispiel #1
0
def generate_world_ts_options(resources=resources,
                              plot_confirmed=True,
                              plot_recovered=True,
                              plot_deaths=True):
    df = df_from_path(resources['worldwide-aggregated'])
    date = df['Date'].tolist()
    confirmed = df['Confirmed'].tolist()
    recovered = df['Recovered'].tolist()
    deaths = df['Deaths'].tolist()

    fig = go.Figure()

    if plot_confirmed:
        fig.add_trace(
            go.Scatter(x=date,
                       y=confirmed,
                       name='Cases',
                       line=dict(color=CONFIRMED_COLOUR, width=2)))
    if plot_recovered:
        fig.add_trace(
            go.Scatter(x=date,
                       y=recovered,
                       name='Recoveries',
                       line=dict(color=RECOVERED_COLOUR, width=2)))
    if plot_deaths:
        fig.add_trace(
            go.Scatter(x=date,
                       y=deaths,
                       name='Deaths',
                       line=dict(color=DEATHS_COLOUR, width=2)))

    fig.update_layout(title={
        'text': f"<b>Global confirmed numbers over time</b>",
        'x': 0.5,
        'xanchor': 'center',
        'y': 0.95,
    },
                      margin={
                          "r": 10,
                          "t": 40,
                          "l": 10,
                          "b": 5
                      },
                      font=dict(
                          family=FONT,
                          size=12,
                      ),
                      xaxis_title='Date',
                      yaxis_title='Confirmed numbers',
                      hovermode='x')

    fig.update_yaxes(nticks=10)
    fig.update_xaxes(nticks=10)

    return fig
Beispiel #2
0
def generate_datatable(
        df=df_from_path(resources['countries-aggregated']), date=False):
    if not date:
        date = df.iloc[len(df) - 1, 0]
    df = filter_df(df, "Date", date)

    df = df[['Country', 'Confirmed', 'Recovered', 'Deaths']]

    for i in range(len(df)):
        if len(df.iloc[i][0]) > 15:
            df.at[df.iloc[i].name, 'Country'] = f"{df.iloc[i][0][:12]}..."

    return df
Beispiel #3
0
def generate_comparable_time_series(plot="Confirmed",
                                    countries=[
                                        "China", "United Kingdom", "Italy",
                                        "Spain", "Iran", "US", "Korea, South"
                                    ],
                                    df=ts_df,
                                    xth=100):

    countries = [
        "China", "United Kingdom", "Italy", "Spain", "Iran", "US",
        "Korea, South"
    ]
    df = df_from_path(resources['countries-aggregated'])
    xth = 100

    fig = go.Figure()

    if plot == "Confirmed":
        plot_word = "case"
        plural_plot_word = "cases"
    elif plot == "Recovered":
        plot_word = "recovery"
        plural_plot_word = "recoveries"
    elif plot == "Deaths":
        plot_word = "death"
        plural_plot_word = "deaths"
    else:
        raise ValueError(
            f"'plot' variable must be equal to 'Confirmed', 'Recovered' or 'Deaths'. Your input was '{plot}'"
        )

    for country in countries:

        base_date = xth_date(country, xth, data=plural_plot_word, df=df)
        country_df = df.loc[df["Country"] == country]
        try:
            country_df = country_df[country_df['Date'] >= base_date]
        except:
            continue

        x_axis_data = []
        y_axis_data = country_df[plot].tolist()

        for i in range(len(y_axis_data)):
            x_axis_data.append(i)

        fig.add_trace(
            go.Scatter(x=x_axis_data,
                       y=y_axis_data,
                       name=country,
                       mode='lines'))

    fig.update_layout(title={
        'text':
        f"<b>Confirmed {plural_plot_word} over time (day 0 = {xth:,} {plural_plot_word})</b>",
        'x': 0.5,
        'xanchor': 'center',
    },
                      margin={
                          "r": 10,
                          "t": 30,
                          "l": 10,
                          "b": 0
                      },
                      font=dict(
                          family=FONT,
                          size=12,
                      ),
                      xaxis_title=f'Days since {xth}th {plot_word}',
                      yaxis_title=f'Confirmed {plural_plot_word}',
                      hovermode='x')

    fig.update_yaxes(nticks=10)
    fig.update_xaxes(nticks=10)

    return fig
Beispiel #4
0
def generate_deathrates_by_country(resources=resources,
                                   max_rows=30,
                                   min_cases=1000,
                                   min_deaths=200,
                                   date=False):
    df = df_from_path(resources['countries-aggregated'])

    if not date:
        date = df.iloc[len(df) - 1, 0]
    df = filter_df(df, "Date", date)

    x_data = []
    y_data = []
    y_label = []

    for location in range((len(df))):
        location_name = df.iloc[location][1]
        if len(location_name.split(" ")) > 1 and len(location_name) > 13:
            location_abbv = ""
            for word in location_name.split(" "):
                location_abbv += word[0]
            location_name = location_abbv

        cases = df.iloc[location][2]
        if cases == 0:
            cases += 1
        deaths = df.iloc[location][4]

        if cases > min_cases and deaths > min_deaths:
            death_rate = deaths / cases
            x_data.append(death_rate)
            y_data.append(location_name)
            y_label.append(f"{death_rate*100:.1f}% ({deaths:,}/{cases:,})")

    num_rows = min(max_rows, len(x_data))

    colours = []
    for row in range(num_rows):
        colours.append(
            f'rgb(255,{230-128*(row/num_rows)},{230-128*(row/num_rows)})')

    total_cases = df.sum(axis=0)[2]
    total_deaths = df.sum(axis=0)[4]
    average_death_rate = total_deaths / total_cases
    x_data.append(average_death_rate)
    y_data.append("Average")
    y_label.append(
        f"{average_death_rate*100:.1f}% ({total_deaths:,}/{total_cases:,})")

    x_data, y_data, y_label = (list(t) for t in zip(
        *sorted(zip(x_data, y_data, y_label))))

    x_data = x_data[len(x_data) - max_rows:len(x_data)]
    y_data = y_data[len(y_data) - max_rows:len(y_data)]
    y_label = y_label[len(y_label) - max_rows:len(y_label)]

    try:
        colours[y_data.index('Average')] = 'rgba(168, 102, 255, 0.8)'
    except:
        print(f"Average not in top {num_rows}")

    fig = go.Figure()
    fig.add_trace(
        go.Bar(x=y_data[::-1],
               y=x_data[::-1],
               text=y_label[::-1],
               name="Death rate summary",
               orientation='v',
               marker=dict(color=colours[::-1],
                           line=dict(color='rgba(38, 24, 74, 0.8)', width=1))))

    fig.update_yaxes(range=[0, 0.2])

    fig.update_layout(title={
        'text':
        f"<b>Death rates ({min_cases}+ cases, {min_deaths}+ deaths)</b>",
        'x': 0.5,
        'y': 0.95,
        'xanchor': 'center',
    },
                      barmode='stack',
                      margin={
                          "r": 15,
                          "t": 40,
                          "l": 10,
                          "b": 10,
                          "pad": 5
                      },
                      width=1 * num_rows,
                      yaxis=dict(tickformat=".1%", ),
                      xaxis_tickangle=-90,
                      font=dict(
                          family=FONT,
                          size=12,
                      ))

    fig.update_yaxes(tickfont=dict(size=12), )
    return fig
Beispiel #5
0
    "toImage", "resetViews", "resetViewMapbox", "resetGeo", "resetScale2d"
],
                           hide_buttons=TOOLBAR_BUTTONS):
    for button in buttons:
        hide_buttons.remove(button)
    return hide_buttons


CHOSEN_BUTTONS = add_buttons_to_default()

MINIMALIST_CONFIG = {
    "displaylogo": False,
    "modeBarButtonsToRemove": CHOSEN_BUTTONS,
}

headline_df = df_from_path(resources['worldwide-aggregated'])
current_date = headline_df.iloc[len(headline_df) - 1][0]
current_confirmed = headline_df.iloc[len(headline_df) - 1][1]
current_recovered = headline_df.iloc[len(headline_df) - 1][2]
current_deaths = headline_df.iloc[len(headline_df) - 1][3]
prev_date = headline_df.iloc[len(headline_df) - 2][0]
prev_confirmed = headline_df.iloc[len(headline_df) - 2][1]
prev_recovered = headline_df.iloc[len(headline_df) - 2][2]
prev_deaths = headline_df.iloc[len(headline_df) - 2][3]
confirmed_growth = (current_confirmed / prev_confirmed) - 1
recovered_growth = (current_recovered / prev_recovered) - 1
deaths_growth = (current_deaths / prev_deaths) - 1


def formatted_mvmt(figure, text):
    if figure >= 0:
Beispiel #6
0
MINIMALIST_CONFIG ={
    "displaylogo": False,
    "modeBarButtonsToRemove": CHOSEN_BUTTONS,
}

# if we currently have no resources, get them
try:
    resources
    current_api
except NameError:
    resources, current_api = get_resources(DATA_SOURCE)

try:
    headline_df
except NameError:
    headline_df = df_from_path(resources['worldwide-aggregate'])

try:
    ts_df
except NameError:
    ts_df = df_from_path(resources['countries-aggregated'])

try:
    df2
except NameError:
    df2 = get_df(current_api)

try:
    ref_table
except NameError:
    ref_table = df_from_path(resources['reference'])