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
import seaborn as sns
rcParams['figure.figsize'] = (14, 7)
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 = '/home/nipun/datasets'
we = DataSet(join(data_dir, 'wikienergy.h5'))
print('loaded ' + str(len(we.buildings)) + ' buildings')
In [4]:
print_dict(we.metadata)
In [41]:
building_number = 22
print_dict(we.buildings[building_number].metadata)
In [42]:
elec = we.buildings[building_number].elec
elec.appliances
Out[42]:
In [43]:
elec.draw_wiring_graph()
In [44]:
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 [9]:
# Select a subset of fridges, otherwise the computation takes a long time
fridges_restricted = MeterGroup(fridges.meters[:5])
proportion_per_fridge = fridges_restricted.proportion_of_upstream_total_per_meter()
In [10]:
proportion_per_fridge.plot(kind='bar');
plt.title('fridge energy as proportion of total building energy');
plt.ylabel('proportion');
plt.xlabel('Fridge (<appliance instance>, <building instance>, <dataset name>)');
In [11]:
# How much energy does the largest-consuming fridge consume in kWh?
fridges.select(building=61).total_energy()
Out[11]:
In [12]:
fridges.select(building=61).plot();
In [13]:
fridges_restricted = MeterGroup(fridges.meters[:20])
daily_energy = pd.Series([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 [85]:
df = elec.power_series_all_data()
In [86]:
print([meter.appliance_label() for meter in elec.meters[1:]])
In [87]:
df = pd.DataFrame({meter.appliance_label():meter.power_series_all_data() for meter in elec.meters[1:]}, index=elec.meters[1].power_series_all_data().index)
In [88]:
df
Out[88]:
In [89]:
x=np.arange(10)
ys = [i+x+(i*x)**2 for i in range(10)]
from matplotlib import cm
colors = iter(cm.rainbow(np.linspace(0, 1, len(ys))))
for y in ys:
plt.scatter(x, y, color=next(colors))
In [90]:
df.mean()
Out[90]:
In [91]:
sns.set_palette("Set3", n_colors=12)
df.plot(kind='area')
plt.xlabel("Time")
plt.ylabel("Power (W)")
plt.tight_layout()
plt.savefig("submetered.png")
In [21]:
sns.timeseries(df)
In [84]:
we.store.window = TimeFrame(start='2014-05-04 12:00:00-05:00', end='2014-05-04 15:30:00-05:00')
#elec.plot(kind='area');
In [24]:
sns.palplot(sns.color_palette("Set3", 12))
In [25]:
sns.choose_colorbrewer_palette()
In [93]:
fraction = elec.submeters().fraction_per_meter().dropna()
In [94]:
# Create convenient labels
labels = elec.get_appliance_labels(fraction.index)
plt.figure(figsize=(8,8))
fraction.plot(kind='pie', labels=labels);
In [17]:
elec.select_using_appliances(category='heating')
Out[17]:
In [18]:
# Find all appliances with a particular type of motor
elec.select_using_appliances(category='single-phase induction motor')
Out[18]:
In [19]:
# Train
co = CombinatorialOptimisation()
co.train(elec)
In [20]:
# Disaggregate
disag_filename = join(data_dir, 'wikienergy-disag.h5')
output = HDFDataStore(disag_filename, 'w')
co.disaggregate(elec.mains(), output)
output.close()
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 [21]:
for model in co.model:
print_dict(model)
In [22]:
disag = DataSet(disag_filename)
disag_elec = disag.buildings[building_number].elec
disag_elec.plot()
disag.store.close()
In [23]:
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()