import pickle
import pandas as pd
import numpy as np
import benchmark         # the main benchmarking script file
import bench_util        # some utility functions related to benchmarking

# Just in case you want to plot something:
import matplotlib.pyplot as plt
 
# Show Plots in the Notebook
%matplotlib inline

# Increase the size of plots and their fonts
plt.rcParams['figure.figsize']= (10, 8)   # set Chart Size
plt.rcParams['font.size'] = 14            # set Font size in Chart

# Use a plotting style'style' the plot using 'bmh' style
plt.style.use('bmh')

"""
Explanation: Example of Using the Utility Bill data in a Notebook
End of explanation
"""


# Read the raw Utility Bill data into a DataFrame
df_raw = pd.read_pickle('df_raw.pkl')

# Read the pre-processed data
dfp = pd.read_pickle('df_processed.pkl')

# Read in the bench_util.Util object, which provides some other data that may 
# be useful.
util_obj = pickle.load(open('util_obj.pkl', 'rb'))

"""
Explanation: Using Data from the Last Run of the Script
This saves about 4 minutes of time because reading the raw data and pre-processing it is avoided.
End of explanation
"""


df_raw.head(3)

dfp.head(3)

# Get some other building info about the Big Dipper building
# A dictionary is returned.
util_obj.building_info('DIPMP1')

"""
Explanation: Take a look at what you got
End of explanation
"""


# This calls the preprocessing function in the main benchmark script file.
# Values in the 'settings.py' file will determine which files are read.
# NOTE:  This will take 4 minutes to run, and the Jupyter Notebook does not 
# always show the messages that printed along the way, so it will look like
# your computer is frozen. Be patient.

# Uncomment the line below to run
# df_raw, dfp, util_obj = benchmark.preprocess_data()

"""
Explanation: Instead, Rerun the Preprocessing routine to get the DataFrames and Util object
End of explanation
"""


# You can't use the Pandas "query" method for columns that have spaces in
# their names.  So use the more general, but cumbersome syntax to filter 
# the "Site ID" column.
# NOTE: a quick rename of the columns to eliminate spaces is another approach:
#  df_raw.columns = [c.replace(' ', '_') for c in df_raw.columns]
df_dip = df_raw[df_raw['Site ID']=='DIPMP1']

df_dip.query("From < '2017-06-01' and From > '2017-01-01' and Units=='kW'")

# Look at the processed data to compare Actual Demand vs. Demand Charge
df_dip = dfp.query("site_id=='DIPMP1' and units=='kW'")
df_pivot = pd.pivot_table(df_dip, index=['cal_year', 'cal_mo'], values='usage', columns='item_desc', aggfunc=np.sum)

# Do the following to show all rows of the DataFrame
with pd.option_context('display.max_rows', None):
    print(df_pivot)

"""
Explanation: Look at Some Data to Illustrate the Duplicate kW Demand Charge Problem
End of explanation
"""


