plotly / plotly/plotly.py

plotly.graph_objects.FigureWidget does not support NaN or Inf due to JSON limitations

オープン
#3,470 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug P3
主要言語
Python
スター
18.8k
フォーク
2.8k
平均マージ
16時間 26分
マージ済み PR(30日)
21

説明

Hello everyone.

I'm trying to plot some interactive heatmaps on a Jupyter Notebook using Ipywidgets and FigureWidget.
These heatmaps, by design, will contain some NaN values that needs to be "not plotted" as such. From this point of view, my previous experience of go.Figure was perfect.

However, when trying to use FigureWidget, I get the ValueError: Out of range float values are not JSON compliant, as JSON base standard is not able to serialize Infs or NaNs. Is it possible to avoid this limitation somehow?

A minimal example to execute in a notebook:

# %%
import numpy as np
import plotly.graph_objects as go


# %%
matrix_with_no_nans = np.random.uniform(0, 1, size=(100, 100))

matrix_with_some_nans = matrix_with_no_nans.copy()
matrix_with_some_nans[matrix_with_some_nans < 0.5] = np.nan


# %%
fig_1 = go.FigureWidget(
    data=go.Heatmap(z=matrix_with_no_nans)
)
# Works as intended
fig_1


# %%
fig_2 = go.FigureWidget(
    data=go.Heatmap(z=matrix_with_some_nans)
)
# Json error!
fig_2

# %%

The full text of the error message from my personal notebook:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_129118/716380412.py in <module>
----> 1 fig_2 = go.FigureWidget(
      2     data=go.Heatmap(z=matrix_with_some_nans)
      3 )
      4 fig_2

~/anaconda3/lib/python3.8/site-packages/plotly/graph_objs/_figurewidget.py in __init__(self, data, layout, frames, skip_invalid, **kwargs)
    588             is invalid AND skip_invalid is False
    589         """
--> 590         super(FigureWidget, self).__init__(data, layout, frames, skip_invalid, **kwargs)
    591 
    592     def add_bar(

~/anaconda3/lib/python3.8/site-packages/plotly/basewidget.py in __init__(self, data, layout, frames, skip_invalid, **kwargs)
    115         # with the `layout` constructor parameter of the `widgets.DOMWidget`
    116         # ipywidgets class
--> 117         super(BaseFigureWidget, self).__init__(
    118             data=data,
    119             layout_plotly=layout,

~/anaconda3/lib/python3.8/site-packages/plotly/basedatatypes.py in __init__(self, data, layout_plotly, frames, skip_invalid, **kwargs)
    524         # The _data property is a list of dicts containing the properties
    525         # explicitly set by the user for each trace.
--> 526         self._data = [deepcopy(trace._props) for trace in data]
    527 
    528         # ### Create data defaults ###

~/anaconda3/lib/python3.8/site-packages/plotly/basedatatypes.py in __setattr__(self, prop, value)
    719         if prop.startswith("_") or hasattr(self, prop):
    720             # Let known properties and private properties through
--> 721             super(BaseFigure, self).__setattr__(prop, value)
    722         else:
    723             # Raise error on unknown public properties

~/anaconda3/lib/python3.8/site-packages/traitlets/traitlets.py in __set__(self, obj, value)
    604             raise TraitError('The "%s" trait is read-only.' % self.name)
    605         else:
--> 606             self.set(obj, value)
    607 
    608     def _validate(self, obj, value):

~/anaconda3/lib/python3.8/site-packages/traitlets/traitlets.py in set(self, obj, value)
   2649             return super().set(obj, [value])
   2650         else:
-> 2651             return super().set(obj, value)
   2652 
   2653 

~/anaconda3/lib/python3.8/site-packages/traitlets/traitlets.py in set(self, obj, value)
    593             # we explicitly compare silent to True just in case the equality
    594             # comparison above returns something other than True/False
--> 595             obj._notify_trait(self.name, old_value, new_value)
    596 
    597     def __set__(self, obj, value):

~/anaconda3/lib/python3.8/site-packages/traitlets/traitlets.py in _notify_trait(self, name, old_value, new_value)
   1217 
   1218     def _notify_trait(self, name, old_value, new_value):
-> 1219         self.notify_change(Bunch(
   1220             name=name,
   1221             old=old_value,

~/anaconda3/lib/python3.8/site-packages/ipywidgets/widgets/widget.py in notify_change(self, change)
    603             if name in self.keys and self._should_send_property(name, getattr(self, name)):
    604                 # Send new state to front-end
--> 605                 self.send_state(key=name)
    606         super(Widget, self).notify_change(change)
    607 

~/anaconda3/lib/python3.8/site-packages/ipywidgets/widgets/widget.py in send_state(self, key)
    487             state, buffer_paths, buffers = _remove_buffers(state)
    488             msg = {'method': 'update', 'state': state, 'buffer_paths': buffer_paths}
--> 489             self._send(msg, buffers=buffers)
    490 
    491 

~/anaconda3/lib/python3.8/site-packages/ipywidgets/widgets/widget.py in _send(self, msg, buffers)
    735         """Sends a message to the model in the front-end."""
    736         if self.comm is not None and self.comm.kernel is not None:
--> 737             self.comm.send(data=msg, buffers=buffers)
    738 
    739     def _repr_keys(self):

~/anaconda3/lib/python3.8/site-packages/ipykernel/comm/comm.py in send(self, data, metadata, buffers)
    120     def send(self, data=None, metadata=None, buffers=None):
    121         """Send a message to the frontend-side version of this comm"""
--> 122         self._publish_msg('comm_msg',
    123             data=data, metadata=metadata, buffers=buffers,
    124         )

~/anaconda3/lib/python3.8/site-packages/ipykernel/comm/comm.py in _publish_msg(self, msg_type, data, metadata, buffers, **keys)
     64         metadata = {} if metadata is None else metadata
     65         content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
---> 66         self.kernel.session.send(self.kernel.iopub_socket, msg_type,
     67             content,
     68             metadata=json_clean(metadata),

~/anaconda3/lib/python3.8/site-packages/jupyter_client/session.py in send(self, stream, msg_or_type, content, parent, ident, buffers, track, header, metadata)
    828         if self.adapt_version:
    829             msg = adapt(msg, self.adapt_version)
--> 830         to_send = self.serialize(msg, ident)
    831         to_send.extend(buffers)
    832         longest = max([len(s) for s in to_send])

~/anaconda3/lib/python3.8/site-packages/jupyter_client/session.py in serialize(self, msg, ident)
    702             content = self.none
    703         elif isinstance(content, dict):
--> 704             content = self.pack(content)
    705         elif isinstance(content, bytes):
    706             # content is already packed, as in a relayed message

~/anaconda3/lib/python3.8/site-packages/jupyter_client/session.py in json_packer(obj)
     93 
     94 def json_packer(obj):
---> 95     return jsonapi.dumps(
     96         obj,
     97         default=json_default,

~/anaconda3/lib/python3.8/site-packages/zmq/utils/jsonapi.py in dumps(o, **kwargs)
     23     Keyword arguments are passed along to :py:func:`json.dumps`.
     24     """
---> 25     return json.dumps(o, **kwargs).encode("utf8")
     26 
     27 

~/anaconda3/lib/python3.8/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    232     if cls is None:
    233         cls = JSONEncoder
--> 234     return cls(
    235         skipkeys=skipkeys, ensure_ascii=ensure_ascii,
    236         check_circular=check_circular, allow_nan=allow_nan, indent=indent,

~/anaconda3/lib/python3.8/json/encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

~/anaconda3/lib/python3.8/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

ValueError: Out of range float values are not JSON compliant

Thanks!

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず最小限の FigureWidget ヒートマップ例を再現し、FigureWidget を plotly/basewidget.py 内で traceback に示されている ipywidgets send_state パスまで追跡します。FigureWidget が JSON ValueError を発生させずに NaN/Inf のヒートマップデータを受け入れ、意図したプロット対象外のセルを保持できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
jupyter-notebook, numpy, python
領域
data-visualization
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。