def plot_piecewise(measure_type, df_all, b, s): npar = 2 if measure_type == 'gas': df_reg = df_all.rename(columns={'Temperature_F': '{0}'.format(s), '{0}'.format(b): 'eui_gas', 'Timestamp': 'timestamp'}) ltm.piecewise_reg_one(b, s, npar, 'eui_gas', False, None, df_reg) elif measure_type == 'electric': df_reg = df_all.rename(columns={'Temperature_F': '{0}'.format(s), '{0}'.format(b): 'eui_elec', 'Timestamp': 'timestamp'}) ltm.piecewise_reg_one(b, s, npar, 'eui_elec', False, None, df_reg)
def compute_piecewise(measure_type, df_all, b, s): npar = 2 if measure_type == 'gas': df_reg = df_all.rename(columns={'Temperature_F': '{0}'.format(s), 'eui': 'eui_gas', 'Timestamp': 'timestamp'}) d = ltm.piecewise_reg_one(b, s, npar, 'eui_gas', False, None, df_reg) elif measure_type == 'electric': df_reg = df_all.rename(columns={'Temperature_F': '{0}'.format(s), 'eui': 'eui_elec', 'Timestamp': 'timestamp'}) d = ltm.piecewise_reg_one(b, s, npar, 'eui_elec', False, None, df_reg) return d
def cv(method, theme, kind): no_invest = gbs.get_no_invest_set() hdd_pair = get_pair('HDD') print hdd_pair[0] hdd_pair = [x for x in hdd_pair if x[0] in no_invest] print len(hdd_pair) n_par_temp = 2 lines = [] if theme == 'eui_elec': base_temp = '55F' else: base_temp = '65F' lines.append('Building Number,CVRMSE_{0}'.format(method)) for p in hdd_pair: b = p[0] s = p[1] df = pd.read_csv(weatherdir + 'dd_temp_eng/{0}_{1}_{2}.csv'.format(kind, b, s)) if len(df) < 36: continue df = df.tail(n=36) df_pairs = get_partitions(3, df) errs = [] for x in df_pairs: df_train = x[0] if df_train[theme].sum() == 0: continue df_test = x[1] y = np.array(df_test[theme]) if method == 'temperature': x = np.array(df_test[s]) d = ltm.piecewise_reg_one(b, s, n_par_temp, theme, None, df_train) y_hat = d['fun'](x, *(d['regression_par'])) elif method == 'dd': par_list = ld.opt_lireg(b, s, df_train, kind, theme, None) x = df_test[par_list[-1]] fun = lambda x: par_list[0] * x + par_list[1] y_hat = x.map(fun) elif method == 'vv': par_list = vv.lean(b, s, df_train, theme, base_temp) df_temp = df_test.copy() df_temp = df_temp[['month', base_temp]] df_perdd = par_list[0] df_perdd.reset_index(inplace=True) df_mg = pd.merge(df_test, df_perdd, left_on='month', right_index=True) y = df_mg[theme] y_hat = df_mg[base_temp] * df_mg['y_per_dd'] + par_list[-1] cvrmse = util.CVRMSE(y, y_hat, n_par_temp) print cvrmse errs.append(cvrmse) line = '{0},{1}'.format(b, (np.array(errs)).mean()) print line lines.append(line) with open(weatherdir + 'cv_{0}_{1}.csv'.format(method, theme), 'w+') as wt: wt.write('\n'.join(lines)) return
def load_data_ion(b, s, npar, theme, timestep, timerange): if timestep == 'D': df_e = pd.read_csv(interval_daily + '{0}_{1}_D.csv'.format(b, theme)) elif timestep == 'H': df_e = pd.read_csv(interval_dir + 'single_hourly/{0}.csv'.format(b)) df_e.info() dates = pd.to_datetime(df_e['Date']) dayOfWeek = pd.DatetimeIndex(dates).dayofweek days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] fun = lambda x: days[x] fun = np.vectorize(fun) dayOfWeek_str = fun(dayOfWeek) if timestep == 'D': df_e.set_index(dates, inplace=True) elif timestep == 'H': time_str = df_e['Timestamp'].map(lambda x: x[:18]) time_idx = pd.to_datetime(time_str) df_e.set_index(time_idx, inplace=True) print len(df_e) # fixme, timerange # df_e = df_e[df_e['Date'] < np.datetime64('2013-09-01')] print len(df_e) minDate = dates.min() maxDate = dates.max() print minDate, maxDate minDate_str = minDate.strftime('%Y-%m-%d %H:%M:%S') maxDate_str = maxDate.strftime('%Y-%m-%d %H:%M:%S') print minDate_str, maxDate_str df_w = ltm.get_weather_data(s, minDate_str, maxDate_str, timestep) df = pd.merge(df_e, df_w, left_index=True, right_index=True, how='left') df['day'] = dayOfWeek_str df['hour'] = df['Timestamp'].map(lambda x: int(x[11:13])) df['year'] = df['Timestamp'].map(lambda x: int(x[:4])) df['month'] = df['Timestamp'].map(lambda x: int(x[5:7])) df_plot = df df_plot = df_plot[df_plot[theme + ' (kBtu)'] >= 0] if timestep == 'H': sns.lmplot(x=s, y='{0} (kBtu)'.format(theme), data=df_plot, fit_reg=False) plt.ylim((0, 4000)) # plt.gca().set_ylim(bottom=0) # plt.show() image_output_dir = os.getcwd() + '/plot_FY_weather/html/single_building/lean_interval/' P.savefig('{0}scatter_{1}_{2}_{3}_{4}_ori.png'.format(image_output_dir, b, s, theme, timestep), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) plt.close() sns.lmplot(x=s, y='{0} (kBtu)'.format(theme), hue='day', col='hour', col_wrap=6, size=3, data=df_plot, fit_reg=False) plt.ylim((0, 4000)) # plt.gca().set_ylim(bottom=0) P.savefig('{0}scatter_{1}_{2}_{3}_{4}.png'.format(image_output_dir, b, s, theme, timestep), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) plt.close() # for year in [2014, 2015]: # df_plot = df[(df['day'] == 'Tue') & (df['year'] == year)] # sns.lmplot(x=s, y='{0} (kBtu)'.format(theme), hue='month', # col='hour', col_wrap=6, size=3, data=df_plot, fit_reg=False) # plt.gca().set_ylim(bottom=0) # P.savefig('{0}scatter_{1}_{2}_{3}_{4}_{5}_Tue.png'.format(image_output_dir, b, s, theme, timestep, year), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) # plt.close() if theme == 'Gas': df_reg = df.rename(columns={'{0} (kBtu)'.format(theme): 'eui_gas', 'Date': 'timestamp'}) df_reg['day'] = dayOfWeek_str ltm.piecewise_reg_one(b, s, npar, 'eui_gas', False, timerange, df_reg) elif theme == 'Electric': df_reg = df.rename(columns={'{0} (kBtu)'.format(theme): 'eui_elec', 'Date': 'timestamp'}) df_reg['day'] = dayOfWeek_str df_reg = df_reg[df_reg['day'] != 'Sat'] df_reg = df_reg[df_reg['day'] != 'Sun'] ltm.piecewise_reg_one(b, s, npar, 'eui_elec', False, timerange, df_reg) # # plt.show() return