Inspector `nanmean` divides by the wrong count

オープン 初心者向け
#630 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
86/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

2つのケースはいずれもplotting/_inspector.pyにあります。90~91行目付近の矩形の平均と、128行目付近のポリゴンの平均を調べてください。それぞれの分子に含まれるビンと分母を比較し、マスクされたビンとプロファイル対象の次元を考慮してください。提示された矩形とポリゴンの例を再現してください。nanmeanが両方で1を返し、sum、nansum、既存のmeanの動作が引き続き正しければ完了です。

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

説明

Both cases are in plotting/_inspector.py, in the mean computed by hand
(the workaround for scipp#3841):

  • Rectangle (lines 90–91): the numerator out.nansum(dims) skips
    masked bins, but the denominator (~sc.isnan(out.data)).sum(dims)
    counts them. A region that includes a mask comes out too low.
  • Polygon (line 128): (inside & non_nan).sum() sums over all
    dimensions, including the profiled one, so the result is too small by a
    factor of that dimension's length, even with no masks or NaNs. It also
    counts masked bins.
import numpy as np, scipp as sc
from plopp.plotting._inspector import _mask_outside_polygon, _slice_rectangular_region

da = sc.DataArray(sc.ones(dims=['x', 'y', 'z'], shape=[4, 3, 5]),
                  coords={d: sc.arange(d, float(n)) for d, n in zip('xyz', (4, 3, 5))})
plain = da.copy()
da.masks['m'] = sc.array(dims=['x'], values=[True, False, False, False])

box = {d: {'dim': d, 'value': sc.array(dims=['p'], values=[-1.0, 9.0])} for d in 'xy'}
print(_slice_rectangular_region(da, box, 'nanmean').values)  # 0.75, expected 1

sizes = {'x': 4, 'y': 3}
pts = np.column_stack([sc.broadcast(plain.coords[d], sizes=sizes).values.ravel() for d in 'xy'])
poly = {'x': {'value': sc.array(dims=['v'], values=[-1.0, 9.0, 9.0, -1.0])},
        'y': {'value': sc.array(dims=['v'], values=[-1.0, -1.0, 9.0, 9.0])}}
print(_mask_outside_polygon(plain, poly, points=pts, sizes=sizes, op='nanmean',
                            non_nan=~sc.isnan(plain.data)).values)  # 0.2, expected 1

(These are the arguments inspector passes.) sum and nansum are
correct. With mean, the polygon is right when nothing is masked, but
both shapes count masked bins.

Suggestion: count only the bins the numerator sums, i.e. leave out masked
bins and, for the polygon, sum the count over the two drawn dimensions
only.

Reproduced on plopp 26.11.0, scipp 26.9.0, matplotlib 3.11.2.

主要言語
Python
スター
12
フォーク
7
平均マージ
16時間 5分
マージ済み PR(30日)
6

環境構築

はじめの一歩

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

scipp/plopp のほかの issue

scipp/plopp の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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