import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
import itertools
import pickle

%matplotlib inline

"""
Explanation: 12-752: Data-Driven Building Energy Management
Fall 2016, Carnegie Mellon University
Assignment #2
We will begin by unpickling the dataset we had played around with in Lecture 4. But first, we will load most modules we will be using:
End of explanation
"""


pickle_file = open('../../lectures/data/campusDemand.pkl','rb')
pickled_data = pickle.load(pickle_file)
pickle_file.close()

# Since we pickled them all together as a list, I'm going to assign each element of the list to the same variable
# we had been using before:
data = pickled_data[0]
pointNames = pickled_data[1]
data_by_day = pickled_data[2]
idx = pickled_data[3]

"""
Explanation: To unpickle just do this:
End of explanation
"""


# Your code goes here


"""
Explanation: -=-=-= Exploring hourly and weekly consumption patterns (no seasonality) =-=-=-
Task #1 (10%)
Create a new Pandas Data Frame that contains only two columns (Time and Value) and only the rows that belong to the University-wide meter (Electric kW Calculations Main Campus). In other words, get rid of the Point Name column and select only the rows for the campus meter.
End of explanation
"""


# Your code goes here



"""
Explanation: Task #2 (10%)
In one figure, plot one histogram showing the average hourly consumption on the entire dataset.
In a separate figure, plot 7 subplots with similar histograms but now showing the average hourly consumption for each day of the weeek (hence the 7 supplots).
End of explanation
"""


# Your code goes here



"""
Explanation: Task #3 (10%)
In one figure, create a box plot of the average hourly electricity consumption for each hour of the day (i.e., your plot will show 24 boxes, one for each hour of the day, and each box will represent the distribution of the average hourly consumption in the dataset for that hour).
In another figure, create 7 subplots showing the same box plots as above, but now for each of the seven days of the week.
End of explanation
"""


# Your code goes here


"""
Explanation: -=-=-= Exploring seasonal effects =-=-=-
Task #4 (10%)
Create a stem plot of the average daily electricity consumption for the whole dataset (i.e., the plot should have ~365 stems):
End of explanation
"""


# Your code goes here...





"""
Explanation: Task #5 (10%)
What are your findings so far? Please elaborate on how the above plots and analysis have informed you about the data. 
Your answer goes here...
Task #6 (10%)
Create a new DataFrame called loadCurves, which contains 24 columns (one for each hour of the day) and each of those columns has a Series with as many rows as there are days in our dataset. Each column wil be composed of the average power consumed during that particular hour for each day of the year.
Note: You may benefit from knowing about the groupby and unstack methods for DataFrames.
End of explanation
"""


# Your code goes here...

"""
Explanation: Task #7 (10%)
Create a heatmap of the daily load curves for campus, similar to those shown in Paper #1. In particular, this heatmap will be a 2D map with the horizontal axis showing the hours of the day (24 in total), and the vertical axis showing the day of the year (~365 total). Then each cell will be color-coded with the value corresponding to the average power consumed during this hour. 
Try different colormaps to see which one works best for you.
Note: you may need to normalize the data to see differences.
End of explanation
"""


# Your code goes here...

"""
Explanation: Task #8 (20%)
Let's see if we can find some patterns in these load curves. Using your favorite implementation and flavor of the k-means algorithm, play around with clustering the daily loadCurves to see if we can find 2 or 3 clusters that would best differentiate between weekdays and weekends. In other words, perform k-means (or k-medioids, or whatever) on the dataset with $k \in {2, 3}$ and the dataset being 365 samples of 24-dimensional vectors.
Note: you will only check the weekend vs. weekday labels after clustering (i.e., do not use this attribute for clustering, but rather only the 24 average hourly consumption values).
End of explanation
"""


# Your code goes here...

"""
Explanation: Task #9 (10%)
In separate plots (one for each cluster), plot the cluster centroids (in a dark, thick line) and the load curves that belong to the cluster (using thin grayish lines), just like the paper did. 
What did you learn from the experiment above?
End of explanation
"""


