Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 730ca6b

Browse files
author
Brioch Hemmings
authored
Merge branch 'develop' into develop
2 parents 87f5394 + 036e162 commit 730ca6b

File tree

4 files changed

+84
-78
lines changed

4 files changed

+84
-78
lines changed

autotest/pst_from_tests.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,18 +3674,18 @@ def shortname_conversion_test():
36743674
if os.path.exists(tmp_model_ws):
36753675
shutil.rmtree(tmp_model_ws)
36763676
os.mkdir(tmp_model_ws)
3677-
3678-
np.savetxt(os.path.join(tmp_model_ws,"parfile1"), np.ones([200,600]))
3679-
np.savetxt(os.path.join(tmp_model_ws, "parfile2"), np.ones([200, 600]))
3680-
np.savetxt(os.path.join(tmp_model_ws, "parfile3"), np.ones([200, 600]))
3681-
np.savetxt(os.path.join(tmp_model_ws, "parfile4"), np.ones([200, 600]))
3682-
np.savetxt(os.path.join(tmp_model_ws, "parfile5"), np.ones([200, 600]))
3683-
np.savetxt(os.path.join(tmp_model_ws, "parfile6"), np.ones([200, 600]))
3684-
np.savetxt(os.path.join(tmp_model_ws, "obsfile1"), np.ones([200, 600]))
3685-
np.savetxt(os.path.join(tmp_model_ws, "obsfile2"), np.ones([200, 600]))
3686-
3687-
np.savetxt(os.path.join(tmp_model_ws, "parfile7"), np.ones([200, 600]))
3688-
np.savetxt(os.path.join(tmp_model_ws, "obsfile3"), np.ones([200, 600]))
3677+
dims = (20,60)
3678+
np.savetxt(os.path.join(tmp_model_ws,"parfile1"), np.ones(dims))
3679+
np.savetxt(os.path.join(tmp_model_ws, "parfile2"), np.ones(dims))
3680+
np.savetxt(os.path.join(tmp_model_ws, "parfile3"), np.ones(dims))
3681+
np.savetxt(os.path.join(tmp_model_ws, "parfile4"), np.ones(dims))
3682+
np.savetxt(os.path.join(tmp_model_ws, "parfile5"), np.ones(dims))
3683+
np.savetxt(os.path.join(tmp_model_ws, "parfile6"), np.ones(dims))
3684+
np.savetxt(os.path.join(tmp_model_ws, "obsfile1"), np.ones(dims))
3685+
np.savetxt(os.path.join(tmp_model_ws, "obsfile2"), np.ones(dims))
3686+
3687+
np.savetxt(os.path.join(tmp_model_ws, "parfile7"), np.ones(dims))
3688+
np.savetxt(os.path.join(tmp_model_ws, "obsfile3"), np.ones(dims))
36893689
# SETUP pest stuff...
36903690

36913691
template_ws = "new_temp"
@@ -3695,8 +3695,8 @@ def shortname_conversion_test():
36953695
# (generated by pyemu.gw_utils.setup_hds_obs())
36963696
# pf.add_observations('freyberg.hds.dat', insfile='freyberg.hds.dat.ins2',
36973697
# index_cols='obsnme', use_cols='obsval', prefix='hds')
3698-
sr = pyemu.helpers.SpatialReference(delr=[10]*600,
3699-
delc=[10]*200,
3698+
sr = pyemu.helpers.SpatialReference(delr=[10]*dims[1],
3699+
delc=[10]*dims[0],
37003700
rotation=0,
37013701
epsg=3070,
37023702
xul=0.,
@@ -3726,18 +3726,34 @@ def shortname_conversion_test():
37263726
geostruct=gr_gs)
37273727
pf.add_observations(
37283728
"obsfile1",
3729-
prefix="obs",
3729+
prefix="longobservationname",
37303730
rebuild_pst=False,
3731-
obsgp="og",
3731+
obsgp="longobservationgroup",
37323732
includes_header=False
37333733
)
37343734
pf.add_observations(
37353735
"obsfile2",
3736-
prefix="obs2",
3736+
prefix="longobservationname2",
37373737
rebuild_pst=False,
3738-
obsgp="og2",
3738+
obsgp="longobservationgroup2",
37393739
includes_header=False
37403740
)
3741+
pf.add_observations(
3742+
"obsfile3",
3743+
prefix="longobservationname-lt",
3744+
rebuild_pst=False,
3745+
obsgp="less_longobservationgroup",
3746+
includes_header=False,
3747+
insfile="lt_obsfile3.ins"
3748+
)
3749+
pf.add_observations(
3750+
"obsfile3",
3751+
prefix="longobservationname-gt",
3752+
rebuild_pst=False,
3753+
obsgp="greater_longobservationgroup",
3754+
includes_header=False,
3755+
insfile="gt_obsfile3.ins"
3756+
)
37413757
pst = pf.build_pst()
37423758
obs = set(pst.observation_data.obsnme)
37433759
trie = pyemu.helpers.Trie()
@@ -3747,7 +3763,7 @@ def shortname_conversion_test():
37473763
with open(os.path.join(pf.new_d, ins), "rt") as f:
37483764
obsin = set(rex.findall(f.read()))
37493765
obs = obs - obsin
3750-
assert len(obs) == 0, f"{len(obs)} obs not found in insfiles: {obs}"
3766+
assert len(obs) == 0, f"{len(obs)} obs not found in insfiles: {obs[:100]}..."
37513767

37523768
par = set(pst.parameter_data.parnme)
37533769
trie = pyemu.helpers.Trie()
@@ -3757,13 +3773,13 @@ def shortname_conversion_test():
37573773
with open(os.path.join(pf.new_d, tpl), "rt") as f:
37583774
parin = set(rex.findall(f.read()))
37593775
par = par - parin
3760-
assert len(par) == 0, f"{len(par)} pars not found in tplfiles: {par}"
3776+
assert len(par) == 0, f"{len(par)} pars not found in tplfiles: {par[:100]}..."
37613777
# test update/rebuild
37623778
pf.add_observations(
37633779
"obsfile3",
3764-
prefix="obs3",
3780+
prefix="longobservationname3",
37653781
rebuild_pst=True,
3766-
obsgp="og3",
3782+
obsgp="longobservationgroup3",
37673783
includes_header=False
37683784
)
37693785
pf.add_parameters(filenames="parfile7",
@@ -3780,7 +3796,7 @@ def shortname_conversion_test():
37803796
with open(os.path.join(pf.new_d, ins), "rt") as f:
37813797
obsin = set(rex.findall(f.read()))
37823798
obs = obs - obsin
3783-
assert len(obs) == 0, f"{len(obs)} obs not found in insfiles: {obs}"
3799+
assert len(obs) == 0, f"{len(obs)} obs not found in insfiles: {obs[:100]}..."
37843800

37853801
par = set(pst.parameter_data.parnme)
37863802
parin = set()
@@ -3791,7 +3807,7 @@ def shortname_conversion_test():
37913807
with open(os.path.join(pf.new_d, tpl), "rt") as f:
37923808
parin = set(rex.findall(f.read()))
37933809
par = par - parin
3794-
assert len(par) == 0, f"{len(par)} pars not found in tplfiles: {par}"
3810+
assert len(par) == 0, f"{len(par)} pars not found in tplfiles: {par[:100]}..."
37953811

37963812

37973813
if __name__ == "__main__":

autotest/pst_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ def pst_ctl_opt_args_test():
12961296
#comments_test()
12971297
#test_e_clean()
12981298
# load_test()
1299-
# res_test()
1299+
res_test()
13001300
#
13011301
# from_io_with_inschek_test()
13021302
# pestpp_args_test()
@@ -1323,5 +1323,5 @@ def pst_ctl_opt_args_test():
13231323

13241324
#rename_pars_test()
13251325
#rename_obs_test()
1326-
pst_ctl_opt_args_test()
1326+
# pst_ctl_opt_args_test()
13271327

pyemu/pst/pst_handler.py

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ def from_par_obs_names(cls, par_names=["par1"], obs_names=["obs1"]):
147147
"""
148148
return pst_utils.generic_pst(par_names=par_names, obs_names=obs_names)
149149

150+
@staticmethod
151+
def get_constraint_tags(ltgt='lt'):
152+
if ltgt == 'lt':
153+
return "l_", "less", ">@"
154+
else:
155+
return "g_", "greater", "<@"
156+
150157
@property
151158
def phi(self):
152159
"""get the weighted total objective function.
@@ -207,19 +214,11 @@ def phi_components(self):
207214
# components[og] = np.sum((og_res_df["residual"] *
208215
# og_df["weight"]) ** 2)
209216
mod_vals = og_res_df.loc[og_df.obsnme, "modelled"]
210-
if (
211-
og.lower().startswith("g_")
212-
or og.lower().startswith("greater_")
213-
or og.lower().startswith("<@")
214-
):
217+
if og.lower().startswith(self.get_constraint_tags('gt')):
215218
mod_vals.loc[mod_vals >= og_df.loc[:, "obsval"]] = og_df.loc[
216219
:, "obsval"
217220
]
218-
elif (
219-
og.lower().startswith("l_")
220-
or og.lower().startswith("less_")
221-
or og.lower().startswith(">@")
222-
):
221+
elif og.lower().startswith(self.get_constraint_tags('lt')):
223222
mod_vals.loc[mod_vals <= og_df.loc[:, "obsval"]] = og_df.loc[
224223
:, "obsval"
225224
]
@@ -249,18 +248,10 @@ def phi_components(self):
249248
+ " vs. "
250249
+ str(og_res_df.shape)
251250
)
252-
if (
253-
og.lower().startswith("g_")
254-
or og.lower().startswith("greater_")
255-
or og.lower().startswith("<@")
256-
):
251+
if og.lower().startswith(self.get_constraint_tags('gt')):
257252
gidx = og_res_df.loc[:, "residual"] >= 0
258253
og_res_df.loc[gidx, "residual"] = 0
259-
elif (
260-
og.lower().startswith("l_")
261-
or og.lower().startswith("less_")
262-
or og.lower().startswith(">@")
263-
):
254+
elif og.lower().startswith(self.get_constraint_tags('lt')):
264255
lidx = og_res_df.loc[:, "residual"] <= 0
265256
og_res_df.loc[lidx, "residual"] = 0
266257
components[og] = np.sum((og_res_df["residual"] * og_df["weight"]) ** 2)
@@ -2382,10 +2373,10 @@ def adjust_weights_discrepancy(
23822373
res = self.res.loc[names, :].residual
23832374
og = obs.obgnme
23842375
res.loc[
2385-
(og.str.startswith(("g_", "greater_", "<@"))) &
2376+
(og.str.startswith(self.get_constraint_tags('gt'))) &
23862377
(res <= 0)] = 0
23872378
res.loc[
2388-
(og.str.startswith(("l_", "less_", ">@"))) &
2379+
(og.str.startswith(self.get_constraint_tags('lt'))) &
23892380
(res >= 0)] = 0
23902381
swr = (res * obs.weight) ** 2
23912382
factors = (1.0 / swr).apply(np.sqrt)
@@ -2474,10 +2465,10 @@ def __reset_weights(self, target_phis, res_idxs, obs_idxs):
24742465
).loc[tmpobs.index]
24752466
og = tmpobs.obgnme
24762467
resid.loc[
2477-
(og.str.startswith(("g_", "greater_", "<@"))) &
2468+
(og.str.startswith(self.get_constraint_tags('gt'))) &
24782469
(resid <= 0)] = 0
24792470
resid.loc[
2480-
(og.str.startswith(("l_", "less_", ">@"))) &
2471+
(og.str.startswith(self.get_constraint_tags('lt'))) &
24812472
(resid >= 0)] = 0
24822473

24832474
actual_phi = np.sum(
@@ -3576,10 +3567,10 @@ def write_obs_summary_table(self, filename=None, group_names=None):
35763567
# print("executing {0} in dir {1}".format(cmd_line, cwd))
35773568
# pyemu.utils.os_utils.run(cmd_line,cwd=cwd)
35783569

3579-
@staticmethod
3580-
def _is_less_const(name):
3581-
constraint_tags = ["l_", "less"]
3582-
return True in [True for c in constraint_tags if name.startswith(c)]
3570+
# @staticmethod
3571+
# def _is_less_const(name):
3572+
# constraint_tags = ["l_", "less"]
3573+
# return True in [True for c in constraint_tags if name.startswith(c)]
35833574

35843575
@property
35853576
def less_than_obs_constraints(self):
@@ -3594,13 +3585,10 @@ def less_than_obs_constraints(self):
35943585
Zero-weighted obs are skipped
35953586
35963587
"""
3597-
35983588
obs = self.observation_data
35993589
lt_obs = obs.loc[
3600-
obs.apply(
3601-
lambda x: self._is_less_const(x.obgnme) and x.weight != 0.0, axis=1
3602-
),
3603-
"obsnme",
3590+
obs.obgnme.str.startswith(self.get_constraint_tags('lt')) &
3591+
(obs.weight != 0.0), "obsnme"
36043592
]
36053593
return lt_obs
36063594

@@ -3620,17 +3608,15 @@ def less_than_pi_constraints(self):
36203608

36213609
pi = self.prior_information
36223610
lt_pi = pi.loc[
3623-
pi.apply(
3624-
lambda x: self._is_less_const(x.obgnme) and x.weight != 0.0, axis=1
3625-
),
3626-
"pilbl",
3611+
pi.obgnme.str.startswith(self.get_constraint_tags('lt')) &
3612+
(pi.weight != 0.0), "pilbl"
36273613
]
36283614
return lt_pi
36293615

3630-
@staticmethod
3631-
def _is_greater_const(name):
3632-
constraint_tags = ["g_", "greater"]
3633-
return True in [True for c in constraint_tags if name.startswith(c)]
3616+
# @staticmethod
3617+
# def _is_greater_const(name):
3618+
# constraint_tags = ["g_", "greater"]
3619+
# return True in [True for c in constraint_tags if name.startswith(c)]
36343620

36353621
@property
36363622
def greater_than_obs_constraints(self):
@@ -3648,10 +3634,8 @@ def greater_than_obs_constraints(self):
36483634

36493635
obs = self.observation_data
36503636
gt_obs = obs.loc[
3651-
obs.apply(
3652-
lambda x: self._is_greater_const(x.obgnme) and x.weight != 0.0, axis=1
3653-
),
3654-
"obsnme",
3637+
obs.obgnme.str.startswith(self.get_constraint_tags('gt')) &
3638+
(obs.weight != 0.0), "obsnme"
36553639
]
36563640
return gt_obs
36573641

@@ -3672,11 +3656,9 @@ def greater_than_pi_constraints(self):
36723656

36733657
pi = self.prior_information
36743658
gt_pi = pi.loc[
3675-
pi.apply(
3676-
lambda x: self._is_greater_const(x.obgnme) and x.weight != 0.0, axis=1
3677-
),
3678-
"pilbl",
3679-
]
3659+
pi.obgnme.str.startswith(self.get_constraint_tags('gt')) &
3660+
(pi.weight != 0.0),
3661+
"pilbl"]
36803662
return gt_pi
36813663

36823664
def get_par_change_limits(self):

pyemu/utils/pst_from.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,8 @@ def build_pst(self, filename=None, update=False, version=1):
774774
obs_data_orig = obs_data_orig.set_index(
775775
"longname")
776776
# starting point for updated mapping
777-
shtmx = obs_data_orig.obsnme.str.strip('ob').astype(int).max() + 1
778-
gshtmx = obs_data_orig.obgnme.str.strip('obg').astype(int).max() + 1
777+
shtmx = obs_data_orig.obsnme.str.lstrip('ob').astype(int).max() + 1
778+
gshtmx = obs_data_orig.obgnme.str.lstrip('l_obg').astype(int).max() + 1
779779
# index of new obs (might need this later)
780780
new_obs_data = obs_data.index.difference(obs_data_orig.index)
781781
else:
@@ -824,6 +824,14 @@ def build_pst(self, filename=None, update=False, version=1):
824824
columns=['longname'])
825825
# shortnames from using previous a starting point (if existing)
826826
obgpmap["shortname"] = "obg" + (obgpmap.index+gshtmx).astype(str)
827+
ltobs = obgpmap.longname.str.startswith(
828+
pyemu.Pst.get_constraint_tags('lt')
829+
)
830+
obgpmap.loc[ltobs, "shortname"] = "l_" + obgpmap.loc[ltobs, "shortname"]
831+
gtobs = obgpmap.longname.str.startswith(
832+
pyemu.Pst.get_constraint_tags('gt')
833+
)
834+
obgpmap.loc[gtobs, "shortname"] = "g_" + obgpmap.loc[gtobs, "shortname"]
827835
obgpmap_dict = obgpmap.set_index('longname').shortname.to_dict()
828836
obs_data.loc[nod.index, "oglong"] = nod.obgnme
829837
obs_data.loc[nod.index, 'obgnme'] = nod.obgnme.map(obgpmap_dict)

0 commit comments

Comments
 (0)