Skip to content

Commit

Permalink
Added bokeh_wegbl backend
Browse files Browse the repository at this point in the history
  • Loading branch information
javiber committed Apr 12, 2024
1 parent f5ba9db commit 8f98388
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Improvements

- Added `before_first` and `after_last` parameters to `EventSet.tick` and `EventSet.tick_calendar`
- Added `bokeh_webgl` as a possible `backend` for `tp.plot`.

### Fixes

Expand Down
9 changes: 8 additions & 1 deletion temporian/implementation/numpy/data/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,16 @@ def bokeh_backend():
return plotter_bokeh.Plotter


def bokeh_webgl_backend():
from temporian.implementation.numpy.data import plotter_bokeh

return plotter_bokeh.PlotterWebGL


BACKENDS: Dict[str, Callable] = {
"matplotlib": matplotlib_backend,
"bokeh": bokeh_backend,
"bokeh_webgl": bokeh_webgl_backend,
}


Expand Down Expand Up @@ -279,7 +286,7 @@ def plot(
effectively selects a backend that support interactive plotting.
Ignored if "backend" is set.
backend: Plotting library to use. Possible values are: matplotlib,
bokeh. If set, overrides the "interactive" argument.
bokeh, and bokeh_webgl. If set, overrides the "interactive" argument.
merge: If true, plots all features in the same plots. If false, plots
features in separate plots. merge=True on event-sets [e1, e2] is
equivalent to plotting (e1, e2).
Expand Down
109 changes: 58 additions & 51 deletions temporian/implementation/numpy/data/plotter_bokeh.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@


class Plotter(PlotterBackend):
output_backend = "canvas"

def __init__(self, num_plots: int, options: Options):
super().__init__(num_plots, options)

Expand All @@ -46,7 +48,7 @@ def finalize_subplot(

def ensure_cur_is_available(self, categorical_values: Optional[List[str]]):
if self.cur_fig is None:
self.cur_fig = create_fig(
self.cur_fig = self.create_fig(
self.cur_title,
self.cur_is_unix_timestamp,
self.options,
Expand Down Expand Up @@ -152,57 +154,62 @@ def finalize(self):
show(figure_set)
return figure_set

def create_fig(
self,
title: Optional[str],
is_unix_timestamp: bool,
options: Options,
categorical_values: Optional[List[str]],
):
tools = [
"xpan",
"pan",
"xwheel_zoom",
"ywheel_zoom",
"box_zoom",
"reset",
"undo",
"save",
"hover",
]

fig_args = {}
if is_unix_timestamp:
fig_args["x_axis_type"] = "datetime"
if title:
fig_args["title"] = title

if options.min_time is not None or options.max_time is not None:
args = {}
if options.min_time is not None:
args["start"] = (
convert_timestamp_to_datetime(options.min_time)
if is_unix_timestamp
else options.min_time
)
if options.max_time is not None:
args["end"] = (
convert_timestamp_to_datetime(options.max_time)
if is_unix_timestamp
else options.max_time
)
fig_args["x_range"] = Range1d(**args)

if categorical_values is not None:
fig_args["y_range"] = categorical_values

def create_fig(
title: Optional[str],
is_unix_timestamp: bool,
options: Options,
categorical_values: Optional[List[str]],
):
tools = [
"xpan",
"pan",
"xwheel_zoom",
"ywheel_zoom",
"box_zoom",
"reset",
"undo",
"save",
"hover",
]

fig_args = {}
if is_unix_timestamp:
fig_args["x_axis_type"] = "datetime"
if title:
fig_args["title"] = title

if options.min_time is not None or options.max_time is not None:
args = {}
if options.min_time is not None:
args["start"] = (
convert_timestamp_to_datetime(options.min_time)
if is_unix_timestamp
else options.min_time
)
if options.max_time is not None:
args["end"] = (
convert_timestamp_to_datetime(options.max_time)
if is_unix_timestamp
else options.max_time
)
fig_args["x_range"] = Range1d(**args)
# Note: Ranges cannot be set after the figure is created see:
# https://discourse.bokeh.org/t/updating-y-range-to-categorical/2397/3
fig = figure(
width=options.width_px,
height=options.height_per_plot_px,
tools=tools,
output_backend=self.output_backend,
**fig_args,
)

if categorical_values is not None:
fig_args["y_range"] = categorical_values
return fig

# Note: Ranges cannot be set after the figure is created see:
# https://discourse.bokeh.org/t/updating-y-range-to-categorical/2397/3
fig = figure(
width=options.width_px,
height=options.height_per_plot_px,
tools=tools,
**fig_args,
)

return fig
class PlotterWebGL(Plotter):
output_backend = "webgl"

0 comments on commit 8f98388

Please sign in to comment.