Skip to content

✨ fix limit_to_view=True but no gaps inserted bug #220

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 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion examples/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ ipywidgets>=7.7.0
memory-profiler>=0.60.0
line-profiler>=3.5.1
pyarrow>=6.0.0
kaleido>=0.2.1
kaleido>=0.2.1
flask-cors>=3.0.10
2 changes: 1 addition & 1 deletion plotly_resampler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

__docformat__ = "numpy"
__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost"
__version__ = "0.9.0rc0"
__version__ = "0.9.0rc3"

__all__ = [
"__version__",
Expand Down
93 changes: 59 additions & 34 deletions plotly_resampler/aggregation/plotly_aggregator_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,58 @@ def get_start_end_indices(hf_trace_data, axis_type, start, end) -> Tuple[int, in
end_idx = bisect.bisect_right(hf_trace_data["x"], end)
return start_idx, end_idx

@staticmethod
def _handle_gaps(
hf_trace_data: dict,
hf_x: np.ndarray,
agg_x: np.ndarray,
agg_y: np.ndarray,
indices: np.ndarray,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Handle the gaps in the aggregated data.

Returns:
- agg_x: the aggregated x-values
- agg_y: the aggregated y-values
- indices: the indices of the hf_data data that were aggregated

"""
gap_handler: AbstractGapHandler = hf_trace_data["gap_handler"]
downsampler = hf_trace_data["downsampler"]

# TODO check for trace mode (markers, lines, etc.) and only perform the
# gap insertion methodology when the mode is lines.
# if trace.get("connectgaps") != True and
if (
isinstance(gap_handler, NoGapHandler)
# rangeIndex | datetimeIndex with freq -> equally spaced x; so no gaps
or isinstance(hf_trace_data["x"], pd.RangeIndex)
or (
isinstance(hf_trace_data["x"], pd.DatetimeIndex)
and hf_trace_data["x"].freq is not None
)
):
return agg_x, agg_y, indices

# Interleave the gaps
# View the data as an int64 when we have a DatetimeIndex
# We only want to detect gaps, so we only want to compare values.
agg_x_parsed = PlotlyAggregatorParser.parse_hf_data(agg_x)
xdt = agg_x_parsed.dtype
if np.issubdtype(xdt, np.timedelta64) or np.issubdtype(xdt, np.datetime64):
agg_x_parsed = agg_x_parsed.view("int64")

agg_y, indices = gap_handler.insert_fill_value_between_gaps(
agg_x_parsed, agg_y, indices
)
if isinstance(downsampler, DataPointSelector):
agg_x = hf_x[indices]
elif isinstance(downsampler, DataAggregator):
# The indices are in this case a repeat
agg_x = agg_x[indices]

return agg_x, agg_y, indices

@staticmethod
def aggregate(
hf_trace_data: dict,
Expand All @@ -107,12 +159,14 @@ def aggregate(
hf_x = hf_trace_data["x"][start_idx:end_idx]
hf_y = hf_trace_data["y"][start_idx:end_idx]

# No downsampling needed ; we show the raw data as is, no gap detection
# No downsampling needed ; we show the raw data as is, but with gap-detection
if (end_idx - start_idx) <= hf_trace_data["max_n_samples"]:
return hf_x, hf_y, np.arange(len(hf_y))
indices = np.arange(len(hf_y))
return PlotlyAggregatorParser._handle_gaps(
hf_trace_data, hf_x=hf_x, agg_x=hf_x, agg_y=hf_y, indices=indices
)

downsampler = hf_trace_data["downsampler"]
gap_handler: AbstractGapHandler = hf_trace_data["gap_handler"]

hf_x_parsed = PlotlyAggregatorParser.parse_hf_data(hf_x)
hf_y_parsed = PlotlyAggregatorParser.parse_hf_data(hf_y)
Expand Down Expand Up @@ -161,35 +215,6 @@ def aggregate(
+ f"DataAggregator or a DataPointSelector, got {type(downsampler)}"
)

# TODO check for trace mode (markers, lines, etc.) and only perform the
# gap insertion methodology when the mode is lines.
# if trace.get("connectgaps") != True and
if (
isinstance(gap_handler, NoGapHandler)
# rangeIndex | datetimeIndex with freq -> equally spaced x; so no gaps
or isinstance(hf_trace_data["x"], pd.RangeIndex)
or (
isinstance(hf_trace_data["x"], pd.DatetimeIndex)
and hf_trace_data["x"].freq is not None
)
):
return agg_x, agg_y, indices

# Interleave the gaps
# View the data as an int64 when we have a DatetimeIndex
# We only want to detect gaps, so we only want to compare values.
agg_x_parsed = PlotlyAggregatorParser.parse_hf_data(agg_x)
xdt = agg_x_parsed.dtype
if np.issubdtype(xdt, np.timedelta64) or np.issubdtype(xdt, np.datetime64):
agg_x_parsed = agg_x_parsed.view("int64")

agg_y, indices = gap_handler.insert_fill_value_between_gaps(
agg_x_parsed, agg_y, indices
return PlotlyAggregatorParser._handle_gaps(
hf_trace_data, hf_x=hf_x, agg_x=agg_x, agg_y=agg_y, indices=indices
)
if isinstance(downsampler, DataPointSelector):
agg_x = hf_x[indices]
elif isinstance(downsampler, DataAggregator):
# The indices are in this case a repeat
agg_x = agg_x[indices]

return agg_x, agg_y, indices
23 changes: 23 additions & 0 deletions tests/test_figure_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ def test_add_trace_not_resampling(float_series):
)


def test_add_trace_not_resampling_insert_gaps():
# This test verifies whether gaps are inserted correctly when adding a trace that
# is not resampled (but `limit_to_view` is True)
idx = np.arange(500)
for i in np.random.randint(0, 500, 4):
idx[i:] += 100
s = pd.Series(np.arange(500), index=idx)

# limit_to_view=False -> no gaps inserted
fr = FigureResampler(default_n_shown_samples=1000)
fr.add_trace({}, hf_x=s.index, hf_y=s.values)
fr.add_trace(dict(x=s.index, y=s.values))
assert np.isnan(fr.data[0]["y"]).sum() == 0
assert np.isnan(fr.data[1]["y"]).sum() == 0

# limit_to_view=True -> gaps inserted
fr = FigureResampler(default_n_shown_samples=1000)
fr.add_trace({}, hf_x=s.index, hf_y=s.values, limit_to_view=True)
fr.add_trace(dict(x=s.index, y=s.values), limit_to_view=True)
assert np.isnan(fr.data[0]["y"]).sum() > 0
assert np.isnan(fr.data[1]["y"]).sum() > 0


def test_various_dtypes(float_series):
# List of dtypes supported by orjson >= 3.8
valid_dtype_list = [
Expand Down