Skip to content

Commit

Permalink
Merge fc9bb38 into 1c84a80
Browse files Browse the repository at this point in the history
  • Loading branch information
ahartikainen committed Nov 21, 2019
2 parents 1c84a80 + fc9bb38 commit 835c16e
Show file tree
Hide file tree
Showing 10 changed files with 731 additions and 192 deletions.
135 changes: 135 additions & 0 deletions arviz/plots/backends/bokeh/bokeh_compareplot.py
@@ -0,0 +1,135 @@
"""Bokeh Compareplot."""
import bokeh.plotting as bkp
from bokeh.models import Span


def _compareplot(
ax,
comp_df,
figsize,
plot_ic_diff,
plot_standard_error,
insample_dev,
yticks_pos,
yticks_labels,
line_width,
plot_kwargs,
information_criterion,
step,
show,
):

if ax is None:
tools = ",".join(
[
"pan",
"wheel_zoom",
"box_zoom",
"lasso_select",
"poly_select",
"undo",
"redo",
"reset",
"save,hover",
]
)
ax = bkp.figure(
width=figsize[0] * 90, height=figsize[1] * 90, output_backend="webgl", tools=tools
)

yticks_pos = list(yticks_pos)

if plot_ic_diff:
yticks_labels[0] = comp_df.index[0]
yticks_labels[2::2] = comp_df.index[1:]

ax.yaxis.ticker = yticks_pos
ax.yaxis.major_label_overrides = {
dtype(key): value
for key, value in zip(yticks_pos, yticks_labels)
for dtype in (int, float)
if (dtype(key) - key == 0)
}
print(ax.yaxis.major_label_overrides)
# create the coordinates for the errorbars
err_xs = []
err_ys = []

for x, y, xerr in zip(
comp_df[information_criterion].iloc[1:], yticks_pos[1::2], comp_df.dse[1:]
):
err_xs.append((x - xerr, x + xerr))
err_ys.append((y, y))

# plot them
ax.triangle(
comp_df[information_criterion].iloc[1:],
yticks_pos[1::2],
line_color=plot_kwargs.get("color_dse", "grey"),
fill_color=plot_kwargs.get("color_dse", "grey"),
line_width=2,
size=6,
)
ax.multi_line(err_xs, err_ys, line_color=plot_kwargs.get("color_dse", "grey"))

else:
yticks_labels = comp_df.index
ax.yaxis.ticker = yticks_pos[::2]
ax.yaxis.major_label_overrides = {
key: value for key, value in zip(yticks_pos[::2], yticks_labels)
}

ax.circle(
comp_df[information_criterion],
yticks_pos[::2],
line_color=plot_kwargs.get("color_ic", "black"),
fill_color=None,
line_width=2,
size=6,
)

if plot_standard_error:
# create the coordinates for the errorbars
err_xs = []
err_ys = []

for x, y, xerr in zip(comp_df[information_criterion], yticks_pos[::2], comp_df.se):
err_xs.append((x - xerr, x + xerr))
err_ys.append((y, y))

# plot them
ax.multi_line(err_xs, err_ys, line_color=plot_kwargs.get("color_ic", "black"))

if insample_dev:
ax.circle(
comp_df[information_criterion] - (2 * comp_df["p_" + information_criterion]),
yticks_pos[::2],
line_color=plot_kwargs.get("color_insample_dev", "black"),
fill_color=plot_kwargs.get("color_insample_dev", "black"),
line_width=2,
size=6,
)

vline = Span(
location=comp_df[information_criterion].iloc[0],
dimension="height",
line_color=plot_kwargs.get("color_ls_min_ic", "grey"),
line_width=line_width,
line_dash=plot_kwargs.get("ls_min_ic", "dashed"),
)

ax.renderers.append(vline)

scale_col = information_criterion + "_scale"
if scale_col in comp_df:
scale = comp_df[scale_col].iloc[0].capitalize()
else:
scale = "Deviance"
ax.xaxis.axis_label = scale
ax.y_range._property_values["start"] = -1 + step # pylint: disable=protected-access
ax.y_range._property_values["end"] = 0 - step # pylint: disable=protected-access

if show:
bkp.show(ax)

return ax
165 changes: 165 additions & 0 deletions arviz/plots/backends/bokeh/bokeh_densityplot.py
@@ -0,0 +1,165 @@
"""Bokeh Densityplot."""
import bokeh.plotting as bkp
from bokeh.models.annotations import Title
from bokeh.layouts import gridplot
import numpy as np

from ....stats import hpd
from ...kdeplot import _fast_kde
from ...plot_utils import make_label


def _plot_density(
ax,
all_labels,
to_plot,
colors,
bw,
line_width,
markersize,
credible_interval,
point_estimate,
hpd_markers,
outline,
shade,
data_labels,
show,
):
axis_map = {label: ax_ for label, ax_ in zip(all_labels, ax.flatten())}
if data_labels is None:
data_labels = {}

for m_idx, plotters in enumerate(to_plot):
for ax_idx, (var_name, selection, values) in enumerate(plotters):
label = make_label(var_name, selection)

if data_labels:
data_label = data_labels[m_idx]
if ax_idx != 0 or data_label == "":
data_label = None
else:
data_label = None

_d_helper(
values.flatten(),
label,
colors[m_idx],
bw,
line_width,
markersize,
credible_interval,
point_estimate,
hpd_markers,
outline,
shade,
axis_map[label],
data_label=data_label,
)

if show:
grid = gridplot([list(item) for item in ax], toolbar_location="above")
bkp.show(grid)

return ax


def _d_helper(
vec,
vname,
color,
bw,
line_width,
markersize,
credible_interval,
point_estimate,
hpd_markers,
outline,
shade,
ax,
data_label,
):
extra = dict()
if data_label is not None:
extra["legend_label"] = data_label

if vec.dtype.kind == "f":
if credible_interval != 1:
hpd_ = hpd(vec, credible_interval, multimodal=False)
new_vec = vec[(vec >= hpd_[0]) & (vec <= hpd_[1])]
else:
new_vec = vec

density, xmin, xmax = _fast_kde(new_vec, bw=bw)
density *= credible_interval
x = np.linspace(xmin, xmax, len(density))
ymin = density[0]
ymax = density[-1]

if outline:
ax.line(x, density, line_color=color, line_width=line_width, **extra)
ax.line(
[xmin, xmin],
[-ymin / 100, ymin],
line_color=color,
line_dash="solid",
line_width=line_width,
)
ax.line(
[xmax, xmax],
[-ymax / 100, ymax],
line_color=color,
line_dash="solid",
line_width=line_width,
)

if shade:
ax.patch(
np.r_[x[::-1], x, x[-1:]],
np.r_[np.zeros_like(x), density, [0]],
fill_color=color,
fill_alpha=shade,
**extra
)

else:
xmin, xmax = hpd(vec, credible_interval, multimodal=False)
bins = range(xmin, xmax + 2)

hist, edges = np.histogram(vec, density=True, bins=bins)

if outline:
ax.quad(
top=hist,
bottom=0,
left=edges[:-1],
right=edges[1:],
line_color=color,
fill_color=None,
**extra
)
else:
ax.quad(
top=hist,
bottom=0,
left=edges[:-1],
right=edges[1:],
line_color=color,
fill_color=color,
fill_alpha=shade,
**extra
)

if hpd_markers:
ax.diamond(xmin, 0, line_color="black", fill_color=color, size=markersize)
ax.diamond(xmax, 0, line_color="black", fill_color=color, size=markersize)

if point_estimate is not None:
if point_estimate == "mean":
est = np.mean(vec)
elif point_estimate == "median":
est = np.median(vec)
ax.circle(est, 0, fill_color=color, line_color="black", size=markersize)

_title = Title()
_title.text = vname
ax.title = _title
15 changes: 14 additions & 1 deletion arviz/plots/backends/bokeh/bokeh_distplot.py
Expand Up @@ -30,7 +30,20 @@ def _plot_dist_bokeh(
):

if ax is None:
ax = bkp.figure(width=500, height=500)
tools = ",".join(
[
"pan",
"wheel_zoom",
"box_zoom",
"lasso_select",
"poly_select",
"undo",
"redo",
"reset",
"save,hover",
]
)
ax = bkp.figure(width=500, height=500, output_backend="webgl", tools=tools)

if kind == "auto":
kind = "hist" if values.dtype.kind == "i" else "kde"
Expand Down
15 changes: 14 additions & 1 deletion arviz/plots/backends/bokeh/bokeh_kdeplot.py
Expand Up @@ -41,7 +41,20 @@ def _plot_kde_bokeh(
show=True,
):
if ax is None:
ax = bkp.figure(width=500, height=500, output_backend="webgl")
tools = ",".join(
[
"pan",
"wheel_zoom",
"box_zoom",
"lasso_select",
"poly_select",
"undo",
"redo",
"reset",
"save,hover",
]
)
ax = bkp.figure(width=500, height=500, output_backend="webgl", tools=tools)

if legend and label is not None:
plot_kwargs["legend_label"] = label
Expand Down
14 changes: 13 additions & 1 deletion arviz/plots/backends/bokeh/bokeh_traceplot.py
Expand Up @@ -198,7 +198,19 @@ def _plot_trace_bokeh(

backend_kwargs.setdefault(
"tools",
("pan,wheel_zoom,box_zoom," "lasso_select,poly_select," "undo,redo,reset,save,hover"),
",".join(
[
"pan",
"wheel_zoom",
"box_zoom",
"lasso_select",
"poly_select",
"undo",
"redo",
"reset",
"save,hover",
]
),
)
backend_kwargs.setdefault("output_backend", "webgl")
backend_kwargs.setdefault("height", figsize[1])
Expand Down

0 comments on commit 835c16e

Please sign in to comment.