Skip to content

Commit

Permalink
Merge pull request #118 from pastas/117-make-get_tmin_tmax-work-for-m…
Browse files Browse the repository at this point in the history
…odels-library

Make get_tmin_tmax method work for models and add del_model method
  • Loading branch information
dbrakenhoff authored Jun 25, 2024
2 parents 8f45fb9 + b97fa63 commit 55a45a2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
10 changes: 10 additions & 0 deletions pastastore/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 22 additions & 7 deletions pastastore/store.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -410,12 +415,22 @@ 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,
)
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):
Expand Down
1 change: 0 additions & 1 deletion pastastore/yaml_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from pastastore.version import PASTAS_LEQ_022


logger = logging.getLogger(__name__)


Expand Down
12 changes: 10 additions & 2 deletions tests/test_003_pastastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +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")
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")
pstore.del_model("oseries1")


@pytest.mark.dependency()
Expand Down

0 comments on commit 55a45a2

Please sign in to comment.