Here are the examples of the python api bokeh.models.LabelSet taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
23 Examples
0
View Source File : draw.py
License : Apache License 2.0
Project Creator : aws
License : Apache License 2.0
Project Creator : aws
def add_labels(plot):
positions = circuit_layout()
data = {"xpos": [], "ypos": [], "label": []}
for label, pos in positions.items():
data["label"].append(label)
data["xpos"].append(pos[0])
data["ypos"].append(pos[1])
labels = LabelSet(
x="xpos",
y="ypos",
text="label",
level="glyph",
source=ColumnDataSource(data),
x_offset=-5,
y_offset=10,
text_color="#F5F7FB",
text_font_size="12pt",
)
plot.add_layout(labels)
def circuit_from(bqm):
0
View Source File : draw.py
License : Apache License 2.0
Project Creator : aws
License : Apache License 2.0
Project Creator : aws
def draw(S, position=None, with_labels=False):
"""Plot the given signed social network.
Args:
S: The network
position (dict, optional):
The position for the nodes. If no position is provided, a layout will be calculated. If the nodes have
'color' attributes, a Kamanda-Kawai layout will be used to group nodes of the same color together.
Otherwise, a circular layout will be used.
Returns:
A dictionary of positions keyed by node.
Examples:
>>> import dwave_structural_imbalance_demo as sbdemo
>>> gssn = sbdemo.GlobalSignedSocialNetwork()
>>> nld_before = gssn.get_node_link_data('Syria', 2013)
>>> nld_after = gssn.solve_structural_imbalance('Syria', 2013)
# draw Global graph before solving; save node layout for reuse
>>> position = sbdemo.draw('syria.png', nld_before)
# draw the Global graph; reusing the above layout, and calculating a new grouped layout
>>> sbdemo.draw('syria_imbalance.png', nld_after, position)
>>> sbdemo.draw('syria_imbalance_grouped', nld_after)
"""
# we need a consistent ordering of the edges
edgelist = S.edges()
nodelist = S.nodes()
def layout_wrapper(S):
pos = position
if pos is None:
try:
# group bipartition if nodes are colored
dist = defaultdict(dict)
for u, v in product(nodelist, repeat=2):
if u == v: # node has no distance from itself
dist[u][v] = 0
elif (
nodelist[u]["color"] == nodelist[v]["color"]
): # make same color nodes closer together
dist[u][v] = 1
else: # make different color nodes further apart
dist[u][v] = 2
pos = nx.kamada_kawai_layout(S, dist)
except KeyError:
# default to circular layout if nodes aren't colored
pos = nx.circular_layout(S)
return pos
# call layout wrapper once with all nodes to store position for calls with partial graph
position = layout_wrapper(S)
plot = Plot(
plot_width=600, plot_height=400, x_range=Range1d(-1.2, 1.2), y_range=Range1d(-1.2, 1.2)
)
tools = [WheelZoomTool(), ZoomInTool(), ZoomOutTool(), PanTool()]
plot.add_tools(*tools)
plot.toolbar.active_scroll = tools[0]
def get_graph_renderer(S, line_dash):
# we need a consistent ordering of the edges
edgelist = S.edges()
nodelist = S.nodes()
# get the colors assigned to each edge based on friendly/hostile
sign_edge_color = ["#87DACD" if S[u][v]["sign"] == 1 else "#FC9291" for u, v in edgelist]
# get the colors assigned to each node by coloring
try:
coloring_node_color = [
"#4378F8" if nodelist[v]["color"] else "#FFE897" for v in nodelist
]
except KeyError:
coloring_node_color = ["#FFFFFF" for __ in nodelist]
graph_renderer = from_networkx(S, layout_wrapper)
circle_size = 10
graph_renderer.node_renderer.data_source.add(coloring_node_color, "color")
graph_renderer.node_renderer.glyph = Circle(size=circle_size, fill_color="color")
edge_size = 2
graph_renderer.edge_renderer.data_source.add(sign_edge_color, "color")
try:
graph_renderer.edge_renderer.data_source.add(
[S[u][v]["event_year"] for u, v in edgelist], "event_year"
)
graph_renderer.edge_renderer.data_source.add(
[S[u][v]["event_description"] for u, v in edgelist], "event_description"
)
plot.add_tools(
HoverTool(
tooltips=[("Year", "@event_year"), ("Description", "@event_description")],
line_policy="interp",
)
)
except KeyError:
pass
graph_renderer.edge_renderer.glyph = MultiLine(line_color="color", line_dash=line_dash)
graph_renderer.inspection_policy = EdgesAndLinkedNodes()
return graph_renderer
try:
S_dash = S.edge_subgraph(((u, v) for u, v in edgelist if S[u][v]["frustrated"]))
S_solid = S.edge_subgraph(((u, v) for u, v in edgelist if not S[u][v]["frustrated"]))
plot.renderers.append(get_graph_renderer(S_dash, "dashed"))
plot.renderers.append(get_graph_renderer(S_solid, "solid"))
except KeyError:
plot.renderers.append(get_graph_renderer(S, "solid"))
plot.background_fill_color = "#202239"
positions = layout_wrapper(S)
if with_labels:
data = {"xpos": [], "ypos": [], "label": []}
for label, pos in positions.items():
data["label"].append(label)
data["xpos"].append(pos[0])
data["ypos"].append(pos[1])
labels = LabelSet(
x="xpos",
y="ypos",
text="label",
level="glyph",
source=ColumnDataSource(data),
x_offset=-5,
y_offset=10,
text_color="#F5F7FB",
text_font_size="12pt",
)
plot.add_layout(labels)
show(Row(plot))
return positions
0
View Source File : variant_qc_plots.py
License : BSD 3-Clause "New" or "Revised" License
Project Creator : broadinstitute
License : BSD 3-Clause "New" or "Revised" License
Project Creator : broadinstitute
def plot_concordance_pr(
pr_df: pd.DataFrame,
snv: bool,
colors: Dict[str, str] = None,
size_prop: str = None,
bins_to_label: List[int] = None
) -> Column:
"""
Generates plots showing Precision/Recall curves for truth samples:
Two tabs:
- One displaying the PR curve with ranking computed on the entire data
- One displaying the PR curve with ranking computed on the truth sample only
Within each tab, a row of n_truth_samples.
The input to this function should come out of the `get_binned_concordance_pd` function, which creates
a DataFrame containing the necessary metris for PR plotting and is grouped by 'rank_name', 'truth_sample', 'model' and 'snv'.
:param DataFrame pr_df: Input Dataframe
:param bool snv: Whether to plot SNVs or Indels
:param dict of str -> str colors: Optional colors to use (model name -> desired color)
:param str size_prop: Either 'radius' or 'area' can be specified. If either is specified, the points will be sized proportionally to the amount of data in that point.
:param list of int bins_to_label: Bins to label
:return: Bokeh grid of plots
:rtype: Tabs
"""
if colors is None:
# Get a palette automatically
models = sorted(list(set([g[2] for g in pr_df.groups])))
palette = d3['Category10'][max(3, len(models))]
colors = {model: palette[i] for i, model in enumerate(models)}
hover = HoverTool(tooltips=[
("model", "@model"),
("bin", "@bin"),
("score (min, max)", "(@min_score, @max_score)"),
('n_alleles', '@n_alleles'),
('cum_alleles', '@cum_alleles'),
("data (x,y)", "($x, $y)")
])
tabs = []
for rank in ['truth_sample_rank', 'global_rank']:
plot_row = []
for truth_sample in set([g[1] for g in pr_df.groups]):
p = figure(title=truth_sample[0].upper() + truth_sample[1:],
x_axis_label='Recall',
y_axis_label='Precision',
tools=[hover] + [tool for tool in TOOLS.split(',') if tool != 'hover'])
p.xaxis[0].formatter = NumeralTickFormatter(format="0%")
p.yaxis[0].formatter = NumeralTickFormatter(format="0.0%")
circles = []
for model in set([g[2] for g in pr_df.groups]):
data = pr_df.get_group((rank, truth_sample, model, snv)).copy()
data['model'] = [model] * len(data)
data['size'] = get_point_size_col(data['n_alleles'], size_prop)
source = ColumnDataSource(data)
circles.append((model, [p.circle('recall',
'precision',
size='size',
color=colors[model], source=source)]))
if bins_to_label is not None:
label_data = data.loc[data.bin.isin(bins_to_label)].copy()
label_data['x_offset'] = label_data['recall'] + 0.025
label_data['y_offset'] = label_data['precision']
label_data['bin_str'] = [str(int(t)) for t in label_data['bin']]
label_source = ColumnDataSource(label_data)
p.add_layout(
LabelSet(x='x_offset',
y='precision',
text='bin_str',
text_color=colors[model],
source=label_source)
)
p.multi_line(
xs=[[x, x + 0.05] for x in label_data.recall],
ys=[[y, y] for y in label_data.precision],
color=colors[model]
)
legend = Legend(items=circles, orientation='horizontal', location=(0, 0), click_policy="hide")
p.add_layout(legend, 'above')
set_plots_defaults(p)
plot_row.append(p)
tabs.append(Panel(child=Row(children=plot_row), title=rank))
return Tabs(tabs=tabs)
0
View Source File : dominance.py
License : MIT License
Project Creator : dominance-analysis
License : MIT License
Project Creator : dominance-analysis
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)
def plot_incremental_rsquare(self):
0
View Source File : bokeh.py
License : Apache License 2.0
Project Creator : fluidml
License : Apache License 2.0
Project Creator : fluidml
def visualize_graph_interactive(graph: nx.Graph, plot_width: int = 500, plot_height: int = 500,
node_width: int = 50, node_height: int = 50,
scale_width: bool = True):
# reformat the graph with attributes
reformatted_graph = reformat_graph(graph)
# bokeh plot settings
plot = figure(plot_width=plot_width, plot_height=plot_height)
plot.title.text = "Task Graph"
plot.grid.visible = False
plot.sizing_mode = "scale_width" if scale_width else "auto"
plot.xaxis.visible = False
plot.yaxis.visible = False
# get sugiyama layout
layout = build_sugiyama_layout(reformatted_graph, 10, node_height, node_width)
positions = {vertex.data.strip(): (vertex.view.xy[0], vertex.view.xy[1]) for vertex in layout.g.sV}
positions = flip_positions(positions, plot_height)
# plot nodes
x, y = zip(*positions.values())
node_labels = nx.get_node_attributes(reformatted_graph, 'task_name')
source = ColumnDataSource({'x': x, 'y': y,
'task_name': list(positions.keys())})
labels = LabelSet(x='x', y='y', text='task_name', source=source, text_align="center",
background_fill_color='white', text_font_size="12px", border_line_color="black", name="task_name")
plot.renderers.append(labels)
# plot edges
xs, ys = get_edges(layout, plot_height)
plot.multi_line(xs, ys)
show(plot)
0
View Source File : plot_bok.py
License : GNU General Public License v3.0
Project Creator : ganeshjawahar
License : GNU General Public License v3.0
Project Creator : ganeshjawahar
def bokey_plot(dictionary_input, folder_bokey_default,mode="single", output=False, id_=None,info="",color_map_mode="continuous"):
"""
as input dictionnary of the form
- label : {x:, y: , label:[labels]}
mode allows you to specify if you want everything on one single plot or if you want distinct plots
"""
reset_output()
source = {}
i = 0
color = ["blue","#ee6666"]
assert color_map_mode in ["continuous","divergent"]
if color_map_mode=="continuous": color_map= cm.OrRd
elif color_map_mode=="divergent": color_map=cm.rainbow
if id_ is None:
id_ = str(uuid.uuid4())[0:8]
if mode == "single":
p = figure(plot_width=800, plot_height=1000,
tools=[BoxZoomTool(), ResetTool(), WheelZoomTool()],
toolbar_sticky=False, toolbar_location="right",
title='T-SNE '+info) # x_range=Range1d(-6,6),y_range=Range1d(int(min(y))-1,int(max(y))+1))
for key in dictionary_input.keys():
if mode == "distinct":
p = figure(plot_width=800, plot_height=1000,
title='T-SNE '+info) # x_range=Range1d(-6,6),y_range=Range1d(int(min(y))-1,int(max(y))+1))
source[key] = ColumnDataSource(data=dict(height=dictionary_input[key]["x"],
weight=dictionary_input[key]["y"],
names=dictionary_input[key]["label"]))
colors = ["#%02x%02x%02x" %(int(r), int(g), int(b)) for r, g, b, _ in (255)*color_map(Normalize(vmin=0,vmax=5)(dictionary_input[key]["color"]))]
colors_legend = ["#%02x%02x%02x" %(int(r), int(g), int(b)) for r, g, b, _ in (255)*color_map(Normalize(vmin=0,vmax=5)(np.sort(list(set(dictionary_input[key]["color"])))))]
color_mapper = LinearColorMapper(palette=colors_legend)
ticker = FixedTicker(ticks=[0, 1, 2, 3, 4,5])
formatter = FuncTickFormatter(code="""
function(tick) {
data = {0: '0-10', 1: '10-20', 2: '20-30', 3: '30-40', 4: '40-50',50: '50plus'}
return data[tick], " ,
}
""")
cbar = ColorBar(color_mapper=color_mapper, ticker=ticker, formatter=formatter,
major_tick_out=0, major_tick_in=0, major_label_text_align='left',
major_label_text_font_size='100pt', label_standoff=5)
p.scatter(x='weight', y='height', size=8, source=source[key], legend=key)# color=colors)
p.add_layout(cbar)
labels = LabelSet(x='weight', y='height', text='names', level='glyph',
x_offset=5, y_offset=5, source=source[key], render_mode='canvas')
p.add_layout(labels)
i += 1
if output:
output_file(folder_bokey_default+id_+"_tsne_"+key+"_"+info+"_bok.html")
print(folder_bokey_default+id_+"_tsne_"+key+"_"+info+"_bok.html")
show(p)
#if mode == "distinct":
# output_notebook()
# show(p)
if mode == "single":
output_notebook()
show(p)
0
View Source File : figures.py
License : GNU General Public License v3.0
Project Creator : gotzl
License : GNU General Public License v3.0
Project Creator : gotzl
def getLapFigure(p1, df_, ds, mode, ref=False, hasref=False):
# add required colors to dataframe and create datasource
df_ = color_mode_map[mode](df_, ref)
to_bokeh = lambda c: list(map(mplcolors.to_hex, c))
x = 'xr' if ref else 'x'
y = 'yr' if ref else 'y'
color = 'color_%s'%((mode+'_r') if ref else mode)
ds.data[color] = to_bokeh(df_[color])
# shift the reference points to the outside
if ref:
ds.data[x] += 30*np.cos(df_.heading+np.pi/2)
ds.data[y] += 30*np.sin(df_.heading+np.pi/2)
# plot the track map, overwrite the (non)selection glyph to keep our color from ds
# the hover effect is configured below
r2 = p1.scatter(x=x, y=y, source=ds, color=color)
r2.nonselection_glyph = r2.selection_glyph
if ref: return p1
# add some lap descriptions
corners = acctelemetry.corners(df_)
corners_ds = ColumnDataSource(dict(
x=df_.x.values[corners],
y=df_.y.values[corners],
text=['T%i'%i for i in range(1, len(corners)+1)],
))
labels = LabelSet(x='x', y='y', text='text', level='glyph',
x_offset=5, y_offset=5,
source=corners_ds, render_mode='canvas')
p1.add_layout(labels)
# create a invisible renderer for the track map
# this is used to trigger the hover, thus the size is large
c1 = p1.circle(x='x', y='y', source=ds, size=10, fill_alpha=0.0, alpha=0.0)
c1.selection_glyph = Circle(fill_color='red', fill_alpha=.7, line_color=None)
c1.nonselection_glyph = Circle(fill_alpha=0, line_color=None)
# create a renderer to show a dot for the reference
if hasref:
cr = p1.circle(x='xr', y='yr', source=ds,
size = 8 if mode in ['absolut', 'gainloss'] else 10,
fill_alpha=0.0, alpha=0.0)
cr.selection_glyph = Circle(fill_color='blue', fill_alpha=.7, line_color=None)
cr.nonselection_glyph = Circle(fill_alpha=0, line_color=None)
return c1
def getLapSlider(ds, p0, r0, hover0, view):
0
View Source File : container.py
License : BSD 3-Clause "New" or "Revised" License
Project Creator : griquelme
License : BSD 3-Clause "New" or "Revised" License
Project Creator : griquelme
def pca_scores(self, x_pc: int = 1, y_pc: int = 2, hue: str = _sample_class,
ignore_classes: Optional[List[str]] = None,
show_order: bool = False, scaling: Optional[str] = None,
normalization: Optional[str] = None, draw: bool = True,
fig_params: Optional[dict] = None,
scatter_params: Optional[dict] = None
) -> bokeh.plotting.Figure:
"""
plots PCA scores
Parameters
----------
x_pc: int
Principal component number to plot along X axis.
y_pc: int
Principal component number to plot along Y axis.
hue: {"class", "type", "batch"}
How to color samples. "class" color points according to sample
class, "type" color points according to the sample type
assigned in the mapping and "batch" uses batch information. Samples
classes without a mapping are not shown in the plot
ignore_classes : list[str], optional
classes in the data to ignore to build the PCA model.
show_order: bool
add a label with the run order.
scaling: {`autoscaling`, `rescaling`, `pareto`}, optional
scaling method.
normalization: {`sum`, `max`, `euclidean`}, optional
normalization method
draw: bool
If True calls bokeh.plotting.show on fig.
fig_params: dict, optional
Optional parameters to pass to bokeh figure
scatter_params: dict, optional
Optional parameters to pass to bokeh scatter plot.
Returns
-------
bokeh.plotting.Figure.
"""
default_fig_params = {"aspect_ratio": 1}
if fig_params is None:
fig_params = default_fig_params
else:
default_fig_params.update(fig_params)
fig_params = default_fig_params
default_scatter_params = {"size": 6}
if scatter_params is None:
scatter_params = default_scatter_params
else:
default_scatter_params.update(scatter_params)
scatter_params = default_scatter_params
tooltips = [(_sample_class, "@{}".format(_sample_class)),
(_sample_order, "@{}".format(_sample_order)),
(_sample_batch, "@{}".format(_sample_batch)),
(_sample_id, "@{}".format(_sample_id))]
fig = bokeh.plotting.figure(tooltips=tooltips, **fig_params)
x_name = "PC" + str(x_pc)
y_name = "PC" + str(y_pc)
n_comps = max(x_pc, y_pc)
score, _, variance, total_var = \
self._data_container.metrics.pca(n_components=n_comps,
ignore_classes=ignore_classes,
normalization=normalization,
scaling=scaling)
score = score.join(self._data_container.sample_metadata)
if hue == _sample_type:
rev_map = _reverse_mapping(self._data_container.mapping)
score[_sample_type] = (score[_sample_class]
.apply(lambda x: rev_map.get(x)))
score = score[~pd.isna(score[_sample_type])]
elif hue == _sample_batch:
score[_sample_batch] = score[_sample_batch].astype(str)
# setup the colors
unique_values = score[hue].unique().astype(str)
score = ColumnDataSource(score)
cmap = Category10[10]
palette = cmap * (int(unique_values.size / len(cmap)) + 1)
palette = palette[:unique_values.size]
# TODO: Category10_3 should be in a parameter file
fig.scatter(source=score, x=x_name, y=y_name,
color=factor_cmap(hue, palette, unique_values),
legend_group=hue, **scatter_params)
# figure appearance
x_label = x_name + " ({:.1f} %)"
x_label = x_label.format(variance[x_pc - 1] * 100 / total_var)
y_label = y_name + " ({:.1f} %)"
y_label = y_label.format(variance[y_pc - 1] * 100 / total_var)
fig.xaxis.axis_label = x_label
fig.yaxis.axis_label = y_label
fig.yaxis.axis_label_text_font_style = "bold"
fig.xaxis.axis_label_text_font_style = "bold"
if show_order:
labels = LabelSet(x=x_name, y=y_name, text=_sample_order,
level="glyph", x_offset=3, y_offset=3,
source=score, render_mode="canvas",
text_font_size="8pt")
fig.add_layout(labels)
if draw:
bokeh.plotting.show(fig)
return fig
def pca_loadings(self, x_pc=1, y_pc=2, scaling: Optional[str] = None,
0
View Source File : ui_main.py
License : Apache License 2.0
Project Creator : IntelLabs
License : Apache License 2.0
Project Creator : IntelLabs
def create_custom_trends_graph():
header = gd["reports"][4]
phrases = gd.values[:, 9]
imp_change = gd.values[:, 10]
phrases_h, changes_h = [], []
phrases_c, changes_c = [], []
text_h, text_c = [], []
custom_count = 0
for i in range(len(phrases)):
phrase = phrases[i]
change = float(imp_change[i])
if phrase in custom_nps:
if custom_count > 30:
logger.warning("too many custom phrases (>30). Showing partial list")
break
phrase = cut_phrase(phrase)
if change > 0:
phrases_h.append(phrase)
changes_h.append(change)
text_h.append("+" + str(("%.1f" % change)) + "%")
else:
phrases_c.append(phrase)
changes_c.append(change)
text_c.append(str(("%.1f" % change)) + "%")
custom_count += 1
changes = changes_h + changes_c
text = text_h + text_c
trends = phrases_h + phrases_c
colors = []
if len(changes_h) > 0:
for i in range(len(changes_h)):
colors.append("#1d6d34")
if len(changes_c) > 0:
for i in range(len(changes_c)):
colors.append("#912605")
if len(changes) < 10: # pad with 10 blanks
changes += [0] * (10 - len(changes))
text += " " * (10 - len(text))
trends += [str((10 - len(trends)) - i) for i in range(0, (10 - len(trends)))]
colors += ["white"] * (10 - len(colors))
source = ColumnDataSource(
dict(y=trends[::-1], x=changes[::-1], colors=colors[::-1], text=text[::-1])
)
plot = figure(
title=header,
plot_width=600,
plot_height=400,
tools="save",
x_range=ranges.Range1d(start=min(changes) - 10, end=max(changes) + 20),
y_range=source.data["y"],
tooltips="@y < br>change: @x",
)
labels = LabelSet(
x=max(changes) + 5,
y="y",
text="text",
level="glyph",
x_offset=0,
y_offset=-10,
source=source,
render_mode="canvas",
)
plot.hbar(source=source, right="x", y="y", left=0, height=0.5, color="colors")
plot.add_layout(labels)
return plot
def create_trend_graphs(top_n):
0
View Source File : ui_main.py
License : Apache License 2.0
Project Creator : IntelLabs
License : Apache License 2.0
Project Creator : IntelLabs
def create_trend_graphs(top_n):
header_h = gd["reports"][2]
header_c = gd["reports"][3]
phrases = gd.values[:, 9] # all trends
imp_change = gd.values[:, 10]
data_h, data_c = {}, {}
text_h, text_c = [], []
hot, cold = 0, 0 # counters for each list
plot_h = figure(plot_width=600, plot_height=400, title=header_h)
plot_c = figure(plot_width=600, plot_height=400, title=header_c)
for i in range(len(phrases)):
if hot == top_n and cold == top_n:
break
phrase = phrases[i]
change = float(imp_change[i])
if phrase in white_list:
phrase = cut_phrase(phrase)
if change > 0:
if hot < top_n:
data_h[phrase] = change
text_h.append("+" + str(("%.1f" % change)) + "%")
hot += 1
elif cold < top_n:
data_c[phrase] = change
text_c.append(str(("%.1f" % change)) + "%")
cold += 1
if len(data_h.keys()) > 0:
phrases_h = sorted(data_h, key=data_h.get)
changes_h = sorted(data_h.values())
source = ColumnDataSource(dict(y=phrases_h, x=changes_h, text=text_h[::-1]))
title = header_h
plot_h = figure(
plot_width=600,
plot_height=400,
tools="save",
title=title,
x_range=ranges.Range1d(start=0, end=max(changes_h) + 20),
y_range=source.data["y"],
tooltips="@y < br>change: @x",
)
labels = LabelSet(
x="x",
y="y",
text="text",
level="glyph",
x_offset=5,
y_offset=0,
source=source,
render_mode="canvas",
text_color="#1d6d34",
)
plot_h.hbar(source=source, right="x", y="y", left=0, height=0.5, color="#1d6d34")
plot_h.add_layout(labels)
plot_h.xaxis.visible = False
if len(data_c.keys()) > 0:
phrases_c = sorted(data_c, key=data_c.get)
changes_c = sorted(data_c.values())
source = ColumnDataSource(dict(y=phrases_c[::-1], x=changes_c[::-1], text=text_c[::-1]))
plot_c = figure(
plot_width=600,
plot_height=400,
tools="save",
title=header_c,
x_range=ranges.Range1d(start=min(changes_c) - 10, end=20),
y_range=source.data["y"],
tooltips="@y < br>change: @x",
)
labels = LabelSet(
x=0,
y="y",
text="text",
level="glyph",
x_offset=5,
y_offset=0,
source=source,
render_mode="canvas",
text_color="#912605",
)
plot_c.hbar(source=source, right="x", y="y", left=0, height=0.5, color="#912605")
plot_c.add_layout(labels)
plot_c.xaxis.visible = False
return [plot_h, plot_c]
def create_trend_clustering_graph(top_n):
0
View Source File : clustree.py
License : MIT License
Project Creator : ivirshup
License : MIT License
Project Creator : ivirshup
def plot_hierarchy(
complist: "ComponentList", coords: pd.DataFrame, *, scatter_kwargs={}
):
"""
Params
------
complist
List of components that will be plotted in this graph.
"""
coords = coords.copy()
scatter_kwargs = scatter_kwargs.copy()
g = complist.to_graph()
assert len(list(nx.components.weakly_connected_components(g))) == 1
for k, v in make_umap_plots(
complist, coords, scatter_kwargs=scatter_kwargs
).items():
g.nodes[k]["img"] = v
pos = json_friendly(
nx.nx_agraph.graphviz_layout(nx.DiGraph(g.edges(data=False)), prog="dot")
)
graph_renderer = from_networkx(g, pos)
graph_renderer.node_renderer.glyph = Circle(size=15)
graph_renderer.edge_renderer.glyph = MultiLine(line_width=1)
node_hover = HoverTool(
tooltips=[
("img", "@img{safe}"),
("component_id", "@index"),
("# solutions:", "@n_solutions"),
("# samples in intersect", "@n_intersect"),
("# samples in union", "@n_union"),
],
attachment="vertical",
)
# Adding labels
label_src = pd.DataFrame.from_dict(
graph_renderer.layout_provider.graph_layout, orient="index", columns=["x", "y"]
)
label_src.index.name = "nodeid"
label_src = ColumnDataSource(label_src)
node_label = LabelSet(
x="x",
y="y",
text="nodeid",
level="annotation",
source=label_src,
text_align="center",
)
# layout = graph_renderer.layout_provider.graph_layout
# label, x, y = zip(*((str(label), x, y) for label, (x, y) in layout.items()))
# node_label = LabelSet(
# x=x, y=y, text=label, level="glyph"
# )
p = Plot(plot_width=1000, plot_height=500, **get_ranges(pos))
p.renderers.append(graph_renderer)
p.add_layout(node_label)
p.add_tools(node_hover, SaveTool())
return p
0
View Source File : visualization.py
License : GNU Affero General Public License v3.0
Project Creator : iza-institute-of-labor-economics
License : GNU Affero General Public License v3.0
Project Creator : iza-institute-of-labor-economics
def plot_dag(
functions,
targets=None,
columns_overriding_functions=None,
check_minimal_specification="ignore",
selectors=None,
labels=True,
tooltips=False,
plot_kwargs=None,
arrow_kwargs=None,
edge_kwargs=None,
label_kwargs=None,
node_kwargs=None,
):
"""Plot the dag of the tax and transfer system.
Parameters
----------
functions : str, pathlib.Path, callable, module, imports statements, dict
Functions can be anything of the specified types and a list of the same objects.
If the object is a dictionary, the keys of the dictionary are used as a name
instead of the function name. For all other objects, the name is inferred from
the function name.
targets : str, list of str
String or list of strings with names of functions whose output is actually
needed by the user.
columns_overriding_functions : str list of str
Names of columns in the data which are preferred over function defined in the
tax and transfer system.
check_minimal_specification : {"ignore", "warn", "raise"}, default "ignore"
Indicator for whether checks which ensure the most minimal configuration should
be silenced, emitted as warnings or errors.
selectors : str or list of str or dict or list of dict or list of str and dict
Selectors allow to you to select and de-select nodes in the graph for
visualization. For the full list of options, see the tutorial about
`visualization < ../docs/tutorials/visualize.ipynb>`_. By default, all nodes are
shown.
labels : bool, default True
Annotate nodes with labels.
tooltips : bool, default False
Experimental feature which makes the source code of the functions accessible as
a tooltip. Sometimes, the tooltip is not properly displayed.
plot_kwargs : dict
Additional keyword arguments passed to :class:`bokeh.models.Plot`.
arrow_kwargs : dict
Additional keyword arguments passed to :class:`bokeh.models.Arrow`. For example,
change the size of the head with ``{"size": 10}``.
edge_kwargs : dict
Additional keyword arguments passed to :class:`bokeh.models.MultiLine`. For
example, change the color with ``{"fill_color": "green"}``.
label_kwargs : dict
Additional keyword arguments passed to :class:`bokeh.models.LabelSet`. For
example, change the fontsize with ``{"text_font_size": "12px"}``.
node_kwargs : dict
Additional keyword arguments passed to :class:`bokeh.models.Circle`. For
example, change the color with ``{"fill_color": "orange"}``.
"""
targets = DEFAULT_TARGETS if targets is None else targets
targets = parse_to_list_of_strings(targets, "targets")
columns_overriding_functions = parse_to_list_of_strings(
columns_overriding_functions, "columns_overriding_functions"
)
# Load functions and perform checks.
functions, internal_functions = load_user_and_internal_functions(functions)
# Create one dictionary of functions and perform check.
functions = {**internal_functions, **functions}
functions = {
k: v for k, v in functions.items() if k not in columns_overriding_functions
}
_fail_if_targets_not_in_functions(functions, targets)
# Partial parameters to functions such that they disappear in the DAG.
functions = _mock_parameters_arguments(functions)
dag = create_dag(
functions, targets, columns_overriding_functions, check_minimal_specification
)
selectors = [] if selectors is None else _to_list(selectors)
plot_kwargs = {} if plot_kwargs is None else plot_kwargs
arrow_kwargs = {} if arrow_kwargs is None else arrow_kwargs
edge_kwargs = {} if edge_kwargs is None else edge_kwargs
label_kwargs = {} if label_kwargs is None else label_kwargs
node_kwargs = {} if node_kwargs is None else node_kwargs
dag = _select_nodes_in_dag(dag, selectors)
dag = _add_url_to_dag(dag)
# Even if we do not use the source codes as tooltips, we need to remove the
# functions.
dag = _replace_functions_with_source_code(dag)
plot_kwargs["title"] = _to_bokeh_title(
plot_kwargs.get("title", "Tax and Transfer System")
)
plot = Plot(**{**PLOT_KWARGS_DEFAULTS, **plot_kwargs})
layout = _create_pydot_layout(dag)
graph_renderer = from_networkx(dag, layout, scale=1, center=(0, 0))
graph_renderer.node_renderer.glyph = Circle(
**{**NODE_KWARGS_DEFAULTS, **node_kwargs}
)
graph_renderer.edge_renderer.visible = False
for (
_,
(start_node, end_node),
) in graph_renderer.edge_renderer.data_source.to_df().iterrows():
(x_start, y_start), (x_end, y_end) = _compute_arrow_coordinates(
layout[start_node], layout[end_node]
)
plot.add_layout(
Arrow(
end=NormalHead(**{**ARROW_KWARGS_DEFAULTS, **arrow_kwargs}),
x_start=x_start,
y_start=y_start,
x_end=x_end,
y_end=y_end,
**{**EDGE_KWARGS_DEFAULTS, **edge_kwargs},
)
)
plot.renderers.append(graph_renderer)
tools = [BoxZoomTool(), ResetTool()]
tools.append(TapTool(callback=OpenURL(url="@url")))
if tooltips:
tools.append(HoverTool(tooltips=TOOLTIPS))
plot.add_tools(*tools)
if labels:
source = ColumnDataSource(
pd.DataFrame(layout).T.rename(columns={0: "x", 1: "y"})
)
labels = LabelSet(
x="x",
y="y",
text="index",
source=source,
**{**LABEL_KWARGS_DEFAULT, **label_kwargs},
)
plot.add_layout(labels)
output_notebook()
show(plot)
return plot
def _mock_parameters_arguments(functions):
0
View Source File : single_tab.py
License : Apache License 2.0
Project Creator : nancyyanyu
License : Apache License 2.0
Project Creator : nancyyanyu
def candlestick():
if '^GSPC' in symbol_list:
symbol_list.remove('^GSPC')
stock_select=Select(value=symbol_list[0],options=symbol_list)
summaryText = Div(text="",width=400)
financialText=Div(text="",width=180)
def update_summary(symbol):
company,buzzsummary,officerString,institution_df=read_company(symbol)
summaryText.text =""" < b> < p style="color:blue;">Overview: < /p> < /b>
< b>Company: < /b> {} < br>
< b>Address: < /b> {} < br>
< b>City: < /b> {} < br>
< b>State: < /b> {} < br>
< b>Website: < /b> < a href="{}">{} < /a> < br>
< b>Industry: < /b> {} < br>
< b>Sector: < /b> {} < br>
< b>Company Officers: < /b> {} < br>
< b>Summary: < /b> {} < br>""".format(company['price']['longName'],
company['summaryProfile']['address1'],
company['summaryProfile']['city'],
company['summaryProfile']['state'],
company['summaryProfile']['website'],
company['summaryProfile']['website'],
company['summaryProfile']['industry'],
company['summaryProfile']['sector'],
officerString,
buzzsummary)
financialText.text=""" < b> < p style="color:blue;">Financial: < /p> < /b>
< b>Recommendation: {} < /b> < br>
< b>Enterprise Value: < /b> {} < br>
< b>Profit Margins: < /b> {} < br>
< b>Beta: < /b> {} < br>
< b>EBITDA: < /b> {} < br>
< b>Total Debt: < /b> {} < br>
< b>Total Revenue: < /b> {} < br>
< b>DebtToEquity: < /b> {} < br>
< b>Revenue Growth: < /b> {} < br>
< b>Current Ratio: < /b> {} < br>
< b>ROE: < /b> {} < br>
< b>ROA: < /b> {} < br>
< b>Gross Profits: < /b> {} < br>
< b>Quick Ratio: < /b> {} < br>
< b>Free Cashflow: < /b> {} < br>
""".format(company['financialData']['recommendationKey'].upper(),
company['defaultKeyStatistics']['enterpriseValue']['fmt'],
company['defaultKeyStatistics']['profitMargins']['fmt'],
company['defaultKeyStatistics']['beta']['fmt'],
company['financialData']['ebitda']['fmt'],
company['financialData']['totalDebt']['fmt'],
company['financialData']['totalRevenue']['fmt'],
company['financialData']['debtToEquity']['fmt'],
company['financialData']['revenueGrowth']['fmt'],
company['financialData']['currentRatio']['fmt'],
company['financialData']['returnOnAssets']['fmt'],
company['financialData']['returnOnEquity']['fmt'],
company['financialData']['grossProfits']['fmt'],
company['financialData']['quickRatio']['fmt'],
company['financialData']['freeCashflow']['fmt'])
update_summary(stock_select.value)
# connect to Cassandra database
database=CassandraStorage(symbol_list[0])
database.session.row_factory = pandas_factory
database.session.default_fetch_size = None
query="SELECT * FROM {} WHERE time>'2015-01-01' ALLOW FILTERING;".format('{}_historical'.format(symbol_list[0]))
rslt = database.session.execute(query, timeout=None)
df = rslt._current_rows
# create color list
color=df.close>df.open
color=color.replace(True,'green')
color=color.replace(False,'red')
# set data source
source = ColumnDataSource(data=dict(close=list(df.close.values),
adjusted_close=list(df.adjusted_close.values),
open=list(df.open.values),
high=list(df.high.values),
low=list(df.low.values),
volume=list(df.volume.values),
time=list(df.time.dt.date.values),
color=list(color.values)))
# hover setting
TOOLTIPS = [
("time", "@time{%F}"),
("adjusted close", "[email protected]_close"),
("close", "[email protected]"),
("open", "[email protected]"),
("high", "[email protected]"),
("low", "[email protected]"),
("volume","@volume")]
formatters={
'time' : 'datetime'}
hover = HoverTool(tooltips=TOOLTIPS,formatters=formatters,mode='vline')
# create figure
p = figure(title='{} Candlestick'.format(stock_select.value),plot_height=400,
tools="crosshair,save,undo,xpan,xwheel_zoom,xbox_zoom,reset",
active_scroll='xwheel_zoom',
x_axis_type="datetime")
p.add_tools(hover)
p.line('time', 'close', alpha=0.2, line_width=1, color='navy', source=source)
p.segment('time', 'high', 'time', 'low', line_width=1,color="black", source=source)
p.segment('time', 'open', 'time', 'close', line_width=3, color='color', source=source)
p.y_range = Range1d(min(source.data['close'])*0.3, max(source.data['close'])*1.05)
p.extra_y_ranges = {"volumes": Range1d(start=min(source.data['volume'])/2,
end=max(source.data['volume'])*2)}
p.add_layout(LinearAxis(y_range_name="volumes"), 'right')
p.vbar('time', width=3,top='volume', color=choice(all_palettes['Set2'][8]),alpha=0.5, y_range_name="volumes",source=source)
p.xaxis.axis_label = 'Time'
# set data source
_,_,_,institution_df=read_company(symbol_list[0])
source_ins = ColumnDataSource(data=dict(organization=list(institution_df.organization.values),
pctHeld=list(institution_df.pctHeld.values),
position=list(institution_df.position.values),
color=Set3[12][:len(institution_df)]))
s1=figure(x_range=source_ins.data['organization'],plot_height=300,plot_width=700,title='Institution Ownership')
s1.vbar(x='organization', top='position', width=0.8, color='color', source=source_ins)
s1.xaxis.major_label_orientation = pi/7
labels = LabelSet(x='organization', y='position', text='pctHeld', level='glyph',
x_offset=-15, y_offset=-10, source=source_ins, render_mode='canvas',text_font_size="8pt")
s1.add_layout(labels)
# callback funtion for Select tool 'stock_select'
def callback(attr,old,new):
symbol=stock_select.value
_,_,_,institution=read_company(symbol)
if symbol=='S&P500':
symbol='^GSPC'
database=CassandraStorage(symbol)
database.session.row_factory = pandas_factory
database.session.default_fetch_size = None
if symbol=='^GSPC':
symbol='GSPC'
query="SELECT * FROM {} WHERE time>'2015-01-01' ALLOW FILTERING;".format(symbol+'_historical')
rslt = database.session.execute(query, timeout=None)
df = rslt._current_rows
color=df.close>df.open
color=color.replace(True,'green')
color=color.replace(False,'red')
# update source data
source.data=dict(close=list(df.close.values),
adjusted_close=list(df.adjusted_close.values),
open=list(df.open.values),
high=list(df.high.values),
low=list(df.low.values),
volume=list(df.volume.values),
time=list(df.time.dt.date.values),
color=list(color.values))
source_ins.data=dict(organization=list(institution.organization.values),
pctHeld=list(institution.pctHeld.values),
position=list(institution.position.values),
color=Set3[12][:len(institution)])
p.title.text=symbol+' Candlestick'
p.y_range.start=min(source.data['close'])*0.3
p.y_range.end=max(source.data['close'])*1.05
p.extra_y_ranges['volumes'].start=min(source.data['volume'])/2.
p.extra_y_ranges['volumes'].end=max(source.data['volume'])*2.
s1.x_range.factors=source_ins.data['organization']
update_summary(symbol)
stock_select.on_change('value', callback)
return p,stock_select,summaryText,financialText,s1
def stream_price():
0
View Source File : visualize_utils.py
License : MIT License
Project Creator : ratsgo
License : MIT License
Project Creator : ratsgo
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)
"""
0
View Source File : visualize_utils.py
License : MIT License
Project Creator : ratsgo
License : MIT License
Project Creator : ratsgo
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)
def visualize_between_sentences(sentences, vec_list, palette="Viridis256",
0
View Source File : visualize_utils.py
License : MIT License
Project Creator : ratsgo
License : MIT License
Project Creator : ratsgo
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)
def visualize_words(words, vecs, palette="Viridis256", filename="/notebooks/embedding/words.png",
0
View Source File : visualize_utils.py
License : MIT License
Project Creator : ratsgo
License : MIT License
Project Creator : ratsgo
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)
def visualize_between_words(words, vecs, palette="Viridis256", filename="/notebooks/embedding/between-words.png",
0
View Source File : pyphi_plots.py
License : MIT License
Project Creator : salvadorgarciamunoz
License : MIT License
Project Creator : salvadorgarciamunoz
def loadings_map(mvmobj,dims,*,plotwidth=600):
"""
Scatter plot overlaying X and Y loadings
by Salvador Garcia-Munoz
([email protected] ,[email protected])
mvmobj: A model created with phi.pca or phi.pls
dims: what latent spaces to plot in x and y axes e.g. dims=[1,2]
"""
A= mvmobj['T'].shape[1]
num_varX=mvmobj['P'].shape[0]
if 'Q' in mvmobj:
lv_prefix='LV #'
lv_labels = []
for a in list(np.arange(A)+1):
lv_labels.append(lv_prefix+str(a))
if 'varidX' in mvmobj:
XVar=mvmobj['varidX']
else:
XVar = []
for n in list(np.arange(num_varX)+1):
XVar.append('XVar #'+str(n))
num_varY=mvmobj['Q'].shape[0]
if 'varidY' in mvmobj:
YVar=mvmobj['varidY']
else:
YVar = []
for n in list(np.arange(num_varY)+1):
YVar.append('YVar #'+str(n))
rnd_num=str(int(np.round(1000*np.random.random_sample())))
output_file("Loadings Map"+rnd_num+".html",title='Loadings Map')
x_ws = mvmobj['Ws'][:,dims[0]-1]
x_ws = x_ws/np.max(np.abs(x_ws))
y_ws = mvmobj['Ws'][:,dims[1]-1]
y_ws = y_ws/np.max(np.abs(y_ws))
x_q = mvmobj['Q'][:,dims[0]-1]
x_q = x_q/np.max(np.abs(x_q))
y_q = mvmobj['Q'][:,dims[1]-1]
y_q = y_q/np.max(np.abs(y_q))
TOOLS = "save,wheel_zoom,box_zoom,pan,reset,box_select,lasso_select"
TOOLTIPS = [
("index", "$index"),
("(x,y)", "($x, $y)"),
("Variable:","@names")
]
source1 = ColumnDataSource(data=dict(x=x_ws, y=y_ws,names=XVar))
source2 = ColumnDataSource(data=dict(x=x_q, y=y_q,names=YVar))
p = figure(tools=TOOLS, tooltips=TOOLTIPS,plot_width=plotwidth, title="Loadings Map LV["+str(dims[0])+"] - LV["+str(dims[1])+"]",
x_range=(-1.5,1.5),y_range=(-1.5,1.5))
p.circle('x', 'y', source=source1,size=10,color='darkblue')
p.circle('x', 'y', source=source2,size=10,color='red')
p.xaxis.axis_label = lv_labels [dims[0]-1]
p.yaxis.axis_label = lv_labels [dims[1]-1]
labelsX = LabelSet(x='x', y='y', text='names', level='glyph',x_offset=5, y_offset=5, source=source1, render_mode='canvas',text_color='darkgray')
labelsY = LabelSet(x='x', y='y', text='names', level='glyph',x_offset=5, y_offset=5, source=source2, render_mode='canvas',text_color='darkgray')
p.add_layout(labelsX)
p.add_layout(labelsY)
vline = Span(location=0, dimension='height', line_color='black', line_width=2)
# Horizontal line
hline = Span(location=0, dimension='width', line_color='black', line_width=2)
p.renderers.extend([vline, hline])
show(p)
else:
lv_prefix='PC #'
lv_labels = []
for a in list(np.arange(A)+1):
lv_labels.append(lv_prefix+str(a))
if 'varidX' in mvmobj:
XVar=mvmobj['varidX']
else:
XVar = []
for n in list(np.arange(num_varX)+1):
XVar.append('XVar #'+str(n))
rnd_num=str(int(np.round(1000*np.random.random_sample())))
output_file("Loadings Map"+rnd_num+".html",title='Loadings Map')
x_p = mvmobj['P'][:,dims[0]-1]
y_p = mvmobj['P'][:,dims[1]-1]
TOOLS = "save,wheel_zoom,box_zoom,pan,reset,box_select,lasso_select"
TOOLTIPS = [
("index", "$index"),
("(x,y)", "($x, $y)"),
("Variable:","@names")
]
source1 = ColumnDataSource(data=dict(x=x_p, y=y_p,names=XVar))
p = figure(tools=TOOLS, tooltips=TOOLTIPS,plot_width=plotwidth, title="Loadings Map PC["+str(dims[0])+"] - PC["+str(dims[1])+"]", x_range=(-1.5,1.5),y_range=(-1.5,1.5))
p.circle('x', 'y', source=source1,size=10,color='darkblue')
p.xaxis.axis_label = lv_labels [dims[0]-1]
p.yaxis.axis_label = lv_labels [dims[1]-1]
labelsX = LabelSet(x='x', y='y', text='names', level='glyph',x_offset=5, y_offset=5, source=source1, render_mode='canvas',text_color='darkgray')
p.add_layout(labelsX)
vline = Span(location=0, dimension='height', line_color='black', line_width=2)
# Horizontal line
hline = Span(location=0, dimension='width', line_color='black', line_width=2)
p.renderers.extend([vline, hline])
show(p)
return
def weighted_loadings(mvmobj,*,plotwidth=600,xgrid=False):
0
View Source File : pyphi_plots.py
License : MIT License
Project Creator : salvadorgarciamunoz
License : MIT License
Project Creator : salvadorgarciamunoz
def score_scatter(mvmobj,xydim,*,CLASSID=False,colorby=False,Xnew=False,add_ci=False,add_labels=False,add_legend=True,plotwidth=600,plotheight=600):
'''
Score scatter plot
by Salvador Garcia-Munoz
([email protected] ,[email protected])
mvmobj : PLS or PCA object from phyphi
xydim : LV to plot on x and y axes. eg [1,2] will plot t1 vs t2
CLASSID : Pandas DataFrame with CLASSIDS
colorby : Category (one of the CLASSIDS) to color by
Xnew : New data for which to make the score plot this routine evaluates and plots
add_ci : when = True will add confidence intervals
add_labels : When = True labels each point with Obs ID
plotwidth : If omitted, width is 600
'''
if isinstance(Xnew,bool):
if 'obsidX' in mvmobj:
ObsID_=mvmobj['obsidX']
else:
ObsID_ = []
for n in list(np.arange(mvmobj['T'].shape[0])+1):
ObsID_.append('Obs #'+str(n))
T_matrix=mvmobj['T']
else:
if isinstance(Xnew,np.ndarray):
X_=Xnew.copy()
ObsID_ = []
for n in list(np.arange(Xnew.shape[0])+1):
ObsID_.append('Obs #'+str(n))
elif isinstance(Xnew,pd.DataFrame):
X_=np.array(Xnew.values[:,1:]).astype(float)
ObsID_ = Xnew.values[:,0].astype(str)
ObsID_ = ObsID_.tolist()
if 'Q' in mvmobj:
xpred=phi.pls_pred(X_,mvmobj)
else:
xpred=phi.pca_pred(X_,mvmobj)
T_matrix=xpred['Tnew']
ObsNum_=[]
for n in list(range(1,len(ObsID_)+1)):
ObsNum_.append('Obs #'+str(n))
if isinstance(CLASSID,np.bool): # No CLASSIDS
rnd_num=str(int(np.round(1000*np.random.random_sample())))
output_file("Score_Scatter_"+rnd_num+".html",title='Score Scatter t['+str(xydim[0])+'] - t['+str(xydim[1])+ ']')
x_=T_matrix[:,[xydim[0]-1]]
y_=T_matrix[:,[xydim[1]-1]]
source = ColumnDataSource(data=dict(x=x_, y=y_,ObsID=ObsID_,ObsNum=ObsNum_))
TOOLS = "save,wheel_zoom,box_zoom,pan,reset,box_select,lasso_select"
TOOLTIPS = [
("Obs #", "@ObsNum"),
("(x,y)", "($x, $y)"),
("Obs: ","@ObsID")
]
p = figure(tools=TOOLS, tooltips=TOOLTIPS,plot_width=plotwidth,plot_height=plotheight, title='Score Scatter t['+str(xydim[0])+'] - t['+str(xydim[1])+ ']')
p.circle('x', 'y', source=source,size=7)
if add_ci:
T_aux1=mvmobj['T'][:,[xydim[0]-1]]
T_aux2=mvmobj['T'][:,[xydim[1]-1]]
T_aux = np.hstack((T_aux1,T_aux2))
st=(T_aux.T @ T_aux)/T_aux.shape[0]
[xd95,xd99,yd95p,yd95n,yd99p,yd99n]=phi.scores_conf_int_calc(st,mvmobj['T'].shape[0])
p.line(xd95,yd95p,line_color="gold",line_dash='dashed')
p.line(xd95,yd95n,line_color="gold",line_dash='dashed')
p.line(xd99,yd99p,line_color="red",line_dash='dashed')
p.line(xd99,yd99n,line_color="red",line_dash='dashed')
if add_labels:
labelsX = LabelSet(x='x', y='y', text='ObsID', level='glyph',x_offset=5, y_offset=5, source=source, render_mode='canvas')
p.add_layout(labelsX)
p.xaxis.axis_label = 't ['+str(xydim[0])+']'
p.yaxis.axis_label = 't ['+str(xydim[1])+']'
# Vertical line
vline = Span(location=0, dimension='height', line_color='black', line_width=2)
# Horizontal line
hline = Span(location=0, dimension='width', line_color='black', line_width=2)
p.renderers.extend([vline, hline])
show(p)
else: # YES CLASSIDS
Classes_=np.unique(CLASSID[colorby]).tolist()
A=len(Classes_)
colormap =cm.get_cmap("rainbow")
different_colors=A
color_mapping=colormap(np.linspace(0,1,different_colors),1,True)
bokeh_palette=["#%02x%02x%02x" % (r, g, b) for r, g, b in color_mapping[:,0:3]]
rnd_num=str(int(np.round(1000*np.random.random_sample())))
output_file("Score_Scatter_"+rnd_num+".html",title='Score Scatter t['+str(xydim[0])+'] - t['+str(xydim[1])+ ']')
x_=T_matrix[:,[xydim[0]-1]]
y_=T_matrix[:,[xydim[1]-1]]
TOOLS = "save,wheel_zoom,box_zoom,pan,reset,box_select,lasso_select"
TOOLTIPS = [
("Obs #", "@ObsNum"),
("(x,y)", "($x, $y)"),
("Obs: ","@ObsID"),
("Class:","@Class")
]
classid_=list(CLASSID[colorby])
legend_it = []
p = figure(tools=TOOLS, tooltips=TOOLTIPS,toolbar_location="above",plot_width=plotwidth,plot_height=plotheight,title='Score Scatter t['+str(xydim[0])+'] - t['+str(xydim[1])+ ']')
for classid_in_turn in Classes_:
x_aux = []
y_aux = []
obsid_aux = []
obsnum_aux = []
classid_aux = []
for i in list(range(len(ObsID_))):
if classid_[i]==classid_in_turn:
x_aux.append(x_[i][0])
y_aux.append(y_[i][0])
obsid_aux.append(ObsID_[i])
obsnum_aux.append(ObsNum_[i])
classid_aux.append(classid_in_turn)
source = ColumnDataSource(data=dict(x=x_aux, y=y_aux,ObsID=obsid_aux,ObsNum=obsnum_aux, Class=classid_aux))
color_=bokeh_palette[Classes_.index(classid_in_turn)]
if add_legend:
c = p.circle('x','y',source=source,color=color_)
aux_=classid_in_turn
if isinstance(aux_,(float,int)):
aux_=str(aux_)
#legend_it.append((classid_in_turn, [c]))
legend_it.append((aux_, [c]))
else:
p.circle('x','y',source=source,color=color_)
if add_labels:
labelsX = LabelSet(x='x', y='y', text='ObsID', level='glyph',x_offset=5, y_offset=5, source=source, render_mode='canvas')
p.add_layout(labelsX)
if add_ci:
T_aux1=mvmobj['T'][:,[xydim[0]-1]]
T_aux2=mvmobj['T'][:,[xydim[1]-1]]
T_aux = np.hstack((T_aux1,T_aux2))
st=(T_aux.T @ T_aux)/T_aux.shape[0]
[xd95,xd99,yd95p,yd95n,yd99p,yd99n]=phi.scores_conf_int_calc(st,mvmobj['T'].shape[0])
p.line(xd95,yd95p,line_color="gold",line_dash='dashed')
p.line(xd95,yd95n,line_color="gold",line_dash='dashed')
p.line(xd99,yd99p,line_color="red",line_dash='dashed')
p.line(xd99,yd99n,line_color="red",line_dash='dashed')
p.xaxis.axis_label = 't ['+str(xydim[0])+']'
p.yaxis.axis_label = 't ['+str(xydim[1])+']'
# Vertical line
vline = Span(location=0, dimension='height', line_color='black', line_width=2)
# Horizontal line
hline = Span(location=0, dimension='width', line_color='black', line_width=2)
p.renderers.extend([vline, hline])
if add_legend:
legend = Legend(items=legend_it, location='top_right')
p.add_layout(legend, 'right')
legend.click_policy="hide"
show(p)
return
def score_line(mvmobj,dim,*,CLASSID=False,colorby=False,Xnew=False,add_ci=False,add_labels=False,add_legend=True,plotline=True,plotwidth=600,plotheight=600):
0
View Source File : pyphi_plots.py
License : MIT License
Project Creator : salvadorgarciamunoz
License : MIT License
Project Creator : salvadorgarciamunoz
def score_line(mvmobj,dim,*,CLASSID=False,colorby=False,Xnew=False,add_ci=False,add_labels=False,add_legend=True,plotline=True,plotwidth=600,plotheight=600):
'''
Score scatter plot
by Salvador Garcia-Munoz
([email protected] ,[email protected])
mvmobj : PLS or PCA object from phyphi
dim : LV to plot eg "1" will plot t1 vs observation #
CLASSID : Pandas DataFrame with CLASSIDS
colorby : Category (one of the CLASSIDS) to color by
Xnew : New data for which to make the score plot this routine evaluates and plots
add_ci : When = True will add confidence intervals
add_labels : When =True will display Obs ID per point
plotwidth : When Omitted is = 600
plotline : Adds a conecting line between dots [True by default]
'''
if not(isinstance(dim,list)):
if isinstance(dim, int):
dim=[dim]
if isinstance(Xnew,bool):
if 'obsidX' in mvmobj:
ObsID_=mvmobj['obsidX']
else:
ObsID_ = []
for n in list(np.arange(mvmobj['T'].shape[0])+1):
ObsID_.append('Obs #'+str(n))
T_matrix=mvmobj['T']
else:
if isinstance(Xnew,np.ndarray):
X_=Xnew.copy()
ObsID_ = []
for n in list(np.arange(Xnew.shape[0])+1):
ObsID_.append('Obs #'+str(n))
elif isinstance(Xnew,pd.DataFrame):
X_=np.array(Xnew.values[:,1:]).astype(float)
ObsID_ = Xnew.values[:,0].astype(str)
ObsID_ = ObsID_.tolist()
if 'Q' in mvmobj:
xpred=phi.pls_pred(X_,mvmobj)
else:
xpred=phi.pca_pred(X_,mvmobj)
T_matrix=xpred['Tnew']
ObsNum_=[]
for n in list(range(1,len(ObsID_)+1)):
ObsNum_.append('Obs #'+str(n))
if isinstance(CLASSID,np.bool): # No CLASSIDS
rnd_num=str(int(np.round(1000*np.random.random_sample())))
output_file("Score_Line_"+rnd_num+".html",title='Score Line t['+str(dim[0])+ ']')
y_=T_matrix[:,[dim[0]-1]]
x_=list(range(1,y_.shape[0]+1))
source = ColumnDataSource(data=dict(x=x_, y=y_,ObsID=ObsID_,ObsNum=ObsNum_))
TOOLS = "save,wheel_zoom,box_zoom,pan,reset,box_select,lasso_select"
TOOLTIPS = [
("Obs#", "@ObsNum"),
("(x,y)", "($x, $y)"),
("Obs: ","@ObsID")
]
p = figure(tools=TOOLS, tooltips=TOOLTIPS,plot_width=plotwidth,plot_height=plotheight, title='Score Line t['+str(dim[0])+']' )
p.circle('x', 'y', source=source,size=7)
if plotline:
p.line('x', 'y', source=source)
if add_ci:
lim95,lim99=phi.single_score_conf_int(mvmobj['T'][:,[dim[0]-1]])
p.line(x_, lim95,line_color="gold",line_dash='dashed')
p.line(x_,-lim95,line_color="gold",line_dash='dashed')
p.line(x_, lim99,line_color="red",line_dash='dashed')
p.line(x_,-lim99,line_color="red",line_dash='dashed')
if add_labels:
labelsX = LabelSet(x='x', y='y', text='ObsID', level='glyph',x_offset=5, y_offset=5, source=source, render_mode='canvas')
p.add_layout(labelsX)
p.xaxis.axis_label = 'Observation'
p.yaxis.axis_label = 't ['+str(dim[0])+']'
show(p)
else: # YES CLASSIDS
Classes_=np.unique(CLASSID[colorby]).tolist()
A=len(Classes_)
colormap =cm.get_cmap("rainbow")
different_colors=A
color_mapping=colormap(np.linspace(0,1,different_colors),1,True)
bokeh_palette=["#%02x%02x%02x" % (r, g, b) for r, g, b in color_mapping[:,0:3]]
rnd_num=str(int(np.round(1000*np.random.random_sample())))
output_file("Score_Line_"+rnd_num+".html",title='Score Line t['+str(dim[0])+ ']')
y_=T_matrix[:,[dim[0]-1]]
x_=list(range(1,y_.shape[0]+1))
TOOLS = "save,wheel_zoom,box_zoom,pan,reset,box_select,lasso_select"
TOOLTIPS = [
("Obs#", "@ObsNum"),
("(x,y)", "($x, $y)"),
("Obs: ","@ObsID"),
("Class:","@Class")
]
classid_=list(CLASSID[colorby])
legend_it = []
p = figure(tools=TOOLS, tooltips=TOOLTIPS,toolbar_location="above",plot_width=plotwidth,plot_height=plotheight, title='Score Line t['+str(dim[0])+ ']')
for classid_in_turn in Classes_:
x_aux=[]
y_aux=[]
obsid_aux=[]
classid_aux=[]
obsnum_aux=[]
for i in list(range(len(ObsID_))):
if classid_[i]==classid_in_turn:
x_aux.append(x_[i])
y_aux.append(y_[i][0])
obsid_aux.append(ObsID_[i])
obsnum_aux.append(ObsNum_[i])
classid_aux.append(classid_in_turn)
source = ColumnDataSource(data=dict(x=x_aux, y=y_aux,ObsID=obsid_aux,ObsNum=obsnum_aux,Class=classid_aux))
color_=bokeh_palette[Classes_.index(classid_in_turn)]
c=p.circle('x','y',source=source,color=color_)
if plotline:
c1=p.line('x','y',source=source,color=color_)
#added to allow numbers in classids
aux_=classid_in_turn
if isinstance(aux_,(float,int)):
aux_=str(aux_)
#
if add_legend and plotline:
# legend_it.append((classid_in_turn, [c,c1]))
legend_it.append((aux_, [c,c1]))
if add_legend and not(plotline):
# legend_it.append((classid_in_turn, [c]))
legend_it.append((aux_, [c]))
if add_labels:
labelsX = LabelSet(x='x', y='y', text='ObsID', level='glyph',x_offset=5, y_offset=5, source=source, render_mode='canvas')
p.add_layout(labelsX)
if add_ci:
lim95,lim99=phi.single_score_conf_int(mvmobj['T'][:,[dim[0]-1]])
p.line(x_, lim95,line_color="gold",line_dash='dashed')
p.line(x_,-lim95,line_color="gold",line_dash='dashed')
p.line(x_, lim99,line_color="red",line_dash='dashed')
p.line(x_,-lim99,line_color="red",line_dash='dashed')
p.xaxis.axis_label = 'Observation'
p.yaxis.axis_label = 't ['+str(dim[0])+']'
if add_legend:
legend = Legend(items=legend_it, location='top_right')
p.add_layout(legend, 'right')
legend.click_policy="hide"
show(p)
return
def diagnostics(mvmobj,*,Xnew=False,Ynew=False,score_plot_xydim=False,plotwidth=600,ht2_logscale=False,spe_logscale=False):
0
View Source File : plots.py
License : Apache License 2.0
Project Creator : scaleoutsystems
License : Apache License 2.0
Project Creator : scaleoutsystems
def make_netgraph_plot(self, df, df_nodes):
"""
Create FEDn network visualization.
:param df: pandas dataframe with defined edges
:type df: pandas.Dataframe
:param df_nodes:pandas dataframe with defined nodes
:type df_nodes: pandas.Dataframe
:return: Bokeh plot with the graph
:rtype: bokeh.plotting.figure.Figure
"""
if df.empty:
#no combiners and thus no clients, plot only reducer
plot = self.make_single_node_plot()
return plot
G = networkx.from_pandas_edgelist(df, 'source', 'target', create_using=networkx.Graph())
degrees = dict(networkx.degree(G))
networkx.set_node_attributes(G, name='degree', values=degrees)
number_to_adjust_by = 20
adjusted_node_size = dict([(node, degree + number_to_adjust_by) for node, degree in networkx.degree(G)])
networkx.set_node_attributes(G, name='adjusted_node_size', values=adjusted_node_size)
# community
from networkx.algorithms import community
communities = community.greedy_modularity_communities(G)
# Create empty dictionaries
modularity_class = {}
modularity_color = {}
# Loop through each community in the network
for community_number, community in enumerate(communities):
# For each member of the community, add their community number and a distinct color
for name in community:
modularity_class[name] = community_number
modularity_color[name] = Spectral8[community_number]
# Add modularity class and color as attributes from the network above
networkx.set_node_attributes(G, modularity_class, 'modularity_class')
networkx.set_node_attributes(G, modularity_color, 'modularity_color')
node_role = {k:v for k,v in zip(df_nodes.id, df_nodes.role)}
networkx.set_node_attributes(G, node_role, 'role')
node_status = {k:v for k,v in zip(df_nodes.id, df_nodes.status)}
networkx.set_node_attributes(G, node_status, 'status')
node_name = {k:v for k,v in zip(df_nodes.id, df_nodes.name)}
networkx.set_node_attributes(G, node_name, 'name')
# Choose colors for node and edge highlighting
node_highlight_color = 'white'
edge_highlight_color = 'black'
# Choose attributes from G network to size and color by — setting manual
# size (e.g. 10) or color (e.g. 'skyblue') also allowed
size_by_this_attribute = 'adjusted_node_size'
color_by_this_attribute = 'modularity_color'
# Establish which categories will appear when hovering over each node
HOVER_TOOLTIPS = [
("Name", "@name"),
("Role", "@role"),
("Status", "@status"),
("Id", "@index"),
]
# Create a plot — set dimensions, toolbar, and title
plot = figure(tooltips=HOVER_TOOLTIPS,
tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',
width=725, height=460, sizing_mode='stretch_width',
x_range=Range1d(-1.5, 1.5), y_range=Range1d(-1.5, 1.5))
# Create a network graph object
# https://networkx.github.io/documentation/networkx-1.9/reference/generated/networkx.drawing.layout.spring_layout.html
# if one like lock reducer add args: pos={'reducer':(0,1)}, fixed=['reducer']
network_graph = from_networkx(G, networkx.spring_layout, scale=1, center=(0, 0), seed=45)
# Set node sizes and colors according to node degree (color as category from attribute)
network_graph.node_renderer.glyph = Circle(size=size_by_this_attribute, fill_color=color_by_this_attribute)
# Set node highlight colors
network_graph.node_renderer.hover_glyph = Circle(size=size_by_this_attribute, fill_color=node_highlight_color,
line_width=2)
network_graph.node_renderer.selection_glyph = Circle(size=size_by_this_attribute,
fill_color=node_highlight_color, line_width=2)
# Set edge opacity and width
network_graph.edge_renderer.glyph = MultiLine(line_alpha=0.5, line_width=1)
# Set edge highlight colors
network_graph.edge_renderer.selection_glyph = MultiLine(line_color=edge_highlight_color, line_width=2)
network_graph.edge_renderer.hover_glyph = MultiLine(line_color=edge_highlight_color, line_width=2)
# Highlight nodes and edges
network_graph.selection_policy = NodesAndLinkedEdges()
network_graph.inspection_policy = NodesAndLinkedEdges()
plot.renderers.append(network_graph)
#Node labels, red if status is offline, green is active
x, y = zip(*network_graph.layout_provider.graph_layout.values())
node_names = list(G.nodes(data='name'))
node_status = list(G.nodes(data='status'))
idx_offline = []
idx_online = []
node_labels = []
for e, n in enumerate(node_names):
if node_status[e][1] == 'active':
idx_online.append(e)
else:
idx_offline.append(e)
node_labels.append(n[1])
source_on = ColumnDataSource({'x': numpy.asarray(x)[idx_online], 'y': numpy.asarray(y)[idx_online], 'name': numpy.asarray(node_labels)[idx_online]})
labels = LabelSet(x='x', y='y', text='name', source=source_on, background_fill_color='#4bbf73', text_font_size='15px',
background_fill_alpha=.7, x_offset=-20, y_offset=10)
plot.renderers.append(labels)
source_off = ColumnDataSource({'x': numpy.asarray(x)[idx_offline], 'y': numpy.asarray(y)[idx_offline], 'name': numpy.asarray(node_labels)[idx_offline]})
labels = LabelSet(x='x', y='y', text='name', source=source_off, background_fill_color='#d9534f', text_font_size='15px',
background_fill_alpha=.7, x_offset=-20, y_offset=10)
plot.renderers.append(labels)
plot.axis.visible = False
plot.grid.visible = False
plot.outline_line_color = None
return plot
0
View Source File : analyze.py
License : GNU General Public License v3.0
Project Creator : varadaio
License : GNU General Public License v3.0
Project Creator : varadaio
def wall_by_selectivity_bins(stats, bins=10, max_selectivity=1, title="Wall time of table scans by selectivity bins"):
selectivity = []
wall = []
for s in stats:
node_map = {node["id"]: node for node in nodes_from_stats(s)}
for op in scan_operators(s["operators"]):
node_id = op["node_id"]
node = node_map[node_id]
if op["input_rows"]:
wall.append(op["input_wall"] + op["output_wall"] + op["finish_wall"])
selectivity.append(op["output_rows"] / op["input_rows"])
if not selectivity:
return
bin_step = 1. / bins
wall = numpy.array(wall)
selectivity_bins = numpy.abs(numpy.round(numpy.array(selectivity) - bin_step / 2, 1))
# each bin should be represented
wall = numpy.append(wall, numpy.zeros(bins))
selectivity_bins = numpy.append(selectivity_bins, numpy.arange(0, max_selectivity, bin_step))
# convert bin to string for representation
selectivity_bins = numpy.array([
'%0.2f' % x if x < = max_selectivity + 1e-9 else "Above" for x in selectivity_bins])
# convert total_wall to percentage for representation
wall = wall / wall.sum() * 100
items = groupby(selectivity_bins, wall, sum)
items.sort(key=lambda item: item[0])
selectivity_bins, total_wall = zip(*items)
tooltips = ['Selectivity %0.2f-%0.2f: %0.2f%% of wall time' % (float(x), float(x) + bin_step, y) for (x, y) in
zip(selectivity_bins, total_wall) if x != 'Above']
if 'Above' == selectivity_bins[-1]:
tooltips.append('Selectivity above %0.2f: %0.2f%% of wall time' % (float(selectivity_bins[-2]), total_wall[-1]))
source = ColumnDataSource(dict(
x=selectivity_bins,
top=total_wall,
text=['%0.1f%%' % x for x in total_wall],
tooltips=tooltips))
p = figure(
width=800,
title=title,
x_axis_label="Selectivity Bin",
y_axis_label="Wall time (%)",
sizing_mode="scale_width",
x_range=selectivity_bins,
y_range=(0, 100),
tools=TOOLS_SIMPLE,
tooltips='@tooltips')
labels = LabelSet(x='x', y='top', text='text', level='glyph',
x_offset=-800 / len(selectivity_bins) / 4, y_offset=5, source=source, render_mode='canvas',
text_font_size="10pt")
p.vbar(source=source, x='x', top='top', width=1, line_width=1, line_color='black')
p.add_layout(labels)
return p
@run
0
View Source File : plotter.py
License : MIT License
Project Creator : ylabbe
License : MIT License
Project Creator : ylabbe
def plot_maskrcnn_bboxes(self, f, detections, colors='red', text=None, text_auto=True, line_width=2, source_id=''):
boxes = detections.bboxes
if text_auto:
text = [f'{row.label} {row.score:.2f}' for _, row in detections.infos.iterrows()]
boxes = self.numpy(boxes)
xs = []
ys = []
patch_colors = []
if text is not None:
assert len(text) == len(boxes)
text_x, text_y = [], []
if isinstance(colors, (list, tuple, np.ndarray)):
assert len(colors) == len(boxes)
else:
colors = [colors for _ in range(len(boxes))]
# Convert boxes to bokeh coordinate system
boxes = np.array(boxes)
boxes[:, [1, 3]] = f.plot_height - boxes[:, [1, 3]]
for n, box in enumerate(boxes):
x1, y1, x2, y2 = box
xs.append([x1, x2, x2, x1])
ys.append([y1, y1, y2, y2])
patch_colors.append(colors[n])
if text is not None:
text_x.append(x1)
text_y.append(y1)
source, new = self.get_source(f'{f.id}/{source_id}/bboxes')
if new:
f.patches(xs='xs', ys='ys', source=source,
line_width=line_width, color='colors', fill_alpha=0.0)
if text is not None:
labelset = LabelSet(x='text_x', y='text_y', text='text',
text_align='left', text_baseline='bottom',
text_color='white',
source=source, background_fill_color='colors',
text_font_size="5pt")
f.add_layout(labelset)
data = dict(xs=xs, ys=ys, colors=patch_colors)
if text is not None:
data.update(text_x=text_x, text_y=text_y, text=text)
source.data = data
return f
def _resize(self, im, size):