In [1]:
import numpy as np
import pandas as pd
from os.path import join
from pylab import rcParams
import matplotlib.pyplot as plt
%matplotlib inline
rcParams['figure.figsize'] = (13, 6)
plt.style.use('ggplot')
import nilmtk
from nilmtk import DataSet, TimeFrame, MeterGroup, HDFDataStore
from nilmtk.disaggregate import CombinatorialOptimisation, fhmm_exact
from nilmtk.utils import print_dict
from nilmtk.metrics import f1_score
import warnings
warnings.filterwarnings("ignore")
The full data set can be downloaded from the remote WikiEnergy database. The credentials are omitted here for security reasons.
In [2]:
# download_wikienergy(database_username, database_password, hdf_filename)
In [3]:
data_dir = '/Users/nipunbatra/Dropbox/nilmtk_datasets/'
we = DataSet(join(data_dir, 'redd.h5'))
print('loaded ' + str(len(we.buildings)) + ' buildings')
In [4]:
building_number = 1
print_dict(we.buildings[building_number].metadata)
In [5]:
elec = we.buildings[building_number].elec
elec.appliances
In [6]:
train = DataSet("/Users/nipunbatra/Dropbox/nilmtk_datasets/redd.h5")
test = DataSet("/Users/nipunbatra/Dropbox/nilmtk_datasets/redd.h5")
In [7]:
train.buildings[1].elec.mains().plot()
Out[7]:
In [8]:
train.set_window(end="30-4-2011")
test.set_window(start="30-4-2011")
In [9]:
train_elec = train.buildings[1].elec
test_elec = test.buildings[1].elec
In [10]:
train_elec.mains().plot()
Out[10]:
In [11]:
test_elec.mains().plot()
Out[11]:
In [12]:
m = train_elec['fridge']
In [13]:
df_raw = m.load().next()
In [14]:
df_raw.head(10)
Out[14]:
In [15]:
df_1_min = m.power_series(sample_period=60).next()
In [16]:
df_1_min.head()
Out[16]:
In [17]:
top_5_train_elec = train_elec.submeters().select_top_k(k=5)
In [18]:
top_5_train_elec
Out[18]:
Let us first train on raw data, no downsampling and see how much time it takes
In [19]:
import time
In [20]:
"""start=time.time()
fhmm = fhmm_exact.FHMM()
fhmm.train(top_5_train_elec)
end=time.time()
print end-start
"""
Out[20]:
In [21]:
start=time.time()
fhmm = fhmm_exact.FHMM()
fhmm.train(top_5_train_elec, sample_period=60)
end=time.time()
print end-start
In [22]:
disag_filename = join(data_dir, 'redd-disag-fhmm-new.h5')
output = HDFDataStore(disag_filename, 'w')
fhmm.disaggregate(test_elec.mains(), output, sample_period=60)
output.close()
In [23]:
disag_filename = join(data_dir, 'redd-disag-fhmm-new.h5')
disag = DataSet(disag_filename)
disag_elec = disag.buildings[building_number].elec
In [24]:
disag_elec.plot()
Out[24]:
In [25]:
f1 = f1_score(disag_elec, test_elec)
f1.index = disag_elec.get_labels(f1.index)
f1.plot(kind='barh')
plt.ylabel('appliance');
plt.xlabel('f-score');
plt.title("FHMM");
In [17]:
# CSS styling
from IPython.core.display import display, HTML
display(HTML(open('static/styles.css', 'r').read()));