def aggregate(transaction_history):
    net = dict()
    buy = dict()
    sell = dict()
    interest = dict()
    dividend = dict()
    historical = dict()
    
    for t in transaction_history:
        quarter = "%s-Q%i" % (t.date.year, (t.date.month-1)//3+1)
        net[quarter] = float(handleNull(net.get(quarter))) + float(t.cashflow)
        if t.kind == Transaction.BUY:
            buy[quarter] = float(handleNull(buy.get(quarter))) + float(t.cashflow)
        elif t.kind == Transaction.SELL:
            sell[quarter] = float(handleNull(sell.get(quarter))) + float(t.cashflow)
        elif t.kind == Transaction.INTEREST:
            interest[quarter] = float(handleNull(interest.get(quarter))) + float(t.cashflow)
        elif t.kind == Transaction.DIVIDEND:
            dividend[quarter] = float(handleNull(dividend.get(quarter))) + float(t.cashflow)
        elif t.kind == Transaction.HISTORICAL or t.kind == Transaction.CURRENT:
            historical[quarter] = float(handleNull(historical.get(quarter))) + float(t.cashflow)

    net = addMissingQuarters(net)
    buy = addMissingQuarters(buy)
    sell = addMissingQuarters(sell)
    interest = addMissingQuarters(interest)
    dividend = addMissingQuarters(dividend)
    historical = addMissingQuarters(historical)

    d = {'net': pd.Series(net),
         'buy': pd.Series(buy), 
         'sell': pd.Series(sell),
         'interest':pd.Series(interest),
         'dividend':pd.Series(dividend),
         'historical':pd.Series(historical)}

    df = pd.DataFrame(d)
    df['label']=df.index
    p1 = Bar(df, 
            values = blend('buy','sell','interest','dividend','historical',name='cashflow', labels_name='cf'),
            label=cat(columns='label',sort=False),
            stack=cat(columns='cf',sort=False))

    p2 = Bar(df,
             values = blend('net'),
             label='label')

    output_file("test.html")
    
    show(vplot(p1, p2))
Exemple #2
0
def scatter():
    s = Scatter(
        flowers,
        title="Fisher's Iris data set", tools='tap,box_select,save',
        x=blend('petal_length', name='Length'),
        y=blend('petal_width', name='Width'),
        color='species', palette=Spectral4, legend=True,
    )
    # Lets move the legend off-canvas!
    legend = s.legend[0]
    legend.border_line_color = None
    legend.orientation = 'horizontal'
    legend.location = (0, 0)
    s.above.append(legend)
    return s
Exemple #3
0
def scatter():
    s = Scatter(
        flowers,
        title="Fisher's Iris data set", tools='tap,box_select,save',
        x=blend('petal_length', name='Length'),
        y=blend('petal_width', name='Width'),
        color='species', palette=Spectral4, legend=True,
    )
    # Lets move the legend off-canvas!
    legend = s.legend[0]
    legend.border_line_color = None
    legend.orientation = 'horizontal'
    legend.location = (0, 0)
    s.above.append(legend)
    return s
Exemple #4
0
def generate_chart(year):
    data = get_data(year)

    barchart = Bar(data,
                   values=blend(*SCORED_EVENTS, labels_name='event'),
                   label=cat(columns='Team', sort=False),
                   stack=cat(columns='event', sort=False),
                   color=color(columns='event', palette=Spectral9, sort=False),
                   xgrid=False, ygrid=False, legend='top_right',
                   plot_width=1000, plot_height=625,
                   tools="pan,wheel_zoom,box_zoom,reset,resize")

    barchart.title = "Formula SAE Michigan " + str(year) + " Total Scores by Place"

    barchart._xaxis.axis_label = "Teams"
    barchart._xaxis.axis_line_color = None
    barchart._xaxis.major_tick_line_color = None
    barchart._xaxis.minor_tick_line_color = None
    barchart._xaxis.major_label_text_font_size = '0.6em'

    barchart._yaxis.axis_label = "Total Score"
    barchart._yaxis.axis_line_color = None
    barchart._yaxis.major_tick_line_color = None
    barchart._yaxis.minor_tick_line_color = None

    barchart.outline_line_color = None

    barchart.toolbar_location = 'right'

    barchart.logo = None

    return barchart
 def plotAverage(self):
     bar = Bar(\
             self.__table,
             values = blend('average', name = 'average', labels_name = 'average'),\
             label = cat(columns = 'country', sort = False),\
             stack = cat(columns = 'average'),\
             agg = 'mean',\
             title = 'Average of CO2 emissions in European Union',\
             legend = False,\
             tooltips = [('Average', '@average{1.11}' + ' g/km')])
     return bar
Exemple #6
0
 def plotAverage(self):
     bar = Bar(\
             self.__table,
             values = blend('average', name = 'average', labels_name = 'average'),\
             label = cat(columns = 'fuel'),\
             stack = cat(columns = 'average'),\
             agg = 'mean',\
             title = 'Average of CO2 emissions (by fuel type) in ' + self.__title,\
             legend = False,\
             tooltips = [('Average', '@average{1.11}' + ' g/km')])
     return bar
 def plot(self):
     bar = Bar(\
             self.__fuel_table,
             values = blend('percentage', name = 'percentage', labels_name = 'percentage'),\
             label = cat(columns = 'fuel'),\
             stack = cat(columns = 'percentage'),\
             agg = 'mean',\
             title = self.__title,\
             legend = False,\
             tooltips = [('Percentage', '@percentage{0.00%}')])
     bar._yaxis.formatter = NumeralTickFormatter(format = '0%')
     return bar
Exemple #8
0
 def plotTotal(self):
     bar = Bar(\
             self.__table,
             values = blend('total', name = 'total', labels_name = 'total'),\
             label = cat(columns = 'fuel'),\
             stack = cat(columns = 'total'),\
             agg = 'mean',\
             title = 'Total of CO2 emissions (by fuel type) in ' + self.__title,\
             legend = False,\
             tooltips = [('Total', '@total{1,1}' + ' g/km')])
     bar._yaxis.formatter = NumeralTickFormatter(format = '0,0' + ' g/km')
     return bar
Exemple #9
0
 def plot(self):
     bar = Bar(\
             self.__table,
             values = blend('registrations', name = 'registrations', labels_name = 'registrations'),\
             label = cat(columns = 'country', sort = False),\
             stack = cat(columns = 'registrations'),\
             agg = 'mean',\
             title = 'Total of new registrations in European Union',\
             legend = False,\
             tooltips = [('Registrations', '@registrations{1,1}')])
     bar._yaxis.formatter = NumeralTickFormatter(format='0,0')
     return bar
Exemple #10
0
def create_bar(data):
    op = blend(*countries, labels_name='countries', name='energy')
    return Bar(data,
               label='year',
               values=op,
               color='countries',
               group='countries',
               width=1400,
               height=600,
               ylabel='Energy use per capita',
               palette=['purple', 'green', 'blue', 'pink'],
               legend=True)
Exemple #11
0
def generate_chart(team):
    data = generate_data(team)
    selectable_years = list(map(str, data.index.unique()))

    # Generate the chart UNFORTUNATELY using the high level plots. Streaming
    # a bunch of quads resulted in lots of graphics corruption when switching
    # teams. I would rather have the plots work all the time but really slow
    # then have the plot show the wrong information.
    barchart = Bar(data,
                   values=blend(*SCORED_EVENTS, labels_name='event'),
                   label=cat(columns='Year', sort=False),
                   stack=cat(columns='event', sort=False),
                   color=color(columns='event', palette=Spectral9, sort=False),
                   xgrid=False, ygrid=False,
                   plot_width=1000, plot_height=625,
                   tools="pan,wheel_zoom,box_zoom,reset,resize")

    barchart.title = "Formula SAE Michigan - " + team

    barchart._xaxis.axis_label = "Teams"
    barchart._xaxis.axis_line_color = None
    barchart._xaxis.major_tick_line_color = None
    barchart._xaxis.minor_tick_line_color = None

    barchart._yaxis.axis_label = "Total Score"
    barchart._yaxis.axis_line_color = None
    barchart._yaxis.major_tick_line_color = None
    barchart._yaxis.minor_tick_line_color = None

    barchart.outline_line_color = None

    barchart.toolbar_location = 'right'

    barchart.logo = None

    # Hacky tooltips
    for renderer in barchart.select(GlyphRenderer):
        if renderer.data_source.data['height'] != [0]:
            year = renderer.data_source.data['Year']
            place = data['Place'].loc[data['Year'] == year]
            score = data['Total Score'].loc[data['Year'] == year]
            hover = HoverTool(renderers=[renderer],
                              tooltips=[("Year", '@Year'),
                                        ("Selection", '@event'),
                                        ("Event Score", '@height'),
                                        ("Total Score", '%.2f' % score.values[0]),
                                        ("Overall Place", '%d' % place.values[0])])
            barchart.add_tools(hover)

    return barchart
Exemple #12
0
    def createBokehChart(self):
        data = self.getWorkingPandasDataFrame()
        keyFields = self.getKeyFields()
        valueFields = self.getValueFields()

        clusterby = self.options.get("clusterby")
        stacked = self.options.get("charttype", "grouped") == "stacked"
        subplots = self.isSubplot()

        # self.debug("keyF={0}, valueF={1}, cluster={2}, stacked={3}".format(keyFields[0], valueFields[0], clusterby, stacked))
        charts = []
        params = []

        if subplots and len(valueFields) > 1:
            for val in valueFields:
                series = clusterby if clusterby is not None else False
                values = val
                params.append((values, series))
        elif clusterby is not None and len(valueFields) <= 1:
            params.append((valueFields[0], clusterby))
        else:
            series = '_'.join(valueFields)
            values = blend(*valueFields,
                           name=series.replace('_', ','),
                           labels_name=series)
            if clusterby is not None:
                self.addMessage(
                    "Warning: 'Cluster By' ignored when you have multiple Value Fields but subplots option is not selected"
                )
            params.append((values, series))

        for p in params:
            if stacked:
                b = Bar(data,
                        label=keyFields[0],
                        values=p[0],
                        stack=p[1],
                        legend=self.showLegend())
            else:
                b = Bar(data,
                        label=keyFields[0],
                        values=p[0],
                        group=p[1],
                        legend=self.showLegend())

            charts.append(b)

        return charts
def plot_stacked_bar_chart(df):
    bar = Bar(df,
              values=blend('Murder',
                           'Rape',
                           'Robbery',
                           'Aggravated Assault',
                           'Burglary',
                           'Larceny Theft',
                           'Motor Vehicle Theft',
                           name='Crime',
                           labels_name='crime'),
              label=cat(columns='State', sort=False),
              stack=cat(columns='crime', sort=False),
              legend='top_right',
              title="Crime per state",
              width=1000,
              height=500,
              tooltips=[('crime', '@crime'), ('state', '@State')])
    #output_file("stacked_bar.html", title="stacked_bar.py example")
    show(bar)
Exemple #14
0
def plot_opt(name):
    with open('{}.txt'.format(name), 'r') as f:
        data = f.read()
    N = int(re.findall('\[I(\d*) \d*\]\n.* .* .*', data)[0])
    B = int(re.findall('\[I\d* (\d*)\]\n.* .* .*', data)[0])
    TP = ['with', 'without']
    T1 = [float(x) for x in re.findall('\[I\d* \d*\]\n(.*) .* .*', data)]
    T2 = [float(x) for x in re.findall('\[I\d* \d*\]\n.* (.*) .*', data)]
    T3 = [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* (.*)', data)]
    T1 += [
        float(x)
        for x in re.findall('\[I\d* \d*\]\n.* .* .*\n(.*) .* .*', data)
    ]
    T2 += [
        float(x)
        for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* (.*) .*', data)
    ]
    T3 += [
        float(x)
        for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* .* (.*)', data)
    ]
    df = pd.DataFrame({
        'type': TP,
        'computation': T1,
        'communication': T2,
        'memcpy': T3
    })
    bar = Bar(df,
              label=['type'],
              values=blend('computation',
                           'communication',
                           'memcpy',
                           name='times',
                           labels_name='time'),
              xlabel='comparison',
              ylabel='total time(s)',
              stack=cat(columns=['time'], sort=False),
              title='{}'.format(name))
    output_file('{}.html'.format(name), title='N={}, B={}'.format(N, B))
    save(bar)
Exemple #15
0
def stack_bokeh(infile):
    with open(infile, "r") as file:
        data = pd.read_csv(file)
    print(data)

    p = Bar(data,
            values=blend('locus dropout',
                         'allelic dropout',
                         'biallelic coverage',
                         name='samp',
                         labels_name="samps"),
            label=cat(columns='Sample', sort=False),
            stack=cat(columns='samps', sort=False),
            color=color(columns="samps",
                        palette=["green", "red", "black"],
                        sort=False),
            legend='top_right',
            title="sam")

    output_file("stacked_bar.html")
    curdoc().add_root(p)
    show(p)
Exemple #16
0
def plot_weak(name):
    with open('{}.tot'.format(name), 'r') as f:
        data = f.read()
    Ns = [int(x) for x in re.findall('\[I(\d*) \d*\]\n.* .* .*', data)]
    Bs = [int(x) for x in re.findall('\[I\d* (\d*)\]\n.* .* .*', data)]
    T1s = [float(x) for x in re.findall('\[I\d* \d*\]\n(.*) .* .*', data)]
    T2s = [float(x) for x in re.findall('\[I\d* \d*\]\n.* (.*) .*', data)]
    T3s = [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* (.*)', data)]
    """
    if 'cuda' not in name:
        Ns += Ns
        Bs += Bs
        T1s += [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n(.*) .* .*', data)]
        T2s += [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* (.*) .*', data)]
        T3s += [float(x) for x in re.findall('\[I\d* \d*\]\n.* .* .*\n.* .* (.*)', data)]
    """
    df = pd.DataFrame({
        'N': Ns,
        'B': Bs,
        'computation': T1s,
        'communication': T2s,
        'memcpy': T3s
    })
    bar = Bar(df,
              label=['N', 'B'],
              values=blend('computation',
                           'communication',
                           'memcpy',
                           name='times',
                           labels_name='time'),
              xlabel='#node',
              ylabel='total time(s)',
              stack=cat(columns=['time'], sort=False),
              title='Weak Scalability')
    output_file('weak_{}.html'.format(name))
    save(bar)
Exemple #17
0
def generate_chart(year):
    data = get_data(year)

    barchart = Bar(data,
                   values=blend(*SCORED_EVENTS, labels_name='event'),
                   label=cat(columns='Team', sort=False),
                   stack=cat(columns='event', sort=False),
                   color=color(columns='event', palette=Spectral9, sort=False),
                   xgrid=False,
                   ygrid=False,
                   legend='top_right',
                   plot_width=1000,
                   plot_height=625,
                   tools="pan,wheel_zoom,box_zoom,reset,resize")

    barchart.title = "Formula SAE Michigan " + str(
        year) + " Total Scores by Place"

    barchart._xaxis.axis_label = "Teams"
    barchart._xaxis.axis_line_color = None
    barchart._xaxis.major_tick_line_color = None
    barchart._xaxis.minor_tick_line_color = None
    barchart._xaxis.major_label_text_font_size = '0.6em'

    barchart._yaxis.axis_label = "Total Score"
    barchart._yaxis.axis_line_color = None
    barchart._yaxis.major_tick_line_color = None
    barchart._yaxis.minor_tick_line_color = None

    barchart.outline_line_color = None

    barchart.toolbar_location = 'right'

    barchart.logo = None

    return barchart
Exemple #18
0
from bokeh.charts import Bar, output_file, show
from bokeh.charts.operations import blend
from bokeh.charts.attributes import cat, color
from bokeh.charts.utils import df_from_json
from bokeh.sampledata.olympics2014 import data

# utilize utility to make it easy to get json/dict data converted to a dataframe
df = df_from_json(data)

# filter by countries with at least one medal and sort by total medals
df = df[df['total'] > 0]
df = df.sort("total", ascending=False)

bar = Bar(df,
          values=blend('bronze',
                       'silver',
                       'gold',
                       name='medals',
                       labels_name='medal'),
          label=cat(columns='abbr', sort=False),
          stack=cat(columns='medal', sort=False),
          color=color(columns='medal',
                      palette=['SaddleBrown', 'Silver', 'Goldenrod'],
                      sort=False),
          legend='top_right',
          title="Medals per Country, Sorted by Total Medals",
          tooltips=[('medal', '@medal'), ('country', '@abbr')])

output_file("stacked_bar.html")
show(bar)
Exemple #19
0
from bokeh.charts import Bar, output_file, show
from bokeh.charts.attributes import cat, color
from bokeh.charts.operations import blend
from bokeh.charts.utils import df_from_json
from bokeh.sampledata.olympics2014 import data

# utilize utility to make it easy to get json/dict data converted to a dataframe
df = df_from_json(data)

# filter by countries with at least one medal and sort by total medals
df = df[df['total'] > 0]
df = df.sort_values(by="total", ascending=False)

bar = Bar(df,
          values=blend('bronze', 'silver', 'gold', name='medals', labels_name='medal'),
          label=cat(columns='abbr', sort=False),
          stack=cat(columns='medal', sort=False),
          color=color(columns='medal', palette=['SaddleBrown', 'Silver', 'Goldenrod'],
                      sort=False),
          legend='top_right',
          title="Medals per Country, Sorted by Total Medals",
          tooltips=[('medal', '@medal'), ('country', '@abbr')])


output_file("stacked_bar.html", title="stacked_bar.py example")

show(bar)
Exemple #20
0
def create_bar(data):
    op = blend(*countries, labels_name='countries', name='energy')
    return Bar(data, label='year', values=op, color='countries', group='countries',
            width=1400, height=600, responsive = False, ylabel='Energy use per capita',
            palette=['purple', 'green', 'blue', 'pink'],
            legend=True)
Exemple #21
0
""" This example uses the Iris data to demonstrate the specification of
combined variables using chart operations.

This specific instance uses a blend, which stacks columns, and renames
the combined column. This can be used where the column itself is a type
of categorical variable. Here, length and width are derived from the
petal and sepal measurements.
"""

from bokeh.charts import Scatter, output_file, show
from bokeh.charts.operations import blend
from bokeh.sampledata.iris import flowers as data

scatter = Scatter(data,
                  x=blend('petal_length', 'sepal_length', name='length'),
                  y=blend('petal_width', 'sepal_width', name='width'),
                  color='species',
                  title='x=petal_length+sepal_length, y=petal_width+sepal_width, color=species',
                  legend='top_right')

output_file("iris_blend.html")

show(scatter)
Exemple #22
0
from bokeh.charts import Bar, output_file, show
from bokeh.charts.attributes import cat, color
from bokeh.charts.operations import blend
from bokeh.charts.utils import df_from_json
from bokeh.sampledata.olympics2014 import data

# utilize utility to make it easy to get json/dict data converted to a dataframe
df = df_from_json(data)

# filter by countries with at least one medal and sort by total medals
df = df[df["total"] > 0]
df = df.sort("total", ascending=False)

bar = Bar(
    df,
    values=blend("bronze", "silver", "gold", name="medals", labels_name="medal"),
    label=cat(columns="abbr", sort=False),
    stack=cat(columns="medal", sort=False),
    color=color(columns="medal", palette=["SaddleBrown", "Silver", "Goldenrod"], sort=False),
    legend="top_right",
    title="Medals per Country, Sorted by Total Medals",
    tooltips=[("medal", "@medal"), ("country", "@abbr")],
)


output_file("stacked_bar.html")

show(bar)
Exemple #23
0
def portalDynamicity(df):

    def getWeekString(yearweek):
        if yearweek is None or len(str(yearweek)) == 0:
            return ''
        year = "'" + str(yearweek)[:2]
        week = int(str(yearweek)[2:])
        # d = d - timedelta(d.weekday())
        # dd=(week)*7
        # dlt = timedelta(days = dd)
        # first= d + dlt

        # dlt = timedelta(days = (week)*7)
        # last= d + dlt + timedelta(days=6)

        return 'W' + str(week) + '-' + str(year)
    bp = figure(plot_width=600, plot_height=300, y_axis_type="datetime", responsive=True,
                tools='')  # ,toolbar_location=None
    bp.toolbar.logo = None
    bp.toolbar_location = None
    label_dict={}
    for i, s in enumerate(df['snapshot']):
        label_dict[i] = getWeekString1(s)

    bp.yaxis[0].formatter = NumeralTickFormatter(format="0.0%")
    bp.xaxis[0].axis_label = 'Snapshots'
    bp.yaxis[0].axis_label = '% of portals'

    li = bp.line(df.index.values.tolist(), df['dyratio'], line_width=2, line_color='red', legend="dyratio")
    c = bp.circle(df.index.values.tolist(), df['dyratio'], line_width=2, line_color='red', legend="dyratio")
    li1 = bp.line(df.index.values.tolist(), df['adddelratio'], line_width=2, line_color='blue', legend="adddelratio")
    c = bp.circle(df.index.values.tolist(), df['adddelratio'], line_width=2, line_color='blue', legend="adddelratio")
    legend = bp.legend[0].legends
    bp.legend[0].legends = []
    l = Legend(location=(0, -30))
    l.items = legend
    bp.add_layout(l, 'right')



    labels=["staticRatio","updatedRatio","addRatio","delRatio"]
    #for l in labels:
    #    df[l]= df[l]*100
    print brewer.keys()
    colors = brewer["Pastel2"][len(labels)]
    bar = Bar(df,
              values=blend("staticRatio","updatedRatio","addRatio","delRatio", name='medals', labels_name='medal'),
              label=cat(columns='snapshot', sort=False),
              stack=cat(columns='medal', sort=False),
              color=color(columns='medal', palette=colors,
                          sort=False),
              legend='top_right',
              bar_width=0.5, responsive=True,
              tooltips=[('ratio', '@medal'), ('snapshot', '@snapshot'),('Value of Total',' @height{0.00%}')])
    legend = bar.legend[0].legends
    bar.legend[0].legends = []
    l = Legend(location=(0, -30))
    l.items = legend
    bar.add_layout(l, 'right')
    bar.xaxis[0].axis_label = 'Snapshots'
    bar.yaxis[0].axis_label = '% of datasets'
    bar.width=600
    bar.height=300
    bar.xaxis[0].formatter = FuncTickFormatter.from_py_func(getWeekStringTick)
    bar.toolbar.logo = None
    bar.toolbar_location = None

    bar.yaxis[0].formatter = NumeralTickFormatter(format="0.0%")
    return {'bar':bar,'lines':bp}
Exemple #24
0
scatter4 = Scatter(
    df, x='mpg', y='hp', color='cyl', marker='origin', title="x='mpg', y='hp', color='cyl', marker='origin'",
    xlabel="Miles Per Gallon", ylabel="Horsepower", legend='top_right')

# Example with nested json/dict like data, which has been pre-aggregated and pivoted
df2 = df_from_json(data)
df2 = df2.sort('total', ascending=False)

df2 = df2.head(10)
df2 = pd.melt(df2, id_vars=['abbr', 'name'])

scatter5 = Scatter(
    df2, x='value', y='name', color='variable', title="x='value', y='name', color='variable'",
    xlabel="Medals", ylabel="Top 10 Countries", legend='bottom_right')


scatter6 = Scatter(flowers, x=blend('petal_length', 'sepal_length', name='length'),
                   y=blend('petal_width', 'sepal_width', name='width'), color='species',
                   title='x=petal_length+sepal_length, y=petal_width+sepal_width, color=species',
                   legend='top_right')

output_file("scatter_multi.html")

show(vplot(
    hplot(scatter0, scatter1),
    hplot(scatter2, scatter3),
    hplot(scatter4, scatter5),
    hplot(scatter6)
))
Exemple #25
0
    def createBokehChart(self):
        keyFields = self.getKeyFields()
        valueFields = self.getValueFields()
        clusterby = self.options.get("clusterby")
        stacked = self.options.get("charttype", "grouped") == "stacked"
        subplots = self.isSubplot()
        workingPDF = self.getWorkingPandasDataFrame().copy()

        def convertPDFDate(df, col):
            #Bokeh doesn't support datetime as index in Bar chart. Convert to String
            if len(keyFields) == 1:
                dtype = df[col].dtype.type if col in df else None
                if numpy.issubdtype(dtype, numpy.datetime64):
                    dateFormat = self.options.get("dateFormat", None)
                    try:
                        df[col] = df[col].apply(lambda x: str(x).replace(':','-') if dateFormat is None else x.strftime(dateFormat))
                    except:
                        self.exception("Error converting dateFormat {}".format(dateFormat))
                        df[col] = df[col].apply(lambda x: str(x).replace(':','-'))

        for index, row in workingPDF.iterrows():
            for k in keyFields:
                if isinstance(row[k], str if sys.version >= '3' else basestring):
                    row[k] = row[k].replace(':', '.')
            workingPDF.loc[index] = row

        charts=[]
        def goChart(label, stack_or_group, values, ylabel=None, color=None):
            convertPDFDate(workingPDF, keyFields[0])
            if ylabel is None:
                ylabel=values
            label=label if isinstance(label, (list, tuple)) else [label]
            if stacked:
                charts.append( Bar(workingPDF, label=CatAttr(columns=label, sort=False), stack=stack_or_group, color=color, values=values, legend=self.showLegend(), ylabel=ylabel))
            else:
                charts.append( Bar(workingPDF, label=CatAttr(columns=label, sort=False), group=stack_or_group, color=color, values=values, legend=self.showLegend(), ylabel=ylabel))

        if clusterby is not None and (subplots or len(valueFields)<=1):
            subplots = subplots if len(valueFields)==1 or subplots else False
            if subplots:
                for j, valueField in enumerate(valueFields):
                    pivot = workingPDF.pivot(
                        index=keyFields[0], columns=clusterby, values=valueField
                    )
                    for i,col in enumerate(pivot.columns[:10]): #max 10
                        data = pd.DataFrame({'values':pivot[col].values, 'names': pivot.index.values})
                        convertPDFDate(data, 'names')
                        if subplots:                        
                            charts.append( 
                                Bar(data, label=CatAttr(columns=['names'], sort=False), color = Colors.hexRGB( 1.*i/2 ), values='values', ylabel=valueField, legend=False, 
                                    title="{0} = {1}".format(clusterby, pivot.columns[i])
                                )
                            )
            else:
                goChart( keyFields[0], clusterby, valueFields[0])
        else:
            if subplots:
                for i,valueField in enumerate(valueFields):
                    goChart( keyFields[0], None, valueField, color=Colors.hexRGB( 1.*i/2 ))
            else:
                if len(valueFields) > 1:
                    series = '_'.join(valueFields)
                    values = blend(*valueFields, name=series.replace('_', ','), labels_name=series)
                else:
                    series = False
                    values = valueFields[0]
                goChart(keyFields, series, values, ylabel=','.join(valueFields))

            if clusterby is not None:
                self.addMessage("Warning: 'Cluster By' ignored when grouped option with multiple Value Fields is selected")
        return charts
Exemple #26
0
""" This example uses the Iris data to demonstrate the specification of
combined variables using chart operations.

This specific instance uses a blend, which stacks columns, and renames
the combined column. This can be used where the column itself is a type
of categorical variable. Here, length and width are derived from the
petal and sepal measurements.
"""

from bokeh.charts import Scatter, output_file, show
from bokeh.charts.operations import blend
from bokeh.sampledata.iris import flowers as data

scatter = Scatter(
    data,
    x=blend('petal_length', 'sepal_length', name='length'),
    y=blend('petal_width', 'sepal_width', name='width'),
    color='species',
    title=
    'x=petal_length+sepal_length, y=petal_width+sepal_width, color=species',
    legend='top_right')

output_file("iris_blend.html", title="iris_blend.py example")

show(scatter)
Exemple #27
0
scatter3 = Scatter(
    df, x='mpg', y='hp', color='origin', title="x='mpg', y='hp', color='origin', with tooltips",
    xlabel="Miles Per Gallon", ylabel="Horsepower",
    legend='top_right', tooltips=[('origin', "@origin")])

scatter4 = Scatter(
    df, x='mpg', y='hp', color='cyl', marker='origin', title="x='mpg', y='hp', color='cyl', marker='origin'",
    xlabel="Miles Per Gallon", ylabel="Horsepower", legend='top_right')

# Example with nested json/dict like data, which has been pre-aggregated and pivoted
df2 = df_from_json(data)
df2 = df2.sort('total', ascending=False)

df2 = df2.head(10)
df2 = pd.melt(df2, id_vars=['abbr', 'name'])

scatter5 = Scatter(
    df2, x='value', y='name', color='variable', title="x='value', y='name', color='variable'",
    xlabel="Medals", ylabel="Top 10 Countries", legend='bottom_right')

scatter6 = Scatter(flowers, x=blend('petal_length', 'sepal_length', name='length'),
                   y=blend('petal_width', 'sepal_width', name='width'), color='species',
                   title='x=petal_length+sepal_length, y=petal_width+sepal_width, color=species',
                   legend='top_right')

output_file("scatter_multi.html", title="scatter_multi.py example")

show(gridplot(scatter0,  scatter2, scatter3, scatter4,
              scatter5, scatter6, ncols=2))
Exemple #28
0
def output_components(prefix, order, maxaln):
    rinfo = "{0}.rinfo".format(prefix)
    comp = "{0}.comp".format(prefix)

    def plot_read_dist(rinfo):
        df = pd.read_table(rinfo)
        data = df[['length', 'mapper', 'count']].groupby(['length',
                                                          'mapper']).count()
        data = data.apply(lambda s: s / data.sum() * 100, axis=1).reset_index()
        p = Bar(data,
                plot_width=500,
                plot_height=400,
                label='length',
                values='count',
                agg='sum',
                stack=cat(columns='mapper', sort=False),
                legend='top_right',
                color=color(columns='mapper',
                            palette=["#f98283", "#a4a4a4"],
                            sort=False),
                xlabel='read length (nt)',
                ylabel='proportion (%)',
                ygrid=False,
                tooltips=[('length', '@length'), ('mapper', '@mapper'),
                          ('percent', '@height')])
        p.toolbar.logo = None
        p.outline_line_color = None
        return p

    rdist = plot_read_dist(rinfo)

    df = pd.read_table(comp, index_col=0)
    total = df.sum()
    total = total * 100 / total.sum()
    df = df.apply(lambda s: s * 100 / s.sum(), axis=1)
    df = df.reset_index()
    #ftypes = df.columns[1:].tolist()
    ftypes = order
    colors = sns.color_palette("hls", len(ftypes)).as_hex()
    bar = Bar(df,
              values=blend(*ftypes, name="ftypes", labels_name="ftype"),
              x_range=rdist.x_range,
              y_range=(0, 100),
              label=cat(columns='rlen', sort=False),
              stack=cat(columns='ftype', sort=False),
              xlabel='read length (nt)',
              ylabel='proportion (%)',
              legend="top_right",
              ygrid=False,
              width=500,
              height=400,
              color=color(columns='ftype', palette=colors, sort=False),
              fill_alpha=1,
              tooltips=[("length", "@rlen"), ("feature", "@ftype"),
                        ("percent", "@height")])
    bar.toolbar.logo = None
    bar.outline_line_color = None

    start_angles = {}
    end_angles = {}
    start = 0
    for ftype in ftypes:
        end = 2 * pi * total[ftype] / 100
        start_angles[ftype] = start
        end_angles[ftype] = start + end
        start += end

    colors = dict(zip(ftypes, colors))
    df = pd.DataFrame(total).reset_index()
    df.columns = ["ftype", "percent"]
    df["start"] = df.apply(lambda s: start_angles[s["ftype"]], axis=1)
    df["end"] = df.apply(lambda s: end_angles[s["ftype"]], axis=1)
    df["color"] = df.apply(lambda s: colors[s["ftype"]], axis=1)
    df["x"] = df.apply(lambda s: 1.2 * cos(
        (start_angles[s["ftype"]] + end_angles[s["ftype"]]) / 2),
                       axis=1)
    df["y"] = df.apply(lambda s: 1.2 * sin(
        (start_angles[s["ftype"]] + end_angles[s["ftype"]]) / 2),
                       axis=1)
    df["text"] = df.apply(lambda s: "{0:.3f}%".format(s["percent"]), axis=1)
    source = ColumnDataSource(data=df)

    pie = figure(width=400,
                 height=400,
                 x_range=(-1.4, 1.4),
                 y_range=(-1.4, 1.4))
    pie.toolbar.logo = None
    wr = pie.annular_wedge(x=0,
                           y=0,
                           inner_radius=0.5,
                           outer_radius=1,
                           start_angle="start",
                           end_angle="end",
                           fill_color="color",
                           line_color="#ffffff",
                           line_width=0.5,
                           source=source)

    pie.axis.visible = False
    pie.grid.grid_line_color = None
    pie.outline_line_color = None

    hover = HoverTool(tooltips=[("feature", "@ftype"),
                                ("percent", "@percent")],
                      renderers=[wr])
    pie.add_tools(hover)

    text_props = {
        "source": source,
        "angle": 0,
        "color": "black",
        "text_align": "center",
        "text_baseline": "middle",
        "text_font_size": "8pt",
        "text_font_style": "normal"
    }
    pie.text(x="x", y="y", text="text", **text_props)

    empty = figure(width=400,
                   height=400,
                   x_range=(-1.1, 1.1),
                   y_range=(-1.1, 1.1))
    empty.toolbar.logo = None
    empty.axis.visible = False
    empty.grid.grid_line_color = None
    empty.outline_line_color = None
    empty.toolbar_location = None

    stat = get_stat(prefix, maxaln)
    source = ColumnDataSource(data=stat)
    columns = [
        TableColumn(field="Statistics", title="Statistics", width=200),
        TableColumn(field="Number of reads",
                    title="Number of reads",
                    formatter=NumberFormatter(format="0,0"),
                    width=150),
        TableColumn(field="Proportion",
                    title="Proportion",
                    formatter=NumberFormatter(format="0.000%"),
                    width=100),
    ]
    data_table = DataTable(source=source,
                           columns=columns,
                           width=450,
                           row_headers=False)

    script, div = components(
        layout([[data_table, rdist], [pie, bar]], sizing_mode="scale_width"))
    return script, div
Exemple #29
0
    data['ACCEPTABLE'] = []
    data['UNACCEPTABLE'] = []
    data['Date'] = []
    for m in range(len(stat)):
        data['ACCEPTABLE'].append(stat[m][0])
        data['UNACCEPTABLE'].append(stat[m][1] + stat[m][2])
        data['Date'].append(unique_dates[m])

    data['Total'] = [
        data['ACCEPTABLE'][k] + data['UNACCEPTABLE'][k]
        for k in range(len(data['Date']))
    ]
    df = pd.DataFrame(data)
    bar = Bar(df,
              values=blend('ACCEPTABLE',
                           'UNACCEPTABLE',
                           name='Hours',
                           labels_name='medal'),
              label=cat(columns='Date', sort=False),
              stack=cat(columns='medal', sort=False),
              color=color(columns='medal',
                          palette=['Green', 'Red'],
                          sort=False),
              legend='top_left',
              title="Respiration data quality of participant id " +
              str(ids[i]),
              width=800)

    output_file('Respiration_DataQuality_of_' + str(ids[i]) + ".html",
                title="RIP_BAR_Plot")

    source = ColumnDataSource(data)
# print(days)

# Put data into dataframe broken into min, mean, and max values each for month
ts = Series(vals, index=days[0])
firstmerge = pd.merge(ts.resample('M').min().to_frame(name="min"),
                      ts.resample('M').mean().to_frame(name="mean"),
                      left_index=True, right_index=True)
frame = pd.merge(firstmerge, ts.resample('M').max().to_frame(name="max"),
                 left_index=True, right_index=True)

# You can use DataFrame index for bokeh x values but it doesn't like timestamp
frame['Month'] = frame.index.strftime('%m-%Y')

# Main object to render with stacking
bar = Bar(frame,
          values=blend('min', 'mean', 'max',
                       name='values', labels_name='stats'),
          label=cat(columns='Month', sort=False),
          stack=cat(columns='values', sort=False),
          color=color(columns='values',
                      palette=['SaddleBrown', 'Silver', 'Goldenrod'],
                      sort=True),
          legend=None,
          title="Statistical Values Grouped by Month",
          tooltips=[('Value', '@values')]
          )

# Legend info (if legend attribute is used it gets ugly with large dataset)
factors = ["min", "mean", "max"]
x = [0] * len(factors)
y = factors
pal = ['SaddleBrown', 'Silver', 'Goldenrod']