Skip to content
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

fix: check if update_data contains update before batch_update #316

Merged
merged 10 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:
matrix:
os: ['windows-latest', 'macOS-latest', 'ubuntu-latest']
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
exclude: # Python < 3.8 is not supported on Apple Silicon ARM64
- os: macOS-latest
python-version: '3.7'
include: # So run on older version on Intel CPU
- os: macOS-13
python-version: '3.7'
defaults:
run:
shell: bash
Expand Down
25 changes: 13 additions & 12 deletions plotly_resampler/figure_resampler/figure_resampler_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class AbstractFigureAggregator(BaseFigure, ABC):
"""Abstract interface for data aggregation functionality for plotly figures."""

_high_frequency_traces = ["scatter", "scattergl"]
_stats_traces = ["histogram"]
jvdd marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
Expand Down Expand Up @@ -980,21 +981,21 @@ def add_trace(
uuid_str = str(uuid4()) if trace.uid is None else trace.uid
trace.uid = uuid_str

# construct the hf_data_container
# TODO in future version -> maybe regex on kwargs which start with `hf_`
dc = self._parse_get_trace_props(
trace,
hf_x,
hf_y,
hf_text,
hf_hovertext,
hf_marker_size,
hf_marker_color,
)

# These traces will determine the autoscale its RANGE!
# -> so also store when `limit_to_view` is set.
if trace["type"].lower() in self._high_frequency_traces:
# construct the hf_data_container
# TODO in future version -> maybe regex on kwargs which start with `hf_`
dc = self._parse_get_trace_props(
trace,
hf_x,
hf_y,
hf_text,
hf_hovertext,
hf_marker_size,
hf_marker_color,
)

n_samples = len(dc.x)
if n_samples > max_out_s or limit_to_view:
self._print(
Expand Down
43 changes: 23 additions & 20 deletions plotly_resampler/figure_resampler/figurewidget_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,27 +254,30 @@ def _update_spike_ranges(self, layout, *showspikes, force_update=False):

# Construct the update data
update_data = self._construct_update_data(relayout_dict)
if self._print_verbose:
self._relayout_hist.append(layout)
self._relayout_hist.append(["showspikes-update", len(update_data) - 1])
self._relayout_hist.append("-" * 30)

with self.batch_update():
# First update the layout (first item of update_data)
if not force_update:
self.layout.update(self._parse_relayout(update_data[0]))

# Also: Remove the showspikes from the layout, otherwise the autorange
# will not work as intended (it will not be triggered again)
# Note: this removal causes a second trigger of this method
# which will go in the "else" part below.
for xaxis_str in self._xaxis_list:
self.layout[xaxis_str].pop("showspikes")
if not self._is_no_update(update_data):
if self._print_verbose:
self._relayout_hist.append(layout)
self._relayout_hist.append(
["showspikes-update", len(update_data) - 1]
)
self._relayout_hist.append("-" * 30)

with self.batch_update():
# First update the layout (first item of update_data)
if not force_update:
self.layout.update(self._parse_relayout(update_data[0]))

# Also: Remove the showspikes from the layout, otherwise the autorange
# will not work as intended (it will not be triggered again)
# Note: this removal causes a second trigger of this method
# which will go in the "else" part below.
for xaxis_str in self._xaxis_list:
self.layout[xaxis_str].pop("showspikes")
Comment on lines +270 to +275
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure whether this is necessary when update_data is NoUpdate
(In the previous logic, this would be executed)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite sure it is correct to only execute this code if update_data is a proper update.


# Then, update the data
for updated_trace in update_data[1:]:
trace_idx = updated_trace.pop("index")
self.data[trace_idx].update(updated_trace)
# Then, update the data
for updated_trace in update_data[1:]:
trace_idx = updated_trace.pop("index")
self.data[trace_idx].update(updated_trace)
elif self._print_verbose:
self._relayout_hist.append(["showspikes", "initial call or showspikes"])
self._relayout_hist.append("-" * 40)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_figurewidget_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ def test_add_not_a_hf_trace(float_series):
)


def test_add_not_scatter():
fw_fig = FigureWidgetResampler(
go.Figure(go.Bar(x=[1, 2, 3, 4], y=[1, 2, 3, 4])), verbose=True
)

# we do not want to have an relayout update
assert len(fw_fig._relayout_hist) == 0

# zoom in on both traces
fw_fig.layout.update(
{"xaxis": {"range": [0, 1]}},
overwrite=False,
)
# reset axes
fw_fig.reset_axes()


def test_box_histogram(float_series):
base_fig = make_subplots(
rows=2,
Expand Down
Loading