Title: `PlotlyJSONEncoder` always casts values to float64 due to using `tolist()` · Issue #3232 · plotly/plotly.py · GitHub
Open Graph Title: `PlotlyJSONEncoder` always casts values to float64 due to using `tolist()` · Issue #3232 · plotly/plotly.py
X Title: `PlotlyJSONEncoder` always casts values to float64 due to using `tolist()` · Issue #3232 · plotly/plotly.py
Description: Regarding the numpy floating point precision and that PlotlyJSONEncoder always casts those to float64 due to using tolist()... This had always bugged me, as it resulted in much larger exports (i.e. html / ipynb file sizes) than necessary...
Open Graph Description: Regarding the numpy floating point precision and that PlotlyJSONEncoder always casts those to float64 due to using tolist()... This had always bugged me, as it resulted in much larger exports (i.e....
X Description: Regarding the numpy floating point precision and that PlotlyJSONEncoder always casts those to float64 due to using tolist()... This had always bugged me, as it resulted in much larger exports (i.e....
Opengraph URL: https://github.com/plotly/plotly.py/issues/3232
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`PlotlyJSONEncoder` always casts values to float64 due to using `tolist()`","articleBody":"Regarding the numpy floating point precision and that `PlotlyJSONEncoder` always casts those to float64 due to using `tolist()`...\r\n\r\nThis had always bugged me, as it resulted in much larger exports (i.e. html / ipynb file sizes) than necessary (when float16 or float32 is sufficient) and affected not only coordinate data, but also marker sizes, meta info, etc.\r\n\r\nJust in case the plotly.py devs or others are interested: I had found a way to avoid this number inflation by modifying (& monkey patching) the [`encode_as_list` method](https://github.com/plotly/plotly.py/blob/340aed33bf116530a574badde64e867175c067ad/packages/python/plotly/_plotly_utils/utils.py#L147):\r\n\r\n```python\r\n@staticmethod\r\ndef encode_as_list_patch(obj):\r\n \"\"\"Attempt to use `tolist` method to convert to normal Python list.\"\"\"\r\n if hasattr(obj, \"tolist\"):\r\n\r\n numpy = get_module(\"numpy\")\r\n try:\r\n if isinstance(obj, numpy.ndarray) \\\r\n and obj.dtype == numpy.float32 or obj.dtype == numpy.float16 \\\r\n and obj.flags.contiguous:\r\n return [float('%s' % x) for x in obj]\r\n except AttributeError:\r\n raise NotEncodable\r\n\r\n return obj.tolist()\r\n else:\r\n raise NotEncodable\r\n```\r\nIt's about 30-50x slower than `.tolist()`, but - being in the order of a few μs - still much faster than the json encoding, with the benefit of ~3x smaller exports.\r\n\r\nI always wanted to report this, and this PR revived the topic. Could this be relevant for a new issue (especially since orjson will not become the default)?\r\n\r\nFYI: for reference, a quick search revealed that a patch of encode_as_list was already suggested before: https://github.com/plotly/plotly.py/issues/1842#issuecomment-549401190, in the context of treating inf & NaN, which got brought up again in https://github.com/plotly/plotly.py/pull/2880#issuecomment-726860782.\r\n\r\n_Originally posted by @mherrmann3 in https://github.com/plotly/plotly.py/issues/2955#issuecomment-856651213_","author":{"url":"https://github.com/nicolaskruchten","@type":"Person","name":"nicolaskruchten"},"datePublished":"2021-06-08T11:58:14Z","url":"https://github.com/plotly/plotly.py/issues/3232"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d546e268-c880-2685-551b-3ac1c8b9683a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB90:25598F:153B5AA:1C08207:6B012C2D |
| html-safe-nonce | 3becd32295be586f69356226b9ecde12d9ec41fc5cff4203a328a7c561cfe44c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjkwOjI1NTk4RjoxNTNCNUFBOjFDMDgyMDc6NkIwMTJDMkQiLCJ2aXNpdG9yX2lkIjoiMTM2MTc1MDIzMzc0MjEyNjEyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b7fb700ebd0bc2d17cbe18184dbc10a8b12c29779cc97058694a03e1d3914f3c |
| hovercard-subject-tag | issue:914919082 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/plotly/plotly.py/3232/issue_layout |
| twitter:image | https://opengraph.githubassets.com/df1072c1247583de727d450acfb12ce545b86555f35308477f78172b43707a88/plotly/plotly.py/issues/3232 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/df1072c1247583de727d450acfb12ce545b86555f35308477f78172b43707a88/plotly/plotly.py/issues/3232 |
| og:image:alt | Regarding the numpy floating point precision and that PlotlyJSONEncoder always casts those to float64 due to using tolist()... This had always bugged me, as it resulted in much larger exports (i.e.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nicolaskruchten |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/plotly/plotly.py git https://github.com/plotly/plotly.py.git |
| octolytics-dimension-user_id | 5997976 |
| octolytics-dimension-user_login | plotly |
| octolytics-dimension-repository_id | 14579099 |
| octolytics-dimension-repository_nwo | plotly/plotly.py |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14579099 |
| octolytics-dimension-repository_network_root_nwo | plotly/plotly.py |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width