Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/drop method for comparers and collections #460

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions modelskill/comparison/_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,38 @@ def sel(
d = d.isel(time=mask)
return Comparer.from_matched_data(data=d, raw_mod_data=raw_mod_data)

def drop(
self,
model: Optional[IdxOrNameTypes] = None,
) -> "Comparer":
"""Drop specified model(s) from the Comparer.

Parameters
----------
model : str or int or list of str or list of int, optional
Model name or index. If None, all models are selected.

Returns
-------
Comparer
New Comparer excluding specified data.
"""
dropped_cmp = self

if model is not None:
if isinstance(model, (str, int)):
models = [model]
else:
models = list(model)
models_to_drop: List[str] = [_get_name(m, self.mod_names) for m in models]
models_to_keep: List[str] = [
m for m in self.mod_names if m not in models_to_drop
]

dropped_cmp = dropped_cmp.sel(model=models_to_keep)

return dropped_cmp

def where(
self,
cond: Union[bool, np.ndarray, xr.DataArray],
Expand Down
43 changes: 43 additions & 0 deletions tests/test_comparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,49 @@ def test_tc_sel_time_and_area(tc):
assert tc2.data.Observation.values.tolist() == [2.0]


def test_pc_drop_model(pc):
pc2 = pc.drop(model="m2")
assert isinstance(pc2, type(pc))
assert pc2.n_models == pc.n_models - 1
assert "m2" not in pc2.mod_names
assert "m2" not in pc2.raw_mod_data
assert "m2" not in pc2.data
assert np.all(pc.data.m1 == pc2.data.m1)
assert np.all(pc.raw_mod_data["m1"] == pc2.raw_mod_data["m1"])


def test_pc_drop_model_first(pc):
pc2 = pc.drop(model=0)
assert isinstance(pc2, type(pc))
assert pc2.n_models == pc.n_models - 1
assert "m1" not in pc2.mod_names
assert "m1" not in pc2.raw_mod_data
assert "m1" not in pc2.data
assert np.all(pc.data.m2 == pc2.data.m2)
assert np.all(pc.raw_mod_data["m2"] == pc2.raw_mod_data["m2"])


def test_pc_drop_model_last(pc):
pc2 = pc.drop(model=-1)
assert isinstance(pc2, type(pc))
assert pc2.n_models == pc.n_models - 1
assert "m2" not in pc2.mod_names
assert "m2" not in pc2.raw_mod_data
assert "m2" not in pc2.data
assert np.all(pc.data.m1 == pc2.data.m1)
assert np.all(pc.raw_mod_data["m1"] == pc2.raw_mod_data["m1"])


def test_tc_drop_model(tc):
tc2 = tc.drop(model="m2")
assert isinstance(tc2, type(tc))
assert tc2.n_models == tc.n_models - 1
assert "m2" not in tc2.mod_names
assert "m2" not in tc2.raw_mod_data
assert "m2" not in tc2.data
assert np.all(tc.data.m1 == tc2.data.m1)


def test_pc_where(pc):
pc2 = pc.where(pc.data.Observation > 2.5)
assert pc2.n_points == 3
Expand Down
Loading