def plot_variation_top_k(o):
    latexify(fig_height=3.6)
    fig, ax = plt.subplots(nrows=3, sharex=True)
    error_energy = pd.DataFrame(o["error energy"]).T
    error_energy["Hart"] = error_energy["Hart"].value_counts().head(1).index[0]
    error_energy.plot(ax=ax[0], kind="bar", rot=0)
    ax[0].set_ylabel(r"\% Error" "\n" "in Energy")
    rmse_power = pd.DataFrame(o["rmse power"]).T
    rmse_power["Hart"] = rmse_power["Hart"].value_counts().head(1).index[0]
    rmse_power.plot(ax=ax[1], kind="bar", legend=False, rot=0)
    ax[1].set_ylabel("Mean absolute error\n in power (W)")
    f_score = pd.DataFrame(o["f_score"]).T
    f_score["Hart"] = f_score["Hart"].value_counts().head(1).index[0]

    f_score.plot(ax=ax[2], kind="bar", legend=False, rot=0)
    ax[2].set_ylabel(r"F-score")

    ax[2].set_xlabel("Top $k$ appliances used for modelling")

    for a in ax:
        format_axes(a)
    plt.tight_layout()
    ax[0].legend(loc='upper center', bbox_to_anchor=(0.5, 1.25), ncol=3)
    plt.savefig("../../figures/%s/top_k_%s.png" % (appliance, appliance),
                bbox_inches="tight")
    plt.savefig("../../figures/%s/top_k_%s.pdf" % (appliance, appliance),
                bbox_inches="tight")
def make_plot():
    y = d[4]['RF']['predicted_labels']
    x = np.hstack([test['rating'].values, train['rating'].values])

    import sys
    import matplotlib.pyplot as plt
    sys.path.append("../common")
    from common_functions import latexify, format_axes
    latexify()
    fig, ax = plt.subplots()
    ax.scatter(x, y)
    ax.set_ylabel("Predicted score")
    ax.set_xlabel("Actual score")
    ax.set_xlim((-0.5, 4.5))
    ax.set_ylim((-0.5, 4.5))
    ax.set_aspect('equal')
    plt.tight_layout()
    format_axes(ax)
    plt.savefig("../../figures/hvac/regression.png")
    plt.savefig("../../figures/hvac/regression.pdf")
def plot_malfunction(malfunction_dict, ax, title):
    for i, (fridge_name, fridge_pair_powers) in enumerate(malfunction_dict.items()):
        xs = [fridge_pair_powers[0][0], fridge_pair_powers[1][0]]
        ys = [fridge_pair_powers[0][1], fridge_pair_powers[1][1]]
        ax.scatter(xs, ys, label=fridge_name, marker='.',alpha=0.2,color=colors[i])
        print fridge_name
        print xs
        print ys

        x = (fridge_pair_powers[0][0] + fridge_pair_powers[1][0])/2
        y = (fridge_pair_powers[0][1] + fridge_pair_powers[1][1])/2
        x_start, dx  = xs[0], xs[1]-xs[0]
        y_start, dy = ys[0], ys[1]-ys[0]
        print x_start, dx, y_start, dy
        #ax.arrow(x_start, y_start, dx, dy,
        #         head_width=5, head_length=10, fc=colors[i], ec=colors[i], lw=0.7,zorder=(10-2*i))
        #  ax.annotate(percent_savings[fridge_name], xy=(x, y), xytext=(x, y),
        #            )
    #ax.set_ylabel("Transient power (W)")

    #ax.set_title(title)
    format_axes(ax)
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
             (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
             (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
             (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
             (188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]

# Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
for i in range(len(tableau20)):
    r, g, b = tableau20[i]
    tableau20[i] = (r / 255., g / 255., b / 255.)

latexify(columns=1, fig_height=1.7)
ax = result_df.plot(colors=[tableau20[0], tableau20[4],tableau20[6]])
ax.set_xlabel(r"Percentage threshold ($P$)")
format_axes(ax)
plt.ylim((-0.1, 1.1))



plt.tight_layout()

L = ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.35),
          ncol=3)
L.get_texts()[0].set_text(r'Usage Energy' '\nProportion')
L.get_texts()[1].set_text(r'Precision')
L.get_texts()[2].set_text(r'Recall')
#plt.tight_layout()


plt.savefig("../../figures/fridge/fridge_activity_energy.png", bbox_inches="tight")
                  edgecolors=None,
                  lw=0.1)
    ax[i].scatter(under_pred_df["GT"],
                  under_pred_df[dictionary_key],
                  c='green',
                  alpha=0.5,
                  zorder=10,
                  lw=0.1)
    ax[i].scatter(extra_pred_df["GT"],
                  extra_pred_df[dictionary_key],
                  c='green',
                  alpha=0.5,
                  zorder=10,
                  lw=0.1)
    ax[i].set_xlabel("GT power")
    ax[i].plot(gt, upper_bound, color='black', alpha=0.6, linewidth=0.4)
    ax[i].plot(gt, lower_bound, color='black', alpha=0.6, linewidth=0.4)

    ax[i].set_title(r"%s" "\n FP= %d" % (algo_name, under_pred + extra_pred))
    format_axes(ax[i])
    ax[i].set_aspect('equal')
ax[0].set_ylabel("Predicted power")
plt.tight_layout()

plt.savefig(os.path.join(script_path, "..", "..",
                         "figures/fridge/power_level_nilm.pdf"),
            bbox_inches="tight")
plt.savefig(os.path.join(script_path, "..", "..",
                         "figures/fridge/power_level_nilm.png"),
            bbox_inches="tight")
    subplot.contourf(xx, yy, Z, levels=[threshold, Z.max()],
                     colors='orange')
    b = subplot.scatter(XY[:-n_outliers, 0], XY[:-n_outliers, 1], c='white')
    c = subplot.scatter(XY[-n_outliers:, 0], XY[-n_outliers:, 1], c='white')
    subplot.axis('tight')
    subplot.legend(
        [a.collections[0]],
        ['Learned decision function'], loc=4)
    #subplot.set_xlabel("%d. %s (errors: %d)" % (1, "rob", n_errors))
    subplot.set_xlim((-0.1, 1.1))
    subplot.set_ylim((0, 100))
    ax[i].axhspan(df["usage_percentage"].median(), df["usage_percentage"].median())
    ax[i].axvspan(df["usage proportion"].median(), df["usage proportion"].median())

    #plt.subplots_adjust(0.04, 0.1, 0.96, 0.94, 0.1, 0.26)
    format_axes(ax[i])
    ax[i].set_xlabel("Proportion of usage cycles")

    #ylims = plt.ylim()
    #plt.ylim((ylims[0]-5, ylims[1]+5))
    #xlims = plt.xlim()
    #plt.xlim((xlims[0]-0.1, xlims[1]+0.1))



    e = df[df.usage_percentage.isin(XY[-n_outliers:, 1])]
    feedback_homes = e[e.usage_percentage>df.usage_percentage.median()]["home"].values
    print "Predicted:", feedback_homes
    extra_pred = np.setdiff1d(feedback_homes, submetered_homes_feedback)
    missed = np.setdiff1d(submetered_homes_feedback, feedback_homes)
    print "Extra predicted:", extra_pred
from nilmtk.metrics import f1_score

import warnings
warnings.filterwarnings("ignore")

ds = DataSet("/Users/nipunbatra/Downloads/wikienergy-2.h5")
fridges = nilmtk.global_meter_group.select_using_appliances(type='fridge')

f_id = 110
b_id = 236
elec = ds.buildings[b_id].elec
mains = elec.mains()

h = Hart85()
h.train(elec.mains())

h2 = h.pair_df['T1 Active']

sys.path.append("../common")

from common_functions import latexify, format_axes

latexify(fig_height=1.2)
ax = h2[(h2<500)].hist(bins=10, color="gray",alpha=0.4)
plt.xlabel("Magnitude of rising and falling edge pairs")
plt.ylabel("Frequency")
format_axes(plt.gca())
plt.grid(False)
plt.title("")
plt.tight_layout()
plt.savefig("../../figures/fridge/hart_level.pdf", bbox_inches="tight")
Exemplo n.º 8
0
    if algo == "Hart":
        hart_dict = o_dict
        print algo, o_dict
        continue
    for metric in ["Extra homes predicted", "Missed homes"]:
        output[metric][N][K][algo] = o_dict[metric]
    print algo, N, K, o_dict

latexify(columns=2, fig_height=3.6)
fig, ax = plt.subplots(nrows=2, ncols=3, sharey=True, sharex=True)

for row_num, metric in enumerate(["Missed homes", "Extra homes predicted"]):
    for col_num, num_states in enumerate(['2', '3', '4']):
        df = pd.DataFrame(output[metric][num_states]).T
        df.plot(kind="bar", ax=ax[row_num, col_num], rot=0, legend=False)
        format_axes(ax[row_num, col_num])
        ax[row_num, col_num].axhline(y=hart_dict[metric],
                                     linestyle="--",
                                     color='red',
                                     label="Hart")
        #ax[row_num, col_num].axhline(y=gt[metric], linestyle="-",color='black', label="Submetered")
        #ax[row_num, col_num].axhline(y=hart[metric], linestyle="--",color='red', label="Hart")

        ax[0, col_num].set_title("N=%s" % num_states)
        ax[-1, col_num].set_xlabel("Top-K")

ax[0, 0].set_ylabel("Missed homes\n(out of 12)")

ax[1, 0].set_ylabel("Extra homes predicted\n(out of 84)")
#ax[0,0].set_ylim((0,8))
#ax[1,0].set_ylim((0,5))
    for i in range(len(df)):
        y_df = df.ix[i]
        y = df.ix[i][f].values.astype(float)
        y = np.append(y, mapping[pred_labels[i]]["value"])
        s1 = inter.InterpolatedUnivariateSpline (x, y,k=4)

        ax[plot_num/2,plot_num%2].plot(np.linspace(0, 1, 1000), s1(np.linspace(0, 1, 1000)), alpha=0.5, color=mapping[y_df["hvac_class"]]["color"],zorder=mapping[y_df["hvac_class"]]["value"]*20)
    algo, N, K = return_name(folder)

    for x in xpos:
        ax[plot_num/2,plot_num%2].axvspan(xmin=x,xmax=x,ymin=0,ymax=1,color="white",alpha=0.5)
    a = confusion_matrix(true_labels, pred_labels)
    fn = a[0,1]
    fp = a[1,0]
    ax[plot_num/2,plot_num%2].set_title(algo+"\nFP= %d, FN= %d" %(fp, fn))
    format_axes(ax[plot_num/2][plot_num%2])
    ax[plot_num/2,plot_num%2].set_xticks(xpos)
    ax[plot_num/2,plot_num%2].set_yticks([0,1])
    ax[plot_num/2,plot_num%2].set_axis_bgcolor('black')

ax[1,0].set_xticklabels(map(lambda x:x.replace("_","\n"),f))
ax[1,1].set_xticklabels(map(lambda x:x.replace("_","\n"),f))

ax[0,0].set_yticklabels(["Feedback", "No feedback"], rotation=90)
ax[1,0].set_yticklabels(["Feedback", "No feedback"], rotation=90)



plt.tight_layout()
plt.savefig(os.path.expanduser("~/git/nilm-actionable/figures/hvac/hvac_feedback_spline.pdf"),bbox_inches="tight")
plt.show()
Exemplo n.º 10
0
                              zorder=mapping[y_df["hvac_class"]]["value"] * 20)
    algo, N, K = return_name(folder)

    for x in xpos:
        ax[plot_num / 2, plot_num % 2].axvspan(xmin=x,
                                               xmax=x,
                                               ymin=0,
                                               ymax=1,
                                               color="white",
                                               alpha=0.5)
    a = confusion_matrix(true_labels, pred_labels)
    fn = a[0, 1]
    fp = a[1, 0]
    ax[plot_num / 2,
       plot_num % 2].set_title(algo + "\nFP= %d, FN= %d" % (fp, fn))
    format_axes(ax[plot_num / 2][plot_num % 2])
    ax[plot_num / 2, plot_num % 2].set_xticks(xpos)
    ax[plot_num / 2, plot_num % 2].set_yticks([0, 1])
    ax[plot_num / 2, plot_num % 2].set_axis_bgcolor('black')

ax[1, 0].set_xticklabels(map(lambda x: x.replace("_", "\n"), f))
ax[1, 1].set_xticklabels(map(lambda x: x.replace("_", "\n"), f))

ax[0, 0].set_yticklabels(["Feedback", "No feedback"], rotation=90)
ax[1, 0].set_yticklabels(["Feedback", "No feedback"], rotation=90)

plt.tight_layout()
plt.savefig(os.path.expanduser(
    "~/git/nilm-actionable/figures/hvac/hvac_feedback_spline.pdf"),
            bbox_inches="tight")
plt.show()
Exemplo n.º 11
0
    x_sort.sort()
    y_upper = x_sort * 1.1
    y_lower = x_sort * 0.9
    ax[i / 4][i % 4].scatter(gt_copy["defrost_percentage"],
                             df["defrost_percentage"],
                             color="gray",
                             alpha=0.6)

    ax[i / 4][i % 4].set_aspect('equal')
    ax[i / 4][i % 4].plot(x_sort, y_upper)
    ax[i / 4][i % 4].plot(x_sort, y_lower)
    percentage = ((x < y_upper) & (x > y_lower)).sum() * 100.0 / len(x)
    ax[i / 4][i % 4].set_title(
        algo + " N=" + str(N) + " K=" + str(K) +
        "\nPercentage of homes within\n 10 percent of submetered=%0.2f" %
        percentage)
    format_axes(ax[i / 4][i % 4])

fig.text(0.5, -0.01, 'Submetered defrost percentage', ha='center')
fig.text(-0.01,
         0.5,
         'Predicted defrost percentage',
         va='center',
         rotation='vertical')

fig.tight_layout()

plt.savefig(os.path.expanduser(
    "~/git/nilm-actionable/figures/fridge/disag_defrost.pdf"),
            bbox_inches="tight")
def plot_overall(out):
    latexify(columns=2, fig_height=3.9)
    fig, ax = plt.subplots(nrows=3, ncols=3, sharex=True)
    output = {}
    for N in range(2, 5):
        output[N] = variation_in_top_k(out, num_states=N)

        for row, metric in enumerate(["f_score", "error energy",
                                      "rmse power"]):
            df = pd.DataFrame(output[N][metric]).T
            if metric == "error energy":
                df = df.mul(100)

            df[["CO", "FHMM"]].plot(kind="bar",
                                    ax=ax[row, N - 2],
                                    rot=0,
                                    legend=False)
            ax[row, N - 2].axhline(y=df["Hart"].median(),
                                   linestyle="-",
                                   color='red',
                                   label="Hart")

            format_axes(ax[row, N - 2])
    ylims = {
        "hvac": {
            0: (0, 1.1),
            1: (0, 40),
            2: (0, 500)
        },
        "fridge": {
            0: (0, 0.8),
            1: (0, 140),
            2: (0, 140)
        }
    }
    """
    for i in range(3):
        ax[0,i].set_ylim(ylims[appliance][0])
    for i in range(3):
        ax[1,i].set_ylim(ylims[appliance][1])
    for i in range(3):
        ax[2,i].set_ylim(ylims[appliance][2])
    for i in range(3):
        ax[-1,i].set_xlabel("Top-K")
        ax[0,i].set_title("N=%d" %(i+2))
    """
    ax[0, 0].set_ylabel("F score\n (Higher is better)")
    ax[1, 0].set_ylabel(r"\% Error" "\n" "in Energy" "\n" "(Lower is better)")
    ax[2, 0].set_ylabel(
        "Root mean squared error\n in power (W)\n (Lower is better)")

    co_patch = mpatches.Patch(color='blue', label='CO')
    fhmm_patch = mpatches.Patch(color='green', label='FHMM')
    hart_patch = mpatches.Patch(
        color='red',
        label='Hart',
    )
    fig.tight_layout()
    fig.legend(loc='upper center',
               bbox_to_anchor=(0.5, 1.05),
               ncol=4,
               handles=[co_patch, fhmm_patch, hart_patch],
               labels=["CO", "FHMM", "Hart"])

    plt.savefig(os.path.join(
        script_path,
        "../../figures/%s/%s_accuracy_nilm.pdf" % (appliance, appliance)),
                bbox_inches="tight")
    dfs[fridge_number] = dfs[fridge_number]["2015-04-12":"2015-04-12 10:00"]

PLOT = False

if PLOT:
    from common_functions import latexify, format_axes
    latexify(columns=1, fig_height=4.4)

    fig, ax = plt.subplots(nrows=4, sharex=True)
    count = 0
    for fridge_number, fridge_df in dfs.iteritems():
        print fridge_number
        fridge_df["power"].plot(ax=ax[count])
        ax[count].set_ylabel("Power (W)")
        ax[count].set_title("Fridge %d" %fridge_number)
        format_axes(ax[count])
        count += 1
    ax[count-1].set_xlabel("Time")
    plt.tight_layout()
    plt.savefig("../../figures/fridge/rice_hall_comparison.pdf")
    plt.savefig("../../figures/fridge/rice_hall_comparison.png")
    plt.show()


#Printing the average steady state and transient power


print "Fridge #, Transient_power, Steady_State_power"
for fridge_number, fridge_df in dfs.iteritems():
    df = fridge_df["power"]
Exemplo n.º 14
0
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
             (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
             (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
             (227, 119, 194), (247, 182, 210), (127, 127, 127),
             (199, 199, 199), (188, 189, 34), (219, 219, 141), (23, 190, 207),
             (158, 218, 229)]

# Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
for i in range(len(tableau20)):
    r, g, b = tableau20[i]
    tableau20[i] = (r / 255., g / 255., b / 255.)

latexify(columns=1, fig_height=1.7)
ax = result_df.plot(colors=[tableau20[0], tableau20[4], tableau20[6]])
ax.set_xlabel(r"Percentage threshold ($P$)")
format_axes(ax)
plt.ylim((-0.1, 1.1))

plt.tight_layout()

L = ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.35), ncol=3)
L.get_texts()[0].set_text(r'Usage Energy' '\nProportion')
L.get_texts()[1].set_text(r'Precision')
L.get_texts()[2].set_text(r'Recall')
#plt.tight_layout()

plt.savefig("../../figures/fridge/fridge_activity_energy.png",
            bbox_inches="tight")
plt.savefig("../../figures/fridge/fridge_activity_energy.pdf",
            bbox_inches="tight")
"""
Exemplo n.º 15
0
from nilmtk.metrics import f1_score

import warnings
warnings.filterwarnings("ignore")

ds = DataSet("/Users/nipunbatra/Downloads/wikienergy-2.h5")
fridges = nilmtk.global_meter_group.select_using_appliances(type='fridge')

f_id = 110
b_id = 236
elec = ds.buildings[b_id].elec
mains = elec.mains()

h = Hart85()
h.train(elec.mains())

h2 = h.pair_df['T1 Active']

sys.path.append("../common")

from common_functions import latexify, format_axes

latexify(fig_height=1.2)
ax = h2[(h2 < 500)].hist(bins=10, color="gray", alpha=0.4)
plt.xlabel("Magnitude of rising and falling edge pairs")
plt.ylabel("Frequency")
format_axes(plt.gca())
plt.grid(False)
plt.title("")
plt.tight_layout()
plt.savefig("../../figures/fridge/hart_level.pdf", bbox_inches="tight")
        output[metric][N][K][algo] = o_dict[metric]

gt = {}
hart = {}
gt["Precision"], gt["Recall"], gt["Accuracy"] = find_p_r_a(gt_confusion)
hart["Precision"], hart["Recall"], hart["Accuracy"] = find_p_r_a(hart_confusion)


latexify(columns=2, fig_height=4.8)
fig, ax = plt.subplots(nrows=3, ncols=3, sharey=True, sharex=True)

for row_num, metric in enumerate(["Precision", "Recall", "Accuracy"]):
    for col_num, num_states in enumerate(['2','3','4']):
        df = pd.DataFrame(output[metric][num_states]).T
        df.plot(kind="bar", ax=ax[row_num, col_num], rot=0, legend=False)
        format_axes(ax[row_num, col_num])
        ax[row_num, col_num].axhline(y=gt[metric], linestyle="-",color='black', label="Submetered")
        ax[row_num, col_num].axhline(y=hart[metric], linestyle="--",color='red', label="Hart")
        ax[row_num, col_num].set_xlabel("Top-K")
        ax[0, col_num].set_title("N=%s" %num_states)
    ax[row_num,0].set_ylabel(metric)

co_patch = mpatches.Patch(color='blue', label='CO')
fhmm_patch =  mpatches.Patch(color='green', label='FHMM')
submetered_patch =  mpatches.Patch(color='black', label='Submetered', lw='0.2')
hart_patch = mpatches.Patch(color='red', label='Hart',lw='0.6')
plt.tight_layout()

fig.legend(loc='upper center', bbox_to_anchor=(0.5, 1),
          ncol=4,handles=[co_patch, fhmm_patch, submetered_patch, hart_patch],
           labels=["CO","FHMM","Submetered","Hart"])
        #            )
    #ax.set_ylabel("Transient power (W)")

    #ax.set_title(title)
    format_axes(ax)



plot_malfunction(wiki_malfunction, ax[1], "WikiEnergy data set")
ax[1].set_ylim(70,280) # outliers only
ax[0].set_ylim(1250,1430)

ax[0].scatter([111], [1303],alpha=0)
ax[1].scatter([80],[105],alpha=0)

format_axes(ax[1])
format_axes(ax[0])

ax[0].spines['top'].set_visible(False)
ax[0].spines['bottom'].set_visible(False)
ax[1].spines['top'].set_visible(False)
ax[0].xaxis.tick_top()
ax[0].tick_params(labeltop='off') # don't put tick labels at the top
#ax[1].xaxis.tick_bottom()

for i, (fridge_floor, fridge_power) in enumerate(rice_malfunction.iteritems()):
    print fridge_power
    ax[0].scatter([fridge_power[0]], [fridge_power[1]], label=fridge_floor, color=colors[i],alpha=0)

ax[1].set_xlabel("Steady state power (W)")
#ax[1].set_title("Rice Hall")