In [25]:
from __future__ import division

import os

import csv
import sqlite3

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
import pandas as pd
import datetime
import pytz

import seaborn as sns
%matplotlib inline

from datetime import date, timedelta, datetime
from pytz import timezone
from dateutil.tz import tzutc, tzoffset
from dateutil import tz
from pandas.tseries.offsets import *
from pandas import *

from scipy import stats

Meters for each building in each catogory


In [2]:
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('US/Eastern')

In [3]:
DAH = ['WA_12_EL_BL_997_2', 'WA_12_EL_BL_997_1', 'WA_12_EL_BL_221_2',
       'WA_12_EL_BL_221_1', 'WA_12_EL_BL_182_2', 'WA_12_EL_BL_182_1',
       'WA_12_EL_BL_1500_3', 'WA_12_EL_BL_1500_2', 'WA_12_EL_BL_1500_1',
       'WA_12_EL_BL_135_2', 'WA_12_EL_BL_135_1', 'WA_12_EL_BL_121_2',
       'WA_12_EL_BL_121_1', 'WA_12_EL_BL_120M_1', 'WA_12_EL_BL_1200_3',
       'WA_12_EL_BL_1200_2', 'WA_12_EL_BL_1200_1']

WNY = ['WA_02_EL_BL_58_1']

JBAB = ['WA_03_EL_BL_417_2','WA_03_EL_BL_417_1', 'WA_03_EL_BL_3623_2', 'WA_03_EL_BL_21_1',
        'WA_03_EL_BL_1_1', 'WA_03_EL_BL_15_B_2', 'WA_03_EL_BL_15_A_1',]

Note: Original timestep is UTC, need to convert it to local time zone: UTC-05:00


In [4]:
TechOffice = [s for s in DAH if "BL_121" in s]
BowlingCenter_135 = [s for s in DAH if "BL_135" in s]
Building_182 = [s for s in DAH if "BL_182" in s]

In [5]:
# consider the holidays
holidays = pd.read_csv('holiday.csv', usecols=[0])
holidays['date'] = pd.to_datetime(pd.Series(holidays['date']), format='%m/%d/%y')
holidays['day'] = holidays.date.apply(lambda x: x.strftime('%Y-%m-%d'))

def MeterData_Format(data):
    data['date'] = data.index
    data['power'] = data[[col for col in data.columns if "WA" in col]].sum(axis=1)
    data['day'] = map(lambda x: x.strftime('%Y/%m/%d'),data.index)
    data['time'] = map(lambda x: x.strftime('%H:%M'),data.index)
    data['month'] = map(lambda x: x.strftime('%m'),data.index)
    data['hour'] = map(lambda x: x.strftime('%H'),data.index)
    data['month'] = data.month.astype(int)
    data['hour'] = data.hour.astype(int)
    data['weekday'] = data.index.weekday
    data['holiday'] = map(lambda x: x.strftime('%Y-%m-%d') in holidays.day.values,data.index)
    data['holiday'] = data.holiday.astype(int)
    
    return data

In [6]:
def MergeMeterID(MeterID):
    df = pd.read_csv('../meter/' + MeterID + '.csv')
    df.index = df['date'].apply(lambda x: datetime.strptime(x,'%Y-%m-%d %H:%M:%S') + timedelta(hours=-5))
    df.columns = ['date',MeterID]
    df.drop(df.columns[[0]],axis=1,inplace=True)
    
    return df

In [12]:
def CleanMeterData(BuildingID,weather):
    dfs = [MergeMeterID(MeterID) for MeterID in BuildingID]
    data = pd.concat(dfs,axis=1)
    data = MeterData_Format(data)
    
    df = pd.concat([data,weather],axis=1,join_axes=[data.index])

    # calculate basepower on weekdays
    df.loc[:,'basepower'] = 0
    df.loc[:,'peakoat'] = 0

    # calculate the daily peak OAT
    PeakOAT = df.groupby(['day'])['oat'].max()

    for day in PeakOAT.index:
        df.loc[(df['day'] == day),'peakoat'] = PeakOAT[day]

    # subset the weekday and weekend power
    df_wd = df.loc[(df['weekday'] >= 0) & (df['weekday'] <= 4) & (df['holiday'] == 0)]
    df_wk = df.loc[(df.weekday.apply(lambda x: x in [5,6])) | (df['holiday'] == 1)]

    # calculate the basepower on weekdays and weekend
    df_wd_base = df_wd.loc[(df_wd['oat'] < 68)]
    df_wd_basedaily = df_wd_base.pivot(index='time',columns='day',values='power')
    df_wk_base = df_wk.loc[(df_wk['oat'] < 68)]
    df_wk_basedaily = df_wk_base.pivot(index='time',columns='day',values='power')

    for tim in df_wd_basedaily.index:
        df.loc[(df['time'] == tim) & (df['weekday'] >= 0) & \
           (df['weekday'] <= 4) & (df['holiday'] == 0),'basepower'] = df_wd_basedaily.mean(axis=1)[tim]
        df.loc[(df['time'] == tim) & (df.weekday.apply(lambda x: x in [5,6])),'basepower'] = df_wk_basedaily.mean(axis=1)[tim]
        df.loc[(df['time'] == tim) & (df['holiday'] == 1),'basepower'] = df_wk_basedaily.mean(axis=1)[tim]

    df.loc[:,'hvac'] = df.power - df.basepower
    
    return data, df

In [13]:
dfs = [MergeMeterID(MeterID) for MeterID in Building_182]
data = pd.concat(dfs,axis=1)
data = MeterData_Format(data)

In [14]:
weather = pd.read_csv('../weather/KVACOLON6.csv')
weather.columns = ['date','oat']
weather.index = weather['date'].apply(lambda x: datetime.strptime(x,'%m/%d/%Y %H:%M'))
weather.drop(weather[['date']],axis=1,inplace=True)
weather = weather.groupby(weather.index).first() # remove the duplicate index

Create the entire dataset from meter data and weather data


In [15]:
data,df = CleanMeterData(Building_182,weather)

In [17]:
df


Out[17]:
WA_12_EL_BL_182_2 WA_12_EL_BL_182_1 date power day time month hour weekday holiday oat basepower peakoat hvac
date
2015-01-01 00:15:00 2.4 6.4 2015-01-01 00:15:00 8.8 2015/01/01 00:15 1 0 3 1 26.2 9.044828 46.3 -0.244828
2015-01-01 00:30:00 3.6 4.8 2015-01-01 00:30:00 8.4 2015/01/01 00:30 1 0 3 1 27.1 8.779487 46.3 -0.379487
2015-01-01 00:45:00 2.4 6.4 2015-01-01 00:45:00 8.8 2015/01/01 00:45 1 0 3 1 26.4 9.038655 46.3 -0.238655
2015-01-01 01:00:00 2.4 6.4 2015-01-01 01:00:00 8.8 2015/01/01 01:00 1 1 3 1 27.3 8.853333 46.3 -0.053333
2015-01-01 01:15:00 3.6 6.4 2015-01-01 01:15:00 10.0 2015/01/01 01:15 1 1 3 1 27.0 9.084298 46.3 0.915702
2015-01-01 01:30:00 2.4 6.4 2015-01-01 01:30:00 8.8 2015/01/01 01:30 1 1 3 1 27.3 8.922314 46.3 -0.122314
2015-01-01 01:45:00 3.6 6.4 2015-01-01 01:45:00 10.0 2015/01/01 01:45 1 1 3 1 26.9 8.953600 46.3 1.046400
2015-01-01 02:00:00 2.4 4.8 2015-01-01 02:00:00 7.2 2015/01/01 02:00 1 2 3 1 27.0 8.898361 46.3 -1.698361
2015-01-01 02:15:00 2.4 6.4 2015-01-01 02:15:00 8.8 2015/01/01 02:15 1 2 3 1 27.5 8.888525 46.3 -0.088525
2015-01-01 02:30:00 3.6 6.4 2015-01-01 02:30:00 10.0 2015/01/01 02:30 1 2 3 1 28.2 9.085246 46.3 0.914754
2015-01-01 02:45:00 2.4 6.4 2015-01-01 02:45:00 8.8 2015/01/01 02:45 1 2 3 1 26.8 8.848387 46.3 -0.048387
2015-01-01 03:00:00 2.4 4.8 2015-01-01 03:00:00 7.2 2015/01/01 03:00 1 3 3 1 28.5 8.879365 46.3 -1.679365
2015-01-01 03:15:00 3.6 6.4 2015-01-01 03:15:00 10.0 2015/01/01 03:15 1 3 3 1 27.6 9.075200 46.3 0.924800
2015-01-01 03:30:00 2.4 4.8 2015-01-01 03:30:00 7.2 2015/01/01 03:30 1 3 3 1 28.5 9.060317 46.3 -1.860317
2015-01-01 03:45:00 3.6 6.4 2015-01-01 03:45:00 10.0 2015/01/01 03:45 1 3 3 1 28.4 9.022222 46.3 0.977778
2015-01-01 04:00:00 2.4 4.8 2015-01-01 04:00:00 7.2 2015/01/01 04:00 1 4 3 1 28.6 9.064567 46.3 -1.864567
2015-01-01 04:15:00 2.4 6.4 2015-01-01 04:15:00 8.8 2015/01/01 04:15 1 4 3 1 28.7 9.130709 46.3 -0.330709
2015-01-01 04:30:00 3.6 6.4 2015-01-01 04:30:00 10.0 2015/01/01 04:30 1 4 3 1 28.6 9.087500 46.3 0.912500
2015-01-01 04:45:00 2.4 6.4 2015-01-01 04:45:00 8.8 2015/01/01 04:45 1 4 3 1 28.6 9.215625 46.3 -0.415625
2015-01-01 05:00:00 2.4 4.8 2015-01-01 05:00:00 7.2 2015/01/01 05:00 1 5 3 1 27.8 9.209231 46.3 -2.009231
2015-01-01 05:15:00 6.0 6.4 2015-01-01 05:15:00 12.4 2015/01/01 05:15 1 5 3 1 28.8 9.538462 46.3 2.861538
2015-01-01 05:30:00 4.8 6.4 2015-01-01 05:30:00 11.2 2015/01/01 05:30 1 5 3 1 27.1 9.675758 46.3 1.524242
2015-01-01 05:45:00 6.0 6.4 2015-01-01 05:45:00 12.4 2015/01/01 05:45 1 5 3 1 28.2 9.651908 46.3 2.748092
2015-01-01 06:00:00 3.6 6.4 2015-01-01 06:00:00 10.0 2015/01/01 06:00 1 6 3 1 27.2 9.596947 46.3 0.403053
2015-01-01 06:15:00 4.8 6.4 2015-01-01 06:15:00 11.2 2015/01/01 06:15 1 6 3 1 27.5 9.850382 46.3 1.349618
2015-01-01 06:30:00 4.8 6.4 2015-01-01 06:30:00 11.2 2015/01/01 06:30 1 6 3 1 26.8 9.852308 46.3 1.347692
2015-01-01 06:45:00 4.8 4.8 2015-01-01 06:45:00 9.6 2015/01/01 06:45 1 6 3 1 26.6 9.956923 46.3 -0.356923
2015-01-01 07:00:00 3.6 8.0 2015-01-01 07:00:00 11.6 2015/01/01 07:00 1 7 3 1 28.2 9.673846 46.3 1.926154
2015-01-01 07:15:00 4.8 4.8 2015-01-01 07:15:00 9.6 2015/01/01 07:15 1 7 3 1 27.2 9.789147 46.3 -0.189147
2015-01-01 07:30:00 3.6 6.4 2015-01-01 07:30:00 10.0 2015/01/01 07:30 1 7 3 1 30.6 9.593701 46.3 0.406299
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2016-04-27 17:00:00 1.2 8.0 2016-04-27 17:00:00 9.2 2016/04/27 17:00 4 17 2 0 61.8 11.215556 71.9 -2.015556
2016-04-27 17:15:00 1.2 6.4 2016-04-27 17:15:00 7.6 2016/04/27 17:15 4 17 2 0 61.3 10.450549 71.9 -2.850549
2016-04-27 17:30:00 2.4 6.4 2016-04-27 17:30:00 8.8 2016/04/27 17:30 4 17 2 0 60.7 10.200000 71.9 -1.400000
2016-04-27 17:45:00 1.2 6.4 2016-04-27 17:45:00 7.6 2016/04/27 17:45 4 17 2 0 60.2 9.796791 71.9 -2.196791
2016-04-27 18:00:00 1.2 6.4 2016-04-27 18:00:00 7.6 2016/04/27 18:00 4 18 2 0 59.6 9.614433 71.9 -2.014433
2016-04-27 18:15:00 1.2 6.4 2016-04-27 18:15:00 7.6 2016/04/27 18:15 4 18 2 0 58.8 9.483417 71.9 -1.883417
2016-04-27 18:30:00 1.2 4.8 2016-04-27 18:30:00 6.0 2016/04/27 18:30 4 18 2 0 58.2 9.483168 71.9 -3.483168
2016-04-27 18:45:00 2.4 8.0 2016-04-27 18:45:00 10.4 2016/04/27 18:45 4 18 2 0 57.7 9.523902 71.9 0.876098
2016-04-27 19:00:00 1.2 4.8 2016-04-27 19:00:00 6.0 2016/04/27 19:00 4 19 2 0 57.2 9.449275 71.9 -3.449275
2016-04-27 19:15:00 2.4 6.4 2016-04-27 19:15:00 8.8 2016/04/27 19:15 4 19 2 0 56.7 9.490909 71.9 -0.690909
2016-04-27 19:30:00 1.2 4.8 2016-04-27 19:30:00 6.0 2016/04/27 19:30 4 19 2 0 56.2 9.327700 71.9 -3.327700
2016-04-27 19:45:00 2.4 6.4 2016-04-27 19:45:00 8.8 2016/04/27 19:45 4 19 2 0 55.8 9.512150 71.9 -0.712150
2016-04-27 20:00:00 2.4 4.8 2016-04-27 20:00:00 7.2 2016/04/27 20:00 4 20 2 0 55.4 9.244444 71.9 -2.044444
2016-04-27 20:15:00 2.4 4.8 2016-04-27 20:15:00 7.2 2016/04/27 20:15 4 20 2 0 55.1 9.247273 71.9 -2.047273
2016-04-27 20:30:00 2.4 6.4 2016-04-27 20:30:00 8.8 2016/04/27 20:30 4 20 2 0 55.0 9.060633 71.9 -0.260633
2016-04-27 20:45:00 2.4 4.8 2016-04-27 20:45:00 7.2 2016/04/27 20:45 4 20 2 0 55.0 9.101786 71.9 -1.901786
2016-04-27 21:00:00 2.4 4.8 2016-04-27 21:00:00 7.2 2016/04/27 21:00 4 21 2 0 55.0 9.059031 71.9 -1.859031
2016-04-27 21:15:00 1.2 6.4 2016-04-27 21:15:00 7.6 2016/04/27 21:15 4 21 2 0 55.0 9.180952 71.9 -1.580952
2016-04-27 21:30:00 2.4 4.8 2016-04-27 21:30:00 7.2 2016/04/27 21:30 4 21 2 0 55.0 9.148498 71.9 -1.948498
2016-04-27 21:45:00 2.4 4.8 2016-04-27 21:45:00 7.2 2016/04/27 21:45 4 21 2 0 55.0 9.155365 71.9 -1.955365
2016-04-27 22:00:00 2.4 4.8 2016-04-27 22:00:00 7.2 2016/04/27 22:00 4 22 2 0 55.0 9.169362 71.9 -1.969362
2016-04-27 22:15:00 2.4 6.4 2016-04-27 22:15:00 8.8 2016/04/27 22:15 4 22 2 0 55.0 9.061017 71.9 -0.261017
2016-04-27 22:30:00 1.2 4.8 2016-04-27 22:30:00 6.0 2016/04/27 22:30 4 22 2 0 55.1 9.125738 71.9 -3.125738
2016-04-27 22:45:00 2.4 4.8 2016-04-27 22:45:00 7.2 2016/04/27 22:45 4 22 2 0 55.1 9.114644 71.9 -1.914644
2016-04-27 23:00:00 2.4 6.4 2016-04-27 23:00:00 8.8 2016/04/27 23:00 4 23 2 0 55.2 9.297521 71.9 -0.497521
2016-04-27 23:15:00 2.4 4.8 2016-04-27 23:15:00 7.2 2016/04/27 23:15 4 23 2 0 55.4 9.091803 71.9 -1.891803
2016-04-27 23:30:00 2.4 6.4 2016-04-27 23:30:00 8.8 2016/04/27 23:30 4 23 2 0 55.5 9.167213 71.9 -0.367213
2016-04-27 23:45:00 1.2 4.8 2016-04-27 23:45:00 6.0 2016/04/27 23:45 4 23 2 0 55.7 9.102041 71.9 -3.102041
2016-04-28 00:00:00 2.4 4.8 2016-04-28 00:00:00 7.2 2016/04/28 00:00 4 0 3 0 55.8 9.030204 55.8 -1.830204
2016-04-28 00:15:00 2.4 NaN 2016-04-28 00:15:00 2.4 2016/04/28 00:15 4 0 3 0 55.8 9.013008 55.8 -6.613008

46369 rows × 14 columns


In [32]:
# Read the holiday and DR event days 
drevent = pd.read_csv('DR_Events_2015.csv', usecols=[3])
drevent['date'] = pd.to_datetime(pd.Series(drevent['date']), format='%m/%d/%y')
drevent['day'] = drevent.date.apply(lambda x: x.strftime('%Y-%m-%d'))

# offdays = pd.DataFrame.append(holidays, drevent)

# Sort DR event days' power use
data = df.copy()

data['year'] = data.date.apply(lambda x: x.strftime('%Y'))
data['time'] = data.date.apply(lambda x: x.strftime('%H:%M'))
data['month'] = data.date.apply(lambda x: x.strftime('%m'))
data['day'] = data.date.apply(lambda x: x.strftime('%d'))
data['hour'] = data.date.apply(lambda x: x.strftime('%H'))
data['minute'] = data.date.apply(lambda x: x.strftime('%M'))
data['weekday'] = data.date.apply(lambda x: x.strftime('%w'))
data['weekday'] = data.weekday.astype(int)
data['DR'] = data.date.apply(lambda x: x.strftime('%Y-%m-%d') in drevent.day.values)
data['DR'] = data.DR.astype(int)
data['holiday'] = data.date.apply(lambda x: x.strftime('%Y-%m-%d') in holidays.day.values)
data['holiday'] = data.holiday.astype(int)

# weekdays for each baseline
data_wk = data[(data['weekday'] > 0) & (data['holiday']==0) & (data['weekday'] < 6)]
data_wk.to_csv('../results/Building182/data_wk.csv')

baseline_day = []
baseline = pd.DataFrame()

# kW shed
kW_shed = 5

# plot all ADR performance figures
ADR_Summary = pd.DataFrame()
# plot all ADR performance figures
i, j, k = 0, 1, 0
for k in range(4):
    
    j = 1
    baseline_day = []
    DayAverage = []
    baseline = pd.DataFrame()
    DR_Event = pd.DataFrame()
        
    for i in range(20):
        baseline_ref = drevent.date[k] - (i+1) * Day()
        if baseline_ref in data_wk.index:
            
            baseline_indicator = data_wk[baseline_ref.strftime('%Y-%m-%d')]['DR'][0]
            baseline_day_indicator = data_wk[baseline_ref.strftime('%Y-%m-%d')]['date'][0]
            if j <= 10:
                if baseline_indicator == 0:
                    j+=1
                    baseline_day = data_wk[baseline_ref.strftime('%Y-%m-%d')][['time','power']]
                    baseline_day.index = baseline_day['time']
                    baseline_day.columns = ['time',baseline_ref.strftime('%Y-%m-%d')]
                    baseline = pd.concat([baseline, baseline_day[baseline_ref.strftime('%Y-%m-%d')]],axis=1)
                    #baseline.index = data_wk[baseline_ref.strftime('%Y-%m-%d')]['time']
                    #baseline.append(baseline_day)
                elif baseline_indicator == 1:
                    pass
            else:
                print("10 baseline days")
                break
        else:
            continue
    
    baseline.to_csv('../results/Building182/Meter_Baseline_' + str(k) +'.csv')
    
    # Calculate the baseline days' load variability, standard deviation
    baseline['mean'] = baseline.mean(axis=1)
    baseline['std'] = baseline.std(axis=1)
    
    DR = data_wk[drevent.date[k].strftime('%Y-%m-%d')][['time','power']]
    DR.index = DR['time']
    DR['Virtual_DR'] = DR['power']
    DR['Virtual_DR']['12:15':'18:00'] = DR['Virtual_DR']['12:15':'18:00'] - kW_shed
    
    # Adjustment of baseline, from 10am to 1pm, no cap
    adj_factor = np.mean(DR['08:15':'11:00']['power'] / baseline['08:15':'11:00']['mean'])
    if adj_factor > 1.2:
        adj_factor = 1.2;
    elif adj_factor < -1.2:
        adj_factor = -1.2;
    
    # Create the ADR event day performance and two baselines
    DR_Event['baseline'] = baseline['mean']
    DR_Event['Adj_baseline'] = baseline['mean'] * adj_factor
    DR_Event['ADR'] = DR['Virtual_DR']
    DR_Event['kW_Shed'] = baseline['mean'] * adj_factor - DR['Virtual_DR']
    DR_Event['kWShed_Per'] = (baseline['mean'] * adj_factor - DR['Virtual_DR']) / (baseline['mean'] * adj_factor)
    DR_Event['var'] = baseline['std'] / baseline['mean']
    
    Min_kWShed = min(DR_Event['12:15':'18:00']['kW_Shed'])
    Max_kWshed = max(DR_Event['12:15':'18:00']['kW_Shed'])
    Ave_kWShed = np.mean(DR_Event['12:15':'18:00']['kW_Shed'])
    Min_kWShed_Per = min(DR_Event['12:15':'18:00']['kWShed_Per'])
    Max_kWShed_Per = max(DR_Event['12:15':'18:00']['kWShed_Per'])
    Ave_kWShed_Per = np.mean(DR_Event['12:15':'18:00']['kWShed_Per'])
    Ave_var = np.mean(DR_Event['09:15':'18:00']['var'])
    Ave_std = np.mean(baseline['12:15':'18:00']['mean']) * np.mean(DR_Event['12:15':'18:00']['var'])
    Ave_err = np.mean(abs(DR_Event['12:15':'18:00']['Adj_baseline'] - DR['12:15':'18:00']['power']))
    
    ADR_Summary['ADR'+ str(k)] =  [Min_kWShed,Max_kWshed,Ave_kWShed,Min_kWShed_Per,Max_kWShed_Per,Ave_kWShed_Per,Ave_var,Ave_std, Ave_err]
    #concat(ADR_Summary, DataFrame([Min_kWShed,Max_kWshed,Ave_kWShed,Min_kWShed_Per,Max_kWShed_Per,Ave_kWShed_Per],columns=['ADR'+str(k)]),axis=1)
    
    DR_Event.to_csv('../results/Building182/Test_ADR' + str(k) +'.csv')
    
    # Plot DR and average baseline with std
    fig, ax = plt.subplots(figsize=(10,6))
    
    ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), DR['power'],'g-', label='ADR Event Day')
#     ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), DR['Virtual_DR'],'r-', label='Virtual ADR Event Day')
    #ax.plot_date(DR1.index, DR1['power'],'b-')

    #ax.set_ylim(0,600)
    ax.set_xlabel('Time')
    ax.set_ylabel('Power Demand, kW')
    ax.set_title('ADR Performance of Meter Customers on ' + drevent.date[k].strftime('%Y-%m-%d'))
    ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), baseline['mean'],'b-', label='10/10 Baseline')
    ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), baseline['mean'] * adj_factor,'b--', label='Adjusted 10/10 Baseline')
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
    
    ax.axvspan(*mdates.datestr2num(['1900-01-01 12:00', '1900-01-01 18:00']), color='green', alpha=0.2)
    #ax.axvspan('15:00', '16:00', color='red', alpha=0.5)
    ax.fill_between(pd.to_datetime(DR.index, format='%H:%M'), baseline['mean']-baseline['std'], baseline['mean']+baseline['std'], color='b', alpha=0.2)
    
    ax.legend(loc='upper left', shadow=False, fontsize=12)
    ax.grid(True)
    
    plt.savefig('../results/Building182/ADR Performance_' + drevent.date[k].strftime('%Y-%m-%d') + '.png', 
                dpi=300, bbox_inches='tight')
                
ADR_Summary.to_csv('../results/Building182/Test_ADR_Summary' +'.csv')

# Plot kW shed distribution
# Per_Expected = ADR_Summary.loc[2,:] / kW_shed
# Var = ADR_Summary.loc[3,:]

# plt.hist(Per_Expected, bins=10,normed=1)

# data_sorted = np.sort(Per_Expected)
# p = 1. * arange(len(Per_Expected)) / (len(Per_Expected)-1)
# fig, ax = plt.subplots(figsize=(10,6))

# ax.plot(data_sorted, p)
# plt.show()

# fig, ax = plt.subplots(figsize=(10,6))
# ax.set_xlabel('Number of ADR Event Days')
# ax.set_ylabel('Per Expected')
# plt.bar(np.arange(len(Per_Expected)),Per_Expected,facecolor='blue', alpha=0.5)
# plt.savefig(path +'TRU Meter4/ADR Performance_Summary' + '.png', dpi=400, bbox_inches='tight')

# fig, ax = plt.subplots(figsize=(10,6))
# width = 0.35
# ax.set_xlabel('Number of ADR Event Days')
# ax.set_ylabel('kW Shed')
# plt.bar(np.arange(len(Per_Expected)),ADR_Summary.loc[2,:],width,facecolor='blue', alpha=0.5)
# plt.bar(np.arange(len(Per_Expected))+width,ADR_Summary.loc[7,:],width,facecolor='green', alpha=0.5)
# ax.legend(('Measured kW Shed','One Standard Deviation'), loc='upper left', shadow=False)

# l = plt.axhline(y=23,linewidth=2, color='r')
# ax.annotate('Approved kW Shed', xy=(2, 32))
# plt.savefig(path +'TRU Meter4/ADR Performance_kW_Summary' + '.png', dpi=400, bbox_inches='tight')


10 baseline days
10 baseline days
10 baseline days
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/kernel/__main__.py:78: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [36]:
# Read the holiday and DR event days 
drevent = pd.read_csv('DR_Events_2015.csv', usecols=[3])
drevent['date'] = pd.to_datetime(pd.Series(drevent['date']), format='%m/%d/%y')
drevent['day'] = drevent.date.apply(lambda x: x.strftime('%Y-%m-%d'))

# offdays = pd.DataFrame.append(holidays, drevent)

# Sort DR event days' power use
data = df.copy()

data['year'] = data.date.apply(lambda x: x.strftime('%Y'))
data['time'] = data.date.apply(lambda x: x.strftime('%H:%M'))
data['month'] = data.date.apply(lambda x: x.strftime('%m'))
data['day'] = data.date.apply(lambda x: x.strftime('%d'))
data['hour'] = data.date.apply(lambda x: x.strftime('%H'))
data['minute'] = data.date.apply(lambda x: x.strftime('%M'))
data['weekday'] = data.date.apply(lambda x: x.strftime('%w'))
data['weekday'] = data.weekday.astype(int)
data['DR'] = data.date.apply(lambda x: x.strftime('%Y-%m-%d') in drevent.day.values)
data['DR'] = data.DR.astype(int)
data['holiday'] = data.date.apply(lambda x: x.strftime('%Y-%m-%d') in holidays.day.values)
data['holiday'] = data.holiday.astype(int)

# weekdays for each baseline
data_wk = data[(data['weekday'] > 0) & (data['holiday']==0) & (data['weekday'] < 6)]
data_wk.to_csv('../results/Building182/data_wk.csv')

baseline_day = []
baseline = pd.DataFrame()

# kW shed
kW_shed = 5

# plot all ADR performance figures
ADR_Summary = pd.DataFrame()
# plot all ADR performance figures
i, j, k = 0, 1, 0
for k in range(4):
    
    j = 1
    baseline_day = []
    DayAverage = []
    baseline = pd.DataFrame()
    DR_Event = pd.DataFrame()
        
    for i in range(20):
        baseline_ref = drevent.date[k] - (i+1) * Day()
        if baseline_ref in data_wk.index:
            
            baseline_indicator = data_wk[baseline_ref.strftime('%Y-%m-%d')]['DR'][0]
            baseline_day_indicator = data_wk[baseline_ref.strftime('%Y-%m-%d')]['date'][0]
            if j <= 10:
                if baseline_indicator == 0:
                    j+=1
                    baseline_day = data_wk[baseline_ref.strftime('%Y-%m-%d')][['time','hvac']]
                    baseline_day.index = baseline_day['time']
                    baseline_day.columns = ['time',baseline_ref.strftime('%Y-%m-%d')]
                    baseline = pd.concat([baseline, baseline_day[baseline_ref.strftime('%Y-%m-%d')]],axis=1)
                    #baseline.index = data_wk[baseline_ref.strftime('%Y-%m-%d')]['time']
                    #baseline.append(baseline_day)
                elif baseline_indicator == 1:
                    pass
            else:
                print("10 baseline days")
                break
        else:
            continue
    
    baseline.to_csv('../results/Building182/Meter_Baseline_HVAC' + str(k) +'.csv')
    
    # Calculate the baseline days' load variability, standard deviation
    baseline['mean'] = baseline.mean(axis=1)
    baseline['std'] = baseline.std(axis=1)
    
    DR = data_wk[drevent.date[k].strftime('%Y-%m-%d')][['time','hvac']]
    DR.index = DR['time']
#     DR['Virtual_DR'] = DR['hvac']
#     DR['Virtual_DR']['12:15':'18:00'] = DR['Virtual_DR']['12:15':'18:00'] * 0.6
    
    # Adjustment of baseline, from 10am to 1pm, no cap
    adj_factor = np.mean(DR['08:15':'11:00']['hvac'] / baseline['08:15':'11:00']['mean'])
    if adj_factor > 1.2:
        adj_factor = 1.2;
    elif adj_factor < -1.2:
        adj_factor = -1.2;
    
    # Create the ADR event day performance and two baselines
    DR_Event['baseline'] = baseline['mean']
    DR_Event['Adj_baseline'] = baseline['mean'] * adj_factor
    DR_Event['ADR'] = DR['hvac']
    DR_Event['kW_Shed'] = baseline['mean'] * adj_factor - DR['hvac']
    DR_Event['kWShed_Per'] = (baseline['mean'] * adj_factor - DR['hvac']) / (baseline['mean'] * adj_factor)
    DR_Event['var'] = baseline['std'] / baseline['mean']
    
    Min_kWShed = min(DR_Event['12:15':'18:00']['kW_Shed'])
    Max_kWshed = max(DR_Event['12:15':'18:00']['kW_Shed'])
    Ave_kWShed = np.mean(DR_Event['12:15':'18:00']['kW_Shed'])
    Min_kWShed_Per = min(DR_Event['12:15':'18:00']['kWShed_Per'])
    Max_kWShed_Per = max(DR_Event['12:15':'18:00']['kWShed_Per'])
    Ave_kWShed_Per = np.mean(DR_Event['12:15':'18:00']['kWShed_Per'])
    Ave_var = np.mean(DR_Event['09:15':'18:00']['var'])
    Ave_std = np.mean(baseline['12:15':'18:00']['mean']) * np.mean(DR_Event['12:15':'18:00']['var'])
    Ave_err = np.mean(abs(DR_Event['12:15':'18:00']['Adj_baseline'] - DR['12:15':'18:00']['hvac']))
    
    DR['Virtual_DR'] = DR_Event['Adj_baseline']
    DR['Virtual_DR']['12:15':'18:00'] = DR['Virtual_DR']['12:15':'18:00'] * 0.6
    
    ADR_Summary['ADR'+ str(k)] =  [Min_kWShed,Max_kWshed,Ave_kWShed,Min_kWShed_Per,Max_kWShed_Per,Ave_kWShed_Per,Ave_var,Ave_std, Ave_err]
    #concat(ADR_Summary, DataFrame([Min_kWShed,Max_kWshed,Ave_kWShed,Min_kWShed_Per,Max_kWShed_Per,Ave_kWShed_Per],columns=['ADR'+str(k)]),axis=1)
    
    DR_Event.to_csv('../results/Building182/Test_ADR' + str(k) +'.csv')
    
    # Plot DR and average baseline with std
    fig, ax = plt.subplots(figsize=(10,6))
    
    ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), DR['hvac'],'g-', label='ADR Event Day')
    ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), DR['Virtual_DR'],'r-', label='Predicted ADR Performance')
    #ax.plot_date(DR1.index, DR1['hvac'],'b-')

    #ax.set_ylim(0,600)
    ax.set_xlabel('Time')
    ax.set_ylabel('HVAC Demand, kW')
    ax.set_title('ADR Performance of Meter Customers on ' + drevent.date[k].strftime('%Y-%m-%d'))
    ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), baseline['mean'],'b-', label='10/10 Baseline')
    ax.plot_date(pd.to_datetime(DR.index, format='%H:%M'), baseline['mean'] * adj_factor,'b--', label='Adjusted 10/10 Baseline')
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
    
    ax.axvspan(*mdates.datestr2num(['1900-01-01 12:00', '1900-01-01 18:00']), color='green', alpha=0.2)
    #ax.axvspan('15:00', '16:00', color='red', alpha=0.5)
    ax.fill_between(pd.to_datetime(DR.index, format='%H:%M'), baseline['mean']-baseline['std'], baseline['mean']+baseline['std'], color='b', alpha=0.2)
    
    ax.legend(loc='upper left', shadow=False, fontsize=12)
    ax.grid(True)
    
    plt.savefig('../results/Building182/ADR Performance_HVAC' + drevent.date[k].strftime('%Y-%m-%d') + '.png', 
                dpi=300, bbox_inches='tight')
                
ADR_Summary.to_csv('../results/Building182/Test_ADR_Summary_HVAC' +'.csv')

# Plot kW shed distribution
# Per_Expected = ADR_Summary.loc[2,:] / kW_shed
# Var = ADR_Summary.loc[3,:]

# plt.hist(Per_Expected, bins=10,normed=1)

# data_sorted = np.sort(Per_Expected)
# p = 1. * arange(len(Per_Expected)) / (len(Per_Expected)-1)
# fig, ax = plt.subplots(figsize=(10,6))

# ax.plot(data_sorted, p)
# plt.show()

# fig, ax = plt.subplots(figsize=(10,6))
# ax.set_xlabel('Number of ADR Event Days')
# ax.set_ylabel('Per Expected')
# plt.bar(np.arange(len(Per_Expected)),Per_Expected,facecolor='blue', alpha=0.5)
# plt.savefig(path +'TRU Meter4/ADR Performance_Summary' + '.png', dpi=400, bbox_inches='tight')

# fig, ax = plt.subplots(figsize=(10,6))
# width = 0.35
# ax.set_xlabel('Number of ADR Event Days')
# ax.set_ylabel('kW Shed')
# plt.bar(np.arange(len(Per_Expected)),ADR_Summary.loc[2,:],width,facecolor='blue', alpha=0.5)
# plt.bar(np.arange(len(Per_Expected))+width,ADR_Summary.loc[7,:],width,facecolor='green', alpha=0.5)
# ax.legend(('Measured kW Shed','One Standard Deviation'), loc='upper left', shadow=False)

# l = plt.axhline(y=23,linewidth=2, color='r')
# ax.annotate('Approved kW Shed', xy=(2, 32))
# plt.savefig(path +'TRU Meter4/ADR Performance_kW_Summary' + '.png', dpi=400, bbox_inches='tight')


10 baseline days
10 baseline days
10 baseline days
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/kernel/__main__.py:106: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In [ ]: