`PlotlyJSONEncoder` always casts values to float64 due to using `tolist()`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 18.8k
- Fork
- 2.8k
- Merge medio
- 16h 26m
- PR unite (30g)
- 21
Descrizione
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 (when float16 or float32 is sufficient) and affected not only coordinate data, but also marker sizes, meta info, etc.
Just 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:
@staticmethod
def encode_as_list_patch(obj):
"""Attempt to use `tolist` method to convert to normal Python list."""
if hasattr(obj, "tolist"):
numpy = get_module("numpy")
try:
if isinstance(obj, numpy.ndarray) \
and obj.dtype == numpy.float32 or obj.dtype == numpy.float16 \
and obj.flags.contiguous:
return [float('%s' % x) for x in obj]
except AttributeError:
raise NotEncodable
return obj.tolist()
else:
raise NotEncodable
It'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.
I 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)?
FYI: 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.
Originally posted by @mherrmann3 in https://github.com/plotly/plotly.py/issues/2955#issuecomment-856651213
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in packages/python/plotly/_plotly_utils/utils.py, presso PlotlyJSONEncoder.encode_as_list, quindi esamina la discussione collegata sulla gestione di NumPy float16 e float32. Determina in che modo la conservazione della loro precisione influisce sulla serializzazione e sulle dimensioni dell'esportazione senza modificare altri valori; il lavoro è completo quando il comportamento è stato convalidato per i tipi NumPy pertinenti e il comportamento di codifica esistente rimane intatto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- numpy, python
- Ambito
- data-visualization
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100