Skip to content

accelerate plotly JSON encoder for numpy arrays without nans #2880

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 10 commits into from
Nov 17, 2020
13 changes: 11 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,11 @@ 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.unsafe = False
encoded_o = super(PlotlyJSONEncoder, self).encode(o)

if self.unsafe:
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 Expand Up @@ -95,6 +96,14 @@ def default(self, obj):
Therefore, we only anticipate either unknown iterables or values here.

"""
numpy = get_module("numpy", should_load=False)
if numpy:
if isinstance(obj, numpy.ndarray) and numpy.issubdtype(
obj.dtype, numpy.number
):
if numpy.all(numpy.isfinite(obj)):
self.unsafe = True

Choose a reason for hiding this comment

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

Not sure if I am missing something. We can test all the numpy arrays that are in the dict being encoded, but we'd not catch nans and infs in any sublists, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this will work only for numpy arrays but for other kinds of objects the existing process (encoding - decoding - reencoding) will still be executed. Or at least this is the intended behaviour :-), maybe I am the one missing something! I need to write tests anyway.

Choose a reason for hiding this comment

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

but for other kinds of objects the existing process (encoding - decoding - reencoding) will still be executed

I don't think this is the case, since as I understand this class, encode() is not called recursively, or is it?


# TODO: The ordering if these methods is *very* important. Is this OK?
encoding_methods = (
self.encode_as_plotly,
Expand Down