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 warnings
warnings.filterwarnings("ignore")

Demo of NILMTK for RCUK Showcase

Downloading data

The full data set can be downloaded from the remote WikiEnergy database. The credentials are omitted here for security reasons.


In [2]:
# download_wikienergy(database_username, database_password, hdf_filename)

Loading data


In [3]:
data_dir = '/data/wikienergy/'
we = DataSet(join(data_dir, 'wikienergy.h5'))
print('loaded ' + str(len(we.buildings)) + ' buildings')


loaded 239 buildings

Plot sub-metered data for a single day


In [4]:
we.store.window = TimeFrame(start='2014-04-01 00:00:00-05:00', end='2014-04-02 00:00:00-05:00')
building_number = 11
elec = we.buildings[building_number].elec
elec.plot();


Plot fraction of energy consumption of each appliance


In [5]:
fraction = elec.submeters().fraction_per_meter().dropna()


10/10 ElecMeter(instance=11, building=11, dataset='WikiEnergy', appliances=[Appliance(type='electric water heating appliance', instance=1)])

In [6]:
# Create convenient labels
labels = elec.get_appliance_labels(fraction.index)
plt.figure(figsize=(8,8))
fraction.plot(kind='pie', labels=labels);


Train model


In [7]:
# Train
co = CombinatorialOptimisation()
co.train(elec)

Alternatively, a model could be specified manually:

co.model = [
    {'states': [0,  100], 'training_metadata': ('television', 1)},
    {'states': [0, 2000], 'training_metadata': ('electric furnace', 1)}
    ]

Examine aggregate data


In [8]:
elec.mains().plot()


Disaggregate smart meter data into appliances


In [ ]:
disag_filename = join(data_dir, 'wikienergy-disag.h5')
output = HDFDataStore(disag_filename, 'w')
co.disaggregate(elec.mains(), output)
output.close()

disag = DataSet(disag_filename)
disag_elec = disag.buildings[building_number].elec
disag_elec.plot()
disag.store.close()

Calculate accuracy of disaggregation


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

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

disag.store.close()