plotly / plotly/plotly.py

significant speedup for "to_scalar_or_list"

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

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

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

説明

hello,

While investigating a slowness in plotly, I have stumbled upon the to_scalar_or_list function (https://github.com/plotly/plotly.py/blob/abd86092e048d5c8b02da65123824b75e4311838/packages/python/plotly/_plotly_utils/basevalidators.py#L30) that was taking much time.
After some tinkering, I came with the two following changes that vastly improves the performance:

So at the end, it looks like

np = get_module("numpy", should_load=False)
pd = get_module("pandas", should_load=False)
# Utility functions
# -----------------
def to_scalar_or_list(v):
    # Handle the case where 'v' is a non-native scalar-like type,
    # such as numpy.float32. Without this case, the object might be
    # considered numpy-convertable and therefore promoted to a
    # 0-dimensional array, but we instead want it converted to a
    # Python native scalar type ('float' in the example above).
    # We explicitly check if is has the 'item' method, which conventionally
    # converts these types to native scalars.

    # check first for the simple case
    if isinstance(v,(int,float,str)):
        return v
    if np and np.isscalar(v) and hasattr(v, "item"):
        return v.item()
    if isinstance(v, (list, tuple)):
        return [to_scalar_or_list(e) for e in v]
    elif np and isinstance(v, np.ndarray):
        if v.ndim == 0:
            return v.item()
        return [to_scalar_or_list(e) for e in v]
    elif pd and isinstance(v, (pd.Series, pd.Index)):
        return [to_scalar_or_list(e) for e in v]
    elif is_numpy_convertable(v):
        return to_scalar_or_list(np.array(v))
    else:
        return v

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

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

はじめの一歩

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

調査の方向性

packages/python/plotly/_plotly_utils/basevalidators.py の to_scalar_or_list から始め、近くで使われている get_module と is_numpy_convertable を調べてください。ネイティブスカラー、リストまたはタプル、NumPy 配列、pandas の Series または Index の値について、動作とパフォーマンスを比較してください。提案されたケースで既存の変換を維持しながら、不要な繰り返し処理を避けられれば完了です。

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

評価

技術スタック
numpy, pandas, python
領域
performance
issue の種類
リファクタリング
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
38/100

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

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