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'] = (16, 8)

import nilmtk
from nilmtk import DataSet, TimeFrame, MeterGroup, HDFDataStore
from nilmtk.disaggregate import CombinatorialOptimisation
from nilmtk.utils import print_dict

from nilmtk.metrics import f1_score

import seaborn as sns

import warnings
#from nilmtk.plots import plot_correlation_heatmap
warnings.filterwarnings("ignore")

In [3]:
data_dir = '/Users/nipunbatra/Dropbox/'
we = DataSet(join(data_dir, 'combed.h5'))
print('loaded ' + str(len(we.buildings)) + ' buildings')


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-bca9176a3ce4> in <module>()
      1 data_dir = '/Users/nipunbatra/Dropbox/'
----> 2 we = DataSet(join(data_dir, 'combed.h5'))
      3 print('loaded ' + str(len(we.buildings)) + ' buildings')

/Users/nipunbatra/git/nilmtk/nilmtk/dataset.pyc in __init__(self, filename, format)
     41         self.metadata = {}
     42         if filename is not None:
---> 43             self.import_metadata(get_datastore(filename, format))
     44 
     45     def import_metadata(self, store):

/Users/nipunbatra/git/nilmtk/nilmtk/dataset.pyc in import_metadata(self, store)
     50         """
     51         self.store = store
---> 52         self.metadata = store.load_metadata()
     53         self._init_buildings(store)
     54         return self

/Users/nipunbatra/git/nilmtk/nilmtk/docinherit.pyc in f(*args, **kwargs)
     44         @wraps(self.mthd, assigned=('__name__','__module__'))
     45         def f(*args, **kwargs):
---> 46             return self.mthd(obj, *args, **kwargs)
     47 
     48         return self.use_parent_doc(f, overridden)

/Users/nipunbatra/git/nilmtk/nilmtk/datastore/hdfdatastore.pyc in load_metadata(self, key)
    179             node = self.store.get_node(key)
    180 
--> 181         metadata = deepcopy(node._v_attrs.metadata)
    182         return metadata
    183 

/Users/nipunbatra/anaconda/lib/python2.7/site-packages/tables/attributeset.pyc in __getattr__(self, name)
    286         if not name in self._v_attrnames:
    287             raise AttributeError("Attribute '%s' does not exist in node: "
--> 288                                  "'%s'" % (name, self._v__nodepath))
    289 
    290         # Read the attribute from disk. This is an optimization to read

AttributeError: Attribute 'metadata' does not exist in node: '/'

In [14]:
building_number = 1
print_dict(we.buildings[building_number].metadata)


Out[14]:
  • instance: 1
  • dataset: iAWE
  • original_name: house_1

In [15]:
elec = we.buildings[building_number].elec
elec.appliances


Out[15]:
[Appliance(type='fridge', instance=1),
 Appliance(type='unknown', instance=1),
 Appliance(type='washing machine', instance=1),
 Appliance(type='clothes iron', instance=1),
 Appliance(type='television', instance=1),
 Appliance(type='wet appliance', instance=1),
 Appliance(type='air conditioner', instance=1),
 Appliance(type='air conditioner', instance=2),
 Appliance(type='motor', instance=1),
 Appliance(type='computer', instance=1)]

In [16]:
submeters = elec.submeters().meters
submeters


Out[16]:
[ElecMeter(instance=3, building=1, dataset='iAWE', appliances=[Appliance(type='fridge', instance=1)]),
 ElecMeter(instance=4, building=1, dataset='iAWE', appliances=[Appliance(type='air conditioner', instance=1)]),
 ElecMeter(instance=5, building=1, dataset='iAWE', appliances=[Appliance(type='air conditioner', instance=2)]),
 ElecMeter(instance=6, building=1, dataset='iAWE', appliances=[Appliance(type='washing machine', instance=1)]),
 ElecMeter(instance=7, building=1, dataset='iAWE', appliances=[Appliance(type='computer', instance=1)]),
 ElecMeter(instance=8, building=1, dataset='iAWE', appliances=[Appliance(type='clothes iron', instance=1)]),
 ElecMeter(instance=9, building=1, dataset='iAWE', appliances=[Appliance(type='unknown', instance=1)]),
 ElecMeter(instance=10, building=1, dataset='iAWE', appliances=[Appliance(type='television', instance=1)]),
 ElecMeter(instance=11, building=1, dataset='iAWE', appliances=[Appliance(type='wet appliance', instance=1)]),
 ElecMeter(instance=12, building=1, dataset='iAWE', appliances=[Appliance(type='motor', instance=1)])]

In [17]:
first =submeters[0]

In [18]:
first.correlation(submeters[1])


Out[18]:
physical_quantity  type    
power              active      0.031198
                   reactive    0.000069
                   apparent    0.019790
frequency                     -0.009948
voltage                       -0.191077
pf                             0.041348
current                        0.010666
dtype: float64

In [19]:
first.power_series().next()


Out[19]:
physical_quantity power frequency voltage pf current
type active reactive apparent
2013-06-07 05:30:00+05:30 0.111000 2.483000 2.486000 50.070000 235.070007 0.045 0.011000
2013-06-07 05:30:01+05:30 0.200000 2.547000 2.555000 50.080002 235.020004 0.078 0.011000
2013-06-07 05:30:02+05:30 0.152000 2.480000 2.485000 50.080002 234.979996 0.061 0.011000
2013-06-07 05:30:03+05:30 0.159000 2.444000 2.449000 50.060001 235.000000 0.065 0.010000
2013-06-07 05:30:04+05:30 0.215000 2.510000 2.519000 50.060001 234.949997 0.085 0.011000
2013-06-07 05:30:05+05:30 0.210000 2.474000 2.483000 50.080002 234.869995 0.085 0.011000
2013-06-07 05:30:06+05:30 0.170000 2.543000 2.549000 50.070000 234.470001 0.067 0.011000
2013-06-07 05:30:07+05:30 0.182000 2.509000 2.515000 50.060001 234.570007 0.072 0.011000
2013-06-07 05:30:08+05:30 0.249000 2.467000 2.479000 50.060001 234.479996 0.101 0.011000
2013-06-07 05:30:09+05:30 0.249000 2.467000 2.479000 50.060001 234.479996 0.101 0.011000
2013-06-07 05:30:10+05:30 0.151000 2.476000 2.480000 50.060001 234.570007 0.061 0.011000
2013-06-07 05:30:11+05:30 0.165000 2.509000 2.515000 50.060001 234.479996 0.066 0.011000
2013-06-07 05:30:12+05:30 0.125000 2.477000 2.480000 50.060001 234.589996 0.022 0.011000
2013-06-07 05:30:13+05:30 0.245000 2.470000 2.482000 50.070000 234.710007 0.047 0.011000
2013-06-07 05:30:14+05:30 0.125000 2.446000 2.553000 50.060001 234.960007 0.064 0.010000
2013-06-07 05:30:15+05:30 0.080000 2.546000 2.554000 50.060001 234.910004 0.076 0.011000
2013-06-07 05:30:16+05:30 0.110000 2.506000 2.519000 50.040001 234.880005 0.100 0.011000
2013-06-07 05:30:17+05:30 0.161000 2.479000 2.483000 50.049999 234.940002 0.061 0.011000
2013-06-07 05:30:18+05:30 0.195000 2.514000 2.519000 50.040001 234.919998 0.065 0.011000
2013-06-07 05:30:19+05:30 0.083000 2.478000 2.484000 50.049999 234.919998 0.069 0.011000
2013-06-07 05:30:20+05:30 0.156000 2.514000 2.519000 50.070000 234.970001 0.062 0.011000
2013-06-07 05:30:21+05:30 0.197000 2.546000 2.554000 50.049999 234.899994 0.077 0.011000
2013-06-07 05:30:22+05:30 0.261000 2.470000 2.483000 50.070000 234.880005 0.105 0.011000
2013-06-07 05:30:23+05:30 0.134000 2.444000 2.448000 50.060001 234.880005 0.055 0.010000
2013-06-07 05:30:24+05:30 0.218000 2.475000 2.485000 50.049999 234.979996 0.088 0.011000
2013-06-07 05:30:25+05:30 0.060000 2.519000 2.519000 50.060001 234.929993 0.024 0.011000
2013-06-07 05:30:26+05:30 0.119000 2.517000 2.519000 50.070000 234.940002 0.047 0.011000
2013-06-07 05:30:27+05:30 0.142000 2.480000 2.485000 50.070000 234.970001 0.057 0.011000
2013-06-07 05:30:28+05:30 0.184000 2.478000 2.484000 50.060001 234.960007 0.074 0.011000
2013-06-07 05:30:29+05:30 0.085000 2.482000 2.484000 50.060001 234.899994 0.034 0.011000
... ... ... ... ... ... ... ...
2013-08-05 22:01:35+05:30 77.773003 50.956001 92.978996 50.180000 223.919998 0.836 0.415000
2013-08-05 22:01:36+05:30 77.843002 51.421001 93.292999 50.160000 223.889999 0.834 0.417000
2013-08-05 22:01:37+05:30 77.816002 50.855999 92.960999 50.169998 223.759995 0.837 0.415000
2013-08-05 22:01:38+05:30 77.654999 51.323002 93.083000 50.169998 223.750000 0.834 0.416000
2013-08-05 22:01:39+05:30 77.860001 51.030998 93.093002 50.160000 223.979996 0.836 0.416000
2013-08-05 22:01:40+05:30 0.005000 0.834000 33.521999 77.879997 38.619999 0.000 93.420998
2013-08-05 22:01:41+05:30 77.845001 51.134998 93.137001 50.169998 224.029999 0.836 0.416000
2013-08-05 22:01:42+05:30 77.935997 51.620998 93.481003 50.189999 224.089996 0.834 0.417000
2013-08-05 22:01:43+05:30 77.834999 51.048000 93.081001 50.189999 224.039993 0.836 0.415000
2013-08-05 22:01:44+05:30 77.931000 51.644001 93.489998 50.169998 224.020004 0.834 0.417000
2013-08-05 22:01:45+05:30 77.875000 51.154999 93.172997 50.160000 224.059998 0.836 0.416000
2013-08-05 22:01:46+05:30 77.894997 51.467999 93.362999 50.180000 223.850006 0.834 0.417000
2013-08-05 22:01:47+05:30 77.558998 50.596001 92.602997 50.180000 222.960007 0.838 0.415000
2013-08-05 22:01:48+05:30 77.517998 50.382999 92.453003 50.180000 223.270004 0.838 0.414000
2013-08-05 22:01:49+05:30 77.551003 50.709999 92.657997 50.169998 223.199997 0.837 0.415000
2013-08-05 22:01:50+05:30 77.491997 50.243000 92.353996 50.169998 223.160004 0.839 0.414000
2013-08-05 22:01:51+05:30 77.513000 50.599998 92.567001 50.189999 223.199997 0.837 0.415000
2013-08-05 22:01:52+05:30 77.430000 50.297001 92.332001 50.189999 223.110001 0.839 0.414000
2013-08-05 22:01:53+05:30 77.489998 50.668999 92.585999 50.180000 223.149994 0.837 0.415000
2013-08-05 22:01:54+05:30 77.396004 50.207001 92.254997 50.189999 223.000000 0.839 0.414000
2013-08-05 22:01:55+05:30 77.497002 50.608002 92.557999 50.160000 223.020004 0.837 0.415000
2013-08-05 22:01:56+05:30 77.469002 50.209999 92.317001 50.189999 222.929993 0.839 0.414000
2013-08-05 22:01:57+05:30 77.674004 50.723000 92.767998 50.189999 223.009995 0.837 0.416000
2013-08-05 22:01:58+05:30 77.564003 50.057999 92.315002 50.180000 222.710007 0.840 0.415000
2013-08-05 22:01:59+05:30 77.537003 50.580002 92.575996 50.160000 222.720001 0.838 0.416000
2013-08-05 22:02:00+05:30 77.623001 50.146000 92.412003 50.169998 222.770004 0.840 0.415000
2013-08-05 22:02:01+05:30 77.603996 50.490002 92.584000 50.180000 222.779999 0.838 0.416000
2013-08-05 22:02:02+05:30 77.445999 50.062000 92.217003 50.160000 222.669998 0.840 0.414000
2013-08-05 22:02:03+05:30 77.414001 50.387001 92.366997 50.180000 222.690002 0.838 0.415000
2013-08-05 22:02:04+05:30 77.389000 50.095001 92.186996 50.160000 222.720001 0.839 0.414000

4538844 rows × 7 columns


In [10]:
correlation_df = elec.pairwise_correlation()

In [11]:
correlation_df


Out[11]:
(1, 11, WikiEnergy) (2, 11, WikiEnergy) (3, 11, WikiEnergy) (4, 11, WikiEnergy) (5, 11, WikiEnergy) (6, 11, WikiEnergy) (7, 11, WikiEnergy) (8, 11, WikiEnergy) (9, 11, WikiEnergy) (10, 11, WikiEnergy) (11, 11, WikiEnergy)
(1, 11, WikiEnergy) 1 0.7767822 0.008505902 0.04546747 0.177442 0.6200149 0.6945421 0.0478113 0.1169699 0.1116754 0.04581769
(2, 11, WikiEnergy) 0.7767822 1 -0.06069557 -0.005558193 0.02070908 0.06489 0.8216437 -0.00557085 0.06428014 0.04670131 0.01036423
(3, 11, WikiEnergy) 0.008505902 -0.06069557 1 0.09864813 0.02942626 0.04369746 -0.05372177 0.002078076 0.04046819 0.005564681 0.02027651
(4, 11, WikiEnergy) 0.04546747 -0.005558193 0.09864813 1 0.02884259 0.001228857 0.03979229 -0.003181818 0.09078146 0.01591458 0.0446082
(5, 11, WikiEnergy) 0.177442 0.02070908 0.02942626 0.02884259 1 0.0114481 0.01597073 -0.01791291 0.006596704 0.01063991 -0.0209113
(6, 11, WikiEnergy) 0.6200149 0.06489 0.04369746 0.001228857 0.0114481 1 0.06720773 0.006494385 0.03847742 0.0120773 0.03580152
(7, 11, WikiEnergy) 0.6945421 0.8216437 -0.05372177 0.03979229 0.01597073 0.06720773 1 -0.001212552 0.1016233 0.03015669 0.07336553
(8, 11, WikiEnergy) 0.0478113 -0.00557085 0.002078076 -0.003181818 -0.01791291 0.006494385 -0.001212552 1 -0.002295276 0.01257717 0.02548502
(9, 11, WikiEnergy) 0.1169699 0.06428014 0.04046819 0.09078146 0.006596704 0.03847742 0.1016233 -0.002295276 1 0.01214392 0.02170789
(10, 11, WikiEnergy) 0.1116754 0.04670131 0.005564681 0.01591458 0.01063991 0.0120773 0.03015669 0.01257717 0.01214392 1 0.0001214065
(11, 11, WikiEnergy) 0.04581769 0.01036423 0.02027651 0.0446082 -0.0209113 0.03580152 0.07336553 0.02548502 0.02170789 0.0001214065 1

In [20]:
labels = elec.get_appliance_labels(correlation_df.index)
plot_correlation_heatmap(correlation_df, labels)