Skip to content

Commit f102cfe

Browse files
authored
Merge pull request #26 from jwhite-usgs/develop
Develop
2 parents eac4de8 + 0e6cd92 commit f102cfe

47 files changed

Lines changed: 436 additions & 46 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

benchmarks/basic_tests.py

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,149 @@ def secondary_marker_test():
552552
# raise Exception(e)
553553
os.chdir(b_d)
554554

555+
def sen_basic_test():
556+
local = True
557+
model_d = "sen_invest"
558+
t_d = os.path.join(model_d, "template")
559+
if os.path.exists(t_d):
560+
shutil.rmtree(t_d)
561+
os.makedirs(t_d)
562+
if "linux" in platform.platform().lower() and "10par" in model_d:
563+
# print("travis_prep")
564+
# prep_for_travis(model_d)
565+
local = False
566+
par_names = ["p1","p2"]
567+
obs_names = ["p1","p2","p1+p2","p1*p2","p1^p2","const"]
568+
569+
tpl_file = os.path.join(t_d,"in.dat.tpl")
570+
with open(tpl_file,'w') as f:
571+
f.write("ptf ~\n")
572+
for par_name in par_names:
573+
f.write("{0} ~ {0} ~\n".format(par_name))
574+
ins_file = os.path.join(t_d,"out.dat.ins")
575+
with open(ins_file,'w') as f:
576+
f.write("pif ~\n")
577+
for obs_name in obs_names:
578+
f.write("l1 w !{0}!\n".format(obs_name))
579+
580+
with open(os.path.join(t_d,"forward_run.py"),'w') as f:
581+
f.write("import pandas as pd\n")
582+
f.write("df = pd.read_csv('in.dat',index_col=0,delim_whitespace=True,names=['name','value'])\n")
583+
f.write("df.loc['p1+p2','value'] = df.loc['p1','value'] + df.loc['p2','value']\n")
584+
f.write("df.loc['p1*p2','value'] = df.loc['p1','value'] * df.loc['p2','value']\n")
585+
f.write("df.loc['p1^p2','value'] = df.loc['p1','value'] * df.loc['p2','value']\n")
586+
f.write("df.loc['const','value'] = 1.0\n")
587+
f.write("df.to_csv('out.dat',sep=' ',header=False)\n")
588+
589+
with open(os.path.join(t_d,"in.dat"),'w') as f:
590+
f.write("p1 1.0\n")
591+
f.write("p2 1.0\n")
592+
f.write("p3 1.0\n")
593+
pyemu.os_utils.run("python forward_run.py",cwd=t_d)
594+
595+
pst = pyemu.Pst.from_io_files(tpl_files=tpl_file,in_files=tpl_file.replace(".tpl",""),
596+
ins_files=ins_file,out_files=ins_file.replace(".ins",""))
597+
pst.model_command = "python forward_run.py"
598+
pst.control_data.noptmax = 0
599+
pst.parameter_data.loc[:,"partrans"] = "log"
600+
pst.parameter_data.loc[:,"parchglim"] = "relative"
601+
pst.parameter_data.loc[:,"parubnd"] = 10.0
602+
pst.parameter_data.loc[:,"parlbnd"] = .1
603+
pst.parameter_data.loc[:,"parval1"] = 1.0
604+
605+
msn_file = os.path.join(t_d,"pest.msn")
606+
mio_file = os.path.join(t_d, "pest.mio")
607+
608+
obs = pst.observation_data
609+
obs.loc[:, "weight"] = 0.0
610+
obs.loc["const", "weight"] = 1.0
611+
pst.write(os.path.join(t_d, "pest.pst"))
612+
pyemu.os_utils.run("{0} pest.pst".format(exe_path.replace("-ies", "-sen")), cwd=t_d)
613+
df = pd.read_csv(msn_file, index_col=0)
614+
df.columns = df.columns.map(str.lower)
615+
df.columns = df.columns.map(str.strip)
616+
df.index = df.index.map(str.lower)
617+
print(df)
618+
assert df.sen_mean_abs.sum() == 0.0
619+
assert df.sen_std_dev.sum() == 0.0
620+
df = pd.read_csv(mio_file, index_col=0)
621+
df.columns = df.columns.map(str.lower)
622+
623+
df.loc[:, "parameter_name"] = df.parameter_name.apply(str.lower)
624+
df.index = df.index.map(str.lower)
625+
print(df)
626+
assert df.loc[df.parameter_name == "p2", :].loc["p1", "sen_mean_abs"] == 0
627+
assert df.loc[df.parameter_name == "p1", :].loc["p2", "sen_mean_abs"] == 0
628+
629+
pst.pestpp_options["gsa_method"] = "sobol"
630+
pst.pestpp_options["gsa_sobol_samples"] = 5
631+
pst.write(os.path.join(t_d, "pest.pst"))
632+
#pyemu.os_utils.run("{0} pest.pst".format(exe_path.replace("-ies", "-sen")), cwd=t_d)
633+
m_d = os.path.join(model_d,"master_sobol")
634+
pyemu.os_utils.start_workers(t_d, exe_path.replace("-ies", "-sen"), "pest.pst", 5, master_dir=m_d,
635+
worker_root=model_d, local=local, port=port)
636+
si_vals = pd.read_csv(os.path.join(m_d,"pest.sobol.si.csv"),index_col=0)
637+
sti_vals = pd.read_csv(os.path.join(m_d,"pest.sobol.sti.csv"),index_col=0)
638+
v_d = os.path.join("sen_invest","verf")
639+
si_verf_vals = pd.read_csv(os.path.join(v_d, "si.csv"), index_col=0)
640+
sti_verf_vals = pd.read_csv(os.path.join(v_d, "sti.csv"), index_col=0)
641+
d_si = (si_vals.loc[pst.obs_names,:] - si_verf_vals.loc[pst.obs_names,:]).apply(np.abs)
642+
print(d_si.max())
643+
assert d_si.max().max() < .001
644+
d_sti = (sti_vals.loc[pst.obs_names, :] - sti_verf_vals.loc[pst.obs_names, :]).apply(np.abs)
645+
print(d_sti.max())
646+
assert d_sti.max().max() < .001
647+
648+
649+
def salib_verf():
650+
import pyemu
651+
from SALib.sample import saltelli
652+
from SALib.analyze import sobol
653+
m_d = os.path.join("sen_invest","master_sobol")
654+
v_d = os.path.join("sen_invest","verf")
655+
if os.path.exists(v_d):
656+
shutil.rmtree(v_d)
657+
os.makedirs(v_d)
658+
pst = pyemu.Pst(os.path.join(m_d,"pest.pst"))
659+
pst.add_transform_columns()
660+
bounds = [[l,u] for l,u in zip(pst.parameter_data.parlbnd_trans,pst.parameter_data.parubnd_trans)]
661+
problem = {"num_vars":pst.npar_adj,"names":pst.par_names,"bounds":bounds}
662+
test = saltelli.sample(problem,100,calc_second_order=False)
663+
out_df = pd.read_csv(os.path.join(m_d,"pest.sobol.obs.csv"),index_col=0)
664+
reorder_df = out_df.copy()
665+
idx = [0,3,1,2]
666+
for i in range(4):
667+
s = i*5
668+
e = s + 5
669+
chunk = out_df.iloc[s:e,:].copy()
670+
reorder_df.iloc[idx[i]::4,:] = chunk.values
671+
print(chunk.p1,reorder_df.iloc[idx[i]::4,:].p1)
672+
pass
673+
si_vals = pd.DataFrame(columns=pst.par_names,index=pst.obs_names)
674+
sti_vals = pd.DataFrame(columns=pst.par_names, index=pst.obs_names)
675+
676+
for obs_name in pst.obs_names:
677+
#if obs_name != "p1":
678+
# continue
679+
si = sobol.analyze(problem,reorder_df.loc[:,obs_name].values,calc_second_order=False,num_resamples=5)
680+
print(obs_name,si)
681+
si_vals.loc[obs_name,:] = si["S1"]
682+
sti_vals.loc[obs_name, :] = si["ST"]
683+
si_vals.to_csv(os.path.join(v_d,"si.csv"))
684+
sti_vals.to_csv(os.path.join(v_d, "sti.csv"))
685+
686+
# in_df = pd.read_csv(os.path.join(m_d,"pest.sobol.par.csv"),index_col=0)
687+
# import matplotlib.pyplot as plt
688+
# fig, ax = plt.subplots(1,1)
689+
# print(test)
690+
# test = test ** 10
691+
# print(test)
692+
# ax.scatter(test[:,0],test[:,1],marker='.',color='g')
693+
# ax.scatter(in_df.iloc[:,0],in_df.iloc[:,1],marker='.',color='r')
694+
#
695+
#
696+
# plt.show()
697+
555698
if __name__ == "__main__":
556699
#glm_long_name_test()
557700
#sen_plusplus_test()
@@ -562,4 +705,6 @@ def secondary_marker_test():
562705
#glm_save_binary_test()
563706
#sweep_forgive_test()
564707
#inv_regul_test()
565-
tie_by_group_test()
708+
#tie_by_group_test()
709+
sen_basic_test()
710+
#salib_verf()

benchmarks/sen_invest/verf/si.csv

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
,p1,p2
2+
const,,
3+
p1,2.7117619551246652,0.0
4+
p1*p2,0.4449501446482477,0.020448352797858175
5+
p1+p2,2.4073989587732068,0.08162649405386102
6+
p1^p2,0.4449501446482477,0.020448352797858175
7+
p2,0.0,0.11594422251593291

benchmarks/sen_invest/verf/sti.csv

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
,p1,p2
2+
const,,
3+
p1,1.3097491216563089,0.0
4+
p1*p2,1.0178331575513264,0.0116125826733042
5+
p1+p2,1.1331531697493031,0.034616972227875076
6+
p1^p2,1.0178331575513264,0.0116125826733042
7+
p2,0.0,0.07302673054508874

0 commit comments

Comments
 (0)