In [11]:
from __future__ import print_function, division

import matplotlib
#matplotlib.use('nbagg') # interactive plots in iPython. New in matplotlib v1.4
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import ticker
import seaborn as sns
sns.reset_orig()

import nilmtk
import numpy as np
from neuralnilm.rectangles import plot_rectangles

In [35]:
STRIDE = 16
# APPLIANCES = ['kettle', 'microwave', 'washing machine', 'dish washer', 'fridge']
APPLIANCES = ['kettle', 'washing machine', 'fridge']
ARCHITECTURES = ['rnn', 'ae', 'rectangles']
PALETTE = 'bright'
FAVORITES = {'fridge': 23, 'kettle': 23, 'washing machine': 23, 'microwave': 11, 'dish washer': 19}

In [36]:
def get_single_output(ax, appliance):
    colors = sns.palettes.color_palette(PALETTE, n_colors=len(ARCHITECTURES) + 1)
    seq_i = FAVORITES[appliance]
    
    # Plot target
    target_filename = appliance + '_target.npy'
    target = np.load(target_filename)
    ax.plot(target[seq_i], color=colors[3], label='Target', zorder=20)
    
    # Seq length
    seq_length = target.shape[1]
    
    # Plot outputs
    for arch_i, arch in enumerate(ARCHITECTURES):
        output_filename = appliance + '_output_' + arch + '.npy'
        output = np.load(output_filename)
        seq = output[seq_i]
        color = colors[arch_i]
        if arch == 'rectangles':
            plot_rectangles(
                ax, seq, plot_seq_width=seq_length, 
                alpha=0.5, color=color, label='Rectangles', zorder=0)
        else:
            ax.plot(seq, color=color, label=arch.upper(), zorder=10)

    ax.set_xlim((0, seq_length))

In [39]:
nilmtk.plots.latexify(fig_width=7, fontsize=8, fig_height=4)

plt.close()
fig, axes = plt.subplots(ncols=len(APPLIANCES))
for appliance, ax in zip(APPLIANCES, axes):
    get_single_output(ax, appliance)
    
    # Format
    ax.set_title(appliance.title())
    ylim = ax.get_ylim()
    ax.set_ylim((0, ylim[1]))
    ax = nilmtk.plots.format_axes(ax, spine_color='k')
    ax.xaxis.set_major_locator(ticker.MaxNLocator(2))
    ax.yaxis.set_ticks([])
    
plt.tight_layout()
plt.savefig(
    '/home/dk3810/Dropbox/MyWork/imperial/PhD/writing/papers/BuildSys_2015_Neural_NILM/net_output.pdf',
    bbox_inches='tight',
    pad_inches=0)
plt.show()



In [206]:
# Separate subplots for each architecture

plt.close()

nilmtk.plots.latexify(fig_width=7, fontsize=8, fig_height=8)

LINEWIDTH = 0.5
NROWS = (len(ARCHITECTURES) * 2) + 2
NCOLS = len(APPLIANCES)

fig, axes = plt.subplots(
    nrows=NROWS, 
    ncols=NCOLS,
    sharex='col',
    sharey=False
)

# New clear axis overlay with 0-1 limits for the grey rectangles
ax = matplotlib.axes.Axes(
    fig=fig, rect=[-0.01, 0, 1, 1], axisbg=(1, 1, 1, 0), frameon=False, zorder=0)
fig.add_axes(ax)
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlim((0, 1))
ax.set_ylim((0, 1))
TEXT_X = -0.005
RECT_COLOR = (.9, .9, .9)
RECT_WIDTH = 0.045

fig.text(TEXT_X, 0.92, 'Data from House 1', rotation=90, zorder=10)
rect = matplotlib.patches.Rectangle(
    (0, 0.75), 
    width=RECT_WIDTH, 
    height=0.22, 
    color=RECT_COLOR, 
    clip_on=False)
ax.add_patch(rect)

fig.text(TEXT_X, 0.66, 'Raw Output from Neural Nets', rotation=90, zorder=10)
rect = matplotlib.patches.Rectangle(
    (0, 0.4), 
    width=RECT_WIDTH, 
    height=0.33, 
    color=RECT_COLOR, 
    clip_on=False)
ax.add_patch(rect)

fig.text(TEXT_X, 0.33, 'Overlapping Output from Neural Nets', rotation=90, zorder=10)
rect = matplotlib.patches.Rectangle(
    (0, 0.05), 
    width=RECT_WIDTH, 
    height=0.33, 
    color=RECT_COLOR, 
    clip_on=False)
ax.add_patch(rect)

colors = sns.palettes.color_palette(PALETTE, n_colors=5)

for col, appliance in enumerate(APPLIANCES):
    seq_i = FAVORITES[appliance]
    
    # Load target
    target_filename = appliance + '_target.npy'
    target = np.load(target_filename)
    seq_length = target.shape[1]
    
    def format_axes(ax, label):
        ax = nilmtk.plots.format_axes(ax, spine_color='k')
        ax.xaxis.set_major_locator(ticker.MaxNLocator(2))
        ax.set_xlim((0, seq_length))
        if label == 'Aggregate':
            ax.yaxis.set_major_locator(ticker.MaxNLocator(3))
        else:
            if label == 'AE':
                label = 'Autoencoder'
            elif label == 'RNN':
                label = 'LSTM'
            ax.set_ylim((0, 1.2))
            ax.set_yticks([0.0, 0.5, 1.0])
            if col > 0:
                ax.set_yticklabels([])
        if col == 0:
            ax.set_ylabel(label)
        ax.patch.set_visible(False)
        ax.set_zorder(10)

    # Plot aggregate
    ax = axes[0, col]
    aggregate_filename = appliance + '_input.npy'
    aggregate = np.load(aggregate_filename)
    ax.plot(aggregate[seq_i], color=colors[0], linewidth=LINEWIDTH)
    format_axes(ax, 'Aggregate')
    ax.set_title(appliance.title())
            
    # Plot target
    ax = axes[1, col]
    ax.plot(target[seq_i], color=colors[1], linewidth=LINEWIDTH)
    format_axes(ax, 'Appliance')
    
    for arch_i, arch in enumerate(ARCHITECTURES):
        row = arch_i + 2
        color = colors[row]
        
        # Plot raw network outputs
        ax = axes[row, col]
        output_filename = appliance + '_output_' + arch + '.npy'
        output = np.load(output_filename)
        seq = output[seq_i]
        if arch == 'rectangles':
            plot_rectangles(
                ax, seq, plot_seq_width=seq_length, how='line',
                color=color, linewidth=LINEWIDTH)
            format_axes(ax, 'Rectangles')
        else:
            ax.plot(seq, color=color, linewidth=LINEWIDTH)
            format_axes(ax, arch.upper())
            
        # Plot overlapping strided outputs
        ax = axes[row + 3, col]
        output = np.load(appliance + '_strided_output_' + arch + '.npy')
        num_seq_per_batch = output.shape[0]
        alpha = STRIDE / seq_length
        for stride_seq_i in range(num_seq_per_batch):
            start = stride_seq_i * STRIDE - seq_length
            end = start + seq_length
            x = np.arange(start, end)
            if arch == 'rectangles':
                plot_rectangles(
                    ax, output[stride_seq_i], plot_seq_width=seq_length, offset=start, 
                    alpha=alpha, color=color, linewidth=2)
                format_axes(ax, 'Rectangles')
            else:
                if arch == 'ae':
                    data = output[stride_seq_i, :, 0]
                elif arch == 'rnn':
                    x = x[50:-50]
                    data = output[stride_seq_i, 50:-50, 0]
                ax.plot(x, data, color=color, alpha=alpha, linewidth=2)
                format_axes(ax, arch.upper())
    
axes[-1, 1].set_xlabel('Time (number of samples)')
    
plt.tight_layout()
plt.savefig(
    '/home/dk3810/Dropbox/MyWork/imperial/PhD/writing/papers/BuildSys_2015_Neural_NILM/net_output.pdf',
    bbox_inches='tight',
    pad_inches=0)
#plt.show()

In [ ]: