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'] = (12, 6)
rcParams['figure.figsize'] = (13, 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, show_versions
from nilmtk.metrics import f1_score
#import seaborn as sns
#sns.set_palette("Set3", n_colors=12)


import warnings
warnings.filterwarnings("ignore")

"""
Explanation: Disaggregation experiments
Customary imports
End of explanation
"""


show_versions()


"""
Explanation: show versions for any diagnostics
End of explanation
"""


data_dir = '/Users/GJWood/nilm_gjw_data/HDF5/'
gjw = DataSet(join(data_dir, 'nilm_gjw_data.hdf5'))
print('loaded ' + str(len(gjw.buildings)) + ' buildings')
building_number=1

"""
Explanation: Load dataset
End of explanation
"""


gjw.store.window = TimeFrame(start='2013-12-03 00:00:00', end='2013-12-05 00:00:00')
elec = gjw.buildings[building_number].elec
mains = elec.mains()

house = elec['fridge'] #only one meter so any selection will do
df = house.load().next() #load the first chunk of data into a dataframe
df.info() #check that the data is what we want (optional)
#note the data has two columns and a time index


df.head()

df.tail()

df.plot()

"""
Explanation: Let us perform our analysis on selected 2 days
End of explanation
"""


df.ix['2013-12-03 11:00:00':'2013-12-03 12:00:00'].plot()# select a time range and plot it

from nilmtk.disaggregate.hart_85 import Hart85
h = Hart85()
h.train(mains,cols=[('power','apparent'),('power','reactive')])


from nilmtk.disaggregate.hart_85 import Hart85
h = Hart85()
h.train(mains,cols=[('power','apparent')])

"""
Explanation: Training
We'll now do the training from the aggregate data. The algorithm segments the time series data into steady and transient states. Thus, we'll first figure out the transient and the steady states. Next, we'll try and pair the on and the off transitions based on their proximity in time and value.
End of explanation
"""


