<commit_before>from __future__ import division
from __future__ import print_function

import plotly.graph_objs as go
from plotly.offline import plot

from cea.plots.variable_naming import LOGO, COLOR, NAMING

def all_tech_district_yearly(data_frame, pv_analysis_fields, pvt_analysis_fields, sc_fp_analysis_fields, sc_et_analysis_fields, title,
                             output_path):
    E_analysis_fields = []
    Q_analysis_fields = []

    pv_E_analysis_fields_used = data_frame.columns[data_frame.columns.isin(pv_analysis_fields)].tolist()
    E_analysis_fields.extend(pv_E_analysis_fields_used)
    sc_fp_Q_analysis_fields_used = data_frame.columns[data_frame.columns.isin(sc_fp_analysis_fields)].tolist()
    Q_analysis_fields.extend(sc_fp_Q_analysis_fields_used)
    sc_et_Q_analysis_fields_used = data_frame.columns[data_frame.columns.isin(sc_et_analysis_fields)].tolist()
    Q_analysis_fields.extend(sc_et_Q_analysis_fields_used)
    pvt_E_analysis_fields_used = data_frame.columns[data_frame.columns.isin(pvt_analysis_fields[0:5])].tolist()
    E_analysis_fields.extend(pvt_E_analysis_fields_used)
    pvt_Q_analysis_fields_used = data_frame.columns[data_frame.columns.isin(pvt_analysis_fields[5:10])].tolist()
    Q_analysis_fields.extend(pvt_Q_analysis_fields_used)
    data_frame_MWh = data_frame / 1000  # to MWh

    # CALCULATE GRAPH
    traces_graph, x_axis = calc_graph(E_analysis_fields, Q_analysis_fields, data_frame_MWh)

    # CALCULATE TABLE
    traces_table = calc_table(E_analysis_fields, Q_analysis_fields, data_frame_MWh)

    # PLOT GRAPH
    traces_graph.append(traces_table)

    # CREATE BUTTON
    annotations = list(
        [dict(
            text='<b>In this plot, users can explore the combined potentials of all solar technologies.</b><br>'
                 'Instruction:'
                 'Click on the technologies to install on each building surface.<br>'
                 'Example: PV_walls_east_E + PVT_walls_south_E/Q + SC_FP_roofs_top_Q <br><br>'
            , x=0.8, y=1.1,
            xanchor='left', xref='paper', yref='paper', align='left', showarrow=False, bgcolor="rgb(254,220,198)")])

    layout = go.Layout(images=LOGO, title=title, barmode='stack', annotations=annotations,
                       yaxis=dict(title='Electricity/Thermal Potential [MWh/yr]', domain=[0.35, 1]),
                       xaxis=dict(title='Building'), legend=dict(x=1, y=0.1, xanchor='left'))
    fig = go.Figure(data=traces_graph, layout=layout)
    plot(fig, auto_open=False, filename=output_path)

    return {'data': traces_graph, 'layout': layout}


def calc_graph(E_analysis_fields, Q_analysis_fields, data_frame):
    # calculate graph
    graph = []

    data_frame['total_E'] = total_E = data_frame[E_analysis_fields].sum(axis=1)
    data_frame['total_Q'] = total_Q = data_frame[Q_analysis_fields].sum(axis=1)
    # data_frame = data_frame.sort_values(by='total', ascending=False) # this will get the maximum value to the left
    for field in E_analysis_fields:
        y = data_frame[field]
        total_perc = (y / total_E.sum() * 100).round(2)
        total_perc_txt = ["(" + str(x) + " %)" for x in total_perc]
        if field.split('_')[0] == 'PVT':
            trace1 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field]), visible='legendonly',
                            width=0.3, offset=-0.35, legendgroup=field.split('_')[1])
        else:
            trace1 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field]), visible='legendonly',
                            width=0.3, offset=-0.35)
        graph.append(trace1)

    for field in Q_analysis_fields:
        y = data_frame[field]
        total_perc = (y / total_Q * 100).round(2).values
        total_perc_txt = ["(" + str(x) + " %)" for x in total_perc]
        if field.split('_')[0] == 'PVT':
            trace2 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field], line=dict(
                                color="rgb(105,105,105)", width=1)), opacity=0.7, visible='legendonly', base=0,
                            width=0.3, offset=0, legendgroup=field.split('_')[1])
        elif field.split('_')[1]== 'FP':
            trace2 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field], line=dict(
                                color="rgb(105,105,105)", width=1)), opacity=0.7, visible='legendonly', base=0,
                            width=0.3, offset=0)
        elif field.split('_')[1]=='ET':
            trace2 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field], line=dict(
                                color="rgb(105,105,105)", width=1)), opacity=0.7, visible='legendonly', base=0,
                            width=0.3, offset=0)
        else: raise ValueError('the specified analysis field is not in the right form: ',field)

        graph.append(trace2)

    return graph, data_frame.index,


def calc_table(E_analysis_fields, Q_analysis_fields, data_frame):
    analysis_fields = []
    total_perc = []
    median = []
    # find the three highest
    anchors = []
    load_names = []
<commit_msg>group pvt legends from the same surface<commit_after>from __future__ import division
from __future__ import print_function

import plotly.graph_objs as go
from plotly.offline import plot

from cea.plots.variable_naming import LOGO, COLOR, NAMING

def all_tech_district_yearly(data_frame, pv_analysis_fields, pvt_analysis_fields, sc_fp_analysis_fields, sc_et_analysis_fields, title,
                             output_path):
    E_analysis_fields = []
    Q_analysis_fields = []

    pv_E_analysis_fields_used = data_frame.columns[data_frame.columns.isin(pv_analysis_fields)].tolist()
    E_analysis_fields.extend(pv_E_analysis_fields_used)
    sc_fp_Q_analysis_fields_used = data_frame.columns[data_frame.columns.isin(sc_fp_analysis_fields)].tolist()
    Q_analysis_fields.extend(sc_fp_Q_analysis_fields_used)
    sc_et_Q_analysis_fields_used = data_frame.columns[data_frame.columns.isin(sc_et_analysis_fields)].tolist()
    Q_analysis_fields.extend(sc_et_Q_analysis_fields_used)
    pvt_E_analysis_fields_used = data_frame.columns[data_frame.columns.isin(pvt_analysis_fields[0:5])].tolist()
    E_analysis_fields.extend(pvt_E_analysis_fields_used)
    pvt_Q_analysis_fields_used = data_frame.columns[data_frame.columns.isin(pvt_analysis_fields[5:10])].tolist()
    Q_analysis_fields.extend(pvt_Q_analysis_fields_used)
    data_frame_MWh = data_frame / 1000  # to MWh

    # CALCULATE GRAPH
    traces_graph, x_axis = calc_graph(E_analysis_fields, Q_analysis_fields, data_frame_MWh)

    # CALCULATE TABLE
    traces_table = calc_table(E_analysis_fields, Q_analysis_fields, data_frame_MWh)

    # PLOT GRAPH
    traces_graph.append(traces_table)

    # CREATE BUTTON
    annotations = list(
        [dict(
            text='<b>In this plot, users can explore the combined potentials of all solar technologies.</b><br>'
                 'Instruction:'
                 'Click on the technologies to install on each building surface.<br>'
                 'Example: PV_walls_east_E + PVT_walls_south_E/Q + SC_FP_roofs_top_Q <br><br>'
            , x=0.8, y=1.1,
            xanchor='left', xref='paper', yref='paper', align='left', showarrow=False, bgcolor="rgb(254,220,198)")])

    layout = go.Layout(images=LOGO, title=title, barmode='stack', annotations=annotations,
                       yaxis=dict(title='Electricity/Thermal Potential [MWh/yr]', domain=[0.35, 1]),
                       yaxis2=dict(overlaying='y', anchor='x', domain=[0.35, 1]),
                       xaxis=dict(title='Building'), legend=dict(x=1, y=0.1, xanchor='left'))
    fig = go.Figure(data=traces_graph, layout=layout)
    plot(fig, auto_open=False, filename=output_path)

    return {'data': traces_graph, 'layout': layout}


def calc_graph(E_analysis_fields, Q_analysis_fields, data_frame):
    # calculate graph
    graph = []

    data_frame['total_E'] = total_E = data_frame[E_analysis_fields].sum(axis=1)
    data_frame['total_Q'] = total_Q = data_frame[Q_analysis_fields].sum(axis=1)
    # data_frame = data_frame.sort_values(by='total', ascending=False) # this will get the maximum value to the left
    for field in E_analysis_fields:
        y = data_frame[field]
        total_perc = (y / total_E.sum() * 100).round(2)
        total_perc_txt = ["(" + str(x) + " %)" for x in total_perc]
        if field.split('_')[0] == 'PVT':
            trace1 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field]), visible='legendonly',
                            width=0.3, offset=-0.35, legendgroup='PVT'+field.split('_')[2])
        else:
            trace1 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field]), visible='legendonly',
                            width=0.3, offset=-0.35)
        graph.append(trace1)

    for field in Q_analysis_fields:
        y = data_frame[field]
        total_perc = (y / total_Q * 100).round(2).values
        total_perc_txt = ["(" + str(x) + " %)" for x in total_perc]
        if field.split('_')[0] == 'PVT':
            trace2 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field], line=dict(
                                color="rgb(105,105,105)", width=1)), opacity=1, visible='legendonly',
                            width=0.3, offset=0, legendgroup='PVT'+field.split('_')[2])
        elif field.split('_')[1]== 'FP':
            trace2 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field], line=dict(
                                color="rgb(105,105,105)", width=1)), opacity=1, visible='legendonly',
                            width=0.3, offset=0)
        elif field.split('_')[1]=='ET':
            trace2 = go.Bar(x=data_frame.index, y=y, name=field.split('_kWh', 1)[0], text=total_perc_txt,
                            marker=dict(color=COLOR[field], line=dict(
                                color="rgb(105,105,105)", width=1)), opacity=1, visible='legendonly',
                            width=0.3, offset=0)
        else: raise ValueError('the specified analysis field is not in the right form: ',field)

        graph.append(trace2)

    return graph, data_frame.index,


def calc_table(E_analysis_fields, Q_analysis_fields, data_frame):
    analysis_fields = []
    total_perc = []
    median = []
    # find the three highest
    anchors = []
    load_names = []

{'fraction_non_alphanumeric': 0.12822828246342785, 'fraction_numerical': 0.021582084161098067, 'mean_word_length': 3.0434465133260313, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '133632', 'n_tokens_mistral': 3831, 'n_tokens_neox': 3642, 'n_words': 611}