Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Model result and observation constructors stamp `kind` into the Dataset they are handed

Đang mở
#712 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
72/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

Hướng nghiên cứu

Start with TimeSeries.init and _validate_dataset in src/modelskill/timeseries/_timeseries.py, then inspect the five constructor sites listed in the issue and the existing vertical re-wrapping tests. Run tests/observation/test_vertical_obs.py and tests/model/test_vertical.py before adding coverage for a source observation surviving a model-result read. Done means constructors no longer mutate handed-in datasets while existing behavior remains intact.

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

Mô tả

bug

TimeSeries.__init__ (src/modelskill/timeseries/_timeseries.py:171) keeps the dataset it is given, by reference:

self.data = data if self._is_input_validated(data) else _validate_dataset(data)

Each constructor then marks the primary variable's kind in place. On the validated path that variable belongs to the caller, so reading an observation's data turns the observation into a model result:

o = ms.PointObservation(df, x=1.0, y=2.0, item="WL")
o.data["WL"].attrs["kind"]     # 'observation'

mr = ms.PointModelResult(o.data)

o.data["WL"].attrs["kind"]     # 'model'   <- the observation changed
mr.data is o.data              # True

Sites

The same three lines are copy-pasted at five constructors:

  • src/modelskill/model/point.py:69
  • src/modelskill/model/track.py:71
  • src/modelskill/model/vertical.py:113
  • src/modelskill/obs.py:155
  • src/modelskill/model/network.py — fixed on network-phase-2 by copying first

Observation.__init__ (src/modelskill/obs.py:152-165) goes furthest. Beyond kind it rewrites the source's dataset-level attrs, its time variable, the plot color and weight:

data.attrs = {**data.attrs, **(attrs or {})}
data["time"] = self._parse_time(data.time)
data[data_var].attrs["color"] = color
super().__init__(data=data)
self.data.attrs["weight"] = weight

_validate_dataset (_timeseries.py:117-160) also mutates its argument and returns it, so the unvalidated path is only incidentally safe: it is safe because the parse functions built a fresh object, not because anything copied.

Why it has gone unnoticed

Re-wrapping a validated dataset is rare, and the two tests that do it re-wrap same-to-same, where the kind written matches the kind already there: tests/observation/test_vertical_obs.py:184 and tests/model/test_vertical.py:170. Nothing asserts that a source observation survives being read.

DummyModelResult is the one class already safe, and it is safe because it copies — da = observation.data[observation.name].copy() (src/modelskill/model/dummy.py:65).

comparison/_comparison.py:509 is a latent instance: PointModelResult(self.data[[str(key)]], name=str(key)) stamps a variable still shared with Comparer.data, because ds[[var]] shares Variable objects. It is benign only because the kind already reads model.

Suggested fix

A shallow ds.copy() before stamping is enough — xarray rebuilds each variable's attrs dict, so no array data is copied. deep=True is not needed. Note that ds[[var]], ds.sel(...) and ds.isel(...) do not work for this: they share the Variable objects and the write goes through to the parent.

Since the stamp is duplicated five times, one helper that copies and then marks the primary variable would remove the duplication and the aliasing together. _include_attributes and _include_coords (timeseries/_point.py:150, :168) already open with ds = ds.copy(), so the discipline exists in the codebase.

Ngôn ngữ chính
Python
Star
58
Fork
10
Merge trung bình
1 giờ 16 phút
Pull request đã merge (30 ngày)
3

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

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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 DHI/modelskill

Tất cả issue của DHI/modelskill

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.