Skip to content

Commit 9b95694

Browse files
committed
Merge branch 'compose_figs' of github.com:predict-idlab/plotly-resampler into compose_figs
2 parents bb057cd + 12c4e93 commit 9b95694

File tree

7 files changed

+61
-38
lines changed

7 files changed

+61
-38
lines changed

plotly_resampler/figure_resampler/figure_resampler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(
4242
verbose: bool = False,
4343
):
4444
# Parse the figure input before calling `super`
45-
if is_figure(figure) and not is_fr(figure):
45+
if is_figure(figure) and not is_fr(figure): # go.Figure
4646
# Base case, the figure does not need to be adjusted
4747
f = figure
4848
else:
@@ -51,7 +51,7 @@ def __init__(
5151
f = go.Figure()
5252
f._data_validator.set_uid = False
5353

54-
if isinstance(figure, BaseFigure):
54+
if isinstance(figure, BaseFigure): # go.FigureWidget or AbstractFigureAggregator
5555
# A base figure object, we first copy the layout and grid ref
5656
f.layout = figure.layout
5757
f._grid_ref = figure._grid_ref

plotly_resampler/figure_resampler/figure_resampler_interface.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def _construct_hf_data_dict(
675675
max_n_samples: int | None,
676676
offset=0,
677677
) -> dict:
678-
"""Create the `hf_data` dict item which will be put in the `_hf_data` property.
678+
"""Create the `hf_data` dict which will be put in the `_hf_data` property.
679679
680680
Parameters
681681
----------
@@ -695,7 +695,6 @@ def _construct_hf_data_dict(
695695
"""
696696
# We will re-create this each time as hf_x and hf_y withholds
697697
# high-frequency data
698-
# index = pd.Index(hf_x, copy=False, name="timestamp")
699698
hf_series = self._to_hf_series(x=dc.x, y=dc.y)
700699

701700
# Checking this now avoids less interpretable `KeyError` when resampling
@@ -856,7 +855,7 @@ def add_trace(
856855
trace = self._data_validator.validate_coerce(trace)[0]
857856

858857
# First add an UUID, as each (even the non-hf_data traces), must contain this
859-
# key for comparison, if the trace already has an UUID, we will keep it.
858+
# key for comparison. If the trace already has an UUID, we will keep it.
860859
uuid_str = str(uuid4()) if trace.uid is None else trace.uid
861860
trace.uid = uuid_str
862861

@@ -976,7 +975,7 @@ def add_traces(
976975
]
977976

978977
# First add an UUID, as each (even the non-hf_data traces), must contain this
979-
# key for comparison, if the trace already has an UUID, we will keep it.
978+
# key for comparison. If the trace already has an UUID, we will keep it.
980979
for trace in data:
981980
uuid_str = str(uuid4()) if trace.uid is None else trace.uid
982981
trace.uid = uuid_str

plotly_resampler/figure_resampler/figurewidget_resampler.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
from typing import Tuple
1414

1515
import plotly.graph_objects as go
16+
from plotly.basedatatypes import BaseFigure
1617

17-
from .figure_resampler_interface import AbstractFigureAggregator
1818
from ..aggregation import AbstractSeriesAggregator, EfficientLTTB
19-
from plotly.basedatatypes import BaseFigure
19+
from .figure_resampler_interface import AbstractFigureAggregator
2020

2121

2222
class _FigureWidgetResamplerM(type(AbstractFigureAggregator), type(go.FigureWidget)):
@@ -51,16 +51,14 @@ def __init__(
5151
show_mean_aggregation_size: bool = True,
5252
verbose: bool = False,
5353
):
54-
5554
# Parse the figure input before calling `super`
5655
f = go.FigureWidget()
5756
f._data_validator.set_uid = False
5857

59-
if isinstance(figure, BaseFigure):
58+
if isinstance(figure, BaseFigure): # go.Figure or go.FigureWidget or AbstractFigureAggregator
6059
# A base figure object, we first copy the layout and grid ref
6160
f.layout = figure.layout
6261
f._grid_ref = figure._grid_ref
63-
6462
f.add_traces(figure.data)
6563
elif isinstance(figure, (dict, list)):
6664
# A single trace dict or a list of traces

plotly_resampler/figure_resampler/utils.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def is_figure(figure: Any) -> bool:
15-
"""Check if the figure is a plotly go.Figure.
15+
"""Check if the figure is a plotly go.Figure or a FigureResampler.
1616
1717
.. Note::
1818
This method does not use isinstance(figure, go.Figure) as this will not work
@@ -27,18 +27,18 @@ def is_figure(figure: Any) -> bool:
2727
Returns
2828
-------
2929
bool
30-
True if the figure is a plotly go.Figure.
30+
True if the figure is a plotly go.Figure or a FigureResampler.
3131
"""
3232

3333
return isinstance(figure, BaseFigure) and (not isinstance(figure, BaseFigureWidget))
3434

3535

36-
def is_fr(figure: Any) -> bool:
37-
"""Check if the figure is a plotly FigureResampler.
36+
def is_figurewidget(figure: Any):
37+
"""Check if the figure is a plotly go.FigureWidget or a FigureWidgetResampler.
3838
3939
.. Note::
40-
This method does not use isinstance(figure, go.Figure) as this will not work
41-
when go.Figure is decorated (after executing the
40+
This method does not use isinstance(figure, go.FigureWidget) as this will not
41+
work when go.FigureWidget is decorated (after executing the
4242
``register_plotly_resampler`` function).
4343
4444
Parameters
@@ -49,20 +49,16 @@ def is_fr(figure: Any) -> bool:
4949
Returns
5050
-------
5151
bool
52-
True if the figure is a plotly go.Figure.
52+
True if the figure is a plotly go.FigureWidget or a FigureWidgetResampler.
5353
"""
54-
from plotly_resampler import FigureResampler
55-
56-
return isinstance(figure, FigureResampler)
54+
return isinstance(figure, BaseFigureWidget)
5755

5856

59-
def is_fwr(figure: Any) -> bool:
60-
"""Check if the figure is a plotly FigureWidgetResampler.
57+
def is_fr(figure: Any) -> bool:
58+
"""Check if the figure is a FigureResampler.
6159
6260
.. Note::
63-
This method does not use isinstance(figure, go.Figure) as this will not work
64-
when go.Figure is decorated (after executing the
65-
``register_plotly_resampler`` function).
61+
This method will not return True if the figure is a plotly go.Figure.
6662
6763
Parameters
6864
----------
@@ -72,20 +68,18 @@ def is_fwr(figure: Any) -> bool:
7268
Returns
7369
-------
7470
bool
75-
True if the figure is a plotly go.Figure.
71+
True if the figure is a FigureResampler.
7672
"""
77-
from plotly_resampler import FigureWidgetResampler
73+
from plotly_resampler import FigureResampler
7874

79-
return isinstance(figure, FigureWidgetResampler)
75+
return isinstance(figure, FigureResampler)
8076

8177

82-
def is_figurewidget(figure: Any):
83-
"""Check if the figure is a plotly go.FigureWidget.
78+
def is_fwr(figure: Any) -> bool:
79+
"""Check if the figure is a FigureWidgetResampler.
8480
8581
.. Note::
86-
This method does not use isinstance(figure, go.FigureWidget) as this will not
87-
work when go.FigureWidget is decorated (after executing the
88-
``register_plotly_resampler`` function).
82+
This method will not return True if the figure is a plotly go.FigureWidget.
8983
9084
Parameters
9185
----------
@@ -95,9 +89,11 @@ def is_figurewidget(figure: Any):
9589
Returns
9690
-------
9791
bool
98-
True if the figure is a plotly go.FigureWidget.
92+
True if the figure is a FigureWidgetResampler.
9993
"""
100-
return isinstance(figure, BaseFigureWidget)
94+
from plotly_resampler import FigureWidgetResampler
95+
96+
return isinstance(figure, FigureWidgetResampler)
10197

10298

10399
### Rounding functions for bin size

tests/test_figure_resampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def test_multiple_tz_no_tz_series_slicing():
574574
t_start = t_start.tz_localize(cs[(i + 1) % len(cs)].index.tz)
575575
t_stop = t_stop.tz_localize(cs[(i + 2) % len(cs)].index.tz)
576576

577-
# Now the assumption cannot be made that s ahd the same time-zone as the
577+
# Now the assumption cannot be made that s has the same time-zone as the
578578
# timestamps -> AssertionError will be raised.
579579
with pytest.raises(AssertionError):
580580
fig._slice_time(s.tz_localize(None), t_start, t_stop)

tests/test_figurewidget_resampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def test_multiple_tz_no_tz_series_slicing():
578578
t_start = t_start.tz_localize(cs[(i + 1) % len(cs)].index.tz)
579579
t_stop = t_stop.tz_localize(cs[(i + 2) % len(cs)].index.tz)
580580

581-
# Now the assumption cannot be made that s ahd the same time-zone as the
581+
# Now the assumption cannot be made that s has the same time-zone as the
582582
# timestamps -> AssertionError will be raised.
583583
with pytest.raises(AssertionError):
584584
fig._slice_time(s.tz_localize(None), t_start, t_stop)

tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from plotly_resampler.figure_resampler.utils import (
44
is_figure,
55
is_figurewidget,
6+
is_fr,
7+
is_fwr,
68
timedelta_to_str,
79
round_td_str,
810
round_number_str,
@@ -24,6 +26,20 @@ def test_is_figure():
2426
assert not is_figure(FigureWidgetResampler(fig_dict))
2527

2628

29+
def test_is_fr():
30+
fig_dict = {"type": "scatter", "y": [1, 2, 3]}
31+
assert is_fr(FigureResampler())
32+
assert is_fr(FigureResampler(fig_dict))
33+
assert not is_fr(go.Figure())
34+
assert not is_fr(go.Figure(fig_dict))
35+
assert not is_fr(go.FigureWidget())
36+
assert not is_fr(None)
37+
assert not is_fr(fig_dict)
38+
assert not is_fr(go.Scatter(y=[1, 2, 3]))
39+
assert not is_fr(FigureWidgetResampler())
40+
assert not is_fr(FigureWidgetResampler(fig_dict))
41+
42+
2743
def test_is_figurewidget():
2844
fig_dict = {"type": "scatter", "y": [1, 2, 3]}
2945
assert is_figurewidget(go.FigureWidget())
@@ -38,6 +54,20 @@ def test_is_figurewidget():
3854
assert not is_figurewidget(FigureResampler(fig_dict))
3955

4056

57+
def test_is_fwr():
58+
fig_dict = {"type": "scatter", "y": [1, 2, 3]}
59+
assert is_fwr(FigureWidgetResampler())
60+
assert is_fwr(FigureWidgetResampler(fig_dict))
61+
assert not is_fwr(go.FigureWidget())
62+
assert not is_fwr(go.FigureWidget(fig_dict))
63+
assert not is_fwr(go.Figure())
64+
assert not is_fwr(None)
65+
assert not is_fwr(fig_dict)
66+
assert not is_fwr(go.Scatter(y=[1, 2, 3]))
67+
assert not is_fwr(FigureResampler())
68+
assert not is_fwr(FigureResampler(fig_dict))
69+
70+
4171
def test_timedelta_to_str():
4272
assert (round_td_str(pd.Timedelta("1W"))) == "7D"
4373
assert (timedelta_to_str(pd.Timedelta("1W"))) == "7D"

0 commit comments

Comments
 (0)