From 792e32f2b4f861ee03efde9371d5027ba27ec259 Mon Sep 17 00:00:00 2001 From: Martin Vonk Date: Mon, 15 Apr 2024 11:48:38 +0200 Subject: [PATCH 1/6] add models as libname option to get_tmin_tmax --- pastastore/store.py | 32 +++++++++++++++++++++++++------- tests/test_003_pastastore.py | 1 + 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pastastore/store.py b/pastastore/store.py index 5617846..f752938 100644 --- a/pastastore/store.py +++ b/pastastore/store.py @@ -1,7 +1,7 @@ import json import os import warnings -from typing import List, Optional, Tuple, Union +from typing import List, Literal, Optional, Tuple, Union import numpy as np import pandas as pd @@ -384,7 +384,12 @@ def get_signatures( return signatures_df - def get_tmin_tmax(self, libname, names=None, progressbar=False): + def get_tmin_tmax( + self, + libname: Literal["oseries", "stresses", "models"], + names: Union[str, List[str], None] = None, + progressbar: bool = False, + ): """Get tmin and tmax for time series. Parameters @@ -410,12 +415,25 @@ def get_tmin_tmax(self, libname, names=None, progressbar=False): ) desc = f"Get tmin/tmax {libname}" for n in tqdm(names, desc=desc) if progressbar else names: - if libname == "oseries": - s = self.conn.get_oseries(n) + if libname == "models": + mld = self.conn.get_models( + n, + return_dict=True, + progressbar=False, + update_ts_settings=False, + squeeze=True, + ) + tmintmax.loc[n, "tmin"] = mld["settings"]["tmin"] + tmintmax.loc[n, "tmax"] = mld["settings"]["tmax"] else: - s = self.conn.get_stresses(n) - tmintmax.loc[n, "tmin"] = s.first_valid_index() - tmintmax.loc[n, "tmax"] = s.last_valid_index() + s = ( + self.conn.get_oseries(n) + if libname == "oseries" + else self.conn.get_stresses(n) + ) + tmintmax.loc[n, "tmin"] = s.first_valid_index() + tmintmax.loc[n, "tmax"] = s.last_valid_index() + return tmintmax def get_extent(self, libname, names=None, buffer=0.0): diff --git a/tests/test_003_pastastore.py b/tests/test_003_pastastore.py index 4d5672f..99b0f37 100644 --- a/tests/test_003_pastastore.py +++ b/tests/test_003_pastastore.py @@ -25,6 +25,7 @@ def test_iter_stresses(pstore): def test_get_tmintmax(pstore): _ = pstore.get_tmin_tmax("oseries") _ = pstore.get_tmin_tmax("stresses") + _ = pstore.get_tmin_tmax("models") @pytest.mark.dependency() From c425d527e042b84860b362d587e47a6d63b38d26 Mon Sep 17 00:00:00 2001 From: Martin Vonk Date: Mon, 15 Apr 2024 12:09:44 +0200 Subject: [PATCH 2/6] assert in get_tmintmax test --- tests/test_003_pastastore.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_003_pastastore.py b/tests/test_003_pastastore.py index 99b0f37..3ab174e 100644 --- a/tests/test_003_pastastore.py +++ b/tests/test_003_pastastore.py @@ -23,9 +23,16 @@ def test_iter_stresses(pstore): @pytest.mark.dependency() def test_get_tmintmax(pstore): - _ = pstore.get_tmin_tmax("oseries") - _ = pstore.get_tmin_tmax("stresses") - _ = pstore.get_tmin_tmax("models") + print(pstore.model_names) + ostt = pstore.get_tmin_tmax("oseries") + assert ostt.at["oseries1", "tmin"] == pd.Timestamp("2010-01-14") + sttt = pstore.get_tmin_tmax("stresses") + assert sttt.at["evap2", "tmax"] == pd.Timestamp("2016-11-22") + ml = pstore.create_model("oseries1") + ml.solve(report=False) + pstore.conn.add_model(ml) + mltt = pstore.get_tmin_tmax("models") + assert mltt.at["oseries1", "tmax"] == pd.Timestamp("2015-06-28") @pytest.mark.dependency() From 7fdb2be22c86c20b2d83f2f6060c878026e25d25 Mon Sep 17 00:00:00 2001 From: Martin Vonk Date: Mon, 15 Apr 2024 12:11:40 +0200 Subject: [PATCH 3/6] fix isort --- pastastore/yaml_interface.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pastastore/yaml_interface.py b/pastastore/yaml_interface.py index 82abf53..73d1bd1 100644 --- a/pastastore/yaml_interface.py +++ b/pastastore/yaml_interface.py @@ -11,7 +11,6 @@ from pastastore.version import PASTAS_LEQ_022 - logger = logging.getLogger(__name__) From 1aa4823dff8eaa472914e705492b34e30a57b109 Mon Sep 17 00:00:00 2001 From: Martin Vonk Date: Mon, 15 Apr 2024 12:18:40 +0200 Subject: [PATCH 4/6] del_model after tests --- tests/test_003_pastastore.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_003_pastastore.py b/tests/test_003_pastastore.py index 3ab174e..d9e687a 100644 --- a/tests/test_003_pastastore.py +++ b/tests/test_003_pastastore.py @@ -33,6 +33,7 @@ def test_get_tmintmax(pstore): pstore.conn.add_model(ml) mltt = pstore.get_tmin_tmax("models") assert mltt.at["oseries1", "tmax"] == pd.Timestamp("2015-06-28") + pstore.del_model("oseries1") @pytest.mark.dependency() From e9f91ef0440cc11dbc9c27c55f217222579e2ccf Mon Sep 17 00:00:00 2001 From: Martin Vonk Date: Mon, 15 Apr 2024 12:26:45 +0200 Subject: [PATCH 5/6] add del_model method that does the same as del_models --- pastastore/base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pastastore/base.py b/pastastore/base.py index 7434e9a..e0b52dd 100644 --- a/pastastore/base.py +++ b/pastastore/base.py @@ -659,6 +659,16 @@ def del_models(self, names: Union[list, str]) -> None: self._del_oseries_model_link(oname, n) self._clear_cache("_modelnames_cache") + def del_model(self, names: Union[list, str]) -> None: + """Delete model(s) from the database. + + Parameters + ---------- + names : str or list of str + name(s) of the model to delete + """ + self.del_models(names=names) + def del_oseries(self, names: Union[list, str], remove_models: bool = False): """Delete oseries from the database. From b97fa634ae3aed09dbb71e18e480a4ded1a5bc34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dav=C3=ADd=20Brakenhoff?= Date: Tue, 25 Jun 2024 11:49:32 +0200 Subject: [PATCH 6/6] remove unnecessary code remove print statement --- pastastore/store.py | 3 --- tests/test_003_pastastore.py | 1 - 2 files changed, 4 deletions(-) diff --git a/pastastore/store.py b/pastastore/store.py index f752938..f2d10dd 100644 --- a/pastastore/store.py +++ b/pastastore/store.py @@ -419,9 +419,6 @@ def get_tmin_tmax( mld = self.conn.get_models( n, return_dict=True, - progressbar=False, - update_ts_settings=False, - squeeze=True, ) tmintmax.loc[n, "tmin"] = mld["settings"]["tmin"] tmintmax.loc[n, "tmax"] = mld["settings"]["tmax"] diff --git a/tests/test_003_pastastore.py b/tests/test_003_pastastore.py index d9e687a..101c10c 100644 --- a/tests/test_003_pastastore.py +++ b/tests/test_003_pastastore.py @@ -23,7 +23,6 @@ def test_iter_stresses(pstore): @pytest.mark.dependency() def test_get_tmintmax(pstore): - print(pstore.model_names) ostt = pstore.get_tmin_tmax("oseries") assert ostt.at["oseries1", "tmin"] == pd.Timestamp("2010-01-14") sttt = pstore.get_tmin_tmax("stresses")