def test_glg(self): idx = pd.date_range('20000101', '20550101', freq='d') s = pd.Series([x.month + x.day for x in idx], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.glg(key='observations') assert v == 16.
def test_ghg_linear(self): idx = pd.to_datetime(['20160113', '20160115']) s = pd.Series([0., 10.], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.ghg(key='observations', fill_method='linear') assert v == 5.
def test_ghg_len_yearly(self): idx = pd.date_range('20000101', '20550101', freq='d') s = pd.Series(np.ones(len(idx)), index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.ghg(key='observations', output='yearly') assert len(v) == 55
def test_q_gvg_nan(self): idx = pd.to_datetime(['20160820', '20160901', '20161120']) s = pd.Series([0, 5, 10], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.q_gvg(key='observations') assert np.isnan(v)
def test_ghg(self): idx = pd.to_datetime(['20160114', '20160115', '20160128', '20160214']) s = pd.Series([10., 3., 30., 20], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.ghg(key='observations') assert v == 20.
def test_q_gvg(self): idx = pd.to_datetime(['20160320', '20160401', '20160420']) s = pd.Series([0, 5, 10], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.q_gvg(key='observations') assert v == 2.5
def test_q_gxg_nan(self): idx = pd.date_range('20160101', freq='d', periods=3) s = pd.Series([1, 3, np.nan], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.q_ghg(key='observations', q=.5) assert v == 2.
def add_recharge(self, ml: ps.Model, rfunc=ps.Gamma, recharge=ps.rch.Linear(), recharge_name: str = "recharge") -> None: """Add recharge to a pastas model. Uses closest precipitation and evaporation timeseries in database. These are assumed to be labeled with kind = 'prec' or 'evap'. Parameters ---------- ml : pastas.Model pastas.Model object rfunc : pastas.rfunc, optional response function to use for recharge in model, by default ps.Gamma (for different response functions, see pastas documentation) recharge : ps.RechargeModel recharge model to use, default is ps.rch.Linear() recharge_name : str name of the RechargeModel """ # get nearest prec and evap stns names = [] for var in ("prec", "evap"): try: name = self.get_nearest_stresses(ml.oseries.name, kind=var).iloc[0, 0] except AttributeError: msg = "No precipitation or evaporation timeseries found!" raise Exception(msg) if isinstance(name, float): if np.isnan(name): raise ValueError(f"Unable to find nearest '{var}' stress! " "Check X and Y coordinates.") else: names.append(name) if len(names) == 0: msg = "No precipitation or evaporation timeseries found!" raise Exception(msg) # get data tsdict = self.conn.get_stresses(names) stresses = [] for (k, s), setting in zip(tsdict.items(), ("prec", "evap")): metadata = self.conn.get_metadata("stresses", k, as_frame=False) stresses.append( ps.TimeSeries(s, name=k, settings=setting, metadata=metadata)) # add recharge to model rch = ps.RechargeModel(stresses[0], stresses[1], rfunc, name=recharge_name, recharge=recharge, settings=("prec", "evap"), metadata=[i.metadata for i in stresses]) ml.add_stressmodel(rch)
def test_q_glg(self): n = 101 idx = pd.date_range('20160101', freq='d', periods=n) s = pd.Series(np.arange(n), index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.q_glg(key='observations', q=.06) assert v == 6.
def test_gvg(self): idx = pd.to_datetime(['20170314', '20170328', '20170414', '20170428']) s = pd.Series([1., 2., 3., 4], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.gvg(key='observations', fill_method='linear', output='mean') assert v == 2.
def test_glg_fill_limit_null(self): idx = pd.to_datetime(['20170101', '20170131', '20200101']) s = pd.Series(np.ones(len(idx)), index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.glg(key='observations', fill_method='linear', limit=10, output='yearly') assert v.count() == 0
def test_gvg_nan(self): idx = pd.to_datetime(['20170228', '20170428', '20170429']) s = pd.Series([1., 2., 3.], index=idx) ml = Model(s) ml.freq = 'D' v = ml.stats.gvg( key='observations', fill_method=None, output='mean', ) assert np.isnan(v)
def add_recharge(self, ml: ps.Model, rfunc=ps.Gamma) -> None: """Add recharge to a pastas model. Uses closest precipitation and evaporation timeseries in database. These are assumed to be labeled with kind = 'prec' or 'evap'. Parameters ---------- ml : pastas.Model pastas.Model object rfunc : pastas.rfunc, optional response function to use for recharge in model, by default ps.Gamma (for different response functions, see pastas documentation) """ # get nearest prec and evap stns names = [] for var in ("prec", "evap"): try: name = self.get_nearest_stresses( ml.oseries.name, kind=var).iloc[0, 0] except AttributeError: msg = "No precipitation or evaporation timeseries found!" raise Exception(msg) names.append(name) if len(names) == 0: msg = "No precipitation or evaporation timeseries found!" raise Exception(msg) # get data tsdict = self.conn.get_stresses(names) stresses = [] for k, s in tsdict.items(): metadata = self.conn.get_metadata("stresses", k, as_frame=False) s = self.conn._get_dataframe_values("stresses", k, s, metadata=metadata) stresses.append(ps.TimeSeries(s, name=k, metadata=metadata)) # add recharge to model rch = ps.StressModel2(stresses, rfunc, name="recharge", metadata=[i.metadata for i in stresses], settings=("prec", "evap")) ml.add_stressmodel(rch)
def test_q_gxg_series(self): s = pd.read_csv('tests/data/hseries_gxg.csv', index_col=0, header=0, parse_dates=True, dayfirst=True, squeeze=True,) ml = Model(s) ml.freq = 'D' ghg = ml.stats.q_ghg(key='observations') glg = ml.stats.q_glg(key='observations') gvg = ml.stats.q_gvg(key='observations') print('\n') print('calculated GXG\'s percentile method: \n') print(('GHG: {ghg:.2f} m+NAP\n' 'GLG: {glg:.2f} m+NAP\n' 'GVG: {gvg:.2f} m+NAP\n').format( ghg=ghg, glg=glg, gvg=gvg)) print('Menyanthes GXG\'s: \n') print(('GHG: {ghg:.2f} m+NAP\n' 'GLG: {glg:.2f} m+NAP\n' 'GVG: {gvg:.2f} m+NAP\n').format( ghg=-3.23, glg=-3.82, gvg=-3.43))