예제 #1
0
def plot_linechart(df, xcol, ycol, ax=None, title="", show_peak=True):
    ax = df.plot(x=xcol,
                 y=[ycol],
                 title=title,
                 figsize=(8, 5),
                 grid=True,
                 ax=ax,
                 lw=3)
    mn_l = DayLocator()
    ax.xaxis.set_minor_locator(mn_l)
    mj_l = AutoDateLocator()
    mj_f = ConciseDateFormatter(mj_l, show_offset=False)
    ax.xaxis.set_major_formatter(mj_f)
    ax.legend(loc=2)

    if show_peak:
        # plot peak in agg
        peakx = df[ycol].idxmax()
        peak = df.loc[peakx]
        peak_desc = peak[xcol].strftime("%d-%b") + "\n" + str(int(peak[ycol]))
        _ = ax.annotate(peak_desc,
                        xy=(peak[xcol] + dt.timedelta(days=1), peak[ycol]),
                        xytext=(peak[xcol] + dt.timedelta(days=45),
                                peak[ycol] * .9),
                        arrowprops={},
                        bbox={'facecolor': 'white'})
        _ = ax.axvline(x=peak[xcol], linewidth=1, color='r')

    return ax
예제 #2
0
def gen_temp_plot(temperature, maximums, minimums, timestamps):
    # create figure with one axes
    fig, ax = plt.subplots()

    # prettify
    ax.xaxis.set_major_formatter(ConciseDateFormatter(AutoDateLocator()))
    fig.autofmt_xdate(rotation=40)
    fig.tight_layout()

    # plot all the data at one axes
    ax.plot(timestamps, temperature, 'ko')
    ax.plot(timestamps, maximums, 'r')
    ax.plot(timestamps, minimums, 'b')

    # create a bytes stream
    img = io.BytesIO()
    # save figure to stream
    plt.savefig(img, format='png')
    img.seek(0)

    # encode bytes-like object using base64, than decode binary data
    plot_url = base64.b64encode(img.getvalue()).decode()
    # close bytes stream
    img.close()

    return plot_url
예제 #3
0
def make_graph_cumm(df, what_to_display):
    with _lock:
        fig1yza, ax = subplots()
        ax3 = ax.twinx()
        ax.set_title('Cummulative cases and growth COVID-19 à la Levitt')
        ax.scatter(df["date"],
                   df["what_to_display_cumm"].values,
                   color="green",
                   s=1,
                   label=f"reality {what_to_display}")
        ax.scatter(df["date"],
                   df["what_to_display_predicted_cumm"].values,
                   color="#00b3b3",
                   s=1,
                   label=f"predicted {what_to_display}")

        ax3.scatter(df["date"],
                    df["real_growth"].values,
                    color="#b300b3",
                    s=1,
                    label="real growth")
        ax3.scatter(df["date"],
                    df["predicted_growth"].values,
                    color="#b3bbb3",
                    s=1,
                    label="predicted growth")
        ax.set_xlim(df.index[0], df.index[-1])
        ax.yaxis.set_major_formatter(
            StrMethodFormatter('{x:,.0f}'))  # comma separators
        ax.grid()
        ax.legend(loc="upper left")
        ax.xaxis.set_major_formatter(
            ConciseDateFormatter(AutoDateLocator(), show_offset=False))
        st.pyplot(fig1yza)
예제 #4
0
    def _get_formatter(self, locator, formatter, concise):

        if formatter is not None:
            return formatter

        if concise:
            # TODO ideally we would have concise coordinate ticks,
            # but full semantic ticks. Is that possible?
            formatter = ConciseDateFormatter(locator)
        else:
            formatter = AutoDateFormatter(locator)

        return formatter
예제 #5
0
파일: scales.py 프로젝트: labaran1/seaborn
    def format(
        self,
        formater: Formatter | None = None,
        *,
        concise: bool = False,
    ) -> Temporal:

        # TODO ideally we would have concise coordinate ticks,
        # but full semantic ticks. Is that possible?
        if concise:
            major_formatter = ConciseDateFormatter(self._major_locator)
        else:
            major_formatter = AutoDateFormatter(self._major_locator)
        self._major_formatter = major_formatter

        return self
예제 #6
0
def make_plot(date_, rate_, show=True, save=False, plot_filename = 'plot.pdf', graphWidth=800, graphHeight=600):
    locator=AutoDateLocator()
    myFmt = ConciseDateFormatter(locator, formats=['%Y', '%b', '%d', '%H:%M', '%H:%M', '%S.%f'])
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    pd.plotting.register_matplotlib_converters()
    axes = f.add_subplot(111)
    axes.plot(date_, rate_)
    axes.set_xlabel('Date')
    axes.set_ylabel('Rubles per Euro')
    plt.gca().xaxis.set_major_formatter(myFmt)
    if show:
        plt.show()
    if save:
        f.savefig(plot_filename)
    plt.close('all')
    return plot_filename
예제 #7
0
class Grafica(FigureCanvasQTAgg, FigureCanvas):
    autox = AutoDateLocator()
    formatterx = ConciseDateFormatter(autox)
    formatterx.formats = [
        '%y',  # ticks are mostly years
        '%b',  # ticks are mostly months
        '%d',  # ticks are mostly days
        '%H:%Mh',  # hrs
        '%H:%Mh',  # min
        '%M:%S',
    ]  # secs

    def formatter(x, pos):
        hours, remainder = divmod(x, 3600)
        minutes, seconds = divmod(remainder, 60)
        if x < 3600:
            format_string = "{:02}'{:02}\"".format(int(minutes), int(seconds))
        else:
            format_string = "{:02}:{:02}'{:02}\"".format(
                int(hours), int(minutes), int(seconds))
        return format_string

    funcformatter = ticker.FuncFormatter(formatter)

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        PacientInterface.__init__(self)
        fig = Figure(figsize=(width, height), dpi=dpi)
        # self.axes = fig.add_subplot(111)
        self.fig, self.ax = pyplot.subplots()
        self.line_lap0 = None
        self.line_lap1 = None
        self.line_lap2 = None
        self.line_total = None
        self.markers0 = None
        self.markers1 = None
        self.markers2 = None
        self.markers_total = None
        self.text0 = None
        self.text1 = None
        self.text2 = None
        super(Grafica, self).__init__(self.fig)
        FigureCanvas.__init__(self, self.fig)
        FigureCanvasQTAgg.updateGeometry(self)
        self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
예제 #8
0
def data_timeplot(data, plot_per_line=5):
    # focntion permettant de représenter les séries temporelles individuelles
    # pour chacune des colonnes du dataset passer en argument
    # distribue les graphiques par défaut 5 par ligne

    paris = pytz.timezone('Europe/Paris')
    locator = AutoDateLocator()
    formatter = ConciseDateFormatter(locator, tz=paris)
    features = data.columns
    ppl = plot_per_line

    fig = plt.figure(figsize=(30, ppl * (len(features) - 1) // ppl + ppl))

    for i, c in enumerate(features):
        ax = fig.add_subplot((len(features) - 1) / ppl + 1, ppl, i + 1)
        ax.plot_date(data.index, data[c], marker=None, linestyle='-');
        ax.xaxis.set_major_locator(locator)
        ax.xaxis.set_major_formatter(formatter)
        ax.xaxis.set_tick_params(rotation=30, labelsize=10)
        ax.set_title(c, fontsize=10)
예제 #9
0
def countrywise(model, cp, df, country='India', plot=True):
    c = country
    pop_fct = df.loc[df.location == c, 'population'].iloc[0] / 1000
    ip_aux = torch.tensor(np.array(
        df.loc[df.location == c, cp['config']['DS']['AUX_FEATURES']].iloc[0])[
            cp['config']['AUX_FEATURES']],
                          dtype=torch.float32).to(DEVICE)

    IP_SEQ_LEN = cp['config']['DS']['IP_SEQ_LEN']
    OP_SEQ_LEN = cp['config']['DS']['OP_SEQ_LEN']

    all_preds = []
    pred_vals = []
    real_vals = []

    test_data = np.array(df.loc[(df.location == c) & (df.total_cases >= 100),
                                cp['config']['DS']['FEATURES']].rolling(
                                    7, center=True, min_periods=1).mean() /
                         pop_fct,
                         dtype=np.float32)

    ip_shp = IP_SEQ_LEN, len(cp['config']['IP_FEATURES'])
    op_shp = OP_SEQ_LEN, len(cp['config']['OP_FEATURES'])

    for i in range(len(test_data) - IP_SEQ_LEN - OP_SEQ_LEN + 1):
        ip = torch.tensor(test_data[i:i + IP_SEQ_LEN,
                                    cp['config']['IP_FEATURES']])
        op = torch.tensor(test_data[i + IP_SEQ_LEN:i + IP_SEQ_LEN + OP_SEQ_LEN,
                                    cp['config']['OP_FEATURES']])
        ip = ip.to(DEVICE)
        op = op.to(DEVICE)

        pred = model.predict(ip.view(1, IP_SEQ_LEN,
                                     len(cp['config']['IP_FEATURES'])),
                             aux_ip=ip_aux.view(
                                 1, len(cp['config']['AUX_FEATURES'])))

        all_preds.append(pred.view(op_shp).cpu().numpy() * pop_fct)
        pred_vals.append(pred.view(op_shp).cpu().numpy()[0] * pop_fct)
        real_vals.append(op.view(op_shp).cpu().numpy()[0] * pop_fct)

    # prepend first input
    nans = np.ndarray((IP_SEQ_LEN, len(cp['config']['OP_FEATURES'])))
    nans.fill(np.NaN)
    pred_vals = list(nans) + pred_vals
    real_vals = list(test_data[:IP_SEQ_LEN, cp['config']['OP_FEATURES']] *
                     pop_fct) + real_vals
    # append last N-1 values
    nans = np.ndarray(op_shp)
    nans.fill(np.NaN)
    pred_vals.extend(nans[1:])  # pad with NaN
    real_vals.extend(op.view(op_shp).cpu().numpy()[1:] * pop_fct)

    real_vals = np.array(real_vals).reshape(len(test_data),
                                            len(cp['config']['OP_FEATURES']))
    pred_vals = np.array(pred_vals).reshape(len(test_data),
                                            len(cp['config']['OP_FEATURES']))

    accs = []
    for o in range(len(cp['config']['OP_FEATURES'])):
        cmp_df = pd.DataFrame({
            'actual': real_vals[:, o],
            'predicted0': pred_vals[:, o]
        })
        # set date
        start_date = df.loc[(df.location == c)
                            & (df.total_cases >= 100)]['date'].iloc[0]
        end_date = start_date + dt.timedelta(days=cmp_df.index[-1])
        cmp_df['Date'] = pd.Series(
            [start_date + dt.timedelta(days=i) for i in range(len(cmp_df))])

        # plot noodles
        ax = None
        i = IP_SEQ_LEN
        mape = []
        for pred in all_preds:
            cmp_df['predicted_cases'] = np.NaN
            cmp_df.loc[i:i + OP_SEQ_LEN - 1, 'predicted_cases'] = pred[:, o]
            ape = np.array(
                100 * ((cmp_df['actual'] - cmp_df['predicted_cases']).abs() /
                       cmp_df['actual']))
            mape.append(ape[~np.isnan(ape)])
            if plot:
                ax = cmp_df.plot(x='Date',
                                 y='predicted_cases',
                                 ax=ax,
                                 legend=False)
            i += 1

        total_acc = np.zeros(OP_SEQ_LEN)
        for m in mape:
            total_acc += m
        elwise_mape = total_acc / len(mape)
        if plot: print("Day wise accuracy:", 100 - elwise_mape)
        acc = 100 - sum(elwise_mape) / len(elwise_mape)
        accs.append(acc)
        acc_str = f"{acc:0.2f}%"

        # plot primary lines
        if plot:
            ax = cmp_df.plot(x='Date',
                             y=['actual', 'predicted0'],
                             figsize=(20, 8),
                             lw=5,
                             title=c + ' | Daily predictions | ' + acc_str,
                             ax=ax)
            mn_l = DayLocator()
            ax.xaxis.set_minor_locator(mn_l)
            mj_l = AutoDateLocator()
            mj_f = ConciseDateFormatter(mj_l, show_offset=False)
            ax.xaxis.set_major_formatter(mj_f)
    return accs
예제 #10
0
# set locator (where ticks/gridlines occur) and formatter (their labels)
# for y axis

# major: logistic positions for ticks
ax.yaxis.set_major_locator(LogitLocator())
ax.yaxis.set_major_formatter(PercentFormatter(xmax=1.0, decimals=1))
# minor: no labels (NullFormater) but in between gridlines
ax.yaxis.set_minor_locator(AutoLogitMinorLocator())
ax.yaxis.set_minor_formatter(NullFormatter())

# for x axis
# WeekdayLocator: mark every week at second day (Wednesday)
# ConciseDateFormatter: minimalist/generalist formatter
xmaj_locator = WeekdayLocator(interval=1, byweekday=2)
xmaj_formatter = ConciseDateFormatter(xmaj_locator)
ax.xaxis.set_major_locator(xmaj_locator)
ax.xaxis.set_major_formatter(xmaj_formatter)
# gridline for every day
ax.xaxis.set_minor_locator(DayLocator(interval=1))

# no ticks marks for minor ticks
ax.tick_params(which='minor', length=0, width=0)
# set fontsize and color of major tick labels
ax.tick_params(labelsize=10 * FONT_SCALE, labelcolor=LABEL_COL)

# activate and format gridline
ax.grid(True,
        which='major',
        axis='both',
        linestyle='-',
def do_levitt(df, what_to_display, df_complete, show_from):
    # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7325180/#FD4
    # https://docs.google.com/spreadsheets/d/1MNXQTFOLN-bMDAyUjeQ4UJR2bp05XYc8qpLsyAqdrZM/edit#gid=329426677
    # G(T)=N/e=0.37N.

    # G(t) = N exp(− exp(− (t − T)/ U)),
    # U is time constant, measured in days

    # . Parameter N is the asymptotic number, the maximum plateau value that G(t) reaches after a long time, t.
    # Parameter T, is the point of inflection, which is the time in days at which the second-derivative of G(t)
    #  is zero and its first derivative is a maximum. It is a natural mid-point of the function where the value
    # of G(T)=N/e=0.37N. The Parameter U, is the most important as it changes the shape of the curve; it is a
    #  time-constant measured in days.

    df, last_value, background_new_cases, background_total_cases = add_column_levit(
        df, what_to_display)
    #print (df)
    df = df.set_index(DATEFIELD)
    firstday = df.index[0] + Timedelta('1d')
    nextday = df.index[-1] + Timedelta('1d')
    lastday = df.index[-1] + Timedelta(TOTAL_DAYS_IN_GRAPH - len(df),
                                       'd')  # extrapolate

    #yd = df['Deceased_cumm'].values # dependent
    exrange = range(
        (Timestamp(nextday) - Timestamp(firstday)) // Timedelta('1d'),
        (Timestamp(lastday) + Timedelta('1d') - Timestamp(firstday)) //
        Timedelta('1d'))  # day-of-year ints
    indates = date_range(df.index[0], df.index[-1])
    exdates = date_range(nextday, lastday)
    alldates = date_range(df.index[0], df.index[-1])
    len_original = len(indates)
    len_total = int(TOTAL_DAYS_IN_GRAPH)

    t = np.linspace(0.0, TOTAL_DAYS_IN_GRAPH, 10000)
    r_sq_opt = 0

    optimim = st.sidebar.selectbox("Find optimal period for trendline",
                                   [True, False],
                                   index=0)

    #x_values = list(range(2, len(df)+1))
    x_values = list(range(0, len(df) + 1))
    y_values = df["log_exp_gr_factor"].to_list()
    if optimim == True:
        # we search for the optimal value for how many days at the end we take to draw the trendline
        for i in range(
                2, 10
        ):  # we start a bit later, because the first values have an R_sq = 1

            x = x_values[-i:]
            y = y_values[-i:]

            df_ = df.tail(i)
            m_, b_, r_sq_ = find_slope_scipy(x, y)
            if r_sq_ > r_sq_opt:
                m, b, r_sq, i_opt = m_, b_, r_sq_, i
                r_sq_opt = r_sq
        st.write(f"Optimal R SQ if I = {i_opt}")
    else:
        x_range = np.arange(len(df))
        m, b, r_sq = find_slope_sklearn(df, x_range, "log_exp_gr_factor")

    # Number of months to extend
    extend = len(exdates)

    # Extrapolate the index first based on original index
    df = pd.DataFrame(data=df,
                      index=pd.date_range(start=df.index[0],
                                          periods=len(df.index) + extend,
                                          freq=df.index.freq))
    df['rownumber'] = np.arange(len(df))
    alldates = date_range(df.index[0], df.index[-1])
    #st.write (f"LENGTE DF {len(df)}")

    df_complete["new_cases_smoothed_original"] = df_complete[
        "new_cases_smoothed"]
    df_complete["date_original"] = df_complete["date"]
    df_complete = df_complete[["date_original", "new_cases_smoothed_original"]]
    mask = (df_complete["date_original"].dt.date >= show_from)
    df_complete = df_complete.loc[mask]
    df__complete_as_str = df_complete.astype(str)
    # st.write(df__complete_as_str)
    # st.write(len(df_complete))
    df = df.reset_index()
    df["date"] = pd.to_datetime(df["index"], format="%Y-%m-%d")
    df = pd.merge(
        df,
        df_complete,
        how="outer",
        left_on="date",
        right_on="date_original",
        #left_index=True,
    )

    df = df.set_index("date")
    df_as_str = df.astype(str)
    #st.write(df_as_str)

    x = ((df.index - Timestamp('2020-01-01'))  # independent
         // Timedelta('1d')).values  # small day-of-year integers

    st.write(f"m = {round(m,2)} | b = {round(b,2)} | r_sq = {round(r_sq,2)}")

    U = (-1 / m) / np.log(10)
    st.write(f"U = {round(U,1)} days [ (-1/m)/log(10) ] ")

    jtdm = np.log10(np.exp(1 / U) - 1)

    st.write(f"J(t) delta_max = {round(jtdm,2)} [ log(exp(1/U)-1)] ")

    day = (jtdm - b) / m
    topday = df.index[0] + Timedelta(day, 'd')  # extrapolate
    st.write(f"Top reached on day  {round(day)} ({topday.date()})")

    df["predicted_growth"] = np.nan
    #df["predicted_value"] = np.nan
    df["predicted_new_cases"] = np.nan
    df["cumm_cases_predicted"] = np.nan
    df['trendline'] = (df['rownumber'] * m + b)
    df = df.reset_index()

    df["cumm_cases_minus_background"] = df[
        "total_cases"] - background_total_cases + df.iloc[0][
            "new_cases_smoothed"]
    df["cumm_cases_minus_background"] = df[
        "cumm_cases_minus_background"].rolling(7).mean()

    # we make the trendline
    for i in range(len_total):
        df.loc[i, "predicted_growth"] = np.exp(10**df.iloc[i]["trendline"])
        df.loc[i, "real_growth"] = df.iloc[i][
            "cumm_cases_minus_background"] / df.iloc[
                i - 1]["cumm_cases_minus_background"]

    # we transfer the last known total cases to the column predicted cases
    df.loc[len_original - 1,
           "cumm_cases_predicted"] = df.iloc[len_original -
                                             1]["cumm_cases_minus_background"]

    # we make the predictions
    df.loc[len_original, "cumm_cases_predicted"] = df.iloc[len_original][
        "predicted_growth"] * df.iloc[len_original - 1]["cumm_cases_predicted"]

    for i in range(len_original, len_total):
        df.loc[i, "cumm_cases_predicted"] = df.iloc[
            i - 1]["cumm_cases_predicted"] * df.iloc[i]["predicted_growth"]
        df.loc[i, "predicted_new_cases"] = df.iloc[i][
            "cumm_cases_predicted"] - df.iloc[i - 1]["cumm_cases_predicted"]

    df["date"] = pd.to_datetime(df["index"], format="%Y-%m-%d")
    df = df.set_index("index")
    df_ = df[[
        "date", what_to_display, "cumm_cases_minus_background",
        "log_exp_gr_factor", 'trendline', "real_growth", "predicted_growth",
        "predicted_new_cases", "cumm_cases_predicted",
        "new_cases_smoothed_original"
    ]]
    # df_as_str = df_.astype(str)
    # st.write(df_as_str)

    df["new_cases_smoothed_original_cumm"] = df[
        "new_cases_smoothed_original"].cumsum()

    with _lock:
        #fig1y = plt.figure()

        fig1yz, ax = subplots()
        ax3 = ax.twinx()
        ax.set_title('Prediction of COVID-19 à la Levitt')
        # ax.scatter(alldates, df[what_to_display].values, color="#00b3b3", s=1, label=what_to_display)
        ax3.scatter(alldates,
                    df["log_exp_gr_factor"].values,
                    color="#b300b3",
                    s=1,
                    label="J(t) reality")
        ax3.scatter(alldates,
                    df["trendline"],
                    color="#b30000",
                    s=1,
                    label="J(t) predicted")
        ax.scatter(alldates,
                   df["predicted_new_cases"].values,
                   color="#0000b3",
                   s=1,
                   label="predicted new cases")
        ax.scatter(alldates,
                   df["new_cases_smoothed_original"].values,
                   color="green",
                   s=1,
                   label="reality new cases")

        ax.set_xlim(df.index[0], lastday)
        ax.yaxis.set_major_formatter(
            StrMethodFormatter('{x:,.0f}'))  # comma separators
        ax.grid()
        ax.legend(loc="upper left")
        ax.xaxis.set_major_formatter(
            ConciseDateFormatter(AutoDateLocator(), show_offset=False))

        st.pyplot(fig1yz)

    with _lock:

        fig1yza, ax = subplots()
        ax3 = ax.twinx()
        ax.set_title('Cummulative cases and growth COVID-19 à la Levitt')
        ax.scatter(alldates,
                   df["new_cases_smoothed_original_cumm"].values,
                   color="green",
                   s=1,
                   label="reality cumm cases")
        ax.scatter(alldates,
                   df["cumm_cases_predicted"].values,
                   color="#00b3b3",
                   s=1,
                   label="predicted cumm cases")

        ax3.scatter(alldates,
                    df["real_growth"].values,
                    color="#b300b3",
                    s=1,
                    label="real growth")
        ax3.scatter(alldates,
                    df["predicted_growth"].values,
                    color="#b3bbb3",
                    s=1,
                    label="predicted growth")
        ax.set_xlim(df.index[0], lastday)
        ax.yaxis.set_major_formatter(
            StrMethodFormatter('{x:,.0f}'))  # comma separators
        ax.grid()
        ax.legend(loc="upper left")
        ax.xaxis.set_major_formatter(
            ConciseDateFormatter(AutoDateLocator(), show_offset=False))

        st.pyplot(fig1yza)
import matplotlib.pyplot as plt

from matplotlib.ticker import ScalarFormatter, PercentFormatter
from matplotlib.dates import AutoDateLocator, AutoDateFormatter, ConciseDateFormatter
from datetime import datetime

from utils import read_batch_run, read_case_data, read_run, infection_rate
from plot import comparison_plots

#%%

sns.set_style("whitegrid")
sns.set_context("paper")
sns.set_palette("deep")

dateFormater = ConciseDateFormatter(AutoDateLocator())

#%%

rki, hospital = read_case_data("berlin-cases.csv", "berlin-hospital.csv")

#%%


def infections(f):
    ev = pd.read_csv(f, sep="\t")

    no_inf = set(ev.infected).difference(set(ev.infector))

    res = np.sort(
        np.concatenate(
예제 #13
0
            buy(
                0.9 * value, round_price(p0),
            )
            last_price = p0
            last_action = "BUY"
    elif action == "SELL":
        if holdings > 1e-6:
            sell(
                0.9 * holdings, round_price(p0),
            )
            last_price = p0
            last_action = "SELL"

plt.rc("font", size=12)
fig, ax = plt.subplots(figsize=(10, 6))

ax.plot(x, y, color="tab:orange", label="Value")
ax.plot(x, z, color="tab:blue", label="Stock")

ax.set_xlabel("Time")
ax.set_ylabel("Value")
ax.set_title("Portfolio Value")
ax.grid(True)
ax.legend(loc="upper left")

locator = AutoDateLocator()
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(ConciseDateFormatter(locator))

plt.show()
def loglognormal(df, what_to_display):
    #https://replit.com/@jsalsman/COVID19USlognormals
    st.subheader("Log Normal")
    df = df.set_index(DATEFIELD)
    firstday = df.index[0] + Timedelta('1d')
    nextday = df.index[-1] + Timedelta('1d')
    lastday = df.index[-1] + Timedelta(TOTAL_DAYS_IN_GRAPH - len(df), 'd') # extrapolate
    with _lock:
        #fig1y = plt.figure()
        fig1yz, ax = subplots()
        ax.set_title('NL COVID-19 cumulative log-lognormal extrapolations\n'
            + 'Source: repl.it/@jsalsman/COVID19USlognormals')

        x = ((df.index - Timestamp('2020-01-01')) # independent
            // Timedelta('1d')).values # small day-of-year integers
        yi = df[what_to_display].values # dependent
        #yd = df['Deceased_cumm'].values # dependent
        exrange = range((Timestamp(nextday)
            - Timestamp(firstday)) // Timedelta('1d'),
            (Timestamp(lastday) + Timedelta('1d')
            - Timestamp(firstday)) // Timedelta('1d')) # day-of-year ints
        indates = date_range(df.index[0], df.index[-1])
        exdates = date_range(nextday, lastday)

        ax.scatter(indates, yi, color="#00b3b3", label='Infected')
        #ax.scatter(indates, yd, color="#00b3b3", label='Dead')

        sqrt2 = sqrt(2)

        #im = Model(normal_c)
        im = Model(lognormal_c)

        iparams = im.make_params(s=0.3, mu=4.3, h=16.5)
        iparams['s'].min = 0; iparams['h'].min = 0
        iresult = im.fit(log(yi+1), iparams, x=x)
        st.text(f'---- {what_to_display}:\n' + iresult.fit_report())
        label_ = (f"{what_to_display} fit")
        ax.plot(indates, exp(iresult.best_fit)-1, 'b', label=label_)
        ipred = iresult.eval(x=exrange)
        ax.plot(exdates, exp(ipred)-1, 'b--',
            label='Forecast: {:,.0f}'.format(exp(ipred[-1])-1))
        iupred = iresult.eval_uncertainty(x=exrange, sigma=0.95) # 95% interval
        iintlow = clip(ipred-iupred, ipred[0], None)
        put(iintlow, range(argmax(iintlow), len(iintlow)), iintlow[argmax(iintlow)])
        ax.fill_between(exdates, exp(iintlow), exp(ipred+iupred), alpha=0.35, color='b')

        #dm = Model(normal_c)
        # dm = Model(lognormal_c)

        # dparams = dm.make_params(s=19.8, mu=79.1, h=11.4) # initial guesses
        # dparams['s'].min = 0; iparams['h'].min = 0
        # dresult = dm.fit(log(yd+1), dparams, x=x)
        # st.text('---- Deaths:\n' + dresult.fit_report())
        # ax.plot(indates, exp(dresult.best_fit)-1, 'r', label='Deaths fit')
        # dpred = dresult.eval(x=exrange)
        # ax.plot(exdates, exp(dpred)-1, 'r--',
        #     label='Forecast: {:,.0f}'.format(exp(dpred[-1])-1))
        # dupred = dresult.eval_uncertainty(x=exrange, sigma=0.95) # 95% interval
        # dintlow = clip(dpred-dupred, log(max(yd)+1), None)
        # put(dintlow, range(argmax(dintlow), len(dintlow)), dintlow[argmax(dintlow)])
        # ax.fill_between(exdates, exp(dintlow), exp(dpred+dupred), alpha=0.35, color='r')
        # ax.fill_between(exdates, 0.012 * (exp(iintlow)), 0.012 * (exp(ipred+iupred)),
        #     alpha=0.85, color='g', label='Deaths from observed fatality rate')

        ax.set_xlim(df.index[0], lastday)
        #ax.set_yscale('log') # semilog
        #ax.set_ylim(0, 1500000)
        ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) # comma separators
        ax.grid()
        ax.legend(loc="upper left")
        ax.xaxis.set_major_formatter(ConciseDateFormatter(AutoDateLocator(), show_offset=False))
        ax.set_xlabel('95% prediction confidence intervals shaded')

        #fig.savefig('plot.png', bbox_inches='tight')
        #print('\nTO VIEW GRAPH: click on plot.png in the file pane to the left.')
        #fig.show()
        st.pyplot(fig1yz)

    st.text(f"{what_to_display} at end of period shown: {int( exp(ipred[-1])-1)}.")
예제 #15
0
def generate(df,
             INFO,
             model,
             cp,
             feature,
             n_days_prediction,
             prediction_offset,
             plot=False,
             weather=False):
    """
    Generates predictions, given states data, a model and checkpoint dictionary
    df: dataframe with current data for all states/districts.
    INFO: static dictionary with info for all states/districts
    model: CovidNet model object
    cp: checkpoint dictionary returned from load_checkpoint
    feature: 0 for confirmed, 1 for deaths etc.
    n_days_prediction: number of days to predict
    prediction_offset: number of days from real data to be skipped
    plot(False): whether to plot charts and print logs
    weather(False): whether to include weather data as input
    """
    IP_SEQ_LEN = cp['config']['DS']['IP_SEQ_LEN']
    OP_SEQ_LEN = cp['config']['DS']['OP_SEQ_LEN']

    first_case_date = df['date'].min()
    n_days_data = (df.loc[df.state == 'KL'].date.max() -
                   df.loc[df.state == 'KL'].date.min()).days
    assert (n_days_prediction % OP_SEQ_LEN == 0)

    agg_days = n_days_data - prediction_offset + n_days_prediction  # number of days for plotting agg curve i.e. prediction + actual data

    api = {}
    states_agg = np.zeros(agg_days)
    state_ax = None
    for state in INFO:
        child_ax = None
        api[state] = {}
        child_agg = np.zeros(agg_days)
        for child in INFO[state]:
            pop_fct = child["popn"] / 1000
            ip_aux = torch.tensor(
                np.array(child["population_density"]), dtype=torch.float32).to(
                    DEVICE) if "population_density" in child else None

            child_df = df.loc[(df['state'] == state) & (
                df['name'] == child['name']
            )][:
               -prediction_offset]  # skip todays data. covid19 returns incomplete.
            child_df = data.prefill(data.expand(child_df), first_case_date)
            child_df['new_cases'] = child_df['confirmed'] - child_df[
                'confirmed'].shift(1).fillna(0)
            child_df['new_deaths'] = child_df['deceased'] - child_df[
                'deceased'].shift(1).fillna(0)
            child_df['new_recovered'] = child_df['recovered'] - child_df[
                'recovered'].shift(1).fillna(0)
            child_df['new_tests'] = child_df['tested'] - child_df[
                'tested'].shift(1).fillna(0)
            test_data = np.array(
                child_df[cp['config']['DS']['FEATURES']].rolling(
                    7, center=True, min_periods=1).mean() / pop_fct,
                dtype=np.float32)

            if weather:
                inp_start_date = child_df.date.min()
                inp_end_date = inp_start_date + dt.timedelta(days=agg_days)
                weather_df = data.get_state_weather_stats(
                    state, inp_start_date, inp_end_date)
                weather_cols = [
                    c for c in cp['config']['DS']['FEATURES']
                    if c in ['temp_mean', 'pressure_mean', 'humidity_mean']
                ]
                weather_df = weather_df[weather_cols]
                weather_data = np.array(weather_df, dtype=np.float32)
                test_data = np.concatenate(
                    (test_data, weather_data[:len(test_data)]), axis=1)
                weather_data = weather_data[len(test_data):]

            in_data = test_data[-IP_SEQ_LEN:, cp['config']['IP_FEATURES']]
            out_data = np.ndarray(shape=(0, len(cp['config']['OP_FEATURES'])),
                                  dtype=np.float32)
            for i in range(int(n_days_prediction / OP_SEQ_LEN)):
                ip = torch.tensor(in_data, dtype=torch.float32).to(DEVICE)
                try:
                    args = [
                        ip.view(-1, IP_SEQ_LEN,
                                len(cp['config']['IP_FEATURES']))
                    ]
                    if len(cp['config'].get('AUX_FEATURES', [])):
                        args.append(
                            ip_aux.view(1, len(cp['config']['AUX_FEATURES'])))
                    pred = model.predict(*args).view(
                        OP_SEQ_LEN, len(cp['config']['OP_FEATURES']))
                except Exception as e:
                    print(state, e)
                pred_data = pred.cpu().numpy()
                if weather:
                    pred_data = np.concatenate(
                        (pred_data, weather_data[:OP_SEQ_LEN]), axis=1)
                    weather_data = weather_data[OP_SEQ_LEN:]
                if IP_SEQ_LEN == OP_SEQ_LEN:
                    in_data = pred_data
                else:
                    in_data = np.append(in_data[-IP_SEQ_LEN + OP_SEQ_LEN:, :],
                                        pred_data,
                                        axis=0)
                out_data = np.append(out_data, pred.cpu().numpy(), axis=0)

            cn = child['name']
            orig_df = pd.DataFrame({
                'actual':
                np.array(test_data[:, feature] * pop_fct, dtype=np.int)
            })
            fut_df = pd.DataFrame({
                'predicted':
                np.array(out_data[:, feature] * pop_fct, dtype=np.int)
            })
            # print(fut_df.to_csv(sep='|'))
            full_df = orig_df.append(fut_df, ignore_index=True, sort=False)
            full_df[cn] = full_df['actual'].fillna(
                0) + full_df['predicted'].fillna(0)
            full_df['total'] = full_df[cn].cumsum()

            child_agg += np.array(full_df[cn][-agg_days:].fillna(0))

            # generate date col for full_df from child_df
            start_date = child_df['date'].iloc[0]
            full_df['Date'] = pd.to_datetime([
                (start_date + dt.timedelta(days=i)).strftime("%Y-%m-%d")
                for i in range(len(full_df))
            ])
            # if full_df[cn].max() < 10000: # or full_df[cn].max() < 5000:
            #     continue

            # print state, cumulative, peak
            peak = full_df.loc[full_df[cn].idxmax()]
            if plot:
                print(state, "|", cn, "|", peak['Date'].strftime("%b %d"), "|",
                      int(peak[cn]), "|", int(full_df['total'].iloc[-1]))

            # export data for API
            full_df['daily_deaths'] = full_df[cn] * 0.015
            full_df['daily_recovered'] = full_df[cn].shift(
                14, fill_value=0) - full_df['daily_deaths'].shift(7,
                                                                  fill_value=0)
            full_df['daily_active'] = full_df[cn] - full_df[
                'daily_recovered'] - full_df['daily_deaths']

            api[state][cn] = {}
            for idx, row in full_df[-agg_days:].iterrows():
                row_date = row['Date'].strftime("%Y-%m-%d")
                api[state][cn][row_date] = {
                    "delta": {
                        "confirmed": int(row[cn]),
                        "deceased": int(row['daily_deaths']),
                        "recovered": int(row['daily_recovered']),
                        "active": int(row['daily_active'])
                    }
                }

            # plot individual chart
            if plot:
                child_ax = full_df.plot(x='Date',
                                        y=[cn],
                                        title='Daily Cases for ' + state,
                                        figsize=(8, 5),
                                        grid=True,
                                        ax=child_ax,
                                        lw=3)
                child_ax.legend(loc=2)
                mn_l = DayLocator()
                child_ax.xaxis.set_minor_locator(mn_l)
                mj_l = AutoDateLocator()
                mj_f = ConciseDateFormatter(mj_l, show_offset=False)
                child_ax.xaxis.set_major_formatter(mj_f)

        states_agg += child_agg
        # plot aggregate chart for children
        if plot:
            agg_df = pd.DataFrame({state: child_agg})
            last_date = full_df['Date'].iloc[-1].to_pydatetime()
            start_date = last_date - dt.timedelta(days=agg_days)
            agg_df['Date'] = pd.to_datetime([
                (start_date + dt.timedelta(days=i)).strftime("%Y-%m-%d")
                for i in range(len(agg_df))
            ])
            if agg_df[state].max() > 10000:
                state_ax = plot_linechart(agg_df,
                                          'Date',
                                          state,
                                          ax=state_ax,
                                          title='Statewise daily cases',
                                          show_peak=False)
    # plot aggregate chart for all states
    if plot:
        agg_df = pd.DataFrame({'states_agg': states_agg})
        last_date = full_df['Date'].iloc[-1].to_pydatetime()
        start_date = last_date - dt.timedelta(days=agg_days)
        agg_df['Date'] = pd.to_datetime([
            (start_date + dt.timedelta(days=i)).strftime("%Y-%m-%d")
            for i in range(len(agg_df))
        ])
        plot_linechart(agg_df,
                       'Date',
                       'states_agg',
                       title='Aggregate daily cases')

    return api
예제 #16
0
    def plot_historic(self):
        dates = []
        positives = []
        hospitalizeds = []
        death_rates = []
        pos_rates = []
        for hist in self.historic:
            date = hist['date']
            positive = hist['positive']
            hospitalized = hist['hospitalized']
            posneg = hist['posNeg']
            death = hist['death']
            dates.append(date)
            if positive:
                positives.append(positive)
                if posneg:
                    pos_rates.append(positive / posneg * 100)
                else:
                    pos_rates.append(float("nan"))
            else:
                positives.append(float("nan"))
                pos_rates.append(float("nan"))

            if death and posneg:
                death_rates.append(death / positive * 100)
            else:
                death_rates.append(float("nan"))

            if hospitalized:
                hospitalizeds.append(hospitalized)
            else:
                hospitalizeds.append(float("nan"))

        fig, axs = subplots(2, 1, sharex=True, figsize=(10, 7))

        title_fs = 24
        label_fs = 18
        tick_fs = 12
        axs[1].set_xlabel("Date", fontsize=label_fs)

        ax1 = axs[0].twinx()
        axs[0].set_ylabel("Positive cases", fontsize=label_fs, color='r')
        ax1.set_ylabel("Hospitalized cases", fontsize=label_fs, color='b')
        axs[0].plot(dates, positives, 'r', lw=5)
        ax1.plot(dates, hospitalizeds, 'b', lw=5)
        axs[0].tick_params(axis='y', labelcolor='r', labelsize=tick_fs)
        ax1.tick_params(axis='y', labelcolor='b', labelsize=tick_fs)
        axs[0].ticklabel_format(axis='y', style='sci', scilimits=(-2, 2))
        ax1.ticklabel_format(axis='y', style='sci', scilimits=(-2, 2))

        ax2 = axs[1].twinx()
        axs[1].set_ylabel("Positive rate (%)", fontsize=label_fs, color='r')
        ax2.set_ylabel("Death rate (%)", fontsize=label_fs, color='b')
        axs[1].plot(dates, pos_rates, 'r', lw=5)
        ax2.plot(dates, death_rates, 'b', lw=5)
        axs[1].tick_params(axis='y', labelcolor='r', labelsize=tick_fs)
        ax2.tick_params(axis='y', labelcolor='b', labelsize=tick_fs)

        locator = AutoDateLocator()
        formatter = ConciseDateFormatter(locator)
        axs[1].xaxis.set_major_formatter(formatter)
        axs[1].xaxis.set_major_locator(locator)
        axs[1].tick_params(axis='x', labelsize=label_fs)

        fig.tight_layout(rect=[0, 0.03, 1, 0.95])
        fig.suptitle(self.state_name, fontsize=title_fs)
        # fig.savefig('test.png')
        return fig
        plot_title = cc + ", " + plant + " (" + ID + ", " + data_source + ", " + \
                       irrig_type + ", " + survey_date + ", Area: " + str(area) + " acres)"

    ax1.set_title(plot_title)
    ax2.set_title("")

    # import matplotlib.dates as mdates
    # ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b'))
    # Rotates and right-aligns the x labels so they don't crowd each other.
    # for label in ax1.get_xticklabels(which='major'):
    #     label.set(rotation=90, horizontalalignment='right')

    # ax1.xaxis.set_major_formatter(
    #     mdates.ConciseDateFormatter(ax1.xaxis.get_major_locator()))
    ax2.xaxis.set_major_formatter(
        ConciseDateFormatter(ax2.xaxis.get_major_locator()))
    # matplotlib.dates.ConciseDateFormatter(ax2.xaxis.get_major_locator()))

    plot_path = SOS_plot_dir + "/exactEval6000_vertLine/" + plant + "/"
    os.makedirs(plot_path, exist_ok=True)
    fig_name = plot_path + county + "_" + ID + '.png'
    plt.savefig(fname=fig_name, dpi=100, bbox_inches='tight', facecolor="w")

    # save flat
    plot_path = SOS_plot_dir + "/exactEval6000_vertLine_flat/"
    os.makedirs(plot_path, exist_ok=True)
    fig_name = plot_path + county + "_" + ID + '.png'
    plt.savefig(fname=fig_name, dpi=100, bbox_inches='tight', facecolor="w")

    plt.close('all')
    counter += 1
예제 #18
0
#print (c.fetchone())
pressure = c.fetchall()

c.execute('SELECT humidity FROM bme280_data;')
#print (c.fetchone())
humidity = c.fetchall()

print(type(humidity[0]))
#plt.plot_date(temperature, pressure, '.')
#plt.scatter(pressure, humidity)

loc = WeekdayLocator(byweekday=(MO, TU, WE, TH, FR, SA, SU,))
formatter = DateFormatter('%Y-%m-%d')

locator = AutoDateLocator()
formatter = ConciseDateFormatter(locator)

fig, (ax1) = plt.subplots(1, sharex='all')
fig.autofmt_xdate(rotation=90)

color = 'tab:red'
ax1.plot_date(timestamp, temperature, fmt='o', color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax1.xaxis.set_major_locator(DayLocator())
ax1.xaxis.set_major_formatter(formatter)
#ax1.xaxis.set_major_locator()
#ax1.xaxis.set_minor_locator(AutoMinorLocator())

ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:green'
ax2.plot_date(timestamp, pressure, '.', color=color)
예제 #19
0
def statewise(model, cp, df, INFO, plot=True):
    #     cp['config']['OP_FEATURES'] = [0] #fix for bug in 1.1740

    IP_SEQ_LEN = cp['config']['DS']['IP_SEQ_LEN']
    OP_SEQ_LEN = cp['config']['DS']['OP_SEQ_LEN']

    first_case_date = df['date'].min()

    accs = {}
    for state in INFO:
        accs[state] = {}
        for child in INFO[state]:
            pop_fct = child["popn"] / 1000
            ip_aux = torch.tensor(
                np.array(child["population_density"]), dtype=torch.float32).to(
                    DEVICE) if "population_density" in child else None

            child_df = df.loc[(df['state'] == state)
                              & (df['name'] == child['name'])]
            child_df = data.prefill(data.expand(child_df), first_case_date)
            child_df['new_cases'] = child_df['confirmed'] - child_df[
                'confirmed'].shift(1).fillna(0)
            child_df['new_deaths'] = child_df['deceased'] - child_df[
                'deceased'].shift(1).fillna(0)
            child_df['new_recovered'] = child_df['recovered'] - child_df[
                'recovered'].shift(1).fillna(0)
            child_df['new_tests'] = child_df['tested'] - child_df[
                'tested'].shift(1).fillna(0)
            test_data = np.array(
                child_df[cp['config']['DS']['FEATURES']].rolling(
                    7, center=True, min_periods=1).mean() / pop_fct,
                dtype=np.float32)

            all_preds = []
            pred_vals = []
            real_vals = []

            ip_shp = IP_SEQ_LEN, len(cp['config']['IP_FEATURES'])
            op_shp = OP_SEQ_LEN, len(cp['config']['OP_FEATURES'])

            for i in range(len(test_data) - IP_SEQ_LEN - OP_SEQ_LEN + 1):
                ip = torch.tensor(test_data[i:i + IP_SEQ_LEN,
                                            cp['config']['IP_FEATURES']])
                op = torch.tensor(
                    test_data[i + IP_SEQ_LEN:i + IP_SEQ_LEN + OP_SEQ_LEN,
                              cp['config']['OP_FEATURES']])
                ip = ip.to(DEVICE)
                op = op.to(DEVICE)

                args = [
                    ip.view(1, IP_SEQ_LEN, len(cp['config']['IP_FEATURES']))
                ]
                if len(cp['config'].get('AUX_FEATURES', [])):
                    args.append(
                        ip_aux.view(1, len(cp['config']['AUX_FEATURES'])))
                pred = model.predict(*args)

                all_preds.append(pred.view(op_shp).cpu().numpy() * pop_fct)
                pred_vals.append(pred.view(op_shp).cpu().numpy()[0] * pop_fct)
                real_vals.append(op.view(op_shp).cpu().numpy()[0] * pop_fct)

            # prepend first input
            nans = np.ndarray((IP_SEQ_LEN, len(cp['config']['OP_FEATURES'])))
            nans.fill(np.NaN)
            pred_vals = list(nans) + pred_vals
            real_vals = list(
                test_data[:IP_SEQ_LEN, cp['config']['OP_FEATURES']] *
                pop_fct) + real_vals
            # append last N-1 values
            nans = np.ndarray(op_shp)
            nans.fill(np.NaN)
            pred_vals.extend(nans[1:])  # pad with NaN
            real_vals.extend(op.view(op_shp).cpu().numpy()[1:] * pop_fct)

            real_vals = np.array(real_vals).reshape(
                len(test_data), len(cp['config']['OP_FEATURES']))
            pred_vals = np.array(pred_vals).reshape(
                len(test_data), len(cp['config']['OP_FEATURES']))

            accs[state][child['name']] = []
            for o in range(len(cp['config']['OP_FEATURES'])):
                cmp_df = pd.DataFrame({
                    'actual': real_vals[:, o],
                    'predicted0': pred_vals[:, o]
                })
                # set date
                cmp_df['Date'] = pd.Series([
                    first_case_date + dt.timedelta(days=i)
                    for i in range(len(cmp_df))
                ])

                # plot noodles
                ax = None
                i = IP_SEQ_LEN
                mape = []
                for pred in all_preds:
                    # skip noodles if cases < N on any day
                    if any(cmp_df.loc[i:i + OP_SEQ_LEN - 1, 'actual'] < 100):
                        i += 1
                        continue
                    cmp_df['predicted_cases'] = np.NaN
                    cmp_df.loc[i:i + OP_SEQ_LEN - 1,
                               'predicted_cases'] = pred[:, o]
                    ape = np.array(
                        100 *
                        ((cmp_df['actual'] - cmp_df['predicted_cases']).abs() /
                         (cmp_df['actual'].abs() + 1)))
                    ape = ape[~np.isnan(ape)]  # remove nans
                    if len(ape): mape.append(ape)
                    if plot:
                        ax = cmp_df.plot(x='Date',
                                         y='predicted_cases',
                                         ax=ax,
                                         legend=False)
                    i += 1
                total_acc = np.zeros(OP_SEQ_LEN)
                for m in mape:
                    total_acc += m
                elwise_mape = total_acc / len(mape)
                acc = 100 - sum(elwise_mape) / len(elwise_mape)
                acc = np.nan if acc < 0 else acc
                accs[state][child['name']].append(acc)
                acc_str = f"{acc:0.2f}%"
                if plot:
                    print(state, child['name'], acc_str)
                    print("daywise accuracy:", 100 - elwise_mape)

                # plot primary lines
                if plot:
                    ax = cmp_df.plot(x='Date',
                                     y=['actual', 'predicted0'],
                                     figsize=(10, 5),
                                     lw=5,
                                     title=child['name'] +
                                     ' | Daily predictions | ' + acc_str,
                                     ax=ax)
                    mn_l = DayLocator()
                    ax.xaxis.set_minor_locator(mn_l)
                    mj_l = AutoDateLocator()
                    mj_f = ConciseDateFormatter(mj_l, show_offset=False)
                    ax.xaxis.set_major_formatter(mj_f)

    # agg accs
    state_accs = []
    weights = []
    for state in accs:
        child_accs = []
        for child in accs[state]:
            child_accs.append(accs[state][child])
            weights.append(INFO[state][0]['popn'])  # dirty hack
        a = np.nanmean(np.array(child_accs))
        print(state, a)
        state_accs.append(a)

    # simple average
#     a = np.nanmean(np.array(state_accs))
# weighted avg
    wts = np.array(weights)
    arr = np.array(state_accs)
    indices = ~np.isnan(arr)
    a = np.average(arr[indices], weights=wts[indices])

    print("India", a)
    return a
예제 #20
0
    def _update(frame):
        date.append(datetime.now() + timedelta(days=next(counter)))

        if len(date) <= 50:
            Y_slider.valmax = len(date)
        ax_slider.set_xlim(Y_slider.valmin, Y_slider.valmax)

        orders = []
        for i in range(N):
            orders.append(
                get_bitbay_data('orderbook',
                                CRYPTO_CURRENCIES[i] + MAIN_CURRENCY))

        for i in range(N):
            bids[i].append(orders[i]['bids'][0][0])
            asks[i].append(orders[i]['asks'][0][0])
            if len(bids[i]) >= Y_slider.val and \
                    Y_slider.val != Y_slider.valinit:
                last_Y_bids = bids[i][-Y_slider.val:]
                avg_bids[i].append(sum(last_Y_bids) / len(last_Y_bids))
            else:
                avg_bids[i].append(sum(bids[i]) / len(bids[i]))
            if len(asks[i]) >= Y_slider.val and \
                    Y_slider.val != Y_slider.valinit:
                last_Y_asks = asks[i][-Y_slider.val:]
                avg_asks[i].append(sum(last_Y_asks) / len(last_Y_asks))
            else:
                avg_asks[i].append(sum(asks[i]) / len(asks[i]))

        for i in range(N):
            volumes[i] += orders[i]['bids'][0][1]
            new_text = f'Trading volume: {round(volumes[i], 4)}'
            volume_textes[i].set_text(new_text)

        print('')
        for i in range(N):
            if len(bids[i]) >= 2:
                if Y_slider.val != Y_slider.valinit and Y_slider.val >= 2:
                    last_Y_bids = bids[i][-Y_slider.val:]
                    diff = last_Y_bids[-2] - last_Y_bids[-1]
                else:
                    diff = bids[i][-2] - bids[i][-1]

                if diff < 0:
                    bid_gains[i].append(abs(diff))
                elif diff > 0:
                    bid_losses[i].append(diff)

            # https://en.wikipedia.org/wiki/Relative_strength_index
            if Y_slider.val != Y_slider.valinit:
                a = np.mean(bid_gains[i][-Y_slider.val:])
                b = np.mean(bid_losses[i][-Y_slider.val:])
            else:
                a, b = np.mean(bid_gains[i]), np.mean(bid_losses[i])
            RS = a / b
            RSI = 100 - (100 / (1 + RS))

            nan_msg = None
            a_is_nan = np.isnan(a)
            b_is_nan = np.isnan(b)
            if a_is_nan and b_is_nan:
                nan_msg = 'Bid rate is stable'
            elif a_is_nan:
                nan_msg = "No bid rate growth recorded"
            elif b_is_nan:
                nan_msg = "No bid rate loss recorded"

            new_text = f'RSI: {nan_msg if nan_msg else round(RSI)}'
            RSI_textes[i].set_text(new_text)
            RSI_range = (_min(RSI_values[i]), _max(RSI_values[i]))
            print(f'plot {i}: RSI={RSI_range}')
            RSI_values[i].append(50 if nan_msg else RSI)

        for i, lines in enumerate(ax_lines):
            if bids[i]:
                lines[0].set_data(date, bids[i])
            if asks[i]:
                lines[1].set_data(date, asks[i])
            if avg_bids[i]:
                lines[2].set_data(date, avg_bids[i])
            if avg_asks[i]:
                lines[3].set_data(date, avg_asks[i])
            if RSI_values[i]:
                lines[4].set_data(date, RSI_values[i])

        for ax, crypto_currency in zip(axes, CRYPTO_CURRENCIES):
            xlocator = AutoDateLocator()
            ylocator = plt.LinearLocator(numticks=3)
            formatter = ConciseDateFormatter(xlocator)

            ax.set(ylabel=f'Rate [{MAIN_CURRENCY}]', title=crypto_currency)
            handles, labels = ax.get_legend_handles_labels()
            ax.legend(handles + [RSI_line],
                      labels + ['RSI'],
                      loc='upper left',
                      bbox_to_anchor=(1.12, 1.05))
            ax.yaxis.set_major_locator(ylocator)
            ax.xaxis.set_major_locator(xlocator)
            ax.xaxis.set_major_formatter(formatter)
            ax.relim()
            ax.autoscale_view()

        for ax in RSI_subaxes:
            ax.axhline(y=70, color='lightgray', linestyle='-', linewidth=0.4)
            ax.axhline(y=30, color='lightgray', linestyle='-', linewidth=0.4)
            ax.set(ylabel='RSI')
            ax.relim()
            ax.set_ylim(0, 100)
            ax.autoscale_view()

        plt.tight_layout()

        return ax_lines,
예제 #21
0
async def stats(ctx, *args):
    req_country = " ".join([country.title() for country in args])
    r = requests.get('https://corona-api.com/countries/{0}'.format(
        name_code_pair[req_country]))
    json_data = json.loads(r.text)['data']
    latest_confirmed = json_data['latest_data']['confirmed']
    latest_deaths = json_data['latest_data']['deaths']

    # Process graph
    dates = date2num([day['date'] for day in json_data['timeline']])
    confirmed = [day['confirmed'] for day in json_data['timeline']]
    deaths = [day['deaths'] for day in json_data['timeline']]

    plt.style.use('dark_background')
    fig, ax = plt.subplots()

    plt.plot_date(dates,
                  confirmed,
                  color='#47a0ff',
                  linestyle='-',
                  marker='',
                  linewidth=4,
                  ydate=False,
                  xdate=True)
    loc = AutoDateLocator()
    formatter = ConciseDateFormatter(loc)

    ax.xaxis.set_major_locator(loc)
    ax.xaxis.set_major_formatter(formatter)

    ax.yaxis.grid()

    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)

    locs, _ = plt.yticks()
    ylabels = []
    for l in locs:
        lab = str(int(l)).replace('000000000', '000M').replace(
            '00000000',
            '00M').replace('0000000', '0M').replace('000000', 'M').replace(
                '00000', '00K').replace('0000', '0K').replace('000', 'K')
        if not ('K' in lab or 'M' in lab):
            lab = "{:,}".format(int(lab))
        ylabels.append(lab)
    plt.yticks(locs, ylabels)
    plt.ylim(bottom=0)

    plt.savefig("images/stats.png", transparent=True)
    plt.close(fig)

    with open('images/stats.png', 'rb') as f:
        file = io.BytesIO(f.read())

    image = discord.File(file, filename='stats.png')

    embed = discord.Embed(title=req_country,
                          description=f'Some stats for {req_country}',
                          colour=discord.Colour.red())
    embed.set_thumbnail(url='https://www.countryflags.io/{0}/flat/64.png'.
                        format(name_code_pair[req_country]))
    embed.set_footer(text='Data from: https://about-corona.net/')
    embed.add_field(name='Total Cases Confirmed',
                    value="{:,}".format(latest_confirmed))
    embed.add_field(name='Total Deaths Confirmed',
                    value="{:,}".format(latest_deaths))
    embed.set_image(url='attachment://stats.png')

    await ctx.send(embed=embed, file=image)
예제 #22
0
def make_graph_delta(df, animated, i, total, showlogyaxis, title,
                     what_to_display, y_limit):

    # st.write("line 258")
    # df_as_str = df.astype(str)
    # st.write(df_as_str)
    with _lock:
        #fig1y = plt.figure()

        fig1yz, ax = subplots()
        ax3 = ax.twinx()
        ax.set_title(
            f'Prediction of COVID-19 à la Levitt - {title} - ({i}/{total})')
        # ax.scatter(alldates, df[what_to_display].values, color="#00b3b3", s=1, label=what_to_display)
        ax3.scatter(df["date"],
                    df["log_exp_gr_factor"].values,
                    color="#b300b3",
                    s=1,
                    label="J(t) reality")
        ax3.scatter(df["date"],
                    df["trendline"],
                    color="#b30000",
                    s=1,
                    label="J(t) predicted")
        ax.scatter(df["date"],
                   df["what_to_display_original"].values,
                   color="orange",
                   s=1,
                   label=f"reality { what_to_display}")
        ax.scatter(df["date"],
                   df[what_to_display].values,
                   color="green",
                   s=1,
                   label=f"reality { what_to_display}")
        ax.scatter(df["date"],
                   df["what_to_display_predicted"].values,
                   color="#0000b3",
                   s=1,
                   label=f"predicted { what_to_display}")

        ax.set_xlim(df.index[0], df.index[-1])

        if showlogyaxis == "10":
            ax.semilogy()
            ax.set_ylim(1, 100_000)
        elif showlogyaxis == "2":
            ax.semilogy(2)
            ax.set_ylim(1, 100_000)
        elif showlogyaxis == "logit":
            ax.set_yscale("logit")
            ax.set_ylim(1, 100_000)
        else:
            ax.set_ylim(0, y_limit)
        ax.set_ylabel(what_to_display)
        ax3.set_ylabel("J(t)")
        ax.set_xlabel("Date")

        ax.yaxis.set_major_formatter(
            StrMethodFormatter('{x:,.0f}'))  # comma separators
        ax.grid()
        ax.legend(loc="upper left")
        ax.xaxis.set_major_formatter(
            ConciseDateFormatter(AutoDateLocator(), show_offset=False))

        filename = (f"{OUTPUT_DIR}\LEVITT_{i}")

        plt.savefig(filename, dpi=100, bbox_inches="tight")

        placeholder1.pyplot(fig1yz)
        plt.close()
    return filename