Skip to content

Commit b301698

Browse files
authored
Feat normtransf optional extrapolation (#528)
* added optional extrapolation to .prep_for_dsi() * hotfix: remove restrictions on randrealgen * added ztz approach; tweaked tests * update tests * le sigh... * docstrings * tweak to nst interpoaltion * hotfix to update dsi forward run * correction to quadratic extrapolation * more updates to pyemu eds * hack to ensure unique values during nstransform * add flag to skip nst for cases where all obs values are equal * cleaner dsi tests * checkin * hotfix for handling cases where all obs are equal * test to catch failing dsi runs * hotfix to energy threshold * handle uneven nreal in randrealgen
1 parent 2da6803 commit b301698

File tree

3 files changed

+299
-139
lines changed

3 files changed

+299
-139
lines changed

autotest/la_tests.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ def ends_freyberg_test(tmp_path):
607607
ends = pyemu.EnDS(pst=pst, sim_ensemble=oe, obscov=os.path.join(test_d, "obs.unc"),predictions=predictions)
608608

609609

610-
def ends_freyberg_dsi_test(tmp_path):
610+
611+
def ends_run_freyberg_dsi(tmp_path,nst=False,nst_extrap=None,ztz=False,energy=1.0):
611612
import pyemu
612613
import os
613614
test_d = "ends_master"
@@ -621,58 +622,44 @@ def ends_freyberg_dsi_test(tmp_path):
621622
oe_name = pst_name.replace(".pst", ".0.obs.csv")
622623
oe = pyemu.ObservationEnsemble.from_csv(pst=pst, filename=oe_name).iloc[:100, :]
623624

624-
625625
ends = pyemu.EnDS(pst=pst, sim_ensemble=oe,verbose=True)
626626
t_d = os.path.join(tmp_path,"dsi_template")
627-
ends.prep_for_dsi(t_d=t_d)
627+
628+
ends.prep_for_dsi(t_d=t_d,
629+
use_ztz=ztz,
630+
apply_normal_score_transform=nst,
631+
nst_extrap=nst_extrap,
632+
energy=energy)
628633
# copy exe to dsi_template
629634
#shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
630-
635+
filename=os.path.join(t_d,"dsi.0.obs.csv")
636+
if os.path.exists(filename):
637+
os.remove(filename)
631638
pst = pyemu.Pst(os.path.join(t_d,"dsi.pst"))
632-
pst.control_data.noptmax = 0
639+
pst.control_data.noptmax = -1
633640
pst.write(os.path.join(t_d,"dsi.pst"),version=2)
634-
#pyemu.os_utils.run("pestpp-ies dsi.pst",cwd="dsi_template")
635-
m_d = os.path.join(tmp_path,"master_dsi")
636-
pyemu.os_utils.start_workers(t_d,"pestpp-ies","dsi.pst",num_workers=15,worker_root=tmp_path,
637-
master_dir=m_d)
641+
pyemu.os_utils.run("pestpp-ies dsi.pst",cwd=t_d)
638642

639-
# run test wtih truncated svd
640-
ends.prep_for_dsi(t_d=t_d,truncated_svd=True)
641-
# copy exe to dsi_template
642-
#shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
643+
#read in the results
644+
oe = pyemu.ObservationEnsemble.from_csv(pst=pst, filename=os.path.join(t_d,"dsi.0.obs.csv"))
645+
assert oe.shape[0]==50, f"{50-oe.shape} failed runs"
646+
phi_vector = oe.phi_vector.sort_values().values
647+
assert phi_vector[0] != phi_vector[1],phi_vector
643648

644-
pst = pyemu.Pst(os.path.join(t_d,"dsi.pst"))
645-
pst.control_data.noptmax = 0
646-
pst.write(os.path.join(t_d,"dsi.pst"),version=2)
647-
pyemu.os_utils.start_workers(t_d,"pestpp-ies","dsi.pst",num_workers=15,worker_root=tmp_path,
648-
master_dir=m_d)
649+
def ends_freyberg_dsi_test(tmp_path):
650+
ends_run_freyberg_dsi(tmp_path)
649651

652+
def ends_freyberg_dsi_nst_test(tmp_path):
653+
ends_run_freyberg_dsi(tmp_path,nst=True,nst_extrap=None)
650654

651-
# run test wtih normal score transform
652-
ends.prep_for_dsi(t_d=t_d,apply_normal_score_transform=True)
653-
# copy exe to dsi_template
654-
#shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
655-
656-
pst = pyemu.Pst(os.path.join(t_d,"dsi.pst"))
657-
pst.control_data.noptmax = 0
658-
pst.write(os.path.join(t_d,"dsi.pst"),version=2)
659-
pyemu.os_utils.start_workers(t_d,"pestpp-ies","dsi.pst",num_workers=15,worker_root=tmp_path,
660-
master_dir=m_d)
661-
662-
# run test with log-transform
663-
pst = pyemu.Pst(pst_name)
664-
pst.pestpp_options["predictions"] = predictions
665-
pst.observation_data["obstransform"] = "log"
666-
ends = pyemu.EnDS(pst=pst, sim_ensemble=oe,verbose=True)
667-
ends.prep_for_dsi(t_d=t_d,apply_normal_score_transform=False)
668-
# copy exe to dsi_template
669-
#shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
670-
671-
pst = pyemu.Pst(os.path.join(t_d,"dsi.pst"))
672-
pst.control_data.noptmax = 0
673-
pst.write(os.path.join(t_d,"dsi.pst"),version=2)
674-
pyemu.os_utils.start_workers(t_d,"pestpp-ies","dsi.pst",num_workers=15,worker_root=tmp_path,
675-
master_dir=m_d)
655+
def ends_freyberg_dsi_extrap_test(tmp_path):
656+
ends_run_freyberg_dsi(tmp_path,nst=True,nst_extrap='quadratic')
657+
658+
def ends_freyberg_dsi_ztz_test(tmp_path):
659+
ends_run_freyberg_dsi(tmp_path,ztz=True)
660+
661+
def ends_freyberg_dsi_svd_test(tmp_path):
662+
ends_run_freyberg_dsi(tmp_path,ztz=True,energy=0.999)
676663

677664

678665
def plot_freyberg_dsi():
@@ -738,15 +725,28 @@ def dsi_normscoretransform_test():
738725
oe = pyemu.ObservationEnsemble.from_csv(pst=pst, filename=oe_name).iloc[:100, :]
739726

740727
nstval = randrealgen_optimized(oe.shape[0], 1e-7, 1e4)
728+
window_size=3
729+
if oe.shape[0]>40:
730+
window_size=5
731+
if oe.shape[0]>90:
732+
window_size=7
733+
if oe.shape[0]>200:
734+
window_size=9
741735
for name in oe.columns:
742736
print("transforming:",name)
743737
sorted_values = oe._df.loc[:,name].sort_values().copy()
738+
#if all values are the same, skip
739+
if sorted_values.iloc[0] == sorted_values.iloc[-1]:
740+
print("all values are the same, skipping")
741+
continue
742+
sorted_values.loc[:] = pyemu.eds.moving_average_with_endpoints(sorted_values.values, window_size)
744743
transformed_values = np.asarray([normal_score_transform(nstval, sorted_values, value)[0] for value in sorted_values])
745744
backtransformed_values = np.asarray([inverse_normal_score_transform(nstval, sorted_values, value)[0] for value in transformed_values])
746745

747746
diff = backtransformed_values-sorted_values
748747
assert max(abs(diff))<1e-7, backtransformed_values
749748

749+
750750
if __name__ == "__main__":
751751
#dsi_normscoretransform_test()
752752
#ends_freyberg_dev()

0 commit comments

Comments
 (0)