Inspector `nanmean` divides by the wrong count

Đang mở Phù hợp với người mới
#630 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
86/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
data-visualization

Hướng nghiên cứu

Cả hai trường hợp đều nằm trong plotting/_inspector.py: kiểm tra giá trị trung bình của hình chữ nhật quanh dòng 90–91 và giá trị trung bình của đa giác quanh dòng 128. So sánh mẫu số với các bin được tính vào từng tử số, đồng thời tính đến các bin bị mask và chiều được lập hồ sơ. Tái hiện các ví dụ hình chữ nhật và đa giác đã cung cấp; hoàn thành khi nanmean trả về 1 cho cả hai, còn sum, nansum và cách hoạt động hiện có của mean vẫn chính xác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
12
Fork
7
Merge trung bình
16 giờ 5 phút
Pull request đã merge (30 ngày)
6

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của scipp/plopp

Tất cả issue của scipp/plopp

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.