<jupyter_start><jupyter_text>Metrics<jupyter_code>data24 = data24.assign(COP='COP') 
#data24['COP'] = data24['oat_C'].apply(lambda x: (0.548 + 0.0951*x + (-7.63E-04)*x**2))
data24['COP'] = 2.9

data25 = data25.assign(COP='COP') 
#data25['COP'] = data25['oat_C'].apply(lambda x: (0.548 + 0.0951*x + (-7.63E-04)*x**2))
data25['COP'] = 2.9
data24 = data24.assign(chiller = 'Chiller_load') 
data24['Chiller_load'] = (data24['1A-Th-Chilled Water']/data24['COP'])*-1
data24['cooling_power [W]'] = data24['1A-El-AHU'] + data24['1A-El-Exhaust Fan'] + data24['1A-El-CHWP'] + data24['Chiller_load']

data25 = data25.assign(chiller = 'Chiller_load') 
data25['Chiller_load'] = (data25['1B-Th-Chilled Water']/data25['COP'])*-1
data25['cooling_power [W]'] = data25['1B-El-AHU'] + data25['1B-El-Exhaust Fan'] + data25['1B-El-CHWP'] + data25['Chiller_load']
en_1a = []
en_1b = []
# define dates for the calculation
for day in range(15, 20):    
    mask_1 = data24['date'] == datetime.date(2021, 9, day)
    print(day)
    day_en_1a = round((data24['cooling_power [W]'][mask_1].sum()/4000),2)
    en_1a.append(day_en_1a)
    
    day_en_1b = round((data25['cooling_power [W]'][mask_1].sum()/4000),2)
    #print(day_en_1b)
    en_1b.append(day_en_1b)
test_day_mask = (((data25['date'] >= datetime.date(2021, 9, 15)) & (data25['date'] <= datetime.date(2021, 9, 19))))
data24 = data24[test_day_mask]
data25 = data25[test_day_mask]
db_plotting.energy_plot(data25, en_1a, en_1b, 'Daily Energy consumption (kWh)', 'Energy (kWh)')
print(en_1b)<jupyter_output>[20.56, 17.47, 17.8, 17.65, 20.46]<jupyter_text>Shed<jupyter_code>data25['G36 Demand Shed'] = data25['cooling_power [W]'] - data24['cooling_power [W]']

shed_1a = []
shed_1b = []
shed_oat = []
# define dates for the calculation
for day in range(15,20):    
    mask_1 = data24['date'] == datetime.date(2021, 9, day)
    #print(day) 
    day_shed_1a = (data24['cooling_power [W]'][mask_1][gta_ts].mean())
    shed_1a.append(day_shed_1a)
    
    day_shed_1b = (data25['cooling_power [W]'][mask_1][gta_ts].mean())
    shed_1b.append(day_shed_1b)
    
    shed_oat_mean=(data24['1B-OAT-1'][mask_1][gta_ts].mean())
    
    shed_oat.append(shed_oat_mean)


print(shed_1b)
db_plotting.shed_demand_plot(data25, shed_1a, shed_1b, 'Demand during GTA period', 'Demand (W)')<jupyter_output>[839.1100297035274, 771.1036852812821, 831.7791331815943, 781.6152274588288, 949.6061529028864]
['09/15', '09/16', '09/17', '09/18', '09/19']
[839.1100297035274, 771.1036852812821, 831.7791331815943, 781.6152274588288, 949.6061529028864]<jupyter_text>SHIFT<jupyter_code>data25['G36 Demand shift'] = data25['cooling_power [W]'] - data24['cooling_power [W]']

precool_1a = []
precool_1b = []
shift_oat = []
# define dates for the calculation
for day in range(15,20):    
    mask_1 = data25['date'] == datetime.date(2021, 9, day)
    #print(mask_1) 
        
    day_precool_1a = (data24['cooling_power [W]'][mask_1][precool_ts].mean())
    precool_1a.append(day_precool_1a)
    print(day_precool_1a)
    day_precool_1b = (data25['cooling_power [W]'][mask_1][precool_ts].mean())
    precool_1b.append(day_precool_1b)
    
    
    shift_oat_mean=(data24['1B-OAT-1'][mask_1][precool_ts].mean())
    
    shift_oat.append(shift_oat_mean)
    
    
print(shed_1b)
db_plotting.precool_demand_plot(data25, precool_1a, precool_1b, 'Demand during Precool period', 'Demand (W)')<jupyter_output>[839.1100297035274, 771.1036852812821, 831.7791331815943, 781.6152274588288, 949.6061529028864]
['09/15', '09/16', '09/17', '09/18', '09/19']
[954.5014486309403, 777.7929619084522, 824.3420084157042, 779.6619195955436, 1165.3663035040427]<jupyter_text>Thermal comfort<jupyter_code>fig, (ax1, ax) = plt.subplots(nrows = 2, ncols = 1, constrained_layout=True, facecolor='w', edgecolor='k',figsize = (16,9))
fig.subplots_adjust(hspace = .4, wspace=.15)

ax1.plot(data25['1B-VAV3-Zn_Temp'], label = 'South zone', color = 'skyblue')
ax1.plot(data25['1B-VAV2-Zn_Temp'], label = 'Core zone', color = 'red')
ax1.plot(data25['1B-VAV1-Zn_Temp'], label = 'North zone', color = 'green')
ax1.plot(data25['1B-VAV3-OccupiedCoolingSetpt'], color = 'grey', linewidth= 1, linestyle = 'dashed')
ax1.plot(data25['1B-VAV3-OccupiedHeatingSetpt'], color = 'grey', linewidth= 1, linestyle = 'dashed')

ax.plot(data24['1A-VAV3-Zn_Temp'], label = 'South zone', color = 'skyblue')
ax.plot(data24['1A-VAV2-Zn_Temp'], label = 'Core zone', color = 'red')
ax.plot(data24['1A-VAV1-Zn_Temp'], label = 'North zone', color = 'green')
ax.plot(data24['1A-VAV3-OccupiedCoolingSetpt'], color = 'grey', linewidth= 1, linestyle = 'dashed')
ax.plot(data24['1A-VAV3-OccupiedHeatingSetpt'], color = 'grey', linewidth= 1, linestyle = 'dashed')

ax1.set_ylabel('Zone Air Temperature (deg F)', fontsize = 12)
ax.set_ylabel('Zone Air Temperature (deg F)', fontsize = 12)

ax1.set_title('Cell B with PCM: Zone Air Temperature', fontsize = 18)
ax.set_title('Cell A: Zone Air Temperature', fontsize = 18)

ax.legend(fontsize = 9, loc = 4)
ax1.legend(fontsize = 9, loc = 4)
data24 = data24.assign(nor_zat_deg = 'nor_zat_deg') 
#for i in range(len(data20.index)):

def zat_col(df_zat, df_cool_sp):
    df_zat = df_zat.apply(lambda x: (x-32)/1.8)
    df_cool_sp = df_cool_sp.apply(lambda x: (x-32)/1.8)
    
    df_zat_deg = df_zat - df_cool_sp
    df_zat_deg = df_zat_deg.apply(lambda x:0 if x <= 0 else x)
    return (df_zat_deg)
data24['nor_zat_deg'] = zat_col(data24['1A-VAV1-Zn_Temp'], data24['1A-VAV1-OccupiedCoolingSetpt'])
data24['cor_zat_deg'] = zat_col(data24['1A-VAV3-Zn_Temp'], data24['1A-VAV2-OccupiedCoolingSetpt'])
data24['sou_zat_deg'] = zat_col(data24['1A-VAV3-Zn_Temp'], data24['1A-VAV3-OccupiedCoolingSetpt'])

data25['nor_zat_deg'] = zat_col(data25['1B-VAV1-Zn_Temp'], data25['1B-VAV1-OccupiedCoolingSetpt'])
data25['cor_zat_deg'] = zat_col(data25['1B-VAV2-Zn_Temp'], data25['1B-VAV2-OccupiedCoolingSetpt'])
data25['sou_zat_deg'] = zat_col(data25['1B-VAV3-Zn_Temp'], data25['1B-VAV3-OccupiedCoolingSetpt'])

#data22['nor_zat_deg'] = zat_col(data22['VAV: North Zone Room Air Temperature'], data22['Tcool_SP'])
#data22['cor_zat_deg'] = zat_col(data22['VAV: Core Zone Room Air Temperature'], data22['Tcool_SP'])
#data22['sou_zat_deg'] = zat_col(data22['VAV: South Zone Room Air Temperature'], data22['Tcool_SP'])

#data23['nor_zat_deg'] = zat_col(data23['VAV: North Zone Room Air Temperature'], data23['Tcool_SP'])
#data23['cor_zat_deg'] = zat_col(data23['VAV: Core Zone Room Air Temperature'], data23['Tcool_SP'])
#data23['sou_zat_deg'] = zat_col(data23['VAV: South Zone Room Air Temperature'], data23['Tcool_SP'])

#data24['nor_zat_deg'] = zat_col(data24['VAV: North Zone Room Air Temperature'], data24['Tcool_SP'])
#data24['cor_zat_deg'] = zat_col(data24['VAV: Core Zone Room Air Temperature'], data24['Tcool_SP'])
#data24['sou_zat_deg'] = zat_col(data24['VAV: South Zone Room Air Temperature'], data24['Tcool_SP'])
#print(data20['nor_zat_deg'], data23['nor_zat_deg'])
def calc_stats(df, df_temp):
   
    overshoot = []
    mean = []
    median = []
    std = []
    oc_st = datetime.time(7,0,0)
    oc_en = datetime.time(19,0,0)

    oc_time = ((df['time'] >= oc_st) & (df['time'] <= oc_en))

    #print(df_temp, df['Tcool_SP'])
        
    for day in range(15, 20):    
        mask_1 = data25['date'] == datetime.date(2021, 9, day)
        daily_mean = df_temp[mask_1][oc_time].mean()
        daily_median = df_temp[mask_1][oc_time].median()
        #print('Variance', df[df[i] > target[i]].var())
        daily_std = df_temp[mask_1][oc_time].std()
        
        mean.append(daily_mean)
        median.append(daily_median)
        std.append(daily_std)
        
    #for i in range(len(df.index)):
        #if (df['time'][i] <= oc_en) & (df['time'][i] >= oc_st):
        #print(df_temp[i], df['Tcool_SP'][i], df_temp[i] - df['Tcool_SP'][i])
        daily_shoot = sum(df_temp[mask_1][oc_time]*.25)
        counter = sum(df_temp[mask_1][oc_time]>0)
        overshoot.append(daily_shoot)
        
        #print(day)
    #print(mean, median, std)
    
    return(overshoot, mean, median, std)
print(data24.columns)
data24_nor_zat_deg, data24_nor_mean, data24_nor_median, data24_nor_std = calc_stats(data24, data24['nor_zat_deg'])
data24_cor_zat_deg, data24_cor_mean, data24_cor_median, data24_cor_std = calc_stats(data24, data24['cor_zat_deg'])
data24_sou_zat_deg, data24_sou_mean, data24_sou_median, data24_sou_std = calc_stats(data24, data24['sou_zat_deg'])

data25_nor_zat_deg, data25_nor_mean, data25_nor_median, data25_nor_std = calc_stats(data25, data25['nor_zat_deg'])
data25_cor_zat_deg, data25_cor_mean, data25_cor_median, data25_cor_std = calc_stats(data25, data25['cor_zat_deg'])
data25_sou_zat_deg, data25_sou_mean, data25_sou_median, data25_sou_std = calc_stats(data25, data25['sou_zat_deg'])

print(data24_nor_zat_deg, data24_nor_mean, data25_nor_median, data25_nor_std)
scenario_dict = {}
scenario_dict[1] = calc_stats(data24, data24['nor_zat_deg'])
scenario_dict[2] = calc_stats(data24, data24['cor_zat_deg'])
scenario_dict[3] = calc_stats(data24, data24['sou_zat_deg'])

scenario_dict[4] = calc_stats(data25, data25['nor_zat_deg'])
scenario_dict[5] = calc_stats(data25, data25['cor_zat_deg'])
scenario_dict[6] = calc_stats(data25, data25['sou_zat_deg'])
scenario_dict = {'data24': {"nor": calc_stats(data24, data24['nor_zat_deg']),
                            "cor": calc_stats(data24, data24['cor_zat_deg']), 
                            "sou": calc_stats(data24, data24['sou_zat_deg'])}, 
                 
                 'data25': {"nor": calc_stats(data25, data25['nor_zat_deg']),
                            "cor": calc_stats(data25, data25['cor_zat_deg']), 
                            "sou": calc_stats(data25, data25['sou_zat_deg'])}}
print(scenario_dict['data25']['sou'])
print(scenario_dict['data25']['sou'][0][1:5])
print(sum(data24_nor_zat_deg), sum(data25_nor_zat_deg))
print(sum(data24_cor_zat_deg), sum(data25_cor_zat_deg))
print(sum(data24_sou_zat_deg), sum(data25_sou_zat_deg))
# key for metrics to be plotted in the scenario dictionary; {1:'overshoot', 2:'mean', 3:'median', 4:'std_dev'}
title_dict = {1: 'Daily overshoot (deg C hour)', 2:'Daily overshoot (mean)', 3:'Daily overshoot (median)' , 4:'Daily overshoot (standard deviation)'}
ylabel = {1: 'deg C - hour', 2: 'deg C', 3: 'deg C', 4: 'deg C'}
db_plotting.thermal_comfort_plot(data24, scenario_dict['data24']['nor'], scenario_dict['data25']['nor'], 
                                 scenario_dict['data24']['cor'], scenario_dict['data25']['cor'], 
                                 scenario_dict['data24']['sou'], scenario_dict['data25']['sou'],
                                 scenario_dict, 0, ylabel[1], title_dict[1])
def demand_plot(data, list_1, list_2, list_3, list_4, title, y_label):
    fig, (ax) = plt.subplots(nrows = 1, ncols = 1, constrained_layout=True, facecolor='w', edgecolor='k',figsize = (18,9))
    fig.subplots_adjust(hspace = .2, wspace=.15)

    inp_day = data['date'].unique() # capture unique dates from the date column
    inp_day = inp_day.tolist() #convert to a list
    #print(inp_day)
    # create labels for each date of the test
    labels = []
    labels1 = []
    for i in range(0,len(inp_day)):
        labels.append(inp_day[i].strftime('%m/%d'))

    for i in range(0,len(inp_day)-1):
        labels1.append(inp_day[i].strftime('%m/%d'))

    x = np.arange(len(labels))
    print(labels)
    print(list_2)
    width = .2

    ax.bar(x-width*.5, list_1, width = width, label='Shed Baseline', color = 'darkviolet')
    ax.bar(x-width*.5, np.array(list_3)*-1, width = width, label='Precool Baseline', color = 'thistle')
    ax.bar(x+width*.5, list_2, width = width, label='Shed Test', color = 'orange')
    ax.bar(x+width*.5, np.array(list_4)*-1, width = width, label='Precool Test', color = 'khaki')
    ax.axhline(linewidth=1, color='black')

    ax.set_xticks(range(len(labels)))
    ax.set_xticklabels(labels[0:len(labels)], fontsize = 12)
    ax.set_title(title, fontsize = 18)
    ax.set_ylabel(y_label, fontsize = 12)
    ax.legend(fontsize = 9, loc = 2)
demand_plot(data25, shed_1a, shed_1b, precool_1a, precool_1b, 'Demand during GTA and Precool period', 'Demand (W)')
df_4 = pd.DataFrame([]) ##### enter list of metrics you want in this dataframe
df_4 = df_4.transpose()
df_4.columns=['demand_dec','demand_inc','net_energy', 'max_demand', 'dates', 'Test group', 'OAT','peak_OAT']





df_4['OAT']=pd.to_numeric(df_4['OAT'])
df_4['demand_dec']=pd.to_numeric(df_4['demand_dec'])
slope, intercept, r_value1, p_value1, std_err = stats.linregress(df_4['OAT'],
                                                               df_4['demand_dec'])

print(p_value, r_value)

sns.regplot(x="OAT", y="demand_dec", ci=None, 
            data=inp_df, marker="+",color="b", label='Field Test',
            line_kws={'label':"R^2={0:.3f}".format(r_value1*r_value1)},ax=ax2)







ax1.set_ylabel('Simulated DDI, [W/ft^2]')
ax1.set_xlabel('Avg. OAT during Event Hours ($^\circ$F)')
ax1.legend()
ax1.set_xlim(55,95)
ax1.set_ylim(0,0.8)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1766868346480815, 'fraction_numerical': 0.08730948916290113, 'mean_word_length': 4.9762013729977115, 'pattern_counts': {'":': 6, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '365258', 'license': 'MIT', 'n_tokens_mistral': 6096, 'n_tokens_neox': 5377, 'n_words': 841, 'path': 'flexlab/test_data_analysis/Flexlab_HIL_DA_metrics_shift_test.ipynb', 'repo_name': 'rongxinyin/HIL-Sim', 'size': '13057'}