Python bokeh.models.LabelSet() Examples

The following are 6 code examples of bokeh.models.LabelSet(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bokeh.models , or try the search function .
Example #1
Source File: visualize_utils.py    From embedding with MIT License 6 votes vote down vote up
def visualize_sentences(vecs, sentences, palette="Viridis256", filename="/notebooks/embedding/sentences.png",
                        use_notebook=False):
    tsne = TSNE(n_components=2)
    tsne_results = tsne.fit_transform(vecs)
    df = pd.DataFrame(columns=['x', 'y', 'sentence'])
    df['x'], df['y'], df['sentence'] = tsne_results[:, 0], tsne_results[:, 1], sentences
    source = ColumnDataSource(ColumnDataSource.from_df(df))
    labels = LabelSet(x="x", y="y", text="sentence", y_offset=8,
                      text_font_size="12pt", text_color="#555555",
                      source=source, text_align='center')
    color_mapper = LinearColorMapper(palette=palette, low=min(tsne_results[:, 1]), high=max(tsne_results[:, 1]))
    plot = figure(plot_width=900, plot_height=900)
    plot.scatter("x", "y", size=12, source=source, color={'field': 'y', 'transform': color_mapper}, line_color=None, fill_alpha=0.8)
    plot.add_layout(labels)
    if use_notebook:
        output_notebook()
        show(plot)
    else:
        export_png(plot, filename)
        print("save @ " + filename) 
Example #2
Source File: visualize_utils.py    From embedding with MIT License 6 votes vote down vote up
def visualize_words(words, vecs, palette="Viridis256", filename="/notebooks/embedding/words.png",
                    use_notebook=False):
    tsne = TSNE(n_components=2)
    tsne_results = tsne.fit_transform(vecs)
    df = pd.DataFrame(columns=['x', 'y', 'word'])
    df['x'], df['y'], df['word'] = tsne_results[:, 0], tsne_results[:, 1], list(words)
    source = ColumnDataSource(ColumnDataSource.from_df(df))
    labels = LabelSet(x="x", y="y", text="word", y_offset=8,
                      text_font_size="15pt", text_color="#555555",
                      source=source, text_align='center')
    color_mapper = LinearColorMapper(palette=palette, low=min(tsne_results[:, 1]), high=max(tsne_results[:, 1]))
    plot = figure(plot_width=900, plot_height=900)
    plot.scatter("x", "y", size=12, source=source, color={'field': 'y', 'transform': color_mapper}, line_color=None,
                 fill_alpha=0.8)
    plot.add_layout(labels)
    if use_notebook:
        output_notebook()
        show(plot)
    else:
        export_png(plot, filename)
        print("save @ " + filename) 
Example #3
Source File: visualize_utils.py    From embedding with MIT License 4 votes vote down vote up
def visualize_homonym(homonym, tokenized_sentences, vecs, model_name, palette="Viridis256",
                      filename="/notebooks/embedding/homonym.png", use_notebook=False):
    # process sentences
    token_list, processed_sentences = [], []
    for tokens in tokenized_sentences:
        token_list.extend(tokens)
        sentence = []
        for token in tokens:
            if model_name == "bert":
                processed_token = token.replace("##", "")
            else:
                processed_token = token
            if token == homonym:
                processed_token = "\"" + processed_token + "\""
            sentence.append(processed_token)
        processed_sentences.append(' '.join(sentence))
    # dimension reduction
    tsne = TSNE(n_components=2)
    tsne_results = tsne.fit_transform(vecs[1:])
    # only plot the word representation of interest
    interest_vecs, idx = np.zeros((len(tokenized_sentences), 2)), 0
    for word, vec in zip(token_list, tsne_results):
        if word == homonym:
            interest_vecs[idx] = vec
            idx += 1
    df = pd.DataFrame(columns=['x', 'y', 'annotation'])
    df['x'], df['y'], df['annotation'] = interest_vecs[:, 0], interest_vecs[:, 1], processed_sentences
    source = ColumnDataSource(ColumnDataSource.from_df(df))
    labels = LabelSet(x="x", y="y", text="annotation", y_offset=8,
                      text_font_size="12pt", text_color="#555555",
                      source=source, text_align='center')
    color_mapper = LinearColorMapper(palette=palette, low=min(tsne_results[:, 1]), high=max(tsne_results[:, 1]))
    plot = figure(plot_width=900, plot_height=900)
    plot.scatter("x", "y", size=12, source=source, color={'field': 'y', 'transform': color_mapper},
                 line_color=None,
                 fill_alpha=0.8)
    plot.add_layout(labels)
    if use_notebook:
        output_notebook()
        show(plot)
    else:
        export_png(plot, filename)
        print("save @ " + filename) 
Example #4
Source File: visualize_utils.py    From embedding with MIT License 4 votes vote down vote up
def visualize_self_attention_scores(tokens, scores, filename="/notebooks/embedding/self-attention.png",
                                    use_notebook=False):
    mean_prob = np.mean(scores)
    weighted_edges = []
    for idx_1, token_prob_dist_1 in enumerate(scores):
        for idx_2, el in enumerate(token_prob_dist_1):
            if idx_1 == idx_2 or el < mean_prob:
                weighted_edges.append((tokens[idx_1], tokens[idx_2], 0))
            else:
                weighted_edges.append((tokens[idx_1], tokens[idx_2], el))
    max_prob = np.max([el[2] for el in weighted_edges])
    weighted_edges = [(el[0], el[1], (el[2] - mean_prob) / (max_prob - mean_prob)) for el in weighted_edges]

    G = nx.Graph()
    G.add_nodes_from([el for el in tokens])
    G.add_weighted_edges_from(weighted_edges)

    plot = Plot(plot_width=500, plot_height=500,
                x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
    plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())

    graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0))

    graph_renderer.node_renderer.data_source.data['colors'] = Spectral8[:len(tokens)]
    graph_renderer.node_renderer.glyph = Circle(size=15, line_color=None, fill_color="colors")
    graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color="colors")
    graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color="grey")

    graph_renderer.edge_renderer.data_source.data["line_width"] = [G.get_edge_data(a, b)['weight'] * 3 for a, b in
                                                                   G.edges()]
    graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_width={'field': 'line_width'})
    graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color="grey", line_width=5)
    graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color="grey", line_width=5)

    graph_renderer.selection_policy = NodesAndLinkedEdges()
    graph_renderer.inspection_policy = EdgesAndLinkedNodes()

    plot.renderers.append(graph_renderer)

    x, y = zip(*graph_renderer.layout_provider.graph_layout.values())
    data = {'x': list(x), 'y': list(y), 'connectionNames': tokens}
    source = ColumnDataSource(data)
    labels = LabelSet(x='x', y='y', text='connectionNames', source=source, text_align='center')
    plot.renderers.append(labels)
    plot.add_tools(SaveTool())
    if use_notebook:
        output_notebook()
        show(plot)
    else:
        export_png(plot, filename)
        print("save @ " + filename) 
Example #5
Source File: main.py    From forest with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def make_plot(title='Forecasts'):
    '''
    # Generates the axes and background for the data to be plot on
    #
    :param title:
    :return:
    '''
    plot = figure(plot_height=500, plot_width=500, tools=["pan,reset,save, wheel_zoom", hover],
                     x_range=[-4, 4], y_range=[-4, 4])

    plot.title.text = title

    # Mark the 8 sectors
    x = 4
    y = 0.707107
    linewidth = 0.25
    plot.line([-x, -y], [-x, -y], line_width=0.5, line_alpha=0.6)
    plot.line([y, x], [y, x], line_width=0.5, line_alpha=0.6)
    plot.line([-x, -y], [x, y], line_width=0.5, line_alpha=0.6)
    plot.line([y, x], [-y, -x], line_width=0.5, line_alpha=0.6)
    plot.line([-x, -1], [0, 0], line_width=0.5, line_alpha=0.6)
    plot.line([1, x], [0, 0], line_width=0.5, line_alpha=0.6)
    plot.line([0, 0], [-x, -1], line_width=0.5, line_alpha=0.6)
    plot.line([0, 0], [1, x], line_width=0.5, line_alpha=0.6)

    xt, yt = 3., 1.5
    phase_marker_source = ColumnDataSource(data=dict(xt=[-xt, -yt, yt, xt, xt, yt, -yt, -xt],
                                                     yt=[-yt, -xt, -xt, -yt, yt, xt, xt, yt],
                                                     phase_labels=[str(i) for i in range(1, 9)]))
    labels = LabelSet(x='xt', y='yt', text='phase_labels', level='glyph',
                      x_offset=0, y_offset=0, source=phase_marker_source,
                      render_mode='canvas', text_color='grey', text_font_size="30pt", text_alpha=0.25)
    plot.add_layout(labels)
    plot.circle([0], [0], radius=1, color="white", line_color='grey', alpha=0.6)

    phase_name_source = ColumnDataSource(dict(x=[0, 0], y=[-3.75, 3.], text=['Indian \n Ocean', 'Western \n Pacific']))
    glyph = Text(x="x", y="y", text="text", angle=0., text_color="grey", text_align='center', text_alpha=0.25)
    plot.add_glyph(phase_name_source, glyph)

    phase_name_source = ColumnDataSource(dict(x=[-3.], y=[0], text=['West. Hem\n Africa']))
    glyph = Text(x="x", y="y", text="text", angle=np.pi / 2., text_color="grey", text_align='center', text_alpha=0.25)
    plot.add_glyph(phase_name_source, glyph)

    phase_name_source = ColumnDataSource(dict(x=[3.], y=[0], text=['Maritime\n continent']))
    glyph = Text(x="x", y="y", text="text", angle=-np.pi / 2., text_color="grey", text_align='center', text_alpha=0.25)
    plot.add_glyph(phase_name_source, glyph)

    plot.xaxis[0].axis_label = 'RMM1'
    plot.yaxis[0].axis_label = 'RMM2'

    return plot 
Example #6
Source File: dominance.py    From dominance-analysis with MIT License 4 votes vote down vote up
def plot_waterfall_relative_importance(self,incremental_rsquare_df):
		index = list(incremental_rsquare_df['Features'].values)
		data = {'Percentage Relative Importance': list(incremental_rsquare_df['percentage_incremental_r2'].values)}
		df = pd.DataFrame(data=data,index=index)
		
		net = df['Percentage Relative Importance'].sum()
		# print("Net ",net)

		df['running_total'] = df['Percentage Relative Importance'].cumsum()
		df['y_start'] = df['running_total'] - df['Percentage Relative Importance']

		df['label_pos'] = df['running_total']

		df_net = pd.DataFrame.from_records([(net, net, 0, net)],
			columns=['Percentage Relative Importance', 'running_total', 'y_start', 'label_pos'],index=["net"])
		
		df = df.append(df_net)

		df['color'] = '#1de9b6'
		df.loc[df['Percentage Relative Importance'] == 100, 'color'] = '#29b6f6'
		df.loc[df['Percentage Relative Importance'] < 0, 'label_pos'] = df.label_pos - 10000
		df["bar_label"] = df["Percentage Relative Importance"].map('{:,.1f}'.format)

		TOOLS = "reset,save"
		source = ColumnDataSource(df)
		p = figure(tools=TOOLS, x_range=list(df.index), y_range=(0, net+10),
			plot_width=1000, title = "Percentage Relative Importance Waterfall")

		p.segment(x0='index', y0='y_start', x1="index", y1='running_total',
			source=source, color="color", line_width=35)

		p.grid.grid_line_alpha=0.4
		p.yaxis[0].formatter = NumeralTickFormatter(format="(0 a)")
		p.xaxis.axis_label = "Predictors"
		p.yaxis.axis_label = "Percentage Relative Importance(%)"
		p.xaxis.axis_label_text_font_size='12pt'
		p.yaxis.axis_label_text_font_size='12pt'

		labels = LabelSet(x='index', y='label_pos', text='bar_label',
		text_font_size="11pt", level='glyph',
		x_offset=-14, y_offset=0, source=source)
		p.add_layout(labels)
		p.xaxis.major_label_orientation = -math.pi/4
		show(p)