From 394cae4e388b4667ae37c276ca8c0d516f4a3b9b Mon Sep 17 00:00:00 2001 From: Ryan Murray <74630349+rywm-dhi@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:14:44 +0200 Subject: [PATCH 1/4] Failing tests for comparer.drop models --- tests/test_comparer.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_comparer.py b/tests/test_comparer.py index 70fee2ebf..7fececf7e 100644 --- a/tests/test_comparer.py +++ b/tests/test_comparer.py @@ -524,6 +524,42 @@ 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_pc_drop_model_error(pc): + with pytest.raises(KeyError): + pc.sel(model="m3") def test_pc_where(pc): pc2 = pc.where(pc.data.Observation > 2.5) assert pc2.n_points == 3 From ebf65eefd03373c344be54553ac483844ebcb97c Mon Sep 17 00:00:00 2001 From: Ryan Murray <74630349+rywm-dhi@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:15:54 +0200 Subject: [PATCH 2/4] Allow comparer to drop models based on sel --- modelskill/comparison/_comparison.py | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/modelskill/comparison/_comparison.py b/modelskill/comparison/_comparison.py index f38f01ff6..92925e548 100644 --- a/modelskill/comparison/_comparison.py +++ b/modelskill/comparison/_comparison.py @@ -907,6 +907,50 @@ 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, + start: Optional[TimeTypes] = None, + end: Optional[TimeTypes] = None, + time: Optional[TimeTypes] = None, + area: Optional[List[float]] = None, + ) -> "Comparer": + """Drop data based on model, time and/or area. + + Parameters + ---------- + model : str or int or list of str or list of int, optional + Model name or index. If None, all models are selected. + start : str or datetime, optional + Start time. If None, all times are selected. + end : str or datetime, optional + End time. If None, all times are selected. + time : str or datetime, optional + Time. If None, all times are selected. + area : list of float, optional + bbox: [x0, y0, x1, y1] or Polygon. If None, all areas 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], From 5d80afcba6fb3f8d6d19c3e8999caaa3a6d01478 Mon Sep 17 00:00:00 2001 From: Ryan Murray <74630349+rywm-dhi@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:59:27 +0200 Subject: [PATCH 3/4] ANot implemented errors for specific comparer.drop args --- modelskill/comparison/_comparison.py | 9 +++++++++ tests/test_comparer.py | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/modelskill/comparison/_comparison.py b/modelskill/comparison/_comparison.py index 92925e548..049d8eb79 100644 --- a/modelskill/comparison/_comparison.py +++ b/modelskill/comparison/_comparison.py @@ -949,6 +949,15 @@ def drop( dropped_cmp = dropped_cmp.sel(model=models_to_keep) + if (start is not None) or (end is not None): + raise NotImplementedError("start and end not implemented yet") + + if time is not None: + raise NotImplementedError("time not implemented yet") + + if area is not None: + raise NotImplementedError("area not implemented yet") + return dropped_cmp def where( diff --git a/tests/test_comparer.py b/tests/test_comparer.py index 7fececf7e..e1de41eab 100644 --- a/tests/test_comparer.py +++ b/tests/test_comparer.py @@ -560,6 +560,12 @@ def test_pc_drop_model_last(pc): def test_pc_drop_model_error(pc): with pytest.raises(KeyError): pc.sel(model="m3") + + +def test_tc_drop_model(tc): + assert False + + def test_pc_where(pc): pc2 = pc.where(pc.data.Observation > 2.5) assert pc2.n_points == 3 @@ -860,7 +866,6 @@ def test_plots_directional(pt_df): def test_from_matched_track_data(): - df = pd.DataFrame( { "lat": [55.0, 55.1], From f950b8770c72744b31852c6c1d5a9aeabe02e4af Mon Sep 17 00:00:00 2001 From: Ryan Murray <74630349+rywm-dhi@users.noreply.github.com> Date: Fri, 25 Oct 2024 13:38:36 +0200 Subject: [PATCH 4/4] Add drop model test for track comparer --- modelskill/comparison/_comparison.py | 23 +---------------------- tests/test_comparer.py | 13 +++++++------ 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/modelskill/comparison/_comparison.py b/modelskill/comparison/_comparison.py index 049d8eb79..f231a32fe 100644 --- a/modelskill/comparison/_comparison.py +++ b/modelskill/comparison/_comparison.py @@ -910,25 +910,13 @@ def sel( def drop( self, model: Optional[IdxOrNameTypes] = None, - start: Optional[TimeTypes] = None, - end: Optional[TimeTypes] = None, - time: Optional[TimeTypes] = None, - area: Optional[List[float]] = None, ) -> "Comparer": - """Drop data based on model, time and/or area. + """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. - start : str or datetime, optional - Start time. If None, all times are selected. - end : str or datetime, optional - End time. If None, all times are selected. - time : str or datetime, optional - Time. If None, all times are selected. - area : list of float, optional - bbox: [x0, y0, x1, y1] or Polygon. If None, all areas are selected. Returns ------- @@ -949,15 +937,6 @@ def drop( dropped_cmp = dropped_cmp.sel(model=models_to_keep) - if (start is not None) or (end is not None): - raise NotImplementedError("start and end not implemented yet") - - if time is not None: - raise NotImplementedError("time not implemented yet") - - if area is not None: - raise NotImplementedError("area not implemented yet") - return dropped_cmp def where( diff --git a/tests/test_comparer.py b/tests/test_comparer.py index e1de41eab..a8d459ffc 100644 --- a/tests/test_comparer.py +++ b/tests/test_comparer.py @@ -557,13 +557,14 @@ def test_pc_drop_model_last(pc): assert np.all(pc.raw_mod_data["m1"] == pc2.raw_mod_data["m1"]) -def test_pc_drop_model_error(pc): - with pytest.raises(KeyError): - pc.sel(model="m3") - - def test_tc_drop_model(tc): - assert False + 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):