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

accelerate plotly JSON encoder for numpy arrays without nans #2880

Merged
merged 10 commits into from
Nov 17, 2020
9 changes: 7 additions & 2 deletions packages/python/plotly/_plotly_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ def encode(self, o):
Note that setting invalid separators will cause a failure at this step.

"""

# this will raise errors in a normal-expected way
self.hasinfnans = False
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you using hasinfnans anywhere else now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no you were right :-). Thanks!

encoded_o = super(PlotlyJSONEncoder, self).encode(o)

# Brute force guessing whether NaN or Infinity values are in the string
# We catch false positive cases (e.g. strings such as titles, labels etc.)
# but this is ok since the intention is to skip the decoding / reencoding
# step when it's completely safe
if not ("Infinity" in encoded_o or "NaN" in encoded_o):
return encoded_o
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with this for now given your description of the performance characteristics.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe check for NaN first, as it feels like that'd appear more frequently?

# now:
# 1. `loads` to switch Infinity, -Infinity, NaN to None
# 2. `dumps` again so you get 'null' instead of extended JSON
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import absolute_import

from inspect import getargspec
from unittest import TestCase

import json as _json

from plotly.utils import PlotlyJSONEncoder, get_by_path, node_generator
from time import time
import numpy as np
import plotly.graph_objects as go


class TestJSONEncoder(TestCase):
Expand All @@ -19,6 +21,38 @@ def test_invalid_encode_exception(self):
with self.assertRaises(TypeError):
_json.dumps({"a": {1}}, cls=PlotlyJSONEncoder)

def test_fast_track_finite_arrays(self):
# if NaN or Infinity is found in the json dump
# of a figure, it is decoded and re-encoded to replace these values
# with null. This test checks that NaN and Infinity values are
# indeed converted to null, and that the encoding of figures
# without inf or nan is faster (because we can avoid decoding
# and reencoding).
z = np.random.randn(100, 100)
x = np.arange(100.0)
fig_1 = go.Figure(go.Heatmap(z=z, x=x))
t1 = time()
json_str_1 = _json.dumps(fig_1, cls=PlotlyJSONEncoder)
t2 = time()
x[0] = np.nan
x[1] = np.inf
fig_2 = go.Figure(go.Heatmap(z=z, x=x))
t3 = time()
json_str_2 = _json.dumps(fig_2, cls=PlotlyJSONEncoder)
t4 = time()
assert t2 - t1 < t4 - t3
assert "null" in json_str_2
assert "NaN" not in json_str_2
assert "Infinity" not in json_str_2
x = np.arange(100.0)
fig_3 = go.Figure(go.Heatmap(z=z, x=x))
fig_3.update_layout(title_text="Infinity")
t5 = time()
json_str_3 = _json.dumps(fig_3, cls=PlotlyJSONEncoder)
t6 = time()
assert t2 - t1 < t6 - t5
assert "Infinity" in json_str_3


class TestGetByPath(TestCase):
def test_get_by_path(self):
Expand Down