Beispiel #1
0
def plot_action_fromdb():
    conn = uo.connect('all')
    with conn:
        df_action = pd.read_sql('SELECT * FROM EUAS_ecm', conn)
        df_pro = pd.read_sql('SELECT * FROM EUAS_ecm_program', conn)
        df_bs = pd.read_sql('SELECT * FROM EUAS_monthly_weather', conn)
    bs_dict = dict(zip(df_bs['Building_Number'], df_bs['ICAO']))
    df_action = df_action[['Building_Number', 'high_level_ECM', 'detail_level_ECM', 'Substantial_Completion_Date']]
    df_act = df_action.copy()
    df_act = df_act.dropna()
    df_act['action'] = df_act.apply(lambda r: r['high_level_ECM'] + ' -- ' + r['detail_level_ECM'], axis=1)
    df_act.drop(['high_level_ECM', 'detail_level_ECM'], axis=1,
                inplace=True)
    gr = df_act.groupby('Building_Number')
    names = list(gr.groups)
    # print names.index('WA0120BN')
    lines = ['Building_Number,Time,Action,Electric_Saving,Gas_Saving,Electric_Before,Electric_After,Gas_Before,Gas_After,Electric_CVRMSE,Gas_CVRMSE']
    # FIXME: PA0060ZZ has None in eui_gas
    del names[153]
    # names = ['CA0306ZZ']
    for i, name in enumerate(names):
        print i, name, '222222222222222222222222222222'
        group = gr.get_group(name)
        df_temp = group.groupby(['Substantial_Completion_Date'])['action'].apply(lambda x: '\n'.join(x))
        df_show = df_temp.to_frame('ECM action')
        # df_show['Building Number'] = 'CT0013ZZ'
        df_show.reset_index(inplace=True)
        df_show['Substantial_Completion_Date'] = pd.to_datetime(df_show['Substantial_Completion_Date'])
        df_show.sort('Substantial_Completion_Date', inplace=True)
        days_diff = useq.dist_between_adjacent(df_show['Substantial_Completion_Date'].tolist())
        pair = zip(df_show['Substantial_Completion_Date'].tolist(),
                   df_show['ECM action'].tolist())
        str_pair = [('{0}-{1}-{2}'.format(x[0].year, x[0].month, x[0].day), x[1]) for x in pair]
        breakpoints = [x[0] for x in str_pair]
        print breakpoints
        assert(len(breakpoints) > 0)
        ranges = ['before {0}'.format(breakpoints[0])] + useq.merge_adjacent(breakpoints, lambda x, y: '{0} -- {1}'.format(x, y)) + ['after {0}'.format(breakpoints[-1])]
        actionpoints = [x[1] for x in str_pair]
        actions = ['pre {0}'.format(actionpoints[0])] + useq.merge_adjacent(actionpoints, lambda x, y: 'post {0} pre {1}'.format(x, y)) + ['post {0}'.format(actionpoints[-1])]
        b = name
        # df_eng = gr_energy.get_group(b)
        # df_eng.reset_index(inplace=True)
        s = bs_dict[b]
        ar_pair = zip(actions, ranges)
        # un-comment if need to plot energy and dd trend side by side
        # plot_trend_fromdb(b, s, breakpoints)

        print 'plot trend per dd'
        plot_trend_per_dd_fromdb(b, s, breakpoints)

        results = []
        for a, r in zip(actions, ranges):
            result = ltm.lean_temperature_fromdb(b, s, 2, r, action=a)
            if result == None:
                result = (None, None, None)
            d = {'building': b, 'station': s, 'timerange': r, 'action': a}
            result += tuple([d])
            results.append(result)
        length = len(results)
        if length == 0:
            print 'no lean plot generated'
            continue
        def merge_action(string):
            string = string.replace('\n', ';')
            string = string.replace('pre ', '')
            return string
        def concat(string):
            return string.replace('\n', ';')
        for i in range(len(results) - 1):
            d_save, d_cvrmse = plot_saving_fromdb(b, s, results[i], results[i + 1])
            lines.append(','.join(map(str, [b, breakpoints[i],
                                            # (merge_action(actions[i])),
                                            concat(actionpoints[i]),
                                            d_save['elec_percent'],
                                            d_save['gas_percent'],
                                            d_save['elec_before'],
                                            d_save['elec_after'],
                                            d_save['gas_before'],
                                            d_save['gas_after'],
                                            d_cvrmse['elec'],d_cvrmse['gas']])))
        process_html(b, s, results, breakpoints)
    with open (os.getcwd() + '/plot_FY_weather/html/table/action_saving.csv', 'w+') as wt:
        wt.write('\n'.join(lines))
Beispiel #2
0
def plot_action():
    df = pd.read_csv(homedir + 'master_table/ECM/EUAS_ecm.csv')
    df = df[['Building Number', 'high_level_ECM', 'detail_level_ECM', 'Substantial Completion Date']]
    df_bs = pd.read_csv(homedir + 'master_table/indicator_wECM_weather.csv')
    df_bs = df_bs[df_bs['Valid Weather Data'] == 1]
    bs_dict = dict(zip(df_bs['Building Number'], df_bs['ICAO']))
    print len(df)
    df.dropna(inplace=True)
    print len(df)
    df['action'] = df.apply(lambda r: r['high_level_ECM'] + ' -- ' + r['detail_level_ECM'], axis=1)
    df.drop(['high_level_ECM', 'detail_level_ECM'], axis=1,
            inplace=True)
    gr = df.groupby('Building Number')
    names = list(gr.groups)
    # print names.index('WA0120BN')
    # names = ['CA0168ZZ']
    for i, name in enumerate(names[64:]):
        print i, name, '222222222222222222222222222222'
        group = gr.get_group(name)
        df_temp = group.groupby(['Substantial Completion Date'])['action'].apply(lambda x: '\n'.join(x))
        df_show = df_temp.to_frame('ECM action')
        # df_show['Building Number'] = 'CT0013ZZ'
        df_show.reset_index(inplace=True)
        df_show['Substantial Completion Date'] = pd.to_datetime(df_show['Substantial Completion Date'])
        df_show.sort('Substantial Completion Date', inplace=True)
        days_diff = useq.dist_between_adjacent(df_show['Substantial Completion Date'].tolist())
        pair = zip(df_show['Substantial Completion Date'].tolist(),
                   df_show['ECM action'].tolist())
        str_pair = [('{0}-{1}-{2}'.format(x[0].year, x[0].month, x[0].day), x[1]) for x in pair]
        breakpoints = [x[0] for x in str_pair]
        print breakpoints
        assert(len(breakpoints) > 0)
        ranges = ['before {0}'.format(breakpoints[0])] + useq.merge_adjacent(breakpoints, lambda x, y: '{0} -- {1}'.format(x, y)) + ['after {0}'.format(breakpoints[-1])]
        actionpoints = [x[1] for x in str_pair]
        actions = ['pre {0}'.format(actionpoints[0])] + useq.merge_adjacent(actionpoints, lambda x, y: 'post {0} pre {1}'.format(x, y)) + ['post {0}'.format(actionpoints[-1])]
        b = name
        if b in bs_dict:
            s = bs_dict[b]
        else:
            print 'no weather data'
            continue
        ar_pair = zip(actions, ranges)
        # FIXME populate energy and weather data
        df_eng = pd.read_csv(weatherdir + \
                             'energy_temp/{0}_{1}.csv'.format(b, s))
        # un-comment if need to plot energy and dd trend side by side
        plot_trend(b, s, df_eng, breakpoints)

        print 'plot trend per dd'
        plot_trend_per_dd(b, s, df_eng, breakpoints)

        results = []
        for a, r in zip(actions, ranges):
            result = ltm.lean_temperature(b, s, 2, r, action=a)
            if result == None:
                result = (None, None, None)
            d = {'building': b, 'station': s, 'timerange': r, 'action': a}
            result += tuple([d])
            results.append(result)
        length = len(results)
        if length == 0:
            print 'no lean plot generated'
            continue
        for i in range(len(results) - 1):
            plot_saving(b, s, results[i], results[i + 1])
        process_html(b, s, results)
Beispiel #3
0
def plot_action():
    df = pd.read_csv(homedir + 'master_table/ECM/EUAS_ecm.csv')
    df = df[[
        'Building Number', 'high_level_ECM', 'detail_level_ECM',
        'Substantial Completion Date'
    ]]
    df_bs = pd.read_csv(homedir + 'master_table/indicator_wECM_weather.csv')
    df_bs = df_bs[df_bs['Valid Weather Data'] == 1]
    bs_dict = dict(zip(df_bs['Building Number'], df_bs['ICAO']))
    print len(df)
    df.dropna(inplace=True)
    print len(df)
    df['action'] = df.apply(
        lambda r: r['high_level_ECM'] + ' -- ' + r['detail_level_ECM'], axis=1)
    df.drop(['high_level_ECM', 'detail_level_ECM'], axis=1, inplace=True)
    gr = df.groupby('Building Number')
    names = list(gr.groups)
    # print names.index('WA0120BN')
    # names = ['CA0168ZZ']
    for i, name in enumerate(names[64:]):
        print i, name, '222222222222222222222222222222'
        group = gr.get_group(name)
        df_temp = group.groupby(['Substantial Completion Date'
                                 ])['action'].apply(lambda x: '\n'.join(x))
        df_show = df_temp.to_frame('ECM action')
        # df_show['Building Number'] = 'CT0013ZZ'
        df_show.reset_index(inplace=True)
        df_show['Substantial Completion Date'] = pd.to_datetime(
            df_show['Substantial Completion Date'])
        df_show.sort('Substantial Completion Date', inplace=True)
        days_diff = useq.dist_between_adjacent(
            df_show['Substantial Completion Date'].tolist())
        pair = zip(df_show['Substantial Completion Date'].tolist(),
                   df_show['ECM action'].tolist())
        str_pair = [('{0}-{1}-{2}'.format(x[0].year, x[0].month,
                                          x[0].day), x[1]) for x in pair]
        breakpoints = [x[0] for x in str_pair]
        print breakpoints
        assert (len(breakpoints) > 0)
        ranges = ['before {0}'.format(breakpoints[0])] + useq.merge_adjacent(
            breakpoints, lambda x, y: '{0} -- {1}'.format(x, y)) + [
                'after {0}'.format(breakpoints[-1])
            ]
        actionpoints = [x[1] for x in str_pair]
        actions = ['pre {0}'.format(actionpoints[0])] + useq.merge_adjacent(
            actionpoints, lambda x, y: 'post {0} pre {1}'.format(x, y)) + [
                'post {0}'.format(actionpoints[-1])
            ]
        b = name
        if b in bs_dict:
            s = bs_dict[b]
        else:
            print 'no weather data'
            continue
        ar_pair = zip(actions, ranges)
        # FIXME populate energy and weather data
        df_eng = pd.read_csv(weatherdir + \
                             'energy_temp/{0}_{1}.csv'.format(b, s))
        # un-comment if need to plot energy and dd trend side by side
        plot_trend(b, s, df_eng, breakpoints)

        print 'plot trend per dd'
        plot_trend_per_dd(b, s, df_eng, breakpoints)

        results = []
        for a, r in zip(actions, ranges):
            result = ltm.lean_temperature(b, s, 2, r, action=a)
            if result == None:
                result = (None, None, None)
            d = {'building': b, 'station': s, 'timerange': r, 'action': a}
            result += tuple([d])
            results.append(result)
        length = len(results)
        if length == 0:
            print 'no lean plot generated'
            continue
        for i in range(len(results) - 1):
            plot_saving(b, s, results[i], results[i + 1])
        process_html(b, s, results)