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")
Exemple #3
0
import pandas as pd
import matplotlib.pyplot as plt
import os

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

from common_functions import latexify, format_axes
latexify(fig_height=0.7)
df = pd.read_csv("../../data/fridge/usage_defrost_cycles.csv")

splits = ["baseline", "usage", "defrost"]
column_names = [x + "_percentage" for x in splits]

df = df[column_names]
df = df.rename(columns={k: v for k, v in zip(column_names, splits)})

ax = df.plot(kind="box", vert=False)
format_axes(ax)

plt.xlabel("Percentage contribution")
plt.savefig("../../figures/fridge/box.pdf", bbox_inches="tight")
plt.savefig("../../figures/fridge/box.png", bbox_inches="tight")
Exemple #4
0
from common_functions import latexify, format_axes

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")
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")
Exemple #6
0
submetered_homes_feedback = np.array(
    [18, 46, 51, 54, 59, 68, 76, 87, 106, 112, 116, 123, 170])


def return_name(folder):
    algo_name = folder.split("_")[-1]
    algo_N = folder.split("_")[0][1]
    algo_K = folder.split("_")[1][1]
    if algo_name == "Hart":
        return "Hart", 2, 3
    else:
        return algo_name, algo_N, algo_K


latexify(columns=2, fig_height=4.8)
algo_total_with_train = [
    "N2_K3_T50_CO", "N2_K4_T50_CO", "N2_K5_T50_CO", "N2_K6_T50_CO",
    "N3_K3_T50_CO", "N3_K4_T50_CO", "N3_K5_T50_CO", "N3_K6_T50_CO",
    "N4_K3_T50_CO", "N4_K4_T50_CO", "N4_K5_T50_CO", "N4_K6_T50_CO",
    "N2_K3_T50_FHMM", "N2_K4_T50_FHMM", "N2_K5_T50_FHMM", "N2_K6_T50_FHMM",
    "N3_K3_T50_FHMM", "N3_K4_T50_FHMM", "N3_K5_T50_FHMM", "N3_K6_T50_FHMM",
    "N4_K3_T50_FHMM", "N4_K4_T50_FHMM", "N4_K5_T50_FHMM", "N4_K6_T50_FHMM",
    "N2_K3_T50_Hart"
]
algo_total = map(lambda x: x[:5] + x[9:], algo_total_with_train)
#algo_total = ["N2_K3_CO","N2_K4_CO","N2_K3_FHMM","N2_K4_FHMM", "N2_K3_Hart"]
ncols = len(algo_total)
fig, ax = plt.subplots(ncols=3, nrows=3, sharex=True, sharey=True)
output = {}
Exemple #7
0
a
temp
new_df = [[74.3, 62.0, 48.0, 68], [73.6, 61.2, 48.0, 77.6],
    [77.27, 64.9, 56.0, 77.6], [78.5, 70.9, 50.0, 77.6]]
new_df = pd.DataFrame([[74.3, 62.0, 48.0, 68], [73.6, 61.2, 48.0, 77.6], [
                      77.27, 64.9, 56.0, 77.6], [78.5, 70.9, 50.0, 77.6]])
new_df
new_df.T
new_df = new_df.T
new_df
new_df.columns = ["Only Aggregate", "FHMM", "Hart", "Submetered"]]
new_df.columns = ["Only Aggregate", "FHMM", "Hart", "Submetered"]
new_df
new_df.index = a.index[2: ]
new_df
latexify(columns=1)
import sys
sys.path.append("../common")
    from common_functions import latexify, format_axes
latexify(columns=1)
ax = new_df.plot(kind="bar", rot=0)
% matplotlib qt
ax = new_df.plot(kind="bar", rot=0)
ax = new_df.plot(kind="bar", rot=0)
ax.legend(pos=1)
ax.legend(loc=1)
ax.legend(loc=2)
plt.draw()
plt.draw(loc=3)
ax.legend(loc=3)
plt.draw()
Exemple #8
0
d_new = pd.DataFrame(o_new).T
d_new.columns = [
    "total", "baseline", "defrost", "usage", "usage_cycles",
    "non_usage_cycles", "defrost_cycles", "baseline_duty_percent", "total_mins"
]

#d = d[d.usage > 0]
d_new["artifical_sum"] = d_new.baseline + d_new.defrost + d_new.usage

d_new["baseline_percentage"] = d_new.baseline * 100 / d_new.artifical_sum
d_new["defrost_percentage"] = d_new.defrost * 100 / d_new.artifical_sum
d_new["usage_percentage"] = d_new.usage * 100 / d_new.artifical_sum

pairs = [[58, 153], [60, 165], [54, 177], [42, 81]]

latexify()
"""

plot_dict = {}
for part in ["baseline", "defrost", "usage"]:
    plot_dict[part] = {}
    for pair in pairs:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        plot_dict[part][str(pair)] = d_new.ix[pair][part].values
        pd.DataFrame(plot_dict[part]).T.plot(ax=ax, kind='bar', legend=False, rot=0)
        plt.ylabel(str.capitalize(part) + " energy consumption\n per month (kWh)")
        plt.xlabel("Same company fridge pairs")
        format_axes(ax)
        plt.tight_layout()
        plt.savefig("../../figures/fridge/same_fridge_"+part+".png")
clusters_separation = [0]

# define two outlier detection tools to be compared
classifiers = {
    "robust covariance estimator": EllipticEnvelope(contamination=.1)
}

# Compare given classifiers under given settings
xx, yy = np.meshgrid(np.linspace(X.min() - 0.3,
                                 X.max() + 0.1, 500),
                     np.linspace(Y.min() - 5,
                                 Y.max() + 5, 500))
n_inliers = int((1. - outliers_fraction) * n_samples)
n_outliers = int(outliers_fraction * n_samples)

latexify(fig_height=1.5)
# Fit the problem with varying cluster separation
np.random.seed(42)
# Data generation

# Fit the model with the One-Class SVM
# plt.figure(figsize=(10, 5))

clf = EllipticEnvelope(contamination=.1)
# fit the data and tag outliers
clf.fit(XY)
y_pred = clf.decision_function(XY).ravel()
threshold = stats.scoreatpercentile(y_pred, 100 * outliers_fraction)
y_pred = y_pred > threshold
# plot the levels lines and the points
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
import pandas as pd
import matplotlib.pyplot as plt
import os

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

from common_functions import latexify, format_axes
latexify(fig_height=0.7)
df = pd.read_csv("../../data/fridge/usage_defrost_cycles.csv")

splits = ["baseline","usage","defrost"]
column_names = [x+"_percentage" for x in splits]

df = df[column_names]
df = df.rename(columns={k:v for k,v in zip(column_names, splits)})

ax = df.plot(kind="box",vert=False)
format_axes(ax)

plt.xlabel("Percentage contribution")
plt.savefig("../../figures/fridge/box.pdf", bbox_inches="tight")
plt.savefig("../../figures/fridge/box.png", bbox_inches="tight")
    print folder
    print accur
    print confusion_matrix(true_labels, pred_labels)
    a = confusion_matrix(true_labels, pred_labels)
    precision = 1.0 * a[0][0] / (a[0][0] + a[1][0])
    recall = 1.0 * a[0][0] / (a[0][0] + a[0][1])
    overall_accuracy = 1.0 * (a[0][0] + a[1][1]) / (np.sum(a))

    algo_identifier = find_name(folder)
    output[algo_identifier] = {
        "Precision": precision,
        "Recall": recall,
        "Accuracy": overall_accuracy
    }
"""
out_df = pd.DataFrame(output)
#out_df.columns = ["Submetered", "CO (N=2, K=3)","CO (N=3, K=3)","CO (N=3, K=4)","FHMM (N=2, K=3)","FHMM (N=3, K=3)", "Hart"]

latexify(columns=2, fig_height=2.6)
ax = out_df.plot(kind="bar", rot=0)
format_axes(ax)
plt.tight_layout()
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.70, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.savefig(os.path.join(script_path, "../../figures/hvac/","hvac_feedback_nilm.pdf"))
plt.savefig(os.path.join(script_path, "../../figures/hvac/","hvac_feedback_nilm.png"))
"""
import numpy as np

submetered_homes_feedback = np.array([ 18,  46,  51,  54,  59,  68,
                                       76,  87, 106, 112, 116, 123, 170])
DATA_PATH = os.path.join(script_path, "..","..","data/fridge")

from collections import OrderedDict

path_dict = OrderedDict()
path_dict["Submetered"] = "usage_defrost_cycles.csv"
path_dict["CO"] = "N2_K5_CO_usage_defrost_cycles.csv"
path_dict["FHMM"] = "N4_K3_FHMM_usage_defrost_cycles.csv"
path_dict["Hart"]="N2_k3_Hart_usage_defrost_cycles.csv"


latexify(columns=2, fig_height=2.6)
fig, ax = plt.subplots(ncols=4, sharey=True)

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

from sklearn.covariance import EllipticEnvelope

for i, (algo_name, algo_path) in enumerate(path_dict.iteritems()):
    df = pd.read_csv(os.path.join(DATA_PATH,algo_path)).dropna()

    df["usage proportion"] = df["usage_cycles"]/(df["usage_cycles"] + df["non_usage_cycles"])

    X = df["usage proportion"].values
    Y = df["usage_percentage"].values
# Example settings
n_samples = len(df)
outliers_fraction = 0.2
clusters_separation = [0]

# define two outlier detection tools to be compared
classifiers = {
    "robust covariance estimator": EllipticEnvelope(contamination=.1)}

# Compare given classifiers under given settings
xx, yy = np.meshgrid(np.linspace(X.min() - 0.3, X.max() + 0.1, 500), np.linspace(Y.min() - 5, Y.max() + 5, 500))
n_inliers = int((1. - outliers_fraction) * n_samples)
n_outliers = int(outliers_fraction * n_samples)

latexify(fig_height=1.5)
# Fit the problem with varying cluster separation
np.random.seed(42)
# Data generation


# Fit the model with the One-Class SVM
# plt.figure(figsize=(10, 5))

clf = EllipticEnvelope(contamination=.1)
# fit the data and tag outliers
clf.fit(XY)
y_pred = clf.decision_function(XY).ravel()
threshold = stats.scoreatpercentile(y_pred,
                                    100 * outliers_fraction)
y_pred = y_pred > threshold
        "FHMM":"../../data/fridge/N2_K3_FHMM_usage_defrost_cycles.csv",
        "Submetered":"../../data/fridge/usage_defrost_cycles.csv"}


dfs = {}
for algo, algo_csv in paths.iteritems():
    dfs[algo] = pd.read_csv(algo_csv)
    dfs[algo].index = dfs[algo].home
    dfs[algo] = dfs[algo].dropna()
ix = dfs["Submetered"].index

for algo, algo_df in dfs.iteritems():
    ix = algo_df.index.intersection(ix)

baseline_percent = {}
num_usage_cycles = {}
for algo, algo_df in dfs.iteritems():
    baseline_percent[algo] = algo_df.ix[ix]["baseline_duty_percent"]
    num_usage_cycles[algo] = algo_df.ix[ix]["usage_cycles"]

from common_functions import latexify, format_axes

latexify(fig_height=1.4)
ax = pd.DataFrame(baseline_percent).plot(kind="box", color="gray",sym='');
format_axes(plt.gca())
plt.ylabel("Baseline duty percentage")
plt.grid(False)
plt.savefig("/Users/nipunbatra/git/nilm-actionable/figures/fridge/baseline_duty_percent.pdf", bbox_inches="tight")


Exemple #15
0
submetered_homes_feedback = np.array(
    [142, 144, 146, 151, 152, 155, 157, 159, 163, 167, 169, 170])


def return_name(folder):
    algo_name = folder.split("_")[-1]
    algo_N = folder.split("_")[0][1]
    algo_K = folder.split("_")[1][1]
    if algo_name == "Hart":
        return "Hart", 2, 3
    else:
        return algo_name, algo_N, algo_K


latexify(columns=2, fig_height=4.8)
algo_total_with_train = [
    "N3_K3_T50_CO", "N3_K4_T50_CO", "N3_K5_T50_CO", "N3_K6_T50_CO",
    "N4_K3_T50_CO", "N4_K4_T50_CO", "N4_K5_T50_CO", "N4_K6_T50_CO",
    "N3_K3_T50_FHMM", "N3_K4_T50_FHMM", "N3_K5_T50_FHMM", "N3_K6_T50_FHMM",
    "N4_K3_T50_FHMM", "N4_K4_T50_FHMM", "N4_K5_T50_FHMM", "N4_K6_T50_FHMM"
]
algo_total = map(lambda x: x[:5] + x[9:], algo_total_with_train)
#algo_total = ["N2_K3_CO","N2_K4_CO","N2_K3_FHMM","N2_K4_FHMM", "N2_K3_Hart"]
ncols = len(algo_total)
fig, ax = plt.subplots(ncols=3, nrows=3, sharex=True, sharey=True)
output = {}
gt_df = pd.read_csv(
    os.path.expanduser(
        "~/git/nilm-actionable/data/fridge/usage_defrost_cycles.csv"))
Exemple #16
0
script_path = os.path.dirname(os.path.realpath(__file__))

from common_functions import latexify, format_axes

NUM_CLASSES = 2

output = {}
gt_confusion = np.array([[25, 9], [8, 16]])
hart_confusion = np.array([[25, 9], [13, 11]])

from sklearn.externals import joblib

x_num = 5
xpos = np.linspace(0, 1, x_num)

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


def powerset(iterable, N_max):
    xs = list(iterable)
    return combinations(xs, N_max)


def accuracy_multiclass(y_true, y_pred):
    confusion = confusion_matrix(y_true, y_pred)
    length = len(confusion)
    out = 0.0
    for i in xrange(length):
        out += confusion[i, i] * 1.0 / np.sum(confusion[i])
    return out / length
import pandas as pd
import numpy as np
import sys
sys.path.append("../common")
import matplotlib.pyplot as plt
from collections import OrderedDict
import matplotlib.patches as mpatches
from common_functions import latexify, format_axes
latexify(fig_height=3)

fig, ax = plt.subplots(nrows=2, sharex=True)
wiki_malfunction = {
    "Frigidaire\n frt18nrjw1": [[139, 160], [188, 255]],
    "Samsung \n rf266abrs": [[107, 141], [130, 162]],
    "LG \n lfx25960st": [[170, 219], [183, 248]],
    "Samsung \n rfg297aars": [[126, 164], [136, 175]]
}

percent_savings = {
    "Frigidaire\n frt18nrjw1": 26,
    "Samsung \n rf266abrs": 18,
    "LG \n lfx25960st": 7,
    "Samsung \n rfg297aars": 7
}

rice_malfunction = OrderedDict({
    "2nd floor": [111, 1303],
    "3rd floor": [81, 109],
    "4th floor": [81, 116],
    "5th floor": [80, 109]
})
from common_functions import latexify, format_axes

NUM_CLASSES = 2

output ={}
gt_confusion = np.array([[25, 9], [8, 16]])
hart_confusion = np.array([[25, 9],[13,11]])


from sklearn.externals import joblib

x_num = 5
xpos = np.linspace(0, 1, x_num)


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


def powerset(iterable, N_max):
    xs = list(iterable)
    return combinations(xs, N_max)


def accuracy_multiclass(y_true, y_pred):
    confusion = confusion_matrix(y_true, y_pred)
    length = len(confusion)
    out = 0.0
    for i in xrange(length):
        out += confusion[i, i] * 1.0 / np.sum(confusion[i])
    return out / length
from common_functions import latexify, format_axes

NUM_CLASSES = 2

output ={}
gt_confusion = np.array([[25, 9], [8, 16]])
hart_confusion = np.array([[25, 9],[13,11]])


from sklearn.externals import joblib

x_num = 5
xpos = np.linspace(0, 1, x_num)


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


def powerset(iterable, N_max):
    xs = list(iterable)
    return combinations(xs, N_max)


def accuracy_multiclass(y_true, y_pred):
    confusion = confusion_matrix(y_true, y_pred)
    length = len(confusion)
    out = 0.0
    for i in xrange(length):
        out += confusion[i, i] * 1.0 / np.sum(confusion[i])
    return out / length
d_new["baseline_percentage"] = d_new.baseline * 100 / d_new.artifical_sum
d_new["defrost_percentage"] = d_new.defrost * 100 / d_new.artifical_sum
d_new["usage_percentage"] = d_new.usage * 100 / d_new.artifical_sum

pairs = [
    [58, 153],
    [60, 165],
    [54, 177],
    [42, 81]
]




latexify()

"""

plot_dict = {}
for part in ["baseline", "defrost", "usage"]:
    plot_dict[part] = {}
    for pair in pairs:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        plot_dict[part][str(pair)] = d_new.ix[pair][part].values
        pd.DataFrame(plot_dict[part]).T.plot(ax=ax, kind='bar', legend=False, rot=0)
        plt.ylabel(str.capitalize(part) + " energy consumption\n per month (kWh)")
        plt.xlabel("Same company fridge pairs")
        format_axes(ax)
        plt.tight_layout()
    ax.set_ylabel(str.capitalize(field))
    ax.set_xlabel("Percentage threshold")
    format_axes(ax)
    plt.ylim((0, 1))
    plt.tight_layout()
    plt.savefig("../../figures/fridge/%s_fridge_activity.png" %field)
    plt.savefig("../../figures/fridge/%s_fridge_activity.pdf" %field)
    plt.close()
"""

# Plotting precision and recall on a single yy plot

fig = plt.figure()
ax = fig.add_subplot(111)

latexify(columns=1)
#precision_recall_df["precision"].plot(ax=ax)
l1 = ax.plot(precision_recall_df["precision"], 'b-')
ax2 = ax.twinx()
#ax.set_ylabel("Precision")
ax.set_xlabel("Percentage threshold")
l2 = ax2.plot(precision_recall_df["recall"], 'r-')
format_axes(ax)
ax.set_ylim((-0.1, 1.1))
ax2.set_ylim((-0.1, 1.1))

lines = l1+l2
labs = ["Precision", "Recall"]
ax.legend(lines, labs, loc='upper center', bbox_to_anchor=(0.5, 1.25),
          ncol=2, fancybox=True, shadow=True)
Exemple #22
0
evening_energy = pd.DataFrame(evening_energy).apply(wm_to_kwh_per_month)

out = {"CO":{}, "FHMM":{},"Hart":{}}
for algo in ["CO", "FHMM", "Hart"]:
    out[algo]["Morning"] = (morning_mins[algo] - morning_mins["Submetered"]).abs().div(morning_mins["Submetered"]).mul(100).median()
    out[algo]["Night"] = (night_mins[algo] - night_mins["Submetered"]).abs().div(night_mins["Submetered"]).mul(100).median()
#plt.ylim((0,200))
#x_line = evening_energy["Submetered"]
#plt.plot(x_line, 1.2*x_line)
#plt.plot(x_line, 0.8*x_line)

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.)
from common_functions import latexify, format_axes
latexify(fig_height=1.2)
ax = pd.DataFrame(out).T.plot(kind="barh",rot=0, colors=[tableau20[0], tableau20[4]])
format_axes(ax)
#plt.xlabel(r"Mean absolute error")
plt.xlabel("Error in Prediction of Minutes of HVAC Usage (\%)")
#plt.xlabel("$\\frac{|Submetered\,mins-Predicted\,mins| \\times 100 \%}{Submetered\,\,mins}$")
plt.legend(loc='upper left', ncol=1)
plt.tight_layout()
plt.savefig("/Users/nipunbatra/git/nilm-actionable/figures/hvac/mins.pdf", bbox_inches="tight")
Exemple #23
0
    "FHMM": "../../data/fridge/N2_K3_FHMM_usage_defrost_cycles.csv",
    "Submetered": "../../data/fridge/usage_defrost_cycles.csv"
}

dfs = {}
for algo, algo_csv in paths.iteritems():
    dfs[algo] = pd.read_csv(algo_csv)
    dfs[algo].index = dfs[algo].home
    dfs[algo] = dfs[algo].dropna()
ix = dfs["Submetered"].index

for algo, algo_df in dfs.iteritems():
    ix = algo_df.index.intersection(ix)

baseline_percent = {}
num_usage_cycles = {}
for algo, algo_df in dfs.iteritems():
    baseline_percent[algo] = algo_df.ix[ix]["baseline_duty_percent"]
    num_usage_cycles[algo] = algo_df.ix[ix]["usage_cycles"]

from common_functions import latexify, format_axes

latexify(fig_height=1.4)
ax = pd.DataFrame(baseline_percent).plot(kind="box", color="gray", sym='')
format_axes(plt.gca())
plt.ylabel("Baseline duty percentage")
plt.grid(False)
plt.savefig(
    "/Users/nipunbatra/git/nilm-actionable/figures/fridge/baseline_duty_percent.pdf",
    bbox_inches="tight")
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

from sklearn.covariance import EllipticEnvelope
sys.path.append("../common")

from common_functions import latexify, format_axes

script_path = os.path.dirname(os.path.realpath(__file__))

DATA_PATH = os.path.join(script_path, "..","..","data/fridge")

submetered_homes_feedback = np.array([ 18,  46,  51,  54,  59,  68,  76,  87, 106, 112, 116, 123, 170])

latexify(columns=2, fig_height=2.8)
algo_total = ["N2_K3_CO","N2_K4_CO","N2_K3_FHMM","N2_K4_FHMM", "N2_K3_Hart"]
#algo_total = ["N2_K3_CO","N2_K4_CO","N2_K3_FHMM","N2_K4_FHMM", "N2_K3_Hart"]
ncols = len(algo_total)
fig, ax = plt.subplots(ncols=3, sharey=True)

for i, algo in enumerate(algo_total):

    df = pd.read_csv(os.path.join(DATA_PATH, "%s_usage_defrost_cycles.csv" %algo)).dropna()

    df["usage proportion"] = df["usage_cycles"]/(df["usage_cycles"] + df["non_usage_cycles"])

    X = df["usage proportion"].values
    Y = df["usage_percentage"].values

    XY = df[["usage proportion","usage_percentage" ]].values
    4: "../../data/fridge/rice/4.csv",
    3: "../../data/fridge/rice/3.csv",
    2: "../../data/fridge/rice/2.csv"
}

dfs = {}
for fridge_number, fridge_csv_path in paths.iteritems():
    print fridge_number
    dfs[fridge_number] = read_fridge_csv(csv_path=fridge_csv_path)
    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()
from common_functions import latexify, format_axes


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()
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")
    for metric in ["Precision", "Recall", "Accuracy"]:
        if K not in output[metric][N]:
            output[metric][N][K] = {}
    o_dict= {"Precision": precision, "Recall": recall, "Accuracy": overall_accuracy}

    for metric in ["Precision", "Recall", "Accuracy"]:
        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')
final = data + result.residual

final = data + result.residual

# write error report
report_fit(params)

SAVE = False

setpoints = [params['t%d' % i].value for i in range(24)]
energy_df = pd.DataFrame({"energy": energy})
energy_hourly_mean_df = get_hourly_aggregate(energy_df)
temp_hourly_mean_df = get_hourly_aggregate(hour_usage_df[["temperature"]])

from common_functions import latexify, format_axes
latexify(columns=1, fig_height=3.0)
fig, ax = plt.subplots(nrows=2)
ax[0].scatter(data, final, color="gray",alpha=0.4, s=2)
ax[0].set_xlabel("Actual energy consumption(kWh)\n(a)")
ax[0].set_ylabel("Predicted energy\n consumption(kWh)")

ax[1].plot(data[:24], label='Actual')
ax[1].plot(final[:24], label='Predicted')
#plt.fill_between(range(len(data[:24])), data[:24], 0, color='g', alpha=1, label='actual')
#plt.fill_between(range(len(final[:24])), final[:24], 0, color='r', alpha=0.5, label='predicted')
ax[1].legend(loc="upper center")
ax[1].set_xlabel("Hours\n(b)")
ax[1].set_ylabel("Energy (kWh)")

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