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")
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 = '/data/wikienergy/'
we = DataSet(join(data_dir, 'wikienergy.h5'))
print('loaded ' + str(len(we.buildings)) + ' buildings')
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();
In [5]:
fraction = elec.submeters().fraction_per_meter().dropna()
In [6]:
# Create convenient labels
labels = elec.get_appliance_labels(fraction.index)
plt.figure(figsize=(8,8))
fraction.plot(kind='pie', labels=labels);
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)}
]
In [8]:
elec.mains().plot()
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()
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()