In [1]:
from __future__ import print_function, division
import pandas as pd
import numpy as np
from copy import deepcopy
from os.path import join, isdir, isfile, dirname, abspath
from os import listdir, getcwd
import re
from sys import stdout, getfilesystemencoding
from nilmtk.datastore import Key
from nilmtk.timeframe import TimeFrame
from nilmtk.measurement import LEVEL_NAMES
from nilm_metadata import convert_yaml_to_hdf5
from inspect import currentframe, getfile, getsourcefile
from copy import copy

In [2]:
import matplotlib.pyplot as plt

In [3]:
from nilmtk import DataSet, TimeFrame
from nilmtk.disaggregate import CombinatorialOptimisation

In [4]:
train = DataSet("/home/nipun/Desktop/redd.h5")
test = copy(train)

In [5]:
elec = train.buildings[2].elec

In [6]:
building_2 = train.buildings[2]

In [7]:
elec.map_meter_instances_to_appliance_ids()


Out[7]:
{1: [],
 2: [],
 3: [Appliance(type='sockets', instance=1)],
 4: [Appliance(type='light', instance=1)],
 5: [Appliance(type='electric stove', instance=1)],
 6: [Appliance(type='microwave', instance=1)],
 7: [Appliance(type='washer dryer', instance=1)],
 8: [Appliance(type='sockets', instance=2)],
 9: [Appliance(type='fridge', instance=1)],
 10: [Appliance(type='dish washer', instance=1)],
 11: [Appliance(type='waste disposal unit', instance=1)]}

Finding top-k appliances for training


In [25]:
top_k = elec.select_top_k(k=6)

In [26]:
top_k


Out[26]:
MeterGroup(meters=
  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=6, building=2, dataset='REDD', appliances=[Appliance(type='microwave', instance=1)])
  ElecMeter(instance=8, building=2, dataset='REDD', appliances=[Appliance(type='sockets', instance=2)])
  ElecMeter(instance=9, building=2, dataset='REDD', appliances=[Appliance(type='fridge', instance=1)])
  ElecMeter(instance=10, building=2, dataset='REDD', appliances=[Appliance(type='dish washer', instance=1)])
)

In [27]:
elec.energy_per_meter().T


Out[27]:
active apparent
1 NaN 14.358204
2 NaN 62.864776
3 2.066441 NaN
4 8.886603 NaN
5 0.483484 NaN
6 5.141312 NaN
7 0.714265 NaN
8 3.431987 NaN
9 26.634318 NaN
10 2.992981 NaN
11 0.026742 NaN

In [28]:
split_point = elec.train_test_split(0.5)

In [29]:
split_point


Out[29]:
Timestamp('2011-04-25 03:22:04-0400', tz='US/Eastern')

In [30]:
elec.good_sections()


Out[30]:
[TimeFrame(start='2011-04-17 19:18:27-04:00', end='2011-04-17 19:22:04-04:00', empty=False),
 TimeFrame(start='2011-04-17 19:25:09-04:00', end='2011-04-17 19:26:14-04:00', empty=False),
 TimeFrame(start='2011-04-17 21:45:01-04:00', end='2011-04-17 21:45:25-04:00', empty=False),
 TimeFrame(start='2011-04-17 22:18:13-04:00', end='2011-04-17 22:18:26-04:00', empty=False),
 TimeFrame(start='2011-04-17 23:09:35-04:00', end='2011-04-17 23:09:43-04:00', empty=False),
 TimeFrame(start='2011-04-17 23:15:29-04:00', end='2011-04-17 23:15:37-04:00', empty=False),
 TimeFrame(start='2011-04-17 23:30:57-04:00', end='2011-04-17 23:31:21-04:00', empty=False),
 TimeFrame(start='2011-04-17 23:42:54-04:00', end='2011-04-17 23:43:03-04:00', empty=False),
 TimeFrame(start='2011-04-18 00:00:53-04:00', end='2011-04-18 00:01:02-04:00', empty=False),
 TimeFrame(start='2011-04-18 00:09:34-04:00', end='2011-04-18 01:21:53-04:00', empty=False),
 TimeFrame(start='2011-04-18 01:22:41-04:00', end='2011-04-18 01:29:10-04:00', empty=False),
 TimeFrame(start='2011-04-18 01:31:36-04:00', end='2011-04-19 12:13:37-04:00', empty=False),
 TimeFrame(start='2011-04-19 12:14:16-04:00', end='2011-04-19 18:45:17-04:00', empty=False),
 TimeFrame(start='2011-04-19 20:20:07-04:00', end='2011-04-21 17:45:11-04:00', empty=False),
 TimeFrame(start='2011-04-21 19:41:34-04:00', end='2011-04-22 02:08:11-04:00', empty=False),
 TimeFrame(start='2011-04-22 02:11:36-04:00', end='2011-04-22 22:47:03-04:00', empty=False),
 TimeFrame(start='2011-04-22 22:48:39-04:00', end='2011-04-23 12:23:17-04:00', empty=False),
 TimeFrame(start='2011-04-23 12:24:06-04:00', end='2011-04-23 13:36:09-04:00', empty=False),
 TimeFrame(start='2011-04-23 13:36:50-04:00', end='2011-04-23 17:02:16-04:00', empty=False),
 TimeFrame(start='2011-04-23 17:04:59-04:00', end='2011-04-24 20:46:50-04:00', empty=False),
 TimeFrame(start='2011-04-24 20:47:30-04:00', end='2011-04-25 19:27:17-04:00', empty=False),
 TimeFrame(start='2011-04-25 19:32:14-04:00', end='2011-04-27 03:17:55-04:00', empty=False),
 TimeFrame(start='2011-04-27 03:21:47-04:00', end='2011-04-29 17:48:41-04:00', empty=False),
 TimeFrame(start='2011-04-29 17:48:42-04:00', end='2011-05-01 01:03:59-04:00', empty=False),
 TimeFrame(start='2011-05-01 01:06:15-04:00', end='2011-05-01 23:04:44-04:00', empty=False),
 TimeFrame(start='2011-05-22 15:57:03-04:00', end='2011-05-22 19:59:16-04:00', empty=False)]

In [31]:
train.store.window = TimeFrame(start=None, end = split_point)
test.store.window = TimeFrame(start=split_point, end=None)

In [32]:
from nilmtk import DataSet, TimeFrame
from nilmtk.disaggregate import CombinatorialOptimisation

In [33]:
co = CombinatorialOptimisation()

In [34]:
co.train(top_k)

In [35]:
co.model


Out[35]:
{3: array([  0,  14, 777], dtype=int32),
 4: array([  0,  20, 155], dtype=int32),
 6: array([   0,   49, 1826], dtype=int32),
 8: array([   0,  191, 1054], dtype=int32),
 9: array([  0, 163, 410], dtype=int32),
 10: array([   0,  208, 1199], dtype=int32)}

In [36]:
from nilmtk import HDFDataStore
output = HDFDataStore('output.h5', 'w')
test_mains = test.buildings[2].elec.mains()
co.disaggregate(test_mains, output)

In [37]:
output.store.keys()


Out[37]:
['/building2/elec/meter10',
 '/building2/elec/meter1_2',
 '/building2/elec/meter3',
 '/building2/elec/meter4',
 '/building2/elec/meter6',
 '/building2/elec/meter8',
 '/building2/elec/meter9']

In [38]:
%matplotlib inline

In [39]:
fig, ax = plt.subplots(nrows=2, sharex=True)
output.store['/building2/elec/meter9'][:'May 3 2011'].plot(label='Predicted', ax=ax[0])
train.buildings[2].elec['fridge'].power_series_all_data()[:'May 3 2011'].plot(ax=ax[1])
plt.legend()


Out[39]:
<matplotlib.legend.Legend at 0x7f7c6e114a90>

In [41]:
output.store['fridge']


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-41-2aad1e896a87> in <module>()
----> 1 output.store['fridge']

/home/nipun/anaconda/lib/python2.7/site-packages/pandas/io/pytables.pyc in __getitem__(self, key)
    414 
    415     def __getitem__(self, key):
--> 416         return self.get(key)
    417 
    418     def __setitem__(self, key, value):

/home/nipun/anaconda/lib/python2.7/site-packages/pandas/io/pytables.pyc in get(self, key)
    617         group = self.get_node(key)
    618         if group is None:
--> 619             raise KeyError('No object named %s in the file' % key)
    620         return self._read_group(group)
    621 

KeyError: 'No object named fridge in the file'

In [ ]: