Exemplo n.º 1
0
def hotmap(datetime, room, query):
    """Generates a heatmap bokeh model"""

    #IMPORTANT: this query returns M-F values

    # get a df with date and count values, for a given room and date
    week_counts = pd.read_sql_query(query.format(date=datetime, room=room),
                                    connectDB())

    headcount = list(week_counts['count'])
    headcount = [linear_predictor(x)
                 for x in headcount]  # predict on all supplied values

    bins = list(week_counts['count'])
    # bins = [tertiary_classifier(x) for x in bins] # predict on all supplied values
    tert_dictionary = {
        'Empty': 0,
        'Medium': 0.5,
        'High': 1
    }  # map to int as only ints can go in heatmap
    # bins = [tert_dictionary[x] for x in bins] # map with listcomp

    # 5 * 9 = 45, so all elements need to have 45 values. Days and hours are just multipled out.
    data = {
        'days':
        ['fri'] * 9 + ['thu'] * 9 + ['wed'] * 9 + ['tue'] * 9 + ['mon'] * 9,
        'occupancy': headcount,
        'hours': ['9', '10', '11', '12', '13', '14', '15', '16', '17'] * 5
    }

    week_counts['headcount'] = week_counts['count'].apply(linear_predictor)
    week_counts['bins'] = week_counts['count'].apply(tertiary_classifier)

    source = ColumnDataSource(ColumnDataSource.from_df(week_counts))

    # hover = HoverTool()
    #
    # tooltips = [("headcount", "@headcount")]
    #
    # tools = [hover]

    # colors = ['#1abc9c', '#ec583a', '#474e5d'] # set colors; currently not shading on continuous features

    hm = HeatMap(data,
                 x='hours',
                 y='days',
                 values='occupancy',
                 title='Occupancy in room over the week'.format(room),
                 stat=None,
                 palette=palette,
                 tools=None,
                 hover_tool=True)

    legend = Legend(location=(0, -30))

    hm.add_layout(legend, 'right')

    all_plots = gridplot([[hm]])

    return all_plots
Exemplo n.º 2
0
def heat_mapping():
	
	data = {'fruit': ['apples']*1 + ['bananas']*3 + ['pears']*3, 'fruit_count': [4, 5, 8, 1, 2, 4, 6, 5, 4], 'sample': [1, 2, 3]*3}
	hm = HeatMap(data, x='fruit', y='sample', values='fruit_count', title='Fruits', stat=None)
	output_file('heatmap.html')
	curdoc().add_root(hm)
	show(hm)
Exemplo n.º 3
0
def plot_occupancy(h5_file_name):
    with tb.open_file(h5_file_name, 'r') as in_file_h5:
        hit_data = in_file_h5.root.hit_data[:]

        #H, _, _ = np.histogram2d(hit_data['col'], hit_data['row'], bins = (range(65), range(65)))
        #grid = np.indices(H.shape)
        #data = {'column': np.ravel(grid[0]),
        #        'row': np.ravel(grid[1]),
        #        'value': np.ravel(H)
        #       }
        #col = np.ravel(grid[0])
        #row = np.ravel(grid[1])
        #value = np.ravel(H)
        
        hits = hit_data['col'].astype(np.uint16)
        hits = hits * 64
        hits = hits + hit_data['row']
        value = np.bincount(hits)
        value = np.pad(value, (0, 64*64 - value.shape[0]), 'constant')
       
        indices = np.indices(value.shape)
        col = indices[0] / 64
        row = indices[0] % 64
        
        data = {'column': col,
                'row': row,
                'value': value
               }
                
        hm = HeatMap(data, x='column', y='row', values='value', legend='top_right', title='Occupancy', palette=RdYlGn6[::-1], stat=None)
        
    return hm, value.reshape((64, 64))
Exemplo n.º 4
0
def create_heatmap(df):

    products=['Debit Card',
              'Personal Credit Card',
              'Business Credit Card',
              'Home Mortgage Loan',
              'Auto Loan',
              'Brokerage Account',
              'Roth IRA',
              '401k',
              'Home Insurance',
              'Automobile Insurance',
              'Medical Insurance',
              'Life Insurance',
              'Cell Phone',
              'Landline'
              ]

    def rename_columns(df):
        df = df.copy()
        df.columns = [products[i] for i in df.columns]
        return df

    # create an artificial dataset with 3 clusters
    X, Y = make_classification(n_samples=100, n_classes=4, n_features=12, n_redundant=0, n_informative=12,
                               scale=1000, n_clusters_per_class=1)
    df2 = pd.DataFrame(X)
    # ensure all values are positive (this is needed for our customer 360 use-case)
    df2 = df2.abs()
    # rename X columns
    df2 = rename_columns(df2)
    # and add the Y
    df2['y'] = Y
    #df
    # split df into cluster groups
    grouped = df2.groupby(['y'], sort=True)

    # compute sums for every column in every group
    sums = grouped.sum()

    score = []
    for x in sums.apply(tuple):
        score.extend(x)

    data=dict(
        persona=list(sums.index) * len(sums.columns),
        product=[item for item in list(sums.columns) for i in range(len(sums.index))],
        score=score
    )

    hm = HeatMap(data, x='product', y='persona', values='score', title='Customer Profiles', xlabel='Product', ylabel='Persona', legend=False, stat=None,  tools=["save"], height=400, width=900, toolbar_location=None)
    hm.yaxis.ticker=FixedTicker(ticks=[0,1,2,3])
    hm_source = ColumnDataSource(data=sums)

    hm_data_table = DataTable(
        source=hm_source,
        columns=[TableColumn(field=c, title=c) for c in sums.columns], width=900, height=150)

    return hm
def prepare_and_draw_matrix(dh_mat2, heading_list, disc_list, outfile):
    
    totals = {}
    for d in disc_list:
        total = 0
        for h in heading_list:
            if( dh_mat2[h].get(d, None) is not None):
                total += dh_mat2[h][d]
        totals[d] = total 
    
    section = []
    d_type = []
    percent = []
    for d in disc_list:
        for h in heading_list:
            if( dh_mat2[h].get(d, None) is None):
                section.append(h)
                d_type.append(d)
                percent.append(0.0)
            else : 
                section.append(h)
                d_type.append(d)
                percent.append(100.0 * dh_mat2[h][d] / totals[d])
        
    data = {'Section': section,
            'Discourse Type': d_type,
             'Percentage': percent
            }
    
    color = ColorAttr(bin=True, palette=gray(6), sort=True, ascending=False)

    hm = HeatMap(data, x='Discourse Type', y='Section', values='Percentage',
                stat=None, plot_height=260, legend=False, color=color)
    
    output_file(outfile+'1.html', mode='cdn', root_dir=None)
    save(hm)

    hm1 = HeatMap(data, x='Discourse Type', y='Section', values='Percentage',
                stat=None, plot_height=260, legend=True, color=color)
    
    output_file(outfile+'2.html', mode='cdn', root_dir=None)
    save(hm1)

     
    '''
Exemplo n.º 6
0
    def plot_heatmap(self, identifier, enso, chart, average_table):
        chart_title = chart + " Heatmap"
        removed_total = average_table
        if (identifier != "both"):
            removed_total = np.delete(average_table, self.NUM_SOURCES, 1)
        if (identifier == "both"):
            removed_total = np.delete(average_table, self.NUM_REGIONS, 1)
        formatted_data = None
        height = 900
        x_label = identifier
        y_label = "Source"
        filename = identifier
        if (identifier == "regions"):
            formatted_data = pandas.DataFrame(data=removed_total,
                                              index=self.basic_sources,
                                              columns=self.regions)
            height = 600
            width = 1400
            x_label = "Regions"
        if (identifier == "species"):
            formatted_data = pandas.DataFrame(data=removed_total,
                                              index=self.species,
                                              columns=self.basic_sources)
            width = 600
            x_label = "Species"
        if (identifier == "both"):
            formatted_data = pandas.DataFrame(data=removed_total,
                                              index=self.species,
                                              columns=self.regions)
            height = 900
            width = 1400
            x_label = "Regions"
            y_label = "Species"
            filename += "_combined"
        palette = red_palette[::
                              -1]  # Reverse the color order so dark red is highest
        if (enso):
            palette = red_blue_palette
            filename += "_ENSO"
        output_file("plots/tables/" + chart + "/plots/" + filename +
                    "_heatmap.html",
                    title=chart_title)

        p = HeatMap(formatted_data,
                    width=width,
                    height=height,
                    title=chart_title,
                    xlabel=x_label,
                    ylabel="Source",
                    palette=palette)
        save(p)
Exemplo n.º 7
0
def heatmap_helper(markgwas,heat_title):
  from bokeh.palettes import RdYlGn8
  markgwas = markgwas[(markgwas.chromosome != "X") & (markgwas.chromosome != "Y") & (markgwas.chromosome != "MT")]
  markgwas.chromosome = markgwas.chromosome.astype(float).fillna(0.0)
  markgwas['bp_rounded'] = markgwas.position.astype(float).fillna(-10000000) // 1000000

  chrom = markgwas.chromosome.values
  position = markgwas.bp_rounded.values
  risk = markgwas.risk.values

  TOOLS = "resize,hover,save,pan,box_zoom,wheel_zoom"
  # http://bokeh.pydata.org/en/latest/docs/gallery/heatmap_chart.html
  outs = {'Base Pair Position (in millions)': position, 'Chromosome': chrom, 'Increased Odds': risk}
  
  
  hm = HeatMap(outs, y='Chromosome', x='Base Pair Position (in millions)', values='Increased Odds', stat='mean', tools=TOOLS, 
               title=heat_title, plot_width=900, plot_height=400,palette=RdYlGn8[::-1],legend='top_right',
               ygrid=True, xgrid=True)
  
  # hacky way of getting around single-snp showing red with low lactose intolerance risk             
  if (heat_title == "Lactose Intolerance Increased Odds Heatmap") & (risk[0] < 30):
      print "HACKY SOLUTION"
      hm = HeatMap(outs, y='Chromosome', x='Base Pair Position (in millions)', values='Increased Odds', stat='mean', tools=TOOLS, 
               title=heat_title, plot_width=900, plot_height=400,palette=RdYlGn8,legend='top_right',
               ygrid=True, xgrid=True)
    
  hm.y_range.start=0
  hm.y_range.end=23
  hm.x_range.start=-1
  hm.x_range.end=260
  hm.select_one(HoverTool).tooltips = [
      ('Chromosome', ' '+' @y '),
      ('Position', ' @x million'),
      ('Increased Odds', ' @values'),
  ]
  script, div = components(hm)
  return script, div
Exemplo n.º 8
0
    def heat_mapping(self,
                     title="heatmap",
                     outfile="blah_output.html",
                     rows=50):
        data = self.process_heat(rows=rows)

        hm = HeatMap(data,
                     title="Heatmap",
                     x="x",
                     y="y",
                     values="count",
                     stat=None)
        output_file(outfile)
        curdoc().add_root(hm)
        show(hm)
Exemplo n.º 9
0
def heatmap(heatMapDF,
            xlabel,
            ylabel,
            value_label,
            title="heatmap",
            palette=None,
            width=500,
            height=500,
            **kwargs):
    from bokeh.charts import HeatMap
    if not palette:
        from bokeh.palettes import RdBu11 as palette_tmpl
        palette = palette_tmpl
    hm = HeatMap(heatMapDF,
                 x=xlabel,
                 y=ylabel,
                 values=value_label,
                 title=title,
                 height=height,
                 width=width,
                 palette=palette,
                 **kwargs)
    return hm
Exemplo n.º 10
0
def plot_occupancy(h5_file_name):
    with tb.open_file(h5_file_name, 'r') as in_file_h5:
        hit_data = in_file_h5.root.hit_data[:]

        hits = hit_data['col'].astype(np.uint16)
        hits = hits * 64
        hits = hits + hit_data['row']
        value = np.bincount(hits)
        value = np.pad(value, (0, 64 * 64 - value.shape[0]), 'constant')

        indices = np.indices(value.shape)
        col = indices[0] / 64
        row = indices[0] % 64

        data = {'column': col,
                'row': row,
                'value': value
                }

        hm = HeatMap(data, x='column', y='row', values='value', legend='top_right', title='Occupancy',
                     palette=RdYlGn6[::-1], stat=None)

    return hm, value.reshape((64, 64))
Exemplo n.º 11
0
    def heatmap(self,
                dataframe,
                y=None,
                x=None,
                values=None,
                width=None,
                height=None,
                palette=None,
                max_color=None,
                min_color=None,
                mid_color=None,
                title='Heatmap',
                xaxis_label=None,
                yaxis_label=None):

        palette = self.__default_options__.get(
            'palette', None) if palette is None else palette
        width = self.__default_options__.get('width',
                                             None) if width is None else width

        width, height = self._width_height(width, height)

        heatmap = HeatMap(dataframe,
                          x=x,
                          y=y,
                          values=values,
                          width=width,
                          height=height,
                          palette=palette,
                          title=title)
        if xaxis_label:
            heatmap._xaxis.axis_label = xaxis_label
        if yaxis_label:
            heatmap._yaxis.axis_label = yaxis_label

        return heatmap
Exemplo n.º 12
0
	df_matrix =  pd.DataFrame(matrix)
	return df_matrix

prem_last = calcMatrixHeatMap(conversion_close)
prem_7day = calcMatrixHeatMap(rolling_7day)
prem_30day = calcMatrixHeatMap(rolling_30day)

from bokeh.layouts import row
from bokeh.plotting import figure, show, output_file
from bokeh.charts import HeatMap, bins, output_file, show
from bokeh.palettes import RdBu, magma
from bokeh.models import ColumnDataSource, LabelSet, Label

output_file("ccy-premium.html", title="BTC premia")

hm1 = HeatMap(prem_last, x='from', y='to', values='diff', title='Premia Last Closing (USD). diff = from - to, % = (from - to)/to', stat=None, palette=['green', 'gray', 'red'], legend=False)
source1 = ColumnDataSource(data=prem_last)
labels1a = LabelSet(x='from', y='to', text='formatted', level='glyph', x_offset=-15, y_offset=-10, render_mode='canvas', source=source1)
labels1b = LabelSet(x='from', y='to', text='percent', level='glyph', x_offset=-15, y_offset=-30, render_mode='canvas', source=source1)
hm1.add_layout(labels1a)
hm1.add_layout(labels1b)

hm2 = HeatMap(prem_7day, x='from', y='to', values='diff', title='Premia 7 days average (USD)', stat=None, palette=['green', 'gray', 'red'], legend=False)
source2 = ColumnDataSource(data=prem_7day)
labels2a = LabelSet(x='from', y='to', text='formatted', level='glyph', x_offset=-15, y_offset=-10, render_mode='canvas', source=source2)
labels2b = LabelSet(x='from', y='to', text='percent', level='glyph', x_offset=-15, y_offset=-30, render_mode='canvas', source=source2)
hm2.add_layout(labels2a)
hm2.add_layout(labels2b)

hm3 = HeatMap(prem_30day, x='from', y='to', values='diff', title='Premia 30 days average (USD)', stat=None, palette=['green', 'gray', 'red'], legend=False)
source3 = ColumnDataSource(data=prem_30day)
Exemplo n.º 13
0
## evaluate z
z_normal = np.load(os.path.join(DSC_DIR,
                                'z_' + NORMAL_NAME))[0].reshape(134 * 32, 64)
z_simple = np.load(os.path.join(DSC_DIR,
                                'z_' + SIMPLE_NAME))[0].reshape(134 * 32, 64)

select_0 = 100
output_file('heat_map_dsc.html')
sq_diff = ((z_normal - z_simple)[:select_0]**2).flatten()
y = np.array([list(range(z_normal.shape[1]))] * select_0).flatten()
x = np.array([[i] * z_normal.shape[1] for i in range(select_0)]).flatten()
pl = HeatMap({
    'values': sq_diff,
    'example': x,
    'z[i]': y
},
             y='z[i]',
             x='example',
             values='values',
             stat=None,
             palette=palettes.Spectral9)
pl.title.text_font_size = '18pt'
pl.legend.location = 'bottom_right'
pl.legend.label_text_font_size = '12pt'
pl.xaxis.major_label_text_font_size = '14pt'
pl.yaxis.major_label_text_font_size = '14pt'
pl.xaxis.axis_label_text_font_size = '14pt'
pl.yaxis.axis_label_text_font_size = '14pt'
show(pl)
output_file('normal_z_heat_map_dsc.html')
pl = HeatMap({
    'values': z_normal[:select_0].flatten(),
Exemplo n.º 14
0
from bokeh.charts import HeatMap, output_file, show

import pandas as pd

output_file('heatmap.html')

df = pd.DataFrame(
        dict(
            apples=[4, 5, 8],
            bananas=[1, 2, 4],
            pears=[6, 5, 4],
        ),
        index=['2012', '2013', '2014']
    )

p = HeatMap(df, title='Fruits')

show(p)
Exemplo n.º 15
0
fruits = {
    'fruit': [
        'apples', 'apples', 'apples', 'apples', 'apples', 'pears', 'pears',
        'pears', 'pears', 'pears', 'bananas', 'bananas', 'bananas', 'bananas',
        'bananas'
    ],
    'fruit_count': [4, 5, 8, 12, 4, 6, 5, 4, 8, 7, 1, 2, 4, 8, 12],
    'year': [
        '2009', '2010', '2011', '2012', '2013', '2009', '2010', '2011', '2012',
        '2013', '2009', '2010', '2011', '2012', '2013'
    ]
}

plot_cat_unbounded = HeatMap(fruits,
                             y='year',
                             x='fruit',
                             values='fruit_count',
                             stat=None,
                             title="Heatmap (unbounded)")

plot_cat_autobounded = HeatMap(fruits,
                               y='year',
                               x='fruit',
                               values='fruit_count',
                               stat=None,
                               title="Heatmap (autobounded)")
###### -- ranges set here -- ########
plot_cat_autobounded.x_range.bounds = 'auto'
plot_cat_autobounded.y_range.bounds = 'auto'
###### -- end -- ########

plot_cat_bounded = HeatMap(
Exemplo n.º 16
0
from collections import OrderedDict

from bokeh.sampledata.unemployment1948 import data
from bokeh.charts import HeatMap

# pandas magic
df = data[data.columns[:-2]]
df2 = df.set_index(df[df.columns[0]].astype(str))
df2.drop(df.columns[0], axis=1, inplace=True)
df3 = df2.transpose()

cols = df3.columns.tolist()
index = df3.index.tolist()

#prepare some inputs
to_odict = lambda v: OrderedDict((kk, v[kk]) for kk in index)

# Create an ordered dict (or ordered dicts) with the data from the DataFrame
datadict = df3.to_dict()
data = OrderedDict(sorted((k, to_odict(v)) for k, v in datadict.items()))

# any of the following commented line is a valid HeatMap input
#data = df3
#data = df3.values.T
#data = list(df3.values.T)

hm = HeatMap(data,
             title="categorical heatmap, pd_input",
             filename="cat_heatmap.html")
hm.width(1000).height(400).show()
Exemplo n.º 17
0
            var.split('*')[1].strip() for var in item[1].split('+')[:5]
        ]
        value_one = [
            var.split('*')[0].strip() for var in item[1].split('+')[:5]
        ]
        corpus_one = corpus
        for i, var in enumerate(index_one):
            corpus_one[var] = float(value_one[i])
        value_mt.append([int(1000 * var) for var in corpus_one.values()])

df = pd.DataFrame(value_mt)
topic_words = pd.DataFrame(corpus.keys())
df2 = df.transpose().set_index(topic_words[topic_words.columns[0]])
df3 = df2.transpose()
topic_label = pd.DataFrame(['topic_' + str(i) for i in range(topic_number)])
df3 = df3.set_index(topic_label[topic_label.columns[0]])

html_path = folder_name + 'Weight_Management.html'
output_file(html_path, title="Weight_Management topics")

palette = palette[::
                  -1]  # Reverse the color order so dark red is highest unemployment
hm = HeatMap(df3,
             title="Weight_Management Topics",
             tools="reset,resize",
             width=1300,
             height=700,
             palette=palette)

show(hm)
Exemplo n.º 18
0
from bokeh.charts import HeatMap
import pandas as pd


xl = pd.ExcelFile(u'Z:\Routines\CONTROLE\REGRESSION v03\AL-2013-2014.xls')
logbook = xl.parse(xl.sheet_names[0])
s2=logbook.groupby('Unit').Code.value_counts()

# pandas magic
df = data[data.columns[:-2]]
df2 = df.set_index(df[df.columns[0]].astype(str))
df2.drop(df.columns[0], axis=1,level=None)
df3 = df2.transpose()

cols = df3.columns.tolist()
index = df3.index.tolist()

#prepare some inputs
to_odict = lambda v: OrderedDict((kk, v[kk]) for kk in index)

# Create an ordered dict (or ordered dicts) with the data from the DataFrame
datadict = df3.to_dict()
data = OrderedDict(sorted((k, to_odict(v)) for k, v in datadict.items()))

# any of the following commented line is a valid HeatMap input
#data = df3
#data = df3.values.T
#data = list(df3.values.T)

hm = HeatMap(data, title="categorical heatmap", filename="cat_heatmap.html")
hm.width(1000).height(400).show()
prem_7day = calcMatrixHeatMap(rolling_7day)
prem_30day = calcMatrixHeatMap(rolling_30day)

from bokeh.layouts import row
from bokeh.plotting import figure, show, output_file
from bokeh.charts import HeatMap, bins, output_file, show
from bokeh.palettes import RdBu, magma
from bokeh.models import ColumnDataSource, LabelSet, Label

output_file("index.html", title="BTC premia")

dateToDisplay = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

custompalette = ['#00814c', '#1ebd7c', '#26cc88', '#3ee09e', '#71ffc5', 'grey','#fcffc8', '#ffeb66', '#ffca49', '#ff9600', '#d00031']

hm1 = HeatMap(prem_last, x='from', y='to', values='diff', title=dateToDisplay+' Premia Last Closing (USD). diff = from - to, % = (from - to)/to', stat=None, palette=custompalette, legend=False)
source1 = ColumnDataSource(data=prem_last)
#labels1a = LabelSet(x='from', y='to', text='formatted', level='glyph', x_offset=-10, y_offset=-10, render_mode='canvas', source=source1, text_font_size='9pt')
labels1b = LabelSet(x='from', y='to', text='percent', level='glyph', x_offset=-10, y_offset=-10, render_mode='canvas', source=source1, text_font_size='9pt')
#hm1.add_layout(labels1a)
hm1.add_layout(labels1b)

hm2 = HeatMap(prem_7day, x='from', y='to', values='diff', title=dateToDisplay+' Premia 7 days average (USD)', stat=None, palette=custompalette, legend=False)
source2 = ColumnDataSource(data=prem_7day)
#labels2a = LabelSet(x='from', y='to', text='formatted', level='glyph', x_offset=-10, y_offset=-10, render_mode='canvas', source=source2, text_font_size='9pt')
labels2b = LabelSet(x='from', y='to', text='percent', level='glyph', x_offset=-10, y_offset=-10, render_mode='canvas', source=source2, text_font_size='9pt')
#hm2.add_layout(labels2a)
hm2.add_layout(labels2b)

hm3 = HeatMap(prem_30day, x='from', y='to', values='diff', title=dateToDisplay+' Premia 30 days average (USD)', stat=None, palette=custompalette, legend=False)
source3 = ColumnDataSource(data=prem_30day)
Exemplo n.º 20
0
    def process_file(self, file_path):
        """
        Process the file given.  This read from the file
        and add it to the codec.  The codec will then decode
        the data and pass it to the UDP port.
        """
        # Check if the file exist
        if os.path.exists(file_path):

            logger.info("Open file: " + file_path)

            # Open the file
            f = open(file_path, "rb")

            # Add the data from the file to the codec
            data = f.read(4096)
            while len(data) > 0:
                # Add data to codec
                self.codec.add(data)

                # Read next block from the file
                data = f.read(4096)

            # Close the file
            f.close()

            # Display the Heatmap and save it to HTML file
            output_file('test.html')

            hover = HoverTool(tooltips=[
                ("index", "$index"),
                ("(x,y)", "($x, $y)"),
                ("desc", "@desc"),
            ])

            TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"

            hm = HeatMap(
                self.df_mag_ens,
                x='ens',
                y='bin',
                values='mag',
                stat=None,
                sort_dim={'x': False},
                width=1000,
                spacing_ratio=0.9,
                tools=TOOLS,
                toolbar_location="above",  # Move toolbar to top
                toolbar_sticky=False)  # Make toolbar not to close to plot

            # Set min and max axis and invert axis
            xmin = self.df_mag_ens['ens'].min()
            xmax = self.df_mag_ens['ens'].max()
            ymax = self.df_mag_ens['bin'].min()  # Swap Min and Max for y axis
            ymin = self.df_mag_ens['bin'].max()  # Swap Min and Max for y axis
            hm.x_range = Range1d(
                xmin, xmax)  # Set the min and max, so no gaps on edges
            hm.y_range = Range1d(
                ymin, ymax)  # Set the min and max, so no gaps on edges
            show(hm)

            while True:
                input("Press enter to continue")
                break

        else:
            logger.error("File does not exist")
df = pd.DataFrame(value_mt)
topic_words = pd.DataFrame(corpus.keys())
df2 = df.transpose().set_index(topic_words[topic_words.columns[0]])
df3 = df2.transpose()
topic_label = pd.DataFrame(['topic_' + str(i) for i in range(topic_number)])
df3 = df3.set_index(topic_label[topic_label.columns[0]])

html_path = folder_name + str(topic_number) + '_topic_Weight_Management.html'
output_file(html_path,
            title=str(topic_number) + '_topic_Weight_Management.html')

palette = palette[::
                  -1]  # Reverse the color order so dark red is highest unemployment
hm = HeatMap(df3,
             title=str(topic_number) + '_topic_Weight_Management.html',
             tools="reset,resize",
             width=1300,
             height=700,
             palette=palette)

show(hm)

#==============================================================================
# generate all urls under each topic
#==============================================================================

tem_list = []
for i in range(len(doc_list)):
    tem = '{},{}'.format(titles[i], fin_sum[i]).split(',')
    tem[1] = float(tem[1])
    tem_list.append(tem)
tem_list = sorted(tem_list, key=lambda res: res[1], reverse=False)
Exemplo n.º 22
0
def scan_pix_hist(h5_file_name, scurve_sel_pix=200):
    with tb.open_file(h5_file_name, 'r') as in_file_h5:
        meta_data = in_file_h5.root.meta_data[:]
        hit_data = in_file_h5.root.hit_data[:]
        en_mask = in_file_h5.root.scan_results.en_mask[:]
        Noise_gauss = in_file_h5.root.Noise_results.Noise_pure.attrs.fitdata_noise
        Noise_pure = in_file_h5.root.Noise_results.Noise_pure[:]
        Thresh_gauss = in_file_h5.root.Thresh_results.Threshold_pure.attrs.fitdata_thresh
        Threshold_pure = in_file_h5.root.Thresh_results.Threshold_pure[:]
        scan_args = yaml.load(in_file_h5.root.meta_data.attrs.kwargs)
        scan_range = scan_args['scan_range']
        scan_range_inx = np.arange(scan_range[0], scan_range[1], scan_range[2])

        # np.set_printoptions(threshold=np.nan)
        param = np.unique(meta_data['scan_param_id'])
        ret = []
        for i in param:
            wh = np.where(hit_data['scan_param_id'] == i)  # this can be faster and multi threaded
            hd = hit_data[wh[0]]
            hits = hd['col'].astype(np.uint16)
            hits = hits * 64
            hits = hits + hd['row']
            value = np.bincount(hits)
            value = np.pad(value, (0, 64 * 64 - value.shape[0]), 'constant')
            if len(ret):
                ret = np.vstack((ret, value))
            else:
                ret = value
        repeat_command = max(ret[-3])
        shape = en_mask.shape
        ges = 1
        for i in range(2):
            ges = ges * shape[i]
        ret_pure = ()
        en_mask = en_mask.reshape(ges)
        for n in range(param[-1]):
            ret_pure1 = ()
            for i in range(ges):
                if (str(en_mask[i]) == 'True'):
                    ret_pure1 = np.append(ret_pure1, ret[n][i])
            if n == 0:
                ret_pure = ret_pure1
                continue
            ret_pure = np.vstack((ret_pure, ret_pure1))

        ret_pure = ret_pure.astype(int)
        s_hist = np.swapaxes(ret_pure, 0, 1)

        pix_scan_hist = np.empty((s_hist.shape[1], repeat_command + 10))
        for param in range(s_hist.shape[1]):
            h_count = np.bincount(s_hist[:, param])
            h_count = h_count[:repeat_command + 10]
            pix_scan_hist[param] = np.pad(h_count, (0, (repeat_command + 10) - h_count.shape[0]), 'constant')

        log_hist = np.log10(pix_scan_hist)
        log_hist[~np.isfinite(log_hist)] = 0
        data = {
            'scan_param': np.ravel(np.indices(pix_scan_hist.shape)[0]),
            'count': np.ravel(np.indices(pix_scan_hist.shape)[1]),
            'value': np.ravel(log_hist)
        }

        x = scan_range_inx
        px = scurve_sel_pix  # 1110 #1539
        single_scan = figure(title="Single pixel scan " + str(px), x_axis_label="Injection[V]", y_axis_label="Hits")
        single_scan.diamond(x=x, y=s_hist[px], size=5, color="#1C9099", line_width=2)
        yf = analysis.scurve(x, 100, Threshold_pure[px], Noise_pure[px])
        single_scan.cross(x=x, y=yf, size=5, color="#E6550D", line_width=2)

        hist, edges = np.histogram(Threshold_pure, density=False, bins=50)

        hm1 = HeatMap(data, x='scan_param', y='count', values='value', title='Threshold Heatmap',
                      palette=Spectral11[::-1], stat=None, plot_width=1000)  # , height=4100)
        hm1.extra_x_ranges = {
            "e": Range1d(start=edges[0] * 1000 * analysis.cap_fac(), end=edges[-1] * 1000 * analysis.cap_fac())}

        hm_th = figure(title="Threshold", x_axis_label="pixel #", y_axis_label="threshold [V]",
                       y_range=(scan_range_inx[0], scan_range_inx[-1]), plot_width=1000)
        hm_th.diamond(y=Threshold_pure, x=range(64 * 64), size=1, color="#1C9099", line_width=2)
        hm_th.extra_y_ranges = {"e": Range1d(start=scan_range_inx[0] * 1000 * analysis.cap_fac(),
                                             end=scan_range_inx[-1] * 1000 * analysis.cap_fac())}
        hm_th.add_layout(LinearAxis(y_range_name="e"), 'right')
        plt_th_dist = figure(title='Threshold Distribution ', x_axis_label="threshold [V]", y_axis_label="#Pixel",
                             y_range=(0, 1.1 * np.max(hist[1:])))
        plt_th_dist.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="#036564",
                         line_color="#033649",
                         legend="# {0:d}  mean={1:.2f}  std={2:.2f}".format(int(np.sum(hist[:])), round(
                             Thresh_gauss['mu'] * 1000 * analysis.cap_fac(), 4),
                                                                            round(Thresh_gauss[
                                                                                      'sigma'] * 1000 * analysis.cap_fac(),
                                                                                  4)))
        plt_th_dist.extra_x_ranges = {"e": Range1d(start=edges[0] * 1000 * analysis.cap_fac(),
                                                   end=edges[-1] * 1000 * analysis.cap_fac())}  # better 7.4?
        plt_th_dist.add_layout(LinearAxis(x_range_name="e"), 'above')
        plt_th_dist.line(np.arange(edges[1], edges[-1], 0.0001),
                         analysis.gauss(np.arange(edges[1], edges[-1], 0.0001), Thresh_gauss['height'],
                                        Thresh_gauss['mu'], Thresh_gauss['sigma']), line_color="#D95B43", line_width=8,
                         alpha=0.7)

        hist, edges = np.histogram(Noise_pure, density=False, bins=50)
        hm_noise = figure(title="Noise", x_axis_label="pixel #", y_axis_label="noise [V]", y_range=(0, edges[-1]),
                          plot_width=1000)
        hm_noise.diamond(y=Noise_pure, x=range(64 * 64), size=2, color="#1C9099", line_width=2)
        hm_noise.extra_y_ranges = {"e": Range1d(start=0, end=edges[-1] * 1000 * analysis.cap_fac())}  # default 7.6
        hm_noise.add_layout(LinearAxis(y_range_name="e"), 'right')

        plt_noise_dist = figure(title='Noise Distribution ', x_axis_label="noise [V]", y_axis_label="#Pixel",
                                y_range=(0, 1.1 * np.max(hist[1:])))
        plt_noise_dist.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="#036564",
                            line_color="#033649",
                            legend="# {0:d}  mean={1:.2f}  std={2:.2f}".format(int(np.sum(hist[:])), round(
                                Noise_gauss['mu'] * 1000 * analysis.cap_fac(), 4), round(
                                Noise_gauss['sigma'] * 1000 * analysis.cap_fac(), 4)))
        plt_noise_dist.extra_x_ranges = {
            "e": Range1d(start=edges[0] * 1000 * analysis.cap_fac(), end=edges[-1] * 1000 * analysis.cap_fac())}
        plt_noise_dist.add_layout(LinearAxis(x_range_name="e"), 'above')
        plt_noise_dist.line(np.arange(edges[0], edges[-1], 0.0001),
                            analysis.gauss(np.arange(edges[0], edges[-1], 0.0001), Noise_gauss['height'],
                                           Noise_gauss['mu'], Noise_gauss['sigma']), line_color="#D95B43", line_width=8,
                            alpha=0.7)

        return vplot(hplot(hm_th, plt_th_dist), hplot(hm_noise, plt_noise_dist), hplot(hm1, single_scan)), s_hist
Exemplo n.º 23
0
#importing modules
import bokeh

import pandas as pd
import numpy as np

from bokeh.charts import Scatter, output_file, save
from bokeh.charts import HeatMap, output_file, save
from bokeh.layouts import gridplot
from bokeh.plotting import figure, save, output_file

#read file
df = pd.read_csv('file.csv')

#Plots a heatmap
p = HeatMap(df, x='Date', y='Temperature', values=None, stat='count', xgrid=False, ygrid=False, hover_tool=True, hover_text='Temperature', plot_width=2000, plot_height=1000)

output_file('HeatMap.html')

#Saves graph
save(p)

#box = BoxPlot(df, values='Temperature', label='Date', title="Temperature Date Box Plot", plot_width=400)

#Creates a Scatter Plot
v = Scatter(df, x='Date', y='Temperature', color='red', title="Date vs. Temperature", legend='top_right', xlabel="Date", ylabel="Temperature", plot_width=2000, plot_height=1000)

output_file('Scatter.html')

#Saves Graph as a html file
save(v)
prem_last = calcMatrixHeatMap(conversion_close)
prem_7day = calcMatrixHeatMap(rolling_7day)
prem_30day = calcMatrixHeatMap(rolling_30day)

from bokeh.layouts import row
from bokeh.plotting import figure, show, output_file
from bokeh.charts import HeatMap, bins, output_file, show
from bokeh.palettes import RdBu, magma
from bokeh.models import ColumnDataSource, LabelSet, Label

output_file("ccy-premium.html", title="BTC premia")

dateToDisplay = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

hm1 = HeatMap(prem_last, x='from', y='to', values='diff', title=dateToDisplay+' Premia Last Minute Closing (USD). diff = from - to, % = (from - to)/to', stat=None, palette=['green', 'gray', 'red'], legend=False)
source1 = ColumnDataSource(data=prem_last)
labels1a = LabelSet(x='from', y='to', text='formatted', level='glyph', x_offset=-15, y_offset=-10, render_mode='canvas', source=source1)
labels1b = LabelSet(x='from', y='to', text='percent', level='glyph', x_offset=-15, y_offset=-30, render_mode='canvas', source=source1)
hm1.add_layout(labels1a)
hm1.add_layout(labels1b)

hm2 = HeatMap(prem_7day, x='from', y='to', values='diff', title=dateToDisplay+' Premia Last 10 minutes average (USD)', stat=None, palette=['green', 'gray', 'red'], legend=False)
source2 = ColumnDataSource(data=prem_7day)
labels2a = LabelSet(x='from', y='to', text='formatted', level='glyph', x_offset=-15, y_offset=-10, render_mode='canvas', source=source2)
labels2b = LabelSet(x='from', y='to', text='percent', level='glyph', x_offset=-15, y_offset=-30, render_mode='canvas', source=source2)
hm2.add_layout(labels2a)
hm2.add_layout(labels2b)

hm3 = HeatMap(prem_30day, x='from', y='to', values='diff', title=dateToDisplay+' Premia last 60 minutes average (USD)', stat=None, palette=['green', 'gray', 'red'], legend=False)
source3 = ColumnDataSource(data=prem_30day)
Exemplo n.º 25
0
from bokeh.charts import HeatMap, output_file, show
import pandas as pd

df.reset_index(inplace=True)

hm = HeatMap(df, y='from', x='to', values='christina', title='TM', stat=None)

output_file('heatmap.html', mode='inline')
show(hm)
Exemplo n.º 26
0
import pandas as pd

from bokeh.charts import HeatMap, bins, output_file, show
from bokeh.layouts import column, gridplot
from bokeh.palettes import RdYlGn6, RdYlGn9
from bokeh.sampledata.unemployment1948 import data

# setup data sources
del data['Annual']
data['Year'] = data['Year'].astype(str)
unempl = pd.melt(data, var_name='Month', value_name='Unemployment', id_vars=['Year'])

fruits = {'fruit': ['apples', 'apples', 'apples', 'apples', 'apples',
                    'pears', 'pears', 'pears', 'pears', 'pears',
                    'bananas', 'bananas', 'bananas', 'bananas', 'bananas'],
          'fruit_count': [4, 5, 8, 12, 4, 6, 5, 4, 8, 7, 1, 2, 4, 8, 12],
          'year': [2009, 2010, 2011, 2012, 2013, 2009, 2010, 2011, 2012, 2013, 2009, 2010,
                   2011, 2012, 2013]}
fruits['year'] = [str(yr) for yr in fruits['year']]

hm9 = HeatMap(fruits, y='year', x='fruit', values='fruit_count', stat=None)

hm10 = HeatMap(unempl, x='Year', y='Month', values='Unemployment', stat=None,
              sort_dim={'x': False}, width=900, plot_height=500)

output_file("hm13.html", title="Bokeh heatmap example (hm13.py)")

show(column(hm9,hm10))

Exemplo n.º 27
0
from bokeh.layouts import column, gridplot
from bokeh.palettes import RdYlGn6, RdYlGn9
from bokeh.sampledata.autompg import autompg
from bokeh.sampledata.unemployment1948 import data
# setup data sources
del data['Annual']
print(type(data))

data['Year'] = data['Year'].astype(str)
unempl = pd.melt(data,
                 var_name='Month',
                 value_name='Unemployment',
                 id_vars=['Year'])

jobs = {
    'time_process': [20161104, 20161105, 20161106],
    'job_status': [0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
    'job_id': [72, 74, 4251, 4252, 4253]
}
jobs['job_id'] = [str(id) for id in jobs['job_id']]

hmap = HeatMap(jobs,
               y='job_id',
               x='time_process',
               values='job_status',
               stat=None)

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

show(column(gridplot(hmap, ncols=2, plot_width=400, plot_height=400), ))
Exemplo n.º 28
0
import pandas as pd

from bokeh.charts import HeatMap, output_file, show, vplot
from bokeh.palettes import Blues9 as palette
from bokeh.sampledata.unemployment1948 import data

# pandas magic
df = data[data.columns[:-2]]
df2 = df.set_index(df[df.columns[0]].astype(str))
df2.drop(df.columns[0], axis=1, inplace=True)
df3 = df2.transpose()

# bokeh magic
hm1 = HeatMap(df3,
              palette=palette,
              title="categorical heatmap, pd_input",
              height=400,
              width=1000)

hm_data = df3.values.T
hm2 = HeatMap(hm_data,
              palette=palette,
              title="Unemployment (Array)",
              xlabel='Years since 1948',
              ylabel='Months',
              height=400,
              width=1000)

simple_df = pd.DataFrame(
    {
        'apples': [4, 5, 8, 12, 4],
Exemplo n.º 29
0
def sample_distribution_for_genes(dataset_identifier, genes, title="", samples=None, unit="zscore", chart_type="heat",
                                  chart_rendering="html"):
    try:
        generator = None
        convert = False
        tooltip = False
        sns.set(style="whitegrid")
        #get path to HDF store from config file
        path_to_store = APP_CONFIG["application_files_location"] + APP_CONFIG["application_store_name"]

        print("path to store " + path_to_store)
        print(genes)

        #get data set as a pandas frame
        dataset = dr.get_data_frame_from_hdf(dataset_identifier, path_to_store)
        tooltip = None
        #filter selected genes
        gene_list = dataset.loc[genes, :]
        labels = list(gene_list.columns)

        #matplotlib settings
        ax = None
        fig = None

        #heatmap
        #html = dynamic plot
        #pdf = pdf file
        if chart_type == "heat":

            if chart_rendering == "html":
                print("bokeh heatmap")
                sns.set()
                title += " heatmap"
                generator = "bokeh"

                print("sns heatmap")
                fig = HeatMap(gene_list.dropna(), title=title)
            elif chart_rendering == "pdf":
                plt.switch_backend('Agg')
                title += " heatmap pdf"
                sns.set_context("paper", font_scale=0.8)
                generator = "matplot"
                ax = sns.heatmap(gene_list)
                #adapt label fontsize
                for tick in ax.xaxis.get_major_ticks():
                    tick.label.set_fontsize(2.0)
                    # tick.label.set
                print("axis loaded")
                print("heatmap with bokeh")
        elif chart_type == "box":
            #boxplot charts
            title += " boxplot"
            generator = "bokeh"
            # convert = True
            TOOLS = "resize,crosshair,pan,wheel_zoom,box_zoom,reset,previewsave,hover"
            print("creating box plot")
            # new_z_set = pd.DataFrame(stats.zscore(gene_list), index=gene_list.index, columns=gene_list.columns)
            t_genes = gene_list.transpose()
            # sns.boxplot(t_genes, color="coolwarm_r", names=t_genes.columns)
            fig = BoxPlot(t_genes, legend=True, tools=TOOLS)
            hover = fig.select(dict(type=HoverTool))
            hover.tooltips = [
                ("sample", "$index")
            ]
        elif chart_type == "scatter":
            if chart_rendering == "html":
                title += " scatter"
                generator = "bokeh"

                # tooltip = True
                TOOLS = "resize, crosshair, pan, wheel_zoom, box_zoom,reset, previewsave, hover"
                fig = bpl.figure(title=title, tools=TOOLS)
                current_palette = sns.color_palette()
                i = 0
                colors = bp.Spectral10
                # fig.scatter(dc, dc["index"])
                for e in gene_list.iterrows():
                    val = [v for v in e[1]]
                    idx = range(0, len(val))
                    col_data_source = ColumnDataSource(dict(
                        x=idx,
                        y=val,
                        sample=gene_list.columns[idx],
                    ))

                    fig.scatter('x', 'y', color=colors[i], legend=e[0], source=col_data_source)
                    i += 1

                hover = fig.select(dict(type=HoverTool))
                hover.tooltips = [
                    ("sample", "@sample"),
                    ("count", "$y")
                ]
            elif chart_rendering == "pdf":
                plt.switch_backend('PDF')
                sns.color_palette("deep")
                title += " scatter pdf"
                generator = "matplot"
                print("starting with matplot")
                transposed_data = gene_list.transpose()
                ax = transposed_data.plot(style='o')
                legend = plt.legend(frameon=1)
                frame = legend.get_frame()
                frame.set_edgecolor('blue')
                coll_len = len(gene_list.columns)
                for idx, gene in enumerate(gene_list.index):
                    for x, y in zip(range(0, coll_len), gene_list.iloc[idx, :]):
                        if y > gene_list.iloc[idx, :].quantile(0.99):
                            ax.annotate(labels[x], xy=(x, y), textcoords='data')
                # grid.add_legend(labels)
                print("plotted with matplot")

        if ax is not None:
            if generator == "matplot" and (title is not None):
                ax.set_title(title)
            elif generator == "matplot" and ax is not None:
                ax.set_title(title)

        if generator == "matplot":
            fig = plt.gcf()

        if convert is True and generator == "bokeh":
            fig = plt.gcf()
            fig = mpl.to_bokeh(fig)

        return {"generator": generator, "chart": fig}
    except BaseException as e:
        print(e.__str__())
Exemplo n.º 30
0
df_hmp = (pd.merge(agg.reset_index(), products,
                   on='Producto_ID', how='left').groupby([
                       'Semana', 'short_name'
                   ])['Demanda_uni_equil_sum', 'Venta_uni_hoy_sum',
                      'Dev_uni_proxima_sum',
                      'Dev_uni_proxima_count'].sum().reset_index())
df_hmp.head()
df_hmp['log1p_Demanda_uni_equil_sum'] = np.log1p(df_hmp.Demanda_uni_equil_sum)
g = sns.FacetGrid(df_hmp, row='Semana')
g = g.map(sns.distplot, 'log1p_Demanda_uni_equil_sum')
from bokeh.charts import HeatMap
from bokeh.plotting import vplot

heatmaps = []
for i in df.Q.cat.categories.values:
    hm = HeatMap(
        df_hmp[df_hmp.short_name.isin(df[df.Q == i].index.values)],
        x='short_name',
        y='Semana',
        values='Demanda_uni_equil_sum',
        hover_tool=True,
        title='Products with summary demand ' + str(i),
        xgrid=False,
        stat='sum',
        plot_width=950,
        plot_height=400,
        tools='hover, box_zoom, resize, save, wheel_zoom, reset',
    )
    heatmaps.append(hm)
show(vplot(*heatmaps))
Exemplo n.º 31
0
fruits = {
    'fruit': [
        'apples', 'apples', 'apples', 'apples', 'apples', 'pears', 'pears',
        'pears', 'pears', 'pears', 'bananas', 'bananas', 'bananas', 'bananas',
        'bananas'
    ],
    'fruit_count': [4, 5, 8, 12, 4, 6, 5, 4, 8, 7, 1, 2, 4, 8, 12],
    'year': [
        2009, 2010, 2011, 2012, 2013, 2009, 2010, 2011, 2012, 2013, 2009, 2010,
        2011, 2012, 2013
    ]
}
fruits['year'] = [str(yr) for yr in fruits['year']]

hm1 = HeatMap(autompg, x=bins('mpg'), y=bins('displ'))

hm2 = HeatMap(autompg,
              x=bins('mpg'),
              y=bins('displ'),
              values='cyl',
              stat='mean')

hm3 = HeatMap(autompg,
              x=bins('mpg'),
              y=bins('displ', bins=15),
              values='cyl',
              stat='mean')

hm4 = HeatMap(autompg, x=bins('mpg'), y='cyl', values='displ', stat='mean')
Exemplo n.º 32
0
#               palette=RdYlGn6)

# hm7 = HeatMap(autompg, x=bins('mpg'), y=bins('displ'), stat='mean', values='cyl',
#               palette=RdYlGn9)

# hm8 = HeatMap(autompg, x=bins('mpg'), y=bins('displ'), values='cyl',
#               stat='mean', legend='top_right')

# hm9 = HeatMap(fruits, y='year', x='fruit', values='fruit_count', stat=None)

# hm10 = HeatMap(unempl, x='Year', y='Month', values='Unemployment', stat=None,
#               sort_dim={'x': False}, width=900, plot_height=500)

TOOLS = [BoxSelectTool(), HoverTool()]

hm11 = HeatMap(test, x='region', y='area', values='data', legend=False, stat=None, palette=GnRd9, width = 500, plot_height=500, title="Actlab Region Detection Test", tools=TOOLS)
hm12 = Bar(data_bar, values='dist', label='region', legend=False, title='Error Distance for Respective Regions', width = 800, plot_height = 500)
hm13 = BoxPlot(data_bar, values='dist', label='region', legend=False, title='Error Distance Boxplot', width = 300, plot_height=300)
#hm11.legend.location = 'right'
# data1 = [int(x) for x in data]
# print data1
# hover = HoverTool(tooltips=[
#   ("error", "@data1")])
# hm11.add_tools(hover)  
hm11.add_layout(labels)
hm11.add_layout(labels_dev)
hm12.add_layout(label_reg)
# output_file("heatmap.html")
output_file("Bar.html")
hm11.axis.visible = False
# show(hm11)
Exemplo n.º 33
0
from bokeh.charts import HeatMap, output_file, show

# (dict, OrderedDict, lists, arrays and DataFrames are valid inputs)
data = {'fruit': ['apples']*3 + ['bananas']*3 + ['pears']*3,
        'fruit_count': [4, 5, 8, 1, 2, 4, 6, 5, 4],
        'sample': [1, 2, 3]*3}

hm = HeatMap(data, x='fruit', y='sample', values='fruit_count',
             title='Fruits', stat=None)

output_file('heatmap.html')
show(hm)
Exemplo n.º 34
0
from bokeh.models import ColumnDataSource, LabelSet, Label

output_file("index.html", title="BTC premia")

dateToDisplay = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

custompalette = [
    '#00814c', '#1ebd7c', '#26cc88', '#3ee09e', '#71ffc5', 'grey', '#fcffc8',
    '#ffeb66', '#ffca49', '#ff9600', '#d00031'
]

hm1 = HeatMap(
    prem_last,
    x='from',
    y='to',
    values='diff',
    title=dateToDisplay +
    ' Premia Last Closing (USD). diff = from - to, % = (from - to)/to',
    stat=None,
    palette=custompalette,
    legend=False)
source1 = ColumnDataSource(data=prem_last)
#labels1a = LabelSet(x='from', y='to', text='formatted', level='glyph', x_offset=-10, y_offset=-10, render_mode='canvas', source=source1, text_font_size='9pt')
labels1b = LabelSet(x='from',
                    y='to',
                    text='percent',
                    level='glyph',
                    x_offset=-10,
                    y_offset=-10,
                    render_mode='canvas',
                    source=source1,
                    text_font_size='9pt')
Exemplo n.º 35
0
from bokeh.charts import HeatMap, output_file, show
from bokeh.sampledata.unemployment1948 import data

# pandas magic
df = data[data.columns[:-2]]
df2 = df.set_index(df[df.columns[0]].astype(str))
df2.drop(df.columns[0], axis=1, inplace=True)
df3 = df2.transpose()

output_file("cat_heatmap.html")

hm = HeatMap(df3, title="categorical heatmap", width=800)

show(hm)
Exemplo n.º 36
0
def bokeh_heatmap(index=['Date', 'Representative']):
    #return three retention matrices
    percent_MRR, actual_MRR, contract_MRR = pivot_tables(combo=generate_data(
        n_sample=100, periods=30),
                                                         index=index)

    #reformat data to column sparse matrix and place into dictionary
    x = []
    y = []
    vals = []
    [(y.append(i[0]), x.append(i[1]), vals.append(v))
     for i, v in np.ndenumerate(percent_MRR)]
    data = {'x': x, 'y': y, 'vals': vals}

    #heatmap instantiate
    hm = HeatMap(data=data,
                 x='x',
                 y='y',
                 values='vals',
                 stat=None,
                 title='Revenue Retention by {}'.format(','.join(index)),
                 ygrid=True,
                 xgrid=True,
                 xlabel='months',
                 ylabel='contract start',
                 yscale='categorical',
                 palette=brewer['RdYlGn'][11],
                 title_text_font_size='20pt')
    #add value labels to heat map
    # create formatted column of values for annotation labels
    data['labels'] = [
        '{:.0%}'.format(round(i, 2)) if i >= 0 else '' for i in data['vals']
    ]
    #dropping vals which are not json compliant
    del data['vals']
    source = ColumnDataSource(data=data)
    labels = LabelSet(x='x',
                      y='y',
                      text='labels',
                      level='glyph',
                      x_offset=-2,
                      y_offset=-4,
                      source=source,
                      render_mode='canvas',
                      text_font_size='7pt',
                      text_color='white')
    hm.add_layout(labels)

    # customize y-axis text

    # new_index=actual_MRR.sum(axis=1).reset_index()
    # new_index.rename(columns={0: 'Actual MRR'}, inplace=True)
    # new_index['Contract MRR']=contract_MRR.sum(axis=1).reset_index()[0]
    y_label_dict = {}

    for index, groups in enumerate(list(percent_MRR.index)):
        y_label_dict[index] = '-'.join(groups)
    #reverse y-axis order
    hm.y_range.start = max(y_label_dict.keys())
    hm.y_range.end = min(y_label_dict.keys())
    hm.x_range = Range1d(0, 12)
    #generate javascript code to reformat y-axis
    hm.yaxis.formatter = FuncTickFormatter(code="""var labels = {};
                                                 return labels[tick];
                                                 """.format(y_label_dict))
    # fix ticks to display all values
    hm.yaxis[0].ticker = FixedTicker(ticks=list(y_label_dict.keys()))

    total_contract_mrr = contract_MRR.sum(axis=1)
    total_actual_mrr = actual_MRR.sum(axis=1)
    yaxis2_text = pd.concat([total_actual_mrr, total_contract_mrr], axis=1)

    yaxis2_text.reset_index(drop=True, inplace=True)
    yaxis2_text.columns = ['Actual MRR', 'Contract MRR']
    yaxis2_text['text'] = yaxis2_text.apply(
        lambda row: '{:,.0f}/{:,.0f} \t ({:.0%})'.format(
            row['Actual MRR'], row['Contract MRR'], row['Actual MRR'] / row[
                'Contract MRR']),
        axis=1)
    yaxis2_text = yaxis2_text['text'].to_dict()

    #add right hand y-axis for additional information
    top = max(yaxis2_text.keys())
    bottom = min(yaxis2_text.keys())
    print(top, bottom)

    hm.extra_y_ranges = {'mrr': Range1d(top, bottom)}

    hm.add_layout(LinearAxis(y_range_name="mrr"), 'right')

    hm.yaxis[1].formatter = FuncTickFormatter(code="""var labels = {};
                                                 return labels[tick];
                                                 """.format(yaxis2_text))
    hm.yaxis[1].ticker = FixedTicker(ticks=list(yaxis2_text.keys()))
    hm.yaxis[1].axis_label = 'Actual and Contract MRR'
    hm.yaxis[1].major_label_standoff = 30

    # #help(hm.legend)
    # legend_items=list(hm.legend)[0].items
    #

    hm.legend.location = 'bottom_right'

    # exit()
    output_file('interactive.html')
    show(hm)