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

Make get_tmin_tmax method work for models and add del_model method #118

Open
wants to merge 5 commits into
base: dev
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
32 changes: 25 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,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):
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
13 changes: 11 additions & 2 deletions tests/test_003_pastastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ def test_iter_stresses(pstore):

@pytest.mark.dependency()
def test_get_tmintmax(pstore):
_ = pstore.get_tmin_tmax("oseries")
_ = pstore.get_tmin_tmax("stresses")
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")
pstore.del_model("oseries1")


@pytest.mark.dependency()
Expand Down