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")
In [2]:
we = DataSet('/Users/nipunbatra/wikienergy-2.h5')
print('loaded ' + str(len(we.buildings)) + ' buildings')
In [3]:
building_number = 1
print_dict(we.buildings[building_number].metadata)
In [4]:
elec = we.buildings[building_number].elec
elec.appliances
Out[4]:
In [5]:
train = DataSet('/Users/nipunbatra/wikienergy-2.h5')
test = DataSet('/Users/nipunbatra/wikienergy-2.h5')
In [6]:
train.buildings[1].elec.mains().plot()
Out[6]:
In [7]:
train.set_window(end="30-4-2014")
test.set_window(start="30-4-2014")
In [8]:
train_elec = train.buildings[1].elec
test_elec = test.buildings[1].elec
In [9]:
train_elec.mains().plot()
Out[9]:
In [10]:
test_elec.mains().plot()
Out[10]:
In [11]:
top_2_train_elec = train_elec.submeters().select_top_k(k=2)
In [12]:
top_2_train_elec
Out[12]:
In [13]:
test_mains_df = test_elec.mains().load().next()
In [14]:
from nilmtk.disaggregate import Hart85
h = Hart85()
In [15]:
h.train(train_elec.mains())
In [19]:
from nilmtk.feature_detectors.steady_states import find_steady_states_transients, find_steady_states
[_, transients] = find_steady_states(test_mains_df, h.cols, h.state_threshold, h.noise_level)
In [21]:
hart_pred_df = h.disaggregate_single_chunk(test_mains_df, {}, transients )
In [22]:
hart_pred_df[[0]].head()
Out[22]:
In [23]:
disag_filename = '/Users/nipunbatra/Desktop/test_hart.h5'
output = HDFDataStore(disag_filename, 'w')
h.disaggregate(test_elec.mains(), output, sample_period=60)
output.close()
disag = DataSet(disag_filename)
disag_elec = disag.buildings[building_number].elec
In [29]:
ax = hart_pred_df[[0]].head(1000).plot()
test_mains_df.head(1000).plot(ax=ax)
disag_elec[('unknown', 0)].load().next().head(1000).plot(ax=ax)
Out[29]:
In [46]:
fhmm = fhmm_exact.FHMM()
fhmm.train(top_2_train_elec, sample_period=60)
In [47]:
d=fhmm.disaggregate_chunk(test_mains_df).head()
In [62]:
def find_specific_appliance(appliance_name, appliance_instance, list_of_elecs):
for elec_name in list_of_elecs:
appl = elec_name.appliances[0]
if (appl.identifier.type, appl.identifier.instance) == (appliance_name, appliance_instance):
return elec_name
In [64]:
d[[find_specific_appliance('air conditioner', 1, d.columns.tolist())]]
Out[64]:
In [53]:
a = d.columns.tolist()[0].appliances[0]
In [61]:
a.identifier.type, a.identifier.instance
Out[61]:
In [55]:
a.type['type']=="fridge"
Out[55]:
In [48]:
from nilmtk.disaggregate import CombinatorialOptimisation
In [31]:
co = CombinatorialOptimisation()
In [32]:
co.train(top_2_train_elec, sample_period=60)
In [33]:
pred_df = co.disaggregate_chunk(test_mains_df)
In [37]:
pred_df.head()
Out[37]:
In [34]:
co.model
Out[34]:
In [44]:
co.model[0]['training_metadata'].appliances[0].type['type']
Out[44]:
In [ ]:
[co.model[i]['training_metadata'] for i in pred_df.columns]
In [65]:
pred_df.columns = [co.model[i]['training_metadata'] for i in pred_df.columns]
In [66]:
pred_df.head()
Out[66]:
In [ ]:
h = Hart85()
In [ ]:
In [ ]:
from nilmtk.feature_detectors.steady_states import find_steady_states_transients, find_steady_states
[_, transients] = find_steady_states(test_mains_df, h.cols, h.state_threshold, h.noise_level)
In [ ]:
In [ ]:
hart_pred_df.head()
In [ ]:
In [ ]:
h.centroids.index.values
In [ ]:
hart_pred_df.head()
In [ ]:
chunk = test_elec.mains().power_series().next()
In [ ]:
cols = pd.MultiIndex.from_tuples([chunk.name])
In [ ]:
cols
In [ ]:
meter = 0
In [ ]:
df = hart_pred_df[[meter]]
In [ ]:
df.columns = cols
In [ ]:
df
In [ ]:
In [ ]:
disag_elec
In [ ]:
disag_elec.plot()
In [ ]:
st = pd.HDFStore(disag_filename)
In [ ]:
d = disag_elec[('unknown',0)].load().next()
In [ ]:
d.tail()
In [ ]:
hart_pred_df[[0]].tail()
In [ ]:
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 [ ]:
# CSS styling
from IPython.core.display import display, HTML
display(HTML(open('static/styles.css', 'r').read()));