def plot_json(jsondir, measure_type, region, season=None, subset=None):
    if season is None:
        files = glob.glob('{0}*_{1}.json'.format(jsondir, measure_type))
    else:
        files = glob.glob('{0}*_{1}_{2}.json'.format(jsondir, measure_type,
                                                     season))

    if measure_type == 'electric':
        good_set = gbs.get_energy_set('eui_elec')
    elif measure_type == 'gas':
        good_set = gbs.get_energy_set('gas')
    files = [f for f in files if len([x for x in good_set if x in f]) > 0]
    print len(files)
    if subset is not None:
        files = [f for f in files if len([x for x in subset if x in f]) > 0]
    # if season is None:
    #     files = glob.glob(os.getcwd() + \
    #                     '/input/FY/interval/ion_0627/{0}/json_{1}/*_{2}.json'.format(dirname, occtime, measure_type))
    # else:
    #     files = glob.glob(os.getcwd() + \
    #                       '/input/FY/interval/ion_0627/{0}/json_{1}/*_{2}_{3}.json'.format(dirname, occtime, measure_type, season))
    def get_name(string):
        idx = string.rfind('/')
        return string[idx + 1:idx + 9]

    data = []
    for x in files:
        with open(x, 'r') as rd:
            j = json.load(rd)
        data.append(j)
    data_str = 'series: [{0}]'.format(','.join(map(str, data)))
    data_str = data_str.replace('u\'', '\'')
    with open(
            os.getcwd() +
            '/input/FY/interval/ion_0627/piecewise_all/template.html',
            'r') as rd:
        lines = rd.readlines()
    if season is None:
        mytitle = 'Region {1} Monthly {0} (kBtu/sq.ft) vs Temperature (F)'.format(
            measure_type, region)
    else:
        mytitle = '{1} Monthly {0} (kBtu/sq.ft) vs Temperature (F)'.format(
            measure_type, season.title())
    for i in range(len(lines)):
        lines[i] = lines[i].replace('series: []', data_str)
        lines[i] = lines[i].replace('Mytitle', mytitle)
        lines[i] = lines[i].replace('Myylabel', 'kBtu/sq.ft')
    if season is None:
        f = os.getcwd(
        ) + '/plot_FY_weather/html/by_region/{}_piecewise_all_region_{}.html'.format(
            measure_type, region)
    if subset is None:
        f = os.getcwd(
        ) + '/plot_FY_weather/html/by_region/{}_piecewise_all_region_{}_no_filter.html'.format(
            measure_type, region)
    with open(f, 'w+') as wt:
        wt.write(''.join(lines))
    print 'end'
    return
Exemple #2
0
def euas_covered():
    covered = gbs.get_covered_set()
    euas = gbs.get_all_building_set()
    good_elec = gbs.get_energy_set('eui_elec')
    good_gas = gbs.get_energy_set('eui_gas')
    print len(covered.intersection(euas))
    print len(covered.intersection(good_elec))
    print len(covered.intersection(good_gas))
Exemple #3
0
def euas_covered():
    covered = gbs.get_covered_set()
    euas = gbs.get_all_building_set()
    good_elec = gbs.get_energy_set('eui_elec')
    good_gas = gbs.get_energy_set('eui_gas')
    print len(covered.intersection(euas))
    print len(covered.intersection(good_elec))
    print len(covered.intersection(good_gas))
Exemple #4
0
def table_for_robust_set():
    conn = uo.connect('all')
    study_set = gbs.get_energy_set('eui').intersection(gbs.get_cat_set(['A', 'I'], conn))
    df = pd.read_csv(os.getcwd() + '/plot_FY_weather/html/table/action_saving.csv')
    df = df[df['Building_Number'].isin(study_set)]
    df.sort('Building_Number', inplace=True)
    df.to_csv(os.getcwd() + '/plot_FY_weather/html/table/action_saving_robustset.csv', index=False)
    return
Exemple #5
0
def study_set_plot():
    conn = uo.connect('all')
    with conn:
        # df1 = pd.read_sql('SELECT DISTINCT Building_Number, Fiscal_Year FROM EUAS_monthly', conn)
        df1 = pd.read_sql(
            'SELECT DISTINCT Building_Number, Fiscal_Year FROM eui_by_fy',
            conn)
        df2 = pd.read_sql('SELECT Building_Number, Cat FROM EUAS_category',
                          conn)
    meter_set = gbs.get_action_set('high_level_ECM', ['Advanced Metering'])
    df = pd.merge(df1, df2, on='Building_Number', how='left')
    df = df[df['Fiscal_Year'] > 2006]
    df = df[df['Fiscal_Year'] < 2016]
    df3 = df.groupby('Building_Number').filter(lambda x: len(x) > 5)
    ai_set = gbs.get_cat_set(['A', 'I'], conn)
    # invest = gbs.get_invest_set()[-1]
    invest = gbs.get_ecm_set()
    all_building = set(df3['Building_Number'].tolist())
    print 'all building > 5 years of data: {0}'.format(len(all_building))
    print 'all building > 5 years of data + ecm: {0}'.format(
        len(all_building.intersection(invest)))
    print 'all building > 5 years of data + meter: {0}'.format(
        len(all_building.intersection(meter_set)))

    df4 = df[df['Cat'].isin(
        ['A', 'I'])].groupby('Building_Number').filter(lambda x: len(x) > 5)
    ai_building = set(df4['Building_Number'].tolist())
    print 'A + I building > 5 years of data: {0}'.format(len(ai_building))
    print 'A + I building > 5 years of data + ecm: {0}'.format(
        len(ai_building.intersection(invest)))
    print 'A + I building > 5 years of data + meter: {0}'.format(
        len(ai_building.intersection(meter_set)))

    print 'elec ', len(gbs.get_energy_set('elec').intersection(ai_set))
    print 'elec + ecm', len(
        gbs.get_energy_set('elec').intersection(ai_set).intersection(invest))
    print 'elec + meter', len(
        gbs.get_energy_set('elec').intersection(ai_set).intersection(
            meter_set))

    print 'gas ', len(gbs.get_energy_set('gas').intersection(ai_set))
    print 'gas + ecm', len(
        gbs.get_energy_set('gas').intersection(ai_set).intersection(invest))
    print 'gas + meter', len(
        gbs.get_energy_set('gas').intersection(ai_set).intersection(meter_set))
    print 'eui', len(gbs.get_energy_set('eui').intersection(ai_set))
    print 'eui + ecm', len(
        gbs.get_energy_set('eui').intersection(ai_set).intersection(invest))
    print 'eui + meter', len(
        gbs.get_energy_set('eui').intersection(ai_set).intersection(meter_set))
    return
Exemple #6
0
def write_robust_energy_set():
    s = gbs.get_energy_set('eui')
    e = gbs.get_energy_set('eui_elec')
    g = gbs.get_energy_set('eui_gas')
    conn = uo.connect('all')
    def get_status(x):
        if x in s:
            return "Electric EUI >= 12 and Gas EUI >= 3for at least 6 years from FY2007 to FY2015"
        elif x in e:
            return "Electric EUI >= 12 for at least 6 years from FY2007 to FY2015, there exist at least 4 years from FY2007 to FY2015 where Gas EUI < 3"
        elif x in g:
            return "Gas EUI >= 3 for at least 6 years from FY2007 to FY2015, there exist at least 4 years from FY2007 to FY2015 where Electric EUI < 12"
        else:
            return "There exist at least 4 years from FY2007 to FY2015 where Gas EUI < 3 and there exist at least 4 years from FY2007 to FY2015 where Electric EUI < 12"
    with conn:
        df = pd.read_sql('SELECT Building_Number, Cat FROM EUAS_category', conn)
    # df['status'] = df['Building_Number'].map(lambda x: "Electric EUI >= 12 and Gas EUI >= 3for at least 6 years from FY2007 to FY2015" if x in s else 'Not robust energy')
    df['status'] = df['Building_Number'].map(get_status)
    df.to_csv(r_input + 'robust_energy_sep.csv', index=False)
Exemple #7
0
def table_for_robust_set():
    conn = uo.connect('all')
    study_set = gbs.get_energy_set('eui').intersection(
        gbs.get_cat_set(['A', 'I'], conn))
    df = pd.read_csv(os.getcwd() +
                     '/plot_FY_weather/html/table/action_saving.csv')
    df = df[df['Building_Number'].isin(study_set)]
    df.sort('Building_Number', inplace=True)
    df.to_csv(os.getcwd() +
              '/plot_FY_weather/html/table/action_saving_robustset.csv',
              index=False)
    return
Exemple #8
0
def write_robust_energy_set():
    s = gbs.get_energy_set('eui')
    e = gbs.get_energy_set('eui_elec')
    g = gbs.get_energy_set('eui_gas')
    conn = uo.connect('all')

    def get_status(x):
        if x in s:
            return "Electric EUI >= 12 and Gas EUI >= 3for at least 6 years from FY2007 to FY2015"
        elif x in e:
            return "Electric EUI >= 12 for at least 6 years from FY2007 to FY2015, there exist at least 4 years from FY2007 to FY2015 where Gas EUI < 3"
        elif x in g:
            return "Gas EUI >= 3 for at least 6 years from FY2007 to FY2015, there exist at least 4 years from FY2007 to FY2015 where Electric EUI < 12"
        else:
            return "There exist at least 4 years from FY2007 to FY2015 where Gas EUI < 3 and there exist at least 4 years from FY2007 to FY2015 where Electric EUI < 12"

    with conn:
        df = pd.read_sql('SELECT Building_Number, Cat FROM EUAS_category',
                         conn)
    # df['status'] = df['Building_Number'].map(lambda x: "Electric EUI >= 12 and Gas EUI >= 3for at least 6 years from FY2007 to FY2015" if x in s else 'Not robust energy')
    df['status'] = df['Building_Number'].map(get_status)
    df.to_csv(r_input + 'robust_energy_sep.csv', index=False)
Exemple #9
0
def invest_cnt():
    conn = uo.connect('all')
    with conn:
        df = pd.read_sql(
            'SELECT DISTINCT Building_Number, high_level_ECM, detail_level_ECM FROM EUAS_ecm WHERE detail_level_ECM != \'GSALink\'',
            conn)
    eng_set = gbs.get_energy_set('eui')
    ai_set = gbs.get_cat_set(['A', 'I'], conn)
    study_set = eng_set.intersection(ai_set)
    df = df[df['Building_Number'].isin(study_set)]
    print df.groupby(['high_level_ECM', 'detail_level_ECM']).count()
    print len(df)
    df = df.groupby(['Building_Number']).filter(lambda x: len(x) == 1)
    print len(df)
    print df.head()
    print df.groupby(['high_level_ECM', 'detail_level_ECM']).count()
    return
Exemple #10
0
def count_invest():
    conn = uo.connect('all')
    with conn:
        df1 = pd.read_sql(
            'SELECT DISTINCT Building_Number, high_level_ECM FROM EUAS_ecm WHERE high_level_ECM != \'GSALink\'',
            conn)
        df2 = pd.read_sql(
            'SELECT DISTINCT Building_Number, ECM_program FROM EUAS_ecm_program',
            conn)
    eng_set = gbs.get_energy_set('eui')
    ai_set = gbs.get_cat_set(['A', 'I'], conn)
    study_set = eng_set.intersection(ai_set)
    df1.dropna(subset=['high_level_ECM'], inplace=True)
    df2.dropna(subset=['ECM_program'], inplace=True)
    df1 = df1[df1['Building_Number'].isin(study_set)]
    df2 = df2[df2['Building_Number'].isin(study_set)]
    print df1['high_level_ECM'].value_counts()
    print df2['ECM_program'].value_counts()
    return
def fit_time(measure_type, region, season=None):
    conn = uo.connect('all')
    with conn:
        df_bs = pd.read_sql(
            'SELECT Building_Number, ICAO, eui_elec, eui_gas, year, month, ave FROM EUAS_monthly_weather',
            conn)
        df_region = pd.read_sql(
            'SELECT DISTINCT Building_Number, [Region_No.] FROM EUAS_monthly',
            conn)
    if measure_type == 'electric':
        good_set = gbs.get_energy_set('eui_elec')
    elif measure_type == 'gas':
        good_set = gbs.get_energy_set('gas')
    df_bs = pd.merge(df_bs, df_region, on='Building_Number', how='left')
    df_bs = df_bs[df_bs['Building_Number'].map(lambda x: x in good_set)]
    df_bs = df_bs[df_bs['Region_No.'] == str(region)]
    df_bs = df_bs[df_bs['eui_elec'].notnull()]
    df_bs = df_bs[df_bs['eui_gas'].notnull()]
    df_bs.sort_values(by=['Building_Number', 'year', 'month'],
                      ascending=False,
                      inplace=True)
    bs_pair = list(set(zip(df_bs['Building_Number'], df_bs['ICAO'])))
    df_bs['Timestamp'] = df_bs.apply(
        lambda r: '{}-{}'.format(int(r['year']), int(r['month'])), axis=1)
    sns.set_style("whitegrid")
    sns.set_context("talk", font_scale=1)
    ylabel = {'electric': 'electric (kBtu/sq.ft)', 'gas': 'gas kBtu/sq.ft'}
    col_dict = {'electric': 'eui_elec', 'gas': 'eui_gas'}
    print len(bs_pair)
    sns.set_style("whitegrid")
    # palette = sns.cubehelix_palette(len(bs_pair))
    palette = sns.color_palette('husl', len(bs_pair))
    sns.set_palette(palette)
    colors_rgb = [util.float2hex(x) for x in palette]
    sns.set_context("talk", font_scale=1)
    jsondir = os.getcwd(
    ) + '/plot_FY_weather/html/by_region/Region{}/piecewise_all/json/'.format(
        region)
    title = "Region {}".format(region)
    col = col_dict[measure_type]
    for i, (b, s) in enumerate(bs_pair):
        print b, s
        df = df_bs[df_bs['Building_Number'] == b]
        df = df.head(n=36)
        print df.head()
        points = df[col]
        min_time = df['Timestamp'].min()
        max_time = df['Timestamp'].max()
        bx = plt.axes()
        d0 = plot_piece(df,
                        bx,
                        title,
                        colors_rgb[i],
                        measure_type,
                        b,
                        s,
                        scatter=False,
                        annote=True,
                        jsondir=jsondir,
                        season=season)
    plt.xlabel('Temperature_F')
    # plt.show()
    if season is None:
        path = os.getcwd(
        ) + '/plot_FY_weather/html/by_region/Region{}/piecewise_all/{}.png'.format(
            region, measure_type)
    else:
        path = os.getcwd(
        ) + '/plot_FY_weather/html/by_region/Region{}/piecewise_all/{}_{}.png'.format(
            region, measure_type, season)
    P.savefig(path,
              dpi=my_dpi,
              figsize=(2000 / my_dpi, 500 / my_dpi),
              bbox_inches='tight')
    plt.close()
    return