In [1]:
from nilmtk import *
from nilmtk.disaggregate import fhmm_exact, combinatorial_optimisation

In [2]:
import time

In [3]:
train = DataSet('/data/REDD/redd.h5')
test = DataSet('/data/REDD/redd.h5')

In [4]:
building = 2
train.set_window(end="30-4-2011")
test.set_window(start="30-4-2011")

In [5]:
train_elec = train.buildings[building].elec
test_elec = test.buildings[building].elec

In [6]:
%matplotlib inline


/Users/nipunbatra/anaconda/lib/python2.7/site-packages/ipykernel/pylab/config.py:66: DeprecationWarning: metadata {'config': True} was set from the constructor.  Metadata should be set using the .tag() method, e.g., Int().tag(key1='value1', key2='value2')
  inline backend."""
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/ipykernel/pylab/config.py:71: DeprecationWarning: metadata {'config': True} was set from the constructor.  Metadata should be set using the .tag() method, e.g., Int().tag(key1='value1', key2='value2')
  'retina', 'jpeg', 'svg', 'pdf'.""")
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/ipykernel/pylab/config.py:85: DeprecationWarning: metadata {'config': True} was set from the constructor.  Metadata should be set using the .tag() method, e.g., Int().tag(key1='value1', key2='value2')
  use `figure_formats` instead)""")
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/ipykernel/pylab/config.py:95: DeprecationWarning: metadata {'config': True} was set from the constructor.  Metadata should be set using the .tag() method, e.g., Int().tag(key1='value1', key2='value2')
  """
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/ipykernel/pylab/config.py:114: DeprecationWarning: metadata {'config': True} was set from the constructor.  Metadata should be set using the .tag() method, e.g., Int().tag(key1='value1', key2='value2')
  """)
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/ipykernel/pylab/config.py:44: DeprecationWarning: InlineBackend._config_changed is deprecated: use @observe and @unobserve instead.
  def _config_changed(self, name, old, new):
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/traitlets/traitlets.py:770: DeprecationWarning: A parent of InlineBackend._config_changed has adopted the new @observe(change) API
  clsname, change_or_name), DeprecationWarning)
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:96: DeprecationWarning: DisplayFormatter._formatters_default is deprecated: use @default decorator instead.
  def _formatters_default(self):
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:675: DeprecationWarning: PlainTextFormatter._deferred_printers_default is deprecated: use @default decorator instead.
  def _deferred_printers_default(self):
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:667: DeprecationWarning: PlainTextFormatter._singleton_printers_default is deprecated: use @default decorator instead.
  def _singleton_printers_default(self):
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:670: DeprecationWarning: PlainTextFormatter._type_printers_default is deprecated: use @default decorator instead.
  def _type_printers_default(self):
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:670: DeprecationWarning: PlainTextFormatter._type_printers_default is deprecated: use @default decorator instead.
  def _type_printers_default(self):
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:675: DeprecationWarning: PlainTextFormatter._deferred_printers_default is deprecated: use @default decorator instead.
  def _deferred_printers_default(self):

In [7]:
train_elec.plot()


/Users/nipunbatra/git/nilmtk/nilmtk/utils.py:390: FutureWarning: fill_method is deprecated to .resample()
the new syntax is .resample(...).ffill(limit=1)
  data = data.resample(**resample_kwargs)
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/dateutil/rrule.py:470: DeprecationWarning: Using both 'count' and 'until' is inconsistent with RFC 2445 and has been deprecated in dateutil. Future versions will raise an error.
  "raise an error.", DeprecationWarning)
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:90: DeprecationWarning: DisplayFormatter._ipython_display_formatter_default is deprecated: use @default decorator instead.
  def _ipython_display_formatter_default(self):
/Users/nipunbatra/anaconda/lib/python2.7/site-packages/IPython/core/formatters.py:667: DeprecationWarning: PlainTextFormatter._singleton_printers_default is deprecated: use @default decorator instead.
  def _singleton_printers_default(self):
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x111ebb1d0>

In [8]:
test_elec.plot()


Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x112d22310>

In [9]:
start = time.time()
clf = combinatorial_optimisation.CombinatorialOptimisation()
clf.train(train_elec, sample_period=120)
end = time.time()
print("Runtime =", end-start, "seconds.")


Training model for submeter 'ElecMeter(instance=3, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=1)])'
Training model for submeter 'ElecMeter(instance=4, building=2, dataset='REDD', appliances=[Appliance(type='light', instance=1)])'
Training model for submeter 'ElecMeter(instance=5, building=2, dataset='REDD', appliances=[Appliance(type='electric stove', instance=1)])'
Training model for submeter 'ElecMeter(instance=6, building=2, dataset='REDD', appliances=[Appliance(type='microwave', instance=1)])'
Training model for submeter 'ElecMeter(instance=7, building=2, dataset='REDD', appliances=[Appliance(type='washer dryer', instance=1)])'
Training model for submeter 'ElecMeter(instance=8, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=2)])'
Training model for submeter 'ElecMeter(instance=9, building=2, dataset='REDD', appliances=[Appliance(type='fridge', instance=1)])'
Training model for submeter 'ElecMeter(instance=10, building=2, dataset='REDD', appliances=[Appliance(type='dish washer', instance=1)])'
Training model for submeter 'ElecMeter(instance=11, building=2, dataset='REDD', appliances=[Appliance(type='waste disposal unit', instance=1)])'
Done training!
('Runtime =', 3.609100818634033, 'seconds.')

In [10]:
import pandas as pd
import numpy as np

In [11]:
pred = {}
gt= {}

for i, chunk in enumerate(test_elec.mains().load(sample_period=120)):
    chunk_drop_na = chunk.dropna()
    pred[i] = clf.disaggregate_chunk(chunk_drop_na)
    gt[i]={}
    
    for meter in test_elec.submeters().meters:
        # Only use the meters that we trained on (this saves time!)    
        gt[i][meter] = meter.load(sample_period=120).next()
    gt[i] = pd.DataFrame({k:v.squeeze() for k,v in gt[i].iteritems()}, index=gt[i].values()[0].index).dropna()


Loading data for meter ElecMeterID(instance=2, building=2, dataset='REDD')     
Done loading data all meters for this chunk.
Estimating power demand for 'ElecMeter(instance=3, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=1)])'
Estimating power demand for 'ElecMeter(instance=4, building=2, dataset='REDD', appliances=[Appliance(type='light', instance=1)])'
Estimating power demand for 'ElecMeter(instance=5, building=2, dataset='REDD', appliances=[Appliance(type='electric stove', instance=1)])'
Estimating power demand for 'ElecMeter(instance=6, building=2, dataset='REDD', appliances=[Appliance(type='microwave', instance=1)])'
Estimating power demand for 'ElecMeter(instance=7, building=2, dataset='REDD', appliances=[Appliance(type='washer dryer', instance=1)])'
Estimating power demand for 'ElecMeter(instance=8, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=2)])'
Estimating power demand for 'ElecMeter(instance=9, building=2, dataset='REDD', appliances=[Appliance(type='fridge', instance=1)])'
Estimating power demand for 'ElecMeter(instance=10, building=2, dataset='REDD', appliances=[Appliance(type='dish washer', instance=1)])'
Estimating power demand for 'ElecMeter(instance=11, building=2, dataset='REDD', appliances=[Appliance(type='waste disposal unit', instance=1)])'
/Users/nipunbatra/git/nilmtk/nilmtk/utils.py:336: FutureWarning: 
.resample() is now a deferred operation
You called index(...) on this deferred object which materialized it into a series
by implicitly taking the mean.  Use .resample(...).mean() instead
  return resampled.index[0]

In [12]:
clf.disaggregate_chunk(chunk_drop_na)


Estimating power demand for 'ElecMeter(instance=3, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=1)])'
Estimating power demand for 'ElecMeter(instance=4, building=2, dataset='REDD', appliances=[Appliance(type='light', instance=1)])'
Estimating power demand for 'ElecMeter(instance=5, building=2, dataset='REDD', appliances=[Appliance(type='electric stove', instance=1)])'
Estimating power demand for 'ElecMeter(instance=6, building=2, dataset='REDD', appliances=[Appliance(type='microwave', instance=1)])'
Estimating power demand for 'ElecMeter(instance=7, building=2, dataset='REDD', appliances=[Appliance(type='washer dryer', instance=1)])'
Estimating power demand for 'ElecMeter(instance=8, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=2)])'
Estimating power demand for 'ElecMeter(instance=9, building=2, dataset='REDD', appliances=[Appliance(type='fridge', instance=1)])'
Estimating power demand for 'ElecMeter(instance=10, building=2, dataset='REDD', appliances=[Appliance(type='dish washer', instance=1)])'
Estimating power demand for 'ElecMeter(instance=11, building=2, dataset='REDD', appliances=[Appliance(type='waste disposal unit', instance=1)])'
Out[12]:
ElecMeter(instance=11, building=2, dataset='REDD', appliances=[Appliance(type='waste disposal unit', instance=1)]) ElecMeter(instance=8, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=2)]) ElecMeter(instance=7, building=2, dataset='REDD', appliances=[Appliance(type='washer dryer', instance=1)]) ElecMeter(instance=3, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=1)]) ElecMeter(instance=4, building=2, dataset='REDD', appliances=[Appliance(type='light', instance=1)]) ElecMeter(instance=10, building=2, dataset='REDD', appliances=[Appliance(type='dish washer', instance=1)]) ElecMeter(instance=6, building=2, dataset='REDD', appliances=[Appliance(type='microwave', instance=1)]) ElecMeter(instance=5, building=2, dataset='REDD', appliances=[Appliance(type='electric stove', instance=1)]) ElecMeter(instance=9, building=2, dataset='REDD', appliances=[Appliance(type='fridge', instance=1)])
2011-04-30 00:00:00-04:00 0 0 0 0 25 0 45 38 162
2011-04-30 00:02:00-04:00 0 0 0 0 25 0 45 38 162
2011-04-30 00:04:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:06:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:08:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:10:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:12:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:14:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:16:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:18:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:20:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:22:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:24:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:26:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:28:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:30:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:32:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:34:00-04:00 0 0 0 14 25 0 45 38 162
2011-04-30 00:36:00-04:00 0 0 0 0 25 211 45 0 0
2011-04-30 00:38:00-04:00 0 0 0 0 25 211 45 0 0
2011-04-30 00:40:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:42:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:44:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:46:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:48:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:50:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:52:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:54:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:56:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:58:00-04:00 0 0 0 0 0 0 0 38 0
... ... ... ... ... ... ... ... ... ...
2011-05-22 19:00:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:02:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:04:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:06:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:08:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:10:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:12:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:14:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:16:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:18:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:20:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:22:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:24:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:26:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:28:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:30:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:32:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:34:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:36:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:38:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:40:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:42:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:44:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:46:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:48:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:50:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:52:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:54:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:56:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:58:00-04:00 0 0 0 14 0 211 0 38 0

1534 rows × 9 columns


In [13]:
# If everything can fit in memory
gt_overall = pd.concat(gt)
gt_overall.index = gt_overall.index.droplevel()
pred_overall = pd.concat(pred)
pred_overall.index = pred_overall.index.droplevel()

# Having the same order of columns
gt_overall = gt_overall[pred_overall.columns]

In [14]:
%matplotlib inline

In [15]:
gt_overall.plot()


Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x112a40f50>

In [16]:
pred_overall.plot()


Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x113df6c90>

In [17]:
pred_overall = pred_overall.ix[gt_overall.index]
gt_overall = gt_overall.ix[pred_overall.index]

In [18]:
from sklearn.metrics import mean_squared_error

In [19]:
pred_overall


Out[19]:
ElecMeter(instance=11, building=2, dataset='REDD', appliances=[Appliance(type='waste disposal unit', instance=1)]) ElecMeter(instance=8, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=2)]) ElecMeter(instance=7, building=2, dataset='REDD', appliances=[Appliance(type='washer dryer', instance=1)]) ElecMeter(instance=3, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=1)]) ElecMeter(instance=4, building=2, dataset='REDD', appliances=[Appliance(type='light', instance=1)]) ElecMeter(instance=10, building=2, dataset='REDD', appliances=[Appliance(type='dish washer', instance=1)]) ElecMeter(instance=6, building=2, dataset='REDD', appliances=[Appliance(type='microwave', instance=1)]) ElecMeter(instance=5, building=2, dataset='REDD', appliances=[Appliance(type='electric stove', instance=1)]) ElecMeter(instance=9, building=2, dataset='REDD', appliances=[Appliance(type='fridge', instance=1)])
2011-04-30 00:02:00-04:00 0 0 0 0 25 0 45 38 162
2011-04-30 00:04:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:06:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:08:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:10:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:12:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:14:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:16:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:18:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:20:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:22:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:24:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:26:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:28:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:30:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:32:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 00:34:00-04:00 0 0 0 14 25 0 45 38 162
2011-04-30 00:36:00-04:00 0 0 0 0 25 211 45 0 0
2011-04-30 00:38:00-04:00 0 0 0 0 25 211 45 0 0
2011-04-30 00:40:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:42:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:44:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:46:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:48:00-04:00 0 0 0 0 25 211 0 38 0
2011-04-30 00:50:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:52:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:54:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:56:00-04:00 0 0 0 14 0 211 45 0 0
2011-04-30 00:58:00-04:00 0 0 0 0 0 0 0 38 0
2011-04-30 01:00:00-04:00 0 0 0 0 0 0 0 38 0
... ... ... ... ... ... ... ... ... ...
2011-05-22 19:00:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:02:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:04:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:06:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:08:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:10:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:12:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:14:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:16:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:18:00-04:00 0 0 0 14 0 211 0 38 0
2011-05-22 19:20:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:22:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:24:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:26:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:28:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:30:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:32:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:34:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:36:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:38:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:40:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:42:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:44:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:46:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:48:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:50:00-04:00 0 0 0 0 0 0 0 38 0
2011-05-22 19:52:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:54:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:56:00-04:00 0 0 0 0 25 0 45 38 162
2011-05-22 19:58:00-04:00 0 0 0 14 0 211 0 38 0

1533 rows × 9 columns


In [20]:
rmse={}
for col in pred_overall.columns:
    rmse[col] = np.sqrt(mean_squared_error(gt_overall[col], pred_overall[col]))

In [21]:
pd.Series(rmse).plot(kind="barh")


Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x1131e9cd0>

In [20]:
pd.Series(rmse).plot(kind="barh")


Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x111febd50>

In [ ]: