Ejemplo n.º 1
0
def calculate_trends():
    output = {'case7': {}, 'death': {}, 'case10': {}}
    si = state_info()
    print("Average case doubling time in days for past 7 days of data")
    for state in si.get_states():
        data = state_historic_data(state)
        latest_data = data.get_latest_n(7)
        fit = case_growth_rate(latest_data)
        output['case7'][str(state)] = str(doubling_time(fit))
        print(state + "," + str(doubling_time(fit)))

    print("Average death doubling time in days for past 7 days of data")
    for state in si.get_states():
        data = state_historic_data(state)
        latest_data = data.get_latest_n(7)
        fit = death_growth_rate(latest_data)
        output["death"][str(state)] = str(doubling_time(fit))
        print(state + "," + str(doubling_time(fit)))

    print("Average case doubling time in days since 10th confirmed case")
    for state in si.get_states():
        data = state_historic_data(state)
        latest_data = data.get_after_n_cases(10)
        fit = death_growth_rate(latest_data)
        output["case10"][str(state)] = str(doubling_time(fit))
        print(state + "," + str(doubling_time(fit)))

    return output
Ejemplo n.º 2
0
def plot_states_growth(states,
                       data_name="positive",
                       logarithmic=True,
                       threshold=100,
                       filename=None):
    index = 0
    si = state_info()
    max_days = 0
    max_data = 0
    for state in states:
        data_handle = state_historic_data(state)
        data_points = data_handle.get_after_n_cases(threshold)

        dates = range(len(data_points))
        data = list(map(lambda x: x[data_name], data_points))

        if max_days < len(data_points):
            max_days = len(data_points)
        if max_data < max(data):
            max_data = max(data)

        plt.plot(dates,
                 data,
                 "C" + str(index) + ".-",
                 label=si.get_name(state))
        index += 1

    for double_time in [1, 2, 3, 5, 10]:
        d = max_days
        c = threshold * (2**(max_days / double_time))
        if c > max_data:
            c = max_data
            d = (numpy.log(c / threshold) / numpy.log(2)) * double_time
        plt.plot([0, d], [threshold, c], color='#bbbbbb', linestyle="--")

    if logarithmic:
        ax = plt.gca()
        ax.set_yscale("log")
    plt.legend()
    plt.title(
        "Growth Rate Since 100th Case\n(dotted lines represent doubling every 1, 2, 3, 5, and 10 days)"
    )
    plt.ylabel("Cases")
    plt.xlabel("Days Since 100th Case")
    fig = plt.gcf()
    fig.set_size_inches(10, 7)
    plt.grid(b=True)
    if filename is None:
        plt.show()
    else:
        fig.savefig("output/" + filename, dpi=100)
Ejemplo n.º 3
0
def plot_mortality_rate(states, threshold=2.5, filename=None, days=0):
    index = 0
    si = state_info()
    for state in states:
        data_handle = state_historic_data(state)
        if days == 0:
            data_points = data_handle.data
        else:
            data_points = data_handle.get_latest_n(days)

        dates = list(
            map(lambda x: datetime.strptime(str(x['date']), "%Y%m%d").date(),
                data_points))
        death = numpy.array(
            list((map(
                lambda x: 0
                if ('death' not in x or x['death'] is None) else x['death'],
                data_points))))
        positive = numpy.array(
            list((map(
                lambda x: 1 if ('positive' not in x or x['positive'] is None)
                else x['positive'], data_points))))
        mortality_rate = death / positive * 100
        plt.plot(dates,
                 mortality_rate,
                 "C" + str(index) + ".-",
                 label=si.get_name(state))
        index += 1
    plt.legend()
    plt.title("Mortality Rate Trend by State")
    plt.ylabel("% of positive cases resulting in death")
    plt.axhline(y=threshold, color='r', linestyle='--')
    fig = plt.gcf()
    fig.set_size_inches(10, 7)
    plt.grid(b=True, which="both")
    if filename is None:
        plt.show()
    else:
        fig.savefig("output/" + filename, dpi=100)
Ejemplo n.º 4
0
                                     str(social_distancing_factor * 100) +
                                     "% social distancing",
                                     plot_color="C0")
    mpl.title(
        "KY Predicted infections over time based on death count, assuming %.1f%% mortality\nUsing SIR model, β=%0.2f, γ=0.15"
        % (mortality_rate * 100, b))
    mpl.grid(b=True)
    if logarithmic:
        mpl.gca().set_yscale("log")
    mpl.show()
    mpl.close()


if __name__ == "__main__":
    state = "KY"
    si = state_info()
    pop = si.get_population(state)
    data = state_historic_data(state).get_latest_n(45)
    bootstrap_date = datetime.datetime.strptime(str(data[0]['date']),
                                                '%Y%m%d').date()
    positive = list(
        map(
            lambda x: 0
            if 'positive' not in x or x['positive'] is None else x['positive'],
            data))
    death = list(
        map(
            lambda x: 0
            if 'death' not in x or x['death'] is None else x['death'], data))
    recovered = list(
        map(
Ejemplo n.º 5
0
def plot_states_trend(states,
                      data_name='positive',
                      trendline=True,
                      logarithmic=True,
                      pop_adjusted=False,
                      days=0,
                      filename=None):
    index = 0
    si = state_info()
    for state in states:
        data_handle = state_historic_data(state)
        if days > 0:
            data_points = data_handle.get_latest_n(days)
        else:
            data_points = data_handle.get_after_n_cases(1)

        dates = list(
            map(lambda x: datetime.strptime(str(x['date']), "%Y%m%d").date(),
                data_points))
        cases = list(
            map(
                lambda x: x[data_name]
                if data_name in x and x[data_name] is not None else 0,
                data_points))

        if pop_adjusted:
            population = si.get_population(state)
            cases = list(map(lambda x: x / population * 10000, cases))

        (r, a) = calculate_trends.weighted_exponential_fit(
            numpy.arange(len(cases)), cases)
        if trendline:
            fit_y = numpy.exp(a) * numpy.exp(r * numpy.arange(len(cases)))
            plt.plot(dates, fit_y, "C" + str(index) + "--")
            plt.plot(dates,
                     cases,
                     "C" + str(index) + "o",
                     label=si.get_name(state) + " (rate=%0.4f)" % r)
        else:
            plt.plot(dates,
                     cases,
                     "C" + str(index) + ".-",
                     label=si.get_name(state) + " (rate=%0.4f)" % r)
        index += 1

    if logarithmic:
        ax = plt.gca()
        ax.set_yscale("log")
    plt.legend()
    title = "Trend in " + data_name.capitalize() + " Count by State, "
    if days == 0:
        title += "All Days"
    else:
        title += "Last " + str(days) + " Days"
    if pop_adjusted:
        title += " (population adjusted)"
    plt.title(title)
    ylabel = data_name + " count"
    if pop_adjusted:
        ylabel += " per 10,000 people"
    plt.ylabel(ylabel)
    fig = plt.gcf()
    fig.set_size_inches(10, 7)
    plt.grid(b=True)
    if filename is None:
        plt.show()
    else:
        fig.savefig("output/" + filename, dpi=100)
Ejemplo n.º 6
0
def update_data():
    si = state_info()
    for state in si.get_states():
        data = get_state_historic(state)
        save_data(state + "_historic", data)