<commit_before># -*- coding: utf-8 -*-
"""
===========================
Dynamic demand graphs algorithm
===========================
J. Fonseca  script development          25.08.15

"""
from __future__ import division

import pandas as pd

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


__author__ = "<NAME>"
__copyright__ = "Copyright 2016, Architecture and Building Systems - ETH Zurich"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"

def graph(locator, parameters, type):

    # create figure in plotlib with subplots
    num_parameters = len(parameters)
    if num_parameters > 1:
        if 2 <= num_parameters < 4:
            rows, columns = 1, num_parameters
        elif 4 <= num_parameters < 7:
            rows, columns = 2, int(num_parameters/2)
        fig = tools.make_subplots(rows=rows, cols=columns, print_grid=False , subplot_titles=parameters)

    if type is 'heatmap':
        fig = heatmap(parameters, locator, rows, columns, fig, num_parameters)
    else:
        fig = bubbles(parameters, locator, rows, columns, fig, num_parameters)

    plot(fig, auto_open=False, filename=locator.get_sensitivity_plots_file('sensitivity_anlaysis'))


def bubbles(parameters, locator, rows, columns, fig, num_parameters):
    counter = 0
    for row in range(1, rows+1):
        for column in range(1, columns+1):
            traces = []
        for parameter in parameters:
            #read the mustar of morris analysis
            data = pd.read_excel(locator.get_sensitivity_output(), parameter+'mu')
            var_names = data.columns.values

            #notmalize to max
            data[var_names] = data[var_names].div(data[var_names].max(axis=1), axis=0)
            # print each diagram
            y = ['opt '+str(x) for x in list(data.index+1)]
            x = list(data.columns)

            counter2 = 0
            for index, datarow in data.iterrows():
                values = list(datarow)
                traces = go.Scatter(x =x, y=y[counter2], mode='markers',
                                         marker = dict(size=values, sizeref=0.2, sizemode='area',))

                fig.append_trace(traces, row, columns)
                print y[counter2], counter, counter2
                counter2 +=1

                fig['layout']['xaxis' + str(counter + 1)].update(title='Variables')
                fig['layout']['yaxis' + str(counter + 1)].update(title='Options')
            counter = +1
            if counter is num_parameters:
                break

    return fig

def heatmap(parameters, locator, rows, columns, fig, num_parameters):
    traces = []
    for parameter in parameters:
        #read the mustar of morris analysis
        data = pd.read_excel(locator.get_sensitivity_output(), parameter+'mu')
        var_names = data.columns.values

        #notmalize to max
        data[var_names] = data[var_names].div(data[var_names].max(axis=1), axis=0)

        # print each diagram
        y = ['opt '+str(x) for x in list(data.index+1)]
        x = list(data.columns)
        matrix = data.values.tolist()
        traces.append(go.Heatmap(z = matrix, x =x, y=y, colorscale = 'Viridis'))

    # check layout
    counter = 0
    for row in range(1, rows+1):
        for column in range(1, columns+1):
            fig.append_trace(traces[counter], row, column)
            fig['layout']['xaxis'+str(counter+1)].update(title='Variables')
            fig['layout']['yaxis' + str(counter + 1)].update(title='Options')
            counter = +1
            if counter is num_parameters:
                break
    fig['layout'].update(title='sensitivity_analysis')

    return fig

def run_as_script():
    import cea.globalvar as gv
    import cea.inputlocator as inputlocator
    gv = gv.GlobalVariables()
    scenario_path = gv.scenario_reference
    locator = inputlocator.InputLocator(scenario_path=scenario_path)
    output_parameters = ['QHf_MWhyr', 'QCf_MWhyr', 'Ef_MWhyr', 'Ef0_kW', 'QHf0_kW', 'QCf0_kW']
    type = 'bubbles' # heatmpa of bubbles
    graph(locator, output_parameters, type)

if __name__ == '__main__':
    run_as_script()<commit_msg>working with elipse and bubble (half there) with plotly<commit_after># -*- coding: utf-8 -*-
"""
===========================
Dynamic demand graphs algorithm
===========================
J. Fonseca  script development          25.08.15

"""
from __future__ import division

import pandas as pd

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


__author__ = "<NAME>"
__copyright__ = "Copyright 2016, Architecture and Building Systems - ETH Zurich"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"

def graph(locator, parameters, type):

    # create figure in plotlib with subplots

    if type is 'heatmap':
        heatmap(parameters, locator)
    else:
        bubbles(parameters, locator)

def bubbles2(parameters, locator):

    for parameter in parameters:
        #read the mustar of morris analysis
        data = pd.read_excel(locator.get_sensitivity_output(), parameter+'mu')
        var_names = data.columns.values

        #notmalize to max
        data[var_names] = data[var_names].div(data[var_names].max(axis=1), axis=0)*1000
        # print each diagram
        y = ['opt '+str(x) for x in list(data.index+1)]
        x = list(data.columns)

        counter2 = 0
        traces = []
        for index, datarow in data.iterrows():
            z = list(datarow)
            print z
            y_trace = [y[counter2]]*len(z)
            traces.append(go.Scatter(x =x, y= y_trace, mode='markers',
                                     marker = dict(size=z, sizeref=0.1, sizemode='area',
                                                   colorscale='Viridis', showscale= True)))
            counter2 +=1
        print 'end'
        plot(traces, auto_open=False, filename=locator.get_sensitivity_plots_file(parameter))

def bubbles(parameters, locator):
    import matplotlib.pyplot as plt
    from matplotlib.patches import Ellipse
    from matplotlib import cm
    import numpy as np

    for parameter in parameters:
        #read the mustar of morris analysis
        data_mu = pd.read_excel(locator.get_sensitivity_output(), parameter+'mu')
        data_sigma = pd.read_excel(locator.get_sensitivity_output(), parameter + 's')
        var_names = data_mu.columns.values

        # normalize data to maximum value
        data_mu[var_names] = data_mu[var_names].div(data_mu[var_names].max(axis=1), axis=0)
        data_sigma[var_names] = data_sigma[var_names].div(data_sigma[var_names].max(axis=1), axis=0)
        # get x_names and y_names
        # columns
        x_names = data_mu.columns.tolist()
        # rows
        y_names = ['opt '+str(i) for i in list(data_mu.index+1)]

        # get counter (integer to create the graph)
        x = range(len(x_names))
        y = range(len (y_names))

        X, Y = np.meshgrid(x,y)
        print X, Y

        # get ellipses
        ells = [Ellipse(xy=(i,j), width=data_mu.ix[j, i], height=data_sigma.ix[j, i], angle=0) for i in x for j in y]
        fig = plt.figure(0)
        ax = fig.add_subplot(111, aspect='equal')
        for e in ells:
            ax.add_artist(e)
            e.set_clip_box(ax.bbox)
            e.set_linewidth(1)

            if e.width > 0.8:
                e.set_alpha(1)
            elif 0.6 < e.width <= 0.8:
                e.set_alpha(0.8)
            elif 0.4 < e.width <= 0.6:
                e.set_alpha(0.6)
            elif 0.2 < e.width <= 0.4:
                e.set_alpha(0.4)
            else:
                e.set_alpha(0.2)

        ax.set_xlim(-1, len(x_names))
        ax.set_ylim(-1, len(y_names))
        ax.set_xlabel('Variables')
        ax.set_ylabel('Configuration')
        cbar = plt.colorbar(ec)
        cbar.set_label('X+Y')

        plt.show()

def heatmap(parameters, locator):
    for parameter in parameters:
        traces = []

        #read the mustar of morris analysis
        data = pd.read_excel(locator.get_sensitivity_output(), parameter+'mu')
        var_names = data.columns.values

        #notmalize to max
        data[var_names] = data[var_names].div(data[var_names].max(axis=1), axis=0)

        # print each diagram
        y = ['opt '+str(x) for x in list(data.index+1)]
        x = list(data.columns)
        matrix = data.values.tolist()

        traces.append(go.Heatmap(z = matrix, x =x, y=y, colorscale = 'Viridis'))

        plot(traces, auto_open=False, filename=locator.get_sensitivity_plots_file(parameter))

def run_as_script():
    import cea.globalvar as gv
    import cea.inputlocator as inputlocator
    gv = gv.GlobalVariables()
    scenario_path = gv.scenario_reference
    locator = inputlocator.InputLocator(scenario_path=scenario_path)
    output_parameters = ['QHf_MWhyr', 'QCf_MWhyr', 'Ef_MWhyr', 'Ef0_kW', 'QHf0_kW', 'QCf0_kW']
    type = 'bubbles'  # heatmap of bubbles
    graph(locator, output_parameters, type)

if __name__ == '__main__':
    run_as_script()
{'fraction_non_alphanumeric': 0.12668698302133216, 'fraction_numerical': 0.013604701784936874, 'mean_word_length': 2.5630089181853433, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 13, '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}}
{'dir': 'git-commits-cleaned', 'id': '5509593', 'n_tokens_mistral': 3046, 'n_tokens_neox': 2903, 'n_words': 700}