In [1]:
from datetime import datetime
import json
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter, DayLocator, HourLocator
%matplotlib inline
import urllib2
from lxml import etree
from bs4 import BeautifulSoup
import time
from calendar import monthrange
import numpy as np
import copy
import statsmodels.api as sm
from scipy import stats
import pickle
import seaborn as sns
sns.set(context="paper", font="monospace")
In [2]:
with open('../data/df_gen.pkl','rb') as f:
df_orig = pickle.load(f)
In [3]:
#1-Hour Ramp
df = copy.deepcopy(df_orig)
In [4]:
df['wind_diff'] = df['wind'].diff(1)
df['hydro_diff'] = df['hydro'].diff(1)
df['bunker_diff'] = df['bunker'].diff(1)
df['biomass_diff'] = df['biomass'].diff(1)
df['interconnect_diff'] = df['interconnect'].diff(1)
df['Demanda_diff'] = df['Demanda'].diff(1)
df['Demanda_diff_12'] = df['Demanda'].diff(12)
df['Demandnowind'] = df['Demanda'] - df['wind']+df['interconnect']
df['Dnowinddiff'] = df['Demandnowind'].diff(1)
df['Dnowinddiff_12'] = df['Demandnowind'].diff(12)
In [5]:
start_date = datetime(2013,1,1)
end_date = datetime(2013,1,5)
plt.plot(df[start_date:end_date]['Demandnowind'])
plt.plot(df[start_date:end_date]['Demanda'],'k')
Out[5]:
In [6]:
df_filter = df[df['Dnowinddiff_12']<0][df['Dnowinddiff']>0][df['hydro_diff']>0]
In [7]:
hist = plt.hist(df_filter.index.hour)
plt.title('Filtered Values by Hour')
plt.xlabel('Hour of Day')
plt.ylabel('#')
Out[7]:
In [8]:
start_date_cutout = datetime(2013,1,1,14)
end_date_cutout = datetime(2013,1,2,14)
df_plot = df[start_date_cutout:end_date_cutout]
plt.plot(df_filter[start_date_cutout:end_date_cutout].index,df_filter[start_date_cutout:end_date_cutout]['Demanda']/max(df['Demanda']),'.')
plt.plot(df_plot.index,df_plot['Demanda']/max(df['Demanda']))
plt.plot(df_plot.index,df_plot['wind_diff']/max(abs(df_plot['wind_diff'])))
plt.plot(df_plot.index,df_plot['hydro_diff']/max(abs(df_plot['hydro_diff'])),'b')
plt.plot(df_plot.index,df_plot['bunker_diff']/max(abs(df_plot['bunker_diff'])),'k')
hydro_diff_mean = np.mean(df_filter[start_date_cutout:end_date_cutout]['hydro_diff'])
bunker_diff_mean = np.mean(df_filter[start_date_cutout:end_date_cutout]['bunker_diff'])
dnowind_diff_mean = np.mean(df_filter[start_date_cutout:end_date_cutout]['Dnowinddiff'])
print hydro_diff_mean/dnowind_diff_mean
print bunker_diff_mean/dnowind_diff_mean
In [9]:
with open('../data/df_temp.pkl','rb') as f:
df_weather = pickle.load(f)
In [10]:
df_hydro_contrib = copy.deepcopy(df_filter['hydro_diff'].resample('M',how='sum')/df_filter['Dnowinddiff'].resample('M',how='sum'))
df_bunker_contrib = copy.deepcopy(df_filter['bunker_diff'].resample('M',how='sum')/df_filter['Dnowinddiff'].resample('M',how='sum'))
df_wind_contrib = copy.deepcopy(df_filter['wind_diff'].resample('M',how='sum')/df_filter['Dnowinddiff'].resample('M',how='sum'))
#df_biomass = copy.deepcopy(df_filter['biomass']/max(df_filter['biomass'])).resample('M',how='sum')
df_rain = copy.deepcopy(df_weather['precipMM']).resample('M',how='sum')
avg_rainfall = np.array([4,1,5,6,76,296,134,130,182,243,59,6])
#df_rain = df_rain/max(df_rain)
#plt.plot(df_rain_contrib[start:end].index,df_rain_contrib[start:end])
#plt.plot(df_hydro_contrib[start:end].index,df_hydro_contrib[start:end])
#plt.plot(df_biomass_contrib[start:end].index,df_biomass_contrib[start:end])
#plt.plot(df_weather['precipMM'])
#precipitation - rain
#biomass
start = datetime(2012,1,1)
end = datetime(2012,12,31)
#plt.plot(df_rain[start:end],'k')
plt.plot(range(1,13),df_hydro_contrib[start:end],'b',label='Hydro 2012')
#plt.plot(df_biomass[start:end],'ko')
plt.plot(range(1,13),df_bunker_contrib[start:end],'r',label='Bunker 2012')
#plt.plot(df['hydro'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'k',label='2012')
#plt.plot(df['bunker'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'k--')
#plt.plot(df['biomass'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'ko')
plt.xlim([1,12])
start = datetime(2013,1,1)
end = datetime(2013,12,31)
#plt.plot(df_rain[start:end],'b')
plt.plot(range(1,13),df_hydro_contrib[start:end],'b:',label='Hydro 2013')
#plt.plot(df_biomass[start:end],'bo')
plt.plot(range(1,13),df_bunker_contrib[start:end],'r:',label='Bunker 2013')
#plt.plot(df['hydro'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'b',label='2013')
#plt.plot(df['bunker'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'b--')
#plt.plot(df['biomass'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'bo')
start = datetime(2014,1,1)
end = datetime(2014,12,31)
#plt.plot(df_rain[start:end],'r')
plt.plot(range(1,13),df_hydro_contrib[start:end],'b--',label='Hydro 2014')
#plt.plot(df_biomass[start:end],'ro')
plt.plot(range(1,13),df_bunker_contrib[start:end],'r--',label='Bunker 2014')
#plt.plot(df['hydro'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'r',label='2014')
#plt.plot(df['bunker'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'r--')
#plt.plot(df['biomass'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'ro')
#plt.plot(avg_rainfall,'g')
plt.ylabel('% Contribution')
plt.xlabel('Month of Year')
plt.title('% Contributions of Hydro and Bunker Fuel to 1-Hour Demand Ramps in Subset')
plt.legend(title="Years",bbox_to_anchor=(1.1, .9),
bbox_transform=plt.gcf().transFigure)
#t-tests
Out[10]:
In [573]:
start_2013 = datetime(2013,1,1)
end_2013 = datetime(2013,12,31)
start_2014 = datetime(2014,1,1)
end_2014 = datetime(2014,12,31)
stats.ttest_ind(np.array(df_hydro_contrib[start_2013:end_2013]),np.array(df_hydro_contrib[start_2014:end_2014]))
Out[573]:
In [537]:
start = datetime(2014,1,1)
end = datetime(2014,12,31)
plt.xlim([1,12])
#plt.plot(df_rain[start:end],'r')
plt.plot(range(1,13),df_hydro_contrib[start:end],'b--',label='Hydro 2014')
#plt.plot(df_biomass[start:end],'ro')
plt.plot(range(1,13),df_bunker_contrib[start:end],'r--',label='Bunker 2014')
#plt.plot(df['hydro'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'r',label='2014')
#plt.plot(df['bunker'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'r--')
#plt.plot(df['biomass'][start:end].resample('M')/df['Demanda'][start:end].resample('M'),'ro')
#plt.plot(avg_rainfall,'g')
plt.ylabel('% Contribution')
plt.xlabel('Month of Year')
plt.title('% Contributions of Hydro and Bunker Fuel to 1-Hour Demand Ramps in Subset')
plt.legend(title="Years",bbox_to_anchor=(1.1, .9),
bbox_transform=plt.gcf().transFigure)
Out[537]:
In [568]:
start_2013 = datetime(2013,6,1)
end_2013 = datetime(2013,11,1)
start_2014 = datetime(2014,6,1)
end_2014 = datetime(2014,11,1)
expect_2014 = np.array(df_bunker_contrib[start_2013:end_2013])*df_filter['Dnowinddiff'].resample('M',how='sum')[start_2014:end_2014]
real_2014 = df_filter['bunker_diff'].resample('M',how='sum')[start_2014:end_2014]
plt.plot(real_2014-expect_2014)
sum(real_2014-expect_2014)
Out[568]:
In [496]:
plt.plot(df_filter.resample('M').index,df_filter['Demanda_diff'].resample('M'),'k',label='Demand')
plt.plot(df_filter.resample('M').index,df_filter['bunker_diff'].resample('M'),'r',label='Bunker')
plt.plot(df_filter.resample('M').index,df_filter['hydro_diff'].resample('M'),'b',label='Hydro')
plt.plot(df_filter.resample('M').index,df_filter['wind_diff'].resample('M'),'g',label='Wind')
plt.plot(df_filter.resample('M').index,df_filter['biomass_diff'].resample('M'),'m',label='Biomass')
plt.plot(df_filter.resample('M').index,df_filter['interconnect_diff'].resample('M'),'k:',label='Interconnect')
plt.plot(df_filter.resample('M').index,df_filter['Dnowinddiff'].resample('M'),'k--',label='Dn')
plt.legend(title='1-Hour Ramps',bbox_to_anchor=(1.1, .9),
bbox_transform=plt.gcf().transFigure)
ticks = plt.xticks(rotation=45)
plt.title('1-Hour Ramps of Different Generation within subset')
Out[496]:
In [382]:
Out[382]:
In [146]:
df_rain_contrib[start:end]
Out[146]:
In [189]:
df['hydro.wind'] = df['hydro_diff']/df['Dnowinddiff']
df['bunker.wind'] = df['bunker_diff']/df['Dnowinddiff']
In [88]:
df_corr = copy.deepcopy(df.dropna())
#The Quantities Correlated
corr_df = pd.DataFrame(np.corrcoef(df_corr.T),columns=df_corr.keys()).T
corr_df.columns = df_corr.keys()
print corr_df
plt.figure()
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(corr_df.as_matrix(), linewidths=0, robust=True, square=False,xticklabels=df_corr.keys(),yticklabels=df_corr.keys())
Out[88]:
In [268]:
#df_filter = copy.deepcopy(df[df['wind'].diff(1)<0][df['hydro'].diff(1)>0])
#DiegoAlg
In [396]:
start_date = datetime(2013,1,1)
end_date = datetime(2013,12,31)
df_filter = copy.deepcopy(df_orig[start_date:end_date])
df_filter['wind_diff'] = df_filter['wind'].diff(1)
df_filter['hydro_diff'] = df_filter['hydro'].diff(1)
df_filter['demand_diff'] = df_filter['Demanda'].diff(1)
df_filter['demandnowind_diff'] = (df_filter['Demanda'] - df_filter['wind']).diff(1)
df_filter['hydro_contrib'] = df_filter['hydro_diff']/(df_filter['demandnowind_diff'])
df_filter = df_filter.dropna()
In [405]:
df_heatmap = df_filter[df_filter['demand_diff']>1][df_filter['wind_diff']<-1][df_filter['hydro_diff']>1]
heatmap_load = np.zeros((365,24))
for i,val in enumerate(df_heatmap.iterrows()):
heatmap_load[val[0].dayofyear-1][val[0].hour] = val[1]['hydro_contrib']
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(heatmap_load,linewidths=0, robust=True, square=False)
plt.title('Hydro Contribution to Demand Changes')
ax.invert_yaxis()
plt.yticks(np.arange(15,365,60), np.arange(1,len(np.arange(15,365,60))+1)*2)
f.tight_layout()
In [406]:
df_heatmap = df_filter[df_filter['demand_diff']>1][df_filter['wind_diff']>1][df_filter['hydro_diff']>1]
heatmap_load = np.zeros((365,24))
for i,val in enumerate(df_heatmap.iterrows()):
heatmap_load[val[0].dayofyear-1][val[0].hour] = val[1]['hydro_contrib']
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(heatmap_load,linewidths=0, robust=True, square=False)
plt.title('Hydro Contribution to Demand Changes')
ax.invert_yaxis()
plt.yticks(np.arange(15,365,60), np.arange(1,len(np.arange(15,365,60))+1)*2)
f.tight_layout()
In [407]:
df_heatmap = df_filter[df_filter['demand_diff']<-1][df_filter['wind_diff']<-1][df_filter['hydro_diff']>1]
heatmap_load = np.zeros((365,24))
for i,val in enumerate(df_heatmap.iterrows()):
heatmap_load[val[0].dayofyear-1][val[0].hour] = val[1]['hydro_contrib']
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(heatmap_load,linewidths=0, robust=True, square=False)
plt.title('Hydro Contribution to Demand Changes')
ax.invert_yaxis()
plt.yticks(np.arange(15,365,60), np.arange(1,len(np.arange(15,365,60))+1)*2)
f.tight_layout()
In [408]:
df_heatmap = df_filter[df_filter['demand_diff']<-1][df_filter['wind_diff']>1][df_filter['hydro_diff']>1]
heatmap_load = np.zeros((365,24))
for i,val in enumerate(df_heatmap.iterrows()):
heatmap_load[val[0].dayofyear-1][val[0].hour] = val[1]['hydro_contrib']
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(heatmap_load,linewidths=0, robust=True, square=False)
plt.title('Hydro Contribution to Demand Changes')
ax.invert_yaxis()
plt.yticks(np.arange(15,365,60), np.arange(1,len(np.arange(15,365,60))+1)*2)
f.tight_layout()
In [ ]:
In [ ]: