Skip to content

Commit

Permalink
Merge pull request #283 from DHI/set_quantity_in_init
Browse files Browse the repository at this point in the history
Set quantity ASAP instead of mutating
  • Loading branch information
ecomodeller authored Nov 22, 2023
2 parents c4f0564 + 27808df commit 88b95fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
45 changes: 26 additions & 19 deletions modelskill/comparison/_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def all(self) -> Sequence[str]:
def parse(
items: List[str],
obs_item: str | int | None = None,
mod_items: Optional[List[str | int]] = None,
aux_items: Optional[List[str | int]] = None,
mod_items: Optional[Iterable[str | int]] = None,
aux_items: Optional[Iterable[str | int]] = None,
) -> ItemSelection:
"""Parse items and return observation, model and auxiliary items
Default behaviour:
Expand Down Expand Up @@ -310,13 +310,14 @@ def _inside_polygon(polygon, xy) -> np.ndarray:

def _matched_data_to_xarray(
df: pd.DataFrame,
obs_item=None,
mod_items=None,
aux_items=None,
name=None,
x=None,
y=None,
z=None,
obs_item: int | str | None = None,
mod_items: Optional[Iterable[str | int]] = None,
aux_items: Optional[Iterable[str | int]] = None,
name: Optional[str] = None,
x: Optional[float] = None,
y: Optional[float] = None,
z: Optional[float] = None,
quantity: Optional[Quantity] = None,
):
"""Convert matched data to accepted xarray.Dataset format"""
assert isinstance(df, pd.DataFrame)
Expand Down Expand Up @@ -346,7 +347,11 @@ def _matched_data_to_xarray(
else:
ds.attrs["gtype"] = str(GeometryType.TRACK)

q = Quantity.undefined()
if quantity is None:
q = Quantity.undefined()
else:
q = quantity

ds["Observation"].attrs["long_name"] = q.name
ds["Observation"].attrs["units"] = q.unit

Expand Down Expand Up @@ -406,15 +411,16 @@ def __init__(
@classmethod
def from_matched_data(
cls,
data,
raw_mod_data=None,
obs_item=None,
mod_items=None,
aux_items=None,
name=None,
x=None,
y=None,
z=None,
data: xr.Dataset | pd.DataFrame,
raw_mod_data: Optional[Dict[str, pd.DataFrame]] = None,
obs_item: str | int | None = None,
mod_items: Optional[Iterable[str | int]] = None,
aux_items: Optional[Iterable[str | int]] = None,
name: Optional[str] = None,
x: Optional[float] = None,
y: Optional[float] = None,
z: Optional[float] = None,
quantity: Optional[Quantity] = None,
):
"""Initialize from compared data"""
if not isinstance(data, xr.Dataset):
Expand All @@ -428,6 +434,7 @@ def from_matched_data(
x=x,
y=y,
z=z,
quantity=quantity,
)
return cls(matched_data=data, raw_mod_data=raw_mod_data)

Expand Down
3 changes: 1 addition & 2 deletions modelskill/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ def from_matched(
x=x,
y=y,
z=z,
quantity=quantity,
)
if quantity is not None:
cmp.quantity = quantity
return cmp


Expand Down

0 comments on commit 88b95fa

Please sign in to comment.