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'] = (14, 6)
plt.style.use('ggplot')
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")
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)
In [3]:
data_dir = '/Users/nipunbatra/Downloads/'
we = DataSet(join(data_dir, 'wikienergy.h5'))
print('loaded ' + str(len(we.buildings)) + ' buildings')
In [4]:
print_dict(we.metadata)
In [5]:
building_number = 11
print_dict(we.buildings[building_number].metadata)
In [6]:
elec = we.buildings[building_number].elec
elec.appliances
Out[6]:
In [7]:
elec.draw_wiring_graph();
In [8]:
fridges = nilmtk.global_meter_group.select_using_appliances(type='fridge')
The energy consumed by each appliance can be expressed as a proportion of the household's total energy. Here we find the range of proportions for each fridge.
In [11]:
# Select a subset of fridges, otherwise the computation takes a long time
fridges_restricted = MeterGroup(fridges.meters[6:16])
proportion_per_fridge = fridges_restricted.proportion_of_upstream_total_per_meter()
In [13]:
proportion_per_fridge.plot(kind='barh');
plt.title('Fridge energy as proportion of total building energy');
plt.xlabel('Proportion');
plt.ylabel('Fridge (<appliance instance>, <building instance>, <dataset name>)');
In [14]:
# How much energy does the largest-consuming fridge consume in kWh?
fridges.select(building=61).total_energy()
Out[14]:
In [15]:
fridges.select(building=61).plot();
plt.xlabel("Time");
In [16]:
fridges_restricted = MeterGroup(fridges.meters[:20])
daily_energy = pd.DataFrame([meter.average_energy_per_period(offset_alias='D')
for meter in fridges_restricted.meters])
daily_energy.plot(kind='hist');
plt.title('Histogram of daily fridge energy');
plt.xlabel('energy (kWh)');
plt.ylabel('occurences');
plt.legend().set_visible(False)
In [19]:
we.set_window(start='2014-04-01 00:00:00', end='2014-04-02 00:00:00')
elec.plot();
plt.xlabel("Time");
In [20]:
fraction = elec.submeters().fraction_per_meter().dropna()
In [21]:
# Create convenient labels
labels = elec.get_labels(fraction.index)
plt.figure(figsize=(6,6))
fraction.plot(kind='pie', labels=labels);
In [22]:
elec.select_using_appliances(category='heating')
Out[22]:
In [23]:
# Find all appliances with a particular type of motor
elec.select_using_appliances(category='single-phase induction motor')
Out[23]:
In [24]:
# Train
co = CombinatorialOptimisation()
co.train(elec)
In [25]:
for model in co.model:
print_dict(model)
In [26]:
# Disaggregate
disag_filename = join(data_dir, 'wikienergy-disag.h5')
output = HDFDataStore(disag_filename, 'w')
co.disaggregate(elec.mains(), output)
output.close()
In [27]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[building_number].elec
disag_elec.plot()
plt.xlabel("Time")
disag.store.close()
In [28]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[building_number].elec
f1 = f1_score(disag_elec, elec)
f1.index = disag_elec.get_labels(f1.index)
f1.plot(kind='barh', figsize=(10,6))
plt.ylabel('appliance');
plt.xlabel('f-score');
disag.store.close()
This notebook was demonstrated at Buildsys 2014 (Nov. 2014), where it won the best demonstration award. Since then, the API has changed a bit and thus this notebook has been updated to reflect those changes.
In [29]:
# CSS styling
from IPython.core.display import display, HTML
display(HTML(open('static/styles.css', 'r').read()));