-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 2 commits
fcf2a5b
0fa810f
561edf1
8392ead
627c00f
c8e2922
224082e
5b18b2d
e528271
c46bae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
# now: | ||
# 1. `loads` to switch Infinity, -Infinity, NaN to None | ||
# 2. `dumps` again so you get 'null' instead of extended JSON | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think this is the case, since as I understand this class, |
||
|
||
# TODO: The ordering if these methods is *very* important. Is this OK? | ||
encoding_methods = ( | ||
self.encode_as_plotly, | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?