Exemplo n.º 1
0
def plot_forecast_ts(df_test_m, df_test):
    fontsize = 16

    fig = plt.figure(figsize=(12, 5))
    gs = gridspec.GridSpec(1, 1, height_ratios=None)
    facecolor = 'white'
    ax0 = plt.subplot(gs[0], facecolor=facecolor)
    # df_test.plot(ax=ax0)
    ax0.plot_date(df_test.index,
                  df_test.iloc[:, 0],
                  ls='-',
                  label='Observed',
                  c='black')

    ax0.plot_date(df_test.index,
                  df_test.iloc[:, 1],
                  ls='-',
                  c='red',
                  label=r'Ridge Regression forecast')
    # ax0.set_xticks()
    # ax0.set_xticklabels(df_test.index.year,
    ax0.set_ylabel('Standardized Soy Yield', fontsize=fontsize)
    ax0.tick_params(labelsize=fontsize)
    ax0.axhline(y=0, color='black', lw=1)
    ax0.legend(fontsize=fontsize)

    df_scores = df_test_m.loc[0][df_test_m.columns[0][0]]
    Texts1 = []
    Texts2 = []
    textprops = dict(color='black', fontsize=fontsize + 4, family='serif')
    rename_met = {
        'RMSE': 'RMSE-SS',
        'corrcoef': 'Corr. Coeff.',
        'MAE': 'MAE-SS',
        'BSS': 'BSS',
        'roc_auc_score': 'ROC-AUC',
        'r2_score': '$r^2$',
        'mean_absolute_percentage_error': 'MAPE'
    }
    for k in df_scores.index:
        label = rename_met[k]
        val = round(df_scores[k], 2)
        Texts1.append(TextArea(f'{label}', textprops=textprops))
        Texts2.append(TextArea(f'{val}', textprops=textprops))
    texts_vbox1 = VPacker(children=Texts1, pad=0, sep=4)
    texts_vbox2 = VPacker(children=Texts2, pad=0, sep=4)

    ann1 = AnnotationBbox(texts_vbox1, (.02, .15),
                          xycoords=ax0.transAxes,
                          box_alignment=(0, .5),
                          bboxprops=dict(facecolor='white',
                                         boxstyle='round',
                                         edgecolor='white'))
    ann2 = AnnotationBbox(texts_vbox2, (.21, .15),
                          xycoords=ax0.transAxes,
                          box_alignment=(0, .5),
                          bboxprops=dict(facecolor='white',
                                         boxstyle='round',
                                         edgecolor='white'))
    ann1.set_figure(fig)
    ann2.set_figure(fig)
    fig.artists.append(ann1)
    fig.artists.append(ann2)
    return
Exemplo n.º 2
0
def plot_cell(show=True, save=False):
    """Makes total cell"""

    # Get the angles
    t_c, t_ld, t_sd, t_d, t_f = get_angles()

    # Get the distances - cosmetic
    r0 = 0.3
    r1 = 0.5

    # Define the fig and axes for the graph
    fig = plt.figure()
    ax = fig.add_subplot(projection='polar')
    ax.set_thetamin(0)
    ax.set_thetamax(60)
    ax.grid(axis='y')
    ax.set_title("Layout of KURRI Main Ring", fontsize=16)
    t_ticks = [0, 5, 10, 15, 20, 25, 30]
    plt.xticks(
        np.array(t_ticks) * TO_RAD * 2, [fr'${t}\degree$' for t in t_ticks])
    plt.yticks([])
    ax.set_ylim(0, r1 + 0.1)

    # Make wedge to contain the entire cell
    #plot_segment(ax, 0, r1+0.1, 0, 0, ls='--', lw=1.5)
    #plot_segment(ax, 0, r1+0.1, t_c, t_c, ls='--', lw=1.5)

    # Make the F and D magnets
    t_d1_start = t_ld
    t_f_start = t_d1_start + t_d + t_sd
    t_d2_start = t_f_start + t_f + t_sd
    plot_wedge(ax,
               r0,
               r1,
               t_d1_start,
               t_d1_start + t_d,
               col='royalblue',
               label='D Magnet')  # First D magnet
    plot_wedge(ax,
               r0,
               r1,
               t_f_start,
               t_f_start + t_f,
               col='firebrick',
               label='F Magnet')  # F magnet
    plot_wedge(ax, r0, r1, t_d2_start, t_d2_start + t_d,
               col='royalblue')  # Second D magnet

    # Plot arrows to show dimensions
    plot_ang_arrow(ax, 0.1, 0, t_c, r'$\theta_{C}$', headsize=0)
    r_arr = r1 + 0.025
    plot_ang_arrow(ax, r_arr, 0, t_ld, r'$\theta_{LD}$')
    plot_ang_arrow(ax, r_arr, t_d1_start, t_d1_start + t_d, r'$\theta_{D}$')
    plot_ang_arrow(ax, r_arr, t_d1_start + t_d, t_d1_start + t_d + t_sd,
                   r'$\theta_{SD}$')
    plot_ang_arrow(ax, r_arr, t_f_start, t_f_start + t_f / 2,
                   r'$\theta_{F}/2$')

    plot_rad_arrow(ax, 0, r0, t_d2_start + t_d, r'$r_0$')
    plot_rad_arrow(ax, 0, r1, t_d2_start + t_d, r'$r_1$')

    # Write in the values of angles and lengths using artists
    text = [
        fr'$\theta_C$', fr'$\theta_{"{LD}"}$', fr'$\theta_D$',
        fr'$\theta_{"{SD}"}$', fr'$\theta_F$'
    ]
    val = [rf'$= {v}\degree$' for v in [t_c, t_ld, t_d, t_sd, t_f]]
    Texts = [TextArea(t) for t in text]
    Vals = [TextArea(v) for v in val]
    textbox = VPacker(children=Texts, pad=0.1, sep=0.1)
    valsbox = VPacker(children=Vals, pad=0.1,
                      sep=2.4)  # This 'sep' is fine-tuned to make boxes align!
    totbox = HPacker(children=[textbox, valsbox], pad=0.1, sep=0.1)
    ann = AnnotationBbox(totbox, (0.05, 0.8),
                         xycoords=ax.transAxes,
                         box_alignment=(0.05, 0.8),
                         bboxprops=dict(facecolor='wheat',
                                        boxstyle='round',
                                        color='black'))
    ann.set_figure(fig)
    fig.artists.append(ann)

    text2 = [r'$r_0$', r'$r_1$']
    val2 = [r'$= 4.3$m', r'$= 5.4$m']
    Texts2 = [TextArea(t) for t in text2]
    Vals2 = [TextArea(v) for v in val2]
    textbox2 = VPacker(children=Texts2, pad=0.1, sep=0.1)
    valsbox2 = VPacker(children=Vals2, pad=0.1,
                       sep=1)  # This 'sep' is fine-tuned to make boxes align!
    totbox2 = HPacker(children=[textbox2, valsbox2], pad=0.1, sep=0.1)
    ann2 = AnnotationBbox(totbox2, (0.05, 0.5),
                          xycoords=ax.transAxes,
                          box_alignment=(0.05, 0.5),
                          bboxprops=dict(facecolor='wheat',
                                         boxstyle='round',
                                         color='black'))
    ann2.set_figure(fig)
    fig.artists.append(ann2)

    # Legend
    ax.legend(loc=(0.75, 0.8), facecolor='lavenderblush', edgecolor='k')

    #Misc
    fig.tight_layout()
    if save:
        fig.savefig('figs/KURRI_layout.png')
        fig.savefig('figs/KURRI_layout.pdf')
    if show:
        fig.show()

    return fig, ax
Exemplo n.º 3
0
texts_vbox1 = VPacker(children=Texts1, pad=0, sep=4)
texts_vbox2 = VPacker(children=Texts2, pad=0, sep=4)

ann1 = AnnotationBbox(texts_vbox1, (.02, .15),
                      xycoords=ax0.transAxes,
                      box_alignment=(0, .5),
                      bboxprops=dict(facecolor='white',
                                     boxstyle='round',
                                     edgecolor='white'))
ann2 = AnnotationBbox(texts_vbox2, (.21, .15),
                      xycoords=ax0.transAxes,
                      box_alignment=(0, .5),
                      bboxprops=dict(facecolor='white',
                                     boxstyle='round',
                                     edgecolor='white'))
ann1.set_figure(fig)
ann2.set_figure(fig)
fig.artists.append(ann1)
fig.artists.append(ann2)
# if save:
#     plt.savefig(os.path.join(rg.path_outsub2,
#                          f'forecast_aCI{alpha_CI}.pdf'),
#             bbox_inches='tight')

#%% forecasting

from sklearn.linear_model import RidgeCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegressionCV
from stat_models_cont import ScikitModel