In this notebook, we'll do some quick exploratory analysis on the COMBED data set


In [2]:
from nilmtk import DataSet

Download the data set


In [3]:
train = DataSet("/Users/nipunbatra/Desktop/combed.h5")
test = DataSet("/Users/nipunbatra/Desktop/combed.h5")

In [4]:
train.set_window(end="16-6-2014")
test.set_window(start="16-6-2014")

In [5]:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
sns.set_style('white')
%matplotlib inline
import seaborn as sns
from matplotlib import rcParams
rcParams['figure.figsize'] = (16, 8)

In [6]:
from nilmtk.disaggregate import FHMM, CombinatorialOptimisation

In [7]:
train_elec = train.buildings[1].elec

In [9]:
mg_ahu = train_elec.select_using_appliances(type='AHU')

In [10]:
mg_ahu


Out[10]:
MeterGroup(meters=
  ElecMeter(instance=8, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=1)])
  ElecMeter(instance=9, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=2)])
  ElecMeter(instance=10, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=3)])
  ElecMeter(instance=11, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=4)])
)

In [11]:
f = FHMM()
f.train(mg_ahu)


Training model for submeter 'ElecMeter(instance=8, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=1)])'
Training model for submeter 'ElecMeter(instance=9, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=2)])'
Training model for submeter 'ElecMeter(instance=10, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=3)])'
Training model for submeter 'ElecMeter(instance=11, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=4)])'

In [12]:
disag_filename = '/Users/nipunbatra/Desktop/disag-fhmm-ahu.h5'
from nilmtk import HDFDataStore
output = HDFDataStore(disag_filename, 'w')
mains = train_elec.mains()
f.disaggregate(mains, output)


Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in 

In [13]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec
#disag_elec[(7)].plot()
#elec[7].plot()
#plt.tight_layout()
#disag.store.close()

In [25]:
disag_elec[('AHU', 1)].plot()
train_elec[('AHU', 1)].plot();



In [27]:
disag_elec[('AHU', 2)].plot()
train_elec[('AHU', 2)].plot();



In [28]:
disag_elec[('AHU', 3)].plot()
train_elec[('AHU', 3)].plot();



In [29]:
disag_elec[('AHU', 4)].plot()
train_elec[('AHU', 4)].plot();



In [39]:
from nilmtk.metrics import f1_score, fraction_energy_assigned_correctly, mean_normalized_error_power, rms_error_power

In [35]:
f1 = f1_score(disag_elec, train_elec)
f1.index = disag_elec.get_labels(f1.index)
f1.plot(kind='bar')
plt.xlabel('appliance');
plt.ylabel('f-score');



In [36]:
mean_normalized_error_power(disag_elec, train_elec)


Out[36]:
8     1.055965
9     1.924816
10    1.000783
11    1.168101
dtype: float64

In [38]:
fraction_energy_assigned_correctly(disag_elec, train_elec)


13/13 ElecMeter(instance=14, building=1, dataset='combed', appliances=[Appliance(type='sockets', instance=2)])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-38-c1c4b3bd350d> in <module>()
----> 1 fraction_energy_assigned_correctly(disag_elec, train_elec)

/Users/nipunbatra/git/nilmtk/nilmtk/metrics.pyc in fraction_energy_assigned_correctly(predictions, ground_truth)
    104     for meter_instance in predictions_submeters.instance():
    105         fraction += min(fraction_per_meter_ground_truth[meter_instance],
--> 106                         fraction_per_meter_predictions[meter_instance])
    107     return fraction
    108 

/Users/nipunbatra/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in __getitem__(self, key)
    507     def __getitem__(self, key):
    508         try:
--> 509             result = self.index.get_value(self, key)
    510 
    511             if not np.isscalar(result):

/Users/nipunbatra/anaconda/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self, series, key)
   1421 
   1422             try:
-> 1423                 return tslib.get_value_box(s, key)
   1424             except IndexError:
   1425                 raise

/Users/nipunbatra/anaconda/lib/python2.7/site-packages/pandas/tslib.so in pandas.tslib.get_value_box (pandas/tslib.c:14021)()

/Users/nipunbatra/anaconda/lib/python2.7/site-packages/pandas/tslib.so in pandas.tslib.get_value_box (pandas/tslib.c:13788)()

IndexError: index out of bounds

In [41]:
rms_error_power(disag_elec, train_elec)


Out[41]:
8     1576.553378
9     2148.882329
10    2288.499910
11    6597.326643
dtype: float64

In [20]:
c = CombinatorialOptimisation()
c.train(mg_ahu)


Training model for submeter 'ElecMeter(instance=2, building=1, dataset='combed', appliances=[Appliance(type='elevator', instance=1)])'
Training model for submeter 'ElecMeter(instance=3, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=4, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=5, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=6, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=7, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=8, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=1)])'
Training model for submeter 'ElecMeter(instance=9, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=2)])'
Training model for submeter 'ElecMeter(instance=10, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=3)])'
Training model for submeter 'ElecMeter(instance=11, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=4)])'
Training model for submeter 'ElecMeter(instance=12, building=1, dataset='combed', appliances=[Appliance(type='light', instance=1)])'
Training model for submeter 'ElecMeter(instance=13, building=1, dataset='combed', appliances=[Appliance(type='sockets', instance=1)])'
Training model for submeter 'ElecMeter(instance=14, building=1, dataset='combed', appliances=[Appliance(type='sockets', instance=2)])'
Done training!
/Users/nipunbatra/git/nilmtk/nilmtk/electric.py:96: UserWarning: If you are using `preprocessing` to resample then please do not!  Instead, please use the `sample_period` parameter and set `resample=True`.
  warn("If you are using `preprocessing` to resample then please"

In [22]:
c.model


Out[22]:
[{'states': array([  0, 677], dtype=int32),
  'training_metadata': ElecMeter(instance=2, building=1, dataset='combed', appliances=[Appliance(type='elevator', instance=1)])},
 {'states': array([   0, 6029], dtype=int32),
  'training_metadata': ElecMeter(instance=3, building=1, dataset='combed', appliances=[])},
 {'states': array([   0, 6234], dtype=int32),
  'training_metadata': ElecMeter(instance=4, building=1, dataset='combed', appliances=[])},
 {'states': array([   0, 2569], dtype=int32),
  'training_metadata': ElecMeter(instance=5, building=1, dataset='combed', appliances=[])},
 {'states': array([   0, 2325], dtype=int32),
  'training_metadata': ElecMeter(instance=6, building=1, dataset='combed', appliances=[])},
 {'states': array([   0, 5434], dtype=int32),
  'training_metadata': ElecMeter(instance=7, building=1, dataset='combed', appliances=[])},
 {'states': array([   0, 2409], dtype=int32),
  'training_metadata': ElecMeter(instance=8, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=1)])},
 {'states': array([   0, 2834], dtype=int32),
  'training_metadata': ElecMeter(instance=9, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=2)])},
 {'states': array([   0, 3337], dtype=int32),
  'training_metadata': ElecMeter(instance=10, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=3)])},
 {'states': array([   0, 9543], dtype=int32),
  'training_metadata': ElecMeter(instance=11, building=1, dataset='combed', appliances=[Appliance(type='AHU', instance=4)])},
 {'states': array([   0, 2154], dtype=int32),
  'training_metadata': ElecMeter(instance=12, building=1, dataset='combed', appliances=[Appliance(type='light', instance=1)])},
 {'states': array([  0, 141], dtype=int32),
  'training_metadata': ElecMeter(instance=13, building=1, dataset='combed', appliances=[Appliance(type='sockets', instance=1)])},
 {'states': array([   0, 1930], dtype=int32),
  'training_metadata': ElecMeter(instance=14, building=1, dataset='combed', appliances=[Appliance(type='sockets', instance=2)])}]

Let us now try and see if we can break down individual floor energy consumption once we have some training data.


In [5]:
from nilmtk.disaggregate import Hart85

In [10]:
elec.load(resample = True, sample_period=240).next()


Loading data for meter ElecMeterID(instance=14, building=1, dataset='combed')     
Done loading data all meters for this chunk.
Out[10]:
physical_quantity energy power current
type active active NaN
2014-06-01 09:28:00+05:30 115781880 8588.750977 NaN
2014-06-01 09:32:00+05:30 115782272 9410.533203 NaN
2014-06-01 09:36:00+05:30 115782928 9066.583984 NaN
2014-06-01 09:40:00+05:30 115783560 9530.541016 NaN
2014-06-01 09:44:00+05:30 115784192 10114.249023 NaN
2014-06-01 09:48:00+05:30 115784856 9641.096680 NaN
2014-06-01 09:52:00+05:30 115785552 9779.908203 NaN
2014-06-01 09:56:00+05:30 115786232 10725.976562 NaN
2014-06-01 10:00:00+05:30 115786936 10191.064453 NaN
2014-06-01 10:04:00+05:30 115787600 9530.521484 NaN
2014-06-01 10:08:00+05:30 115788264 10242.521484 NaN
2014-06-01 10:12:00+05:30 115788984 10849.342773 NaN
2014-06-01 10:16:00+05:30 115789672 9757.737305 NaN
2014-06-01 10:20:00+05:30 115790352 9513.259766 NaN
2014-06-01 10:24:00+05:30 115791024 10294.590820 NaN
2014-06-01 10:28:00+05:30 115791720 10606.353516 NaN
2014-06-01 10:32:00+05:30 115792432 9826.396484 NaN
2014-06-01 10:36:00+05:30 115793152 10401.857422 NaN
2014-06-01 10:40:00+05:30 115793832 10359.908203 NaN
2014-06-01 10:44:00+05:30 115794504 9850.422852 NaN
2014-06-01 10:48:00+05:30 115795160 9624.167969 NaN
2014-06-01 10:52:00+05:30 115795848 10677.748047 NaN
2014-06-01 10:56:00+05:30 115796528 10392.708008 NaN
2014-06-01 11:00:00+05:30 115797232 10163.604492 NaN
2014-06-01 11:04:00+05:30 115797936 10759.177734 NaN
2014-06-01 11:08:00+05:30 115798640 10877.091797 NaN
2014-06-01 11:12:00+05:30 115799360 10537.074219 NaN
2014-06-01 11:16:00+05:30 115800088 10825.633789 NaN
2014-06-01 11:20:00+05:30 115800800 11248.236328 NaN
2014-06-01 11:24:00+05:30 115801512 10326.277344 NaN
... ... ... ...
2014-07-01 07:28:00+05:30 322625664 71013.289062 NaN
2014-07-01 07:32:00+05:30 322630400 69895.203125 NaN
2014-07-01 07:36:00+05:30 322635040 69362.593750 NaN
2014-07-01 07:40:00+05:30 322639712 70097.226562 NaN
2014-07-01 07:44:00+05:30 322644416 69530.289062 NaN
2014-07-01 07:48:00+05:30 322649024 69253.820312 NaN
2014-07-01 07:52:00+05:30 322653696 69352.125000 NaN
2014-07-01 07:56:00+05:30 322658368 70589.640625 NaN
2014-07-01 08:00:00+05:30 322663104 73158.593750 NaN
2014-07-01 08:04:00+05:30 322668032 73116.820312 NaN
2014-07-01 08:08:00+05:30 322672896 73031.046875 NaN
2014-07-01 08:12:00+05:30 322677792 74851.156250 NaN
2014-07-01 08:16:00+05:30 322682848 75340.046875 NaN
2014-07-01 08:20:00+05:30 322687840 74429.968750 NaN
2014-07-01 08:24:00+05:30 322692832 75890.695312 NaN
2014-07-01 08:28:00+05:30 322698016 79030.429688 NaN
2014-07-01 08:32:00+05:30 322703232 76038.312500 NaN
2014-07-01 08:36:00+05:30 322708320 76634.031250 NaN
2014-07-01 08:40:00+05:30 322713504 78907.867188 NaN
2014-07-01 08:44:00+05:30 322718720 78310.023438 NaN
2014-07-01 08:48:00+05:30 322724000 79291.265625 NaN
2014-07-01 08:52:00+05:30 322729312 79253.273438 NaN
2014-07-01 08:56:00+05:30 322734560 78209.843750 NaN
2014-07-01 09:00:00+05:30 322739808 80888.375000 NaN
2014-07-01 09:04:00+05:30 322745120 80303.468750 NaN
2014-07-01 09:08:00+05:30 322750656 83721.398438 NaN
2014-07-01 09:12:00+05:30 322756096 81467.234375 NaN
2014-07-01 09:16:00+05:30 322761568 81626.546875 NaN
2014-07-01 09:20:00+05:30 322767200 84252.281250 NaN
2014-07-01 09:24:00+05:30 322772768 82122.046875 NaN

10800 rows × 3 columns


In [6]:
h = Hart85()

In [21]:
h.train(elec.mains(), buffer_size=50, resample = True, sample_period=60 )


Finding Edges, please wait ...
Edge detection complete.
Creating transition frame ...
Transition frame created.
Creating states frame ...
States frame created.
Finished.
[('power', 'active')]
('power', 'active')
<type 'tuple'>

In [22]:
h.centroids.sort()


Out[22]:
(power, active)
0 288.920489
1 1812.944337
2 3271.449579
3 4194.080722
4 12471.953451
5 5065.139795
6 15807.552300
7 6637.652785
8 18857.845471
9 20420.012939
10 11197.805420
11 9012.688965
12 21820.431519

In [23]:
h.steady_states


Out[23]:
active average
2014-06-02 13:27:00+05:30 36411.041016
2014-06-02 15:58:00+05:30 36925.379883
2014-06-02 17:52:00+05:30 13941.479248
2014-06-02 18:52:00+05:30 14191.222900
2014-06-02 19:06:00+05:30 22841.055176
2014-06-02 20:19:00+05:30 24443.914551
2014-06-02 20:22:00+05:30 21951.810547
2014-06-02 20:32:00+05:30 34771.663086
2014-06-02 23:21:00+05:30 34216.839844
2014-06-03 00:43:00+05:30 18938.238281
2014-06-03 01:38:00+05:30 19832.306152
2014-06-03 01:40:00+05:30 17984.331055
2014-06-03 01:42:00+05:30 17460.734863
2014-06-03 01:52:00+05:30 17005.563965
2014-06-03 02:08:00+05:30 18886.914062
2014-06-03 02:37:00+05:30 16625.189453
2014-06-03 03:00:00+05:30 18000.113770
2014-06-03 03:08:00+05:30 16224.587891
2014-06-03 03:11:00+05:30 15959.612305
2014-06-03 03:38:00+05:30 15678.052490
2014-06-03 03:43:00+05:30 15453.002767
2014-06-03 03:49:00+05:30 15759.963379
2014-06-03 04:14:00+05:30 15782.667725
2014-06-03 04:16:00+05:30 15485.559814
2014-06-03 04:21:00+05:30 15891.287842
2014-06-03 04:24:00+05:30 15409.526693
2014-06-03 04:29:00+05:30 15499.049642
2014-06-03 04:34:00+05:30 15142.582520
2014-06-03 04:38:00+05:30 15790.873779
2014-06-03 04:44:00+05:30 15120.590251
... ...
2014-07-01 02:10:00+05:30 15070.327148
2014-07-01 02:33:00+05:30 14966.399292
2014-07-01 03:00:00+05:30 14603.229370
2014-07-01 03:26:00+05:30 14523.328979
2014-07-01 03:35:00+05:30 14755.730957
2014-07-01 03:43:00+05:30 14657.958740
2014-07-01 03:45:00+05:30 14448.866211
2014-07-01 03:47:00+05:30 14562.131592
2014-07-01 03:58:00+05:30 14436.710286
2014-07-01 04:06:00+05:30 14741.850830
2014-07-01 04:26:00+05:30 14547.990397
2014-07-01 04:42:00+05:30 14331.221680
2014-07-01 04:48:00+05:30 14436.857422
2014-07-01 04:51:00+05:30 14338.501953
2014-07-01 04:59:00+05:30 14823.559326
2014-07-01 05:04:00+05:30 14613.060791
2014-07-01 05:07:00+05:30 7886.119303
2014-07-01 05:37:00+05:30 7734.275513
2014-07-01 05:42:00+05:30 7939.074219
2014-07-01 05:45:00+05:30 8280.978271
2014-07-01 05:50:00+05:30 8087.164600
2014-07-01 06:01:00+05:30 7952.034424
2014-07-01 06:15:00+05:30 7837.976807
2014-07-01 06:22:00+05:30 7943.817057
2014-07-01 06:45:00+05:30 8180.983276
2014-07-01 06:49:00+05:30 7676.170288
2014-07-01 06:54:00+05:30 7573.272095
2014-07-01 06:57:00+05:30 21740.337891
2014-07-01 07:11:00+05:30 26027.974609
2014-07-01 07:25:00+05:30 25381.940430

1770 rows × 1 columns


In [15]:
mains_df = elec.mains().load(cols=[('power','active')], resample = True, sample_period=240).next()

In [16]:
ax = mains_df.plot()
h.steady_states['active average'].plot(style='ko', ax = ax);

In [54]:
%matplotlib osx
#sns.set_palette("Set1", n_colors=12)
ax = elec.mains().plot()
h.steady_states['active average'].plot(style='o', ax = ax);
plt.ylabel("Power (W)")
plt.xlabel("Time");

In [11]:
disag_filename = '/Users/nipunbatra/Desktop/disag.h5'
from nilmtk import HDFDataStore
output = HDFDataStore(disag_filename, 'w')
mains = elec.mains()
h.disaggregate(mains, output)


Finding Edges, please wait ...
Edge detection complete.
Creating transition frame ...
Transition frame created.
Creating states frame ...
States frame created.
Finished.
12581752
59841222
140068068
177381018
323700615
635645890
594628248
306135865
384395071
227594110
543155886
343958686
587542384
745667445

In [12]:
output.close()

In [13]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec

In [14]:
disag_elec


Out[14]:
MeterGroup(meters=
  ElecMeter(instance=1, building=1, dataset='Hart85_2014-12-28T14:23:52', site_meter, appliances=[])
  ElecMeter(instance=2, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=0)])
  ElecMeter(instance=3, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=1)])
  ElecMeter(instance=4, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=2)])
  ElecMeter(instance=5, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=3)])
  ElecMeter(instance=6, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=4)])
  ElecMeter(instance=7, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=5)])
  ElecMeter(instance=8, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=6)])
  ElecMeter(instance=9, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=7)])
  ElecMeter(instance=10, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=8)])
  ElecMeter(instance=11, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=9)])
  ElecMeter(instance=12, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=10)])
  ElecMeter(instance=13, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=11)])
  ElecMeter(instance=14, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=12)])
  ElecMeter(instance=15, building=1, dataset='Hart85_2014-12-28T14:23:52', appliances=[Appliance(type='unknown', instance=13)])
)

In [18]:
fig, ax = plt.subplots(nrows=2, sharex = True)
ax1 = disag_elec['unknown', 2].plot(ax=ax[0])
elec.submeters().plot(ax=ax[1])
plt.xlabel("Time");

In [38]:
fig, ax = plt.subplots(nrows=2, sharex = True)
ax1 = disag_elec['unknown', 12].plot(ax=ax[0])
elec.meters[6].plot(ax=ax[1])
plt.xlabel("Time");

In [51]:
%matplotlib osx
sns.set_palette(sns.color_palette("coolwarm", 7))
disag_elec.plot()


Out[51]:
<matplotlib.axes._subplots.AxesSubplot at 0x116739b50>

In [52]:
disag_elec.plot()


Out[52]:
<matplotlib.axes._subplots.AxesSubplot at 0x111ca8b50>

In [17]:
split_point


Out[17]:
Timestamp('2014-06-16 23:28:38+0530', tz='Asia/Kolkata')

Trying with deepcopy


In [18]:
from copy import copy, deepcopy

train = deepcopy(ds)
test = deepcopy(train)


Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in 

In [19]:
train.set_window(end='2014-06-16 23:28:38')
test.set_window(start='2014-06-16 23:28:38')

Plotting full DS. This should have data for the entire time duration


In [20]:
ds.buildings[1].elec.plot();



In [21]:
train.buildings[1].elec.plot()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-c30ba582b1f7> in <module>()
----> 1 train.buildings[1].elec.plot()

/Users/nipunbatra/git/nilmtk/nilmtk/metergroup.pyc in plot(self, kind, **kwargs)
   1363         except KeyError:
   1364             raise ValueError("'{}' not a valid setting for 'kind' parameter."
-> 1365                              .format(kind))
   1366         return ax
   1367 

ValueError: 'separate lines' not a valid setting for 'kind' parameter.

Okay this won't work, lets try and get some data from train


In [22]:
train.buildings[1].elec.meters[1].load(chunksize=100).next()


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-22-0576bc0f2c9f> in <module>()
----> 1 train.buildings[1].elec.meters[1].load(chunksize=100).next()

/Users/nipunbatra/git/nilmtk/nilmtk/datastore/hdfdatastore.pyc in load(self, key, cols, sections, n_look_ahead_rows, chunksize, verbose)
     86                     if str(e) == ("'NoneType' object has no attribute "
     87                                   "'read_coordinates'"):
---> 88                         raise KeyError("key '{}' not found".format(key))
     89                     else:
     90                         raise

KeyError: "key '/building1/elec/meter2' not found"

Maybe, we need to import metadat from ds?


In [23]:
train.import_metadata(ds.store)


Out[23]:
<nilmtk.dataset.DataSet at 0x10cc2a8d0>

In [24]:
train.set_window(end='2014-06-16 23:28:38')

In [25]:
train.buildings[1].elec.plot();


Great, train seems to have been divided at the right point now. What about ds again?


In [26]:
ds.buildings[1].elec.plot();


No! This ain't correct! ds should have retained the full window.

Let me try with test now.


In [27]:
test.buildings[1].elec.meters[1].load(chunksize=100).next()


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-27-012137d9c331> in <module>()
----> 1 test.buildings[1].elec.meters[1].load(chunksize=100).next()

/Users/nipunbatra/git/nilmtk/nilmtk/datastore/hdfdatastore.pyc in load(self, key, cols, sections, n_look_ahead_rows, chunksize, verbose)
     86                     if str(e) == ("'NoneType' object has no attribute "
     87                                   "'read_coordinates'"):
---> 88                         raise KeyError("key '{}' not found".format(key))
     89                     else:
     90                         raise

KeyError: "key '/building1/elec/meter2' not found"

In [28]:
test.import_metadata(ds.store)


Out[28]:
<nilmtk.dataset.DataSet at 0x10cc2a890>

In [29]:
test.buildings[1].elec.plot()


Out[29]:
<matplotlib.axes._subplots.AxesSubplot at 0x10adc4790>

This surely ain't right!


In [30]:
test.set_window(start='2014-06-16 23:28:38')

In [31]:
test.buildings[1].elec.plot();


What about train. Hope train doesn't change its window automatically now :(


In [32]:
train.buildings[1].elec.plot();


It does!

Train-test testing ends

Lets try again


In [33]:
ds_2 = DataSet("/Users/nipunbatra/Desktop/combed.h5")
tr_store = deepcopy(ds_2.store)


Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in 

In [36]:
tr = deepcopy(ds_2)
tr.import_metadata(tr_store)
te = deepcopy(ds_2)
te.import_metadata(deepcopy(ds_2.store))


Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in 
Out[36]:
<nilmtk.dataset.DataSet at 0x103855490>

In [37]:
tr.set_window(end='2014-06-16 23:28:38')
te.set_window(start='2014-06-16 23:28:38')

In [38]:
tr.buildings[1].elec.plot()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-38-86adf469e76a> in <module>()
----> 1 tr.buildings[1].elec.plot()

/Users/nipunbatra/git/nilmtk/nilmtk/metergroup.pyc in plot(self, kind, **kwargs)
   1363         except KeyError:
   1364             raise ValueError("'{}' not a valid setting for 'kind' parameter."
-> 1365                              .format(kind))
   1366         return ax
   1367 

ValueError: 'separate lines' not a valid setting for 'kind' parameter.

In [32]:
co = CombinatorialOptimisation()
co.train(floor_mg)


Training model for submeter 'ElecMeter(instance=3, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=4, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=5, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=6, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=7, building=1, dataset='combed', appliances=[])'
Done training!
/Users/nipunbatra/git/nilmtk/nilmtk/electric.py:96: UserWarning: If you are using `preprocessing` to resample then please do not!  Instead, please use the `sample_period` parameter and set `resample=True`.
  warn("If you are using `preprocessing` to resample then please"

In [39]:
ds


Out[39]:
<nilmtk.dataset.DataSet at 0x109301b90>

In [41]:
b=ds.buildings[1]

In [42]:
elec = train.buildings[1].elec

In [43]:
elec.plot()


Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x10bd4cbd0>

In [15]:
co.model


Out[15]:
[{'states': array([   0, 2317, 7565], dtype=int32),
  'training_metadata': ElecMeter(instance=3, building=1, dataset='combed', appliances=[])},
 {'states': array([    0,  3190, 10190], dtype=int32),
  'training_metadata': ElecMeter(instance=4, building=1, dataset='combed', appliances=[])},
 {'states': array([   0, 1740, 3356], dtype=int32),
  'training_metadata': ElecMeter(instance=5, building=1, dataset='combed', appliances=[])},
 {'states': array([   0, 1456, 2843], dtype=int32),
  'training_metadata': ElecMeter(instance=6, building=1, dataset='combed', appliances=[])},
 {'states': array([    0,  1174, 11285], dtype=int32),
  'training_metadata': ElecMeter(instance=7, building=1, dataset='combed', appliances=[])}]

In [71]:
from nilmtk import DataSet, TimeFrame, MeterGroup, HDFDataStore

disag_filename = "/Users/nipunbatra/Desktop/out_co.h5"
output = HDFDataStore(disag_filename, 'w')
co.disaggregate(elec.mains(), output)
output.close()


vampire_power = 0.0 watts
Estimating power demand for 'ElecMeter(instance=3, building=1, dataset='combed', appliances=[])'
Estimating power demand for 'ElecMeter(instance=4, building=1, dataset='combed', appliances=[])'
Estimating power demand for 'ElecMeter(instance=5, building=1, dataset='combed', appliances=[])'
Estimating power demand for 'ElecMeter(instance=6, building=1, dataset='combed', appliances=[])'
Estimating power demand for 'ElecMeter(instance=7, building=1, dataset='combed', appliances=[])'
ElecMeter(instance=3, building=1, dataset='combed', appliances=[])
ElecMeter(instance=4, building=1, dataset='combed', appliances=[])
ElecMeter(instance=5, building=1, dataset='combed', appliances=[])
ElecMeter(instance=6, building=1, dataset='combed', appliances=[])
ElecMeter(instance=7, building=1, dataset='combed', appliances=[])

In [72]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec
disag_elec[(7)].plot()
elec[7].plot()
plt.tight_layout()
disag.store.close()



In [ ]:
HDFDataStore(disag_filename

In [30]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec
disag_elec.plot()
disag.store.close()


/Users/nipunbatra/git/nilmtk/nilmtk/metergroup.py:76: RuntimeWarning: Building 1 has an empty 'appliances' list.
  .format(building_id.instance), RuntimeWarning)

In [17]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec
disag_elec.plot()
disag.store.close()


/Users/nipunbatra/git/nilmtk/nilmtk/metergroup.py:76: RuntimeWarning: Building 1 has an empty 'appliances' list.
  .format(building_id.instance), RuntimeWarning)

In [31]:
e = elec.meters[1]

In [32]:
e.dominant_appliance().identifier.type


Out[32]:
'elevator'

In [33]:
disag_elec


Out[33]:
MeterGroup(meters=
  ElecMeter(instance=1, building=1, dataset='NILMTK_CO_2014-12-22T14:52:03', site_meter, appliances=[])
  ElecMeter(instance=3, building=1, dataset='NILMTK_CO_2014-12-22T14:52:03', appliances=[])
  ElecMeter(instance=4, building=1, dataset='NILMTK_CO_2014-12-22T14:52:03', appliances=[])
  ElecMeter(instance=5, building=1, dataset='NILMTK_CO_2014-12-22T14:52:03', appliances=[])
  ElecMeter(instance=6, building=1, dataset='NILMTK_CO_2014-12-22T14:52:03', appliances=[])
  ElecMeter(instance=7, building=1, dataset='NILMTK_CO_2014-12-22T14:52:03', appliances=[])
)

In [34]:
a = {2:None}

In [35]:
a =


  File "<ipython-input-35-beb00c8a707f>", line 1
    a =
        ^
SyntaxError: invalid syntax

In [18]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec

f1 = f1_score(disag_elec, elec)
f1.index = disag_elec.get_labels(f1.index)
f1.plot(kind='bar')
plt.xlabel('appliance');
plt.ylabel('f-score');

disag.store.close()



In [37]:
f1


Out[37]:
First floor total     0.615812
Second floor total    0.703973
Third floor total     0.952787
Fourth floor total    0.946971
Fifth floor total     0.689214
dtype: float64

In [31]:
from nilmtk.metrics import error_in_assigned_energy, mean_normalized_error_power

In [32]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec

e = mean_normalized_error_power(disag_elec, elec)

disag.store.close()

In [34]:
e


Out[34]:
3    0.394772
4    0.465725
5    0.396835
6    0.427563
7    0.423408
dtype: float64

In [22]:
disag = DataSet(disag_filename)

disag_elec = disag.buildings[1].elec

#%matplotlib inline
disag_elec[(3)].plot()
elec[3].plot()
plt.tight_layout()



In [23]:
disag_elec[(4)].plot()
elec[4].plot()
plt.tight_layout()



In [52]:
elec[3].load().next()['power','active'].plot()


Out[52]:
<matplotlib.axes._subplots.AxesSubplot at 0x10ad5e5d0>

In [24]:
elec[3].load().next()['power','active'].plot()


Out[24]:
<matplotlib.axes._subplots.AxesSubplot at 0x10cd0c390>

In [25]:
df = elec[3].load().next()

In [59]:
df['power','active']['13-06-2014'].plot()


Out[59]:
<matplotlib.axes._subplots.AxesSubplot at 0x10e3558d0>

In [64]:
plt.plot(df['power','active']['16-06-2014'].values)
#df['power','active']['15-06-2014'].plot()
plt.plot(df['power','active']['15-06-2014'].values)
plt.plot(df['power','active']['14-06-2014'].values)
plt.plot(df['power','active']['13-06-2014'].values)


Out[64]:
[<matplotlib.lines.Line2D at 0x112058c10>]

In [65]:
plt.plot(df['power','active']['16-06-2014'].values)


Out[65]:
[<matplotlib.lines.Line2D at 0x1123c0110>]

In [28]:
#disag_elec[('AHU', 2)].plot()
elec[('AHU', 2)].plot()


Out[28]:
<matplotlib.axes._subplots.AxesSubplot at 0x10a617a90>

In [29]:
#disag_elec[('AHU', 3)].plot()
elec[('AHU', 3)].plot()


Out[29]:
<matplotlib.axes._subplots.AxesSubplot at 0x10cd17610>

In [30]:
#disag_elec[('sockets', 1)].plot()
elec[('sockets', 1)].plot()


Out[30]:
<matplotlib.axes._subplots.AxesSubplot at 0x10b1523d0>

In [30]:
df = elec[('sockets',1)].load().next()

In [32]:
df2 = disag_elec[('sockets',1)].load().next()

In [35]:
from nilmtk.disaggregate import fhmm_exact

In [36]:
fh = fhmm_exact.FHMM()

In [37]:
fh.train(floor_mg)


Training model for submeter 'ElecMeter(instance=3, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=4, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=5, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=6, building=1, dataset='combed', appliances=[])'
Training model for submeter 'ElecMeter(instance=7, building=1, dataset='combed', appliances=[])'

In [38]:
disag_filename = "/Users/nipunbatra/Desktop/out_fhmm.h5"
output = HDFDataStore(disag_filename, 'w')
fh.disaggregate(elec.mains(), output)
output.close()


Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in Exception tables.exceptions.HDF5ExtError: HDF5ExtError('Problems closing the Group None',) in 

In [39]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec

e = mean_normalized_error_power(disag_elec, elec)

disag.store.close()

In [40]:
e


Out[40]:
3    0.333697
4    0.400300
5    0.277672
6    0.303830
7    0.288941
dtype: float64

In [42]:
e.plot(kind='bar')


Out[42]:
<matplotlib.axes._subplots.AxesSubplot at 0x110b45150>

In [67]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[1].elec
disag_elec[(7)].plot()
elec[7].plot()
plt.tight_layout()
disag.store.close()



In [ ]: