Skip to content

✨ register plotly-resampler #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8df121e
:construction: 1st quick implementation of register plotly-resampler
jvdd Jun 9, 2022
a92a5a4
:sparkles: first draft of `add_traces`
jonasvdd Jun 9, 2022
22a57f3
:see_no_evil:
jonasvdd Jun 9, 2022
96b3bbb
:recycle: cleanup + fix some :bug:s
jvdd Jun 10, 2022
f1cc248
:broom: move utils.py to figure_resampler + add tests
jvdd Jun 10, 2022
6b682cd
:recycle: test figure as dictionary
jvdd Jun 10, 2022
ca8976b
:fire: add automatic google colab ipywidget support
jvdd Jun 11, 2022
6863c54
:crayon:
jonasvdd Jun 11, 2022
75a57be
:see_no_evil:
jonasvdd Jun 11, 2022
52607dc
:muscle: adding the (un)register functions into __init__
jonasvdd Jun 11, 2022
e4fbe7b
:mag: docs + check for fr & fwr
jonasvdd Jun 11, 2022
1d0cbab
:fire: compose behavior v1 :goat:
jonasvdd Jun 11, 2022
1db6486
:hedgehog: removing unused methods
jonasvdd Jun 11, 2022
b9403a1
:olive: first draft of tests
jonasvdd Jun 12, 2022
9639c06
:cat2: 2nd iteration of compose functionality
jonasvdd Jun 12, 2022
3b4476e
:beaver: composability tests :feet:
jonasvdd Jun 12, 2022
3c80a8f
:deer: adjusting some trace template data
jonasvdd Jun 12, 2022
7769376
:construction: fix pandas deprecation warning
jonasvdd Jun 13, 2022
7dc7df6
:bug: fix small bug where we drop the `hf_data`
jonasvdd Jun 13, 2022
b8ecf99
:mag: adding `limit_to_view` and `agg` `no_agg` tests
jonasvdd Jun 13, 2022
12c4e93
:pen: review code
jvdd Jun 14, 2022
bb057cd
:mag: apply review suggestions
jonasvdd Jun 14, 2022
9b95694
Merge branch 'compose_figs' of github.com:predict-idlab/plotly-resamp…
jonasvdd Jun 14, 2022
2b7f2e7
Merge pull request #72 from predict-idlab/compose_figs
jonasvdd Jun 14, 2022
50a2a2b
:sparkles: improve registering implementation
jvdd Jun 15, 2022
7517dfd
:fire: add registering tests
jvdd Jun 15, 2022
22f32ac
:book: updating docs
jonasvdd Jun 16, 2022
6ab5701
:pencil: review
jonasvdd Jun 16, 2022
d5fe2d1
:pencil: review
jonasvdd Jun 16, 2022
59d753c
:goggles: updating docs conf
jonasvdd Jun 16, 2022
392f8aa
:virgo: adding hf_data_copy tests
jonasvdd Jun 16, 2022
59c70ab
Update README.md
jonasvdd Jun 16, 2022
b94dc4a
:curly_loop: updating & improving docs
jonasvdd Jun 17, 2022
4804862
:pen: update readme
jvdd Jun 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions plotly_resampler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
"""**plotly\_resampler**: visualizing large sequences."""

from .aggregation import (
LTTB,
EfficientLTTB,
EveryNthPoint,
FuncAggregator,
MinMaxOverlapAggregator,
)
from .aggregation import LTTB, EfficientLTTB, EveryNthPoint
from .figure_resampler import FigureResampler, FigureWidgetResampler
from .registering import register_plotly_resampler, unregister_plotly_resampler

__docformat__ = "numpy"
__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost"
__version__ = "0.6.4.2"
__version__ = "0.7.0"

__all__ = [
"__version__",
"FigureResampler",
"FigureWidgetResampler",
"EfficientLTTB",
"MinMaxOverlapAggregator",
"LTTB",
"EveryNthPoint",
"FuncAggregator",
"register_plotly_resampler",
"unregister_plotly_resampler",
]


try: # Enable ipywidgets on google colab!
import sys

if "google.colab" in sys.modules:
from google.colab import output

output.enable_custom_widget_manager()
except ImportError:
pass
52 changes: 46 additions & 6 deletions plotly_resampler/figure_resampler/figure_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
import plotly.graph_objects as go
from dash import Dash
from jupyter_dash import JupyterDash
from plotly.basedatatypes import BaseFigure
from trace_updater import TraceUpdater

from .figure_resampler_interface import AbstractFigureAggregator
from ..aggregation import AbstractSeriesAggregator, EfficientLTTB
from .figure_resampler_interface import AbstractFigureAggregator
from .utils import is_figure, is_fr


class FigureResampler(AbstractFigureAggregator, go.Figure):
"""Data aggregation functionality for ``go.Figures``."""

def __init__(
self,
figure: go.Figure = None,
figure: BaseFigure | dict = None,
convert_existing_traces: bool = True,
default_n_shown_samples: int = 1000,
default_downsampler: AbstractSeriesAggregator = EfficientLTTB(),
Expand All @@ -39,12 +41,27 @@ def __init__(
show_mean_aggregation_size: bool = True,
verbose: bool = False,
):
if figure is None:
figure = go.Figure()
# Parse the figure input before calling `super`
if is_figure(figure) and not is_fr(figure): # go.Figure
# Base case, the figure does not need to be adjusted
f = figure
else:
# Create a new figure object and make sure that the trace uid will not get
# adjusted when they are added.
f = self._get_figure_class(go.Figure)()
f._data_validator.set_uid = False

if isinstance(figure, BaseFigure): # go.FigureWidget or AbstractFigureAggregator
# A base figure object, we first copy the layout and grid ref
f.layout = figure.layout
f._grid_ref = figure._grid_ref
f.add_traces(figure.data)
elif isinstance(figure, (dict, list)):
# A single trace dict or a list of traces
f.add_traces(figure)

assert isinstance(figure, go.Figure)
super().__init__(
figure,
f,
convert_existing_traces,
default_n_shown_samples,
default_downsampler,
Expand All @@ -53,11 +70,34 @@ def __init__(
verbose,
)

if isinstance(figure, AbstractFigureAggregator):
# Copy the `_hf_data` if the previous figure was an AbstractFigureAggregator
# and adjust the default `max_n_samples` and `downsampler`
self._hf_data.update(
self._copy_hf_data(figure._hf_data, adjust_default_values=True)
)

# Note: This hack ensures that the this figure object initially uses
# data of the whole view. More concretely; we create a dict
# serialization figure and adjust the hf-traces to the whole view
# with the check-update method (by passing no range / filter args)
with self.batch_update():
graph_dict: dict = self._get_current_graph()
update_indices = self._check_update_figure_dict(graph_dict)
for idx in update_indices:
self.data[idx].update(graph_dict["data"][idx])

# The FigureResampler needs a dash app
self._app: JupyterDash | Dash | None = None
self._port: int | None = None
self._host: str | None = None

# @staticmethod
# def _get_figure_class() -> type:
# """Return the class of the underlying figure."""
# from ..module import get_plotly_constr
# return get_plotly_constr(go.Figure)

def show_dash(
self,
mode=None,
Expand Down
Loading