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

Comparer/ComparerCollection isel #374

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions modelskill/comparison/_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,25 @@ def sel(

return cc

def isel(
self, model: int | None = None, observation: int | None = None
) -> "ComparerCollection":
"""Select data based on model and observation index.

Parameters
----------
model : int, optional
Model index. If None, all models are selected.
observation : int, optional
Observation index. If None, all observations are selected.

Returns
-------
ComparerCollection
New ComparerCollection with selected data.
"""
return self.sel(model=model, observation=observation)

def filter_by_attrs(self, **kwargs) -> "ComparerCollection":
"""Filter by comparer attrs similar to xarray.Dataset.filter_by_attrs

Expand Down
15 changes: 15 additions & 0 deletions modelskill/comparison/_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,21 @@ def sel(
d = d.isel(time=mask)
return Comparer.from_matched_data(data=d, raw_mod_data=raw_mod_data)

def isel(self, model: int) -> "Comparer":
"""Select data based on model index.

Parameters
----------
model : int
Model index.

Returns
-------
Comparer
New Comparer with selected data.
"""
return self.sel(model=model)

def where(
self,
cond: Union[bool, np.ndarray, xr.DataArray],
Expand Down
7 changes: 7 additions & 0 deletions tests/test_comparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ def test_pc_sel_model_first(pc):
assert np.all(pc2.data.m1 == pc.data.m1)


def test_pc_isel_model_first(pc):
pc2 = pc.isel(model=0)
assert pc2.n_points == 5
assert pc2.n_models == 1
assert np.all(pc2.data.m1 == pc.data.m1)


def test_pc_sel_model_last(pc):
pc2 = pc.sel(model=-1)
assert pc2.n_points == 5
Expand Down
11 changes: 11 additions & 0 deletions tests/test_comparercollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ def test_cc_sel_model_last(cc):
assert cc2.mod_names == ["m3"]


def test_cc_isel_model_last(cc):
cc2 = cc.isel(model=-1)
assert len(cc2) == 1
assert cc2.n_models == 1
assert cc2.n_points == 5
assert cc2.start_time == pd.Timestamp("2019-01-03")
assert cc2.end_time == pd.Timestamp("2019-01-07")
assert cc2.obs_names == ["fake track obs"]
assert cc2.mod_names == ["m3"]


# TODO: FAILS
# def test_cc_sel_time_single(cc):
# cc1 = cc.sel(time="2019-01-03")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_multimodelcompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def test_mm_skill_obs(cc):
assert s.loc["SW_2"].bias == s2.loc["SW_2"].bias


def test_mm_isel(cc):
s2 = cc.isel(observation=-1).skill()
assert s2.loc["SW_2"].bias == pytest.approx(0.081431053)


def test_mm_mean_skill_obs(cc):
df = cc.sel(model=0, observation=[0, "c2"]).mean_skill().to_dataframe()
assert pytest.approx(df.iloc[0].si) == 0.11113215
Expand Down
Loading